Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(27)

Side by Side Diff: src/scanner.h

Issue 1102523003: Implement a 'trial parse' step, that will abort pre-parsing excessively (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Improve comments + naming. (Marja's feedback.) Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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
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 };
jochen (gone - plz use gerrit) 2015/04/30 13:20:18 disallow copy/assign
vogelheim 2015/04/30 15:35:05 Done.
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 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
502 // Call this after setting source_ to the input. 533 // Call this after setting source_ to the input.
503 void Init() { 534 void Init() {
504 // Set c0_ (one character ahead) 535 // Set c0_ (one character ahead)
505 STATIC_ASSERT(kCharacterLookaheadBufferSize == 1); 536 STATIC_ASSERT(kCharacterLookaheadBufferSize == 1);
506 Advance(); 537 Advance();
507 // Initialize current_ to not refer to a literal. 538 // Initialize current_ to not refer to a literal.
508 current_.literal_chars = NULL; 539 current_.literal_chars = NULL;
509 current_.raw_literal_chars = NULL; 540 current_.raw_literal_chars = NULL;
510 } 541 }
511 542
543 // Support BookmarkScope functionality.
544 bool SetBookmark();
545 void ResetToBookmark();
546 bool BookmarkHasBeenSet();
547 bool BookmarkHasBeenReset();
548 void DropBookmark();
549 static void CopyTokenDesc(TokenDesc* to, TokenDesc* from);
550
512 // Literal buffer support 551 // Literal buffer support
513 inline void StartLiteral() { 552 inline void StartLiteral() {
514 LiteralBuffer* free_buffer = (current_.literal_chars == &literal_buffer1_) ? 553 LiteralBuffer* free_buffer = (current_.literal_chars == &literal_buffer1_) ?
515 &literal_buffer2_ : &literal_buffer1_; 554 &literal_buffer2_ : &literal_buffer1_;
516 free_buffer->Reset(); 555 free_buffer->Reset();
517 next_.literal_chars = free_buffer; 556 next_.literal_chars = free_buffer;
518 } 557 }
519 558
520 inline void StartRawLiteral() { 559 inline void StartRawLiteral() {
521 LiteralBuffer* free_buffer = 560 LiteralBuffer* free_buffer =
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
704 LiteralBuffer source_url_; 743 LiteralBuffer source_url_;
705 LiteralBuffer source_mapping_url_; 744 LiteralBuffer source_mapping_url_;
706 745
707 // Buffer to store raw string values 746 // Buffer to store raw string values
708 LiteralBuffer raw_literal_buffer1_; 747 LiteralBuffer raw_literal_buffer1_;
709 LiteralBuffer raw_literal_buffer2_; 748 LiteralBuffer raw_literal_buffer2_;
710 749
711 TokenDesc current_; // desc for current token (as returned by Next()) 750 TokenDesc current_; // desc for current token (as returned by Next())
712 TokenDesc next_; // desc for next token (one token look-ahead) 751 TokenDesc next_; // desc for next token (one token look-ahead)
713 752
753 // Variables for Scanner::BookmarkScope and the *Bookmark implementation.
754 // These variables contain the scanner state when a bookmark is set.
755 //
756 // We will use bookmark_c0_ as a 'control' variable, where:
757 // - bookmark_c0_ >= 0: A bookmark has been set and this contains c0_.
758 // - bookmark_c0_ == -1: No bookmark has been set.
jochen (gone - plz use gerrit) 2015/04/30 13:20:18 how about defining an enum for -1 and -2?
vogelheim 2015/04/30 15:35:06 Done.
759 // - bookmark_c0_ == -2: The bookmark has been applied (ResetToBookmark).
760 //
761 // Which state is being bookmarked? The parser state is distributed over
762 // several variables, roughly like this:
763 // ... 1234 + 5678 ..... [character stream]
764 // [current_] [next_] c0_ | [scanner state]
765 // So when the scanner is logically at the beginning of an expression
766 // like "1234 + 4567", then:
767 // - current_ contains "1234"
768 // - next_ contains "+"
769 // - c0_ contains ' ' (the space between "+" and "5678",
770 // - the source_ character stream points to the beginning of "5678".
771 // To be able to restore this state, we will keep copies of current_, next_,
772 // and c0_; we'll ask the stream to bookmark itself, and we'll copy the
773 // contents of current_'s and next_'s literal buffers to bookmark_*_literal_.
774 TokenDesc bookmark_current_;
775 TokenDesc bookmark_next_;
776 uc32 bookmark_c0_;
777 LiteralBuffer bookmark_current_literal_;
778 LiteralBuffer bookmark_current_raw_literal_;
779 LiteralBuffer bookmark_next_literal_;
780 LiteralBuffer bookmark_next_raw_literal_;
781
714 // Input stream. Must be initialized to an Utf16CharacterStream. 782 // Input stream. Must be initialized to an Utf16CharacterStream.
715 Utf16CharacterStream* source_; 783 Utf16CharacterStream* source_;
716 784
717 785
718 // Start position of the octal literal last scanned. 786 // Start position of the octal literal last scanned.
719 Location octal_pos_; 787 Location octal_pos_;
720 788
721 // Value of the last smi that was scanned. 789 // Value of the last smi that was scanned.
722 int smi_value_; 790 int smi_value_;
723 791
(...skipping 11 matching lines...) Expand all
735 bool harmony_modules_; 803 bool harmony_modules_;
736 // Whether we scan 'class', 'extends', 'static' and 'super' as keywords. 804 // Whether we scan 'class', 'extends', 'static' and 'super' as keywords.
737 bool harmony_classes_; 805 bool harmony_classes_;
738 // Whether we allow \u{xxxxx}. 806 // Whether we allow \u{xxxxx}.
739 bool harmony_unicode_; 807 bool harmony_unicode_;
740 }; 808 };
741 809
742 } } // namespace v8::internal 810 } } // namespace v8::internal
743 811
744 #endif // V8_SCANNER_H_ 812 #endif // V8_SCANNER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698