scannerc.go 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718
  1. package yaml
  2. import (
  3. "bytes"
  4. "fmt"
  5. )
  6. // Introduction
  7. // ************
  8. //
  9. // The following notes assume that you are familiar with the YAML specification
  10. // (http://yaml.org/spec/1.2/spec.html). We mostly follow it, although in
  11. // some cases we are less restrictive that it requires.
  12. //
  13. // The process of transforming a YAML stream into a sequence of events is
  14. // divided on two steps: Scanning and Parsing.
  15. //
  16. // The Scanner transforms the input stream into a sequence of tokens, while the
  17. // parser transform the sequence of tokens produced by the Scanner into a
  18. // sequence of parsing events.
  19. //
  20. // The Scanner is rather clever and complicated. The Parser, on the contrary,
  21. // is a straightforward implementation of a recursive-descendant parser (or,
  22. // LL(1) parser, as it is usually called).
  23. //
  24. // Actually there are two issues of Scanning that might be called "clever", the
  25. // rest is quite straightforward. The issues are "block collection start" and
  26. // "simple keys". Both issues are explained below in details.
  27. //
  28. // Here the Scanning step is explained and implemented. We start with the list
  29. // of all the tokens produced by the Scanner together with short descriptions.
  30. //
  31. // Now, tokens:
  32. //
  33. // STREAM-START(encoding) # The stream start.
  34. // STREAM-END # The stream end.
  35. // VERSION-DIRECTIVE(major,minor) # The '%YAML' directive.
  36. // TAG-DIRECTIVE(handle,prefix) # The '%TAG' directive.
  37. // DOCUMENT-START # '---'
  38. // DOCUMENT-END # '...'
  39. // BLOCK-SEQUENCE-START # Indentation increase denoting a block
  40. // BLOCK-MAPPING-START # sequence or a block mapping.
  41. // BLOCK-END # Indentation decrease.
  42. // FLOW-SEQUENCE-START # '['
  43. // FLOW-SEQUENCE-END # ']'
  44. // BLOCK-SEQUENCE-START # '{'
  45. // BLOCK-SEQUENCE-END # '}'
  46. // BLOCK-ENTRY # '-'
  47. // FLOW-ENTRY # ','
  48. // KEY # '?' or nothing (simple keys).
  49. // VALUE # ':'
  50. // ALIAS(anchor) # '*anchor'
  51. // ANCHOR(anchor) # '&anchor'
  52. // TAG(handle,suffix) # '!handle!suffix'
  53. // SCALAR(value,style) # A scalar.
  54. //
  55. // The following two tokens are "virtual" tokens denoting the beginning and the
  56. // end of the stream:
  57. //
  58. // STREAM-START(encoding)
  59. // STREAM-END
  60. //
  61. // We pass the information about the input stream encoding with the
  62. // STREAM-START token.
  63. //
  64. // The next two tokens are responsible for tags:
  65. //
  66. // VERSION-DIRECTIVE(major,minor)
  67. // TAG-DIRECTIVE(handle,prefix)
  68. //
  69. // Example:
  70. //
  71. // %YAML 1.1
  72. // %TAG ! !foo
  73. // %TAG !yaml! tag:yaml.org,2002:
  74. // ---
  75. //
  76. // The correspoding sequence of tokens:
  77. //
  78. // STREAM-START(utf-8)
  79. // VERSION-DIRECTIVE(1,1)
  80. // TAG-DIRECTIVE("!","!foo")
  81. // TAG-DIRECTIVE("!yaml","tag:yaml.org,2002:")
  82. // DOCUMENT-START
  83. // STREAM-END
  84. //
  85. // Note that the VERSION-DIRECTIVE and TAG-DIRECTIVE tokens occupy a whole
  86. // line.
  87. //
  88. // The document start and end indicators are represented by:
  89. //
  90. // DOCUMENT-START
  91. // DOCUMENT-END
  92. //
  93. // Note that if a YAML stream contains an implicit document (without '---'
  94. // and '...' indicators), no DOCUMENT-START and DOCUMENT-END tokens will be
  95. // produced.
  96. //
  97. // In the following examples, we present whole documents together with the
  98. // produced tokens.
  99. //
  100. // 1. An implicit document:
  101. //
  102. // 'a scalar'
  103. //
  104. // Tokens:
  105. //
  106. // STREAM-START(utf-8)
  107. // SCALAR("a scalar",single-quoted)
  108. // STREAM-END
  109. //
  110. // 2. An explicit document:
  111. //
  112. // ---
  113. // 'a scalar'
  114. // ...
  115. //
  116. // Tokens:
  117. //
  118. // STREAM-START(utf-8)
  119. // DOCUMENT-START
  120. // SCALAR("a scalar",single-quoted)
  121. // DOCUMENT-END
  122. // STREAM-END
  123. //
  124. // 3. Several documents in a stream:
  125. //
  126. // 'a scalar'
  127. // ---
  128. // 'another scalar'
  129. // ---
  130. // 'yet another scalar'
  131. //
  132. // Tokens:
  133. //
  134. // STREAM-START(utf-8)
  135. // SCALAR("a scalar",single-quoted)
  136. // DOCUMENT-START
  137. // SCALAR("another scalar",single-quoted)
  138. // DOCUMENT-START
  139. // SCALAR("yet another scalar",single-quoted)
  140. // STREAM-END
  141. //
  142. // We have already introduced the SCALAR token above. The following tokens are
  143. // used to describe aliases, anchors, tag, and scalars:
  144. //
  145. // ALIAS(anchor)
  146. // ANCHOR(anchor)
  147. // TAG(handle,suffix)
  148. // SCALAR(value,style)
  149. //
  150. // The following series of examples illustrate the usage of these tokens:
  151. //
  152. // 1. A recursive sequence:
  153. //
  154. // &A [ *A ]
  155. //
  156. // Tokens:
  157. //
  158. // STREAM-START(utf-8)
  159. // ANCHOR("A")
  160. // FLOW-SEQUENCE-START
  161. // ALIAS("A")
  162. // FLOW-SEQUENCE-END
  163. // STREAM-END
  164. //
  165. // 2. A tagged scalar:
  166. //
  167. // !!float "3.14" # A good approximation.
  168. //
  169. // Tokens:
  170. //
  171. // STREAM-START(utf-8)
  172. // TAG("!!","float")
  173. // SCALAR("3.14",double-quoted)
  174. // STREAM-END
  175. //
  176. // 3. Various scalar styles:
  177. //
  178. // --- # Implicit empty plain scalars do not produce tokens.
  179. // --- a plain scalar
  180. // --- 'a single-quoted scalar'
  181. // --- "a double-quoted scalar"
  182. // --- |-
  183. // a literal scalar
  184. // --- >-
  185. // a folded
  186. // scalar
  187. //
  188. // Tokens:
  189. //
  190. // STREAM-START(utf-8)
  191. // DOCUMENT-START
  192. // DOCUMENT-START
  193. // SCALAR("a plain scalar",plain)
  194. // DOCUMENT-START
  195. // SCALAR("a single-quoted scalar",single-quoted)
  196. // DOCUMENT-START
  197. // SCALAR("a double-quoted scalar",double-quoted)
  198. // DOCUMENT-START
  199. // SCALAR("a literal scalar",literal)
  200. // DOCUMENT-START
  201. // SCALAR("a folded scalar",folded)
  202. // STREAM-END
  203. //
  204. // Now it's time to review collection-related tokens. We will start with
  205. // flow collections:
  206. //
  207. // FLOW-SEQUENCE-START
  208. // FLOW-SEQUENCE-END
  209. // FLOW-MAPPING-START
  210. // FLOW-MAPPING-END
  211. // FLOW-ENTRY
  212. // KEY
  213. // VALUE
  214. //
  215. // The tokens FLOW-SEQUENCE-START, FLOW-SEQUENCE-END, FLOW-MAPPING-START, and
  216. // FLOW-MAPPING-END represent the indicators '[', ']', '{', and '}'
  217. // correspondingly. FLOW-ENTRY represent the ',' indicator. Finally the
  218. // indicators '?' and ':', which are used for denoting mapping keys and values,
  219. // are represented by the KEY and VALUE tokens.
  220. //
  221. // The following examples show flow collections:
  222. //
  223. // 1. A flow sequence:
  224. //
  225. // [item 1, item 2, item 3]
  226. //
  227. // Tokens:
  228. //
  229. // STREAM-START(utf-8)
  230. // FLOW-SEQUENCE-START
  231. // SCALAR("item 1",plain)
  232. // FLOW-ENTRY
  233. // SCALAR("item 2",plain)
  234. // FLOW-ENTRY
  235. // SCALAR("item 3",plain)
  236. // FLOW-SEQUENCE-END
  237. // STREAM-END
  238. //
  239. // 2. A flow mapping:
  240. //
  241. // {
  242. // a simple key: a value, # Note that the KEY token is produced.
  243. // ? a complex key: another value,
  244. // }
  245. //
  246. // Tokens:
  247. //
  248. // STREAM-START(utf-8)
  249. // FLOW-MAPPING-START
  250. // KEY
  251. // SCALAR("a simple key",plain)
  252. // VALUE
  253. // SCALAR("a value",plain)
  254. // FLOW-ENTRY
  255. // KEY
  256. // SCALAR("a complex key",plain)
  257. // VALUE
  258. // SCALAR("another value",plain)
  259. // FLOW-ENTRY
  260. // FLOW-MAPPING-END
  261. // STREAM-END
  262. //
  263. // A simple key is a key which is not denoted by the '?' indicator. Note that
  264. // the Scanner still produce the KEY token whenever it encounters a simple key.
  265. //
  266. // For scanning block collections, the following tokens are used (note that we
  267. // repeat KEY and VALUE here):
  268. //
  269. // BLOCK-SEQUENCE-START
  270. // BLOCK-MAPPING-START
  271. // BLOCK-END
  272. // BLOCK-ENTRY
  273. // KEY
  274. // VALUE
  275. //
  276. // The tokens BLOCK-SEQUENCE-START and BLOCK-MAPPING-START denote indentation
  277. // increase that precedes a block collection (cf. the INDENT token in Python).
  278. // The token BLOCK-END denote indentation decrease that ends a block collection
  279. // (cf. the DEDENT token in Python). However YAML has some syntax pecularities
  280. // that makes detections of these tokens more complex.
  281. //
  282. // The tokens BLOCK-ENTRY, KEY, and VALUE are used to represent the indicators
  283. // '-', '?', and ':' correspondingly.
  284. //
  285. // The following examples show how the tokens BLOCK-SEQUENCE-START,
  286. // BLOCK-MAPPING-START, and BLOCK-END are emitted by the Scanner:
  287. //
  288. // 1. Block sequences:
  289. //
  290. // - item 1
  291. // - item 2
  292. // -
  293. // - item 3.1
  294. // - item 3.2
  295. // -
  296. // key 1: value 1
  297. // key 2: value 2
  298. //
  299. // Tokens:
  300. //
  301. // STREAM-START(utf-8)
  302. // BLOCK-SEQUENCE-START
  303. // BLOCK-ENTRY
  304. // SCALAR("item 1",plain)
  305. // BLOCK-ENTRY
  306. // SCALAR("item 2",plain)
  307. // BLOCK-ENTRY
  308. // BLOCK-SEQUENCE-START
  309. // BLOCK-ENTRY
  310. // SCALAR("item 3.1",plain)
  311. // BLOCK-ENTRY
  312. // SCALAR("item 3.2",plain)
  313. // BLOCK-END
  314. // BLOCK-ENTRY
  315. // BLOCK-MAPPING-START
  316. // KEY
  317. // SCALAR("key 1",plain)
  318. // VALUE
  319. // SCALAR("value 1",plain)
  320. // KEY
  321. // SCALAR("key 2",plain)
  322. // VALUE
  323. // SCALAR("value 2",plain)
  324. // BLOCK-END
  325. // BLOCK-END
  326. // STREAM-END
  327. //
  328. // 2. Block mappings:
  329. //
  330. // a simple key: a value # The KEY token is produced here.
  331. // ? a complex key
  332. // : another value
  333. // a mapping:
  334. // key 1: value 1
  335. // key 2: value 2
  336. // a sequence:
  337. // - item 1
  338. // - item 2
  339. //
  340. // Tokens:
  341. //
  342. // STREAM-START(utf-8)
  343. // BLOCK-MAPPING-START
  344. // KEY
  345. // SCALAR("a simple key",plain)
  346. // VALUE
  347. // SCALAR("a value",plain)
  348. // KEY
  349. // SCALAR("a complex key",plain)
  350. // VALUE
  351. // SCALAR("another value",plain)
  352. // KEY
  353. // SCALAR("a mapping",plain)
  354. // BLOCK-MAPPING-START
  355. // KEY
  356. // SCALAR("key 1",plain)
  357. // VALUE
  358. // SCALAR("value 1",plain)
  359. // KEY
  360. // SCALAR("key 2",plain)
  361. // VALUE
  362. // SCALAR("value 2",plain)
  363. // BLOCK-END
  364. // KEY
  365. // SCALAR("a sequence",plain)
  366. // VALUE
  367. // BLOCK-SEQUENCE-START
  368. // BLOCK-ENTRY
  369. // SCALAR("item 1",plain)
  370. // BLOCK-ENTRY
  371. // SCALAR("item 2",plain)
  372. // BLOCK-END
  373. // BLOCK-END
  374. // STREAM-END
  375. //
  376. // YAML does not always require to start a new block collection from a new
  377. // line. If the current line contains only '-', '?', and ':' indicators, a new
  378. // block collection may start at the current line. The following examples
  379. // illustrate this case:
  380. //
  381. // 1. Collections in a sequence:
  382. //
  383. // - - item 1
  384. // - item 2
  385. // - key 1: value 1
  386. // key 2: value 2
  387. // - ? complex key
  388. // : complex value
  389. //
  390. // Tokens:
  391. //
  392. // STREAM-START(utf-8)
  393. // BLOCK-SEQUENCE-START
  394. // BLOCK-ENTRY
  395. // BLOCK-SEQUENCE-START
  396. // BLOCK-ENTRY
  397. // SCALAR("item 1",plain)
  398. // BLOCK-ENTRY
  399. // SCALAR("item 2",plain)
  400. // BLOCK-END
  401. // BLOCK-ENTRY
  402. // BLOCK-MAPPING-START
  403. // KEY
  404. // SCALAR("key 1",plain)
  405. // VALUE
  406. // SCALAR("value 1",plain)
  407. // KEY
  408. // SCALAR("key 2",plain)
  409. // VALUE
  410. // SCALAR("value 2",plain)
  411. // BLOCK-END
  412. // BLOCK-ENTRY
  413. // BLOCK-MAPPING-START
  414. // KEY
  415. // SCALAR("complex key")
  416. // VALUE
  417. // SCALAR("complex value")
  418. // BLOCK-END
  419. // BLOCK-END
  420. // STREAM-END
  421. //
  422. // 2. Collections in a mapping:
  423. //
  424. // ? a sequence
  425. // : - item 1
  426. // - item 2
  427. // ? a mapping
  428. // : key 1: value 1
  429. // key 2: value 2
  430. //
  431. // Tokens:
  432. //
  433. // STREAM-START(utf-8)
  434. // BLOCK-MAPPING-START
  435. // KEY
  436. // SCALAR("a sequence",plain)
  437. // VALUE
  438. // BLOCK-SEQUENCE-START
  439. // BLOCK-ENTRY
  440. // SCALAR("item 1",plain)
  441. // BLOCK-ENTRY
  442. // SCALAR("item 2",plain)
  443. // BLOCK-END
  444. // KEY
  445. // SCALAR("a mapping",plain)
  446. // VALUE
  447. // BLOCK-MAPPING-START
  448. // KEY
  449. // SCALAR("key 1",plain)
  450. // VALUE
  451. // SCALAR("value 1",plain)
  452. // KEY
  453. // SCALAR("key 2",plain)
  454. // VALUE
  455. // SCALAR("value 2",plain)
  456. // BLOCK-END
  457. // BLOCK-END
  458. // STREAM-END
  459. //
  460. // YAML also permits non-indented sequences if they are included into a block
  461. // mapping. In this case, the token BLOCK-SEQUENCE-START is not produced:
  462. //
  463. // key:
  464. // - item 1 # BLOCK-SEQUENCE-START is NOT produced here.
  465. // - item 2
  466. //
  467. // Tokens:
  468. //
  469. // STREAM-START(utf-8)
  470. // BLOCK-MAPPING-START
  471. // KEY
  472. // SCALAR("key",plain)
  473. // VALUE
  474. // BLOCK-ENTRY
  475. // SCALAR("item 1",plain)
  476. // BLOCK-ENTRY
  477. // SCALAR("item 2",plain)
  478. // BLOCK-END
  479. //
  480. // Ensure that the buffer contains the required number of characters.
  481. // Return true on success, false on failure (reader error or memory error).
  482. func cache(parser *yaml_parser_t, length int) bool {
  483. // [Go] This was inlined: !cache(A, B) -> unread < B && !update(A, B)
  484. return parser.unread >= length || yaml_parser_update_buffer(parser, length)
  485. }
  486. // Advance the buffer pointer.
  487. func skip(parser *yaml_parser_t) {
  488. parser.mark.index++
  489. parser.mark.column++
  490. parser.unread--
  491. parser.buffer_pos += width(parser.buffer[parser.buffer_pos])
  492. }
  493. func skip_line(parser *yaml_parser_t) {
  494. if is_crlf(parser.buffer, parser.buffer_pos) {
  495. parser.mark.index += 2
  496. parser.mark.column = 0
  497. parser.mark.line++
  498. parser.unread -= 2
  499. parser.buffer_pos += 2
  500. } else if is_break(parser.buffer, parser.buffer_pos) {
  501. parser.mark.index++
  502. parser.mark.column = 0
  503. parser.mark.line++
  504. parser.unread--
  505. parser.buffer_pos += width(parser.buffer[parser.buffer_pos])
  506. }
  507. }
  508. // Copy a character to a string buffer and advance pointers.
  509. func read(parser *yaml_parser_t, s []byte) []byte {
  510. w := width(parser.buffer[parser.buffer_pos])
  511. if w == 0 {
  512. panic("invalid character sequence")
  513. }
  514. if len(s) == 0 {
  515. s = make([]byte, 0, 32)
  516. }
  517. if w == 1 && len(s)+w <= cap(s) {
  518. s = s[:len(s)+1]
  519. s[len(s)-1] = parser.buffer[parser.buffer_pos]
  520. parser.buffer_pos++
  521. } else {
  522. s = append(s, parser.buffer[parser.buffer_pos:parser.buffer_pos+w]...)
  523. parser.buffer_pos += w
  524. }
  525. parser.mark.index++
  526. parser.mark.column++
  527. parser.unread--
  528. return s
  529. }
  530. // Copy a line break character to a string buffer and advance pointers.
  531. func read_line(parser *yaml_parser_t, s []byte) []byte {
  532. buf := parser.buffer
  533. pos := parser.buffer_pos
  534. switch {
  535. case buf[pos] == '\r' && buf[pos+1] == '\n':
  536. // CR LF . LF
  537. s = append(s, '\n')
  538. parser.buffer_pos += 2
  539. parser.mark.index++
  540. parser.unread--
  541. case buf[pos] == '\r' || buf[pos] == '\n':
  542. // CR|LF . LF
  543. s = append(s, '\n')
  544. parser.buffer_pos += 1
  545. case buf[pos] == '\xC2' && buf[pos+1] == '\x85':
  546. // NEL . LF
  547. s = append(s, '\n')
  548. parser.buffer_pos += 2
  549. case buf[pos] == '\xE2' && buf[pos+1] == '\x80' && (buf[pos+2] == '\xA8' || buf[pos+2] == '\xA9'):
  550. // LS|PS . LS|PS
  551. s = append(s, buf[parser.buffer_pos:pos+3]...)
  552. parser.buffer_pos += 3
  553. default:
  554. return s
  555. }
  556. parser.mark.index++
  557. parser.mark.column = 0
  558. parser.mark.line++
  559. parser.unread--
  560. return s
  561. }
  562. // Get the next token.
  563. func yaml_parser_scan(parser *yaml_parser_t, token *yaml_token_t) bool {
  564. // Erase the token object.
  565. *token = yaml_token_t{} // [Go] Is this necessary?
  566. // No tokens after STREAM-END or error.
  567. if parser.stream_end_produced || parser.error != yaml_NO_ERROR {
  568. return true
  569. }
  570. // Ensure that the tokens queue contains enough tokens.
  571. if !parser.token_available {
  572. if !yaml_parser_fetch_more_tokens(parser) {
  573. return false
  574. }
  575. }
  576. // Fetch the next token from the queue.
  577. *token = parser.tokens[parser.tokens_head]
  578. parser.tokens_head++
  579. parser.tokens_parsed++
  580. parser.token_available = false
  581. if token.typ == yaml_STREAM_END_TOKEN {
  582. parser.stream_end_produced = true
  583. }
  584. return true
  585. }
  586. // Set the scanner error and return false.
  587. func yaml_parser_set_scanner_error(parser *yaml_parser_t, context string, context_mark yaml_mark_t, problem string) bool {
  588. parser.error = yaml_SCANNER_ERROR
  589. parser.context = context
  590. parser.context_mark = context_mark
  591. parser.problem = problem
  592. parser.problem_mark = parser.mark
  593. return false
  594. }
  595. func yaml_parser_set_scanner_tag_error(parser *yaml_parser_t, directive bool, context_mark yaml_mark_t, problem string) bool {
  596. context := "while parsing a tag"
  597. if directive {
  598. context = "while parsing a %TAG directive"
  599. }
  600. return yaml_parser_set_scanner_error(parser, context, context_mark, problem)
  601. }
  602. func trace(args ...interface{}) func() {
  603. pargs := append([]interface{}{"+++"}, args...)
  604. fmt.Println(pargs...)
  605. pargs = append([]interface{}{"---"}, args...)
  606. return func() { fmt.Println(pargs...) }
  607. }
  608. // Ensure that the tokens queue contains at least one token which can be
  609. // returned to the Parser.
  610. func yaml_parser_fetch_more_tokens(parser *yaml_parser_t) bool {
  611. // While we need more tokens to fetch, do it.
  612. for {
  613. // Check if we really need to fetch more tokens.
  614. need_more_tokens := false
  615. if parser.tokens_head == len(parser.tokens) {
  616. // Queue is empty.
  617. need_more_tokens = true
  618. } else {
  619. // Check if any potential simple key may occupy the head position.
  620. for i := len(parser.simple_keys) - 1; i >= 0; i-- {
  621. simple_key := &parser.simple_keys[i]
  622. if simple_key.token_number < parser.tokens_parsed {
  623. break
  624. }
  625. if valid, ok := yaml_simple_key_is_valid(parser, simple_key); !ok {
  626. return false
  627. } else if valid && simple_key.token_number == parser.tokens_parsed {
  628. need_more_tokens = true
  629. break
  630. }
  631. }
  632. }
  633. // We are finished.
  634. if !need_more_tokens {
  635. break
  636. }
  637. // Fetch the next token.
  638. if !yaml_parser_fetch_next_token(parser) {
  639. return false
  640. }
  641. }
  642. parser.token_available = true
  643. return true
  644. }
  645. // The dispatcher for token fetchers.
  646. func yaml_parser_fetch_next_token(parser *yaml_parser_t) bool {
  647. // Ensure that the buffer is initialized.
  648. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  649. return false
  650. }
  651. // Check if we just started scanning. Fetch STREAM-START then.
  652. if !parser.stream_start_produced {
  653. return yaml_parser_fetch_stream_start(parser)
  654. }
  655. // Eat whitespaces and comments until we reach the next token.
  656. if !yaml_parser_scan_to_next_token(parser) {
  657. return false
  658. }
  659. // Check the indentation level against the current column.
  660. if !yaml_parser_unroll_indent(parser, parser.mark.column) {
  661. return false
  662. }
  663. // Ensure that the buffer contains at least 4 characters. 4 is the length
  664. // of the longest indicators ('--- ' and '... ').
  665. if parser.unread < 4 && !yaml_parser_update_buffer(parser, 4) {
  666. return false
  667. }
  668. // Is it the end of the stream?
  669. if is_z(parser.buffer, parser.buffer_pos) {
  670. return yaml_parser_fetch_stream_end(parser)
  671. }
  672. // Is it a directive?
  673. if parser.mark.column == 0 && parser.buffer[parser.buffer_pos] == '%' {
  674. return yaml_parser_fetch_directive(parser)
  675. }
  676. buf := parser.buffer
  677. pos := parser.buffer_pos
  678. // Is it the document start indicator?
  679. if parser.mark.column == 0 && buf[pos] == '-' && buf[pos+1] == '-' && buf[pos+2] == '-' && is_blankz(buf, pos+3) {
  680. return yaml_parser_fetch_document_indicator(parser, yaml_DOCUMENT_START_TOKEN)
  681. }
  682. // Is it the document end indicator?
  683. if parser.mark.column == 0 && buf[pos] == '.' && buf[pos+1] == '.' && buf[pos+2] == '.' && is_blankz(buf, pos+3) {
  684. return yaml_parser_fetch_document_indicator(parser, yaml_DOCUMENT_END_TOKEN)
  685. }
  686. // Is it the flow sequence start indicator?
  687. if buf[pos] == '[' {
  688. return yaml_parser_fetch_flow_collection_start(parser, yaml_FLOW_SEQUENCE_START_TOKEN)
  689. }
  690. // Is it the flow mapping start indicator?
  691. if parser.buffer[parser.buffer_pos] == '{' {
  692. return yaml_parser_fetch_flow_collection_start(parser, yaml_FLOW_MAPPING_START_TOKEN)
  693. }
  694. // Is it the flow sequence end indicator?
  695. if parser.buffer[parser.buffer_pos] == ']' {
  696. return yaml_parser_fetch_flow_collection_end(parser,
  697. yaml_FLOW_SEQUENCE_END_TOKEN)
  698. }
  699. // Is it the flow mapping end indicator?
  700. if parser.buffer[parser.buffer_pos] == '}' {
  701. return yaml_parser_fetch_flow_collection_end(parser,
  702. yaml_FLOW_MAPPING_END_TOKEN)
  703. }
  704. // Is it the flow entry indicator?
  705. if parser.buffer[parser.buffer_pos] == ',' {
  706. return yaml_parser_fetch_flow_entry(parser)
  707. }
  708. // Is it the block entry indicator?
  709. if parser.buffer[parser.buffer_pos] == '-' && is_blankz(parser.buffer, parser.buffer_pos+1) {
  710. return yaml_parser_fetch_block_entry(parser)
  711. }
  712. // Is it the key indicator?
  713. if parser.buffer[parser.buffer_pos] == '?' && (parser.flow_level > 0 || is_blankz(parser.buffer, parser.buffer_pos+1)) {
  714. return yaml_parser_fetch_key(parser)
  715. }
  716. // Is it the value indicator?
  717. if parser.buffer[parser.buffer_pos] == ':' && (parser.flow_level > 0 || is_blankz(parser.buffer, parser.buffer_pos+1)) {
  718. return yaml_parser_fetch_value(parser)
  719. }
  720. // Is it an alias?
  721. if parser.buffer[parser.buffer_pos] == '*' {
  722. return yaml_parser_fetch_anchor(parser, yaml_ALIAS_TOKEN)
  723. }
  724. // Is it an anchor?
  725. if parser.buffer[parser.buffer_pos] == '&' {
  726. return yaml_parser_fetch_anchor(parser, yaml_ANCHOR_TOKEN)
  727. }
  728. // Is it a tag?
  729. if parser.buffer[parser.buffer_pos] == '!' {
  730. return yaml_parser_fetch_tag(parser)
  731. }
  732. // Is it a literal scalar?
  733. if parser.buffer[parser.buffer_pos] == '|' && parser.flow_level == 0 {
  734. return yaml_parser_fetch_block_scalar(parser, true)
  735. }
  736. // Is it a folded scalar?
  737. if parser.buffer[parser.buffer_pos] == '>' && parser.flow_level == 0 {
  738. return yaml_parser_fetch_block_scalar(parser, false)
  739. }
  740. // Is it a single-quoted scalar?
  741. if parser.buffer[parser.buffer_pos] == '\'' {
  742. return yaml_parser_fetch_flow_scalar(parser, true)
  743. }
  744. // Is it a double-quoted scalar?
  745. if parser.buffer[parser.buffer_pos] == '"' {
  746. return yaml_parser_fetch_flow_scalar(parser, false)
  747. }
  748. // Is it a plain scalar?
  749. //
  750. // A plain scalar may start with any non-blank characters except
  751. //
  752. // '-', '?', ':', ',', '[', ']', '{', '}',
  753. // '#', '&', '*', '!', '|', '>', '\'', '\"',
  754. // '%', '@', '`'.
  755. //
  756. // In the block context (and, for the '-' indicator, in the flow context
  757. // too), it may also start with the characters
  758. //
  759. // '-', '?', ':'
  760. //
  761. // if it is followed by a non-space character.
  762. //
  763. // The last rule is more restrictive than the specification requires.
  764. // [Go] Make this logic more reasonable.
  765. //switch parser.buffer[parser.buffer_pos] {
  766. //case '-', '?', ':', ',', '?', '-', ',', ':', ']', '[', '}', '{', '&', '#', '!', '*', '>', '|', '"', '\'', '@', '%', '-', '`':
  767. //}
  768. if !(is_blankz(parser.buffer, parser.buffer_pos) || parser.buffer[parser.buffer_pos] == '-' ||
  769. parser.buffer[parser.buffer_pos] == '?' || parser.buffer[parser.buffer_pos] == ':' ||
  770. parser.buffer[parser.buffer_pos] == ',' || parser.buffer[parser.buffer_pos] == '[' ||
  771. parser.buffer[parser.buffer_pos] == ']' || parser.buffer[parser.buffer_pos] == '{' ||
  772. parser.buffer[parser.buffer_pos] == '}' || parser.buffer[parser.buffer_pos] == '#' ||
  773. parser.buffer[parser.buffer_pos] == '&' || parser.buffer[parser.buffer_pos] == '*' ||
  774. parser.buffer[parser.buffer_pos] == '!' || parser.buffer[parser.buffer_pos] == '|' ||
  775. parser.buffer[parser.buffer_pos] == '>' || parser.buffer[parser.buffer_pos] == '\'' ||
  776. parser.buffer[parser.buffer_pos] == '"' || parser.buffer[parser.buffer_pos] == '%' ||
  777. parser.buffer[parser.buffer_pos] == '@' || parser.buffer[parser.buffer_pos] == '`') ||
  778. (parser.buffer[parser.buffer_pos] == '-' && !is_blank(parser.buffer, parser.buffer_pos+1)) ||
  779. (parser.flow_level == 0 &&
  780. (parser.buffer[parser.buffer_pos] == '?' || parser.buffer[parser.buffer_pos] == ':') &&
  781. !is_blankz(parser.buffer, parser.buffer_pos+1)) {
  782. return yaml_parser_fetch_plain_scalar(parser)
  783. }
  784. // If we don't determine the token type so far, it is an error.
  785. return yaml_parser_set_scanner_error(parser,
  786. "while scanning for the next token", parser.mark,
  787. "found character that cannot start any token")
  788. }
  789. func yaml_simple_key_is_valid(parser *yaml_parser_t, simple_key *yaml_simple_key_t) (valid, ok bool) {
  790. if !simple_key.possible {
  791. return false, true
  792. }
  793. // The 1.2 specification says:
  794. //
  795. // "If the ? indicator is omitted, parsing needs to see past the
  796. // implicit key to recognize it as such. To limit the amount of
  797. // lookahead required, the “:” indicator must appear at most 1024
  798. // Unicode characters beyond the start of the key. In addition, the key
  799. // is restricted to a single line."
  800. //
  801. if simple_key.mark.line < parser.mark.line || simple_key.mark.index+1024 < parser.mark.index {
  802. // Check if the potential simple key to be removed is required.
  803. if simple_key.required {
  804. return false, yaml_parser_set_scanner_error(parser,
  805. "while scanning a simple key", simple_key.mark,
  806. "could not find expected ':'")
  807. }
  808. simple_key.possible = false
  809. return false, true
  810. }
  811. return true, true
  812. }
  813. // Check if a simple key may start at the current position and add it if
  814. // needed.
  815. func yaml_parser_save_simple_key(parser *yaml_parser_t) bool {
  816. // A simple key is required at the current position if the scanner is in
  817. // the block context and the current column coincides with the indentation
  818. // level.
  819. required := parser.flow_level == 0 && parser.indent == parser.mark.column
  820. //
  821. // If the current position may start a simple key, save it.
  822. //
  823. if parser.simple_key_allowed {
  824. simple_key := yaml_simple_key_t{
  825. possible: true,
  826. required: required,
  827. token_number: parser.tokens_parsed + (len(parser.tokens) - parser.tokens_head),
  828. mark: parser.mark,
  829. }
  830. if !yaml_parser_remove_simple_key(parser) {
  831. return false
  832. }
  833. parser.simple_keys[len(parser.simple_keys)-1] = simple_key
  834. }
  835. return true
  836. }
  837. // Remove a potential simple key at the current flow level.
  838. func yaml_parser_remove_simple_key(parser *yaml_parser_t) bool {
  839. i := len(parser.simple_keys) - 1
  840. if parser.simple_keys[i].possible {
  841. // If the key is required, it is an error.
  842. if parser.simple_keys[i].required {
  843. return yaml_parser_set_scanner_error(parser,
  844. "while scanning a simple key", parser.simple_keys[i].mark,
  845. "could not find expected ':'")
  846. }
  847. }
  848. // Remove the key from the stack.
  849. parser.simple_keys[i].possible = false
  850. return true
  851. }
  852. // max_flow_level limits the flow_level
  853. const max_flow_level = 10000
  854. // Increase the flow level and resize the simple key list if needed.
  855. func yaml_parser_increase_flow_level(parser *yaml_parser_t) bool {
  856. // Reset the simple key on the next level.
  857. parser.simple_keys = append(parser.simple_keys, yaml_simple_key_t{
  858. possible: false,
  859. required: false,
  860. token_number: parser.tokens_parsed + (len(parser.tokens) - parser.tokens_head),
  861. mark: parser.mark,
  862. })
  863. // Increase the flow level.
  864. parser.flow_level++
  865. if parser.flow_level > max_flow_level {
  866. return yaml_parser_set_scanner_error(parser,
  867. "while increasing flow level", parser.simple_keys[len(parser.simple_keys)-1].mark,
  868. fmt.Sprintf("exceeded max depth of %d", max_flow_level))
  869. }
  870. return true
  871. }
  872. // Decrease the flow level.
  873. func yaml_parser_decrease_flow_level(parser *yaml_parser_t) bool {
  874. if parser.flow_level > 0 {
  875. parser.flow_level--
  876. parser.simple_keys = parser.simple_keys[:len(parser.simple_keys)-1]
  877. }
  878. return true
  879. }
  880. // max_indents limits the indents stack size
  881. const max_indents = 10000
  882. // Push the current indentation level to the stack and set the new level
  883. // the current column is greater than the indentation level. In this case,
  884. // append or insert the specified token into the token queue.
  885. func yaml_parser_roll_indent(parser *yaml_parser_t, column, number int, typ yaml_token_type_t, mark yaml_mark_t) bool {
  886. // In the flow context, do nothing.
  887. if parser.flow_level > 0 {
  888. return true
  889. }
  890. if parser.indent < column {
  891. // Push the current indentation level to the stack and set the new
  892. // indentation level.
  893. parser.indents = append(parser.indents, parser.indent)
  894. parser.indent = column
  895. if len(parser.indents) > max_indents {
  896. return yaml_parser_set_scanner_error(parser,
  897. "while increasing indent level", parser.simple_keys[len(parser.simple_keys)-1].mark,
  898. fmt.Sprintf("exceeded max depth of %d", max_indents))
  899. }
  900. // Create a token and insert it into the queue.
  901. token := yaml_token_t{
  902. typ: typ,
  903. start_mark: mark,
  904. end_mark: mark,
  905. }
  906. if number > -1 {
  907. number -= parser.tokens_parsed
  908. }
  909. yaml_insert_token(parser, number, &token)
  910. }
  911. return true
  912. }
  913. // Pop indentation levels from the indents stack until the current level
  914. // becomes less or equal to the column. For each indentation level, append
  915. // the BLOCK-END token.
  916. func yaml_parser_unroll_indent(parser *yaml_parser_t, column int) bool {
  917. // In the flow context, do nothing.
  918. if parser.flow_level > 0 {
  919. return true
  920. }
  921. // Loop through the indentation levels in the stack.
  922. for parser.indent > column {
  923. // Create a token and append it to the queue.
  924. token := yaml_token_t{
  925. typ: yaml_BLOCK_END_TOKEN,
  926. start_mark: parser.mark,
  927. end_mark: parser.mark,
  928. }
  929. yaml_insert_token(parser, -1, &token)
  930. // Pop the indentation level.
  931. parser.indent = parser.indents[len(parser.indents)-1]
  932. parser.indents = parser.indents[:len(parser.indents)-1]
  933. }
  934. return true
  935. }
  936. // Initialize the scanner and produce the STREAM-START token.
  937. func yaml_parser_fetch_stream_start(parser *yaml_parser_t) bool {
  938. // Set the initial indentation.
  939. parser.indent = -1
  940. // Initialize the simple key stack.
  941. parser.simple_keys = append(parser.simple_keys, yaml_simple_key_t{})
  942. // A simple key is allowed at the beginning of the stream.
  943. parser.simple_key_allowed = true
  944. // We have started.
  945. parser.stream_start_produced = true
  946. // Create the STREAM-START token and append it to the queue.
  947. token := yaml_token_t{
  948. typ: yaml_STREAM_START_TOKEN,
  949. start_mark: parser.mark,
  950. end_mark: parser.mark,
  951. encoding: parser.encoding,
  952. }
  953. yaml_insert_token(parser, -1, &token)
  954. return true
  955. }
  956. // Produce the STREAM-END token and shut down the scanner.
  957. func yaml_parser_fetch_stream_end(parser *yaml_parser_t) bool {
  958. // Force new line.
  959. if parser.mark.column != 0 {
  960. parser.mark.column = 0
  961. parser.mark.line++
  962. }
  963. // Reset the indentation level.
  964. if !yaml_parser_unroll_indent(parser, -1) {
  965. return false
  966. }
  967. // Reset simple keys.
  968. if !yaml_parser_remove_simple_key(parser) {
  969. return false
  970. }
  971. parser.simple_key_allowed = false
  972. // Create the STREAM-END token and append it to the queue.
  973. token := yaml_token_t{
  974. typ: yaml_STREAM_END_TOKEN,
  975. start_mark: parser.mark,
  976. end_mark: parser.mark,
  977. }
  978. yaml_insert_token(parser, -1, &token)
  979. return true
  980. }
  981. // Produce a VERSION-DIRECTIVE or TAG-DIRECTIVE token.
  982. func yaml_parser_fetch_directive(parser *yaml_parser_t) bool {
  983. // Reset the indentation level.
  984. if !yaml_parser_unroll_indent(parser, -1) {
  985. return false
  986. }
  987. // Reset simple keys.
  988. if !yaml_parser_remove_simple_key(parser) {
  989. return false
  990. }
  991. parser.simple_key_allowed = false
  992. // Create the YAML-DIRECTIVE or TAG-DIRECTIVE token.
  993. token := yaml_token_t{}
  994. if !yaml_parser_scan_directive(parser, &token) {
  995. return false
  996. }
  997. // Append the token to the queue.
  998. yaml_insert_token(parser, -1, &token)
  999. return true
  1000. }
  1001. // Produce the DOCUMENT-START or DOCUMENT-END token.
  1002. func yaml_parser_fetch_document_indicator(parser *yaml_parser_t, typ yaml_token_type_t) bool {
  1003. // Reset the indentation level.
  1004. if !yaml_parser_unroll_indent(parser, -1) {
  1005. return false
  1006. }
  1007. // Reset simple keys.
  1008. if !yaml_parser_remove_simple_key(parser) {
  1009. return false
  1010. }
  1011. parser.simple_key_allowed = false
  1012. // Consume the token.
  1013. start_mark := parser.mark
  1014. skip(parser)
  1015. skip(parser)
  1016. skip(parser)
  1017. end_mark := parser.mark
  1018. // Create the DOCUMENT-START or DOCUMENT-END token.
  1019. token := yaml_token_t{
  1020. typ: typ,
  1021. start_mark: start_mark,
  1022. end_mark: end_mark,
  1023. }
  1024. // Append the token to the queue.
  1025. yaml_insert_token(parser, -1, &token)
  1026. return true
  1027. }
  1028. // Produce the FLOW-SEQUENCE-START or FLOW-MAPPING-START token.
  1029. func yaml_parser_fetch_flow_collection_start(parser *yaml_parser_t, typ yaml_token_type_t) bool {
  1030. // The indicators '[' and '{' may start a simple key.
  1031. if !yaml_parser_save_simple_key(parser) {
  1032. return false
  1033. }
  1034. // Increase the flow level.
  1035. if !yaml_parser_increase_flow_level(parser) {
  1036. return false
  1037. }
  1038. // A simple key may follow the indicators '[' and '{'.
  1039. parser.simple_key_allowed = true
  1040. // Consume the token.
  1041. start_mark := parser.mark
  1042. skip(parser)
  1043. end_mark := parser.mark
  1044. // Create the FLOW-SEQUENCE-START of FLOW-MAPPING-START token.
  1045. token := yaml_token_t{
  1046. typ: typ,
  1047. start_mark: start_mark,
  1048. end_mark: end_mark,
  1049. }
  1050. // Append the token to the queue.
  1051. yaml_insert_token(parser, -1, &token)
  1052. return true
  1053. }
  1054. // Produce the FLOW-SEQUENCE-END or FLOW-MAPPING-END token.
  1055. func yaml_parser_fetch_flow_collection_end(parser *yaml_parser_t, typ yaml_token_type_t) bool {
  1056. // Reset any potential simple key on the current flow level.
  1057. if !yaml_parser_remove_simple_key(parser) {
  1058. return false
  1059. }
  1060. // Decrease the flow level.
  1061. if !yaml_parser_decrease_flow_level(parser) {
  1062. return false
  1063. }
  1064. // No simple keys after the indicators ']' and '}'.
  1065. parser.simple_key_allowed = false
  1066. // Consume the token.
  1067. start_mark := parser.mark
  1068. skip(parser)
  1069. end_mark := parser.mark
  1070. // Create the FLOW-SEQUENCE-END of FLOW-MAPPING-END token.
  1071. token := yaml_token_t{
  1072. typ: typ,
  1073. start_mark: start_mark,
  1074. end_mark: end_mark,
  1075. }
  1076. // Append the token to the queue.
  1077. yaml_insert_token(parser, -1, &token)
  1078. return true
  1079. }
  1080. // Produce the FLOW-ENTRY token.
  1081. func yaml_parser_fetch_flow_entry(parser *yaml_parser_t) bool {
  1082. // Reset any potential simple keys on the current flow level.
  1083. if !yaml_parser_remove_simple_key(parser) {
  1084. return false
  1085. }
  1086. // Simple keys are allowed after ','.
  1087. parser.simple_key_allowed = true
  1088. // Consume the token.
  1089. start_mark := parser.mark
  1090. skip(parser)
  1091. end_mark := parser.mark
  1092. // Create the FLOW-ENTRY token and append it to the queue.
  1093. token := yaml_token_t{
  1094. typ: yaml_FLOW_ENTRY_TOKEN,
  1095. start_mark: start_mark,
  1096. end_mark: end_mark,
  1097. }
  1098. yaml_insert_token(parser, -1, &token)
  1099. return true
  1100. }
  1101. // Produce the BLOCK-ENTRY token.
  1102. func yaml_parser_fetch_block_entry(parser *yaml_parser_t) bool {
  1103. // Check if the scanner is in the block context.
  1104. if parser.flow_level == 0 {
  1105. // Check if we are allowed to start a new entry.
  1106. if !parser.simple_key_allowed {
  1107. return yaml_parser_set_scanner_error(parser, "", parser.mark,
  1108. "block sequence entries are not allowed in this context")
  1109. }
  1110. // Add the BLOCK-SEQUENCE-START token if needed.
  1111. if !yaml_parser_roll_indent(parser, parser.mark.column, -1, yaml_BLOCK_SEQUENCE_START_TOKEN, parser.mark) {
  1112. return false
  1113. }
  1114. } else {
  1115. // It is an error for the '-' indicator to occur in the flow context,
  1116. // but we let the Parser detect and report about it because the Parser
  1117. // is able to point to the context.
  1118. }
  1119. // Reset any potential simple keys on the current flow level.
  1120. if !yaml_parser_remove_simple_key(parser) {
  1121. return false
  1122. }
  1123. // Simple keys are allowed after '-'.
  1124. parser.simple_key_allowed = true
  1125. // Consume the token.
  1126. start_mark := parser.mark
  1127. skip(parser)
  1128. end_mark := parser.mark
  1129. // Create the BLOCK-ENTRY token and append it to the queue.
  1130. token := yaml_token_t{
  1131. typ: yaml_BLOCK_ENTRY_TOKEN,
  1132. start_mark: start_mark,
  1133. end_mark: end_mark,
  1134. }
  1135. yaml_insert_token(parser, -1, &token)
  1136. return true
  1137. }
  1138. // Produce the KEY token.
  1139. func yaml_parser_fetch_key(parser *yaml_parser_t) bool {
  1140. // In the block context, additional checks are required.
  1141. if parser.flow_level == 0 {
  1142. // Check if we are allowed to start a new key (not nessesary simple).
  1143. if !parser.simple_key_allowed {
  1144. return yaml_parser_set_scanner_error(parser, "", parser.mark,
  1145. "mapping keys are not allowed in this context")
  1146. }
  1147. // Add the BLOCK-MAPPING-START token if needed.
  1148. if !yaml_parser_roll_indent(parser, parser.mark.column, -1, yaml_BLOCK_MAPPING_START_TOKEN, parser.mark) {
  1149. return false
  1150. }
  1151. }
  1152. // Reset any potential simple keys on the current flow level.
  1153. if !yaml_parser_remove_simple_key(parser) {
  1154. return false
  1155. }
  1156. // Simple keys are allowed after '?' in the block context.
  1157. parser.simple_key_allowed = parser.flow_level == 0
  1158. // Consume the token.
  1159. start_mark := parser.mark
  1160. skip(parser)
  1161. end_mark := parser.mark
  1162. // Create the KEY token and append it to the queue.
  1163. token := yaml_token_t{
  1164. typ: yaml_KEY_TOKEN,
  1165. start_mark: start_mark,
  1166. end_mark: end_mark,
  1167. }
  1168. yaml_insert_token(parser, -1, &token)
  1169. return true
  1170. }
  1171. // Produce the VALUE token.
  1172. func yaml_parser_fetch_value(parser *yaml_parser_t) bool {
  1173. simple_key := &parser.simple_keys[len(parser.simple_keys)-1]
  1174. // Have we found a simple key?
  1175. if valid, ok := yaml_simple_key_is_valid(parser, simple_key); !ok {
  1176. return false
  1177. } else if valid {
  1178. // Create the KEY token and insert it into the queue.
  1179. token := yaml_token_t{
  1180. typ: yaml_KEY_TOKEN,
  1181. start_mark: simple_key.mark,
  1182. end_mark: simple_key.mark,
  1183. }
  1184. yaml_insert_token(parser, simple_key.token_number-parser.tokens_parsed, &token)
  1185. // In the block context, we may need to add the BLOCK-MAPPING-START token.
  1186. if !yaml_parser_roll_indent(parser, simple_key.mark.column,
  1187. simple_key.token_number,
  1188. yaml_BLOCK_MAPPING_START_TOKEN, simple_key.mark) {
  1189. return false
  1190. }
  1191. // Remove the simple key.
  1192. simple_key.possible = false
  1193. // A simple key cannot follow another simple key.
  1194. parser.simple_key_allowed = false
  1195. } else {
  1196. // The ':' indicator follows a complex key.
  1197. // In the block context, extra checks are required.
  1198. if parser.flow_level == 0 {
  1199. // Check if we are allowed to start a complex value.
  1200. if !parser.simple_key_allowed {
  1201. return yaml_parser_set_scanner_error(parser, "", parser.mark,
  1202. "mapping values are not allowed in this context")
  1203. }
  1204. // Add the BLOCK-MAPPING-START token if needed.
  1205. if !yaml_parser_roll_indent(parser, parser.mark.column, -1, yaml_BLOCK_MAPPING_START_TOKEN, parser.mark) {
  1206. return false
  1207. }
  1208. }
  1209. // Simple keys after ':' are allowed in the block context.
  1210. parser.simple_key_allowed = parser.flow_level == 0
  1211. }
  1212. // Consume the token.
  1213. start_mark := parser.mark
  1214. skip(parser)
  1215. end_mark := parser.mark
  1216. // Create the VALUE token and append it to the queue.
  1217. token := yaml_token_t{
  1218. typ: yaml_VALUE_TOKEN,
  1219. start_mark: start_mark,
  1220. end_mark: end_mark,
  1221. }
  1222. yaml_insert_token(parser, -1, &token)
  1223. return true
  1224. }
  1225. // Produce the ALIAS or ANCHOR token.
  1226. func yaml_parser_fetch_anchor(parser *yaml_parser_t, typ yaml_token_type_t) bool {
  1227. // An anchor or an alias could be a simple key.
  1228. if !yaml_parser_save_simple_key(parser) {
  1229. return false
  1230. }
  1231. // A simple key cannot follow an anchor or an alias.
  1232. parser.simple_key_allowed = false
  1233. // Create the ALIAS or ANCHOR token and append it to the queue.
  1234. var token yaml_token_t
  1235. if !yaml_parser_scan_anchor(parser, &token, typ) {
  1236. return false
  1237. }
  1238. yaml_insert_token(parser, -1, &token)
  1239. return true
  1240. }
  1241. // Produce the TAG token.
  1242. func yaml_parser_fetch_tag(parser *yaml_parser_t) bool {
  1243. // A tag could be a simple key.
  1244. if !yaml_parser_save_simple_key(parser) {
  1245. return false
  1246. }
  1247. // A simple key cannot follow a tag.
  1248. parser.simple_key_allowed = false
  1249. // Create the TAG token and append it to the queue.
  1250. var token yaml_token_t
  1251. if !yaml_parser_scan_tag(parser, &token) {
  1252. return false
  1253. }
  1254. yaml_insert_token(parser, -1, &token)
  1255. return true
  1256. }
  1257. // Produce the SCALAR(...,literal) or SCALAR(...,folded) tokens.
  1258. func yaml_parser_fetch_block_scalar(parser *yaml_parser_t, literal bool) bool {
  1259. // Remove any potential simple keys.
  1260. if !yaml_parser_remove_simple_key(parser) {
  1261. return false
  1262. }
  1263. // A simple key may follow a block scalar.
  1264. parser.simple_key_allowed = true
  1265. // Create the SCALAR token and append it to the queue.
  1266. var token yaml_token_t
  1267. if !yaml_parser_scan_block_scalar(parser, &token, literal) {
  1268. return false
  1269. }
  1270. yaml_insert_token(parser, -1, &token)
  1271. return true
  1272. }
  1273. // Produce the SCALAR(...,single-quoted) or SCALAR(...,double-quoted) tokens.
  1274. func yaml_parser_fetch_flow_scalar(parser *yaml_parser_t, single bool) bool {
  1275. // A plain scalar could be a simple key.
  1276. if !yaml_parser_save_simple_key(parser) {
  1277. return false
  1278. }
  1279. // A simple key cannot follow a flow scalar.
  1280. parser.simple_key_allowed = false
  1281. // Create the SCALAR token and append it to the queue.
  1282. var token yaml_token_t
  1283. if !yaml_parser_scan_flow_scalar(parser, &token, single) {
  1284. return false
  1285. }
  1286. yaml_insert_token(parser, -1, &token)
  1287. return true
  1288. }
  1289. // Produce the SCALAR(...,plain) token.
  1290. func yaml_parser_fetch_plain_scalar(parser *yaml_parser_t) bool {
  1291. // A plain scalar could be a simple key.
  1292. if !yaml_parser_save_simple_key(parser) {
  1293. return false
  1294. }
  1295. // A simple key cannot follow a flow scalar.
  1296. parser.simple_key_allowed = false
  1297. // Create the SCALAR token and append it to the queue.
  1298. var token yaml_token_t
  1299. if !yaml_parser_scan_plain_scalar(parser, &token) {
  1300. return false
  1301. }
  1302. yaml_insert_token(parser, -1, &token)
  1303. return true
  1304. }
  1305. // Eat whitespaces and comments until the next token is found.
  1306. func yaml_parser_scan_to_next_token(parser *yaml_parser_t) bool {
  1307. // Until the next token is not found.
  1308. for {
  1309. // Allow the BOM mark to start a line.
  1310. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1311. return false
  1312. }
  1313. if parser.mark.column == 0 && is_bom(parser.buffer, parser.buffer_pos) {
  1314. skip(parser)
  1315. }
  1316. // Eat whitespaces.
  1317. // Tabs are allowed:
  1318. // - in the flow context
  1319. // - in the block context, but not at the beginning of the line or
  1320. // after '-', '?', or ':' (complex value).
  1321. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1322. return false
  1323. }
  1324. for parser.buffer[parser.buffer_pos] == ' ' || ((parser.flow_level > 0 || !parser.simple_key_allowed) && parser.buffer[parser.buffer_pos] == '\t') {
  1325. skip(parser)
  1326. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1327. return false
  1328. }
  1329. }
  1330. // Eat a comment until a line break.
  1331. if parser.buffer[parser.buffer_pos] == '#' {
  1332. for !is_breakz(parser.buffer, parser.buffer_pos) {
  1333. skip(parser)
  1334. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1335. return false
  1336. }
  1337. }
  1338. }
  1339. // If it is a line break, eat it.
  1340. if is_break(parser.buffer, parser.buffer_pos) {
  1341. if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
  1342. return false
  1343. }
  1344. skip_line(parser)
  1345. // In the block context, a new line may start a simple key.
  1346. if parser.flow_level == 0 {
  1347. parser.simple_key_allowed = true
  1348. }
  1349. } else {
  1350. break // We have found a token.
  1351. }
  1352. }
  1353. return true
  1354. }
  1355. // Scan a YAML-DIRECTIVE or TAG-DIRECTIVE token.
  1356. //
  1357. // Scope:
  1358. // %YAML 1.1 # a comment \n
  1359. // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1360. // %TAG !yaml! tag:yaml.org,2002: \n
  1361. // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1362. //
  1363. func yaml_parser_scan_directive(parser *yaml_parser_t, token *yaml_token_t) bool {
  1364. // Eat '%'.
  1365. start_mark := parser.mark
  1366. skip(parser)
  1367. // Scan the directive name.
  1368. var name []byte
  1369. if !yaml_parser_scan_directive_name(parser, start_mark, &name) {
  1370. return false
  1371. }
  1372. // Is it a YAML directive?
  1373. if bytes.Equal(name, []byte("YAML")) {
  1374. // Scan the VERSION directive value.
  1375. var major, minor int8
  1376. if !yaml_parser_scan_version_directive_value(parser, start_mark, &major, &minor) {
  1377. return false
  1378. }
  1379. end_mark := parser.mark
  1380. // Create a VERSION-DIRECTIVE token.
  1381. *token = yaml_token_t{
  1382. typ: yaml_VERSION_DIRECTIVE_TOKEN,
  1383. start_mark: start_mark,
  1384. end_mark: end_mark,
  1385. major: major,
  1386. minor: minor,
  1387. }
  1388. // Is it a TAG directive?
  1389. } else if bytes.Equal(name, []byte("TAG")) {
  1390. // Scan the TAG directive value.
  1391. var handle, prefix []byte
  1392. if !yaml_parser_scan_tag_directive_value(parser, start_mark, &handle, &prefix) {
  1393. return false
  1394. }
  1395. end_mark := parser.mark
  1396. // Create a TAG-DIRECTIVE token.
  1397. *token = yaml_token_t{
  1398. typ: yaml_TAG_DIRECTIVE_TOKEN,
  1399. start_mark: start_mark,
  1400. end_mark: end_mark,
  1401. value: handle,
  1402. prefix: prefix,
  1403. }
  1404. // Unknown directive.
  1405. } else {
  1406. yaml_parser_set_scanner_error(parser, "while scanning a directive",
  1407. start_mark, "found unknown directive name")
  1408. return false
  1409. }
  1410. // Eat the rest of the line including any comments.
  1411. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1412. return false
  1413. }
  1414. for is_blank(parser.buffer, parser.buffer_pos) {
  1415. skip(parser)
  1416. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1417. return false
  1418. }
  1419. }
  1420. if parser.buffer[parser.buffer_pos] == '#' {
  1421. for !is_breakz(parser.buffer, parser.buffer_pos) {
  1422. skip(parser)
  1423. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1424. return false
  1425. }
  1426. }
  1427. }
  1428. // Check if we are at the end of the line.
  1429. if !is_breakz(parser.buffer, parser.buffer_pos) {
  1430. yaml_parser_set_scanner_error(parser, "while scanning a directive",
  1431. start_mark, "did not find expected comment or line break")
  1432. return false
  1433. }
  1434. // Eat a line break.
  1435. if is_break(parser.buffer, parser.buffer_pos) {
  1436. if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
  1437. return false
  1438. }
  1439. skip_line(parser)
  1440. }
  1441. return true
  1442. }
  1443. // Scan the directive name.
  1444. //
  1445. // Scope:
  1446. // %YAML 1.1 # a comment \n
  1447. // ^^^^
  1448. // %TAG !yaml! tag:yaml.org,2002: \n
  1449. // ^^^
  1450. //
  1451. func yaml_parser_scan_directive_name(parser *yaml_parser_t, start_mark yaml_mark_t, name *[]byte) bool {
  1452. // Consume the directive name.
  1453. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1454. return false
  1455. }
  1456. var s []byte
  1457. for is_alpha(parser.buffer, parser.buffer_pos) {
  1458. s = read(parser, s)
  1459. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1460. return false
  1461. }
  1462. }
  1463. // Check if the name is empty.
  1464. if len(s) == 0 {
  1465. yaml_parser_set_scanner_error(parser, "while scanning a directive",
  1466. start_mark, "could not find expected directive name")
  1467. return false
  1468. }
  1469. // Check for an blank character after the name.
  1470. if !is_blankz(parser.buffer, parser.buffer_pos) {
  1471. yaml_parser_set_scanner_error(parser, "while scanning a directive",
  1472. start_mark, "found unexpected non-alphabetical character")
  1473. return false
  1474. }
  1475. *name = s
  1476. return true
  1477. }
  1478. // Scan the value of VERSION-DIRECTIVE.
  1479. //
  1480. // Scope:
  1481. // %YAML 1.1 # a comment \n
  1482. // ^^^^^^
  1483. func yaml_parser_scan_version_directive_value(parser *yaml_parser_t, start_mark yaml_mark_t, major, minor *int8) bool {
  1484. // Eat whitespaces.
  1485. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1486. return false
  1487. }
  1488. for is_blank(parser.buffer, parser.buffer_pos) {
  1489. skip(parser)
  1490. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1491. return false
  1492. }
  1493. }
  1494. // Consume the major version number.
  1495. if !yaml_parser_scan_version_directive_number(parser, start_mark, major) {
  1496. return false
  1497. }
  1498. // Eat '.'.
  1499. if parser.buffer[parser.buffer_pos] != '.' {
  1500. return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
  1501. start_mark, "did not find expected digit or '.' character")
  1502. }
  1503. skip(parser)
  1504. // Consume the minor version number.
  1505. if !yaml_parser_scan_version_directive_number(parser, start_mark, minor) {
  1506. return false
  1507. }
  1508. return true
  1509. }
  1510. const max_number_length = 2
  1511. // Scan the version number of VERSION-DIRECTIVE.
  1512. //
  1513. // Scope:
  1514. // %YAML 1.1 # a comment \n
  1515. // ^
  1516. // %YAML 1.1 # a comment \n
  1517. // ^
  1518. func yaml_parser_scan_version_directive_number(parser *yaml_parser_t, start_mark yaml_mark_t, number *int8) bool {
  1519. // Repeat while the next character is digit.
  1520. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1521. return false
  1522. }
  1523. var value, length int8
  1524. for is_digit(parser.buffer, parser.buffer_pos) {
  1525. // Check if the number is too long.
  1526. length++
  1527. if length > max_number_length {
  1528. return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
  1529. start_mark, "found extremely long version number")
  1530. }
  1531. value = value*10 + int8(as_digit(parser.buffer, parser.buffer_pos))
  1532. skip(parser)
  1533. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1534. return false
  1535. }
  1536. }
  1537. // Check if the number was present.
  1538. if length == 0 {
  1539. return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
  1540. start_mark, "did not find expected version number")
  1541. }
  1542. *number = value
  1543. return true
  1544. }
  1545. // Scan the value of a TAG-DIRECTIVE token.
  1546. //
  1547. // Scope:
  1548. // %TAG !yaml! tag:yaml.org,2002: \n
  1549. // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1550. //
  1551. func yaml_parser_scan_tag_directive_value(parser *yaml_parser_t, start_mark yaml_mark_t, handle, prefix *[]byte) bool {
  1552. var handle_value, prefix_value []byte
  1553. // Eat whitespaces.
  1554. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1555. return false
  1556. }
  1557. for is_blank(parser.buffer, parser.buffer_pos) {
  1558. skip(parser)
  1559. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1560. return false
  1561. }
  1562. }
  1563. // Scan a handle.
  1564. if !yaml_parser_scan_tag_handle(parser, true, start_mark, &handle_value) {
  1565. return false
  1566. }
  1567. // Expect a whitespace.
  1568. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1569. return false
  1570. }
  1571. if !is_blank(parser.buffer, parser.buffer_pos) {
  1572. yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
  1573. start_mark, "did not find expected whitespace")
  1574. return false
  1575. }
  1576. // Eat whitespaces.
  1577. for is_blank(parser.buffer, parser.buffer_pos) {
  1578. skip(parser)
  1579. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1580. return false
  1581. }
  1582. }
  1583. // Scan a prefix.
  1584. if !yaml_parser_scan_tag_uri(parser, true, nil, start_mark, &prefix_value) {
  1585. return false
  1586. }
  1587. // Expect a whitespace or line break.
  1588. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1589. return false
  1590. }
  1591. if !is_blankz(parser.buffer, parser.buffer_pos) {
  1592. yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
  1593. start_mark, "did not find expected whitespace or line break")
  1594. return false
  1595. }
  1596. *handle = handle_value
  1597. *prefix = prefix_value
  1598. return true
  1599. }
  1600. func yaml_parser_scan_anchor(parser *yaml_parser_t, token *yaml_token_t, typ yaml_token_type_t) bool {
  1601. var s []byte
  1602. // Eat the indicator character.
  1603. start_mark := parser.mark
  1604. skip(parser)
  1605. // Consume the value.
  1606. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1607. return false
  1608. }
  1609. for is_alpha(parser.buffer, parser.buffer_pos) {
  1610. s = read(parser, s)
  1611. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1612. return false
  1613. }
  1614. }
  1615. end_mark := parser.mark
  1616. /*
  1617. * Check if length of the anchor is greater than 0 and it is followed by
  1618. * a whitespace character or one of the indicators:
  1619. *
  1620. * '?', ':', ',', ']', '}', '%', '@', '`'.
  1621. */
  1622. if len(s) == 0 ||
  1623. !(is_blankz(parser.buffer, parser.buffer_pos) || parser.buffer[parser.buffer_pos] == '?' ||
  1624. parser.buffer[parser.buffer_pos] == ':' || parser.buffer[parser.buffer_pos] == ',' ||
  1625. parser.buffer[parser.buffer_pos] == ']' || parser.buffer[parser.buffer_pos] == '}' ||
  1626. parser.buffer[parser.buffer_pos] == '%' || parser.buffer[parser.buffer_pos] == '@' ||
  1627. parser.buffer[parser.buffer_pos] == '`') {
  1628. context := "while scanning an alias"
  1629. if typ == yaml_ANCHOR_TOKEN {
  1630. context = "while scanning an anchor"
  1631. }
  1632. yaml_parser_set_scanner_error(parser, context, start_mark,
  1633. "did not find expected alphabetic or numeric character")
  1634. return false
  1635. }
  1636. // Create a token.
  1637. *token = yaml_token_t{
  1638. typ: typ,
  1639. start_mark: start_mark,
  1640. end_mark: end_mark,
  1641. value: s,
  1642. }
  1643. return true
  1644. }
  1645. /*
  1646. * Scan a TAG token.
  1647. */
  1648. func yaml_parser_scan_tag(parser *yaml_parser_t, token *yaml_token_t) bool {
  1649. var handle, suffix []byte
  1650. start_mark := parser.mark
  1651. // Check if the tag is in the canonical form.
  1652. if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
  1653. return false
  1654. }
  1655. if parser.buffer[parser.buffer_pos+1] == '<' {
  1656. // Keep the handle as ''
  1657. // Eat '!<'
  1658. skip(parser)
  1659. skip(parser)
  1660. // Consume the tag value.
  1661. if !yaml_parser_scan_tag_uri(parser, false, nil, start_mark, &suffix) {
  1662. return false
  1663. }
  1664. // Check for '>' and eat it.
  1665. if parser.buffer[parser.buffer_pos] != '>' {
  1666. yaml_parser_set_scanner_error(parser, "while scanning a tag",
  1667. start_mark, "did not find the expected '>'")
  1668. return false
  1669. }
  1670. skip(parser)
  1671. } else {
  1672. // The tag has either the '!suffix' or the '!handle!suffix' form.
  1673. // First, try to scan a handle.
  1674. if !yaml_parser_scan_tag_handle(parser, false, start_mark, &handle) {
  1675. return false
  1676. }
  1677. // Check if it is, indeed, handle.
  1678. if handle[0] == '!' && len(handle) > 1 && handle[len(handle)-1] == '!' {
  1679. // Scan the suffix now.
  1680. if !yaml_parser_scan_tag_uri(parser, false, nil, start_mark, &suffix) {
  1681. return false
  1682. }
  1683. } else {
  1684. // It wasn't a handle after all. Scan the rest of the tag.
  1685. if !yaml_parser_scan_tag_uri(parser, false, handle, start_mark, &suffix) {
  1686. return false
  1687. }
  1688. // Set the handle to '!'.
  1689. handle = []byte{'!'}
  1690. // A special case: the '!' tag. Set the handle to '' and the
  1691. // suffix to '!'.
  1692. if len(suffix) == 0 {
  1693. handle, suffix = suffix, handle
  1694. }
  1695. }
  1696. }
  1697. // Check the character which ends the tag.
  1698. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1699. return false
  1700. }
  1701. if !is_blankz(parser.buffer, parser.buffer_pos) {
  1702. yaml_parser_set_scanner_error(parser, "while scanning a tag",
  1703. start_mark, "did not find expected whitespace or line break")
  1704. return false
  1705. }
  1706. end_mark := parser.mark
  1707. // Create a token.
  1708. *token = yaml_token_t{
  1709. typ: yaml_TAG_TOKEN,
  1710. start_mark: start_mark,
  1711. end_mark: end_mark,
  1712. value: handle,
  1713. suffix: suffix,
  1714. }
  1715. return true
  1716. }
  1717. // Scan a tag handle.
  1718. func yaml_parser_scan_tag_handle(parser *yaml_parser_t, directive bool, start_mark yaml_mark_t, handle *[]byte) bool {
  1719. // Check the initial '!' character.
  1720. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1721. return false
  1722. }
  1723. if parser.buffer[parser.buffer_pos] != '!' {
  1724. yaml_parser_set_scanner_tag_error(parser, directive,
  1725. start_mark, "did not find expected '!'")
  1726. return false
  1727. }
  1728. var s []byte
  1729. // Copy the '!' character.
  1730. s = read(parser, s)
  1731. // Copy all subsequent alphabetical and numerical characters.
  1732. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1733. return false
  1734. }
  1735. for is_alpha(parser.buffer, parser.buffer_pos) {
  1736. s = read(parser, s)
  1737. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1738. return false
  1739. }
  1740. }
  1741. // Check if the trailing character is '!' and copy it.
  1742. if parser.buffer[parser.buffer_pos] == '!' {
  1743. s = read(parser, s)
  1744. } else {
  1745. // It's either the '!' tag or not really a tag handle. If it's a %TAG
  1746. // directive, it's an error. If it's a tag token, it must be a part of URI.
  1747. if directive && string(s) != "!" {
  1748. yaml_parser_set_scanner_tag_error(parser, directive,
  1749. start_mark, "did not find expected '!'")
  1750. return false
  1751. }
  1752. }
  1753. *handle = s
  1754. return true
  1755. }
  1756. // Scan a tag.
  1757. func yaml_parser_scan_tag_uri(parser *yaml_parser_t, directive bool, head []byte, start_mark yaml_mark_t, uri *[]byte) bool {
  1758. //size_t length = head ? strlen((char *)head) : 0
  1759. var s []byte
  1760. hasTag := len(head) > 0
  1761. // Copy the head if needed.
  1762. //
  1763. // Note that we don't copy the leading '!' character.
  1764. if len(head) > 1 {
  1765. s = append(s, head[1:]...)
  1766. }
  1767. // Scan the tag.
  1768. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1769. return false
  1770. }
  1771. // The set of characters that may appear in URI is as follows:
  1772. //
  1773. // '0'-'9', 'A'-'Z', 'a'-'z', '_', '-', ';', '/', '?', ':', '@', '&',
  1774. // '=', '+', '$', ',', '.', '!', '~', '*', '\'', '(', ')', '[', ']',
  1775. // '%'.
  1776. // [Go] Convert this into more reasonable logic.
  1777. for is_alpha(parser.buffer, parser.buffer_pos) || parser.buffer[parser.buffer_pos] == ';' ||
  1778. parser.buffer[parser.buffer_pos] == '/' || parser.buffer[parser.buffer_pos] == '?' ||
  1779. parser.buffer[parser.buffer_pos] == ':' || parser.buffer[parser.buffer_pos] == '@' ||
  1780. parser.buffer[parser.buffer_pos] == '&' || parser.buffer[parser.buffer_pos] == '=' ||
  1781. parser.buffer[parser.buffer_pos] == '+' || parser.buffer[parser.buffer_pos] == '$' ||
  1782. parser.buffer[parser.buffer_pos] == ',' || parser.buffer[parser.buffer_pos] == '.' ||
  1783. parser.buffer[parser.buffer_pos] == '!' || parser.buffer[parser.buffer_pos] == '~' ||
  1784. parser.buffer[parser.buffer_pos] == '*' || parser.buffer[parser.buffer_pos] == '\'' ||
  1785. parser.buffer[parser.buffer_pos] == '(' || parser.buffer[parser.buffer_pos] == ')' ||
  1786. parser.buffer[parser.buffer_pos] == '[' || parser.buffer[parser.buffer_pos] == ']' ||
  1787. parser.buffer[parser.buffer_pos] == '%' {
  1788. // Check if it is a URI-escape sequence.
  1789. if parser.buffer[parser.buffer_pos] == '%' {
  1790. if !yaml_parser_scan_uri_escapes(parser, directive, start_mark, &s) {
  1791. return false
  1792. }
  1793. } else {
  1794. s = read(parser, s)
  1795. }
  1796. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1797. return false
  1798. }
  1799. hasTag = true
  1800. }
  1801. if !hasTag {
  1802. yaml_parser_set_scanner_tag_error(parser, directive,
  1803. start_mark, "did not find expected tag URI")
  1804. return false
  1805. }
  1806. *uri = s
  1807. return true
  1808. }
  1809. // Decode an URI-escape sequence corresponding to a single UTF-8 character.
  1810. func yaml_parser_scan_uri_escapes(parser *yaml_parser_t, directive bool, start_mark yaml_mark_t, s *[]byte) bool {
  1811. // Decode the required number of characters.
  1812. w := 1024
  1813. for w > 0 {
  1814. // Check for a URI-escaped octet.
  1815. if parser.unread < 3 && !yaml_parser_update_buffer(parser, 3) {
  1816. return false
  1817. }
  1818. if !(parser.buffer[parser.buffer_pos] == '%' &&
  1819. is_hex(parser.buffer, parser.buffer_pos+1) &&
  1820. is_hex(parser.buffer, parser.buffer_pos+2)) {
  1821. return yaml_parser_set_scanner_tag_error(parser, directive,
  1822. start_mark, "did not find URI escaped octet")
  1823. }
  1824. // Get the octet.
  1825. octet := byte((as_hex(parser.buffer, parser.buffer_pos+1) << 4) + as_hex(parser.buffer, parser.buffer_pos+2))
  1826. // If it is the leading octet, determine the length of the UTF-8 sequence.
  1827. if w == 1024 {
  1828. w = width(octet)
  1829. if w == 0 {
  1830. return yaml_parser_set_scanner_tag_error(parser, directive,
  1831. start_mark, "found an incorrect leading UTF-8 octet")
  1832. }
  1833. } else {
  1834. // Check if the trailing octet is correct.
  1835. if octet&0xC0 != 0x80 {
  1836. return yaml_parser_set_scanner_tag_error(parser, directive,
  1837. start_mark, "found an incorrect trailing UTF-8 octet")
  1838. }
  1839. }
  1840. // Copy the octet and move the pointers.
  1841. *s = append(*s, octet)
  1842. skip(parser)
  1843. skip(parser)
  1844. skip(parser)
  1845. w--
  1846. }
  1847. return true
  1848. }
  1849. // Scan a block scalar.
  1850. func yaml_parser_scan_block_scalar(parser *yaml_parser_t, token *yaml_token_t, literal bool) bool {
  1851. // Eat the indicator '|' or '>'.
  1852. start_mark := parser.mark
  1853. skip(parser)
  1854. // Scan the additional block scalar indicators.
  1855. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1856. return false
  1857. }
  1858. // Check for a chomping indicator.
  1859. var chomping, increment int
  1860. if parser.buffer[parser.buffer_pos] == '+' || parser.buffer[parser.buffer_pos] == '-' {
  1861. // Set the chomping method and eat the indicator.
  1862. if parser.buffer[parser.buffer_pos] == '+' {
  1863. chomping = +1
  1864. } else {
  1865. chomping = -1
  1866. }
  1867. skip(parser)
  1868. // Check for an indentation indicator.
  1869. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1870. return false
  1871. }
  1872. if is_digit(parser.buffer, parser.buffer_pos) {
  1873. // Check that the indentation is greater than 0.
  1874. if parser.buffer[parser.buffer_pos] == '0' {
  1875. yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
  1876. start_mark, "found an indentation indicator equal to 0")
  1877. return false
  1878. }
  1879. // Get the indentation level and eat the indicator.
  1880. increment = as_digit(parser.buffer, parser.buffer_pos)
  1881. skip(parser)
  1882. }
  1883. } else if is_digit(parser.buffer, parser.buffer_pos) {
  1884. // Do the same as above, but in the opposite order.
  1885. if parser.buffer[parser.buffer_pos] == '0' {
  1886. yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
  1887. start_mark, "found an indentation indicator equal to 0")
  1888. return false
  1889. }
  1890. increment = as_digit(parser.buffer, parser.buffer_pos)
  1891. skip(parser)
  1892. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1893. return false
  1894. }
  1895. if parser.buffer[parser.buffer_pos] == '+' || parser.buffer[parser.buffer_pos] == '-' {
  1896. if parser.buffer[parser.buffer_pos] == '+' {
  1897. chomping = +1
  1898. } else {
  1899. chomping = -1
  1900. }
  1901. skip(parser)
  1902. }
  1903. }
  1904. // Eat whitespaces and comments to the end of the line.
  1905. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1906. return false
  1907. }
  1908. for is_blank(parser.buffer, parser.buffer_pos) {
  1909. skip(parser)
  1910. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1911. return false
  1912. }
  1913. }
  1914. if parser.buffer[parser.buffer_pos] == '#' {
  1915. for !is_breakz(parser.buffer, parser.buffer_pos) {
  1916. skip(parser)
  1917. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1918. return false
  1919. }
  1920. }
  1921. }
  1922. // Check if we are at the end of the line.
  1923. if !is_breakz(parser.buffer, parser.buffer_pos) {
  1924. yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
  1925. start_mark, "did not find expected comment or line break")
  1926. return false
  1927. }
  1928. // Eat a line break.
  1929. if is_break(parser.buffer, parser.buffer_pos) {
  1930. if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
  1931. return false
  1932. }
  1933. skip_line(parser)
  1934. }
  1935. end_mark := parser.mark
  1936. // Set the indentation level if it was specified.
  1937. var indent int
  1938. if increment > 0 {
  1939. if parser.indent >= 0 {
  1940. indent = parser.indent + increment
  1941. } else {
  1942. indent = increment
  1943. }
  1944. }
  1945. // Scan the leading line breaks and determine the indentation level if needed.
  1946. var s, leading_break, trailing_breaks []byte
  1947. if !yaml_parser_scan_block_scalar_breaks(parser, &indent, &trailing_breaks, start_mark, &end_mark) {
  1948. return false
  1949. }
  1950. // Scan the block scalar content.
  1951. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1952. return false
  1953. }
  1954. var leading_blank, trailing_blank bool
  1955. for parser.mark.column == indent && !is_z(parser.buffer, parser.buffer_pos) {
  1956. // We are at the beginning of a non-empty line.
  1957. // Is it a trailing whitespace?
  1958. trailing_blank = is_blank(parser.buffer, parser.buffer_pos)
  1959. // Check if we need to fold the leading line break.
  1960. if !literal && !leading_blank && !trailing_blank && len(leading_break) > 0 && leading_break[0] == '\n' {
  1961. // Do we need to join the lines by space?
  1962. if len(trailing_breaks) == 0 {
  1963. s = append(s, ' ')
  1964. }
  1965. } else {
  1966. s = append(s, leading_break...)
  1967. }
  1968. leading_break = leading_break[:0]
  1969. // Append the remaining line breaks.
  1970. s = append(s, trailing_breaks...)
  1971. trailing_breaks = trailing_breaks[:0]
  1972. // Is it a leading whitespace?
  1973. leading_blank = is_blank(parser.buffer, parser.buffer_pos)
  1974. // Consume the current line.
  1975. for !is_breakz(parser.buffer, parser.buffer_pos) {
  1976. s = read(parser, s)
  1977. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1978. return false
  1979. }
  1980. }
  1981. // Consume the line break.
  1982. if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
  1983. return false
  1984. }
  1985. leading_break = read_line(parser, leading_break)
  1986. // Eat the following indentation spaces and line breaks.
  1987. if !yaml_parser_scan_block_scalar_breaks(parser, &indent, &trailing_breaks, start_mark, &end_mark) {
  1988. return false
  1989. }
  1990. }
  1991. // Chomp the tail.
  1992. if chomping != -1 {
  1993. s = append(s, leading_break...)
  1994. }
  1995. if chomping == 1 {
  1996. s = append(s, trailing_breaks...)
  1997. }
  1998. // Create a token.
  1999. *token = yaml_token_t{
  2000. typ: yaml_SCALAR_TOKEN,
  2001. start_mark: start_mark,
  2002. end_mark: end_mark,
  2003. value: s,
  2004. style: yaml_LITERAL_SCALAR_STYLE,
  2005. }
  2006. if !literal {
  2007. token.style = yaml_FOLDED_SCALAR_STYLE
  2008. }
  2009. return true
  2010. }
  2011. // Scan indentation spaces and line breaks for a block scalar. Determine the
  2012. // indentation level if needed.
  2013. func yaml_parser_scan_block_scalar_breaks(parser *yaml_parser_t, indent *int, breaks *[]byte, start_mark yaml_mark_t, end_mark *yaml_mark_t) bool {
  2014. *end_mark = parser.mark
  2015. // Eat the indentation spaces and line breaks.
  2016. max_indent := 0
  2017. for {
  2018. // Eat the indentation spaces.
  2019. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  2020. return false
  2021. }
  2022. for (*indent == 0 || parser.mark.column < *indent) && is_space(parser.buffer, parser.buffer_pos) {
  2023. skip(parser)
  2024. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  2025. return false
  2026. }
  2027. }
  2028. if parser.mark.column > max_indent {
  2029. max_indent = parser.mark.column
  2030. }
  2031. // Check for a tab character messing the indentation.
  2032. if (*indent == 0 || parser.mark.column < *indent) && is_tab(parser.buffer, parser.buffer_pos) {
  2033. return yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
  2034. start_mark, "found a tab character where an indentation space is expected")
  2035. }
  2036. // Have we found a non-empty line?
  2037. if !is_break(parser.buffer, parser.buffer_pos) {
  2038. break
  2039. }
  2040. // Consume the line break.
  2041. if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
  2042. return false
  2043. }
  2044. // [Go] Should really be returning breaks instead.
  2045. *breaks = read_line(parser, *breaks)
  2046. *end_mark = parser.mark
  2047. }
  2048. // Determine the indentation level if needed.
  2049. if *indent == 0 {
  2050. *indent = max_indent
  2051. if *indent < parser.indent+1 {
  2052. *indent = parser.indent + 1
  2053. }
  2054. if *indent < 1 {
  2055. *indent = 1
  2056. }
  2057. }
  2058. return true
  2059. }
  2060. // Scan a quoted scalar.
  2061. func yaml_parser_scan_flow_scalar(parser *yaml_parser_t, token *yaml_token_t, single bool) bool {
  2062. // Eat the left quote.
  2063. start_mark := parser.mark
  2064. skip(parser)
  2065. // Consume the content of the quoted scalar.
  2066. var s, leading_break, trailing_breaks, whitespaces []byte
  2067. for {
  2068. // Check that there are no document indicators at the beginning of the line.
  2069. if parser.unread < 4 && !yaml_parser_update_buffer(parser, 4) {
  2070. return false
  2071. }
  2072. if parser.mark.column == 0 &&
  2073. ((parser.buffer[parser.buffer_pos+0] == '-' &&
  2074. parser.buffer[parser.buffer_pos+1] == '-' &&
  2075. parser.buffer[parser.buffer_pos+2] == '-') ||
  2076. (parser.buffer[parser.buffer_pos+0] == '.' &&
  2077. parser.buffer[parser.buffer_pos+1] == '.' &&
  2078. parser.buffer[parser.buffer_pos+2] == '.')) &&
  2079. is_blankz(parser.buffer, parser.buffer_pos+3) {
  2080. yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
  2081. start_mark, "found unexpected document indicator")
  2082. return false
  2083. }
  2084. // Check for EOF.
  2085. if is_z(parser.buffer, parser.buffer_pos) {
  2086. yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
  2087. start_mark, "found unexpected end of stream")
  2088. return false
  2089. }
  2090. // Consume non-blank characters.
  2091. leading_blanks := false
  2092. for !is_blankz(parser.buffer, parser.buffer_pos) {
  2093. if single && parser.buffer[parser.buffer_pos] == '\'' && parser.buffer[parser.buffer_pos+1] == '\'' {
  2094. // Is is an escaped single quote.
  2095. s = append(s, '\'')
  2096. skip(parser)
  2097. skip(parser)
  2098. } else if single && parser.buffer[parser.buffer_pos] == '\'' {
  2099. // It is a right single quote.
  2100. break
  2101. } else if !single && parser.buffer[parser.buffer_pos] == '"' {
  2102. // It is a right double quote.
  2103. break
  2104. } else if !single && parser.buffer[parser.buffer_pos] == '\\' && is_break(parser.buffer, parser.buffer_pos+1) {
  2105. // It is an escaped line break.
  2106. if parser.unread < 3 && !yaml_parser_update_buffer(parser, 3) {
  2107. return false
  2108. }
  2109. skip(parser)
  2110. skip_line(parser)
  2111. leading_blanks = true
  2112. break
  2113. } else if !single && parser.buffer[parser.buffer_pos] == '\\' {
  2114. // It is an escape sequence.
  2115. code_length := 0
  2116. // Check the escape character.
  2117. switch parser.buffer[parser.buffer_pos+1] {
  2118. case '0':
  2119. s = append(s, 0)
  2120. case 'a':
  2121. s = append(s, '\x07')
  2122. case 'b':
  2123. s = append(s, '\x08')
  2124. case 't', '\t':
  2125. s = append(s, '\x09')
  2126. case 'n':
  2127. s = append(s, '\x0A')
  2128. case 'v':
  2129. s = append(s, '\x0B')
  2130. case 'f':
  2131. s = append(s, '\x0C')
  2132. case 'r':
  2133. s = append(s, '\x0D')
  2134. case 'e':
  2135. s = append(s, '\x1B')
  2136. case ' ':
  2137. s = append(s, '\x20')
  2138. case '"':
  2139. s = append(s, '"')
  2140. case '\'':
  2141. s = append(s, '\'')
  2142. case '\\':
  2143. s = append(s, '\\')
  2144. case 'N': // NEL (#x85)
  2145. s = append(s, '\xC2')
  2146. s = append(s, '\x85')
  2147. case '_': // #xA0
  2148. s = append(s, '\xC2')
  2149. s = append(s, '\xA0')
  2150. case 'L': // LS (#x2028)
  2151. s = append(s, '\xE2')
  2152. s = append(s, '\x80')
  2153. s = append(s, '\xA8')
  2154. case 'P': // PS (#x2029)
  2155. s = append(s, '\xE2')
  2156. s = append(s, '\x80')
  2157. s = append(s, '\xA9')
  2158. case 'x':
  2159. code_length = 2
  2160. case 'u':
  2161. code_length = 4
  2162. case 'U':
  2163. code_length = 8
  2164. default:
  2165. yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
  2166. start_mark, "found unknown escape character")
  2167. return false
  2168. }
  2169. skip(parser)
  2170. skip(parser)
  2171. // Consume an arbitrary escape code.
  2172. if code_length > 0 {
  2173. var value int
  2174. // Scan the character value.
  2175. if parser.unread < code_length && !yaml_parser_update_buffer(parser, code_length) {
  2176. return false
  2177. }
  2178. for k := 0; k < code_length; k++ {
  2179. if !is_hex(parser.buffer, parser.buffer_pos+k) {
  2180. yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
  2181. start_mark, "did not find expected hexdecimal number")
  2182. return false
  2183. }
  2184. value = (value << 4) + as_hex(parser.buffer, parser.buffer_pos+k)
  2185. }
  2186. // Check the value and write the character.
  2187. if (value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF {
  2188. yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
  2189. start_mark, "found invalid Unicode character escape code")
  2190. return false
  2191. }
  2192. if value <= 0x7F {
  2193. s = append(s, byte(value))
  2194. } else if value <= 0x7FF {
  2195. s = append(s, byte(0xC0+(value>>6)))
  2196. s = append(s, byte(0x80+(value&0x3F)))
  2197. } else if value <= 0xFFFF {
  2198. s = append(s, byte(0xE0+(value>>12)))
  2199. s = append(s, byte(0x80+((value>>6)&0x3F)))
  2200. s = append(s, byte(0x80+(value&0x3F)))
  2201. } else {
  2202. s = append(s, byte(0xF0+(value>>18)))
  2203. s = append(s, byte(0x80+((value>>12)&0x3F)))
  2204. s = append(s, byte(0x80+((value>>6)&0x3F)))
  2205. s = append(s, byte(0x80+(value&0x3F)))
  2206. }
  2207. // Advance the pointer.
  2208. for k := 0; k < code_length; k++ {
  2209. skip(parser)
  2210. }
  2211. }
  2212. } else {
  2213. // It is a non-escaped non-blank character.
  2214. s = read(parser, s)
  2215. }
  2216. if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
  2217. return false
  2218. }
  2219. }
  2220. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  2221. return false
  2222. }
  2223. // Check if we are at the end of the scalar.
  2224. if single {
  2225. if parser.buffer[parser.buffer_pos] == '\'' {
  2226. break
  2227. }
  2228. } else {
  2229. if parser.buffer[parser.buffer_pos] == '"' {
  2230. break
  2231. }
  2232. }
  2233. // Consume blank characters.
  2234. for is_blank(parser.buffer, parser.buffer_pos) || is_break(parser.buffer, parser.buffer_pos) {
  2235. if is_blank(parser.buffer, parser.buffer_pos) {
  2236. // Consume a space or a tab character.
  2237. if !leading_blanks {
  2238. whitespaces = read(parser, whitespaces)
  2239. } else {
  2240. skip(parser)
  2241. }
  2242. } else {
  2243. if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
  2244. return false
  2245. }
  2246. // Check if it is a first line break.
  2247. if !leading_blanks {
  2248. whitespaces = whitespaces[:0]
  2249. leading_break = read_line(parser, leading_break)
  2250. leading_blanks = true
  2251. } else {
  2252. trailing_breaks = read_line(parser, trailing_breaks)
  2253. }
  2254. }
  2255. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  2256. return false
  2257. }
  2258. }
  2259. // Join the whitespaces or fold line breaks.
  2260. if leading_blanks {
  2261. // Do we need to fold line breaks?
  2262. if len(leading_break) > 0 && leading_break[0] == '\n' {
  2263. if len(trailing_breaks) == 0 {
  2264. s = append(s, ' ')
  2265. } else {
  2266. s = append(s, trailing_breaks...)
  2267. }
  2268. } else {
  2269. s = append(s, leading_break...)
  2270. s = append(s, trailing_breaks...)
  2271. }
  2272. trailing_breaks = trailing_breaks[:0]
  2273. leading_break = leading_break[:0]
  2274. } else {
  2275. s = append(s, whitespaces...)
  2276. whitespaces = whitespaces[:0]
  2277. }
  2278. }
  2279. // Eat the right quote.
  2280. skip(parser)
  2281. end_mark := parser.mark
  2282. // Create a token.
  2283. *token = yaml_token_t{
  2284. typ: yaml_SCALAR_TOKEN,
  2285. start_mark: start_mark,
  2286. end_mark: end_mark,
  2287. value: s,
  2288. style: yaml_SINGLE_QUOTED_SCALAR_STYLE,
  2289. }
  2290. if !single {
  2291. token.style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
  2292. }
  2293. return true
  2294. }
  2295. // Scan a plain scalar.
  2296. func yaml_parser_scan_plain_scalar(parser *yaml_parser_t, token *yaml_token_t) bool {
  2297. var s, leading_break, trailing_breaks, whitespaces []byte
  2298. var leading_blanks bool
  2299. var indent = parser.indent + 1
  2300. start_mark := parser.mark
  2301. end_mark := parser.mark
  2302. // Consume the content of the plain scalar.
  2303. for {
  2304. // Check for a document indicator.
  2305. if parser.unread < 4 && !yaml_parser_update_buffer(parser, 4) {
  2306. return false
  2307. }
  2308. if parser.mark.column == 0 &&
  2309. ((parser.buffer[parser.buffer_pos+0] == '-' &&
  2310. parser.buffer[parser.buffer_pos+1] == '-' &&
  2311. parser.buffer[parser.buffer_pos+2] == '-') ||
  2312. (parser.buffer[parser.buffer_pos+0] == '.' &&
  2313. parser.buffer[parser.buffer_pos+1] == '.' &&
  2314. parser.buffer[parser.buffer_pos+2] == '.')) &&
  2315. is_blankz(parser.buffer, parser.buffer_pos+3) {
  2316. break
  2317. }
  2318. // Check for a comment.
  2319. if parser.buffer[parser.buffer_pos] == '#' {
  2320. break
  2321. }
  2322. // Consume non-blank characters.
  2323. for !is_blankz(parser.buffer, parser.buffer_pos) {
  2324. // Check for indicators that may end a plain scalar.
  2325. if (parser.buffer[parser.buffer_pos] == ':' && is_blankz(parser.buffer, parser.buffer_pos+1)) ||
  2326. (parser.flow_level > 0 &&
  2327. (parser.buffer[parser.buffer_pos] == ',' ||
  2328. parser.buffer[parser.buffer_pos] == '?' || parser.buffer[parser.buffer_pos] == '[' ||
  2329. parser.buffer[parser.buffer_pos] == ']' || parser.buffer[parser.buffer_pos] == '{' ||
  2330. parser.buffer[parser.buffer_pos] == '}')) {
  2331. break
  2332. }
  2333. // Check if we need to join whitespaces and breaks.
  2334. if leading_blanks || len(whitespaces) > 0 {
  2335. if leading_blanks {
  2336. // Do we need to fold line breaks?
  2337. if leading_break[0] == '\n' {
  2338. if len(trailing_breaks) == 0 {
  2339. s = append(s, ' ')
  2340. } else {
  2341. s = append(s, trailing_breaks...)
  2342. }
  2343. } else {
  2344. s = append(s, leading_break...)
  2345. s = append(s, trailing_breaks...)
  2346. }
  2347. trailing_breaks = trailing_breaks[:0]
  2348. leading_break = leading_break[:0]
  2349. leading_blanks = false
  2350. } else {
  2351. s = append(s, whitespaces...)
  2352. whitespaces = whitespaces[:0]
  2353. }
  2354. }
  2355. // Copy the character.
  2356. s = read(parser, s)
  2357. end_mark = parser.mark
  2358. if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
  2359. return false
  2360. }
  2361. }
  2362. // Is it the end?
  2363. if !(is_blank(parser.buffer, parser.buffer_pos) || is_break(parser.buffer, parser.buffer_pos)) {
  2364. break
  2365. }
  2366. // Consume blank characters.
  2367. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  2368. return false
  2369. }
  2370. for is_blank(parser.buffer, parser.buffer_pos) || is_break(parser.buffer, parser.buffer_pos) {
  2371. if is_blank(parser.buffer, parser.buffer_pos) {
  2372. // Check for tab characters that abuse indentation.
  2373. if leading_blanks && parser.mark.column < indent && is_tab(parser.buffer, parser.buffer_pos) {
  2374. yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
  2375. start_mark, "found a tab character that violates indentation")
  2376. return false
  2377. }
  2378. // Consume a space or a tab character.
  2379. if !leading_blanks {
  2380. whitespaces = read(parser, whitespaces)
  2381. } else {
  2382. skip(parser)
  2383. }
  2384. } else {
  2385. if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
  2386. return false
  2387. }
  2388. // Check if it is a first line break.
  2389. if !leading_blanks {
  2390. whitespaces = whitespaces[:0]
  2391. leading_break = read_line(parser, leading_break)
  2392. leading_blanks = true
  2393. } else {
  2394. trailing_breaks = read_line(parser, trailing_breaks)
  2395. }
  2396. }
  2397. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  2398. return false
  2399. }
  2400. }
  2401. // Check indentation level.
  2402. if parser.flow_level == 0 && parser.mark.column < indent {
  2403. break
  2404. }
  2405. }
  2406. // Create a token.
  2407. *token = yaml_token_t{
  2408. typ: yaml_SCALAR_TOKEN,
  2409. start_mark: start_mark,
  2410. end_mark: end_mark,
  2411. value: s,
  2412. style: yaml_PLAIN_SCALAR_STYLE,
  2413. }
  2414. // Note that we change the 'simple_key_allowed' flag.
  2415. if leading_blanks {
  2416. parser.simple_key_allowed = true
  2417. }
  2418. return true
  2419. }