deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / src / scanner.h
1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Features shared by parsing and pre-parsing scanners.
6
7 #ifndef V8_SCANNER_H_
8 #define V8_SCANNER_H_
9
10 #include "src/allocation.h"
11 #include "src/base/logging.h"
12 #include "src/char-predicates.h"
13 #include "src/globals.h"
14 #include "src/hashmap.h"
15 #include "src/list.h"
16 #include "src/token.h"
17 #include "src/unicode-inl.h"
18 #include "src/unicode-decoder.h"
19 #include "src/utils.h"
20
21 namespace v8 {
22 namespace internal {
23
24
25 class AstRawString;
26 class AstValueFactory;
27 class ParserRecorder;
28
29
30 // Returns the value (0 .. 15) of a hexadecimal character c.
31 // If c is not a legal hexadecimal character, returns a value < 0.
32 inline int HexValue(uc32 c) {
33   c -= '0';
34   if (static_cast<unsigned>(c) <= 9) return c;
35   c = (c | 0x20) - ('a' - '0');  // detect 0x11..0x16 and 0x31..0x36.
36   if (static_cast<unsigned>(c) <= 5) return c + 10;
37   return -1;
38 }
39
40
41 // ---------------------------------------------------------------------
42 // Buffered stream of UTF-16 code units, using an internal UTF-16 buffer.
43 // A code unit is a 16 bit value representing either a 16 bit code point
44 // or one part of a surrogate pair that make a single 21 bit code point.
45
46 class Utf16CharacterStream {
47  public:
48   Utf16CharacterStream() : pos_(0) { }
49   virtual ~Utf16CharacterStream() { }
50
51   // Returns and advances past the next UTF-16 code unit in the input
52   // stream. If there are no more code units, it returns a negative
53   // value.
54   inline uc32 Advance() {
55     if (buffer_cursor_ < buffer_end_ || ReadBlock()) {
56       pos_++;
57       return static_cast<uc32>(*(buffer_cursor_++));
58     }
59     // Note: currently the following increment is necessary to avoid a
60     // parser problem! The scanner treats the final kEndOfInput as
61     // a code unit with a position, and does math relative to that
62     // position.
63     pos_++;
64
65     return kEndOfInput;
66   }
67
68   // Return the current position in the code unit stream.
69   // Starts at zero.
70   inline size_t pos() const { return pos_; }
71
72   // Skips forward past the next code_unit_count UTF-16 code units
73   // in the input, or until the end of input if that comes sooner.
74   // Returns the number of code units actually skipped. If less
75   // than code_unit_count,
76   inline size_t SeekForward(size_t code_unit_count) {
77     size_t buffered_chars = buffer_end_ - buffer_cursor_;
78     if (code_unit_count <= buffered_chars) {
79       buffer_cursor_ += code_unit_count;
80       pos_ += code_unit_count;
81       return code_unit_count;
82     }
83     return SlowSeekForward(code_unit_count);
84   }
85
86   // Pushes back the most recently read UTF-16 code unit (or negative
87   // value if at end of input), i.e., the value returned by the most recent
88   // call to Advance.
89   // Must not be used right after calling SeekForward.
90   virtual void PushBack(int32_t code_unit) = 0;
91
92  protected:
93   static const uc32 kEndOfInput = -1;
94
95   // Ensures that the buffer_cursor_ points to the code_unit at
96   // position pos_ of the input, if possible. If the position
97   // is at or after the end of the input, return false. If there
98   // are more code_units available, return true.
99   virtual bool ReadBlock() = 0;
100   virtual size_t SlowSeekForward(size_t code_unit_count) = 0;
101
102   const uint16_t* buffer_cursor_;
103   const uint16_t* buffer_end_;
104   size_t pos_;
105 };
106
107
108 // ---------------------------------------------------------------------
109 // Caching predicates used by scanners.
110
111 class UnicodeCache {
112  public:
113   UnicodeCache() {}
114   typedef unibrow::Utf8Decoder<512> Utf8Decoder;
115
116   StaticResource<Utf8Decoder>* utf8_decoder() {
117     return &utf8_decoder_;
118   }
119
120   bool IsIdentifierStart(unibrow::uchar c) { return kIsIdentifierStart.get(c); }
121   bool IsIdentifierPart(unibrow::uchar c) { return kIsIdentifierPart.get(c); }
122   bool IsLineTerminator(unibrow::uchar c) { return kIsLineTerminator.get(c); }
123   bool IsLineTerminatorSequence(unibrow::uchar c, unibrow::uchar next) {
124     if (!IsLineTerminator(c)) return false;
125     if (c == 0x000d && next == 0x000a) return false;  // CR with following LF.
126     return true;
127   }
128
129   bool IsWhiteSpace(unibrow::uchar c) { return kIsWhiteSpace.get(c); }
130   bool IsWhiteSpaceOrLineTerminator(unibrow::uchar c) {
131     return kIsWhiteSpaceOrLineTerminator.get(c);
132   }
133
134  private:
135   unibrow::Predicate<IdentifierStart, 128> kIsIdentifierStart;
136   unibrow::Predicate<IdentifierPart, 128> kIsIdentifierPart;
137   unibrow::Predicate<unibrow::LineTerminator, 128> kIsLineTerminator;
138   unibrow::Predicate<WhiteSpace, 128> kIsWhiteSpace;
139   unibrow::Predicate<WhiteSpaceOrLineTerminator, 128>
140       kIsWhiteSpaceOrLineTerminator;
141   StaticResource<Utf8Decoder> utf8_decoder_;
142
143   DISALLOW_COPY_AND_ASSIGN(UnicodeCache);
144 };
145
146
147 // ---------------------------------------------------------------------
148 // DuplicateFinder discovers duplicate symbols.
149
150 class DuplicateFinder {
151  public:
152   explicit DuplicateFinder(UnicodeCache* constants)
153       : unicode_constants_(constants),
154         backing_store_(16),
155         map_(&Match) { }
156
157   int AddOneByteSymbol(Vector<const uint8_t> key, int value);
158   int AddTwoByteSymbol(Vector<const uint16_t> key, int value);
159   // Add a a number literal by converting it (if necessary)
160   // to the string that ToString(ToNumber(literal)) would generate.
161   // and then adding that string with AddOneByteSymbol.
162   // This string is the actual value used as key in an object literal,
163   // and the one that must be different from the other keys.
164   int AddNumber(Vector<const uint8_t> key, int value);
165
166  private:
167   int AddSymbol(Vector<const uint8_t> key, bool is_one_byte, int value);
168   // Backs up the key and its length in the backing store.
169   // The backup is stored with a base 127 encoding of the
170   // length (plus a bit saying whether the string is one byte),
171   // followed by the bytes of the key.
172   uint8_t* BackupKey(Vector<const uint8_t> key, bool is_one_byte);
173
174   // Compare two encoded keys (both pointing into the backing store)
175   // for having the same base-127 encoded lengths and representation.
176   // and then having the same 'length' bytes following.
177   static bool Match(void* first, void* second);
178   // Creates a hash from a sequence of bytes.
179   static uint32_t Hash(Vector<const uint8_t> key, bool is_one_byte);
180   // Checks whether a string containing a JS number is its canonical
181   // form.
182   static bool IsNumberCanonical(Vector<const uint8_t> key);
183
184   // Size of buffer. Sufficient for using it to call DoubleToCString in
185   // from conversions.h.
186   static const int kBufferSize = 100;
187
188   UnicodeCache* unicode_constants_;
189   // Backing store used to store strings used as hashmap keys.
190   SequenceCollector<unsigned char> backing_store_;
191   HashMap map_;
192   // Buffer used for string->number->canonical string conversions.
193   char number_buffer_[kBufferSize];
194 };
195
196
197 // ----------------------------------------------------------------------------
198 // LiteralBuffer -  Collector of chars of literals.
199
200 class LiteralBuffer {
201  public:
202   LiteralBuffer() : is_one_byte_(true), position_(0), backing_store_() { }
203
204   ~LiteralBuffer() {
205     if (backing_store_.length() > 0) {
206       backing_store_.Dispose();
207     }
208   }
209
210   INLINE(void AddChar(uint32_t code_unit)) {
211     if (position_ >= backing_store_.length()) ExpandBuffer();
212     if (is_one_byte_) {
213       if (code_unit <= unibrow::Latin1::kMaxChar) {
214         backing_store_[position_] = static_cast<byte>(code_unit);
215         position_ += kOneByteSize;
216         return;
217       }
218       ConvertToTwoByte();
219     }
220     if (code_unit <= unibrow::Utf16::kMaxNonSurrogateCharCode) {
221       *reinterpret_cast<uint16_t*>(&backing_store_[position_]) = code_unit;
222       position_ += kUC16Size;
223     } else {
224       *reinterpret_cast<uint16_t*>(&backing_store_[position_]) =
225           unibrow::Utf16::LeadSurrogate(code_unit);
226       position_ += kUC16Size;
227       if (position_ >= backing_store_.length()) ExpandBuffer();
228       *reinterpret_cast<uint16_t*>(&backing_store_[position_]) =
229           unibrow::Utf16::TrailSurrogate(code_unit);
230       position_ += kUC16Size;
231     }
232   }
233
234   bool is_one_byte() const { return is_one_byte_; }
235
236   bool is_contextual_keyword(Vector<const char> keyword) const {
237     return is_one_byte() && keyword.length() == position_ &&
238         (memcmp(keyword.start(), backing_store_.start(), position_) == 0);
239   }
240
241   Vector<const uint16_t> two_byte_literal() const {
242     DCHECK(!is_one_byte_);
243     DCHECK((position_ & 0x1) == 0);
244     return Vector<const uint16_t>(
245         reinterpret_cast<const uint16_t*>(backing_store_.start()),
246         position_ >> 1);
247   }
248
249   Vector<const uint8_t> one_byte_literal() const {
250     DCHECK(is_one_byte_);
251     return Vector<const uint8_t>(
252         reinterpret_cast<const uint8_t*>(backing_store_.start()),
253         position_);
254   }
255
256   int length() const {
257     return is_one_byte_ ? position_ : (position_ >> 1);
258   }
259
260   void ReduceLength(int delta) {
261     position_ -= delta * (is_one_byte_ ? kOneByteSize : kUC16Size);
262   }
263
264   void Reset() {
265     position_ = 0;
266     is_one_byte_ = true;
267   }
268
269   Handle<String> Internalize(Isolate* isolate) const;
270
271  private:
272   static const int kInitialCapacity = 16;
273   static const int kGrowthFactory = 4;
274   static const int kMinConversionSlack = 256;
275   static const int kMaxGrowth = 1 * MB;
276   inline int NewCapacity(int min_capacity) {
277     int capacity = Max(min_capacity, backing_store_.length());
278     int new_capacity = Min(capacity * kGrowthFactory, capacity + kMaxGrowth);
279     return new_capacity;
280   }
281
282   void ExpandBuffer() {
283     Vector<byte> new_store = Vector<byte>::New(NewCapacity(kInitialCapacity));
284     MemCopy(new_store.start(), backing_store_.start(), position_);
285     backing_store_.Dispose();
286     backing_store_ = new_store;
287   }
288
289   void ConvertToTwoByte() {
290     DCHECK(is_one_byte_);
291     Vector<byte> new_store;
292     int new_content_size = position_ * kUC16Size;
293     if (new_content_size >= backing_store_.length()) {
294       // Ensure room for all currently read code units as UC16 as well
295       // as the code unit about to be stored.
296       new_store = Vector<byte>::New(NewCapacity(new_content_size));
297     } else {
298       new_store = backing_store_;
299     }
300     uint8_t* src = backing_store_.start();
301     uint16_t* dst = reinterpret_cast<uint16_t*>(new_store.start());
302     for (int i = position_ - 1; i >= 0; i--) {
303       dst[i] = src[i];
304     }
305     if (new_store.start() != backing_store_.start()) {
306       backing_store_.Dispose();
307       backing_store_ = new_store;
308     }
309     position_ = new_content_size;
310     is_one_byte_ = false;
311   }
312
313   bool is_one_byte_;
314   int position_;
315   Vector<byte> backing_store_;
316
317   DISALLOW_COPY_AND_ASSIGN(LiteralBuffer);
318 };
319
320
321 // ----------------------------------------------------------------------------
322 // JavaScript Scanner.
323
324 class Scanner {
325  public:
326   // Scoped helper for literal recording. Automatically drops the literal
327   // if aborting the scanning before it's complete.
328   class LiteralScope {
329    public:
330     explicit LiteralScope(Scanner* self) : scanner_(self), complete_(false) {
331       scanner_->StartLiteral();
332     }
333      ~LiteralScope() {
334        if (!complete_) scanner_->DropLiteral();
335      }
336     void Complete() {
337       complete_ = true;
338     }
339
340    private:
341     Scanner* scanner_;
342     bool complete_;
343   };
344
345   // Representation of an interval of source positions.
346   struct Location {
347     Location(int b, int e) : beg_pos(b), end_pos(e) { }
348     Location() : beg_pos(0), end_pos(0) { }
349
350     bool IsValid() const {
351       return beg_pos >= 0 && end_pos >= beg_pos;
352     }
353
354     static Location invalid() { return Location(-1, -1); }
355
356     int beg_pos;
357     int end_pos;
358   };
359
360   // -1 is outside of the range of any real source code.
361   static const int kNoOctalLocation = -1;
362
363   explicit Scanner(UnicodeCache* scanner_contants);
364
365   void Initialize(Utf16CharacterStream* source);
366
367   // Returns the next token and advances input.
368   Token::Value Next();
369   // Returns the current token again.
370   Token::Value current_token() { return current_.token; }
371   // Returns the location information for the current token
372   // (the token last returned by Next()).
373   Location location() const { return current_.location; }
374
375   // Similar functions for the upcoming token.
376
377   // One token look-ahead (past the token returned by Next()).
378   Token::Value peek() const { return next_.token; }
379
380   Location peek_location() const { return next_.location; }
381
382   bool literal_contains_escapes() const {
383     Location location = current_.location;
384     int source_length = (location.end_pos - location.beg_pos);
385     if (current_.token == Token::STRING) {
386       // Subtract delimiters.
387       source_length -= 2;
388     }
389     return current_.literal_chars->length() != source_length;
390   }
391   bool is_literal_contextual_keyword(Vector<const char> keyword) {
392     DCHECK_NOT_NULL(current_.literal_chars);
393     return current_.literal_chars->is_contextual_keyword(keyword);
394   }
395   bool is_next_contextual_keyword(Vector<const char> keyword) {
396     DCHECK_NOT_NULL(next_.literal_chars);
397     return next_.literal_chars->is_contextual_keyword(keyword);
398   }
399
400   const AstRawString* CurrentSymbol(AstValueFactory* ast_value_factory);
401   const AstRawString* NextSymbol(AstValueFactory* ast_value_factory);
402   const AstRawString* CurrentRawSymbol(AstValueFactory* ast_value_factory);
403
404   double DoubleValue();
405   bool LiteralMatches(const char* data, int length, bool allow_escapes = true) {
406     if (is_literal_one_byte() &&
407         literal_length() == length &&
408         (allow_escapes || !literal_contains_escapes())) {
409       const char* token =
410           reinterpret_cast<const char*>(literal_one_byte_string().start());
411       return !strncmp(token, data, length);
412     }
413     return false;
414   }
415   inline bool UnescapedLiteralMatches(const char* data, int length) {
416     return LiteralMatches(data, length, false);
417   }
418
419   void IsGetOrSet(bool* is_get, bool* is_set) {
420     if (is_literal_one_byte() &&
421         literal_length() == 3 &&
422         !literal_contains_escapes()) {
423       const char* token =
424           reinterpret_cast<const char*>(literal_one_byte_string().start());
425       *is_get = strncmp(token, "get", 3) == 0;
426       *is_set = !*is_get && strncmp(token, "set", 3) == 0;
427     }
428   }
429
430   int FindSymbol(DuplicateFinder* finder, int value);
431
432   UnicodeCache* unicode_cache() { return unicode_cache_; }
433
434   // Returns the location of the last seen octal literal.
435   Location octal_position() const { return octal_pos_; }
436   void clear_octal_position() { octal_pos_ = Location::invalid(); }
437
438   // Returns the value of the last smi that was scanned.
439   int smi_value() const { return smi_value_; }
440
441   // Seek forward to the given position.  This operation does not
442   // work in general, for instance when there are pushed back
443   // characters, but works for seeking forward until simple delimiter
444   // tokens, which is what it is used for.
445   void SeekForward(int pos);
446
447   bool HarmonyModules() const {
448     return harmony_modules_;
449   }
450   void SetHarmonyModules(bool modules) {
451     harmony_modules_ = modules;
452   }
453   bool HarmonyNumericLiterals() const {
454     return harmony_numeric_literals_;
455   }
456   void SetHarmonyNumericLiterals(bool numeric_literals) {
457     harmony_numeric_literals_ = numeric_literals;
458   }
459   bool HarmonyClasses() const {
460     return harmony_classes_;
461   }
462   void SetHarmonyClasses(bool classes) {
463     harmony_classes_ = classes;
464   }
465   bool HarmonyUnicode() const { return harmony_unicode_; }
466   void SetHarmonyUnicode(bool unicode) { harmony_unicode_ = unicode; }
467
468   // Returns true if there was a line terminator before the peek'ed token,
469   // possibly inside a multi-line comment.
470   bool HasAnyLineTerminatorBeforeNext() const {
471     return has_line_terminator_before_next_ ||
472            has_multiline_comment_before_next_;
473   }
474
475   // Scans the input as a regular expression pattern, previous
476   // character(s) must be /(=). Returns true if a pattern is scanned.
477   bool ScanRegExpPattern(bool seen_equal);
478   // Returns true if regexp flags are scanned (always since flags can
479   // be empty).
480   bool ScanRegExpFlags();
481
482   // Scans the input as a template literal
483   Token::Value ScanTemplateStart();
484   Token::Value ScanTemplateContinuation();
485
486   const LiteralBuffer* source_url() const { return &source_url_; }
487   const LiteralBuffer* source_mapping_url() const {
488     return &source_mapping_url_;
489   }
490
491   bool IdentifierIsFutureStrictReserved(const AstRawString* string) const;
492
493  private:
494   // The current and look-ahead token.
495   struct TokenDesc {
496     Token::Value token;
497     Location location;
498     LiteralBuffer* literal_chars;
499     LiteralBuffer* raw_literal_chars;
500   };
501
502   static const int kCharacterLookaheadBufferSize = 1;
503
504   // Scans octal escape sequence. Also accepts "\0" decimal escape sequence.
505   template <bool capture_raw>
506   uc32 ScanOctalEscape(uc32 c, int length);
507
508   // Call this after setting source_ to the input.
509   void Init() {
510     // Set c0_ (one character ahead)
511     STATIC_ASSERT(kCharacterLookaheadBufferSize == 1);
512     Advance();
513     // Initialize current_ to not refer to a literal.
514     current_.literal_chars = NULL;
515     current_.raw_literal_chars = NULL;
516   }
517
518   // Literal buffer support
519   inline void StartLiteral() {
520     LiteralBuffer* free_buffer = (current_.literal_chars == &literal_buffer1_) ?
521             &literal_buffer2_ : &literal_buffer1_;
522     free_buffer->Reset();
523     next_.literal_chars = free_buffer;
524   }
525
526   inline void StartRawLiteral() {
527     LiteralBuffer* free_buffer =
528         (current_.raw_literal_chars == &raw_literal_buffer1_) ?
529             &raw_literal_buffer2_ : &raw_literal_buffer1_;
530     free_buffer->Reset();
531     next_.raw_literal_chars = free_buffer;
532   }
533
534   INLINE(void AddLiteralChar(uc32 c)) {
535     DCHECK_NOT_NULL(next_.literal_chars);
536     next_.literal_chars->AddChar(c);
537   }
538
539   INLINE(void AddRawLiteralChar(uc32 c)) {
540     DCHECK_NOT_NULL(next_.raw_literal_chars);
541     next_.raw_literal_chars->AddChar(c);
542   }
543
544   INLINE(void ReduceRawLiteralLength(int delta)) {
545     DCHECK_NOT_NULL(next_.raw_literal_chars);
546     next_.raw_literal_chars->ReduceLength(delta);
547   }
548
549   // Stops scanning of a literal and drop the collected characters,
550   // e.g., due to an encountered error.
551   inline void DropLiteral() {
552     next_.literal_chars = NULL;
553     next_.raw_literal_chars = NULL;
554   }
555
556   inline void AddLiteralCharAdvance() {
557     AddLiteralChar(c0_);
558     Advance();
559   }
560
561   // Low-level scanning support.
562   template <bool capture_raw = false, bool check_surrogate = true>
563   void Advance() {
564     if (capture_raw) {
565       AddRawLiteralChar(c0_);
566     }
567     c0_ = source_->Advance();
568     if (check_surrogate) HandleLeadSurrogate();
569   }
570
571   void HandleLeadSurrogate() {
572     if (unibrow::Utf16::IsLeadSurrogate(c0_)) {
573       uc32 c1 = source_->Advance();
574       if (!unibrow::Utf16::IsTrailSurrogate(c1)) {
575         source_->PushBack(c1);
576       } else {
577         c0_ = unibrow::Utf16::CombineSurrogatePair(c0_, c1);
578       }
579     }
580   }
581
582   void PushBack(uc32 ch) {
583     if (ch > static_cast<uc32>(unibrow::Utf16::kMaxNonSurrogateCharCode)) {
584       source_->PushBack(unibrow::Utf16::TrailSurrogate(c0_));
585       source_->PushBack(unibrow::Utf16::LeadSurrogate(c0_));
586     } else {
587       source_->PushBack(c0_);
588     }
589     c0_ = ch;
590   }
591
592   inline Token::Value Select(Token::Value tok) {
593     Advance();
594     return tok;
595   }
596
597   inline Token::Value Select(uc32 next, Token::Value then, Token::Value else_) {
598     Advance();
599     if (c0_ == next) {
600       Advance();
601       return then;
602     } else {
603       return else_;
604     }
605   }
606
607   // Returns the literal string, if any, for the current token (the
608   // token last returned by Next()). The string is 0-terminated.
609   // Literal strings are collected for identifiers, strings, numbers as well
610   // as for template literals. For template literals we also collect the raw
611   // form.
612   // These functions only give the correct result if the literal was scanned
613   // when a LiteralScope object is alive.
614   Vector<const uint8_t> literal_one_byte_string() {
615     DCHECK_NOT_NULL(current_.literal_chars);
616     return current_.literal_chars->one_byte_literal();
617   }
618   Vector<const uint16_t> literal_two_byte_string() {
619     DCHECK_NOT_NULL(current_.literal_chars);
620     return current_.literal_chars->two_byte_literal();
621   }
622   bool is_literal_one_byte() {
623     DCHECK_NOT_NULL(current_.literal_chars);
624     return current_.literal_chars->is_one_byte();
625   }
626   int literal_length() const {
627     DCHECK_NOT_NULL(current_.literal_chars);
628     return current_.literal_chars->length();
629   }
630   // Returns the literal string for the next token (the token that
631   // would be returned if Next() were called).
632   Vector<const uint8_t> next_literal_one_byte_string() {
633     DCHECK_NOT_NULL(next_.literal_chars);
634     return next_.literal_chars->one_byte_literal();
635   }
636   Vector<const uint16_t> next_literal_two_byte_string() {
637     DCHECK_NOT_NULL(next_.literal_chars);
638     return next_.literal_chars->two_byte_literal();
639   }
640   bool is_next_literal_one_byte() {
641     DCHECK_NOT_NULL(next_.literal_chars);
642     return next_.literal_chars->is_one_byte();
643   }
644   Vector<const uint8_t> raw_literal_one_byte_string() {
645     DCHECK_NOT_NULL(current_.raw_literal_chars);
646     return current_.raw_literal_chars->one_byte_literal();
647   }
648   Vector<const uint16_t> raw_literal_two_byte_string() {
649     DCHECK_NOT_NULL(current_.raw_literal_chars);
650     return current_.raw_literal_chars->two_byte_literal();
651   }
652   bool is_raw_literal_one_byte() {
653     DCHECK_NOT_NULL(current_.raw_literal_chars);
654     return current_.raw_literal_chars->is_one_byte();
655   }
656
657   template <bool capture_raw>
658   uc32 ScanHexNumber(int expected_length);
659   // Scan a number of any length but not bigger than max_value. For example, the
660   // number can be 000000001, so it's very long in characters but its value is
661   // small.
662   template <bool capture_raw>
663   uc32 ScanUnlimitedLengthHexNumber(int max_value);
664
665   // Scans a single JavaScript token.
666   void Scan();
667
668   bool SkipWhiteSpace();
669   Token::Value SkipSingleLineComment();
670   Token::Value SkipSourceURLComment();
671   void TryToParseSourceURLComment();
672   Token::Value SkipMultiLineComment();
673   // Scans a possible HTML comment -- begins with '<!'.
674   Token::Value ScanHtmlComment();
675
676   void ScanDecimalDigits();
677   Token::Value ScanNumber(bool seen_period);
678   Token::Value ScanIdentifierOrKeyword();
679   Token::Value ScanIdentifierSuffix(LiteralScope* literal);
680
681   Token::Value ScanString();
682
683   // Scans an escape-sequence which is part of a string and adds the
684   // decoded character to the current literal. Returns true if a pattern
685   // is scanned.
686   template <bool capture_raw, bool in_template_literal>
687   bool ScanEscape();
688
689   // Decodes a Unicode escape-sequence which is part of an identifier.
690   // If the escape sequence cannot be decoded the result is kBadChar.
691   uc32 ScanIdentifierUnicodeEscape();
692   // Helper for the above functions.
693   template <bool capture_raw>
694   uc32 ScanUnicodeEscape();
695
696   Token::Value ScanTemplateSpan();
697
698   // Return the current source position.
699   int source_pos() {
700     return static_cast<int>(source_->pos()) - kCharacterLookaheadBufferSize;
701   }
702
703   UnicodeCache* unicode_cache_;
704
705   // Buffers collecting literal strings, numbers, etc.
706   LiteralBuffer literal_buffer1_;
707   LiteralBuffer literal_buffer2_;
708
709   // Values parsed from magic comments.
710   LiteralBuffer source_url_;
711   LiteralBuffer source_mapping_url_;
712
713   // Buffer to store raw string values
714   LiteralBuffer raw_literal_buffer1_;
715   LiteralBuffer raw_literal_buffer2_;
716
717   TokenDesc current_;  // desc for current token (as returned by Next())
718   TokenDesc next_;     // desc for next token (one token look-ahead)
719
720   // Input stream. Must be initialized to an Utf16CharacterStream.
721   Utf16CharacterStream* source_;
722
723
724   // Start position of the octal literal last scanned.
725   Location octal_pos_;
726
727   // Value of the last smi that was scanned.
728   int smi_value_;
729
730   // One Unicode character look-ahead; c0_ < 0 at the end of the input.
731   uc32 c0_;
732
733   // Whether there is a line terminator whitespace character after
734   // the current token, and  before the next. Does not count newlines
735   // inside multiline comments.
736   bool has_line_terminator_before_next_;
737   // Whether there is a multi-line comment that contains a
738   // line-terminator after the current token, and before the next.
739   bool has_multiline_comment_before_next_;
740   // Whether we scan 'module', 'import', 'export' as keywords.
741   bool harmony_modules_;
742   // Whether we scan 0o777 and 0b111 as numbers.
743   bool harmony_numeric_literals_;
744   // Whether we scan 'class', 'extends', 'static' and 'super' as keywords.
745   bool harmony_classes_;
746   // Whether we allow \u{xxxxx}.
747   bool harmony_unicode_;
748 };
749
750 } }  // namespace v8::internal
751
752 #endif  // V8_SCANNER_H_