scannerc.go 75 KB

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