scannerc.go 75 KB

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