scannerc.go 75 KB

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