OLD | NEW |
---|---|
1 // Copyright 2011 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Features shared by parsing and pre-parsing scanners. | 5 // Features shared by parsing and pre-parsing scanners. |
6 | 6 |
7 #ifndef V8_SCANNER_H_ | 7 #ifndef V8_SCANNER_H_ |
8 #define V8_SCANNER_H_ | 8 #define V8_SCANNER_H_ |
9 | 9 |
10 #include "src/allocation.h" | 10 #include "src/allocation.h" |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
82 } | 82 } |
83 return SlowSeekForward(code_unit_count); | 83 return SlowSeekForward(code_unit_count); |
84 } | 84 } |
85 | 85 |
86 // Pushes back the most recently read UTF-16 code unit (or negative | 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 | 87 // value if at end of input), i.e., the value returned by the most recent |
88 // call to Advance. | 88 // call to Advance. |
89 // Must not be used right after calling SeekForward. | 89 // Must not be used right after calling SeekForward. |
90 virtual void PushBack(int32_t code_unit) = 0; | 90 virtual void PushBack(int32_t code_unit) = 0; |
91 | 91 |
92 virtual bool SetBookmark(); | |
93 virtual void ResetToBookmark(); | |
94 | |
92 protected: | 95 protected: |
93 static const uc32 kEndOfInput = -1; | 96 static const uc32 kEndOfInput = -1; |
94 | 97 |
95 // Ensures that the buffer_cursor_ points to the code_unit at | 98 // Ensures that the buffer_cursor_ points to the code_unit at |
96 // position pos_ of the input, if possible. If the position | 99 // position pos_ of the input, if possible. If the position |
97 // is at or after the end of the input, return false. If there | 100 // is at or after the end of the input, return false. If there |
98 // are more code_units available, return true. | 101 // are more code_units available, return true. |
99 virtual bool ReadBlock() = 0; | 102 virtual bool ReadBlock() = 0; |
100 virtual size_t SlowSeekForward(size_t code_unit_count) = 0; | 103 virtual size_t SlowSeekForward(size_t code_unit_count) = 0; |
101 | 104 |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
261 position_ -= delta * (is_one_byte_ ? kOneByteSize : kUC16Size); | 264 position_ -= delta * (is_one_byte_ ? kOneByteSize : kUC16Size); |
262 } | 265 } |
263 | 266 |
264 void Reset() { | 267 void Reset() { |
265 position_ = 0; | 268 position_ = 0; |
266 is_one_byte_ = true; | 269 is_one_byte_ = true; |
267 } | 270 } |
268 | 271 |
269 Handle<String> Internalize(Isolate* isolate) const; | 272 Handle<String> Internalize(Isolate* isolate) const; |
270 | 273 |
274 void CopyFrom(const LiteralBuffer* other) { | |
275 if (other == nullptr) { | |
276 Reset(); | |
277 } else { | |
278 is_one_byte_ = other->is_one_byte_; | |
279 position_ = other->position_; | |
280 backing_store_.Dispose(); | |
281 backing_store_ = other->backing_store_.Clone(); | |
282 } | |
283 } | |
284 | |
271 private: | 285 private: |
272 static const int kInitialCapacity = 16; | 286 static const int kInitialCapacity = 16; |
273 static const int kGrowthFactory = 4; | 287 static const int kGrowthFactory = 4; |
274 static const int kMinConversionSlack = 256; | 288 static const int kMinConversionSlack = 256; |
275 static const int kMaxGrowth = 1 * MB; | 289 static const int kMaxGrowth = 1 * MB; |
276 inline int NewCapacity(int min_capacity) { | 290 inline int NewCapacity(int min_capacity) { |
277 int capacity = Max(min_capacity, backing_store_.length()); | 291 int capacity = Max(min_capacity, backing_store_.length()); |
278 int new_capacity = Min(capacity * kGrowthFactory, capacity + kMaxGrowth); | 292 int new_capacity = Min(capacity * kGrowthFactory, capacity + kMaxGrowth); |
279 return new_capacity; | 293 return new_capacity; |
280 } | 294 } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
335 } | 349 } |
336 void Complete() { | 350 void Complete() { |
337 complete_ = true; | 351 complete_ = true; |
338 } | 352 } |
339 | 353 |
340 private: | 354 private: |
341 Scanner* scanner_; | 355 Scanner* scanner_; |
342 bool complete_; | 356 bool complete_; |
343 }; | 357 }; |
344 | 358 |
359 // Scoped helper for a re-settable bookmark. | |
360 class BookmarkScope { | |
361 public: | |
362 explicit BookmarkScope(Scanner* scanner) : scanner_(scanner) { | |
363 DCHECK_NOT_NULL(scanner_); | |
364 } | |
365 ~BookmarkScope() { scanner_->DropBookmark(); } | |
366 | |
367 bool Set() { return scanner_->SetBookmark(); } | |
368 void Reset() { scanner_->ResetToBookmark(); } | |
369 bool HasBeenSet() { return scanner_->BookmarkHasBeenSet(); } | |
370 bool HasBeenReset() { return scanner_->BookmarkHasBeenReset(); } | |
371 | |
372 private: | |
373 Scanner* scanner_; | |
374 }; | |
375 | |
345 // Representation of an interval of source positions. | 376 // Representation of an interval of source positions. |
346 struct Location { | 377 struct Location { |
347 Location(int b, int e) : beg_pos(b), end_pos(e) { } | 378 Location(int b, int e) : beg_pos(b), end_pos(e) { } |
348 Location() : beg_pos(0), end_pos(0) { } | 379 Location() : beg_pos(0), end_pos(0) { } |
349 | 380 |
350 bool IsValid() const { | 381 bool IsValid() const { |
351 return beg_pos >= 0 && end_pos >= beg_pos; | 382 return beg_pos >= 0 && end_pos >= beg_pos; |
352 } | 383 } |
353 | 384 |
354 static Location invalid() { return Location(-1, -1); } | 385 static Location invalid() { return Location(-1, -1); } |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
510 // Call this after setting source_ to the input. | 541 // Call this after setting source_ to the input. |
511 void Init() { | 542 void Init() { |
512 // Set c0_ (one character ahead) | 543 // Set c0_ (one character ahead) |
513 STATIC_ASSERT(kCharacterLookaheadBufferSize == 1); | 544 STATIC_ASSERT(kCharacterLookaheadBufferSize == 1); |
514 Advance(); | 545 Advance(); |
515 // Initialize current_ to not refer to a literal. | 546 // Initialize current_ to not refer to a literal. |
516 current_.literal_chars = NULL; | 547 current_.literal_chars = NULL; |
517 current_.raw_literal_chars = NULL; | 548 current_.raw_literal_chars = NULL; |
518 } | 549 } |
519 | 550 |
551 // Support BookmarkScope functionality. | |
552 bool SetBookmark(); | |
553 void ResetToBookmark(); | |
554 bool BookmarkHasBeenSet(); | |
555 bool BookmarkHasBeenReset(); | |
556 void DropBookmark(); | |
557 static void CopyTokenDesc(TokenDesc* to, TokenDesc* from); | |
558 | |
520 // Literal buffer support | 559 // Literal buffer support |
521 inline void StartLiteral() { | 560 inline void StartLiteral() { |
522 LiteralBuffer* free_buffer = (current_.literal_chars == &literal_buffer1_) ? | 561 LiteralBuffer* free_buffer = (current_.literal_chars == &literal_buffer1_) ? |
523 &literal_buffer2_ : &literal_buffer1_; | 562 &literal_buffer2_ : &literal_buffer1_; |
524 free_buffer->Reset(); | 563 free_buffer->Reset(); |
525 next_.literal_chars = free_buffer; | 564 next_.literal_chars = free_buffer; |
526 } | 565 } |
527 | 566 |
528 inline void StartRawLiteral() { | 567 inline void StartRawLiteral() { |
529 LiteralBuffer* free_buffer = | 568 LiteralBuffer* free_buffer = |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
712 LiteralBuffer source_url_; | 751 LiteralBuffer source_url_; |
713 LiteralBuffer source_mapping_url_; | 752 LiteralBuffer source_mapping_url_; |
714 | 753 |
715 // Buffer to store raw string values | 754 // Buffer to store raw string values |
716 LiteralBuffer raw_literal_buffer1_; | 755 LiteralBuffer raw_literal_buffer1_; |
717 LiteralBuffer raw_literal_buffer2_; | 756 LiteralBuffer raw_literal_buffer2_; |
718 | 757 |
719 TokenDesc current_; // desc for current token (as returned by Next()) | 758 TokenDesc current_; // desc for current token (as returned by Next()) |
720 TokenDesc next_; // desc for next token (one token look-ahead) | 759 TokenDesc next_; // desc for next token (one token look-ahead) |
721 | 760 |
761 // Variables for Scanner::BookmarkScope and the *Bookmark implementation. | |
762 // These variables contain the scanner state when a bookmark is set. | |
763 // We will use bookmark_c0_ as a 'control' variable, where: | |
764 // - bookmark_c0_ >= 0: A bookmark has been set and this contains c0_. | |
765 // - bookmark_c0_ == -1: No bookmark has been set. | |
766 // - bookmark_c0_ == -2: The bookmark has been applied (ResetToBookmark). | |
767 TokenDesc bookmark_current_; | |
768 TokenDesc bookmark_next_; | |
769 uc32 bookmark_c0_; | |
marja
2015/04/24 12:08:13
Pls add a comment here about why we need to store
| |
770 LiteralBuffer bookmark_current_literal_; | |
771 LiteralBuffer bookmark_current_raw_literal_; | |
772 LiteralBuffer bookmark_next_literal_; | |
773 LiteralBuffer bookmark_next_raw_literal_; | |
774 | |
722 // Input stream. Must be initialized to an Utf16CharacterStream. | 775 // Input stream. Must be initialized to an Utf16CharacterStream. |
723 Utf16CharacterStream* source_; | 776 Utf16CharacterStream* source_; |
724 | 777 |
725 | 778 |
726 // Start position of the octal literal last scanned. | 779 // Start position of the octal literal last scanned. |
727 Location octal_pos_; | 780 Location octal_pos_; |
728 | 781 |
729 // Value of the last smi that was scanned. | 782 // Value of the last smi that was scanned. |
730 int smi_value_; | 783 int smi_value_; |
731 | 784 |
(...skipping 11 matching lines...) Expand all Loading... | |
743 bool harmony_modules_; | 796 bool harmony_modules_; |
744 // Whether we scan 'class', 'extends', 'static' and 'super' as keywords. | 797 // Whether we scan 'class', 'extends', 'static' and 'super' as keywords. |
745 bool harmony_classes_; | 798 bool harmony_classes_; |
746 // Whether we allow \u{xxxxx}. | 799 // Whether we allow \u{xxxxx}. |
747 bool harmony_unicode_; | 800 bool harmony_unicode_; |
748 }; | 801 }; |
749 | 802 |
750 } } // namespace v8::internal | 803 } } // namespace v8::internal |
751 | 804 |
752 #endif // V8_SCANNER_H_ | 805 #endif // V8_SCANNER_H_ |
OLD | NEW |