| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570 |
- /*
- * Introduction
- * ************
- *
- * The following notes assume that you are familiar with the YAML specification
- * (http://yaml.org/spec/cvs/current.html). We mostly follow it, although in
- * some cases we are less restrictive that it requires.
- *
- * The process of transforming a YAML stream into a sequence of events is
- * divided on two steps: Scanning and Parsing.
- *
- * The Scanner transforms the input stream into a sequence of tokens, while the
- * parser transform the sequence of tokens produced by the Scanner into a
- * sequence of parsing events.
- *
- * The Scanner is rather clever and complicated. The Parser, on the contrary,
- * is a straightforward implementation of a recursive-descendant parser (or,
- * LL(1) parser, as it is usually called).
- *
- * Actually there are two issues of Scanning that might be called "clever", the
- * rest is quite straightforward. The issues are "block collection start" and
- * "simple keys". Both issues are explained below in details.
- *
- * Here the Scanning step is explained and implemented. We start with the list
- * of all the tokens produced by the Scanner together with short descriptions.
- *
- * Now, tokens:
- *
- * STREAM-START(encoding) # The stream start.
- * STREAM-END # The stream end.
- * VERSION-DIRECTIVE(major,minor) # The '%YAML' directive.
- * TAG-DIRECTIVE(handle,prefix) # The '%TAG' directive.
- * DOCUMENT-START # '---'
- * DOCUMENT-END # '...'
- * BLOCK-SEQUENCE-START # Indentation increase denoting a block
- * BLOCK-MAPPING-START # sequence or a block mapping.
- * BLOCK-END # Indentation decrease.
- * FLOW-SEQUENCE-START # '['
- * FLOW-SEQUENCE-END # ']'
- * BLOCK-SEQUENCE-START # '{'
- * BLOCK-SEQUENCE-END # '}'
- * BLOCK-ENTRY # '-'
- * FLOW-ENTRY # ','
- * KEY # '?' or nothing (simple keys).
- * VALUE # ':'
- * ALIAS(anchor) # '*anchor'
- * ANCHOR(anchor) # '&anchor'
- * TAG(handle,suffix) # '!handle!suffix'
- * SCALAR(value,style) # A scalar.
- *
- * The following two tokens are "virtual" tokens denoting the beginning and the
- * end of the stream:
- *
- * STREAM-START(encoding)
- * STREAM-END
- *
- * We pass the information about the input stream encoding with the
- * STREAM-START token.
- *
- * The next two tokens are responsible for tags:
- *
- * VERSION-DIRECTIVE(major,minor)
- * TAG-DIRECTIVE(handle,prefix)
- *
- * Example:
- *
- * %YAML 1.1
- * %TAG ! !foo
- * %TAG !yaml! tag:yaml.org,2002:
- * ---
- *
- * The correspoding sequence of tokens:
- *
- * STREAM-START(utf-8)
- * VERSION-DIRECTIVE(1,1)
- * TAG-DIRECTIVE("!","!foo")
- * TAG-DIRECTIVE("!yaml","tag:yaml.org,2002:")
- * DOCUMENT-START
- * STREAM-END
- *
- * Note that the VERSION-DIRECTIVE and TAG-DIRECTIVE tokens occupy a whole
- * line.
- *
- * The document start and end indicators are represented by:
- *
- * DOCUMENT-START
- * DOCUMENT-END
- *
- * Note that if a YAML stream contains an implicit document (without '---'
- * and '...' indicators), no DOCUMENT-START and DOCUMENT-END tokens will be
- * produced.
- *
- * In the following examples, we present whole documents together with the
- * produced tokens.
- *
- * 1. An implicit document:
- *
- * 'a scalar'
- *
- * Tokens:
- *
- * STREAM-START(utf-8)
- * SCALAR("a scalar",single-quoted)
- * STREAM-END
- *
- * 2. An explicit document:
- *
- * ---
- * 'a scalar'
- * ...
- *
- * Tokens:
- *
- * STREAM-START(utf-8)
- * DOCUMENT-START
- * SCALAR("a scalar",single-quoted)
- * DOCUMENT-END
- * STREAM-END
- *
- * 3. Several documents in a stream:
- *
- * 'a scalar'
- * ---
- * 'another scalar'
- * ---
- * 'yet another scalar'
- *
- * Tokens:
- *
- * STREAM-START(utf-8)
- * SCALAR("a scalar",single-quoted)
- * DOCUMENT-START
- * SCALAR("another scalar",single-quoted)
- * DOCUMENT-START
- * SCALAR("yet another scalar",single-quoted)
- * STREAM-END
- *
- * We have already introduced the SCALAR token above. The following tokens are
- * used to describe aliases, anchors, tag, and scalars:
- *
- * ALIAS(anchor)
- * ANCHOR(anchor)
- * TAG(handle,suffix)
- * SCALAR(value,style)
- *
- * The following series of examples illustrate the usage of these tokens:
- *
- * 1. A recursive sequence:
- *
- * &A [ *A ]
- *
- * Tokens:
- *
- * STREAM-START(utf-8)
- * ANCHOR("A")
- * FLOW-SEQUENCE-START
- * ALIAS("A")
- * FLOW-SEQUENCE-END
- * STREAM-END
- *
- * 2. A tagged scalar:
- *
- * !!float "3.14" # A good approximation.
- *
- * Tokens:
- *
- * STREAM-START(utf-8)
- * TAG("!!","float")
- * SCALAR("3.14",double-quoted)
- * STREAM-END
- *
- * 3. Various scalar styles:
- *
- * --- # Implicit empty plain scalars do not produce tokens.
- * --- a plain scalar
- * --- 'a single-quoted scalar'
- * --- "a double-quoted scalar"
- * --- |-
- * a literal scalar
- * --- >-
- * a folded
- * scalar
- *
- * Tokens:
- *
- * STREAM-START(utf-8)
- * DOCUMENT-START
- * DOCUMENT-START
- * SCALAR("a plain scalar",plain)
- * DOCUMENT-START
- * SCALAR("a single-quoted scalar",single-quoted)
- * DOCUMENT-START
- * SCALAR("a double-quoted scalar",double-quoted)
- * DOCUMENT-START
- * SCALAR("a literal scalar",literal)
- * DOCUMENT-START
- * SCALAR("a folded scalar",folded)
- * STREAM-END
- *
- * Now it's time to review collection-related tokens. We will start with
- * flow collections:
- *
- * FLOW-SEQUENCE-START
- * FLOW-SEQUENCE-END
- * FLOW-MAPPING-START
- * FLOW-MAPPING-END
- * FLOW-ENTRY
- * KEY
- * VALUE
- *
- * The tokens FLOW-SEQUENCE-START, FLOW-SEQUENCE-END, FLOW-MAPPING-START, and
- * FLOW-MAPPING-END represent the indicators '[', ']', '{', and '}'
- * correspondingly. FLOW-ENTRY represent the ',' indicator. Finally the
- * indicators '?' and ':', which are used for denoting mapping keys and values,
- * are represented by the KEY and VALUE tokens.
- *
- * The following examples show flow collections:
- *
- * 1. A flow sequence:
- *
- * [item 1, item 2, item 3]
- *
- * Tokens:
- *
- * STREAM-START(utf-8)
- * FLOW-SEQUENCE-START
- * SCALAR("item 1",plain)
- * FLOW-ENTRY
- * SCALAR("item 2",plain)
- * FLOW-ENTRY
- * SCALAR("item 3",plain)
- * FLOW-SEQUENCE-END
- * STREAM-END
- *
- * 2. A flow mapping:
- *
- * {
- * a simple key: a value, # Note that the KEY token is produced.
- * ? a complex key: another value,
- * }
- *
- * Tokens:
- *
- * STREAM-START(utf-8)
- * FLOW-MAPPING-START
- * KEY
- * SCALAR("a simple key",plain)
- * VALUE
- * SCALAR("a value",plain)
- * FLOW-ENTRY
- * KEY
- * SCALAR("a complex key",plain)
- * VALUE
- * SCALAR("another value",plain)
- * FLOW-ENTRY
- * FLOW-MAPPING-END
- * STREAM-END
- *
- * A simple key is a key which is not denoted by the '?' indicator. Note that
- * the Scanner still produce the KEY token whenever it encounters a simple key.
- *
- * For scanning block collections, the following tokens are used (note that we
- * repeat KEY and VALUE here):
- *
- * BLOCK-SEQUENCE-START
- * BLOCK-MAPPING-START
- * BLOCK-END
- * BLOCK-ENTRY
- * KEY
- * VALUE
- *
- * The tokens BLOCK-SEQUENCE-START and BLOCK-MAPPING-START denote indentation
- * increase that precedes a block collection (cf. the INDENT token in Python).
- * The token BLOCK-END denote indentation decrease that ends a block collection
- * (cf. the DEDENT token in Python). However YAML has some syntax pecularities
- * that makes detections of these tokens more complex.
- *
- * The tokens BLOCK-ENTRY, KEY, and VALUE are used to represent the indicators
- * '-', '?', and ':' correspondingly.
- *
- * The following examples show how the tokens BLOCK-SEQUENCE-START,
- * BLOCK-MAPPING-START, and BLOCK-END are emitted by the Scanner:
- *
- * 1. Block sequences:
- *
- * - item 1
- * - item 2
- * -
- * - item 3.1
- * - item 3.2
- * -
- * key 1: value 1
- * key 2: value 2
- *
- * Tokens:
- *
- * STREAM-START(utf-8)
- * BLOCK-SEQUENCE-START
- * BLOCK-ENTRY
- * SCALAR("item 1",plain)
- * BLOCK-ENTRY
- * SCALAR("item 2",plain)
- * BLOCK-ENTRY
- * BLOCK-SEQUENCE-START
- * BLOCK-ENTRY
- * SCALAR("item 3.1",plain)
- * BLOCK-ENTRY
- * SCALAR("item 3.2",plain)
- * BLOCK-END
- * BLOCK-ENTRY
- * BLOCK-MAPPING-START
- * KEY
- * SCALAR("key 1",plain)
- * VALUE
- * SCALAR("value 1",plain)
- * KEY
- * SCALAR("key 2",plain)
- * VALUE
- * SCALAR("value 2",plain)
- * BLOCK-END
- * BLOCK-END
- * STREAM-END
- *
- * 2. Block mappings:
- *
- * a simple key: a value # The KEY token is produced here.
- * ? a complex key
- * : another value
- * a mapping:
- * key 1: value 1
- * key 2: value 2
- * a sequence:
- * - item 1
- * - item 2
- *
- * Tokens:
- *
- * STREAM-START(utf-8)
- * BLOCK-MAPPING-START
- * KEY
- * SCALAR("a simple key",plain)
- * VALUE
- * SCALAR("a value",plain)
- * KEY
- * SCALAR("a complex key",plain)
- * VALUE
- * SCALAR("another value",plain)
- * KEY
- * SCALAR("a mapping",plain)
- * BLOCK-MAPPING-START
- * KEY
- * SCALAR("key 1",plain)
- * VALUE
- * SCALAR("value 1",plain)
- * KEY
- * SCALAR("key 2",plain)
- * VALUE
- * SCALAR("value 2",plain)
- * BLOCK-END
- * KEY
- * SCALAR("a sequence",plain)
- * VALUE
- * BLOCK-SEQUENCE-START
- * BLOCK-ENTRY
- * SCALAR("item 1",plain)
- * BLOCK-ENTRY
- * SCALAR("item 2",plain)
- * BLOCK-END
- * BLOCK-END
- * STREAM-END
- *
- * YAML does not always require to start a new block collection from a new
- * line. If the current line contains only '-', '?', and ':' indicators, a new
- * block collection may start at the current line. The following examples
- * illustrate this case:
- *
- * 1. Collections in a sequence:
- *
- * - - item 1
- * - item 2
- * - key 1: value 1
- * key 2: value 2
- * - ? complex key
- * : complex value
- *
- * Tokens:
- *
- * STREAM-START(utf-8)
- * BLOCK-SEQUENCE-START
- * BLOCK-ENTRY
- * BLOCK-SEQUENCE-START
- * BLOCK-ENTRY
- * SCALAR("item 1",plain)
- * BLOCK-ENTRY
- * SCALAR("item 2",plain)
- * BLOCK-END
- * BLOCK-ENTRY
- * BLOCK-MAPPING-START
- * KEY
- * SCALAR("key 1",plain)
- * VALUE
- * SCALAR("value 1",plain)
- * KEY
- * SCALAR("key 2",plain)
- * VALUE
- * SCALAR("value 2",plain)
- * BLOCK-END
- * BLOCK-ENTRY
- * BLOCK-MAPPING-START
- * KEY
- * SCALAR("complex key")
- * VALUE
- * SCALAR("complex value")
- * BLOCK-END
- * BLOCK-END
- * STREAM-END
- *
- * 2. Collections in a mapping:
- *
- * ? a sequence
- * : - item 1
- * - item 2
- * ? a mapping
- * : key 1: value 1
- * key 2: value 2
- *
- * Tokens:
- *
- * STREAM-START(utf-8)
- * BLOCK-MAPPING-START
- * KEY
- * SCALAR("a sequence",plain)
- * VALUE
- * BLOCK-SEQUENCE-START
- * BLOCK-ENTRY
- * SCALAR("item 1",plain)
- * BLOCK-ENTRY
- * SCALAR("item 2",plain)
- * BLOCK-END
- * KEY
- * SCALAR("a mapping",plain)
- * VALUE
- * BLOCK-MAPPING-START
- * KEY
- * SCALAR("key 1",plain)
- * VALUE
- * SCALAR("value 1",plain)
- * KEY
- * SCALAR("key 2",plain)
- * VALUE
- * SCALAR("value 2",plain)
- * BLOCK-END
- * BLOCK-END
- * STREAM-END
- *
- * YAML also permits non-indented sequences if they are included into a block
- * mapping. In this case, the token BLOCK-SEQUENCE-START is not produced:
- *
- * key:
- * - item 1 # BLOCK-SEQUENCE-START is NOT produced here.
- * - item 2
- *
- * Tokens:
- *
- * STREAM-START(utf-8)
- * BLOCK-MAPPING-START
- * KEY
- * SCALAR("key",plain)
- * VALUE
- * BLOCK-ENTRY
- * SCALAR("item 1",plain)
- * BLOCK-ENTRY
- * SCALAR("item 2",plain)
- * BLOCK-END
- */
- #include "yaml_private.h"
- /*
- * Ensure that the buffer contains the required number of characters.
- * Return 1 on success, 0 on failure (reader error or memory error).
- */
- #define CACHE(parser,length) \
- (parser->unread >= (length) \
- ? 1 \
- : yaml_parser_update_buffer(parser, (length)))
- /*
- * Advance the buffer pointer.
- */
- #define SKIP(parser) \
- (parser->mark.index ++, \
- parser->mark.column ++, \
- parser->unread --, \
- parser->buffer.pointer += WIDTH(parser->buffer))
- #define SKIP_LINE(parser) \
- (IS_CRLF(parser->buffer) ? \
- (parser->mark.index += 2, \
- parser->mark.column = 0, \
- parser->mark.line ++, \
- parser->unread -= 2, \
- parser->buffer.pointer += 2) : \
- IS_BREAK(parser->buffer) ? \
- (parser->mark.index ++, \
- parser->mark.column = 0, \
- parser->mark.line ++, \
- parser->unread --, \
- parser->buffer.pointer += WIDTH(parser->buffer)) : 0)
- /*
- * Copy a character to a string buffer and advance pointers.
- */
- #define READ(parser,string) \
- (STRING_EXTEND(parser,string) ? \
- (COPY(string,parser->buffer), \
- parser->mark.index ++, \
- parser->mark.column ++, \
- parser->unread --, \
- 1) : 0)
- /*
- * Copy a line break character to a string buffer and advance pointers.
- */
- #define READ_LINE(parser,string) \
- (STRING_EXTEND(parser,string) ? \
- (((CHECK_AT(parser->buffer,'\r',0) \
- && CHECK_AT(parser->buffer,'\n',1)) ? /* CR LF -> LF */ \
- (*((string).pointer++) = (yaml_char_t) '\n', \
- parser->buffer.pointer += 2, \
- parser->mark.index += 2, \
- parser->mark.column = 0, \
- parser->mark.line ++, \
- parser->unread -= 2) : \
- (CHECK_AT(parser->buffer,'\r',0) \
- || CHECK_AT(parser->buffer,'\n',0)) ? /* CR|LF -> LF */ \
- (*((string).pointer++) = (yaml_char_t) '\n', \
- parser->buffer.pointer ++, \
- parser->mark.index ++, \
- parser->mark.column = 0, \
- parser->mark.line ++, \
- parser->unread --) : \
- (CHECK_AT(parser->buffer,'\xC2',0) \
- && CHECK_AT(parser->buffer,'\x85',1)) ? /* NEL -> LF */ \
- (*((string).pointer++) = (yaml_char_t) '\n', \
- parser->buffer.pointer += 2, \
- parser->mark.index ++, \
- parser->mark.column = 0, \
- parser->mark.line ++, \
- parser->unread --) : \
- (CHECK_AT(parser->buffer,'\xE2',0) && \
- CHECK_AT(parser->buffer,'\x80',1) && \
- (CHECK_AT(parser->buffer,'\xA8',2) || \
- CHECK_AT(parser->buffer,'\xA9',2))) ? /* LS|PS -> LS|PS */ \
- (*((string).pointer++) = *(parser->buffer.pointer++), \
- *((string).pointer++) = *(parser->buffer.pointer++), \
- *((string).pointer++) = *(parser->buffer.pointer++), \
- parser->mark.index ++, \
- parser->mark.column = 0, \
- parser->mark.line ++, \
- parser->unread --) : 0), \
- 1) : 0)
- /*
- * Public API declarations.
- */
- YAML_DECLARE(int)
- yaml_parser_scan(yaml_parser_t *parser, yaml_token_t *token);
- /*
- * Error handling.
- */
- static int
- yaml_parser_set_scanner_error(yaml_parser_t *parser, const char *context,
- yaml_mark_t context_mark, const char *problem);
- /*
- * High-level token API.
- */
- YAML_DECLARE(int)
- yaml_parser_fetch_more_tokens(yaml_parser_t *parser);
- static int
- yaml_parser_fetch_next_token(yaml_parser_t *parser);
- /*
- * Potential simple keys.
- */
- static int
- yaml_parser_stale_simple_keys(yaml_parser_t *parser);
- static int
- yaml_parser_save_simple_key(yaml_parser_t *parser);
- static int
- yaml_parser_remove_simple_key(yaml_parser_t *parser);
- static int
- yaml_parser_increase_flow_level(yaml_parser_t *parser);
- static int
- yaml_parser_decrease_flow_level(yaml_parser_t *parser);
- /*
- * Indentation treatment.
- */
- static int
- yaml_parser_roll_indent(yaml_parser_t *parser, int column,
- int number, yaml_token_type_t type, yaml_mark_t mark);
- static int
- yaml_parser_unroll_indent(yaml_parser_t *parser, int column);
- /*
- * Token fetchers.
- */
- static int
- yaml_parser_fetch_stream_start(yaml_parser_t *parser);
- static int
- yaml_parser_fetch_stream_end(yaml_parser_t *parser);
- static int
- yaml_parser_fetch_directive(yaml_parser_t *parser);
- static int
- yaml_parser_fetch_document_indicator(yaml_parser_t *parser,
- yaml_token_type_t type);
- static int
- yaml_parser_fetch_flow_collection_start(yaml_parser_t *parser,
- yaml_token_type_t type);
- static int
- yaml_parser_fetch_flow_collection_end(yaml_parser_t *parser,
- yaml_token_type_t type);
- static int
- yaml_parser_fetch_flow_entry(yaml_parser_t *parser);
- static int
- yaml_parser_fetch_block_entry(yaml_parser_t *parser);
- static int
- yaml_parser_fetch_key(yaml_parser_t *parser);
- static int
- yaml_parser_fetch_value(yaml_parser_t *parser);
- static int
- yaml_parser_fetch_anchor(yaml_parser_t *parser, yaml_token_type_t type);
- static int
- yaml_parser_fetch_tag(yaml_parser_t *parser);
- static int
- yaml_parser_fetch_block_scalar(yaml_parser_t *parser, int literal);
- static int
- yaml_parser_fetch_flow_scalar(yaml_parser_t *parser, int single);
- static int
- yaml_parser_fetch_plain_scalar(yaml_parser_t *parser);
- /*
- * Token scanners.
- */
- static int
- yaml_parser_scan_to_next_token(yaml_parser_t *parser);
- static int
- yaml_parser_scan_directive(yaml_parser_t *parser, yaml_token_t *token);
- static int
- yaml_parser_scan_directive_name(yaml_parser_t *parser,
- yaml_mark_t start_mark, yaml_char_t **name);
- static int
- yaml_parser_scan_version_directive_value(yaml_parser_t *parser,
- yaml_mark_t start_mark, int *major, int *minor);
- static int
- yaml_parser_scan_version_directive_number(yaml_parser_t *parser,
- yaml_mark_t start_mark, int *number);
- static int
- yaml_parser_scan_tag_directive_value(yaml_parser_t *parser,
- yaml_mark_t mark, yaml_char_t **handle, yaml_char_t **prefix);
- static int
- yaml_parser_scan_anchor(yaml_parser_t *parser, yaml_token_t *token,
- yaml_token_type_t type);
- static int
- yaml_parser_scan_tag(yaml_parser_t *parser, yaml_token_t *token);
- static int
- yaml_parser_scan_tag_handle(yaml_parser_t *parser, int directive,
- yaml_mark_t start_mark, yaml_char_t **handle);
- static int
- yaml_parser_scan_tag_uri(yaml_parser_t *parser, int directive,
- yaml_char_t *head, yaml_mark_t start_mark, yaml_char_t **uri);
- static int
- yaml_parser_scan_uri_escapes(yaml_parser_t *parser, int directive,
- yaml_mark_t start_mark, yaml_string_t *string);
- static int
- yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token,
- int literal);
- static int
- yaml_parser_scan_block_scalar_breaks(yaml_parser_t *parser,
- int *indent, yaml_string_t *breaks,
- yaml_mark_t start_mark, yaml_mark_t *end_mark);
- static int
- yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token,
- int single);
- static int
- yaml_parser_scan_plain_scalar(yaml_parser_t *parser, yaml_token_t *token);
- /*
- * Get the next token.
- */
- YAML_DECLARE(int)
- yaml_parser_scan(yaml_parser_t *parser, yaml_token_t *token)
- {
- assert(parser); /* Non-NULL parser object is expected. */
- assert(token); /* Non-NULL token object is expected. */
- /* Erase the token object. */
- memset(token, 0, sizeof(yaml_token_t));
- /* No tokens after STREAM-END or error. */
- if (parser->stream_end_produced || parser->error) {
- return 1;
- }
- /* Ensure that the tokens queue contains enough tokens. */
- if (!parser->token_available) {
- if (!yaml_parser_fetch_more_tokens(parser))
- return 0;
- }
- /* Fetch the next token from the queue. */
-
- *token = DEQUEUE(parser, parser->tokens);
- parser->token_available = 0;
- parser->tokens_parsed ++;
- if (token->type == YAML_STREAM_END_TOKEN) {
- parser->stream_end_produced = 1;
- }
- return 1;
- }
- /*
- * Set the scanner error and return 0.
- */
- static int
- yaml_parser_set_scanner_error(yaml_parser_t *parser, const char *context,
- yaml_mark_t context_mark, const char *problem)
- {
- parser->error = YAML_SCANNER_ERROR;
- parser->context = context;
- parser->context_mark = context_mark;
- parser->problem = problem;
- parser->problem_mark = parser->mark;
- return 0;
- }
- /*
- * Ensure that the tokens queue contains at least one token which can be
- * returned to the Parser.
- */
- YAML_DECLARE(int)
- yaml_parser_fetch_more_tokens(yaml_parser_t *parser)
- {
- int need_more_tokens;
- /* While we need more tokens to fetch, do it. */
- while (1)
- {
- /*
- * Check if we really need to fetch more tokens.
- */
- need_more_tokens = 0;
- if (parser->tokens.head == parser->tokens.tail)
- {
- /* Queue is empty. */
- need_more_tokens = 1;
- }
- else
- {
- yaml_simple_key_t *simple_key;
- /* Check if any potential simple key may occupy the head position. */
- if (!yaml_parser_stale_simple_keys(parser))
- return 0;
- for (simple_key = parser->simple_keys.start;
- simple_key != parser->simple_keys.top; simple_key++) {
- if (simple_key->possible
- && simple_key->token_number == parser->tokens_parsed) {
- need_more_tokens = 1;
- break;
- }
- }
- }
- /* We are finished. */
- if (!need_more_tokens)
- break;
- /* Fetch the next token. */
- if (!yaml_parser_fetch_next_token(parser))
- return 0;
- }
- parser->token_available = 1;
- return 1;
- }
- /*
- * The dispatcher for token fetchers.
- */
- static int
- yaml_parser_fetch_next_token(yaml_parser_t *parser)
- {
- /* Ensure that the buffer is initialized. */
- if (!CACHE(parser, 1))
- return 0;
- /* Check if we just started scanning. Fetch STREAM-START then. */
- if (!parser->stream_start_produced)
- return yaml_parser_fetch_stream_start(parser);
- /* Eat whitespaces and comments until we reach the next token. */
- if (!yaml_parser_scan_to_next_token(parser))
- return 0;
- /* Remove obsolete potential simple keys. */
- if (!yaml_parser_stale_simple_keys(parser))
- return 0;
- /* Check the indentation level against the current column. */
- if (!yaml_parser_unroll_indent(parser, parser->mark.column))
- return 0;
- /*
- * Ensure that the buffer contains at least 4 characters. 4 is the length
- * of the longest indicators ('--- ' and '... ').
- */
- if (!CACHE(parser, 4))
- return 0;
- /* Is it the end of the stream? */
- if (IS_Z(parser->buffer))
- return yaml_parser_fetch_stream_end(parser);
- /* Is it a directive? */
- if (parser->mark.column == 0 && CHECK(parser->buffer, '%'))
- return yaml_parser_fetch_directive(parser);
- /* Is it the document start indicator? */
- if (parser->mark.column == 0
- && CHECK_AT(parser->buffer, '-', 0)
- && CHECK_AT(parser->buffer, '-', 1)
- && CHECK_AT(parser->buffer, '-', 2)
- && IS_BLANKZ_AT(parser->buffer, 3))
- return yaml_parser_fetch_document_indicator(parser,
- YAML_DOCUMENT_START_TOKEN);
- /* Is it the document end indicator? */
- if (parser->mark.column == 0
- && CHECK_AT(parser->buffer, '.', 0)
- && CHECK_AT(parser->buffer, '.', 1)
- && CHECK_AT(parser->buffer, '.', 2)
- && IS_BLANKZ_AT(parser->buffer, 3))
- return yaml_parser_fetch_document_indicator(parser,
- YAML_DOCUMENT_END_TOKEN);
- /* Is it the flow sequence start indicator? */
- if (CHECK(parser->buffer, '['))
- return yaml_parser_fetch_flow_collection_start(parser,
- YAML_FLOW_SEQUENCE_START_TOKEN);
- /* Is it the flow mapping start indicator? */
- if (CHECK(parser->buffer, '{'))
- return yaml_parser_fetch_flow_collection_start(parser,
- YAML_FLOW_MAPPING_START_TOKEN);
- /* Is it the flow sequence end indicator? */
- if (CHECK(parser->buffer, ']'))
- return yaml_parser_fetch_flow_collection_end(parser,
- YAML_FLOW_SEQUENCE_END_TOKEN);
- /* Is it the flow mapping end indicator? */
- if (CHECK(parser->buffer, '}'))
- return yaml_parser_fetch_flow_collection_end(parser,
- YAML_FLOW_MAPPING_END_TOKEN);
- /* Is it the flow entry indicator? */
- if (CHECK(parser->buffer, ','))
- return yaml_parser_fetch_flow_entry(parser);
- /* Is it the block entry indicator? */
- if (CHECK(parser->buffer, '-') && IS_BLANKZ_AT(parser->buffer, 1))
- return yaml_parser_fetch_block_entry(parser);
- /* Is it the key indicator? */
- if (CHECK(parser->buffer, '?')
- && (parser->flow_level || IS_BLANKZ_AT(parser->buffer, 1)))
- return yaml_parser_fetch_key(parser);
- /* Is it the value indicator? */
- if (CHECK(parser->buffer, ':')
- && (parser->flow_level || IS_BLANKZ_AT(parser->buffer, 1)))
- return yaml_parser_fetch_value(parser);
- /* Is it an alias? */
- if (CHECK(parser->buffer, '*'))
- return yaml_parser_fetch_anchor(parser, YAML_ALIAS_TOKEN);
- /* Is it an anchor? */
- if (CHECK(parser->buffer, '&'))
- return yaml_parser_fetch_anchor(parser, YAML_ANCHOR_TOKEN);
- /* Is it a tag? */
- if (CHECK(parser->buffer, '!'))
- return yaml_parser_fetch_tag(parser);
- /* Is it a literal scalar? */
- if (CHECK(parser->buffer, '|') && !parser->flow_level)
- return yaml_parser_fetch_block_scalar(parser, 1);
- /* Is it a folded scalar? */
- if (CHECK(parser->buffer, '>') && !parser->flow_level)
- return yaml_parser_fetch_block_scalar(parser, 0);
- /* Is it a single-quoted scalar? */
- if (CHECK(parser->buffer, '\''))
- return yaml_parser_fetch_flow_scalar(parser, 1);
- /* Is it a double-quoted scalar? */
- if (CHECK(parser->buffer, '"'))
- return yaml_parser_fetch_flow_scalar(parser, 0);
- /*
- * Is it a plain scalar?
- *
- * A plain scalar may start with any non-blank characters except
- *
- * '-', '?', ':', ',', '[', ']', '{', '}',
- * '#', '&', '*', '!', '|', '>', '\'', '\"',
- * '%', '@', '`'.
- *
- * In the block context (and, for the '-' indicator, in the flow context
- * too), it may also start with the characters
- *
- * '-', '?', ':'
- *
- * if it is followed by a non-space character.
- *
- * The last rule is more restrictive than the specification requires.
- */
- if (!(IS_BLANKZ(parser->buffer) || CHECK(parser->buffer, '-')
- || CHECK(parser->buffer, '?') || CHECK(parser->buffer, ':')
- || CHECK(parser->buffer, ',') || CHECK(parser->buffer, '[')
- || CHECK(parser->buffer, ']') || CHECK(parser->buffer, '{')
- || CHECK(parser->buffer, '}') || CHECK(parser->buffer, '#')
- || CHECK(parser->buffer, '&') || CHECK(parser->buffer, '*')
- || CHECK(parser->buffer, '!') || CHECK(parser->buffer, '|')
- || CHECK(parser->buffer, '>') || CHECK(parser->buffer, '\'')
- || CHECK(parser->buffer, '"') || CHECK(parser->buffer, '%')
- || CHECK(parser->buffer, '@') || CHECK(parser->buffer, '`')) ||
- (CHECK(parser->buffer, '-') && !IS_BLANK_AT(parser->buffer, 1)) ||
- (!parser->flow_level &&
- (CHECK(parser->buffer, '?') || CHECK(parser->buffer, ':'))
- && !IS_BLANKZ_AT(parser->buffer, 1)))
- return yaml_parser_fetch_plain_scalar(parser);
- /*
- * If we don't determine the token type so far, it is an error.
- */
- return yaml_parser_set_scanner_error(parser,
- "while scanning for the next token", parser->mark,
- "found character that cannot start any token");
- }
- /*
- * Check the list of potential simple keys and remove the positions that
- * cannot contain simple keys anymore.
- */
- static int
- yaml_parser_stale_simple_keys(yaml_parser_t *parser)
- {
- yaml_simple_key_t *simple_key;
- /* Check for a potential simple key for each flow level. */
- for (simple_key = parser->simple_keys.start;
- simple_key != parser->simple_keys.top; simple_key ++)
- {
- /*
- * The specification requires that a simple key
- *
- * - is limited to a single line,
- * - is shorter than 1024 characters.
- */
- if (simple_key->possible
- && (simple_key->mark.line < parser->mark.line
- || simple_key->mark.index+1024 < parser->mark.index)) {
- /* Check if the potential simple key to be removed is required. */
- if (simple_key->required) {
- return yaml_parser_set_scanner_error(parser,
- "while scanning a simple key", simple_key->mark,
- "could not find expected ':'");
- }
- simple_key->possible = 0;
- }
- }
- return 1;
- }
- /*
- * Check if a simple key may start at the current position and add it if
- * needed.
- */
- static int
- yaml_parser_save_simple_key(yaml_parser_t *parser)
- {
- /*
- * A simple key is required at the current position if the scanner is in
- * the block context and the current column coincides with the indentation
- * level.
- */
- int required = (!parser->flow_level
- && parser->indent == (int)parser->mark.column);
- /*
- * A simple key is required only when it is the first token in the current
- * line. Therefore it is always allowed. But we add a check anyway.
- */
- assert(parser->simple_key_allowed || !required); /* Impossible. */
- /*
- * If the current position may start a simple key, save it.
- */
- if (parser->simple_key_allowed)
- {
- yaml_simple_key_t simple_key;
- simple_key.possible = 1;
- simple_key.required = required;
- simple_key.token_number =
- parser->tokens_parsed + parser->tokens.tail - parser->tokens.head;
- simple_key.mark = parser->mark;
- if (!yaml_parser_remove_simple_key(parser)) return 0;
- *(parser->simple_keys.top-1) = simple_key;
- }
- return 1;
- }
- /*
- * Remove a potential simple key at the current flow level.
- */
- static int
- yaml_parser_remove_simple_key(yaml_parser_t *parser)
- {
- yaml_simple_key_t *simple_key = parser->simple_keys.top-1;
- if (simple_key->possible)
- {
- /* If the key is required, it is an error. */
- if (simple_key->required) {
- return yaml_parser_set_scanner_error(parser,
- "while scanning a simple key", simple_key->mark,
- "could not find expected ':'");
- }
- }
- /* Remove the key from the stack. */
- simple_key->possible = 0;
- return 1;
- }
- /*
- * Increase the flow level and resize the simple key list if needed.
- */
- static int
- yaml_parser_increase_flow_level(yaml_parser_t *parser)
- {
- yaml_simple_key_t empty_simple_key = { 0, 0, 0, { 0, 0, 0 } };
- /* Reset the simple key on the next level. */
- if (!PUSH(parser, parser->simple_keys, empty_simple_key))
- return 0;
- /* Increase the flow level. */
- parser->flow_level++;
- return 1;
- }
- /*
- * Decrease the flow level.
- */
- static int
- yaml_parser_decrease_flow_level(yaml_parser_t *parser)
- {
- yaml_simple_key_t dummy_key; /* Used to eliminate a compiler warning. */
- if (parser->flow_level) {
- parser->flow_level --;
- dummy_key = POP(parser, parser->simple_keys);
- }
- return 1;
- }
- /*
- * Push the current indentation level to the stack and set the new level
- * the current column is greater than the indentation level. In this case,
- * append or insert the specified token into the token queue.
- *
- */
- static int
- yaml_parser_roll_indent(yaml_parser_t *parser, int column,
- int number, yaml_token_type_t type, yaml_mark_t mark)
- {
- yaml_token_t token;
- /* In the flow context, do nothing. */
- if (parser->flow_level)
- return 1;
- if (parser->indent < column)
- {
- /*
- * Push the current indentation level to the stack and set the new
- * indentation level.
- */
- if (!PUSH(parser, parser->indents, parser->indent))
- return 0;
- parser->indent = column;
- /* Create a token and insert it into the queue. */
- TOKEN_INIT(token, type, mark, mark);
- if (number == -1) {
- if (!ENQUEUE(parser, parser->tokens, token))
- return 0;
- }
- else {
- if (!QUEUE_INSERT(parser,
- parser->tokens, number - parser->tokens_parsed, token))
- return 0;
- }
- }
- return 1;
- }
- /*
- * Pop indentation levels from the indents stack until the current level
- * becomes less or equal to the column. For each intendation level, append
- * the BLOCK-END token.
- */
- static int
- yaml_parser_unroll_indent(yaml_parser_t *parser, int column)
- {
- yaml_token_t token;
- /* In the flow context, do nothing. */
- if (parser->flow_level)
- return 1;
- /* Loop through the intendation levels in the stack. */
- while (parser->indent > column)
- {
- /* Create a token and append it to the queue. */
- TOKEN_INIT(token, YAML_BLOCK_END_TOKEN, parser->mark, parser->mark);
- if (!ENQUEUE(parser, parser->tokens, token))
- return 0;
- /* Pop the indentation level. */
- parser->indent = POP(parser, parser->indents);
- }
- return 1;
- }
- /*
- * Initialize the scanner and produce the STREAM-START token.
- */
- static int
- yaml_parser_fetch_stream_start(yaml_parser_t *parser)
- {
- yaml_simple_key_t simple_key = { 0, 0, 0, { 0, 0, 0 } };
- yaml_token_t token;
- /* Set the initial indentation. */
- parser->indent = -1;
- /* Initialize the simple key stack. */
- if (!PUSH(parser, parser->simple_keys, simple_key))
- return 0;
- /* A simple key is allowed at the beginning of the stream. */
- parser->simple_key_allowed = 1;
- /* We have started. */
- parser->stream_start_produced = 1;
- /* Create the STREAM-START token and append it to the queue. */
- STREAM_START_TOKEN_INIT(token, parser->encoding,
- parser->mark, parser->mark);
- if (!ENQUEUE(parser, parser->tokens, token))
- return 0;
- return 1;
- }
- /*
- * Produce the STREAM-END token and shut down the scanner.
- */
- static int
- yaml_parser_fetch_stream_end(yaml_parser_t *parser)
- {
- yaml_token_t token;
- /* Force new line. */
- if (parser->mark.column != 0) {
- parser->mark.column = 0;
- parser->mark.line ++;
- }
- /* Reset the indentation level. */
- if (!yaml_parser_unroll_indent(parser, -1))
- return 0;
- /* Reset simple keys. */
- if (!yaml_parser_remove_simple_key(parser))
- return 0;
- parser->simple_key_allowed = 0;
- /* Create the STREAM-END token and append it to the queue. */
- STREAM_END_TOKEN_INIT(token, parser->mark, parser->mark);
- if (!ENQUEUE(parser, parser->tokens, token))
- return 0;
- return 1;
- }
- /*
- * Produce a VERSION-DIRECTIVE or TAG-DIRECTIVE token.
- */
- static int
- yaml_parser_fetch_directive(yaml_parser_t *parser)
- {
- yaml_token_t token;
- /* Reset the indentation level. */
- if (!yaml_parser_unroll_indent(parser, -1))
- return 0;
- /* Reset simple keys. */
- if (!yaml_parser_remove_simple_key(parser))
- return 0;
- parser->simple_key_allowed = 0;
- /* Create the YAML-DIRECTIVE or TAG-DIRECTIVE token. */
- if (!yaml_parser_scan_directive(parser, &token))
- return 0;
- /* Append the token to the queue. */
- if (!ENQUEUE(parser, parser->tokens, token)) {
- yaml_token_delete(&token);
- return 0;
- }
- return 1;
- }
- /*
- * Produce the DOCUMENT-START or DOCUMENT-END token.
- */
- static int
- yaml_parser_fetch_document_indicator(yaml_parser_t *parser,
- yaml_token_type_t type)
- {
- yaml_mark_t start_mark, end_mark;
- yaml_token_t token;
- /* Reset the indentation level. */
- if (!yaml_parser_unroll_indent(parser, -1))
- return 0;
- /* Reset simple keys. */
- if (!yaml_parser_remove_simple_key(parser))
- return 0;
- parser->simple_key_allowed = 0;
- /* Consume the token. */
- start_mark = parser->mark;
- SKIP(parser);
- SKIP(parser);
- SKIP(parser);
- end_mark = parser->mark;
- /* Create the DOCUMENT-START or DOCUMENT-END token. */
- TOKEN_INIT(token, type, start_mark, end_mark);
- /* Append the token to the queue. */
- if (!ENQUEUE(parser, parser->tokens, token))
- return 0;
- return 1;
- }
- /*
- * Produce the FLOW-SEQUENCE-START or FLOW-MAPPING-START token.
- */
- static int
- yaml_parser_fetch_flow_collection_start(yaml_parser_t *parser,
- yaml_token_type_t type)
- {
- yaml_mark_t start_mark, end_mark;
- yaml_token_t token;
- /* The indicators '[' and '{' may start a simple key. */
- if (!yaml_parser_save_simple_key(parser))
- return 0;
- /* Increase the flow level. */
- if (!yaml_parser_increase_flow_level(parser))
- return 0;
- /* A simple key may follow the indicators '[' and '{'. */
- parser->simple_key_allowed = 1;
- /* Consume the token. */
- start_mark = parser->mark;
- SKIP(parser);
- end_mark = parser->mark;
- /* Create the FLOW-SEQUENCE-START of FLOW-MAPPING-START token. */
- TOKEN_INIT(token, type, start_mark, end_mark);
- /* Append the token to the queue. */
- if (!ENQUEUE(parser, parser->tokens, token))
- return 0;
- return 1;
- }
- /*
- * Produce the FLOW-SEQUENCE-END or FLOW-MAPPING-END token.
- */
- static int
- yaml_parser_fetch_flow_collection_end(yaml_parser_t *parser,
- yaml_token_type_t type)
- {
- yaml_mark_t start_mark, end_mark;
- yaml_token_t token;
- /* Reset any potential simple key on the current flow level. */
- if (!yaml_parser_remove_simple_key(parser))
- return 0;
- /* Decrease the flow level. */
- if (!yaml_parser_decrease_flow_level(parser))
- return 0;
- /* No simple keys after the indicators ']' and '}'. */
- parser->simple_key_allowed = 0;
- /* Consume the token. */
- start_mark = parser->mark;
- SKIP(parser);
- end_mark = parser->mark;
- /* Create the FLOW-SEQUENCE-END of FLOW-MAPPING-END token. */
- TOKEN_INIT(token, type, start_mark, end_mark);
- /* Append the token to the queue. */
- if (!ENQUEUE(parser, parser->tokens, token))
- return 0;
- return 1;
- }
- /*
- * Produce the FLOW-ENTRY token.
- */
- static int
- yaml_parser_fetch_flow_entry(yaml_parser_t *parser)
- {
- yaml_mark_t start_mark, end_mark;
- yaml_token_t token;
- /* Reset any potential simple keys on the current flow level. */
- if (!yaml_parser_remove_simple_key(parser))
- return 0;
- /* Simple keys are allowed after ','. */
- parser->simple_key_allowed = 1;
- /* Consume the token. */
- start_mark = parser->mark;
- SKIP(parser);
- end_mark = parser->mark;
- /* Create the FLOW-ENTRY token and append it to the queue. */
- TOKEN_INIT(token, YAML_FLOW_ENTRY_TOKEN, start_mark, end_mark);
- if (!ENQUEUE(parser, parser->tokens, token))
- return 0;
- return 1;
- }
- /*
- * Produce the BLOCK-ENTRY token.
- */
- static int
- yaml_parser_fetch_block_entry(yaml_parser_t *parser)
- {
- yaml_mark_t start_mark, end_mark;
- yaml_token_t token;
- /* Check if the scanner is in the block context. */
- if (!parser->flow_level)
- {
- /* Check if we are allowed to start a new entry. */
- if (!parser->simple_key_allowed) {
- return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
- "block sequence entries are not allowed in this context");
- }
- /* Add the BLOCK-SEQUENCE-START token if needed. */
- if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
- YAML_BLOCK_SEQUENCE_START_TOKEN, parser->mark))
- return 0;
- }
- else
- {
- /*
- * It is an error for the '-' indicator to occur in the flow context,
- * but we let the Parser detect and report about it because the Parser
- * is able to point to the context.
- */
- }
- /* Reset any potential simple keys on the current flow level. */
- if (!yaml_parser_remove_simple_key(parser))
- return 0;
- /* Simple keys are allowed after '-'. */
- parser->simple_key_allowed = 1;
- /* Consume the token. */
- start_mark = parser->mark;
- SKIP(parser);
- end_mark = parser->mark;
- /* Create the BLOCK-ENTRY token and append it to the queue. */
- TOKEN_INIT(token, YAML_BLOCK_ENTRY_TOKEN, start_mark, end_mark);
- if (!ENQUEUE(parser, parser->tokens, token))
- return 0;
- return 1;
- }
- /*
- * Produce the KEY token.
- */
- static int
- yaml_parser_fetch_key(yaml_parser_t *parser)
- {
- yaml_mark_t start_mark, end_mark;
- yaml_token_t token;
- /* In the block context, additional checks are required. */
- if (!parser->flow_level)
- {
- /* Check if we are allowed to start a new key (not nessesary simple). */
- if (!parser->simple_key_allowed) {
- return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
- "mapping keys are not allowed in this context");
- }
- /* Add the BLOCK-MAPPING-START token if needed. */
- if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
- YAML_BLOCK_MAPPING_START_TOKEN, parser->mark))
- return 0;
- }
- /* Reset any potential simple keys on the current flow level. */
- if (!yaml_parser_remove_simple_key(parser))
- return 0;
- /* Simple keys are allowed after '?' in the block context. */
- parser->simple_key_allowed = (!parser->flow_level);
- /* Consume the token. */
- start_mark = parser->mark;
- SKIP(parser);
- end_mark = parser->mark;
- /* Create the KEY token and append it to the queue. */
- TOKEN_INIT(token, YAML_KEY_TOKEN, start_mark, end_mark);
- if (!ENQUEUE(parser, parser->tokens, token))
- return 0;
- return 1;
- }
- /*
- * Produce the VALUE token.
- */
- static int
- yaml_parser_fetch_value(yaml_parser_t *parser)
- {
- yaml_mark_t start_mark, end_mark;
- yaml_token_t token;
- yaml_simple_key_t *simple_key = parser->simple_keys.top-1;
- /* Have we found a simple key? */
- if (simple_key->possible)
- {
- /* Create the KEY token and insert it into the queue. */
- TOKEN_INIT(token, YAML_KEY_TOKEN, simple_key->mark, simple_key->mark);
- if (!QUEUE_INSERT(parser, parser->tokens,
- simple_key->token_number - parser->tokens_parsed, token))
- return 0;
- /* In the block context, we may need to add the BLOCK-MAPPING-START token. */
- if (!yaml_parser_roll_indent(parser, simple_key->mark.column,
- simple_key->token_number,
- YAML_BLOCK_MAPPING_START_TOKEN, simple_key->mark))
- return 0;
- /* Remove the simple key. */
- simple_key->possible = 0;
- /* A simple key cannot follow another simple key. */
- parser->simple_key_allowed = 0;
- }
- else
- {
- /* The ':' indicator follows a complex key. */
- /* In the block context, extra checks are required. */
- if (!parser->flow_level)
- {
- /* Check if we are allowed to start a complex value. */
- if (!parser->simple_key_allowed) {
- return yaml_parser_set_scanner_error(parser, NULL, parser->mark,
- "mapping values are not allowed in this context");
- }
- /* Add the BLOCK-MAPPING-START token if needed. */
- if (!yaml_parser_roll_indent(parser, parser->mark.column, -1,
- YAML_BLOCK_MAPPING_START_TOKEN, parser->mark))
- return 0;
- }
- /* Simple keys after ':' are allowed in the block context. */
- parser->simple_key_allowed = (!parser->flow_level);
- }
- /* Consume the token. */
- start_mark = parser->mark;
- SKIP(parser);
- end_mark = parser->mark;
- /* Create the VALUE token and append it to the queue. */
- TOKEN_INIT(token, YAML_VALUE_TOKEN, start_mark, end_mark);
- if (!ENQUEUE(parser, parser->tokens, token))
- return 0;
- return 1;
- }
- /*
- * Produce the ALIAS or ANCHOR token.
- */
- static int
- yaml_parser_fetch_anchor(yaml_parser_t *parser, yaml_token_type_t type)
- {
- yaml_token_t token;
- /* An anchor or an alias could be a simple key. */
- if (!yaml_parser_save_simple_key(parser))
- return 0;
- /* A simple key cannot follow an anchor or an alias. */
- parser->simple_key_allowed = 0;
- /* Create the ALIAS or ANCHOR token and append it to the queue. */
- if (!yaml_parser_scan_anchor(parser, &token, type))
- return 0;
- if (!ENQUEUE(parser, parser->tokens, token)) {
- yaml_token_delete(&token);
- return 0;
- }
- return 1;
- }
- /*
- * Produce the TAG token.
- */
- static int
- yaml_parser_fetch_tag(yaml_parser_t *parser)
- {
- yaml_token_t token;
- /* A tag could be a simple key. */
- if (!yaml_parser_save_simple_key(parser))
- return 0;
- /* A simple key cannot follow a tag. */
- parser->simple_key_allowed = 0;
- /* Create the TAG token and append it to the queue. */
- if (!yaml_parser_scan_tag(parser, &token))
- return 0;
- if (!ENQUEUE(parser, parser->tokens, token)) {
- yaml_token_delete(&token);
- return 0;
- }
- return 1;
- }
- /*
- * Produce the SCALAR(...,literal) or SCALAR(...,folded) tokens.
- */
- static int
- yaml_parser_fetch_block_scalar(yaml_parser_t *parser, int literal)
- {
- yaml_token_t token;
- /* Remove any potential simple keys. */
- if (!yaml_parser_remove_simple_key(parser))
- return 0;
- /* A simple key may follow a block scalar. */
- parser->simple_key_allowed = 1;
- /* Create the SCALAR token and append it to the queue. */
- if (!yaml_parser_scan_block_scalar(parser, &token, literal))
- return 0;
- if (!ENQUEUE(parser, parser->tokens, token)) {
- yaml_token_delete(&token);
- return 0;
- }
- return 1;
- }
- /*
- * Produce the SCALAR(...,single-quoted) or SCALAR(...,double-quoted) tokens.
- */
- static int
- yaml_parser_fetch_flow_scalar(yaml_parser_t *parser, int single)
- {
- yaml_token_t token;
- /* A plain scalar could be a simple key. */
- if (!yaml_parser_save_simple_key(parser))
- return 0;
- /* A simple key cannot follow a flow scalar. */
- parser->simple_key_allowed = 0;
- /* Create the SCALAR token and append it to the queue. */
- if (!yaml_parser_scan_flow_scalar(parser, &token, single))
- return 0;
- if (!ENQUEUE(parser, parser->tokens, token)) {
- yaml_token_delete(&token);
- return 0;
- }
- return 1;
- }
- /*
- * Produce the SCALAR(...,plain) token.
- */
- static int
- yaml_parser_fetch_plain_scalar(yaml_parser_t *parser)
- {
- yaml_token_t token;
- /* A plain scalar could be a simple key. */
- if (!yaml_parser_save_simple_key(parser))
- return 0;
- /* A simple key cannot follow a flow scalar. */
- parser->simple_key_allowed = 0;
- /* Create the SCALAR token and append it to the queue. */
- if (!yaml_parser_scan_plain_scalar(parser, &token))
- return 0;
- if (!ENQUEUE(parser, parser->tokens, token)) {
- yaml_token_delete(&token);
- return 0;
- }
- return 1;
- }
- /*
- * Eat whitespaces and comments until the next token is found.
- */
- static int
- yaml_parser_scan_to_next_token(yaml_parser_t *parser)
- {
- /* Until the next token is not found. */
- while (1)
- {
- /* Allow the BOM mark to start a line. */
- if (!CACHE(parser, 1)) return 0;
- if (parser->mark.column == 0 && IS_BOM(parser->buffer))
- SKIP(parser);
- /*
- * Eat whitespaces.
- *
- * Tabs are allowed:
- *
- * - in the flow context;
- * - in the block context, but not at the beginning of the line or
- * after '-', '?', or ':' (complex value).
- */
- if (!CACHE(parser, 1)) return 0;
- while (CHECK(parser->buffer,' ') ||
- ((parser->flow_level || !parser->simple_key_allowed) &&
- CHECK(parser->buffer, '\t'))) {
- SKIP(parser);
- if (!CACHE(parser, 1)) return 0;
- }
- /* Eat a comment until a line break. */
- if (CHECK(parser->buffer, '#')) {
- while (!IS_BREAKZ(parser->buffer)) {
- SKIP(parser);
- if (!CACHE(parser, 1)) return 0;
- }
- }
- /* If it is a line break, eat it. */
- if (IS_BREAK(parser->buffer))
- {
- if (!CACHE(parser, 2)) return 0;
- SKIP_LINE(parser);
- /* In the block context, a new line may start a simple key. */
- if (!parser->flow_level) {
- parser->simple_key_allowed = 1;
- }
- }
- else
- {
- /* We have found a token. */
- break;
- }
- }
- return 1;
- }
- /*
- * Scan a YAML-DIRECTIVE or TAG-DIRECTIVE token.
- *
- * Scope:
- * %YAML 1.1 # a comment \n
- * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- * %TAG !yaml! tag:yaml.org,2002: \n
- * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- */
- int
- yaml_parser_scan_directive(yaml_parser_t *parser, yaml_token_t *token)
- {
- yaml_mark_t start_mark, end_mark;
- yaml_char_t *name = NULL;
- int major, minor;
- yaml_char_t *handle = NULL, *prefix = NULL;
- /* Eat '%'. */
- start_mark = parser->mark;
- SKIP(parser);
- /* Scan the directive name. */
- if (!yaml_parser_scan_directive_name(parser, start_mark, &name))
- goto error;
- /* Is it a YAML directive? */
- if (strcmp((char *)name, "YAML") == 0)
- {
- /* Scan the VERSION directive value. */
- if (!yaml_parser_scan_version_directive_value(parser, start_mark,
- &major, &minor))
- goto error;
- end_mark = parser->mark;
- /* Create a VERSION-DIRECTIVE token. */
- VERSION_DIRECTIVE_TOKEN_INIT(*token, major, minor,
- start_mark, end_mark);
- }
- /* Is it a TAG directive? */
- else if (strcmp((char *)name, "TAG") == 0)
- {
- /* Scan the TAG directive value. */
- if (!yaml_parser_scan_tag_directive_value(parser, start_mark,
- &handle, &prefix))
- goto error;
- end_mark = parser->mark;
- /* Create a TAG-DIRECTIVE token. */
- TAG_DIRECTIVE_TOKEN_INIT(*token, handle, prefix,
- start_mark, end_mark);
- }
- /* Unknown directive. */
- else
- {
- yaml_parser_set_scanner_error(parser, "while scanning a directive",
- start_mark, "found uknown directive name");
- goto error;
- }
- /* Eat the rest of the line including any comments. */
- if (!CACHE(parser, 1)) goto error;
- while (IS_BLANK(parser->buffer)) {
- SKIP(parser);
- if (!CACHE(parser, 1)) goto error;
- }
- if (CHECK(parser->buffer, '#')) {
- while (!IS_BREAKZ(parser->buffer)) {
- SKIP(parser);
- if (!CACHE(parser, 1)) goto error;
- }
- }
- /* Check if we are at the end of the line. */
- if (!IS_BREAKZ(parser->buffer)) {
- yaml_parser_set_scanner_error(parser, "while scanning a directive",
- start_mark, "did not find expected comment or line break");
- goto error;
- }
- /* Eat a line break. */
- if (IS_BREAK(parser->buffer)) {
- if (!CACHE(parser, 2)) goto error;
- SKIP_LINE(parser);
- }
- yaml_free(name);
- return 1;
- error:
- yaml_free(prefix);
- yaml_free(handle);
- yaml_free(name);
- return 0;
- }
- /*
- * Scan the directive name.
- *
- * Scope:
- * %YAML 1.1 # a comment \n
- * ^^^^
- * %TAG !yaml! tag:yaml.org,2002: \n
- * ^^^
- */
- static int
- yaml_parser_scan_directive_name(yaml_parser_t *parser,
- yaml_mark_t start_mark, yaml_char_t **name)
- {
- yaml_string_t string = NULL_STRING;
- if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
- /* Consume the directive name. */
- if (!CACHE(parser, 1)) goto error;
- while (IS_ALPHA(parser->buffer))
- {
- if (!READ(parser, string)) goto error;
- if (!CACHE(parser, 1)) goto error;
- }
- /* Check if the name is empty. */
- if (string.start == string.pointer) {
- yaml_parser_set_scanner_error(parser, "while scanning a directive",
- start_mark, "could not find expected directive name");
- goto error;
- }
- /* Check for an blank character after the name. */
- if (!IS_BLANKZ(parser->buffer)) {
- yaml_parser_set_scanner_error(parser, "while scanning a directive",
- start_mark, "found unexpected non-alphabetical character");
- goto error;
- }
- *name = string.start;
- return 1;
- error:
- STRING_DEL(parser, string);
- return 0;
- }
- /*
- * Scan the value of VERSION-DIRECTIVE.
- *
- * Scope:
- * %YAML 1.1 # a comment \n
- * ^^^^^^
- */
- static int
- yaml_parser_scan_version_directive_value(yaml_parser_t *parser,
- yaml_mark_t start_mark, int *major, int *minor)
- {
- /* Eat whitespaces. */
- if (!CACHE(parser, 1)) return 0;
- while (IS_BLANK(parser->buffer)) {
- SKIP(parser);
- if (!CACHE(parser, 1)) return 0;
- }
- /* Consume the major version number. */
- if (!yaml_parser_scan_version_directive_number(parser, start_mark, major))
- return 0;
- /* Eat '.'. */
- if (!CHECK(parser->buffer, '.')) {
- return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
- start_mark, "did not find expected digit or '.' character");
- }
- SKIP(parser);
- /* Consume the minor version number. */
- if (!yaml_parser_scan_version_directive_number(parser, start_mark, minor))
- return 0;
- return 1;
- }
- #define MAX_NUMBER_LENGTH 9
- /*
- * Scan the version number of VERSION-DIRECTIVE.
- *
- * Scope:
- * %YAML 1.1 # a comment \n
- * ^
- * %YAML 1.1 # a comment \n
- * ^
- */
- static int
- yaml_parser_scan_version_directive_number(yaml_parser_t *parser,
- yaml_mark_t start_mark, int *number)
- {
- int value = 0;
- size_t length = 0;
- /* Repeat while the next character is digit. */
- if (!CACHE(parser, 1)) return 0;
- while (IS_DIGIT(parser->buffer))
- {
- /* Check if the number is too long. */
- if (++length > MAX_NUMBER_LENGTH) {
- return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
- start_mark, "found extremely long version number");
- }
- value = value*10 + AS_DIGIT(parser->buffer);
- SKIP(parser);
- if (!CACHE(parser, 1)) return 0;
- }
- /* Check if the number was present. */
- if (!length) {
- return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive",
- start_mark, "did not find expected version number");
- }
- *number = value;
- return 1;
- }
- /*
- * Scan the value of a TAG-DIRECTIVE token.
- *
- * Scope:
- * %TAG !yaml! tag:yaml.org,2002: \n
- * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- */
- static int
- yaml_parser_scan_tag_directive_value(yaml_parser_t *parser,
- yaml_mark_t start_mark, yaml_char_t **handle, yaml_char_t **prefix)
- {
- yaml_char_t *handle_value = NULL;
- yaml_char_t *prefix_value = NULL;
- /* Eat whitespaces. */
- if (!CACHE(parser, 1)) goto error;
- while (IS_BLANK(parser->buffer)) {
- SKIP(parser);
- if (!CACHE(parser, 1)) goto error;
- }
- /* Scan a handle. */
- if (!yaml_parser_scan_tag_handle(parser, 1, start_mark, &handle_value))
- goto error;
- /* Expect a whitespace. */
- if (!CACHE(parser, 1)) goto error;
- if (!IS_BLANK(parser->buffer)) {
- yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
- start_mark, "did not find expected whitespace");
- goto error;
- }
- /* Eat whitespaces. */
- while (IS_BLANK(parser->buffer)) {
- SKIP(parser);
- if (!CACHE(parser, 1)) goto error;
- }
- /* Scan a prefix. */
- if (!yaml_parser_scan_tag_uri(parser, 1, NULL, start_mark, &prefix_value))
- goto error;
- /* Expect a whitespace or line break. */
- if (!CACHE(parser, 1)) goto error;
- if (!IS_BLANKZ(parser->buffer)) {
- yaml_parser_set_scanner_error(parser, "while scanning a %TAG directive",
- start_mark, "did not find expected whitespace or line break");
- goto error;
- }
- *handle = handle_value;
- *prefix = prefix_value;
- return 1;
- error:
- yaml_free(handle_value);
- yaml_free(prefix_value);
- return 0;
- }
- static int
- yaml_parser_scan_anchor(yaml_parser_t *parser, yaml_token_t *token,
- yaml_token_type_t type)
- {
- int length = 0;
- yaml_mark_t start_mark, end_mark;
- yaml_string_t string = NULL_STRING;
- if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
- /* Eat the indicator character. */
- start_mark = parser->mark;
- SKIP(parser);
- /* Consume the value. */
- if (!CACHE(parser, 1)) goto error;
- while (IS_ALPHA(parser->buffer)) {
- if (!READ(parser, string)) goto error;
- if (!CACHE(parser, 1)) goto error;
- length ++;
- }
- end_mark = parser->mark;
- /*
- * Check if length of the anchor is greater than 0 and it is followed by
- * a whitespace character or one of the indicators:
- *
- * '?', ':', ',', ']', '}', '%', '@', '`'.
- */
- if (!length || !(IS_BLANKZ(parser->buffer) || CHECK(parser->buffer, '?')
- || CHECK(parser->buffer, ':') || CHECK(parser->buffer, ',')
- || CHECK(parser->buffer, ']') || CHECK(parser->buffer, '}')
- || CHECK(parser->buffer, '%') || CHECK(parser->buffer, '@')
- || CHECK(parser->buffer, '`'))) {
- yaml_parser_set_scanner_error(parser, type == YAML_ANCHOR_TOKEN ?
- "while scanning an anchor" : "while scanning an alias", start_mark,
- "did not find expected alphabetic or numeric character");
- goto error;
- }
- /* Create a token. */
- if (type == YAML_ANCHOR_TOKEN) {
- ANCHOR_TOKEN_INIT(*token, string.start, start_mark, end_mark);
- }
- else {
- ALIAS_TOKEN_INIT(*token, string.start, start_mark, end_mark);
- }
- return 1;
- error:
- STRING_DEL(parser, string);
- return 0;
- }
- /*
- * Scan a TAG token.
- */
- static int
- yaml_parser_scan_tag(yaml_parser_t *parser, yaml_token_t *token)
- {
- yaml_char_t *handle = NULL;
- yaml_char_t *suffix = NULL;
- yaml_mark_t start_mark, end_mark;
- start_mark = parser->mark;
- /* Check if the tag is in the canonical form. */
- if (!CACHE(parser, 2)) goto error;
- if (CHECK_AT(parser->buffer, '<', 1))
- {
- /* Set the handle to '' */
- handle = yaml_malloc(1);
- if (!handle) goto error;
- handle[0] = '\0';
- /* Eat '!<' */
- SKIP(parser);
- SKIP(parser);
- /* Consume the tag value. */
- if (!yaml_parser_scan_tag_uri(parser, 0, NULL, start_mark, &suffix))
- goto error;
- /* Check for '>' and eat it. */
- if (!CHECK(parser->buffer, '>')) {
- yaml_parser_set_scanner_error(parser, "while scanning a tag",
- start_mark, "did not find the expected '>'");
- goto error;
- }
- SKIP(parser);
- }
- else
- {
- /* The tag has either the '!suffix' or the '!handle!suffix' form. */
- /* First, try to scan a handle. */
- if (!yaml_parser_scan_tag_handle(parser, 0, start_mark, &handle))
- goto error;
- /* Check if it is, indeed, handle. */
- if (handle[0] == '!' && handle[1] != '\0' && handle[strlen((char *)handle)-1] == '!')
- {
- /* Scan the suffix now. */
- if (!yaml_parser_scan_tag_uri(parser, 0, NULL, start_mark, &suffix))
- goto error;
- }
- else
- {
- /* It wasn't a handle after all. Scan the rest of the tag. */
- if (!yaml_parser_scan_tag_uri(parser, 0, handle, start_mark, &suffix))
- goto error;
- /* Set the handle to '!'. */
- yaml_free(handle);
- handle = yaml_malloc(2);
- if (!handle) goto error;
- handle[0] = '!';
- handle[1] = '\0';
- /*
- * A special case: the '!' tag. Set the handle to '' and the
- * suffix to '!'.
- */
- if (suffix[0] == '\0') {
- yaml_char_t *tmp = handle;
- handle = suffix;
- suffix = tmp;
- }
- }
- }
- /* Check the character which ends the tag. */
- if (!CACHE(parser, 1)) goto error;
- if (!IS_BLANKZ(parser->buffer)) {
- yaml_parser_set_scanner_error(parser, "while scanning a tag",
- start_mark, "did not find expected whitespace or line break");
- goto error;
- }
- end_mark = parser->mark;
- /* Create a token. */
- TAG_TOKEN_INIT(*token, handle, suffix, start_mark, end_mark);
- return 1;
- error:
- yaml_free(handle);
- yaml_free(suffix);
- return 0;
- }
- /*
- * Scan a tag handle.
- */
- static int
- yaml_parser_scan_tag_handle(yaml_parser_t *parser, int directive,
- yaml_mark_t start_mark, yaml_char_t **handle)
- {
- yaml_string_t string = NULL_STRING;
- if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
- /* Check the initial '!' character. */
- if (!CACHE(parser, 1)) goto error;
- if (!CHECK(parser->buffer, '!')) {
- yaml_parser_set_scanner_error(parser, directive ?
- "while scanning a tag directive" : "while scanning a tag",
- start_mark, "did not find expected '!'");
- goto error;
- }
- /* Copy the '!' character. */
- if (!READ(parser, string)) goto error;
- /* Copy all subsequent alphabetical and numerical characters. */
- if (!CACHE(parser, 1)) goto error;
- while (IS_ALPHA(parser->buffer))
- {
- if (!READ(parser, string)) goto error;
- if (!CACHE(parser, 1)) goto error;
- }
- /* Check if the trailing character is '!' and copy it. */
- if (CHECK(parser->buffer, '!'))
- {
- if (!READ(parser, string)) goto error;
- }
- else
- {
- /*
- * It's either the '!' tag or not really a tag handle. If it's a %TAG
- * directive, it's an error. If it's a tag token, it must be a part of
- * URI.
- */
- if (directive && !(string.start[0] == '!' && string.start[1] == '\0')) {
- yaml_parser_set_scanner_error(parser, "while parsing a tag directive",
- start_mark, "did not find expected '!'");
- goto error;
- }
- }
- *handle = string.start;
- return 1;
- error:
- STRING_DEL(parser, string);
- return 0;
- }
- /*
- * Scan a tag.
- */
- static int
- yaml_parser_scan_tag_uri(yaml_parser_t *parser, int directive,
- yaml_char_t *head, yaml_mark_t start_mark, yaml_char_t **uri)
- {
- size_t length = head ? strlen((char *)head) : 0;
- yaml_string_t string = NULL_STRING;
- if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
- /* Resize the string to include the head. */
- while (string.end - string.start <= (int)length) {
- if (!yaml_string_extend(&string.start, &string.pointer, &string.end)) {
- parser->error = YAML_MEMORY_ERROR;
- goto error;
- }
- }
- /*
- * Copy the head if needed.
- *
- * Note that we don't copy the leading '!' character.
- */
- if (length > 1) {
- memcpy(string.start, head+1, length-1);
- string.pointer += length-1;
- }
- /* Scan the tag. */
- if (!CACHE(parser, 1)) goto error;
- /*
- * The set of characters that may appear in URI is as follows:
- *
- * '0'-'9', 'A'-'Z', 'a'-'z', '_', '-', ';', '/', '?', ':', '@', '&',
- * '=', '+', '$', ',', '.', '!', '~', '*', '\'', '(', ')', '[', ']',
- * '%'.
- */
- while (IS_ALPHA(parser->buffer) || CHECK(parser->buffer, ';')
- || CHECK(parser->buffer, '/') || CHECK(parser->buffer, '?')
- || CHECK(parser->buffer, ':') || CHECK(parser->buffer, '@')
- || CHECK(parser->buffer, '&') || CHECK(parser->buffer, '=')
- || CHECK(parser->buffer, '+') || CHECK(parser->buffer, '$')
- || CHECK(parser->buffer, ',') || CHECK(parser->buffer, '.')
- || CHECK(parser->buffer, '!') || CHECK(parser->buffer, '~')
- || CHECK(parser->buffer, '*') || CHECK(parser->buffer, '\'')
- || CHECK(parser->buffer, '(') || CHECK(parser->buffer, ')')
- || CHECK(parser->buffer, '[') || CHECK(parser->buffer, ']')
- || CHECK(parser->buffer, '%'))
- {
- /* Check if it is a URI-escape sequence. */
- if (CHECK(parser->buffer, '%')) {
- if (!yaml_parser_scan_uri_escapes(parser,
- directive, start_mark, &string)) goto error;
- }
- else {
- if (!READ(parser, string)) goto error;
- }
- length ++;
- if (!CACHE(parser, 1)) goto error;
- }
- /* Check if the tag is non-empty. */
- if (!length) {
- if (!STRING_EXTEND(parser, string))
- goto error;
- yaml_parser_set_scanner_error(parser, directive ?
- "while parsing a %TAG directive" : "while parsing a tag",
- start_mark, "did not find expected tag URI");
- goto error;
- }
- *uri = string.start;
- return 1;
- error:
- STRING_DEL(parser, string);
- return 0;
- }
- /*
- * Decode an URI-escape sequence corresponding to a single UTF-8 character.
- */
- static int
- yaml_parser_scan_uri_escapes(yaml_parser_t *parser, int directive,
- yaml_mark_t start_mark, yaml_string_t *string)
- {
- int width = 0;
- /* Decode the required number of characters. */
- do {
- unsigned char octet = 0;
- /* Check for a URI-escaped octet. */
- if (!CACHE(parser, 3)) return 0;
- if (!(CHECK(parser->buffer, '%')
- && IS_HEX_AT(parser->buffer, 1)
- && IS_HEX_AT(parser->buffer, 2))) {
- return yaml_parser_set_scanner_error(parser, directive ?
- "while parsing a %TAG directive" : "while parsing a tag",
- start_mark, "did not find URI escaped octet");
- }
- /* Get the octet. */
- octet = (AS_HEX_AT(parser->buffer, 1) << 4) + AS_HEX_AT(parser->buffer, 2);
- /* If it is the leading octet, determine the length of the UTF-8 sequence. */
- if (!width)
- {
- width = (octet & 0x80) == 0x00 ? 1 :
- (octet & 0xE0) == 0xC0 ? 2 :
- (octet & 0xF0) == 0xE0 ? 3 :
- (octet & 0xF8) == 0xF0 ? 4 : 0;
- if (!width) {
- return yaml_parser_set_scanner_error(parser, directive ?
- "while parsing a %TAG directive" : "while parsing a tag",
- start_mark, "found an incorrect leading UTF-8 octet");
- }
- }
- else
- {
- /* Check if the trailing octet is correct. */
- if ((octet & 0xC0) != 0x80) {
- return yaml_parser_set_scanner_error(parser, directive ?
- "while parsing a %TAG directive" : "while parsing a tag",
- start_mark, "found an incorrect trailing UTF-8 octet");
- }
- }
- /* Copy the octet and move the pointers. */
- *(string->pointer++) = octet;
- SKIP(parser);
- SKIP(parser);
- SKIP(parser);
- } while (--width);
- return 1;
- }
- /*
- * Scan a block scalar.
- */
- static int
- yaml_parser_scan_block_scalar(yaml_parser_t *parser, yaml_token_t *token,
- int literal)
- {
- yaml_mark_t start_mark;
- yaml_mark_t end_mark;
- yaml_string_t string = NULL_STRING;
- yaml_string_t leading_break = NULL_STRING;
- yaml_string_t trailing_breaks = NULL_STRING;
- int chomping = 0;
- int increment = 0;
- int indent = 0;
- int leading_blank = 0;
- int trailing_blank = 0;
- if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
- if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
- if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
- /* Eat the indicator '|' or '>'. */
- start_mark = parser->mark;
- SKIP(parser);
- /* Scan the additional block scalar indicators. */
- if (!CACHE(parser, 1)) goto error;
- /* Check for a chomping indicator. */
- if (CHECK(parser->buffer, '+') || CHECK(parser->buffer, '-'))
- {
- /* Set the chomping method and eat the indicator. */
- chomping = CHECK(parser->buffer, '+') ? +1 : -1;
- SKIP(parser);
- /* Check for an indentation indicator. */
- if (!CACHE(parser, 1)) goto error;
- if (IS_DIGIT(parser->buffer))
- {
- /* Check that the intendation is greater than 0. */
- if (CHECK(parser->buffer, '0')) {
- yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
- start_mark, "found an intendation indicator equal to 0");
- goto error;
- }
- /* Get the intendation level and eat the indicator. */
- increment = AS_DIGIT(parser->buffer);
- SKIP(parser);
- }
- }
- /* Do the same as above, but in the opposite order. */
- else if (IS_DIGIT(parser->buffer))
- {
- if (CHECK(parser->buffer, '0')) {
- yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
- start_mark, "found an intendation indicator equal to 0");
- goto error;
- }
- increment = AS_DIGIT(parser->buffer);
- SKIP(parser);
- if (!CACHE(parser, 1)) goto error;
- if (CHECK(parser->buffer, '+') || CHECK(parser->buffer, '-')) {
- chomping = CHECK(parser->buffer, '+') ? +1 : -1;
- SKIP(parser);
- }
- }
- /* Eat whitespaces and comments to the end of the line. */
- if (!CACHE(parser, 1)) goto error;
- while (IS_BLANK(parser->buffer)) {
- SKIP(parser);
- if (!CACHE(parser, 1)) goto error;
- }
- if (CHECK(parser->buffer, '#')) {
- while (!IS_BREAKZ(parser->buffer)) {
- SKIP(parser);
- if (!CACHE(parser, 1)) goto error;
- }
- }
- /* Check if we are at the end of the line. */
- if (!IS_BREAKZ(parser->buffer)) {
- yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
- start_mark, "did not find expected comment or line break");
- goto error;
- }
- /* Eat a line break. */
- if (IS_BREAK(parser->buffer)) {
- if (!CACHE(parser, 2)) goto error;
- SKIP_LINE(parser);
- }
- end_mark = parser->mark;
- /* Set the intendation level if it was specified. */
- if (increment) {
- indent = parser->indent >= 0 ? parser->indent+increment : increment;
- }
- /* Scan the leading line breaks and determine the indentation level if needed. */
- if (!yaml_parser_scan_block_scalar_breaks(parser, &indent, &trailing_breaks,
- start_mark, &end_mark)) goto error;
- /* Scan the block scalar content. */
- if (!CACHE(parser, 1)) goto error;
- while ((int)parser->mark.column == indent && !IS_Z(parser->buffer))
- {
- /*
- * We are at the beginning of a non-empty line.
- */
- /* Is it a trailing whitespace? */
- trailing_blank = IS_BLANK(parser->buffer);
- /* Check if we need to fold the leading line break. */
- if (!literal && (*leading_break.start == '\n')
- && !leading_blank && !trailing_blank)
- {
- /* Do we need to join the lines by space? */
- if (*trailing_breaks.start == '\0') {
- if (!STRING_EXTEND(parser, string)) goto error;
- *(string.pointer ++) = ' ';
- }
- CLEAR(parser, leading_break);
- }
- else {
- if (!JOIN(parser, string, leading_break)) goto error;
- CLEAR(parser, leading_break);
- }
- /* Append the remaining line breaks. */
- if (!JOIN(parser, string, trailing_breaks)) goto error;
- CLEAR(parser, trailing_breaks);
- /* Is it a leading whitespace? */
- leading_blank = IS_BLANK(parser->buffer);
- /* Consume the current line. */
- while (!IS_BREAKZ(parser->buffer)) {
- if (!READ(parser, string)) goto error;
- if (!CACHE(parser, 1)) goto error;
- }
- /* Consume the line break. */
- if (!CACHE(parser, 2)) goto error;
- if (!READ_LINE(parser, leading_break)) goto error;
- /* Eat the following intendation spaces and line breaks. */
- if (!yaml_parser_scan_block_scalar_breaks(parser,
- &indent, &trailing_breaks, start_mark, &end_mark)) goto error;
- }
- /* Chomp the tail. */
- if (chomping != -1) {
- if (!JOIN(parser, string, leading_break)) goto error;
- }
- if (chomping == 1) {
- if (!JOIN(parser, string, trailing_breaks)) goto error;
- }
- /* Create a token. */
- SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
- literal ? YAML_LITERAL_SCALAR_STYLE : YAML_FOLDED_SCALAR_STYLE,
- start_mark, end_mark);
- STRING_DEL(parser, leading_break);
- STRING_DEL(parser, trailing_breaks);
- return 1;
- error:
- STRING_DEL(parser, string);
- STRING_DEL(parser, leading_break);
- STRING_DEL(parser, trailing_breaks);
- return 0;
- }
- /*
- * Scan intendation spaces and line breaks for a block scalar. Determine the
- * intendation level if needed.
- */
- static int
- yaml_parser_scan_block_scalar_breaks(yaml_parser_t *parser,
- int *indent, yaml_string_t *breaks,
- yaml_mark_t start_mark, yaml_mark_t *end_mark)
- {
- int max_indent = 0;
- *end_mark = parser->mark;
- /* Eat the intendation spaces and line breaks. */
- while (1)
- {
- /* Eat the intendation spaces. */
- if (!CACHE(parser, 1)) return 0;
- while ((!*indent || (int)parser->mark.column < *indent)
- && IS_SPACE(parser->buffer)) {
- SKIP(parser);
- if (!CACHE(parser, 1)) return 0;
- }
- if ((int)parser->mark.column > max_indent)
- max_indent = (int)parser->mark.column;
- /* Check for a tab character messing the intendation. */
- if ((!*indent || (int)parser->mark.column < *indent)
- && IS_TAB(parser->buffer)) {
- return yaml_parser_set_scanner_error(parser, "while scanning a block scalar",
- start_mark, "found a tab character where an intendation space is expected");
- }
- /* Have we found a non-empty line? */
- if (!IS_BREAK(parser->buffer)) break;
- /* Consume the line break. */
- if (!CACHE(parser, 2)) return 0;
- if (!READ_LINE(parser, *breaks)) return 0;
- *end_mark = parser->mark;
- }
- /* Determine the indentation level if needed. */
- if (!*indent) {
- *indent = max_indent;
- if (*indent < parser->indent + 1)
- *indent = parser->indent + 1;
- if (*indent < 1)
- *indent = 1;
- }
- return 1;
- }
- /*
- * Scan a quoted scalar.
- */
- static int
- yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token,
- int single)
- {
- yaml_mark_t start_mark;
- yaml_mark_t end_mark;
- yaml_string_t string = NULL_STRING;
- yaml_string_t leading_break = NULL_STRING;
- yaml_string_t trailing_breaks = NULL_STRING;
- yaml_string_t whitespaces = NULL_STRING;
- int leading_blanks;
- if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
- if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
- if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
- if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_SIZE)) goto error;
- /* Eat the left quote. */
- start_mark = parser->mark;
- SKIP(parser);
- /* Consume the content of the quoted scalar. */
- while (1)
- {
- /* Check that there are no document indicators at the beginning of the line. */
- if (!CACHE(parser, 4)) goto error;
- if (parser->mark.column == 0 &&
- ((CHECK_AT(parser->buffer, '-', 0) &&
- CHECK_AT(parser->buffer, '-', 1) &&
- CHECK_AT(parser->buffer, '-', 2)) ||
- (CHECK_AT(parser->buffer, '.', 0) &&
- CHECK_AT(parser->buffer, '.', 1) &&
- CHECK_AT(parser->buffer, '.', 2))) &&
- IS_BLANKZ_AT(parser->buffer, 3))
- {
- yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
- start_mark, "found unexpected document indicator");
- goto error;
- }
- /* Check for EOF. */
- if (IS_Z(parser->buffer)) {
- yaml_parser_set_scanner_error(parser, "while scanning a quoted scalar",
- start_mark, "found unexpected end of stream");
- goto error;
- }
- /* Consume non-blank characters. */
- if (!CACHE(parser, 2)) goto error;
- leading_blanks = 0;
- while (!IS_BLANKZ(parser->buffer))
- {
- /* Check for an escaped single quote. */
- if (single && CHECK_AT(parser->buffer, '\'', 0)
- && CHECK_AT(parser->buffer, '\'', 1))
- {
- if (!STRING_EXTEND(parser, string)) goto error;
- *(string.pointer++) = '\'';
- SKIP(parser);
- SKIP(parser);
- }
- /* Check for the right quote. */
- else if (CHECK(parser->buffer, single ? '\'' : '"'))
- {
- break;
- }
- /* Check for an escaped line break. */
- else if (!single && CHECK(parser->buffer, '\\')
- && IS_BREAK_AT(parser->buffer, 1))
- {
- if (!CACHE(parser, 3)) goto error;
- SKIP(parser);
- SKIP_LINE(parser);
- leading_blanks = 1;
- break;
- }
- /* Check for an escape sequence. */
- else if (!single && CHECK(parser->buffer, '\\'))
- {
- size_t code_length = 0;
- if (!STRING_EXTEND(parser, string)) goto error;
- /* Check the escape character. */
- switch (parser->buffer.pointer[1])
- {
- case '0':
- *(string.pointer++) = '\0';
- break;
- case 'a':
- *(string.pointer++) = '\x07';
- break;
- case 'b':
- *(string.pointer++) = '\x08';
- break;
- case 't':
- case '\t':
- *(string.pointer++) = '\x09';
- break;
- case 'n':
- *(string.pointer++) = '\x0A';
- break;
- case 'v':
- *(string.pointer++) = '\x0B';
- break;
- case 'f':
- *(string.pointer++) = '\x0C';
- break;
- case 'r':
- *(string.pointer++) = '\x0D';
- break;
- case 'e':
- *(string.pointer++) = '\x1B';
- break;
- case ' ':
- *(string.pointer++) = '\x20';
- break;
- case '"':
- *(string.pointer++) = '"';
- break;
- case '\'':
- *(string.pointer++) = '\'';
- break;
- case '\\':
- *(string.pointer++) = '\\';
- break;
- case 'N': /* NEL (#x85) */
- *(string.pointer++) = '\xC2';
- *(string.pointer++) = '\x85';
- break;
- case '_': /* #xA0 */
- *(string.pointer++) = '\xC2';
- *(string.pointer++) = '\xA0';
- break;
- case 'L': /* LS (#x2028) */
- *(string.pointer++) = '\xE2';
- *(string.pointer++) = '\x80';
- *(string.pointer++) = '\xA8';
- break;
- case 'P': /* PS (#x2029) */
- *(string.pointer++) = '\xE2';
- *(string.pointer++) = '\x80';
- *(string.pointer++) = '\xA9';
- break;
- case 'x':
- code_length = 2;
- break;
- case 'u':
- code_length = 4;
- break;
- case 'U':
- code_length = 8;
- break;
- default:
- yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
- start_mark, "found unknown escape character");
- goto error;
- }
- SKIP(parser);
- SKIP(parser);
- /* Consume an arbitrary escape code. */
- if (code_length)
- {
- unsigned int value = 0;
- size_t k;
- /* Scan the character value. */
- if (!CACHE(parser, code_length)) goto error;
- for (k = 0; k < code_length; k ++) {
- if (!IS_HEX_AT(parser->buffer, k)) {
- yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
- start_mark, "did not find expected hexdecimal number");
- goto error;
- }
- value = (value << 4) + AS_HEX_AT(parser->buffer, k);
- }
- /* Check the value and write the character. */
- if ((value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF) {
- yaml_parser_set_scanner_error(parser, "while parsing a quoted scalar",
- start_mark, "found invalid Unicode character escape code");
- goto error;
- }
- if (value <= 0x7F) {
- *(string.pointer++) = value;
- }
- else if (value <= 0x7FF) {
- *(string.pointer++) = 0xC0 + (value >> 6);
- *(string.pointer++) = 0x80 + (value & 0x3F);
- }
- else if (value <= 0xFFFF) {
- *(string.pointer++) = 0xE0 + (value >> 12);
- *(string.pointer++) = 0x80 + ((value >> 6) & 0x3F);
- *(string.pointer++) = 0x80 + (value & 0x3F);
- }
- else {
- *(string.pointer++) = 0xF0 + (value >> 18);
- *(string.pointer++) = 0x80 + ((value >> 12) & 0x3F);
- *(string.pointer++) = 0x80 + ((value >> 6) & 0x3F);
- *(string.pointer++) = 0x80 + (value & 0x3F);
- }
- /* Advance the pointer. */
- for (k = 0; k < code_length; k ++) {
- SKIP(parser);
- }
- }
- }
- else
- {
- /* It is a non-escaped non-blank character. */
- if (!READ(parser, string)) goto error;
- }
- if (!CACHE(parser, 2)) goto error;
- }
- /* Check if we are at the end of the scalar. */
- if (CHECK(parser->buffer, single ? '\'' : '"'))
- break;
- /* Consume blank characters. */
- if (!CACHE(parser, 1)) goto error;
- while (IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer))
- {
- if (IS_BLANK(parser->buffer))
- {
- /* Consume a space or a tab character. */
- if (!leading_blanks) {
- if (!READ(parser, whitespaces)) goto error;
- }
- else {
- SKIP(parser);
- }
- }
- else
- {
- if (!CACHE(parser, 2)) goto error;
- /* Check if it is a first line break. */
- if (!leading_blanks)
- {
- CLEAR(parser, whitespaces);
- if (!READ_LINE(parser, leading_break)) goto error;
- leading_blanks = 1;
- }
- else
- {
- if (!READ_LINE(parser, trailing_breaks)) goto error;
- }
- }
- if (!CACHE(parser, 1)) goto error;
- }
- /* Join the whitespaces or fold line breaks. */
- if (leading_blanks)
- {
- /* Do we need to fold line breaks? */
- if (leading_break.start[0] == '\n') {
- if (trailing_breaks.start[0] == '\0') {
- if (!STRING_EXTEND(parser, string)) goto error;
- *(string.pointer++) = ' ';
- }
- else {
- if (!JOIN(parser, string, trailing_breaks)) goto error;
- CLEAR(parser, trailing_breaks);
- }
- CLEAR(parser, leading_break);
- }
- else {
- if (!JOIN(parser, string, leading_break)) goto error;
- if (!JOIN(parser, string, trailing_breaks)) goto error;
- CLEAR(parser, leading_break);
- CLEAR(parser, trailing_breaks);
- }
- }
- else
- {
- if (!JOIN(parser, string, whitespaces)) goto error;
- CLEAR(parser, whitespaces);
- }
- }
- /* Eat the right quote. */
- SKIP(parser);
- end_mark = parser->mark;
- /* Create a token. */
- SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
- single ? YAML_SINGLE_QUOTED_SCALAR_STYLE : YAML_DOUBLE_QUOTED_SCALAR_STYLE,
- start_mark, end_mark);
- STRING_DEL(parser, leading_break);
- STRING_DEL(parser, trailing_breaks);
- STRING_DEL(parser, whitespaces);
- return 1;
- error:
- STRING_DEL(parser, string);
- STRING_DEL(parser, leading_break);
- STRING_DEL(parser, trailing_breaks);
- STRING_DEL(parser, whitespaces);
- return 0;
- }
- /*
- * Scan a plain scalar.
- */
- static int
- yaml_parser_scan_plain_scalar(yaml_parser_t *parser, yaml_token_t *token)
- {
- yaml_mark_t start_mark;
- yaml_mark_t end_mark;
- yaml_string_t string = NULL_STRING;
- yaml_string_t leading_break = NULL_STRING;
- yaml_string_t trailing_breaks = NULL_STRING;
- yaml_string_t whitespaces = NULL_STRING;
- int leading_blanks = 0;
- int indent = parser->indent+1;
- if (!STRING_INIT(parser, string, INITIAL_STRING_SIZE)) goto error;
- if (!STRING_INIT(parser, leading_break, INITIAL_STRING_SIZE)) goto error;
- if (!STRING_INIT(parser, trailing_breaks, INITIAL_STRING_SIZE)) goto error;
- if (!STRING_INIT(parser, whitespaces, INITIAL_STRING_SIZE)) goto error;
- start_mark = end_mark = parser->mark;
- /* Consume the content of the plain scalar. */
- while (1)
- {
- /* Check for a document indicator. */
- if (!CACHE(parser, 4)) goto error;
- if (parser->mark.column == 0 &&
- ((CHECK_AT(parser->buffer, '-', 0) &&
- CHECK_AT(parser->buffer, '-', 1) &&
- CHECK_AT(parser->buffer, '-', 2)) ||
- (CHECK_AT(parser->buffer, '.', 0) &&
- CHECK_AT(parser->buffer, '.', 1) &&
- CHECK_AT(parser->buffer, '.', 2))) &&
- IS_BLANKZ_AT(parser->buffer, 3)) break;
- /* Check for a comment. */
- if (CHECK(parser->buffer, '#'))
- break;
- /* Consume non-blank characters. */
- while (!IS_BLANKZ(parser->buffer))
- {
- /* Check for 'x:x' in the flow context. TODO: Fix the test "spec-08-13". */
- if (parser->flow_level
- && CHECK(parser->buffer, ':')
- && !IS_BLANKZ_AT(parser->buffer, 1)) {
- yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
- start_mark, "found unexpected ':'");
- goto error;
- }
- /* Check for indicators that may end a plain scalar. */
- if ((CHECK(parser->buffer, ':') && IS_BLANKZ_AT(parser->buffer, 1))
- || (parser->flow_level &&
- (CHECK(parser->buffer, ',') || CHECK(parser->buffer, ':')
- || CHECK(parser->buffer, '?') || CHECK(parser->buffer, '[')
- || CHECK(parser->buffer, ']') || CHECK(parser->buffer, '{')
- || CHECK(parser->buffer, '}'))))
- break;
- /* Check if we need to join whitespaces and breaks. */
- if (leading_blanks || whitespaces.start != whitespaces.pointer)
- {
- if (leading_blanks)
- {
- /* Do we need to fold line breaks? */
- if (leading_break.start[0] == '\n') {
- if (trailing_breaks.start[0] == '\0') {
- if (!STRING_EXTEND(parser, string)) goto error;
- *(string.pointer++) = ' ';
- }
- else {
- if (!JOIN(parser, string, trailing_breaks)) goto error;
- CLEAR(parser, trailing_breaks);
- }
- CLEAR(parser, leading_break);
- }
- else {
- if (!JOIN(parser, string, leading_break)) goto error;
- if (!JOIN(parser, string, trailing_breaks)) goto error;
- CLEAR(parser, leading_break);
- CLEAR(parser, trailing_breaks);
- }
- leading_blanks = 0;
- }
- else
- {
- if (!JOIN(parser, string, whitespaces)) goto error;
- CLEAR(parser, whitespaces);
- }
- }
- /* Copy the character. */
- if (!READ(parser, string)) goto error;
- end_mark = parser->mark;
- if (!CACHE(parser, 2)) goto error;
- }
- /* Is it the end? */
- if (!(IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer)))
- break;
- /* Consume blank characters. */
- if (!CACHE(parser, 1)) goto error;
- while (IS_BLANK(parser->buffer) || IS_BREAK(parser->buffer))
- {
- if (IS_BLANK(parser->buffer))
- {
- /* Check for tab character that abuse intendation. */
- if (leading_blanks && (int)parser->mark.column < indent
- && IS_TAB(parser->buffer)) {
- yaml_parser_set_scanner_error(parser, "while scanning a plain scalar",
- start_mark, "found a tab character that violate intendation");
- goto error;
- }
- /* Consume a space or a tab character. */
- if (!leading_blanks) {
- if (!READ(parser, whitespaces)) goto error;
- }
- else {
- SKIP(parser);
- }
- }
- else
- {
- if (!CACHE(parser, 2)) goto error;
- /* Check if it is a first line break. */
- if (!leading_blanks)
- {
- CLEAR(parser, whitespaces);
- if (!READ_LINE(parser, leading_break)) goto error;
- leading_blanks = 1;
- }
- else
- {
- if (!READ_LINE(parser, trailing_breaks)) goto error;
- }
- }
- if (!CACHE(parser, 1)) goto error;
- }
- /* Check intendation level. */
- if (!parser->flow_level && (int)parser->mark.column < indent)
- break;
- }
- /* Create a token. */
- SCALAR_TOKEN_INIT(*token, string.start, string.pointer-string.start,
- YAML_PLAIN_SCALAR_STYLE, start_mark, end_mark);
- /* Note that we change the 'simple_key_allowed' flag. */
- if (leading_blanks) {
- parser->simple_key_allowed = 1;
- }
- STRING_DEL(parser, leading_break);
- STRING_DEL(parser, trailing_breaks);
- STRING_DEL(parser, whitespaces);
- return 1;
- error:
- STRING_DEL(parser, string);
- STRING_DEL(parser, leading_break);
- STRING_DEL(parser, trailing_breaks);
- STRING_DEL(parser, whitespaces);
- return 0;
- }
|