scanner.c 96 KB

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