scannerc.go 81 KB

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