scannerc.go 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990
  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. // max_flow_level limits the flow_level
  904. const max_flow_level = 10000
  905. // Increase the flow level and resize the simple key list if needed.
  906. func yaml_parser_increase_flow_level(parser *yaml_parser_t) bool {
  907. // Reset the simple key on the next level.
  908. parser.simple_keys = append(parser.simple_keys, yaml_simple_key_t{})
  909. // Increase the flow level.
  910. parser.flow_level++
  911. if parser.flow_level > max_flow_level {
  912. return yaml_parser_set_scanner_error(parser,
  913. "while increasing flow level", parser.simple_keys[len(parser.simple_keys)-1].mark,
  914. fmt.Sprintf("exceeded max depth of %d", max_flow_level))
  915. }
  916. return true
  917. }
  918. // Decrease the flow level.
  919. func yaml_parser_decrease_flow_level(parser *yaml_parser_t) bool {
  920. if parser.flow_level > 0 {
  921. parser.flow_level--
  922. parser.simple_keys = parser.simple_keys[:len(parser.simple_keys)-1]
  923. }
  924. return true
  925. }
  926. // max_indents limits the indents stack size
  927. const max_indents = 10000
  928. // Push the current indentation level to the stack and set the new level
  929. // the current column is greater than the indentation level. In this case,
  930. // append or insert the specified token into the token queue.
  931. func yaml_parser_roll_indent(parser *yaml_parser_t, column, number int, typ yaml_token_type_t, mark yaml_mark_t) bool {
  932. // In the flow context, do nothing.
  933. if parser.flow_level > 0 {
  934. return true
  935. }
  936. if parser.indent < column {
  937. // Push the current indentation level to the stack and set the new
  938. // indentation level.
  939. parser.indents = append(parser.indents, parser.indent)
  940. parser.indent = column
  941. if len(parser.indents) > max_indents {
  942. return yaml_parser_set_scanner_error(parser,
  943. "while increasing indent level", parser.simple_keys[len(parser.simple_keys)-1].mark,
  944. fmt.Sprintf("exceeded max depth of %d", max_indents))
  945. }
  946. // Create a token and insert it into the queue.
  947. token := yaml_token_t{
  948. typ: typ,
  949. start_mark: mark,
  950. end_mark: mark,
  951. }
  952. if number > -1 {
  953. number -= parser.tokens_parsed
  954. }
  955. yaml_insert_token(parser, number, &token)
  956. }
  957. return true
  958. }
  959. // Pop indentation levels from the indents stack until the current level
  960. // becomes less or equal to the column. For each indentation level, append
  961. // the BLOCK-END token.
  962. func yaml_parser_unroll_indent(parser *yaml_parser_t, column int, scan_mark yaml_mark_t) bool {
  963. // In the flow context, do nothing.
  964. if parser.flow_level > 0 {
  965. return true
  966. }
  967. block_mark := scan_mark
  968. block_mark.index--
  969. // Loop through the indentation levels in the stack.
  970. for parser.indent > column {
  971. // [Go] Reposition the end token before potential following
  972. // foot comments of parent blocks. For that, search
  973. // backwards for recent comments that were at the same
  974. // indent as the block that is ending now.
  975. stop_index := block_mark.index
  976. for i := len(parser.comments) - 1; i >= 0; i-- {
  977. comment := &parser.comments[i]
  978. if comment.end_mark.index < stop_index {
  979. // Don't go back beyond the start of the comment/whitespace scan, unless column < 0.
  980. // If requested indent column is < 0, then the document is over and everything else
  981. // is a foot anyway.
  982. break
  983. }
  984. if comment.start_mark.column == parser.indent+1 {
  985. // This is a good match. But maybe there's a former comment
  986. // at that same indent level, so keep searching.
  987. block_mark = comment.start_mark
  988. }
  989. // While the end of the former comment matches with
  990. // the start of the following one, we know there's
  991. // nothing in between and scanning is still safe.
  992. stop_index = comment.scan_mark.index
  993. }
  994. // Create a token and append it to the queue.
  995. token := yaml_token_t{
  996. typ: yaml_BLOCK_END_TOKEN,
  997. start_mark: block_mark,
  998. end_mark: block_mark,
  999. }
  1000. yaml_insert_token(parser, -1, &token)
  1001. // Pop the indentation level.
  1002. parser.indent = parser.indents[len(parser.indents)-1]
  1003. parser.indents = parser.indents[:len(parser.indents)-1]
  1004. }
  1005. return true
  1006. }
  1007. // Initialize the scanner and produce the STREAM-START token.
  1008. func yaml_parser_fetch_stream_start(parser *yaml_parser_t) bool {
  1009. // Set the initial indentation.
  1010. parser.indent = -1
  1011. // Initialize the simple key stack.
  1012. parser.simple_keys = append(parser.simple_keys, yaml_simple_key_t{})
  1013. // A simple key is allowed at the beginning of the stream.
  1014. parser.simple_key_allowed = true
  1015. // We have started.
  1016. parser.stream_start_produced = true
  1017. // Create the STREAM-START token and append it to the queue.
  1018. token := yaml_token_t{
  1019. typ: yaml_STREAM_START_TOKEN,
  1020. start_mark: parser.mark,
  1021. end_mark: parser.mark,
  1022. encoding: parser.encoding,
  1023. }
  1024. yaml_insert_token(parser, -1, &token)
  1025. return true
  1026. }
  1027. // Produce the STREAM-END token and shut down the scanner.
  1028. func yaml_parser_fetch_stream_end(parser *yaml_parser_t) bool {
  1029. // Force new line.
  1030. if parser.mark.column != 0 {
  1031. parser.mark.column = 0
  1032. parser.mark.line++
  1033. }
  1034. // Reset the indentation level.
  1035. if !yaml_parser_unroll_indent(parser, -1, parser.mark) {
  1036. return false
  1037. }
  1038. // Reset simple keys.
  1039. if !yaml_parser_remove_simple_key(parser) {
  1040. return false
  1041. }
  1042. parser.simple_key_allowed = false
  1043. // Create the STREAM-END token and append it to the queue.
  1044. token := yaml_token_t{
  1045. typ: yaml_STREAM_END_TOKEN,
  1046. start_mark: parser.mark,
  1047. end_mark: parser.mark,
  1048. }
  1049. yaml_insert_token(parser, -1, &token)
  1050. return true
  1051. }
  1052. // Produce a VERSION-DIRECTIVE or TAG-DIRECTIVE token.
  1053. func yaml_parser_fetch_directive(parser *yaml_parser_t) bool {
  1054. // Reset the indentation level.
  1055. if !yaml_parser_unroll_indent(parser, -1, parser.mark) {
  1056. return false
  1057. }
  1058. // Reset simple keys.
  1059. if !yaml_parser_remove_simple_key(parser) {
  1060. return false
  1061. }
  1062. parser.simple_key_allowed = false
  1063. // Create the YAML-DIRECTIVE or TAG-DIRECTIVE token.
  1064. token := yaml_token_t{}
  1065. if !yaml_parser_scan_directive(parser, &token) {
  1066. return false
  1067. }
  1068. // Append the token to the queue.
  1069. yaml_insert_token(parser, -1, &token)
  1070. return true
  1071. }
  1072. // Produce the DOCUMENT-START or DOCUMENT-END token.
  1073. func yaml_parser_fetch_document_indicator(parser *yaml_parser_t, typ yaml_token_type_t) bool {
  1074. // Reset the indentation level.
  1075. if !yaml_parser_unroll_indent(parser, -1, parser.mark) {
  1076. return false
  1077. }
  1078. // Reset simple keys.
  1079. if !yaml_parser_remove_simple_key(parser) {
  1080. return false
  1081. }
  1082. parser.simple_key_allowed = false
  1083. // Consume the token.
  1084. start_mark := parser.mark
  1085. skip(parser)
  1086. skip(parser)
  1087. skip(parser)
  1088. end_mark := parser.mark
  1089. // Create the DOCUMENT-START or DOCUMENT-END token.
  1090. token := yaml_token_t{
  1091. typ: typ,
  1092. start_mark: start_mark,
  1093. end_mark: end_mark,
  1094. }
  1095. // Append the token to the queue.
  1096. yaml_insert_token(parser, -1, &token)
  1097. return true
  1098. }
  1099. // Produce the FLOW-SEQUENCE-START or FLOW-MAPPING-START token.
  1100. func yaml_parser_fetch_flow_collection_start(parser *yaml_parser_t, typ yaml_token_type_t) bool {
  1101. // The indicators '[' and '{' may start a simple key.
  1102. if !yaml_parser_save_simple_key(parser) {
  1103. return false
  1104. }
  1105. // Increase the flow level.
  1106. if !yaml_parser_increase_flow_level(parser) {
  1107. return false
  1108. }
  1109. // A simple key may follow the indicators '[' and '{'.
  1110. parser.simple_key_allowed = true
  1111. // Consume the token.
  1112. start_mark := parser.mark
  1113. skip(parser)
  1114. end_mark := parser.mark
  1115. // Create the FLOW-SEQUENCE-START of FLOW-MAPPING-START token.
  1116. token := yaml_token_t{
  1117. typ: typ,
  1118. start_mark: start_mark,
  1119. end_mark: end_mark,
  1120. }
  1121. // Append the token to the queue.
  1122. yaml_insert_token(parser, -1, &token)
  1123. return true
  1124. }
  1125. // Produce the FLOW-SEQUENCE-END or FLOW-MAPPING-END token.
  1126. func yaml_parser_fetch_flow_collection_end(parser *yaml_parser_t, typ yaml_token_type_t) bool {
  1127. // Reset any potential simple key on the current flow level.
  1128. if !yaml_parser_remove_simple_key(parser) {
  1129. return false
  1130. }
  1131. // Decrease the flow level.
  1132. if !yaml_parser_decrease_flow_level(parser) {
  1133. return false
  1134. }
  1135. // No simple keys after the indicators ']' and '}'.
  1136. parser.simple_key_allowed = false
  1137. // Consume the token.
  1138. start_mark := parser.mark
  1139. skip(parser)
  1140. end_mark := parser.mark
  1141. // Create the FLOW-SEQUENCE-END of FLOW-MAPPING-END token.
  1142. token := yaml_token_t{
  1143. typ: typ,
  1144. start_mark: start_mark,
  1145. end_mark: end_mark,
  1146. }
  1147. // Append the token to the queue.
  1148. yaml_insert_token(parser, -1, &token)
  1149. return true
  1150. }
  1151. // Produce the FLOW-ENTRY token.
  1152. func yaml_parser_fetch_flow_entry(parser *yaml_parser_t) bool {
  1153. // Reset any potential simple keys on the current flow level.
  1154. if !yaml_parser_remove_simple_key(parser) {
  1155. return false
  1156. }
  1157. // Simple keys are allowed after ','.
  1158. parser.simple_key_allowed = true
  1159. // Consume the token.
  1160. start_mark := parser.mark
  1161. skip(parser)
  1162. end_mark := parser.mark
  1163. // Create the FLOW-ENTRY token and append it to the queue.
  1164. token := yaml_token_t{
  1165. typ: yaml_FLOW_ENTRY_TOKEN,
  1166. start_mark: start_mark,
  1167. end_mark: end_mark,
  1168. }
  1169. yaml_insert_token(parser, -1, &token)
  1170. return true
  1171. }
  1172. // Produce the BLOCK-ENTRY token.
  1173. func yaml_parser_fetch_block_entry(parser *yaml_parser_t) bool {
  1174. // Check if the scanner is in the block context.
  1175. if parser.flow_level == 0 {
  1176. // Check if we are allowed to start a new entry.
  1177. if !parser.simple_key_allowed {
  1178. return yaml_parser_set_scanner_error(parser, "", parser.mark,
  1179. "block sequence entries are not allowed in this context")
  1180. }
  1181. // Add the BLOCK-SEQUENCE-START token if needed.
  1182. if !yaml_parser_roll_indent(parser, parser.mark.column, -1, yaml_BLOCK_SEQUENCE_START_TOKEN, parser.mark) {
  1183. return false
  1184. }
  1185. } else {
  1186. // It is an error for the '-' indicator to occur in the flow context,
  1187. // but we let the Parser detect and report about it because the Parser
  1188. // is able to point to the context.
  1189. }
  1190. // Reset any potential simple keys on the current flow level.
  1191. if !yaml_parser_remove_simple_key(parser) {
  1192. return false
  1193. }
  1194. // Simple keys are allowed after '-'.
  1195. parser.simple_key_allowed = true
  1196. // Consume the token.
  1197. start_mark := parser.mark
  1198. skip(parser)
  1199. end_mark := parser.mark
  1200. // Create the BLOCK-ENTRY token and append it to the queue.
  1201. token := yaml_token_t{
  1202. typ: yaml_BLOCK_ENTRY_TOKEN,
  1203. start_mark: start_mark,
  1204. end_mark: end_mark,
  1205. }
  1206. yaml_insert_token(parser, -1, &token)
  1207. return true
  1208. }
  1209. // Produce the KEY token.
  1210. func yaml_parser_fetch_key(parser *yaml_parser_t) bool {
  1211. // In the block context, additional checks are required.
  1212. if parser.flow_level == 0 {
  1213. // Check if we are allowed to start a new key (not nessesary simple).
  1214. if !parser.simple_key_allowed {
  1215. return yaml_parser_set_scanner_error(parser, "", parser.mark,
  1216. "mapping keys are not allowed in this context")
  1217. }
  1218. // Add the BLOCK-MAPPING-START token if needed.
  1219. if !yaml_parser_roll_indent(parser, parser.mark.column, -1, yaml_BLOCK_MAPPING_START_TOKEN, parser.mark) {
  1220. return false
  1221. }
  1222. }
  1223. // Reset any potential simple keys on the current flow level.
  1224. if !yaml_parser_remove_simple_key(parser) {
  1225. return false
  1226. }
  1227. // Simple keys are allowed after '?' in the block context.
  1228. parser.simple_key_allowed = parser.flow_level == 0
  1229. // Consume the token.
  1230. start_mark := parser.mark
  1231. skip(parser)
  1232. end_mark := parser.mark
  1233. // Create the KEY token and append it to the queue.
  1234. token := yaml_token_t{
  1235. typ: yaml_KEY_TOKEN,
  1236. start_mark: start_mark,
  1237. end_mark: end_mark,
  1238. }
  1239. yaml_insert_token(parser, -1, &token)
  1240. return true
  1241. }
  1242. // Produce the VALUE token.
  1243. func yaml_parser_fetch_value(parser *yaml_parser_t) bool {
  1244. simple_key := &parser.simple_keys[len(parser.simple_keys)-1]
  1245. // Have we found a simple key?
  1246. if simple_key.possible {
  1247. // Create the KEY token and insert it into the queue.
  1248. token := yaml_token_t{
  1249. typ: yaml_KEY_TOKEN,
  1250. start_mark: simple_key.mark,
  1251. end_mark: simple_key.mark,
  1252. }
  1253. yaml_insert_token(parser, simple_key.token_number-parser.tokens_parsed, &token)
  1254. // In the block context, we may need to add the BLOCK-MAPPING-START token.
  1255. if !yaml_parser_roll_indent(parser, simple_key.mark.column,
  1256. simple_key.token_number,
  1257. yaml_BLOCK_MAPPING_START_TOKEN, simple_key.mark) {
  1258. return false
  1259. }
  1260. // Remove the simple key.
  1261. simple_key.possible = false
  1262. // A simple key cannot follow another simple key.
  1263. parser.simple_key_allowed = false
  1264. } else {
  1265. // The ':' indicator follows a complex key.
  1266. // In the block context, extra checks are required.
  1267. if parser.flow_level == 0 {
  1268. // Check if we are allowed to start a complex value.
  1269. if !parser.simple_key_allowed {
  1270. return yaml_parser_set_scanner_error(parser, "", parser.mark,
  1271. "mapping values are not allowed in this context")
  1272. }
  1273. // Add the BLOCK-MAPPING-START token if needed.
  1274. if !yaml_parser_roll_indent(parser, parser.mark.column, -1, yaml_BLOCK_MAPPING_START_TOKEN, parser.mark) {
  1275. return false
  1276. }
  1277. }
  1278. // Simple keys after ':' are allowed in the block context.
  1279. parser.simple_key_allowed = parser.flow_level == 0
  1280. }
  1281. // Consume the token.
  1282. start_mark := parser.mark
  1283. skip(parser)
  1284. end_mark := parser.mark
  1285. // Create the VALUE token and append it to the queue.
  1286. token := yaml_token_t{
  1287. typ: yaml_VALUE_TOKEN,
  1288. start_mark: start_mark,
  1289. end_mark: end_mark,
  1290. }
  1291. yaml_insert_token(parser, -1, &token)
  1292. return true
  1293. }
  1294. // Produce the ALIAS or ANCHOR token.
  1295. func yaml_parser_fetch_anchor(parser *yaml_parser_t, typ yaml_token_type_t) bool {
  1296. // An anchor or an alias could be a simple key.
  1297. if !yaml_parser_save_simple_key(parser) {
  1298. return false
  1299. }
  1300. // A simple key cannot follow an anchor or an alias.
  1301. parser.simple_key_allowed = false
  1302. // Create the ALIAS or ANCHOR token and append it to the queue.
  1303. var token yaml_token_t
  1304. if !yaml_parser_scan_anchor(parser, &token, typ) {
  1305. return false
  1306. }
  1307. yaml_insert_token(parser, -1, &token)
  1308. return true
  1309. }
  1310. // Produce the TAG token.
  1311. func yaml_parser_fetch_tag(parser *yaml_parser_t) bool {
  1312. // A tag could be a simple key.
  1313. if !yaml_parser_save_simple_key(parser) {
  1314. return false
  1315. }
  1316. // A simple key cannot follow a tag.
  1317. parser.simple_key_allowed = false
  1318. // Create the TAG token and append it to the queue.
  1319. var token yaml_token_t
  1320. if !yaml_parser_scan_tag(parser, &token) {
  1321. return false
  1322. }
  1323. yaml_insert_token(parser, -1, &token)
  1324. return true
  1325. }
  1326. // Produce the SCALAR(...,literal) or SCALAR(...,folded) tokens.
  1327. func yaml_parser_fetch_block_scalar(parser *yaml_parser_t, literal bool) bool {
  1328. // Remove any potential simple keys.
  1329. if !yaml_parser_remove_simple_key(parser) {
  1330. return false
  1331. }
  1332. // A simple key may follow a block scalar.
  1333. parser.simple_key_allowed = true
  1334. // Create the SCALAR token and append it to the queue.
  1335. var token yaml_token_t
  1336. if !yaml_parser_scan_block_scalar(parser, &token, literal) {
  1337. return false
  1338. }
  1339. yaml_insert_token(parser, -1, &token)
  1340. return true
  1341. }
  1342. // Produce the SCALAR(...,single-quoted) or SCALAR(...,double-quoted) tokens.
  1343. func yaml_parser_fetch_flow_scalar(parser *yaml_parser_t, single bool) bool {
  1344. // A plain scalar could be a simple key.
  1345. if !yaml_parser_save_simple_key(parser) {
  1346. return false
  1347. }
  1348. // A simple key cannot follow a flow scalar.
  1349. parser.simple_key_allowed = false
  1350. // Create the SCALAR token and append it to the queue.
  1351. var token yaml_token_t
  1352. if !yaml_parser_scan_flow_scalar(parser, &token, single) {
  1353. return false
  1354. }
  1355. yaml_insert_token(parser, -1, &token)
  1356. return true
  1357. }
  1358. // Produce the SCALAR(...,plain) token.
  1359. func yaml_parser_fetch_plain_scalar(parser *yaml_parser_t) bool {
  1360. // A plain scalar could be a simple key.
  1361. if !yaml_parser_save_simple_key(parser) {
  1362. return false
  1363. }
  1364. // A simple key cannot follow a flow scalar.
  1365. parser.simple_key_allowed = false
  1366. // Create the SCALAR token and append it to the queue.
  1367. var token yaml_token_t
  1368. if !yaml_parser_scan_plain_scalar(parser, &token) {
  1369. return false
  1370. }
  1371. yaml_insert_token(parser, -1, &token)
  1372. return true
  1373. }
  1374. // Eat whitespaces and comments until the next token is found.
  1375. func yaml_parser_scan_to_next_token(parser *yaml_parser_t) bool {
  1376. scan_mark := parser.mark
  1377. // Until the next token is not found.
  1378. for {
  1379. // Allow the BOM mark to start a line.
  1380. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1381. return false
  1382. }
  1383. if parser.mark.column == 0 && is_bom(parser.buffer, parser.buffer_pos) {
  1384. skip(parser)
  1385. }
  1386. // Eat whitespaces.
  1387. // Tabs are allowed:
  1388. // - in the flow context
  1389. // - in the block context, but not at the beginning of the line or
  1390. // after '-', '?', or ':' (complex value).
  1391. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1392. return false
  1393. }
  1394. for parser.buffer[parser.buffer_pos] == ' ' || ((parser.flow_level > 0 || !parser.simple_key_allowed) && parser.buffer[parser.buffer_pos] == '\t') {
  1395. skip(parser)
  1396. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1397. return false
  1398. }
  1399. }
  1400. // Eat a comment until a line break.
  1401. if parser.buffer[parser.buffer_pos] == '#' {
  1402. if !yaml_parser_scan_comments(parser, scan_mark) {
  1403. return false
  1404. }
  1405. }
  1406. // If it is a line break, eat it.
  1407. if is_break(parser.buffer, parser.buffer_pos) {
  1408. if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
  1409. return false
  1410. }
  1411. skip_line(parser)
  1412. // In the block context, a new line may start a simple key.
  1413. if parser.flow_level == 0 {
  1414. parser.simple_key_allowed = true
  1415. }
  1416. } else {
  1417. break // We have found a token.
  1418. }
  1419. }
  1420. return true
  1421. }
  1422. // Scan a YAML-DIRECTIVE or TAG-DIRECTIVE token.
  1423. //
  1424. // Scope:
  1425. // %YAML 1.1 # a comment \n
  1426. // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1427. // %TAG !yaml! tag:yaml.org,2002: \n
  1428. // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1429. //
  1430. func yaml_parser_scan_directive(parser *yaml_parser_t, token *yaml_token_t) bool {
  1431. // Eat '%'.
  1432. start_mark := parser.mark
  1433. skip(parser)
  1434. // Scan the directive name.
  1435. var name []byte
  1436. if !yaml_parser_scan_directive_name(parser, start_mark, &name) {
  1437. return false
  1438. }
  1439. // Is it a YAML directive?
  1440. if bytes.Equal(name, []byte("YAML")) {
  1441. // Scan the VERSION directive value.
  1442. var major, minor int8
  1443. if !yaml_parser_scan_version_directive_value(parser, start_mark, &major, &minor) {
  1444. return false
  1445. }
  1446. end_mark := parser.mark
  1447. // Create a VERSION-DIRECTIVE token.
  1448. *token = yaml_token_t{
  1449. typ: yaml_VERSION_DIRECTIVE_TOKEN,
  1450. start_mark: start_mark,
  1451. end_mark: end_mark,
  1452. major: major,
  1453. minor: minor,
  1454. }
  1455. // Is it a TAG directive?
  1456. } else if bytes.Equal(name, []byte("TAG")) {
  1457. // Scan the TAG directive value.
  1458. var handle, prefix []byte
  1459. if !yaml_parser_scan_tag_directive_value(parser, start_mark, &handle, &prefix) {
  1460. return false
  1461. }
  1462. end_mark := parser.mark
  1463. // Create a TAG-DIRECTIVE token.
  1464. *token = yaml_token_t{
  1465. typ: yaml_TAG_DIRECTIVE_TOKEN,
  1466. start_mark: start_mark,
  1467. end_mark: end_mark,
  1468. value: handle,
  1469. prefix: prefix,
  1470. }
  1471. // Unknown directive.
  1472. } else {
  1473. yaml_parser_set_scanner_error(parser, "while scanning a directive",
  1474. start_mark, "found unknown directive name")
  1475. return false
  1476. }
  1477. // Eat the rest of the line including any comments.
  1478. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1479. return false
  1480. }
  1481. for is_blank(parser.buffer, parser.buffer_pos) {
  1482. skip(parser)
  1483. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1484. return false
  1485. }
  1486. }
  1487. if parser.buffer[parser.buffer_pos] == '#' {
  1488. // [Go] Discard this inline comment for the time being.
  1489. //if !yaml_parser_scan_line_comment(parser, start_mark) {
  1490. // return false
  1491. //}
  1492. for !is_breakz(parser.buffer, parser.buffer_pos) {
  1493. skip(parser)
  1494. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1495. return false
  1496. }
  1497. }
  1498. }
  1499. // Check if we are at the end of the line.
  1500. if !is_breakz(parser.buffer, parser.buffer_pos) {
  1501. yaml_parser_set_scanner_error(parser, "while scanning a directive",
  1502. start_mark, "did not find expected comment or line break")
  1503. return false
  1504. }
  1505. // Eat a line break.
  1506. if is_break(parser.buffer, parser.buffer_pos) {
  1507. if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
  1508. return false
  1509. }
  1510. skip_line(parser)
  1511. }
  1512. return true
  1513. }
  1514. // Scan the directive name.
  1515. //
  1516. // Scope:
  1517. // %YAML 1.1 # a comment \n
  1518. // ^^^^
  1519. // %TAG !yaml! tag:yaml.org,2002: \n
  1520. // ^^^
  1521. //
  1522. func yaml_parser_scan_directive_name(parser *yaml_parser_t, start_mark yaml_mark_t, name *[]byte) bool {
  1523. // Consume the directive name.
  1524. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1525. return false
  1526. }
  1527. var s []byte
  1528. for is_alpha(parser.buffer, parser.buffer_pos) {
  1529. s = read(parser, s)
  1530. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1531. return false
  1532. }
  1533. }
  1534. // Check if the name is empty.
  1535. if len(s) == 0 {
  1536. yaml_parser_set_scanner_error(parser, "while scanning a directive",
  1537. start_mark, "could not find expected directive name")
  1538. return false
  1539. }
  1540. // Check for an blank character after the name.
  1541. if !is_blankz(parser.buffer, parser.buffer_pos) {
  1542. yaml_parser_set_scanner_error(parser, "while scanning a directive",
  1543. start_mark, "found unexpected non-alphabetical character")
  1544. return false
  1545. }
  1546. *name = s
  1547. return true
  1548. }
  1549. // Scan the value of VERSION-DIRECTIVE.
  1550. //
  1551. // Scope:
  1552. // %YAML 1.1 # a comment \n
  1553. // ^^^^^^
  1554. func yaml_parser_scan_version_directive_value(parser *yaml_parser_t, start_mark yaml_mark_t, major, minor *int8) bool {
  1555. // Eat whitespaces.
  1556. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1557. return false
  1558. }
  1559. for is_blank(parser.buffer, parser.buffer_pos) {
  1560. skip(parser)
  1561. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1562. return false
  1563. }
  1564. }
  1565. // Consume the major version number.
  1566. if !yaml_parser_scan_version_directive_number(parser, start_mark, major) {
  1567. return false
  1568. }
  1569. // Eat '.'.
  1570. if parser.buffer[parser.buffer_pos] != '.' {
  1571. return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
  1572. start_mark, "did not find expected digit or '.' character")
  1573. }
  1574. skip(parser)
  1575. // Consume the minor version number.
  1576. if !yaml_parser_scan_version_directive_number(parser, start_mark, minor) {
  1577. return false
  1578. }
  1579. return true
  1580. }
  1581. const max_number_length = 2
  1582. // Scan the version number of VERSION-DIRECTIVE.
  1583. //
  1584. // Scope:
  1585. // %YAML 1.1 # a comment \n
  1586. // ^
  1587. // %YAML 1.1 # a comment \n
  1588. // ^
  1589. func yaml_parser_scan_version_directive_number(parser *yaml_parser_t, start_mark yaml_mark_t, number *int8) bool {
  1590. // Repeat while the next character is digit.
  1591. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1592. return false
  1593. }
  1594. var value, length int8
  1595. for is_digit(parser.buffer, parser.buffer_pos) {
  1596. // Check if the number is too long.
  1597. length++
  1598. if length > max_number_length {
  1599. return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
  1600. start_mark, "found extremely long version number")
  1601. }
  1602. value = value*10 + int8(as_digit(parser.buffer, parser.buffer_pos))
  1603. skip(parser)
  1604. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1605. return false
  1606. }
  1607. }
  1608. // Check if the number was present.
  1609. if length == 0 {
  1610. return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
  1611. start_mark, "did not find expected version number")
  1612. }
  1613. *number = value
  1614. return true
  1615. }
  1616. // Scan the value of a TAG-DIRECTIVE token.
  1617. //
  1618. // Scope:
  1619. // %TAG !yaml! tag:yaml.org,2002: \n
  1620. // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1621. //
  1622. func yaml_parser_scan_tag_directive_value(parser *yaml_parser_t, start_mark yaml_mark_t, handle, prefix *[]byte) bool {
  1623. var handle_value, prefix_value []byte
  1624. // Eat whitespaces.
  1625. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1626. return false
  1627. }
  1628. for is_blank(parser.buffer, parser.buffer_pos) {
  1629. skip(parser)
  1630. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1631. return false
  1632. }
  1633. }
  1634. // Scan a handle.
  1635. if !yaml_parser_scan_tag_handle(parser, true, start_mark, &handle_value) {
  1636. return false
  1637. }
  1638. // Expect a whitespace.
  1639. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1640. return false
  1641. }
  1642. if !is_blank(parser.buffer, parser.buffer_pos) {
  1643. yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
  1644. start_mark, "did not find expected whitespace")
  1645. return false
  1646. }
  1647. // Eat whitespaces.
  1648. for is_blank(parser.buffer, parser.buffer_pos) {
  1649. skip(parser)
  1650. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1651. return false
  1652. }
  1653. }
  1654. // Scan a prefix.
  1655. if !yaml_parser_scan_tag_uri(parser, true, nil, start_mark, &prefix_value) {
  1656. return false
  1657. }
  1658. // Expect a whitespace or line break.
  1659. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1660. return false
  1661. }
  1662. if !is_blankz(parser.buffer, parser.buffer_pos) {
  1663. yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
  1664. start_mark, "did not find expected whitespace or line break")
  1665. return false
  1666. }
  1667. *handle = handle_value
  1668. *prefix = prefix_value
  1669. return true
  1670. }
  1671. func yaml_parser_scan_anchor(parser *yaml_parser_t, token *yaml_token_t, typ yaml_token_type_t) bool {
  1672. var s []byte
  1673. // Eat the indicator character.
  1674. start_mark := parser.mark
  1675. skip(parser)
  1676. // Consume the value.
  1677. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1678. return false
  1679. }
  1680. for is_alpha(parser.buffer, parser.buffer_pos) {
  1681. s = read(parser, s)
  1682. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1683. return false
  1684. }
  1685. }
  1686. end_mark := parser.mark
  1687. /*
  1688. * Check if length of the anchor is greater than 0 and it is followed by
  1689. * a whitespace character or one of the indicators:
  1690. *
  1691. * '?', ':', ',', ']', '}', '%', '@', '`'.
  1692. */
  1693. if len(s) == 0 ||
  1694. !(is_blankz(parser.buffer, parser.buffer_pos) || parser.buffer[parser.buffer_pos] == '?' ||
  1695. parser.buffer[parser.buffer_pos] == ':' || parser.buffer[parser.buffer_pos] == ',' ||
  1696. parser.buffer[parser.buffer_pos] == ']' || parser.buffer[parser.buffer_pos] == '}' ||
  1697. parser.buffer[parser.buffer_pos] == '%' || parser.buffer[parser.buffer_pos] == '@' ||
  1698. parser.buffer[parser.buffer_pos] == '`') {
  1699. context := "while scanning an alias"
  1700. if typ == yaml_ANCHOR_TOKEN {
  1701. context = "while scanning an anchor"
  1702. }
  1703. yaml_parser_set_scanner_error(parser, context, start_mark,
  1704. "did not find expected alphabetic or numeric character")
  1705. return false
  1706. }
  1707. // Create a token.
  1708. *token = yaml_token_t{
  1709. typ: typ,
  1710. start_mark: start_mark,
  1711. end_mark: end_mark,
  1712. value: s,
  1713. }
  1714. return true
  1715. }
  1716. /*
  1717. * Scan a TAG token.
  1718. */
  1719. func yaml_parser_scan_tag(parser *yaml_parser_t, token *yaml_token_t) bool {
  1720. var handle, suffix []byte
  1721. start_mark := parser.mark
  1722. // Check if the tag is in the canonical form.
  1723. if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
  1724. return false
  1725. }
  1726. if parser.buffer[parser.buffer_pos+1] == '<' {
  1727. // Keep the handle as ''
  1728. // Eat '!<'
  1729. skip(parser)
  1730. skip(parser)
  1731. // Consume the tag value.
  1732. if !yaml_parser_scan_tag_uri(parser, false, nil, start_mark, &suffix) {
  1733. return false
  1734. }
  1735. // Check for '>' and eat it.
  1736. if parser.buffer[parser.buffer_pos] != '>' {
  1737. yaml_parser_set_scanner_error(parser, "while scanning a tag",
  1738. start_mark, "did not find the expected '>'")
  1739. return false
  1740. }
  1741. skip(parser)
  1742. } else {
  1743. // The tag has either the '!suffix' or the '!handle!suffix' form.
  1744. // First, try to scan a handle.
  1745. if !yaml_parser_scan_tag_handle(parser, false, start_mark, &handle) {
  1746. return false
  1747. }
  1748. // Check if it is, indeed, handle.
  1749. if handle[0] == '!' && len(handle) > 1 && handle[len(handle)-1] == '!' {
  1750. // Scan the suffix now.
  1751. if !yaml_parser_scan_tag_uri(parser, false, nil, start_mark, &suffix) {
  1752. return false
  1753. }
  1754. } else {
  1755. // It wasn't a handle after all. Scan the rest of the tag.
  1756. if !yaml_parser_scan_tag_uri(parser, false, handle, start_mark, &suffix) {
  1757. return false
  1758. }
  1759. // Set the handle to '!'.
  1760. handle = []byte{'!'}
  1761. // A special case: the '!' tag. Set the handle to '' and the
  1762. // suffix to '!'.
  1763. if len(suffix) == 0 {
  1764. handle, suffix = suffix, handle
  1765. }
  1766. }
  1767. }
  1768. // Check the character which ends the tag.
  1769. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1770. return false
  1771. }
  1772. if !is_blankz(parser.buffer, parser.buffer_pos) {
  1773. yaml_parser_set_scanner_error(parser, "while scanning a tag",
  1774. start_mark, "did not find expected whitespace or line break")
  1775. return false
  1776. }
  1777. end_mark := parser.mark
  1778. // Create a token.
  1779. *token = yaml_token_t{
  1780. typ: yaml_TAG_TOKEN,
  1781. start_mark: start_mark,
  1782. end_mark: end_mark,
  1783. value: handle,
  1784. suffix: suffix,
  1785. }
  1786. return true
  1787. }
  1788. // Scan a tag handle.
  1789. func yaml_parser_scan_tag_handle(parser *yaml_parser_t, directive bool, start_mark yaml_mark_t, handle *[]byte) bool {
  1790. // Check the initial '!' character.
  1791. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1792. return false
  1793. }
  1794. if parser.buffer[parser.buffer_pos] != '!' {
  1795. yaml_parser_set_scanner_tag_error(parser, directive,
  1796. start_mark, "did not find expected '!'")
  1797. return false
  1798. }
  1799. var s []byte
  1800. // Copy the '!' character.
  1801. s = read(parser, s)
  1802. // Copy all subsequent alphabetical and numerical characters.
  1803. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1804. return false
  1805. }
  1806. for is_alpha(parser.buffer, parser.buffer_pos) {
  1807. s = read(parser, s)
  1808. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1809. return false
  1810. }
  1811. }
  1812. // Check if the trailing character is '!' and copy it.
  1813. if parser.buffer[parser.buffer_pos] == '!' {
  1814. s = read(parser, s)
  1815. } else {
  1816. // It's either the '!' tag or not really a tag handle. If it's a %TAG
  1817. // directive, it's an error. If it's a tag token, it must be a part of URI.
  1818. if directive && string(s) != "!" {
  1819. yaml_parser_set_scanner_tag_error(parser, directive,
  1820. start_mark, "did not find expected '!'")
  1821. return false
  1822. }
  1823. }
  1824. *handle = s
  1825. return true
  1826. }
  1827. // Scan a tag.
  1828. func yaml_parser_scan_tag_uri(parser *yaml_parser_t, directive bool, head []byte, start_mark yaml_mark_t, uri *[]byte) bool {
  1829. //size_t length = head ? strlen((char *)head) : 0
  1830. var s []byte
  1831. hasTag := len(head) > 0
  1832. // Copy the head if needed.
  1833. //
  1834. // Note that we don't copy the leading '!' character.
  1835. if len(head) > 1 {
  1836. s = append(s, head[1:]...)
  1837. }
  1838. // Scan the tag.
  1839. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1840. return false
  1841. }
  1842. // The set of characters that may appear in URI is as follows:
  1843. //
  1844. // '0'-'9', 'A'-'Z', 'a'-'z', '_', '-', ';', '/', '?', ':', '@', '&',
  1845. // '=', '+', '$', ',', '.', '!', '~', '*', '\'', '(', ')', '[', ']',
  1846. // '%'.
  1847. // [Go] TODO Convert this into more reasonable logic.
  1848. for is_alpha(parser.buffer, parser.buffer_pos) || parser.buffer[parser.buffer_pos] == ';' ||
  1849. parser.buffer[parser.buffer_pos] == '/' || parser.buffer[parser.buffer_pos] == '?' ||
  1850. parser.buffer[parser.buffer_pos] == ':' || parser.buffer[parser.buffer_pos] == '@' ||
  1851. parser.buffer[parser.buffer_pos] == '&' || parser.buffer[parser.buffer_pos] == '=' ||
  1852. parser.buffer[parser.buffer_pos] == '+' || parser.buffer[parser.buffer_pos] == '$' ||
  1853. parser.buffer[parser.buffer_pos] == ',' || parser.buffer[parser.buffer_pos] == '.' ||
  1854. parser.buffer[parser.buffer_pos] == '!' || parser.buffer[parser.buffer_pos] == '~' ||
  1855. parser.buffer[parser.buffer_pos] == '*' || parser.buffer[parser.buffer_pos] == '\'' ||
  1856. parser.buffer[parser.buffer_pos] == '(' || parser.buffer[parser.buffer_pos] == ')' ||
  1857. parser.buffer[parser.buffer_pos] == '[' || parser.buffer[parser.buffer_pos] == ']' ||
  1858. parser.buffer[parser.buffer_pos] == '%' {
  1859. // Check if it is a URI-escape sequence.
  1860. if parser.buffer[parser.buffer_pos] == '%' {
  1861. if !yaml_parser_scan_uri_escapes(parser, directive, start_mark, &s) {
  1862. return false
  1863. }
  1864. } else {
  1865. s = read(parser, s)
  1866. }
  1867. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1868. return false
  1869. }
  1870. hasTag = true
  1871. }
  1872. if !hasTag {
  1873. yaml_parser_set_scanner_tag_error(parser, directive,
  1874. start_mark, "did not find expected tag URI")
  1875. return false
  1876. }
  1877. *uri = s
  1878. return true
  1879. }
  1880. // Decode an URI-escape sequence corresponding to a single UTF-8 character.
  1881. func yaml_parser_scan_uri_escapes(parser *yaml_parser_t, directive bool, start_mark yaml_mark_t, s *[]byte) bool {
  1882. // Decode the required number of characters.
  1883. w := 1024
  1884. for w > 0 {
  1885. // Check for a URI-escaped octet.
  1886. if parser.unread < 3 && !yaml_parser_update_buffer(parser, 3) {
  1887. return false
  1888. }
  1889. if !(parser.buffer[parser.buffer_pos] == '%' &&
  1890. is_hex(parser.buffer, parser.buffer_pos+1) &&
  1891. is_hex(parser.buffer, parser.buffer_pos+2)) {
  1892. return yaml_parser_set_scanner_tag_error(parser, directive,
  1893. start_mark, "did not find URI escaped octet")
  1894. }
  1895. // Get the octet.
  1896. octet := byte((as_hex(parser.buffer, parser.buffer_pos+1) << 4) + as_hex(parser.buffer, parser.buffer_pos+2))
  1897. // If it is the leading octet, determine the length of the UTF-8 sequence.
  1898. if w == 1024 {
  1899. w = width(octet)
  1900. if w == 0 {
  1901. return yaml_parser_set_scanner_tag_error(parser, directive,
  1902. start_mark, "found an incorrect leading UTF-8 octet")
  1903. }
  1904. } else {
  1905. // Check if the trailing octet is correct.
  1906. if octet&0xC0 != 0x80 {
  1907. return yaml_parser_set_scanner_tag_error(parser, directive,
  1908. start_mark, "found an incorrect trailing UTF-8 octet")
  1909. }
  1910. }
  1911. // Copy the octet and move the pointers.
  1912. *s = append(*s, octet)
  1913. skip(parser)
  1914. skip(parser)
  1915. skip(parser)
  1916. w--
  1917. }
  1918. return true
  1919. }
  1920. // Scan a block scalar.
  1921. func yaml_parser_scan_block_scalar(parser *yaml_parser_t, token *yaml_token_t, literal bool) bool {
  1922. // Eat the indicator '|' or '>'.
  1923. start_mark := parser.mark
  1924. skip(parser)
  1925. // Scan the additional block scalar indicators.
  1926. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1927. return false
  1928. }
  1929. // Check for a chomping indicator.
  1930. var chomping, increment int
  1931. if parser.buffer[parser.buffer_pos] == '+' || parser.buffer[parser.buffer_pos] == '-' {
  1932. // Set the chomping method and eat the indicator.
  1933. if parser.buffer[parser.buffer_pos] == '+' {
  1934. chomping = +1
  1935. } else {
  1936. chomping = -1
  1937. }
  1938. skip(parser)
  1939. // Check for an indentation indicator.
  1940. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1941. return false
  1942. }
  1943. if is_digit(parser.buffer, parser.buffer_pos) {
  1944. // Check that the indentation is greater than 0.
  1945. if parser.buffer[parser.buffer_pos] == '0' {
  1946. yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
  1947. start_mark, "found an indentation indicator equal to 0")
  1948. return false
  1949. }
  1950. // Get the indentation level and eat the indicator.
  1951. increment = as_digit(parser.buffer, parser.buffer_pos)
  1952. skip(parser)
  1953. }
  1954. } else if is_digit(parser.buffer, parser.buffer_pos) {
  1955. // Do the same as above, but in the opposite order.
  1956. if parser.buffer[parser.buffer_pos] == '0' {
  1957. yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
  1958. start_mark, "found an indentation indicator equal to 0")
  1959. return false
  1960. }
  1961. increment = as_digit(parser.buffer, parser.buffer_pos)
  1962. skip(parser)
  1963. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1964. return false
  1965. }
  1966. if parser.buffer[parser.buffer_pos] == '+' || parser.buffer[parser.buffer_pos] == '-' {
  1967. if parser.buffer[parser.buffer_pos] == '+' {
  1968. chomping = +1
  1969. } else {
  1970. chomping = -1
  1971. }
  1972. skip(parser)
  1973. }
  1974. }
  1975. // Eat whitespaces and comments to the end of the line.
  1976. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1977. return false
  1978. }
  1979. for is_blank(parser.buffer, parser.buffer_pos) {
  1980. skip(parser)
  1981. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  1982. return false
  1983. }
  1984. }
  1985. if parser.buffer[parser.buffer_pos] == '#' {
  1986. if !yaml_parser_scan_line_comment(parser, start_mark) {
  1987. return false
  1988. }
  1989. }
  1990. // Check if we are at the end of the line.
  1991. if !is_breakz(parser.buffer, parser.buffer_pos) {
  1992. yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
  1993. start_mark, "did not find expected comment or line break")
  1994. return false
  1995. }
  1996. // Eat a line break.
  1997. if is_break(parser.buffer, parser.buffer_pos) {
  1998. if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
  1999. return false
  2000. }
  2001. skip_line(parser)
  2002. }
  2003. end_mark := parser.mark
  2004. // Set the indentation level if it was specified.
  2005. var indent int
  2006. if increment > 0 {
  2007. if parser.indent >= 0 {
  2008. indent = parser.indent + increment
  2009. } else {
  2010. indent = increment
  2011. }
  2012. }
  2013. // Scan the leading line breaks and determine the indentation level if needed.
  2014. var s, leading_break, trailing_breaks []byte
  2015. if !yaml_parser_scan_block_scalar_breaks(parser, &indent, &trailing_breaks, start_mark, &end_mark) {
  2016. return false
  2017. }
  2018. // Scan the block scalar content.
  2019. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  2020. return false
  2021. }
  2022. var leading_blank, trailing_blank bool
  2023. for parser.mark.column == indent && !is_z(parser.buffer, parser.buffer_pos) {
  2024. // We are at the beginning of a non-empty line.
  2025. // Is it a trailing whitespace?
  2026. trailing_blank = is_blank(parser.buffer, parser.buffer_pos)
  2027. // Check if we need to fold the leading line break.
  2028. if !literal && !leading_blank && !trailing_blank && len(leading_break) > 0 && leading_break[0] == '\n' {
  2029. // Do we need to join the lines by space?
  2030. if len(trailing_breaks) == 0 {
  2031. s = append(s, ' ')
  2032. }
  2033. } else {
  2034. s = append(s, leading_break...)
  2035. }
  2036. leading_break = leading_break[:0]
  2037. // Append the remaining line breaks.
  2038. s = append(s, trailing_breaks...)
  2039. trailing_breaks = trailing_breaks[:0]
  2040. // Is it a leading whitespace?
  2041. leading_blank = is_blank(parser.buffer, parser.buffer_pos)
  2042. // Consume the current line.
  2043. for !is_breakz(parser.buffer, parser.buffer_pos) {
  2044. s = read(parser, s)
  2045. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  2046. return false
  2047. }
  2048. }
  2049. // Consume the line break.
  2050. if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
  2051. return false
  2052. }
  2053. leading_break = read_line(parser, leading_break)
  2054. // Eat the following indentation spaces and line breaks.
  2055. if !yaml_parser_scan_block_scalar_breaks(parser, &indent, &trailing_breaks, start_mark, &end_mark) {
  2056. return false
  2057. }
  2058. }
  2059. // Chomp the tail.
  2060. if chomping != -1 {
  2061. s = append(s, leading_break...)
  2062. }
  2063. if chomping == 1 {
  2064. s = append(s, trailing_breaks...)
  2065. }
  2066. // Create a token.
  2067. *token = yaml_token_t{
  2068. typ: yaml_SCALAR_TOKEN,
  2069. start_mark: start_mark,
  2070. end_mark: end_mark,
  2071. value: s,
  2072. style: yaml_LITERAL_SCALAR_STYLE,
  2073. }
  2074. if !literal {
  2075. token.style = yaml_FOLDED_SCALAR_STYLE
  2076. }
  2077. return true
  2078. }
  2079. // Scan indentation spaces and line breaks for a block scalar. Determine the
  2080. // indentation level if needed.
  2081. 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 {
  2082. *end_mark = parser.mark
  2083. // Eat the indentation spaces and line breaks.
  2084. max_indent := 0
  2085. for {
  2086. // Eat the indentation spaces.
  2087. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  2088. return false
  2089. }
  2090. for (*indent == 0 || parser.mark.column < *indent) && is_space(parser.buffer, parser.buffer_pos) {
  2091. skip(parser)
  2092. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  2093. return false
  2094. }
  2095. }
  2096. if parser.mark.column > max_indent {
  2097. max_indent = parser.mark.column
  2098. }
  2099. // Check for a tab character messing the indentation.
  2100. if (*indent == 0 || parser.mark.column < *indent) && is_tab(parser.buffer, parser.buffer_pos) {
  2101. return yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
  2102. start_mark, "found a tab character where an indentation space is expected")
  2103. }
  2104. // Have we found a non-empty line?
  2105. if !is_break(parser.buffer, parser.buffer_pos) {
  2106. break
  2107. }
  2108. // Consume the line break.
  2109. if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
  2110. return false
  2111. }
  2112. // [Go] Should really be returning breaks instead.
  2113. *breaks = read_line(parser, *breaks)
  2114. *end_mark = parser.mark
  2115. }
  2116. // Determine the indentation level if needed.
  2117. if *indent == 0 {
  2118. *indent = max_indent
  2119. if *indent < parser.indent+1 {
  2120. *indent = parser.indent + 1
  2121. }
  2122. if *indent < 1 {
  2123. *indent = 1
  2124. }
  2125. }
  2126. return true
  2127. }
  2128. // Scan a quoted scalar.
  2129. func yaml_parser_scan_flow_scalar(parser *yaml_parser_t, token *yaml_token_t, single bool) bool {
  2130. // Eat the left quote.
  2131. start_mark := parser.mark
  2132. skip(parser)
  2133. // Consume the content of the quoted scalar.
  2134. var s, leading_break, trailing_breaks, whitespaces []byte
  2135. for {
  2136. // Check that there are no document indicators at the beginning of the line.
  2137. if parser.unread < 4 && !yaml_parser_update_buffer(parser, 4) {
  2138. return false
  2139. }
  2140. if parser.mark.column == 0 &&
  2141. ((parser.buffer[parser.buffer_pos+0] == '-' &&
  2142. parser.buffer[parser.buffer_pos+1] == '-' &&
  2143. parser.buffer[parser.buffer_pos+2] == '-') ||
  2144. (parser.buffer[parser.buffer_pos+0] == '.' &&
  2145. parser.buffer[parser.buffer_pos+1] == '.' &&
  2146. parser.buffer[parser.buffer_pos+2] == '.')) &&
  2147. is_blankz(parser.buffer, parser.buffer_pos+3) {
  2148. yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
  2149. start_mark, "found unexpected document indicator")
  2150. return false
  2151. }
  2152. // Check for EOF.
  2153. if is_z(parser.buffer, parser.buffer_pos) {
  2154. yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
  2155. start_mark, "found unexpected end of stream")
  2156. return false
  2157. }
  2158. // Consume non-blank characters.
  2159. leading_blanks := false
  2160. for !is_blankz(parser.buffer, parser.buffer_pos) {
  2161. if single && parser.buffer[parser.buffer_pos] == '\'' && parser.buffer[parser.buffer_pos+1] == '\'' {
  2162. // Is is an escaped single quote.
  2163. s = append(s, '\'')
  2164. skip(parser)
  2165. skip(parser)
  2166. } else if single && parser.buffer[parser.buffer_pos] == '\'' {
  2167. // It is a right single quote.
  2168. break
  2169. } else if !single && parser.buffer[parser.buffer_pos] == '"' {
  2170. // It is a right double quote.
  2171. break
  2172. } else if !single && parser.buffer[parser.buffer_pos] == '\\' && is_break(parser.buffer, parser.buffer_pos+1) {
  2173. // It is an escaped line break.
  2174. if parser.unread < 3 && !yaml_parser_update_buffer(parser, 3) {
  2175. return false
  2176. }
  2177. skip(parser)
  2178. skip_line(parser)
  2179. leading_blanks = true
  2180. break
  2181. } else if !single && parser.buffer[parser.buffer_pos] == '\\' {
  2182. // It is an escape sequence.
  2183. code_length := 0
  2184. // Check the escape character.
  2185. switch parser.buffer[parser.buffer_pos+1] {
  2186. case '0':
  2187. s = append(s, 0)
  2188. case 'a':
  2189. s = append(s, '\x07')
  2190. case 'b':
  2191. s = append(s, '\x08')
  2192. case 't', '\t':
  2193. s = append(s, '\x09')
  2194. case 'n':
  2195. s = append(s, '\x0A')
  2196. case 'v':
  2197. s = append(s, '\x0B')
  2198. case 'f':
  2199. s = append(s, '\x0C')
  2200. case 'r':
  2201. s = append(s, '\x0D')
  2202. case 'e':
  2203. s = append(s, '\x1B')
  2204. case ' ':
  2205. s = append(s, '\x20')
  2206. case '"':
  2207. s = append(s, '"')
  2208. case '\'':
  2209. s = append(s, '\'')
  2210. case '\\':
  2211. s = append(s, '\\')
  2212. case 'N': // NEL (#x85)
  2213. s = append(s, '\xC2')
  2214. s = append(s, '\x85')
  2215. case '_': // #xA0
  2216. s = append(s, '\xC2')
  2217. s = append(s, '\xA0')
  2218. case 'L': // LS (#x2028)
  2219. s = append(s, '\xE2')
  2220. s = append(s, '\x80')
  2221. s = append(s, '\xA8')
  2222. case 'P': // PS (#x2029)
  2223. s = append(s, '\xE2')
  2224. s = append(s, '\x80')
  2225. s = append(s, '\xA9')
  2226. case 'x':
  2227. code_length = 2
  2228. case 'u':
  2229. code_length = 4
  2230. case 'U':
  2231. code_length = 8
  2232. default:
  2233. yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
  2234. start_mark, "found unknown escape character")
  2235. return false
  2236. }
  2237. skip(parser)
  2238. skip(parser)
  2239. // Consume an arbitrary escape code.
  2240. if code_length > 0 {
  2241. var value int
  2242. // Scan the character value.
  2243. if parser.unread < code_length && !yaml_parser_update_buffer(parser, code_length) {
  2244. return false
  2245. }
  2246. for k := 0; k < code_length; k++ {
  2247. if !is_hex(parser.buffer, parser.buffer_pos+k) {
  2248. yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
  2249. start_mark, "did not find expected hexdecimal number")
  2250. return false
  2251. }
  2252. value = (value << 4) + as_hex(parser.buffer, parser.buffer_pos+k)
  2253. }
  2254. // Check the value and write the character.
  2255. if (value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF {
  2256. yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
  2257. start_mark, "found invalid Unicode character escape code")
  2258. return false
  2259. }
  2260. if value <= 0x7F {
  2261. s = append(s, byte(value))
  2262. } else if value <= 0x7FF {
  2263. s = append(s, byte(0xC0+(value>>6)))
  2264. s = append(s, byte(0x80+(value&0x3F)))
  2265. } else if value <= 0xFFFF {
  2266. s = append(s, byte(0xE0+(value>>12)))
  2267. s = append(s, byte(0x80+((value>>6)&0x3F)))
  2268. s = append(s, byte(0x80+(value&0x3F)))
  2269. } else {
  2270. s = append(s, byte(0xF0+(value>>18)))
  2271. s = append(s, byte(0x80+((value>>12)&0x3F)))
  2272. s = append(s, byte(0x80+((value>>6)&0x3F)))
  2273. s = append(s, byte(0x80+(value&0x3F)))
  2274. }
  2275. // Advance the pointer.
  2276. for k := 0; k < code_length; k++ {
  2277. skip(parser)
  2278. }
  2279. }
  2280. } else {
  2281. // It is a non-escaped non-blank character.
  2282. s = read(parser, s)
  2283. }
  2284. if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
  2285. return false
  2286. }
  2287. }
  2288. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  2289. return false
  2290. }
  2291. // Check if we are at the end of the scalar.
  2292. if single {
  2293. if parser.buffer[parser.buffer_pos] == '\'' {
  2294. break
  2295. }
  2296. } else {
  2297. if parser.buffer[parser.buffer_pos] == '"' {
  2298. break
  2299. }
  2300. }
  2301. // Consume blank characters.
  2302. for is_blank(parser.buffer, parser.buffer_pos) || is_break(parser.buffer, parser.buffer_pos) {
  2303. if is_blank(parser.buffer, parser.buffer_pos) {
  2304. // Consume a space or a tab character.
  2305. if !leading_blanks {
  2306. whitespaces = read(parser, whitespaces)
  2307. } else {
  2308. skip(parser)
  2309. }
  2310. } else {
  2311. if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
  2312. return false
  2313. }
  2314. // Check if it is a first line break.
  2315. if !leading_blanks {
  2316. whitespaces = whitespaces[:0]
  2317. leading_break = read_line(parser, leading_break)
  2318. leading_blanks = true
  2319. } else {
  2320. trailing_breaks = read_line(parser, trailing_breaks)
  2321. }
  2322. }
  2323. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  2324. return false
  2325. }
  2326. }
  2327. // Join the whitespaces or fold line breaks.
  2328. if leading_blanks {
  2329. // Do we need to fold line breaks?
  2330. if len(leading_break) > 0 && leading_break[0] == '\n' {
  2331. if len(trailing_breaks) == 0 {
  2332. s = append(s, ' ')
  2333. } else {
  2334. s = append(s, trailing_breaks...)
  2335. }
  2336. } else {
  2337. s = append(s, leading_break...)
  2338. s = append(s, trailing_breaks...)
  2339. }
  2340. trailing_breaks = trailing_breaks[:0]
  2341. leading_break = leading_break[:0]
  2342. } else {
  2343. s = append(s, whitespaces...)
  2344. whitespaces = whitespaces[:0]
  2345. }
  2346. }
  2347. // Eat the right quote.
  2348. skip(parser)
  2349. end_mark := parser.mark
  2350. // Create a token.
  2351. *token = yaml_token_t{
  2352. typ: yaml_SCALAR_TOKEN,
  2353. start_mark: start_mark,
  2354. end_mark: end_mark,
  2355. value: s,
  2356. style: yaml_SINGLE_QUOTED_SCALAR_STYLE,
  2357. }
  2358. if !single {
  2359. token.style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
  2360. }
  2361. return true
  2362. }
  2363. // Scan a plain scalar.
  2364. func yaml_parser_scan_plain_scalar(parser *yaml_parser_t, token *yaml_token_t) bool {
  2365. var s, leading_break, trailing_breaks, whitespaces []byte
  2366. var leading_blanks bool
  2367. var indent = parser.indent + 1
  2368. start_mark := parser.mark
  2369. end_mark := parser.mark
  2370. // Consume the content of the plain scalar.
  2371. for {
  2372. // Check for a document indicator.
  2373. if parser.unread < 4 && !yaml_parser_update_buffer(parser, 4) {
  2374. return false
  2375. }
  2376. if parser.mark.column == 0 &&
  2377. ((parser.buffer[parser.buffer_pos+0] == '-' &&
  2378. parser.buffer[parser.buffer_pos+1] == '-' &&
  2379. parser.buffer[parser.buffer_pos+2] == '-') ||
  2380. (parser.buffer[parser.buffer_pos+0] == '.' &&
  2381. parser.buffer[parser.buffer_pos+1] == '.' &&
  2382. parser.buffer[parser.buffer_pos+2] == '.')) &&
  2383. is_blankz(parser.buffer, parser.buffer_pos+3) {
  2384. break
  2385. }
  2386. // Check for a comment.
  2387. if parser.buffer[parser.buffer_pos] == '#' {
  2388. break
  2389. }
  2390. // Consume non-blank characters.
  2391. for !is_blankz(parser.buffer, parser.buffer_pos) {
  2392. // Check for indicators that may end a plain scalar.
  2393. if (parser.buffer[parser.buffer_pos] == ':' && is_blankz(parser.buffer, parser.buffer_pos+1)) ||
  2394. (parser.flow_level > 0 &&
  2395. (parser.buffer[parser.buffer_pos] == ',' ||
  2396. parser.buffer[parser.buffer_pos] == '?' || parser.buffer[parser.buffer_pos] == '[' ||
  2397. parser.buffer[parser.buffer_pos] == ']' || parser.buffer[parser.buffer_pos] == '{' ||
  2398. parser.buffer[parser.buffer_pos] == '}')) {
  2399. break
  2400. }
  2401. // Check if we need to join whitespaces and breaks.
  2402. if leading_blanks || len(whitespaces) > 0 {
  2403. if leading_blanks {
  2404. // Do we need to fold line breaks?
  2405. if leading_break[0] == '\n' {
  2406. if len(trailing_breaks) == 0 {
  2407. s = append(s, ' ')
  2408. } else {
  2409. s = append(s, trailing_breaks...)
  2410. }
  2411. } else {
  2412. s = append(s, leading_break...)
  2413. s = append(s, trailing_breaks...)
  2414. }
  2415. trailing_breaks = trailing_breaks[:0]
  2416. leading_break = leading_break[:0]
  2417. leading_blanks = false
  2418. } else {
  2419. s = append(s, whitespaces...)
  2420. whitespaces = whitespaces[:0]
  2421. }
  2422. }
  2423. // Copy the character.
  2424. s = read(parser, s)
  2425. end_mark = parser.mark
  2426. if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
  2427. return false
  2428. }
  2429. }
  2430. // Is it the end?
  2431. if !(is_blank(parser.buffer, parser.buffer_pos) || is_break(parser.buffer, parser.buffer_pos)) {
  2432. break
  2433. }
  2434. // Consume blank characters.
  2435. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  2436. return false
  2437. }
  2438. for is_blank(parser.buffer, parser.buffer_pos) || is_break(parser.buffer, parser.buffer_pos) {
  2439. if is_blank(parser.buffer, parser.buffer_pos) {
  2440. // Check for tab characters that abuse indentation.
  2441. if leading_blanks && parser.mark.column < indent && is_tab(parser.buffer, parser.buffer_pos) {
  2442. yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
  2443. start_mark, "found a tab character that violates indentation")
  2444. return false
  2445. }
  2446. // Consume a space or a tab character.
  2447. if !leading_blanks {
  2448. whitespaces = read(parser, whitespaces)
  2449. } else {
  2450. skip(parser)
  2451. }
  2452. } else {
  2453. if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
  2454. return false
  2455. }
  2456. // Check if it is a first line break.
  2457. if !leading_blanks {
  2458. whitespaces = whitespaces[:0]
  2459. leading_break = read_line(parser, leading_break)
  2460. leading_blanks = true
  2461. } else {
  2462. trailing_breaks = read_line(parser, trailing_breaks)
  2463. }
  2464. }
  2465. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  2466. return false
  2467. }
  2468. }
  2469. // Check indentation level.
  2470. if parser.flow_level == 0 && parser.mark.column < indent {
  2471. break
  2472. }
  2473. }
  2474. // Create a token.
  2475. *token = yaml_token_t{
  2476. typ: yaml_SCALAR_TOKEN,
  2477. start_mark: start_mark,
  2478. end_mark: end_mark,
  2479. value: s,
  2480. style: yaml_PLAIN_SCALAR_STYLE,
  2481. }
  2482. // Note that we change the 'simple_key_allowed' flag.
  2483. if leading_blanks {
  2484. parser.simple_key_allowed = true
  2485. }
  2486. return true
  2487. }
  2488. func yaml_parser_scan_line_comment(parser *yaml_parser_t, token_mark yaml_mark_t) bool {
  2489. if parser.newlines > 0 {
  2490. return true
  2491. }
  2492. parser.comments = append(parser.comments, yaml_comment_t{token_mark: token_mark})
  2493. comment := &parser.comments[len(parser.comments)-1].line
  2494. for peek := 0; peek < 512; peek++ {
  2495. if parser.unread < peek+1 && !yaml_parser_update_buffer(parser, peek+1) {
  2496. break
  2497. }
  2498. if is_blank(parser.buffer, parser.buffer_pos+peek) {
  2499. continue
  2500. }
  2501. if parser.buffer[parser.buffer_pos+peek] == '#' {
  2502. if len(*comment) > 0 {
  2503. *comment = append(*comment, '\n')
  2504. }
  2505. // Consume until after the consumed comment line.
  2506. seen := parser.mark.index+peek
  2507. for {
  2508. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  2509. return false
  2510. }
  2511. if is_breakz(parser.buffer, parser.buffer_pos) {
  2512. if parser.mark.index >= seen {
  2513. break
  2514. }
  2515. if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
  2516. return false
  2517. }
  2518. skip_line(parser)
  2519. } else {
  2520. if parser.mark.index >= seen {
  2521. *comment = append(*comment, parser.buffer[parser.buffer_pos])
  2522. }
  2523. skip(parser)
  2524. }
  2525. }
  2526. }
  2527. break
  2528. }
  2529. return true
  2530. }
  2531. func yaml_parser_scan_comments(parser *yaml_parser_t, scan_mark yaml_mark_t) bool {
  2532. token := parser.tokens[len(parser.tokens)-1]
  2533. if token.typ == yaml_FLOW_ENTRY_TOKEN && len(parser.tokens) > 1 {
  2534. token = parser.tokens[len(parser.tokens)-2]
  2535. }
  2536. var token_mark = token.start_mark
  2537. var start_mark yaml_mark_t
  2538. var recent_empty = false
  2539. var first_empty = parser.newlines <= 1
  2540. var line = parser.mark.line
  2541. var column = parser.mark.column
  2542. var text []byte
  2543. // The foot line is the place where a comment must start to
  2544. // still be considered as a foot of the prior content.
  2545. // If there's some content in the currently parsed line, then
  2546. // the foot is the line below it.
  2547. var foot_line = -1
  2548. if scan_mark.line > 0 {
  2549. foot_line = parser.mark.line-parser.newlines+1
  2550. if parser.newlines == 0 && parser.mark.column > 1 {
  2551. foot_line++
  2552. }
  2553. }
  2554. var peek = 0
  2555. for ; peek < 512; peek++ {
  2556. if parser.unread < peek+1 && !yaml_parser_update_buffer(parser, peek+1) {
  2557. break
  2558. }
  2559. column++
  2560. if is_blank(parser.buffer, parser.buffer_pos+peek) {
  2561. continue
  2562. }
  2563. c := parser.buffer[parser.buffer_pos+peek]
  2564. if is_breakz(parser.buffer, parser.buffer_pos+peek) || parser.flow_level > 0 && (c == ']' || c == '}') {
  2565. // Got line break or terminator.
  2566. if !recent_empty {
  2567. if first_empty && (start_mark.line == foot_line || start_mark.column-1 < parser.indent) {
  2568. // This is the first empty line and there were no empty lines before,
  2569. // so this initial part of the comment is a foot of the prior token
  2570. // instead of being a head for the following one. Split it up.
  2571. if len(text) > 0 {
  2572. if start_mark.column-1 < parser.indent {
  2573. // If dedented it's unrelated to the prior token.
  2574. token_mark = start_mark
  2575. }
  2576. parser.comments = append(parser.comments, yaml_comment_t{
  2577. scan_mark: scan_mark,
  2578. token_mark: token_mark,
  2579. start_mark: start_mark,
  2580. end_mark: yaml_mark_t{parser.mark.index + peek, line, column},
  2581. foot: text,
  2582. })
  2583. scan_mark = yaml_mark_t{parser.mark.index + peek, line, column}
  2584. token_mark = scan_mark
  2585. text = nil
  2586. }
  2587. } else {
  2588. if len(text) > 0 && parser.buffer[parser.buffer_pos+peek] != 0 {
  2589. text = append(text, '\n')
  2590. }
  2591. }
  2592. }
  2593. if !is_break(parser.buffer, parser.buffer_pos+peek) {
  2594. break
  2595. }
  2596. first_empty = false
  2597. recent_empty = true
  2598. column = 0
  2599. line++
  2600. continue
  2601. }
  2602. if len(text) > 0 && column < parser.indent+1 && column != start_mark.column {
  2603. // The comment at the different indentation is a foot of the
  2604. // preceding data rather than a head of the upcoming one.
  2605. parser.comments = append(parser.comments, yaml_comment_t{
  2606. scan_mark: scan_mark,
  2607. token_mark: token_mark,
  2608. start_mark: start_mark,
  2609. end_mark: yaml_mark_t{parser.mark.index + peek, line, column},
  2610. foot: text,
  2611. })
  2612. scan_mark = yaml_mark_t{parser.mark.index + peek, line, column}
  2613. token_mark = scan_mark
  2614. text = nil
  2615. }
  2616. if parser.buffer[parser.buffer_pos+peek] != '#' {
  2617. break
  2618. }
  2619. if len(text) == 0 {
  2620. start_mark = yaml_mark_t{parser.mark.index + peek, line, column}
  2621. } else {
  2622. text = append(text, '\n')
  2623. }
  2624. recent_empty = false
  2625. // Consume until after the consumed comment line.
  2626. seen := parser.mark.index+peek
  2627. for {
  2628. if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
  2629. return false
  2630. }
  2631. if is_breakz(parser.buffer, parser.buffer_pos) {
  2632. if parser.mark.index >= seen {
  2633. break
  2634. }
  2635. if parser.unread < 2 && !yaml_parser_update_buffer(parser, 2) {
  2636. return false
  2637. }
  2638. skip_line(parser)
  2639. } else {
  2640. if parser.mark.index >= seen {
  2641. text = append(text, parser.buffer[parser.buffer_pos])
  2642. }
  2643. skip(parser)
  2644. }
  2645. }
  2646. peek = 0
  2647. column = 0
  2648. line = parser.mark.line
  2649. }
  2650. if len(text) > 0 {
  2651. parser.comments = append(parser.comments, yaml_comment_t{
  2652. scan_mark: scan_mark,
  2653. token_mark: start_mark,
  2654. start_mark: start_mark,
  2655. end_mark: yaml_mark_t{parser.mark.index + peek - 1, line, column},
  2656. head: text,
  2657. })
  2658. }
  2659. return true
  2660. }