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