scannerc.go 83 KB

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