scannerc.go 76 KB

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