2
0

YAMLParser.cpp 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447
  1. //===--- YAMLParser.cpp - Simple YAML parser ------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements a YAML parser.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Support/YAMLParser.h"
  14. #include "llvm/ADT/SmallString.h"
  15. #include "llvm/ADT/SmallVector.h"
  16. #include "llvm/ADT/StringExtras.h"
  17. #include "llvm/ADT/Twine.h"
  18. #include "llvm/ADT/ilist.h"
  19. #include "llvm/ADT/ilist_node.h"
  20. #include "llvm/Support/ErrorHandling.h"
  21. #include "llvm/Support/MemoryBuffer.h"
  22. #include "llvm/Support/SourceMgr.h"
  23. #include "llvm/Support/raw_ostream.h"
  24. using namespace llvm;
  25. using namespace yaml;
  26. enum UnicodeEncodingForm {
  27. UEF_UTF32_LE, ///< UTF-32 Little Endian
  28. UEF_UTF32_BE, ///< UTF-32 Big Endian
  29. UEF_UTF16_LE, ///< UTF-16 Little Endian
  30. UEF_UTF16_BE, ///< UTF-16 Big Endian
  31. UEF_UTF8, ///< UTF-8 or ascii.
  32. UEF_Unknown ///< Not a valid Unicode encoding.
  33. };
  34. /// EncodingInfo - Holds the encoding type and length of the byte order mark if
  35. /// it exists. Length is in {0, 2, 3, 4}.
  36. typedef std::pair<UnicodeEncodingForm, unsigned> EncodingInfo;
  37. /// getUnicodeEncoding - Reads up to the first 4 bytes to determine the Unicode
  38. /// encoding form of \a Input.
  39. ///
  40. /// @param Input A string of length 0 or more.
  41. /// @returns An EncodingInfo indicating the Unicode encoding form of the input
  42. /// and how long the byte order mark is if one exists.
  43. static EncodingInfo getUnicodeEncoding(StringRef Input) {
  44. if (Input.size() == 0)
  45. return std::make_pair(UEF_Unknown, 0);
  46. switch (uint8_t(Input[0])) {
  47. case 0x00:
  48. if (Input.size() >= 4) {
  49. if ( Input[1] == 0
  50. && uint8_t(Input[2]) == 0xFE
  51. && uint8_t(Input[3]) == 0xFF)
  52. return std::make_pair(UEF_UTF32_BE, 4);
  53. if (Input[1] == 0 && Input[2] == 0 && Input[3] != 0)
  54. return std::make_pair(UEF_UTF32_BE, 0);
  55. }
  56. if (Input.size() >= 2 && Input[1] != 0)
  57. return std::make_pair(UEF_UTF16_BE, 0);
  58. return std::make_pair(UEF_Unknown, 0);
  59. case 0xFF:
  60. if ( Input.size() >= 4
  61. && uint8_t(Input[1]) == 0xFE
  62. && Input[2] == 0
  63. && Input[3] == 0)
  64. return std::make_pair(UEF_UTF32_LE, 4);
  65. if (Input.size() >= 2 && uint8_t(Input[1]) == 0xFE)
  66. return std::make_pair(UEF_UTF16_LE, 2);
  67. return std::make_pair(UEF_Unknown, 0);
  68. case 0xFE:
  69. if (Input.size() >= 2 && uint8_t(Input[1]) == 0xFF)
  70. return std::make_pair(UEF_UTF16_BE, 2);
  71. return std::make_pair(UEF_Unknown, 0);
  72. case 0xEF:
  73. if ( Input.size() >= 3
  74. && uint8_t(Input[1]) == 0xBB
  75. && uint8_t(Input[2]) == 0xBF)
  76. return std::make_pair(UEF_UTF8, 3);
  77. return std::make_pair(UEF_Unknown, 0);
  78. }
  79. // It could still be utf-32 or utf-16.
  80. if (Input.size() >= 4 && Input[1] == 0 && Input[2] == 0 && Input[3] == 0)
  81. return std::make_pair(UEF_UTF32_LE, 0);
  82. if (Input.size() >= 2 && Input[1] == 0)
  83. return std::make_pair(UEF_UTF16_LE, 0);
  84. return std::make_pair(UEF_UTF8, 0);
  85. }
  86. namespace llvm {
  87. namespace yaml {
  88. /// Pin the vtables to this file.
  89. void Node::anchor() {}
  90. void NullNode::anchor() {}
  91. void ScalarNode::anchor() {}
  92. void BlockScalarNode::anchor() {}
  93. void KeyValueNode::anchor() {}
  94. void MappingNode::anchor() {}
  95. void SequenceNode::anchor() {}
  96. void AliasNode::anchor() {}
  97. /// Token - A single YAML token.
  98. struct Token : ilist_node<Token> {
  99. enum TokenKind {
  100. TK_Error, // Uninitialized token.
  101. TK_StreamStart,
  102. TK_StreamEnd,
  103. TK_VersionDirective,
  104. TK_TagDirective,
  105. TK_DocumentStart,
  106. TK_DocumentEnd,
  107. TK_BlockEntry,
  108. TK_BlockEnd,
  109. TK_BlockSequenceStart,
  110. TK_BlockMappingStart,
  111. TK_FlowEntry,
  112. TK_FlowSequenceStart,
  113. TK_FlowSequenceEnd,
  114. TK_FlowMappingStart,
  115. TK_FlowMappingEnd,
  116. TK_Key,
  117. TK_Value,
  118. TK_Scalar,
  119. TK_BlockScalar,
  120. TK_Alias,
  121. TK_Anchor,
  122. TK_Tag
  123. } Kind;
  124. /// A string of length 0 or more whose begin() points to the logical location
  125. /// of the token in the input.
  126. StringRef Range;
  127. /// The value of a block scalar node.
  128. std::string Value;
  129. Token() : Kind(TK_Error) {}
  130. };
  131. }
  132. }
  133. namespace llvm {
  134. template<>
  135. struct ilist_sentinel_traits<Token> {
  136. Token *createSentinel() const {
  137. return &Sentinel;
  138. }
  139. static void destroySentinel(Token*) {}
  140. Token *provideInitialHead() const { return createSentinel(); }
  141. Token *ensureHead(Token*) const { return createSentinel(); }
  142. static void noteHead(Token*, Token*) {}
  143. private:
  144. mutable Token Sentinel;
  145. };
  146. template<>
  147. struct ilist_node_traits<Token> {
  148. Token *createNode(const Token &V) {
  149. return new (Alloc.Allocate<Token>()) Token(V);
  150. }
  151. static void deleteNode(Token *V) { V->~Token(); }
  152. void addNodeToList(Token *) {}
  153. void removeNodeFromList(Token *) {}
  154. void transferNodesFromList(ilist_node_traits & /*SrcTraits*/,
  155. ilist_iterator<Token> /*first*/,
  156. ilist_iterator<Token> /*last*/) {}
  157. BumpPtrAllocator Alloc;
  158. };
  159. }
  160. typedef ilist<Token> TokenQueueT;
  161. namespace {
  162. /// @brief This struct is used to track simple keys.
  163. ///
  164. /// Simple keys are handled by creating an entry in SimpleKeys for each Token
  165. /// which could legally be the start of a simple key. When peekNext is called,
  166. /// if the Token To be returned is referenced by a SimpleKey, we continue
  167. /// tokenizing until that potential simple key has either been found to not be
  168. /// a simple key (we moved on to the next line or went further than 1024 chars).
  169. /// Or when we run into a Value, and then insert a Key token (and possibly
  170. /// others) before the SimpleKey's Tok.
  171. struct SimpleKey {
  172. TokenQueueT::iterator Tok;
  173. unsigned Column;
  174. unsigned Line;
  175. unsigned FlowLevel;
  176. bool IsRequired;
  177. bool operator ==(const SimpleKey &Other) {
  178. return Tok == Other.Tok;
  179. }
  180. };
  181. }
  182. /// @brief The Unicode scalar value of a UTF-8 minimal well-formed code unit
  183. /// subsequence and the subsequence's length in code units (uint8_t).
  184. /// A length of 0 represents an error.
  185. typedef std::pair<uint32_t, unsigned> UTF8Decoded;
  186. static UTF8Decoded decodeUTF8(StringRef Range) {
  187. StringRef::iterator Position= Range.begin();
  188. StringRef::iterator End = Range.end();
  189. // 1 byte: [0x00, 0x7f]
  190. // Bit pattern: 0xxxxxxx
  191. if ((*Position & 0x80) == 0) {
  192. return std::make_pair(*Position, 1);
  193. }
  194. // 2 bytes: [0x80, 0x7ff]
  195. // Bit pattern: 110xxxxx 10xxxxxx
  196. if (Position + 1 != End &&
  197. ((*Position & 0xE0) == 0xC0) &&
  198. ((*(Position + 1) & 0xC0) == 0x80)) {
  199. uint32_t codepoint = ((*Position & 0x1F) << 6) |
  200. (*(Position + 1) & 0x3F);
  201. if (codepoint >= 0x80)
  202. return std::make_pair(codepoint, 2);
  203. }
  204. // 3 bytes: [0x8000, 0xffff]
  205. // Bit pattern: 1110xxxx 10xxxxxx 10xxxxxx
  206. if (Position + 2 != End &&
  207. ((*Position & 0xF0) == 0xE0) &&
  208. ((*(Position + 1) & 0xC0) == 0x80) &&
  209. ((*(Position + 2) & 0xC0) == 0x80)) {
  210. uint32_t codepoint = ((*Position & 0x0F) << 12) |
  211. ((*(Position + 1) & 0x3F) << 6) |
  212. (*(Position + 2) & 0x3F);
  213. // Codepoints between 0xD800 and 0xDFFF are invalid, as
  214. // they are high / low surrogate halves used by UTF-16.
  215. if (codepoint >= 0x800 &&
  216. (codepoint < 0xD800 || codepoint > 0xDFFF))
  217. return std::make_pair(codepoint, 3);
  218. }
  219. // 4 bytes: [0x10000, 0x10FFFF]
  220. // Bit pattern: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  221. if (Position + 3 != End &&
  222. ((*Position & 0xF8) == 0xF0) &&
  223. ((*(Position + 1) & 0xC0) == 0x80) &&
  224. ((*(Position + 2) & 0xC0) == 0x80) &&
  225. ((*(Position + 3) & 0xC0) == 0x80)) {
  226. uint32_t codepoint = ((*Position & 0x07) << 18) |
  227. ((*(Position + 1) & 0x3F) << 12) |
  228. ((*(Position + 2) & 0x3F) << 6) |
  229. (*(Position + 3) & 0x3F);
  230. if (codepoint >= 0x10000 && codepoint <= 0x10FFFF)
  231. return std::make_pair(codepoint, 4);
  232. }
  233. return std::make_pair(0, 0);
  234. }
  235. namespace llvm {
  236. namespace yaml {
  237. /// @brief Scans YAML tokens from a MemoryBuffer.
  238. class Scanner {
  239. public:
  240. Scanner(StringRef Input, SourceMgr &SM, bool ShowColors = true);
  241. Scanner(MemoryBufferRef Buffer, SourceMgr &SM_, bool ShowColors = true);
  242. /// @brief Parse the next token and return it without popping it.
  243. Token &peekNext();
  244. /// @brief Parse the next token and pop it from the queue.
  245. Token getNext();
  246. void printError(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Message,
  247. ArrayRef<SMRange> Ranges = None) {
  248. SM.PrintMessage(Loc, Kind, Message, Ranges, /* FixIts= */ None, ShowColors);
  249. }
  250. void setError(const Twine &Message, StringRef::iterator Position) {
  251. if (Current >= End)
  252. Current = End - 1;
  253. // Don't print out more errors after the first one we encounter. The rest
  254. // are just the result of the first, and have no meaning.
  255. if (!Failed)
  256. printError(SMLoc::getFromPointer(Current), SourceMgr::DK_Error, Message);
  257. Failed = true;
  258. }
  259. void setError(const Twine &Message) {
  260. setError(Message, Current);
  261. }
  262. /// @brief Returns true if an error occurred while parsing.
  263. bool failed() {
  264. return Failed;
  265. }
  266. private:
  267. void init(MemoryBufferRef Buffer);
  268. StringRef currentInput() {
  269. return StringRef(Current, End - Current);
  270. }
  271. /// @brief Decode a UTF-8 minimal well-formed code unit subsequence starting
  272. /// at \a Position.
  273. ///
  274. /// If the UTF-8 code units starting at Position do not form a well-formed
  275. /// code unit subsequence, then the Unicode scalar value is 0, and the length
  276. /// is 0.
  277. UTF8Decoded decodeUTF8(StringRef::iterator Position) {
  278. return ::decodeUTF8(StringRef(Position, End - Position));
  279. }
  280. // The following functions are based on the gramar rules in the YAML spec. The
  281. // style of the function names it meant to closely match how they are written
  282. // in the spec. The number within the [] is the number of the grammar rule in
  283. // the spec.
  284. //
  285. // See 4.2 [Production Naming Conventions] for the meaning of the prefixes.
  286. //
  287. // c-
  288. // A production starting and ending with a special character.
  289. // b-
  290. // A production matching a single line break.
  291. // nb-
  292. // A production starting and ending with a non-break character.
  293. // s-
  294. // A production starting and ending with a white space character.
  295. // ns-
  296. // A production starting and ending with a non-space character.
  297. // l-
  298. // A production matching complete line(s).
  299. /// @brief Skip a single nb-char[27] starting at Position.
  300. ///
  301. /// A nb-char is 0x9 | [0x20-0x7E] | 0x85 | [0xA0-0xD7FF] | [0xE000-0xFEFE]
  302. /// | [0xFF00-0xFFFD] | [0x10000-0x10FFFF]
  303. ///
  304. /// @returns The code unit after the nb-char, or Position if it's not an
  305. /// nb-char.
  306. StringRef::iterator skip_nb_char(StringRef::iterator Position);
  307. /// @brief Skip a single b-break[28] starting at Position.
  308. ///
  309. /// A b-break is 0xD 0xA | 0xD | 0xA
  310. ///
  311. /// @returns The code unit after the b-break, or Position if it's not a
  312. /// b-break.
  313. StringRef::iterator skip_b_break(StringRef::iterator Position);
  314. /// Skip a single s-space[31] starting at Position.
  315. ///
  316. /// An s-space is 0x20
  317. ///
  318. /// @returns The code unit after the s-space, or Position if it's not a
  319. /// s-space.
  320. StringRef::iterator skip_s_space(StringRef::iterator Position);
  321. /// @brief Skip a single s-white[33] starting at Position.
  322. ///
  323. /// A s-white is 0x20 | 0x9
  324. ///
  325. /// @returns The code unit after the s-white, or Position if it's not a
  326. /// s-white.
  327. StringRef::iterator skip_s_white(StringRef::iterator Position);
  328. /// @brief Skip a single ns-char[34] starting at Position.
  329. ///
  330. /// A ns-char is nb-char - s-white
  331. ///
  332. /// @returns The code unit after the ns-char, or Position if it's not a
  333. /// ns-char.
  334. StringRef::iterator skip_ns_char(StringRef::iterator Position);
  335. typedef StringRef::iterator (Scanner::*SkipWhileFunc)(StringRef::iterator);
  336. /// @brief Skip minimal well-formed code unit subsequences until Func
  337. /// returns its input.
  338. ///
  339. /// @returns The code unit after the last minimal well-formed code unit
  340. /// subsequence that Func accepted.
  341. StringRef::iterator skip_while( SkipWhileFunc Func
  342. , StringRef::iterator Position);
  343. /// Skip minimal well-formed code unit subsequences until Func returns its
  344. /// input.
  345. void advanceWhile(SkipWhileFunc Func);
  346. /// @brief Scan ns-uri-char[39]s starting at Cur.
  347. ///
  348. /// This updates Cur and Column while scanning.
  349. ///
  350. /// @returns A StringRef starting at Cur which covers the longest contiguous
  351. /// sequence of ns-uri-char.
  352. StringRef scan_ns_uri_char();
  353. /// @brief Consume a minimal well-formed code unit subsequence starting at
  354. /// \a Cur. Return false if it is not the same Unicode scalar value as
  355. /// \a Expected. This updates \a Column.
  356. bool consume(uint32_t Expected);
  357. /// @brief Skip \a Distance UTF-8 code units. Updates \a Cur and \a Column.
  358. void skip(uint32_t Distance);
  359. /// @brief Return true if the minimal well-formed code unit subsequence at
  360. /// Pos is whitespace or a new line
  361. bool isBlankOrBreak(StringRef::iterator Position);
  362. /// Consume a single b-break[28] if it's present at the current position.
  363. ///
  364. /// Return false if the code unit at the current position isn't a line break.
  365. bool consumeLineBreakIfPresent();
  366. /// @brief If IsSimpleKeyAllowed, create and push_back a new SimpleKey.
  367. void saveSimpleKeyCandidate( TokenQueueT::iterator Tok
  368. , unsigned AtColumn
  369. , bool IsRequired);
  370. /// @brief Remove simple keys that can no longer be valid simple keys.
  371. ///
  372. /// Invalid simple keys are not on the current line or are further than 1024
  373. /// columns back.
  374. void removeStaleSimpleKeyCandidates();
  375. /// @brief Remove all simple keys on FlowLevel \a Level.
  376. void removeSimpleKeyCandidatesOnFlowLevel(unsigned Level);
  377. /// @brief Unroll indentation in \a Indents back to \a Col. Creates BlockEnd
  378. /// tokens if needed.
  379. bool unrollIndent(int ToColumn);
  380. /// @brief Increase indent to \a Col. Creates \a Kind token at \a InsertPoint
  381. /// if needed.
  382. bool rollIndent( int ToColumn
  383. , Token::TokenKind Kind
  384. , TokenQueueT::iterator InsertPoint);
  385. /// @brief Skip a single-line comment when the comment starts at the current
  386. /// position of the scanner.
  387. void skipComment();
  388. /// @brief Skip whitespace and comments until the start of the next token.
  389. void scanToNextToken();
  390. /// @brief Must be the first token generated.
  391. bool scanStreamStart();
  392. /// @brief Generate tokens needed to close out the stream.
  393. bool scanStreamEnd();
  394. /// @brief Scan a %BLAH directive.
  395. bool scanDirective();
  396. /// @brief Scan a ... or ---.
  397. bool scanDocumentIndicator(bool IsStart);
  398. /// @brief Scan a [ or { and generate the proper flow collection start token.
  399. bool scanFlowCollectionStart(bool IsSequence);
  400. /// @brief Scan a ] or } and generate the proper flow collection end token.
  401. bool scanFlowCollectionEnd(bool IsSequence);
  402. /// @brief Scan the , that separates entries in a flow collection.
  403. bool scanFlowEntry();
  404. /// @brief Scan the - that starts block sequence entries.
  405. bool scanBlockEntry();
  406. /// @brief Scan an explicit ? indicating a key.
  407. bool scanKey();
  408. /// @brief Scan an explicit : indicating a value.
  409. bool scanValue();
  410. /// @brief Scan a quoted scalar.
  411. bool scanFlowScalar(bool IsDoubleQuoted);
  412. /// @brief Scan an unquoted scalar.
  413. bool scanPlainScalar();
  414. /// @brief Scan an Alias or Anchor starting with * or &.
  415. bool scanAliasOrAnchor(bool IsAlias);
  416. /// @brief Scan a block scalar starting with | or >.
  417. bool scanBlockScalar(bool IsLiteral);
  418. /// Scan a chomping indicator in a block scalar header.
  419. char scanBlockChompingIndicator();
  420. /// Scan an indentation indicator in a block scalar header.
  421. unsigned scanBlockIndentationIndicator();
  422. /// Scan a block scalar header.
  423. ///
  424. /// Return false if an error occurred.
  425. bool scanBlockScalarHeader(char &ChompingIndicator, unsigned &IndentIndicator,
  426. bool &IsDone);
  427. /// Look for the indentation level of a block scalar.
  428. ///
  429. /// Return false if an error occurred.
  430. bool findBlockScalarIndent(unsigned &BlockIndent, unsigned BlockExitIndent,
  431. unsigned &LineBreaks, bool &IsDone);
  432. /// Scan the indentation of a text line in a block scalar.
  433. ///
  434. /// Return false if an error occurred.
  435. bool scanBlockScalarIndent(unsigned BlockIndent, unsigned BlockExitIndent,
  436. bool &IsDone);
  437. /// @brief Scan a tag of the form !stuff.
  438. bool scanTag();
  439. /// @brief Dispatch to the next scanning function based on \a *Cur.
  440. bool fetchMoreTokens();
  441. /// @brief The SourceMgr used for diagnostics and buffer management.
  442. SourceMgr &SM;
  443. /// @brief The original input.
  444. MemoryBufferRef InputBuffer;
  445. /// @brief The current position of the scanner.
  446. StringRef::iterator Current;
  447. /// @brief The end of the input (one past the last character).
  448. StringRef::iterator End;
  449. /// @brief Current YAML indentation level in spaces.
  450. int Indent;
  451. /// @brief Current column number in Unicode code points.
  452. unsigned Column;
  453. /// @brief Current line number.
  454. unsigned Line;
  455. /// @brief How deep we are in flow style containers. 0 Means at block level.
  456. unsigned FlowLevel;
  457. /// @brief Are we at the start of the stream?
  458. bool IsStartOfStream;
  459. /// @brief Can the next token be the start of a simple key?
  460. bool IsSimpleKeyAllowed;
  461. /// @brief True if an error has occurred.
  462. bool Failed;
  463. /// @brief Should colors be used when printing out the diagnostic messages?
  464. bool ShowColors;
  465. /// @brief Queue of tokens. This is required to queue up tokens while looking
  466. /// for the end of a simple key. And for cases where a single character
  467. /// can produce multiple tokens (e.g. BlockEnd).
  468. TokenQueueT TokenQueue;
  469. /// @brief Indentation levels.
  470. SmallVector<int, 4> Indents;
  471. /// @brief Potential simple keys.
  472. SmallVector<SimpleKey, 4> SimpleKeys;
  473. };
  474. } // end namespace yaml
  475. } // end namespace llvm
  476. /// encodeUTF8 - Encode \a UnicodeScalarValue in UTF-8 and append it to result.
  477. static void encodeUTF8( uint32_t UnicodeScalarValue
  478. , SmallVectorImpl<char> &Result) {
  479. if (UnicodeScalarValue <= 0x7F) {
  480. Result.push_back(UnicodeScalarValue & 0x7F);
  481. } else if (UnicodeScalarValue <= 0x7FF) {
  482. uint8_t FirstByte = 0xC0 | ((UnicodeScalarValue & 0x7C0) >> 6);
  483. uint8_t SecondByte = 0x80 | (UnicodeScalarValue & 0x3F);
  484. Result.push_back(FirstByte);
  485. Result.push_back(SecondByte);
  486. } else if (UnicodeScalarValue <= 0xFFFF) {
  487. uint8_t FirstByte = 0xE0 | ((UnicodeScalarValue & 0xF000) >> 12);
  488. uint8_t SecondByte = 0x80 | ((UnicodeScalarValue & 0xFC0) >> 6);
  489. uint8_t ThirdByte = 0x80 | (UnicodeScalarValue & 0x3F);
  490. Result.push_back(FirstByte);
  491. Result.push_back(SecondByte);
  492. Result.push_back(ThirdByte);
  493. } else if (UnicodeScalarValue <= 0x10FFFF) {
  494. uint8_t FirstByte = 0xF0 | ((UnicodeScalarValue & 0x1F0000) >> 18);
  495. uint8_t SecondByte = 0x80 | ((UnicodeScalarValue & 0x3F000) >> 12);
  496. uint8_t ThirdByte = 0x80 | ((UnicodeScalarValue & 0xFC0) >> 6);
  497. uint8_t FourthByte = 0x80 | (UnicodeScalarValue & 0x3F);
  498. Result.push_back(FirstByte);
  499. Result.push_back(SecondByte);
  500. Result.push_back(ThirdByte);
  501. Result.push_back(FourthByte);
  502. }
  503. }
  504. bool yaml::dumpTokens(StringRef Input, raw_ostream &OS) {
  505. SourceMgr SM;
  506. Scanner scanner(Input, SM);
  507. while (true) {
  508. Token T = scanner.getNext();
  509. switch (T.Kind) {
  510. case Token::TK_StreamStart:
  511. OS << "Stream-Start: ";
  512. break;
  513. case Token::TK_StreamEnd:
  514. OS << "Stream-End: ";
  515. break;
  516. case Token::TK_VersionDirective:
  517. OS << "Version-Directive: ";
  518. break;
  519. case Token::TK_TagDirective:
  520. OS << "Tag-Directive: ";
  521. break;
  522. case Token::TK_DocumentStart:
  523. OS << "Document-Start: ";
  524. break;
  525. case Token::TK_DocumentEnd:
  526. OS << "Document-End: ";
  527. break;
  528. case Token::TK_BlockEntry:
  529. OS << "Block-Entry: ";
  530. break;
  531. case Token::TK_BlockEnd:
  532. OS << "Block-End: ";
  533. break;
  534. case Token::TK_BlockSequenceStart:
  535. OS << "Block-Sequence-Start: ";
  536. break;
  537. case Token::TK_BlockMappingStart:
  538. OS << "Block-Mapping-Start: ";
  539. break;
  540. case Token::TK_FlowEntry:
  541. OS << "Flow-Entry: ";
  542. break;
  543. case Token::TK_FlowSequenceStart:
  544. OS << "Flow-Sequence-Start: ";
  545. break;
  546. case Token::TK_FlowSequenceEnd:
  547. OS << "Flow-Sequence-End: ";
  548. break;
  549. case Token::TK_FlowMappingStart:
  550. OS << "Flow-Mapping-Start: ";
  551. break;
  552. case Token::TK_FlowMappingEnd:
  553. OS << "Flow-Mapping-End: ";
  554. break;
  555. case Token::TK_Key:
  556. OS << "Key: ";
  557. break;
  558. case Token::TK_Value:
  559. OS << "Value: ";
  560. break;
  561. case Token::TK_Scalar:
  562. OS << "Scalar: ";
  563. break;
  564. case Token::TK_BlockScalar:
  565. OS << "Block Scalar: ";
  566. break;
  567. case Token::TK_Alias:
  568. OS << "Alias: ";
  569. break;
  570. case Token::TK_Anchor:
  571. OS << "Anchor: ";
  572. break;
  573. case Token::TK_Tag:
  574. OS << "Tag: ";
  575. break;
  576. case Token::TK_Error:
  577. break;
  578. }
  579. OS << T.Range << "\n";
  580. if (T.Kind == Token::TK_StreamEnd)
  581. break;
  582. else if (T.Kind == Token::TK_Error)
  583. return false;
  584. }
  585. return true;
  586. }
  587. bool yaml::scanTokens(StringRef Input) {
  588. llvm::SourceMgr SM;
  589. llvm::yaml::Scanner scanner(Input, SM);
  590. for (;;) {
  591. llvm::yaml::Token T = scanner.getNext();
  592. if (T.Kind == Token::TK_StreamEnd)
  593. break;
  594. else if (T.Kind == Token::TK_Error)
  595. return false;
  596. }
  597. return true;
  598. }
  599. std::string yaml::escape(StringRef Input) {
  600. std::string EscapedInput;
  601. for (StringRef::iterator i = Input.begin(), e = Input.end(); i != e; ++i) {
  602. if (*i == '\\')
  603. EscapedInput += "\\\\";
  604. else if (*i == '"')
  605. EscapedInput += "\\\"";
  606. else if (*i == 0)
  607. EscapedInput += "\\0";
  608. else if (*i == 0x07)
  609. EscapedInput += "\\a";
  610. else if (*i == 0x08)
  611. EscapedInput += "\\b";
  612. else if (*i == 0x09)
  613. EscapedInput += "\\t";
  614. else if (*i == 0x0A)
  615. EscapedInput += "\\n";
  616. else if (*i == 0x0B)
  617. EscapedInput += "\\v";
  618. else if (*i == 0x0C)
  619. EscapedInput += "\\f";
  620. else if (*i == 0x0D)
  621. EscapedInput += "\\r";
  622. else if (*i == 0x1B)
  623. EscapedInput += "\\e";
  624. else if ((unsigned char)*i < 0x20) { // Control characters not handled above.
  625. std::string HexStr = utohexstr(*i);
  626. EscapedInput += "\\x" + std::string(2 - HexStr.size(), '0') + HexStr;
  627. } else if (*i & 0x80) { // UTF-8 multiple code unit subsequence.
  628. UTF8Decoded UnicodeScalarValue
  629. = decodeUTF8(StringRef(i, Input.end() - i));
  630. if (UnicodeScalarValue.second == 0) {
  631. // Found invalid char.
  632. SmallString<4> Val;
  633. encodeUTF8(0xFFFD, Val);
  634. EscapedInput.insert(EscapedInput.end(), Val.begin(), Val.end());
  635. // FIXME: Error reporting.
  636. return EscapedInput;
  637. }
  638. if (UnicodeScalarValue.first == 0x85)
  639. EscapedInput += "\\N";
  640. else if (UnicodeScalarValue.first == 0xA0)
  641. EscapedInput += "\\_";
  642. else if (UnicodeScalarValue.first == 0x2028)
  643. EscapedInput += "\\L";
  644. else if (UnicodeScalarValue.first == 0x2029)
  645. EscapedInput += "\\P";
  646. else {
  647. std::string HexStr = utohexstr(UnicodeScalarValue.first);
  648. if (HexStr.size() <= 2)
  649. EscapedInput += "\\x" + std::string(2 - HexStr.size(), '0') + HexStr;
  650. else if (HexStr.size() <= 4)
  651. EscapedInput += "\\u" + std::string(4 - HexStr.size(), '0') + HexStr;
  652. else if (HexStr.size() <= 8)
  653. EscapedInput += "\\U" + std::string(8 - HexStr.size(), '0') + HexStr;
  654. }
  655. i += UnicodeScalarValue.second - 1;
  656. } else
  657. EscapedInput.push_back(*i);
  658. }
  659. return EscapedInput;
  660. }
  661. Scanner::Scanner(StringRef Input, SourceMgr &sm, bool ShowColors)
  662. : SM(sm), ShowColors(ShowColors) {
  663. init(MemoryBufferRef(Input, "YAML"));
  664. }
  665. Scanner::Scanner(MemoryBufferRef Buffer, SourceMgr &SM_, bool ShowColors)
  666. : SM(SM_), ShowColors(ShowColors) {
  667. init(Buffer);
  668. }
  669. void Scanner::init(MemoryBufferRef Buffer) {
  670. InputBuffer = Buffer;
  671. Current = InputBuffer.getBufferStart();
  672. End = InputBuffer.getBufferEnd();
  673. Indent = -1;
  674. Column = 0;
  675. Line = 0;
  676. FlowLevel = 0;
  677. IsStartOfStream = true;
  678. IsSimpleKeyAllowed = true;
  679. Failed = false;
  680. std::unique_ptr<MemoryBuffer> InputBufferOwner =
  681. MemoryBuffer::getMemBuffer(Buffer);
  682. SM.AddNewSourceBuffer(std::move(InputBufferOwner), SMLoc());
  683. }
  684. Token &Scanner::peekNext() {
  685. // If the current token is a possible simple key, keep parsing until we
  686. // can confirm.
  687. bool NeedMore = false;
  688. while (true) {
  689. if (TokenQueue.empty() || NeedMore) {
  690. if (!fetchMoreTokens()) {
  691. TokenQueue.clear();
  692. TokenQueue.push_back(Token());
  693. return TokenQueue.front();
  694. }
  695. }
  696. assert(!TokenQueue.empty() &&
  697. "fetchMoreTokens lied about getting tokens!");
  698. removeStaleSimpleKeyCandidates();
  699. SimpleKey SK;
  700. SK.Tok = TokenQueue.front();
  701. if (std::find(SimpleKeys.begin(), SimpleKeys.end(), SK)
  702. == SimpleKeys.end())
  703. break;
  704. else
  705. NeedMore = true;
  706. }
  707. return TokenQueue.front();
  708. }
  709. Token Scanner::getNext() {
  710. Token Ret = peekNext();
  711. // TokenQueue can be empty if there was an error getting the next token.
  712. if (!TokenQueue.empty())
  713. TokenQueue.pop_front();
  714. // There cannot be any referenced Token's if the TokenQueue is empty. So do a
  715. // quick deallocation of them all.
  716. if (TokenQueue.empty()) {
  717. TokenQueue.Alloc.Reset();
  718. }
  719. return Ret;
  720. }
  721. StringRef::iterator Scanner::skip_nb_char(StringRef::iterator Position) {
  722. if (Position == End)
  723. return Position;
  724. // Check 7 bit c-printable - b-char.
  725. if ( *Position == 0x09
  726. || (*Position >= 0x20 && *Position <= 0x7E))
  727. return Position + 1;
  728. // Check for valid UTF-8.
  729. if (uint8_t(*Position) & 0x80) {
  730. UTF8Decoded u8d = decodeUTF8(Position);
  731. if ( u8d.second != 0
  732. && u8d.first != 0xFEFF
  733. && ( u8d.first == 0x85
  734. || ( u8d.first >= 0xA0
  735. && u8d.first <= 0xD7FF)
  736. || ( u8d.first >= 0xE000
  737. && u8d.first <= 0xFFFD)
  738. || ( u8d.first >= 0x10000
  739. && u8d.first <= 0x10FFFF)))
  740. return Position + u8d.second;
  741. }
  742. return Position;
  743. }
  744. StringRef::iterator Scanner::skip_b_break(StringRef::iterator Position) {
  745. if (Position == End)
  746. return Position;
  747. if (*Position == 0x0D) {
  748. if (Position + 1 != End && *(Position + 1) == 0x0A)
  749. return Position + 2;
  750. return Position + 1;
  751. }
  752. if (*Position == 0x0A)
  753. return Position + 1;
  754. return Position;
  755. }
  756. StringRef::iterator Scanner::skip_s_space(StringRef::iterator Position) {
  757. if (Position == End)
  758. return Position;
  759. if (*Position == ' ')
  760. return Position + 1;
  761. return Position;
  762. }
  763. StringRef::iterator Scanner::skip_s_white(StringRef::iterator Position) {
  764. if (Position == End)
  765. return Position;
  766. if (*Position == ' ' || *Position == '\t')
  767. return Position + 1;
  768. return Position;
  769. }
  770. StringRef::iterator Scanner::skip_ns_char(StringRef::iterator Position) {
  771. if (Position == End)
  772. return Position;
  773. if (*Position == ' ' || *Position == '\t')
  774. return Position;
  775. return skip_nb_char(Position);
  776. }
  777. StringRef::iterator Scanner::skip_while( SkipWhileFunc Func
  778. , StringRef::iterator Position) {
  779. while (true) {
  780. StringRef::iterator i = (this->*Func)(Position);
  781. if (i == Position)
  782. break;
  783. Position = i;
  784. }
  785. return Position;
  786. }
  787. void Scanner::advanceWhile(SkipWhileFunc Func) {
  788. auto Final = skip_while(Func, Current);
  789. Column += Final - Current;
  790. Current = Final;
  791. }
  792. static bool is_ns_hex_digit(const char C) {
  793. return (C >= '0' && C <= '9')
  794. || (C >= 'a' && C <= 'z')
  795. || (C >= 'A' && C <= 'Z');
  796. }
  797. static bool is_ns_word_char(const char C) {
  798. return C == '-'
  799. || (C >= 'a' && C <= 'z')
  800. || (C >= 'A' && C <= 'Z');
  801. }
  802. StringRef Scanner::scan_ns_uri_char() {
  803. StringRef::iterator Start = Current;
  804. while (true) {
  805. if (Current == End)
  806. break;
  807. if (( *Current == '%'
  808. && Current + 2 < End
  809. && is_ns_hex_digit(*(Current + 1))
  810. && is_ns_hex_digit(*(Current + 2)))
  811. || is_ns_word_char(*Current)
  812. || StringRef(Current, 1).find_first_of("#;/?:@&=+$,_.!~*'()[]")
  813. != StringRef::npos) {
  814. ++Current;
  815. ++Column;
  816. } else
  817. break;
  818. }
  819. return StringRef(Start, Current - Start);
  820. }
  821. bool Scanner::consume(uint32_t Expected) {
  822. if (Expected >= 0x80)
  823. report_fatal_error("Not dealing with this yet");
  824. if (Current == End)
  825. return false;
  826. if (uint8_t(*Current) >= 0x80)
  827. report_fatal_error("Not dealing with this yet");
  828. if (uint8_t(*Current) == Expected) {
  829. ++Current;
  830. ++Column;
  831. return true;
  832. }
  833. return false;
  834. }
  835. void Scanner::skip(uint32_t Distance) {
  836. Current += Distance;
  837. Column += Distance;
  838. assert(Current <= End && "Skipped past the end");
  839. }
  840. bool Scanner::isBlankOrBreak(StringRef::iterator Position) {
  841. if (Position == End)
  842. return false;
  843. if ( *Position == ' ' || *Position == '\t'
  844. || *Position == '\r' || *Position == '\n')
  845. return true;
  846. return false;
  847. }
  848. bool Scanner::consumeLineBreakIfPresent() {
  849. auto Next = skip_b_break(Current);
  850. if (Next == Current)
  851. return false;
  852. Column = 0;
  853. ++Line;
  854. Current = Next;
  855. return true;
  856. }
  857. void Scanner::saveSimpleKeyCandidate( TokenQueueT::iterator Tok
  858. , unsigned AtColumn
  859. , bool IsRequired) {
  860. if (IsSimpleKeyAllowed) {
  861. SimpleKey SK;
  862. SK.Tok = Tok;
  863. SK.Line = Line;
  864. SK.Column = AtColumn;
  865. SK.IsRequired = IsRequired;
  866. SK.FlowLevel = FlowLevel;
  867. SimpleKeys.push_back(SK);
  868. }
  869. }
  870. void Scanner::removeStaleSimpleKeyCandidates() {
  871. for (SmallVectorImpl<SimpleKey>::iterator i = SimpleKeys.begin();
  872. i != SimpleKeys.end();) {
  873. if (i->Line != Line || i->Column + 1024 < Column) {
  874. if (i->IsRequired)
  875. setError( "Could not find expected : for simple key"
  876. , i->Tok->Range.begin());
  877. i = SimpleKeys.erase(i);
  878. } else
  879. ++i;
  880. }
  881. }
  882. void Scanner::removeSimpleKeyCandidatesOnFlowLevel(unsigned Level) {
  883. if (!SimpleKeys.empty() && (SimpleKeys.end() - 1)->FlowLevel == Level)
  884. SimpleKeys.pop_back();
  885. }
  886. bool Scanner::unrollIndent(int ToColumn) {
  887. Token T;
  888. // Indentation is ignored in flow.
  889. if (FlowLevel != 0)
  890. return true;
  891. while (Indent > ToColumn) {
  892. T.Kind = Token::TK_BlockEnd;
  893. T.Range = StringRef(Current, 1);
  894. TokenQueue.push_back(T);
  895. Indent = Indents.pop_back_val();
  896. }
  897. return true;
  898. }
  899. bool Scanner::rollIndent( int ToColumn
  900. , Token::TokenKind Kind
  901. , TokenQueueT::iterator InsertPoint) {
  902. if (FlowLevel)
  903. return true;
  904. if (Indent < ToColumn) {
  905. Indents.push_back(Indent);
  906. Indent = ToColumn;
  907. Token T;
  908. T.Kind = Kind;
  909. T.Range = StringRef(Current, 0);
  910. TokenQueue.insert(InsertPoint, T);
  911. }
  912. return true;
  913. }
  914. void Scanner::skipComment() {
  915. if (*Current != '#')
  916. return;
  917. while (true) {
  918. // This may skip more than one byte, thus Column is only incremented
  919. // for code points.
  920. StringRef::iterator I = skip_nb_char(Current);
  921. if (I == Current)
  922. break;
  923. Current = I;
  924. ++Column;
  925. }
  926. }
  927. void Scanner::scanToNextToken() {
  928. while (true) {
  929. while (*Current == ' ' || *Current == '\t') {
  930. skip(1);
  931. }
  932. skipComment();
  933. // Skip EOL.
  934. StringRef::iterator i = skip_b_break(Current);
  935. if (i == Current)
  936. break;
  937. Current = i;
  938. ++Line;
  939. Column = 0;
  940. // New lines may start a simple key.
  941. if (!FlowLevel)
  942. IsSimpleKeyAllowed = true;
  943. }
  944. }
  945. bool Scanner::scanStreamStart() {
  946. IsStartOfStream = false;
  947. EncodingInfo EI = getUnicodeEncoding(currentInput());
  948. Token T;
  949. T.Kind = Token::TK_StreamStart;
  950. T.Range = StringRef(Current, EI.second);
  951. TokenQueue.push_back(T);
  952. Current += EI.second;
  953. return true;
  954. }
  955. bool Scanner::scanStreamEnd() {
  956. // Force an ending new line if one isn't present.
  957. if (Column != 0) {
  958. Column = 0;
  959. ++Line;
  960. }
  961. unrollIndent(-1);
  962. SimpleKeys.clear();
  963. IsSimpleKeyAllowed = false;
  964. Token T;
  965. T.Kind = Token::TK_StreamEnd;
  966. T.Range = StringRef(Current, 0);
  967. TokenQueue.push_back(T);
  968. return true;
  969. }
  970. bool Scanner::scanDirective() {
  971. // Reset the indentation level.
  972. unrollIndent(-1);
  973. SimpleKeys.clear();
  974. IsSimpleKeyAllowed = false;
  975. StringRef::iterator Start = Current;
  976. consume('%');
  977. StringRef::iterator NameStart = Current;
  978. Current = skip_while(&Scanner::skip_ns_char, Current);
  979. StringRef Name(NameStart, Current - NameStart);
  980. Current = skip_while(&Scanner::skip_s_white, Current);
  981. Token T;
  982. if (Name == "YAML") {
  983. Current = skip_while(&Scanner::skip_ns_char, Current);
  984. T.Kind = Token::TK_VersionDirective;
  985. T.Range = StringRef(Start, Current - Start);
  986. TokenQueue.push_back(T);
  987. return true;
  988. } else if(Name == "TAG") {
  989. Current = skip_while(&Scanner::skip_ns_char, Current);
  990. Current = skip_while(&Scanner::skip_s_white, Current);
  991. Current = skip_while(&Scanner::skip_ns_char, Current);
  992. T.Kind = Token::TK_TagDirective;
  993. T.Range = StringRef(Start, Current - Start);
  994. TokenQueue.push_back(T);
  995. return true;
  996. }
  997. return false;
  998. }
  999. bool Scanner::scanDocumentIndicator(bool IsStart) {
  1000. unrollIndent(-1);
  1001. SimpleKeys.clear();
  1002. IsSimpleKeyAllowed = false;
  1003. Token T;
  1004. T.Kind = IsStart ? Token::TK_DocumentStart : Token::TK_DocumentEnd;
  1005. T.Range = StringRef(Current, 3);
  1006. skip(3);
  1007. TokenQueue.push_back(T);
  1008. return true;
  1009. }
  1010. bool Scanner::scanFlowCollectionStart(bool IsSequence) {
  1011. Token T;
  1012. T.Kind = IsSequence ? Token::TK_FlowSequenceStart
  1013. : Token::TK_FlowMappingStart;
  1014. T.Range = StringRef(Current, 1);
  1015. skip(1);
  1016. TokenQueue.push_back(T);
  1017. // [ and { may begin a simple key.
  1018. saveSimpleKeyCandidate(TokenQueue.back(), Column - 1, false);
  1019. // And may also be followed by a simple key.
  1020. IsSimpleKeyAllowed = true;
  1021. ++FlowLevel;
  1022. return true;
  1023. }
  1024. bool Scanner::scanFlowCollectionEnd(bool IsSequence) {
  1025. removeSimpleKeyCandidatesOnFlowLevel(FlowLevel);
  1026. IsSimpleKeyAllowed = false;
  1027. Token T;
  1028. T.Kind = IsSequence ? Token::TK_FlowSequenceEnd
  1029. : Token::TK_FlowMappingEnd;
  1030. T.Range = StringRef(Current, 1);
  1031. skip(1);
  1032. TokenQueue.push_back(T);
  1033. if (FlowLevel)
  1034. --FlowLevel;
  1035. return true;
  1036. }
  1037. bool Scanner::scanFlowEntry() {
  1038. removeSimpleKeyCandidatesOnFlowLevel(FlowLevel);
  1039. IsSimpleKeyAllowed = true;
  1040. Token T;
  1041. T.Kind = Token::TK_FlowEntry;
  1042. T.Range = StringRef(Current, 1);
  1043. skip(1);
  1044. TokenQueue.push_back(T);
  1045. return true;
  1046. }
  1047. bool Scanner::scanBlockEntry() {
  1048. rollIndent(Column, Token::TK_BlockSequenceStart, TokenQueue.end());
  1049. removeSimpleKeyCandidatesOnFlowLevel(FlowLevel);
  1050. IsSimpleKeyAllowed = true;
  1051. Token T;
  1052. T.Kind = Token::TK_BlockEntry;
  1053. T.Range = StringRef(Current, 1);
  1054. skip(1);
  1055. TokenQueue.push_back(T);
  1056. return true;
  1057. }
  1058. bool Scanner::scanKey() {
  1059. if (!FlowLevel)
  1060. rollIndent(Column, Token::TK_BlockMappingStart, TokenQueue.end());
  1061. removeSimpleKeyCandidatesOnFlowLevel(FlowLevel);
  1062. IsSimpleKeyAllowed = !FlowLevel;
  1063. Token T;
  1064. T.Kind = Token::TK_Key;
  1065. T.Range = StringRef(Current, 1);
  1066. skip(1);
  1067. TokenQueue.push_back(T);
  1068. return true;
  1069. }
  1070. bool Scanner::scanValue() {
  1071. // If the previous token could have been a simple key, insert the key token
  1072. // into the token queue.
  1073. if (!SimpleKeys.empty()) {
  1074. SimpleKey SK = SimpleKeys.pop_back_val();
  1075. Token T;
  1076. T.Kind = Token::TK_Key;
  1077. T.Range = SK.Tok->Range;
  1078. TokenQueueT::iterator i, e;
  1079. for (i = TokenQueue.begin(), e = TokenQueue.end(); i != e; ++i) {
  1080. if (i == SK.Tok)
  1081. break;
  1082. }
  1083. assert(i != e && "SimpleKey not in token queue!");
  1084. i = TokenQueue.insert(i, T);
  1085. // We may also need to add a Block-Mapping-Start token.
  1086. rollIndent(SK.Column, Token::TK_BlockMappingStart, i);
  1087. IsSimpleKeyAllowed = false;
  1088. } else {
  1089. if (!FlowLevel)
  1090. rollIndent(Column, Token::TK_BlockMappingStart, TokenQueue.end());
  1091. IsSimpleKeyAllowed = !FlowLevel;
  1092. }
  1093. Token T;
  1094. T.Kind = Token::TK_Value;
  1095. T.Range = StringRef(Current, 1);
  1096. skip(1);
  1097. TokenQueue.push_back(T);
  1098. return true;
  1099. }
  1100. // Forbidding inlining improves performance by roughly 20%.
  1101. // FIXME: Remove once llvm optimizes this to the faster version without hints.
  1102. LLVM_ATTRIBUTE_NOINLINE static bool
  1103. wasEscaped(StringRef::iterator First, StringRef::iterator Position);
  1104. // Returns whether a character at 'Position' was escaped with a leading '\'.
  1105. // 'First' specifies the position of the first character in the string.
  1106. static bool wasEscaped(StringRef::iterator First,
  1107. StringRef::iterator Position) {
  1108. assert(Position - 1 >= First);
  1109. StringRef::iterator I = Position - 1;
  1110. // We calculate the number of consecutive '\'s before the current position
  1111. // by iterating backwards through our string.
  1112. while (I >= First && *I == '\\') --I;
  1113. // (Position - 1 - I) now contains the number of '\'s before the current
  1114. // position. If it is odd, the character at 'Position' was escaped.
  1115. return (Position - 1 - I) % 2 == 1;
  1116. }
  1117. bool Scanner::scanFlowScalar(bool IsDoubleQuoted) {
  1118. StringRef::iterator Start = Current;
  1119. unsigned ColStart = Column;
  1120. if (IsDoubleQuoted) {
  1121. do {
  1122. ++Current;
  1123. while (Current != End && *Current != '"')
  1124. ++Current;
  1125. // Repeat until the previous character was not a '\' or was an escaped
  1126. // backslash.
  1127. } while ( Current != End
  1128. && *(Current - 1) == '\\'
  1129. && wasEscaped(Start + 1, Current));
  1130. } else {
  1131. skip(1);
  1132. while (true) {
  1133. // Skip a ' followed by another '.
  1134. if (Current + 1 < End && *Current == '\'' && *(Current + 1) == '\'') {
  1135. skip(2);
  1136. continue;
  1137. } else if (*Current == '\'')
  1138. break;
  1139. StringRef::iterator i = skip_nb_char(Current);
  1140. if (i == Current) {
  1141. i = skip_b_break(Current);
  1142. if (i == Current)
  1143. break;
  1144. Current = i;
  1145. Column = 0;
  1146. ++Line;
  1147. } else {
  1148. if (i == End)
  1149. break;
  1150. Current = i;
  1151. ++Column;
  1152. }
  1153. }
  1154. }
  1155. if (Current == End) {
  1156. setError("Expected quote at end of scalar", Current);
  1157. return false;
  1158. }
  1159. skip(1); // Skip ending quote.
  1160. Token T;
  1161. T.Kind = Token::TK_Scalar;
  1162. T.Range = StringRef(Start, Current - Start);
  1163. TokenQueue.push_back(T);
  1164. saveSimpleKeyCandidate(TokenQueue.back(), ColStart, false);
  1165. IsSimpleKeyAllowed = false;
  1166. return true;
  1167. }
  1168. bool Scanner::scanPlainScalar() {
  1169. StringRef::iterator Start = Current;
  1170. unsigned ColStart = Column;
  1171. unsigned LeadingBlanks = 0;
  1172. assert(Indent >= -1 && "Indent must be >= -1 !");
  1173. unsigned indent = static_cast<unsigned>(Indent + 1);
  1174. while (true) {
  1175. if (*Current == '#')
  1176. break;
  1177. while (!isBlankOrBreak(Current)) {
  1178. if ( FlowLevel && *Current == ':'
  1179. && !(isBlankOrBreak(Current + 1) || *(Current + 1) == ',')) {
  1180. setError("Found unexpected ':' while scanning a plain scalar", Current);
  1181. return false;
  1182. }
  1183. // Check for the end of the plain scalar.
  1184. if ( (*Current == ':' && isBlankOrBreak(Current + 1))
  1185. || ( FlowLevel
  1186. && (StringRef(Current, 1).find_first_of(",:?[]{}")
  1187. != StringRef::npos)))
  1188. break;
  1189. StringRef::iterator i = skip_nb_char(Current);
  1190. if (i == Current)
  1191. break;
  1192. Current = i;
  1193. ++Column;
  1194. }
  1195. // Are we at the end?
  1196. if (!isBlankOrBreak(Current))
  1197. break;
  1198. // Eat blanks.
  1199. StringRef::iterator Tmp = Current;
  1200. while (isBlankOrBreak(Tmp)) {
  1201. StringRef::iterator i = skip_s_white(Tmp);
  1202. if (i != Tmp) {
  1203. if (LeadingBlanks && (Column < indent) && *Tmp == '\t') {
  1204. setError("Found invalid tab character in indentation", Tmp);
  1205. return false;
  1206. }
  1207. Tmp = i;
  1208. ++Column;
  1209. } else {
  1210. i = skip_b_break(Tmp);
  1211. if (!LeadingBlanks)
  1212. LeadingBlanks = 1;
  1213. Tmp = i;
  1214. Column = 0;
  1215. ++Line;
  1216. }
  1217. }
  1218. if (!FlowLevel && Column < indent)
  1219. break;
  1220. Current = Tmp;
  1221. }
  1222. if (Start == Current) {
  1223. setError("Got empty plain scalar", Start);
  1224. return false;
  1225. }
  1226. Token T;
  1227. T.Kind = Token::TK_Scalar;
  1228. T.Range = StringRef(Start, Current - Start);
  1229. TokenQueue.push_back(T);
  1230. // Plain scalars can be simple keys.
  1231. saveSimpleKeyCandidate(TokenQueue.back(), ColStart, false);
  1232. IsSimpleKeyAllowed = false;
  1233. return true;
  1234. }
  1235. bool Scanner::scanAliasOrAnchor(bool IsAlias) {
  1236. StringRef::iterator Start = Current;
  1237. unsigned ColStart = Column;
  1238. skip(1);
  1239. while(true) {
  1240. if ( *Current == '[' || *Current == ']'
  1241. || *Current == '{' || *Current == '}'
  1242. || *Current == ','
  1243. || *Current == ':')
  1244. break;
  1245. StringRef::iterator i = skip_ns_char(Current);
  1246. if (i == Current)
  1247. break;
  1248. Current = i;
  1249. ++Column;
  1250. }
  1251. if (Start == Current) {
  1252. setError("Got empty alias or anchor", Start);
  1253. return false;
  1254. }
  1255. Token T;
  1256. T.Kind = IsAlias ? Token::TK_Alias : Token::TK_Anchor;
  1257. T.Range = StringRef(Start, Current - Start);
  1258. TokenQueue.push_back(T);
  1259. // Alias and anchors can be simple keys.
  1260. saveSimpleKeyCandidate(TokenQueue.back(), ColStart, false);
  1261. IsSimpleKeyAllowed = false;
  1262. return true;
  1263. }
  1264. char Scanner::scanBlockChompingIndicator() {
  1265. char Indicator = ' ';
  1266. if (Current != End && (*Current == '+' || *Current == '-')) {
  1267. Indicator = *Current;
  1268. skip(1);
  1269. }
  1270. return Indicator;
  1271. }
  1272. /// Get the number of line breaks after chomping.
  1273. ///
  1274. /// Return the number of trailing line breaks to emit, depending on
  1275. /// \p ChompingIndicator.
  1276. static unsigned getChompedLineBreaks(char ChompingIndicator,
  1277. unsigned LineBreaks, StringRef Str) {
  1278. if (ChompingIndicator == '-') // Strip all line breaks.
  1279. return 0;
  1280. if (ChompingIndicator == '+') // Keep all line breaks.
  1281. return LineBreaks;
  1282. // Clip trailing lines.
  1283. return Str.empty() ? 0 : 1;
  1284. }
  1285. unsigned Scanner::scanBlockIndentationIndicator() {
  1286. unsigned Indent = 0;
  1287. if (Current != End && (*Current >= '1' && *Current <= '9')) {
  1288. Indent = unsigned(*Current - '0');
  1289. skip(1);
  1290. }
  1291. return Indent;
  1292. }
  1293. bool Scanner::scanBlockScalarHeader(char &ChompingIndicator,
  1294. unsigned &IndentIndicator, bool &IsDone) {
  1295. auto Start = Current;
  1296. ChompingIndicator = scanBlockChompingIndicator();
  1297. IndentIndicator = scanBlockIndentationIndicator();
  1298. // Check for the chomping indicator once again.
  1299. if (ChompingIndicator == ' ')
  1300. ChompingIndicator = scanBlockChompingIndicator();
  1301. Current = skip_while(&Scanner::skip_s_white, Current);
  1302. skipComment();
  1303. if (Current == End) { // EOF, we have an empty scalar.
  1304. Token T;
  1305. T.Kind = Token::TK_BlockScalar;
  1306. T.Range = StringRef(Start, Current - Start);
  1307. TokenQueue.push_back(T);
  1308. IsDone = true;
  1309. return true;
  1310. }
  1311. if (!consumeLineBreakIfPresent()) {
  1312. setError("Expected a line break after block scalar header", Current);
  1313. return false;
  1314. }
  1315. return true;
  1316. }
  1317. bool Scanner::findBlockScalarIndent(unsigned &BlockIndent,
  1318. unsigned BlockExitIndent,
  1319. unsigned &LineBreaks, bool &IsDone) {
  1320. unsigned MaxAllSpaceLineCharacters = 0;
  1321. StringRef::iterator LongestAllSpaceLine;
  1322. while (true) {
  1323. advanceWhile(&Scanner::skip_s_space);
  1324. if (skip_nb_char(Current) != Current) {
  1325. // This line isn't empty, so try and find the indentation.
  1326. if (Column <= BlockExitIndent) { // End of the block literal.
  1327. IsDone = true;
  1328. return true;
  1329. }
  1330. // We found the block's indentation.
  1331. BlockIndent = Column;
  1332. if (MaxAllSpaceLineCharacters > BlockIndent) {
  1333. setError(
  1334. "Leading all-spaces line must be smaller than the block indent",
  1335. LongestAllSpaceLine);
  1336. return false;
  1337. }
  1338. return true;
  1339. }
  1340. if (skip_b_break(Current) != Current &&
  1341. Column > MaxAllSpaceLineCharacters) {
  1342. // Record the longest all-space line in case it's longer than the
  1343. // discovered block indent.
  1344. MaxAllSpaceLineCharacters = Column;
  1345. LongestAllSpaceLine = Current;
  1346. }
  1347. // Check for EOF.
  1348. if (Current == End) {
  1349. IsDone = true;
  1350. return true;
  1351. }
  1352. if (!consumeLineBreakIfPresent()) {
  1353. IsDone = true;
  1354. return true;
  1355. }
  1356. ++LineBreaks;
  1357. }
  1358. return true;
  1359. }
  1360. bool Scanner::scanBlockScalarIndent(unsigned BlockIndent,
  1361. unsigned BlockExitIndent, bool &IsDone) {
  1362. // Skip the indentation.
  1363. while (Column < BlockIndent) {
  1364. auto I = skip_s_space(Current);
  1365. if (I == Current)
  1366. break;
  1367. Current = I;
  1368. ++Column;
  1369. }
  1370. if (skip_nb_char(Current) == Current)
  1371. return true;
  1372. if (Column <= BlockExitIndent) { // End of the block literal.
  1373. IsDone = true;
  1374. return true;
  1375. }
  1376. if (Column < BlockIndent) {
  1377. if (Current != End && *Current == '#') { // Trailing comment.
  1378. IsDone = true;
  1379. return true;
  1380. }
  1381. setError("A text line is less indented than the block scalar", Current);
  1382. return false;
  1383. }
  1384. return true; // A normal text line.
  1385. }
  1386. bool Scanner::scanBlockScalar(bool IsLiteral) {
  1387. // Eat '|' or '>'
  1388. assert(*Current == '|' || *Current == '>');
  1389. skip(1);
  1390. char ChompingIndicator;
  1391. unsigned BlockIndent;
  1392. bool IsDone = false;
  1393. if (!scanBlockScalarHeader(ChompingIndicator, BlockIndent, IsDone))
  1394. return false;
  1395. if (IsDone)
  1396. return true;
  1397. auto Start = Current;
  1398. unsigned BlockExitIndent = Indent < 0 ? 0 : (unsigned)Indent;
  1399. unsigned LineBreaks = 0;
  1400. if (BlockIndent == 0) {
  1401. if (!findBlockScalarIndent(BlockIndent, BlockExitIndent, LineBreaks,
  1402. IsDone))
  1403. return false;
  1404. }
  1405. // Scan the block's scalars body.
  1406. SmallString<256> Str;
  1407. while (!IsDone) {
  1408. if (!scanBlockScalarIndent(BlockIndent, BlockExitIndent, IsDone))
  1409. return false;
  1410. if (IsDone)
  1411. break;
  1412. // Parse the current line.
  1413. auto LineStart = Current;
  1414. advanceWhile(&Scanner::skip_nb_char);
  1415. if (LineStart != Current) {
  1416. Str.append(LineBreaks, '\n');
  1417. Str.append(StringRef(LineStart, Current - LineStart));
  1418. LineBreaks = 0;
  1419. }
  1420. // Check for EOF.
  1421. if (Current == End)
  1422. break;
  1423. if (!consumeLineBreakIfPresent())
  1424. break;
  1425. ++LineBreaks;
  1426. }
  1427. if (Current == End && !LineBreaks)
  1428. // Ensure that there is at least one line break before the end of file.
  1429. LineBreaks = 1;
  1430. Str.append(getChompedLineBreaks(ChompingIndicator, LineBreaks, Str), '\n');
  1431. // New lines may start a simple key.
  1432. if (!FlowLevel)
  1433. IsSimpleKeyAllowed = true;
  1434. Token T;
  1435. T.Kind = Token::TK_BlockScalar;
  1436. T.Range = StringRef(Start, Current - Start);
  1437. T.Value = Str.str().str();
  1438. TokenQueue.push_back(T);
  1439. return true;
  1440. }
  1441. bool Scanner::scanTag() {
  1442. StringRef::iterator Start = Current;
  1443. unsigned ColStart = Column;
  1444. skip(1); // Eat !.
  1445. if (Current == End || isBlankOrBreak(Current)); // An empty tag.
  1446. else if (*Current == '<') {
  1447. skip(1);
  1448. scan_ns_uri_char();
  1449. if (!consume('>'))
  1450. return false;
  1451. } else {
  1452. // FIXME: Actually parse the c-ns-shorthand-tag rule.
  1453. Current = skip_while(&Scanner::skip_ns_char, Current);
  1454. }
  1455. Token T;
  1456. T.Kind = Token::TK_Tag;
  1457. T.Range = StringRef(Start, Current - Start);
  1458. TokenQueue.push_back(T);
  1459. // Tags can be simple keys.
  1460. saveSimpleKeyCandidate(TokenQueue.back(), ColStart, false);
  1461. IsSimpleKeyAllowed = false;
  1462. return true;
  1463. }
  1464. bool Scanner::fetchMoreTokens() {
  1465. if (IsStartOfStream)
  1466. return scanStreamStart();
  1467. scanToNextToken();
  1468. if (Current == End)
  1469. return scanStreamEnd();
  1470. removeStaleSimpleKeyCandidates();
  1471. unrollIndent(Column);
  1472. if (Column == 0 && *Current == '%')
  1473. return scanDirective();
  1474. if (Column == 0 && Current + 4 <= End
  1475. && *Current == '-'
  1476. && *(Current + 1) == '-'
  1477. && *(Current + 2) == '-'
  1478. && (Current + 3 == End || isBlankOrBreak(Current + 3)))
  1479. return scanDocumentIndicator(true);
  1480. if (Column == 0 && Current + 4 <= End
  1481. && *Current == '.'
  1482. && *(Current + 1) == '.'
  1483. && *(Current + 2) == '.'
  1484. && (Current + 3 == End || isBlankOrBreak(Current + 3)))
  1485. return scanDocumentIndicator(false);
  1486. if (*Current == '[')
  1487. return scanFlowCollectionStart(true);
  1488. if (*Current == '{')
  1489. return scanFlowCollectionStart(false);
  1490. if (*Current == ']')
  1491. return scanFlowCollectionEnd(true);
  1492. if (*Current == '}')
  1493. return scanFlowCollectionEnd(false);
  1494. if (*Current == ',')
  1495. return scanFlowEntry();
  1496. if (*Current == '-' && isBlankOrBreak(Current + 1))
  1497. return scanBlockEntry();
  1498. if (*Current == '?' && (FlowLevel || isBlankOrBreak(Current + 1)))
  1499. return scanKey();
  1500. if (*Current == ':' && (FlowLevel || isBlankOrBreak(Current + 1)))
  1501. return scanValue();
  1502. if (*Current == '*')
  1503. return scanAliasOrAnchor(true);
  1504. if (*Current == '&')
  1505. return scanAliasOrAnchor(false);
  1506. if (*Current == '!')
  1507. return scanTag();
  1508. if (*Current == '|' && !FlowLevel)
  1509. return scanBlockScalar(true);
  1510. if (*Current == '>' && !FlowLevel)
  1511. return scanBlockScalar(false);
  1512. if (*Current == '\'')
  1513. return scanFlowScalar(false);
  1514. if (*Current == '"')
  1515. return scanFlowScalar(true);
  1516. // Get a plain scalar.
  1517. StringRef FirstChar(Current, 1);
  1518. if (!(isBlankOrBreak(Current)
  1519. || FirstChar.find_first_of("-?:,[]{}#&*!|>'\"%@`") != StringRef::npos)
  1520. || (*Current == '-' && !isBlankOrBreak(Current + 1))
  1521. || (!FlowLevel && (*Current == '?' || *Current == ':')
  1522. && isBlankOrBreak(Current + 1))
  1523. || (!FlowLevel && *Current == ':'
  1524. && Current + 2 < End
  1525. && *(Current + 1) == ':'
  1526. && !isBlankOrBreak(Current + 2)))
  1527. return scanPlainScalar();
  1528. setError("Unrecognized character while tokenizing.");
  1529. return false;
  1530. }
  1531. Stream::Stream(StringRef Input, SourceMgr &SM, bool ShowColors)
  1532. : scanner(new Scanner(Input, SM, ShowColors)), CurrentDoc() {}
  1533. Stream::Stream(MemoryBufferRef InputBuffer, SourceMgr &SM, bool ShowColors)
  1534. : scanner(new Scanner(InputBuffer, SM, ShowColors)), CurrentDoc() {}
  1535. Stream::~Stream() {}
  1536. bool Stream::failed() { return scanner->failed(); }
  1537. void Stream::printError(Node *N, const Twine &Msg) {
  1538. scanner->printError( N->getSourceRange().Start
  1539. , SourceMgr::DK_Error
  1540. , Msg
  1541. , N->getSourceRange());
  1542. }
  1543. document_iterator Stream::begin() {
  1544. if (CurrentDoc)
  1545. report_fatal_error("Can only iterate over the stream once");
  1546. // Skip Stream-Start.
  1547. scanner->getNext();
  1548. CurrentDoc.reset(new Document(*this));
  1549. return document_iterator(CurrentDoc);
  1550. }
  1551. document_iterator Stream::end() {
  1552. return document_iterator();
  1553. }
  1554. void Stream::skip() {
  1555. for (document_iterator i = begin(), e = end(); i != e; ++i)
  1556. i->skip();
  1557. }
  1558. Node::Node(unsigned int Type, std::unique_ptr<Document> &D, StringRef A,
  1559. StringRef T)
  1560. : Doc(D), TypeID(Type), Anchor(A), Tag(T) {
  1561. SMLoc Start = SMLoc::getFromPointer(peekNext().Range.begin());
  1562. SourceRange = SMRange(Start, Start);
  1563. }
  1564. std::string Node::getVerbatimTag() const {
  1565. StringRef Raw = getRawTag();
  1566. if (!Raw.empty() && Raw != "!") {
  1567. std::string Ret;
  1568. if (Raw.find_last_of('!') == 0) {
  1569. Ret = Doc->getTagMap().find("!")->second;
  1570. Ret += Raw.substr(1);
  1571. return Ret;
  1572. } else if (Raw.startswith("!!")) {
  1573. Ret = Doc->getTagMap().find("!!")->second;
  1574. Ret += Raw.substr(2);
  1575. return Ret;
  1576. } else {
  1577. StringRef TagHandle = Raw.substr(0, Raw.find_last_of('!') + 1);
  1578. std::map<StringRef, StringRef>::const_iterator It =
  1579. Doc->getTagMap().find(TagHandle);
  1580. if (It != Doc->getTagMap().end())
  1581. Ret = It->second;
  1582. else {
  1583. Token T;
  1584. T.Kind = Token::TK_Tag;
  1585. T.Range = TagHandle;
  1586. setError(Twine("Unknown tag handle ") + TagHandle, T);
  1587. }
  1588. Ret += Raw.substr(Raw.find_last_of('!') + 1);
  1589. return Ret;
  1590. }
  1591. }
  1592. switch (getType()) {
  1593. case NK_Null:
  1594. return "tag:yaml.org,2002:null";
  1595. case NK_Scalar:
  1596. case NK_BlockScalar:
  1597. // TODO: Tag resolution.
  1598. return "tag:yaml.org,2002:str";
  1599. case NK_Mapping:
  1600. return "tag:yaml.org,2002:map";
  1601. case NK_Sequence:
  1602. return "tag:yaml.org,2002:seq";
  1603. }
  1604. return "";
  1605. }
  1606. Token &Node::peekNext() {
  1607. return Doc->peekNext();
  1608. }
  1609. Token Node::getNext() {
  1610. return Doc->getNext();
  1611. }
  1612. Node *Node::parseBlockNode() {
  1613. return Doc->parseBlockNode();
  1614. }
  1615. BumpPtrAllocator &Node::getAllocator() {
  1616. return Doc->NodeAllocator;
  1617. }
  1618. void Node::setError(const Twine &Msg, Token &Tok) const {
  1619. Doc->setError(Msg, Tok);
  1620. }
  1621. bool Node::failed() const {
  1622. return Doc->failed();
  1623. }
  1624. StringRef ScalarNode::getValue(SmallVectorImpl<char> &Storage) const {
  1625. // TODO: Handle newlines properly. We need to remove leading whitespace.
  1626. if (Value[0] == '"') { // Double quoted.
  1627. // Pull off the leading and trailing "s.
  1628. StringRef UnquotedValue = Value.substr(1, Value.size() - 2);
  1629. // Search for characters that would require unescaping the value.
  1630. StringRef::size_type i = UnquotedValue.find_first_of("\\\r\n");
  1631. if (i != StringRef::npos)
  1632. return unescapeDoubleQuoted(UnquotedValue, i, Storage);
  1633. return UnquotedValue;
  1634. } else if (Value[0] == '\'') { // Single quoted.
  1635. // Pull off the leading and trailing 's.
  1636. StringRef UnquotedValue = Value.substr(1, Value.size() - 2);
  1637. StringRef::size_type i = UnquotedValue.find('\'');
  1638. if (i != StringRef::npos) {
  1639. // We're going to need Storage.
  1640. Storage.clear();
  1641. Storage.reserve(UnquotedValue.size());
  1642. for (; i != StringRef::npos; i = UnquotedValue.find('\'')) {
  1643. StringRef Valid(UnquotedValue.begin(), i);
  1644. Storage.insert(Storage.end(), Valid.begin(), Valid.end());
  1645. Storage.push_back('\'');
  1646. UnquotedValue = UnquotedValue.substr(i + 2);
  1647. }
  1648. Storage.insert(Storage.end(), UnquotedValue.begin(), UnquotedValue.end());
  1649. return StringRef(Storage.begin(), Storage.size());
  1650. }
  1651. return UnquotedValue;
  1652. }
  1653. // Plain or block.
  1654. return Value.rtrim(" ");
  1655. }
  1656. StringRef ScalarNode::unescapeDoubleQuoted( StringRef UnquotedValue
  1657. , StringRef::size_type i
  1658. , SmallVectorImpl<char> &Storage)
  1659. const {
  1660. // Use Storage to build proper value.
  1661. Storage.clear();
  1662. Storage.reserve(UnquotedValue.size());
  1663. for (; i != StringRef::npos; i = UnquotedValue.find_first_of("\\\r\n")) {
  1664. // Insert all previous chars into Storage.
  1665. StringRef Valid(UnquotedValue.begin(), i);
  1666. Storage.insert(Storage.end(), Valid.begin(), Valid.end());
  1667. // Chop off inserted chars.
  1668. UnquotedValue = UnquotedValue.substr(i);
  1669. assert(!UnquotedValue.empty() && "Can't be empty!");
  1670. // Parse escape or line break.
  1671. switch (UnquotedValue[0]) {
  1672. case '\r':
  1673. case '\n':
  1674. Storage.push_back('\n');
  1675. if ( UnquotedValue.size() > 1
  1676. && (UnquotedValue[1] == '\r' || UnquotedValue[1] == '\n'))
  1677. UnquotedValue = UnquotedValue.substr(1);
  1678. UnquotedValue = UnquotedValue.substr(1);
  1679. break;
  1680. default:
  1681. if (UnquotedValue.size() == 1)
  1682. // TODO: Report error.
  1683. break;
  1684. UnquotedValue = UnquotedValue.substr(1);
  1685. switch (UnquotedValue[0]) {
  1686. default: {
  1687. Token T;
  1688. T.Range = StringRef(UnquotedValue.begin(), 1);
  1689. setError("Unrecognized escape code!", T);
  1690. return "";
  1691. }
  1692. case '\r':
  1693. case '\n':
  1694. // Remove the new line.
  1695. if ( UnquotedValue.size() > 1
  1696. && (UnquotedValue[1] == '\r' || UnquotedValue[1] == '\n'))
  1697. UnquotedValue = UnquotedValue.substr(1);
  1698. // If this was just a single byte newline, it will get skipped
  1699. // below.
  1700. break;
  1701. case '0':
  1702. Storage.push_back(0x00);
  1703. break;
  1704. case 'a':
  1705. Storage.push_back(0x07);
  1706. break;
  1707. case 'b':
  1708. Storage.push_back(0x08);
  1709. break;
  1710. case 't':
  1711. case 0x09:
  1712. Storage.push_back(0x09);
  1713. break;
  1714. case 'n':
  1715. Storage.push_back(0x0A);
  1716. break;
  1717. case 'v':
  1718. Storage.push_back(0x0B);
  1719. break;
  1720. case 'f':
  1721. Storage.push_back(0x0C);
  1722. break;
  1723. case 'r':
  1724. Storage.push_back(0x0D);
  1725. break;
  1726. case 'e':
  1727. Storage.push_back(0x1B);
  1728. break;
  1729. case ' ':
  1730. Storage.push_back(0x20);
  1731. break;
  1732. case '"':
  1733. Storage.push_back(0x22);
  1734. break;
  1735. case '/':
  1736. Storage.push_back(0x2F);
  1737. break;
  1738. case '\\':
  1739. Storage.push_back(0x5C);
  1740. break;
  1741. case 'N':
  1742. encodeUTF8(0x85, Storage);
  1743. break;
  1744. case '_':
  1745. encodeUTF8(0xA0, Storage);
  1746. break;
  1747. case 'L':
  1748. encodeUTF8(0x2028, Storage);
  1749. break;
  1750. case 'P':
  1751. encodeUTF8(0x2029, Storage);
  1752. break;
  1753. case 'x': {
  1754. if (UnquotedValue.size() < 3)
  1755. // TODO: Report error.
  1756. break;
  1757. unsigned int UnicodeScalarValue;
  1758. if (UnquotedValue.substr(1, 2).getAsInteger(16, UnicodeScalarValue))
  1759. // TODO: Report error.
  1760. UnicodeScalarValue = 0xFFFD;
  1761. encodeUTF8(UnicodeScalarValue, Storage);
  1762. UnquotedValue = UnquotedValue.substr(2);
  1763. break;
  1764. }
  1765. case 'u': {
  1766. if (UnquotedValue.size() < 5)
  1767. // TODO: Report error.
  1768. break;
  1769. unsigned int UnicodeScalarValue;
  1770. if (UnquotedValue.substr(1, 4).getAsInteger(16, UnicodeScalarValue))
  1771. // TODO: Report error.
  1772. UnicodeScalarValue = 0xFFFD;
  1773. encodeUTF8(UnicodeScalarValue, Storage);
  1774. UnquotedValue = UnquotedValue.substr(4);
  1775. break;
  1776. }
  1777. case 'U': {
  1778. if (UnquotedValue.size() < 9)
  1779. // TODO: Report error.
  1780. break;
  1781. unsigned int UnicodeScalarValue;
  1782. if (UnquotedValue.substr(1, 8).getAsInteger(16, UnicodeScalarValue))
  1783. // TODO: Report error.
  1784. UnicodeScalarValue = 0xFFFD;
  1785. encodeUTF8(UnicodeScalarValue, Storage);
  1786. UnquotedValue = UnquotedValue.substr(8);
  1787. break;
  1788. }
  1789. }
  1790. UnquotedValue = UnquotedValue.substr(1);
  1791. }
  1792. }
  1793. Storage.insert(Storage.end(), UnquotedValue.begin(), UnquotedValue.end());
  1794. return StringRef(Storage.begin(), Storage.size());
  1795. }
  1796. Node *KeyValueNode::getKey() {
  1797. if (Key)
  1798. return Key;
  1799. // Handle implicit null keys.
  1800. {
  1801. Token &t = peekNext();
  1802. if ( t.Kind == Token::TK_BlockEnd
  1803. || t.Kind == Token::TK_Value
  1804. || t.Kind == Token::TK_Error) {
  1805. return Key = new (getAllocator()) NullNode(Doc);
  1806. }
  1807. if (t.Kind == Token::TK_Key)
  1808. getNext(); // skip TK_Key.
  1809. }
  1810. // Handle explicit null keys.
  1811. Token &t = peekNext();
  1812. if (t.Kind == Token::TK_BlockEnd || t.Kind == Token::TK_Value) {
  1813. return Key = new (getAllocator()) NullNode(Doc);
  1814. }
  1815. // We've got a normal key.
  1816. return Key = parseBlockNode();
  1817. }
  1818. Node *KeyValueNode::getValue() {
  1819. if (Value)
  1820. return Value;
  1821. getKey()->skip();
  1822. if (failed())
  1823. return Value = new (getAllocator()) NullNode(Doc);
  1824. // Handle implicit null values.
  1825. {
  1826. Token &t = peekNext();
  1827. if ( t.Kind == Token::TK_BlockEnd
  1828. || t.Kind == Token::TK_FlowMappingEnd
  1829. || t.Kind == Token::TK_Key
  1830. || t.Kind == Token::TK_FlowEntry
  1831. || t.Kind == Token::TK_Error) {
  1832. return Value = new (getAllocator()) NullNode(Doc);
  1833. }
  1834. if (t.Kind != Token::TK_Value) {
  1835. setError("Unexpected token in Key Value.", t);
  1836. return Value = new (getAllocator()) NullNode(Doc);
  1837. }
  1838. getNext(); // skip TK_Value.
  1839. }
  1840. // Handle explicit null values.
  1841. Token &t = peekNext();
  1842. if (t.Kind == Token::TK_BlockEnd || t.Kind == Token::TK_Key) {
  1843. return Value = new (getAllocator()) NullNode(Doc);
  1844. }
  1845. // We got a normal value.
  1846. return Value = parseBlockNode();
  1847. }
  1848. void MappingNode::increment() {
  1849. if (failed()) {
  1850. IsAtEnd = true;
  1851. CurrentEntry = nullptr;
  1852. return;
  1853. }
  1854. if (CurrentEntry) {
  1855. CurrentEntry->skip();
  1856. if (Type == MT_Inline) {
  1857. IsAtEnd = true;
  1858. CurrentEntry = nullptr;
  1859. return;
  1860. }
  1861. }
  1862. Token T = peekNext();
  1863. if (T.Kind == Token::TK_Key || T.Kind == Token::TK_Scalar) {
  1864. // KeyValueNode eats the TK_Key. That way it can detect null keys.
  1865. CurrentEntry = new (getAllocator()) KeyValueNode(Doc);
  1866. } else if (Type == MT_Block) {
  1867. switch (T.Kind) {
  1868. case Token::TK_BlockEnd:
  1869. getNext();
  1870. IsAtEnd = true;
  1871. CurrentEntry = nullptr;
  1872. break;
  1873. default:
  1874. setError("Unexpected token. Expected Key or Block End", T);
  1875. case Token::TK_Error:
  1876. IsAtEnd = true;
  1877. CurrentEntry = nullptr;
  1878. }
  1879. } else {
  1880. switch (T.Kind) {
  1881. case Token::TK_FlowEntry:
  1882. // Eat the flow entry and recurse.
  1883. getNext();
  1884. return increment();
  1885. case Token::TK_FlowMappingEnd:
  1886. getNext();
  1887. case Token::TK_Error:
  1888. // Set this to end iterator.
  1889. IsAtEnd = true;
  1890. CurrentEntry = nullptr;
  1891. break;
  1892. default:
  1893. setError( "Unexpected token. Expected Key, Flow Entry, or Flow "
  1894. "Mapping End."
  1895. , T);
  1896. IsAtEnd = true;
  1897. CurrentEntry = nullptr;
  1898. }
  1899. }
  1900. }
  1901. void SequenceNode::increment() {
  1902. if (failed()) {
  1903. IsAtEnd = true;
  1904. CurrentEntry = nullptr;
  1905. return;
  1906. }
  1907. if (CurrentEntry)
  1908. CurrentEntry->skip();
  1909. Token T = peekNext();
  1910. if (SeqType == ST_Block) {
  1911. switch (T.Kind) {
  1912. case Token::TK_BlockEntry:
  1913. getNext();
  1914. CurrentEntry = parseBlockNode();
  1915. if (!CurrentEntry) { // An error occurred.
  1916. IsAtEnd = true;
  1917. CurrentEntry = nullptr;
  1918. }
  1919. break;
  1920. case Token::TK_BlockEnd:
  1921. getNext();
  1922. IsAtEnd = true;
  1923. CurrentEntry = nullptr;
  1924. break;
  1925. default:
  1926. setError( "Unexpected token. Expected Block Entry or Block End."
  1927. , T);
  1928. case Token::TK_Error:
  1929. IsAtEnd = true;
  1930. CurrentEntry = nullptr;
  1931. }
  1932. } else if (SeqType == ST_Indentless) {
  1933. switch (T.Kind) {
  1934. case Token::TK_BlockEntry:
  1935. getNext();
  1936. CurrentEntry = parseBlockNode();
  1937. if (!CurrentEntry) { // An error occurred.
  1938. IsAtEnd = true;
  1939. CurrentEntry = nullptr;
  1940. }
  1941. break;
  1942. default:
  1943. case Token::TK_Error:
  1944. IsAtEnd = true;
  1945. CurrentEntry = nullptr;
  1946. }
  1947. } else if (SeqType == ST_Flow) {
  1948. switch (T.Kind) {
  1949. case Token::TK_FlowEntry:
  1950. // Eat the flow entry and recurse.
  1951. getNext();
  1952. WasPreviousTokenFlowEntry = true;
  1953. return increment();
  1954. case Token::TK_FlowSequenceEnd:
  1955. getNext();
  1956. case Token::TK_Error:
  1957. // Set this to end iterator.
  1958. IsAtEnd = true;
  1959. CurrentEntry = nullptr;
  1960. break;
  1961. case Token::TK_StreamEnd:
  1962. case Token::TK_DocumentEnd:
  1963. case Token::TK_DocumentStart:
  1964. setError("Could not find closing ]!", T);
  1965. // Set this to end iterator.
  1966. IsAtEnd = true;
  1967. CurrentEntry = nullptr;
  1968. break;
  1969. default:
  1970. if (!WasPreviousTokenFlowEntry) {
  1971. setError("Expected , between entries!", T);
  1972. IsAtEnd = true;
  1973. CurrentEntry = nullptr;
  1974. break;
  1975. }
  1976. // Otherwise it must be a flow entry.
  1977. CurrentEntry = parseBlockNode();
  1978. if (!CurrentEntry) {
  1979. IsAtEnd = true;
  1980. }
  1981. WasPreviousTokenFlowEntry = false;
  1982. break;
  1983. }
  1984. }
  1985. }
  1986. Document::Document(Stream &S) : stream(S), Root(nullptr) {
  1987. // Tag maps starts with two default mappings.
  1988. TagMap["!"] = "!";
  1989. TagMap["!!"] = "tag:yaml.org,2002:";
  1990. if (parseDirectives())
  1991. expectToken(Token::TK_DocumentStart);
  1992. Token &T = peekNext();
  1993. if (T.Kind == Token::TK_DocumentStart)
  1994. getNext();
  1995. }
  1996. bool Document::skip() {
  1997. if (stream.scanner->failed())
  1998. return false;
  1999. if (!Root)
  2000. getRoot();
  2001. Root->skip();
  2002. Token &T = peekNext();
  2003. if (T.Kind == Token::TK_StreamEnd)
  2004. return false;
  2005. if (T.Kind == Token::TK_DocumentEnd) {
  2006. getNext();
  2007. return skip();
  2008. }
  2009. return true;
  2010. }
  2011. Token &Document::peekNext() {
  2012. return stream.scanner->peekNext();
  2013. }
  2014. Token Document::getNext() {
  2015. return stream.scanner->getNext();
  2016. }
  2017. void Document::setError(const Twine &Message, Token &Location) const {
  2018. stream.scanner->setError(Message, Location.Range.begin());
  2019. }
  2020. bool Document::failed() const {
  2021. return stream.scanner->failed();
  2022. }
  2023. Node *Document::parseBlockNode() {
  2024. Token T = peekNext();
  2025. // Handle properties.
  2026. Token AnchorInfo;
  2027. Token TagInfo;
  2028. parse_property:
  2029. switch (T.Kind) {
  2030. case Token::TK_Alias:
  2031. getNext();
  2032. return new (NodeAllocator) AliasNode(stream.CurrentDoc, T.Range.substr(1));
  2033. case Token::TK_Anchor:
  2034. if (AnchorInfo.Kind == Token::TK_Anchor) {
  2035. setError("Already encountered an anchor for this node!", T);
  2036. return nullptr;
  2037. }
  2038. AnchorInfo = getNext(); // Consume TK_Anchor.
  2039. T = peekNext();
  2040. goto parse_property;
  2041. case Token::TK_Tag:
  2042. if (TagInfo.Kind == Token::TK_Tag) {
  2043. setError("Already encountered a tag for this node!", T);
  2044. return nullptr;
  2045. }
  2046. TagInfo = getNext(); // Consume TK_Tag.
  2047. T = peekNext();
  2048. goto parse_property;
  2049. default:
  2050. break;
  2051. }
  2052. switch (T.Kind) {
  2053. case Token::TK_BlockEntry:
  2054. // We got an unindented BlockEntry sequence. This is not terminated with
  2055. // a BlockEnd.
  2056. // Don't eat the TK_BlockEntry, SequenceNode needs it.
  2057. return new (NodeAllocator) SequenceNode( stream.CurrentDoc
  2058. , AnchorInfo.Range.substr(1)
  2059. , TagInfo.Range
  2060. , SequenceNode::ST_Indentless);
  2061. case Token::TK_BlockSequenceStart:
  2062. getNext();
  2063. return new (NodeAllocator)
  2064. SequenceNode( stream.CurrentDoc
  2065. , AnchorInfo.Range.substr(1)
  2066. , TagInfo.Range
  2067. , SequenceNode::ST_Block);
  2068. case Token::TK_BlockMappingStart:
  2069. getNext();
  2070. return new (NodeAllocator)
  2071. MappingNode( stream.CurrentDoc
  2072. , AnchorInfo.Range.substr(1)
  2073. , TagInfo.Range
  2074. , MappingNode::MT_Block);
  2075. case Token::TK_FlowSequenceStart:
  2076. getNext();
  2077. return new (NodeAllocator)
  2078. SequenceNode( stream.CurrentDoc
  2079. , AnchorInfo.Range.substr(1)
  2080. , TagInfo.Range
  2081. , SequenceNode::ST_Flow);
  2082. case Token::TK_FlowMappingStart:
  2083. getNext();
  2084. return new (NodeAllocator)
  2085. MappingNode( stream.CurrentDoc
  2086. , AnchorInfo.Range.substr(1)
  2087. , TagInfo.Range
  2088. , MappingNode::MT_Flow);
  2089. case Token::TK_Scalar:
  2090. getNext();
  2091. return new (NodeAllocator)
  2092. ScalarNode( stream.CurrentDoc
  2093. , AnchorInfo.Range.substr(1)
  2094. , TagInfo.Range
  2095. , T.Range);
  2096. case Token::TK_BlockScalar: {
  2097. getNext();
  2098. StringRef NullTerminatedStr(T.Value.c_str(), T.Value.length() + 1);
  2099. StringRef StrCopy = NullTerminatedStr.copy(NodeAllocator).drop_back();
  2100. return new (NodeAllocator)
  2101. BlockScalarNode(stream.CurrentDoc, AnchorInfo.Range.substr(1),
  2102. TagInfo.Range, StrCopy, T.Range);
  2103. }
  2104. case Token::TK_Key:
  2105. // Don't eat the TK_Key, KeyValueNode expects it.
  2106. return new (NodeAllocator)
  2107. MappingNode( stream.CurrentDoc
  2108. , AnchorInfo.Range.substr(1)
  2109. , TagInfo.Range
  2110. , MappingNode::MT_Inline);
  2111. case Token::TK_DocumentStart:
  2112. case Token::TK_DocumentEnd:
  2113. case Token::TK_StreamEnd:
  2114. default:
  2115. // TODO: Properly handle tags. "[!!str ]" should resolve to !!str "", not
  2116. // !!null null.
  2117. return new (NodeAllocator) NullNode(stream.CurrentDoc);
  2118. case Token::TK_Error:
  2119. return nullptr;
  2120. }
  2121. llvm_unreachable("Control flow shouldn't reach here.");
  2122. return nullptr;
  2123. }
  2124. bool Document::parseDirectives() {
  2125. bool isDirective = false;
  2126. while (true) {
  2127. Token T = peekNext();
  2128. if (T.Kind == Token::TK_TagDirective) {
  2129. parseTAGDirective();
  2130. isDirective = true;
  2131. } else if (T.Kind == Token::TK_VersionDirective) {
  2132. parseYAMLDirective();
  2133. isDirective = true;
  2134. } else
  2135. break;
  2136. }
  2137. return isDirective;
  2138. }
  2139. void Document::parseYAMLDirective() {
  2140. getNext(); // Eat %YAML <version>
  2141. }
  2142. void Document::parseTAGDirective() {
  2143. Token Tag = getNext(); // %TAG <handle> <prefix>
  2144. StringRef T = Tag.Range;
  2145. // Strip %TAG
  2146. T = T.substr(T.find_first_of(" \t")).ltrim(" \t");
  2147. std::size_t HandleEnd = T.find_first_of(" \t");
  2148. StringRef TagHandle = T.substr(0, HandleEnd);
  2149. StringRef TagPrefix = T.substr(HandleEnd).ltrim(" \t");
  2150. TagMap[TagHandle] = TagPrefix;
  2151. }
  2152. bool Document::expectToken(int TK) {
  2153. Token T = getNext();
  2154. if (T.Kind != TK) {
  2155. setError("Unexpected token", T);
  2156. return false;
  2157. }
  2158. return true;
  2159. }