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

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: Created 5 years, 8 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 bool CanResetToBookmark();
94 virtual bool ResetToBookmark();
95
92 protected: 96 protected:
93 static const uc32 kEndOfInput = -1; 97 static const uc32 kEndOfInput = -1;
94 98
95 // Ensures that the buffer_cursor_ points to the code_unit at 99 // Ensures that the buffer_cursor_ points to the code_unit at
96 // position pos_ of the input, if possible. If the position 100 // position pos_ of the input, if possible. If the position
97 // is at or after the end of the input, return false. If there 101 // is at or after the end of the input, return false. If there
98 // are more code_units available, return true. 102 // are more code_units available, return true.
99 virtual bool ReadBlock() = 0; 103 virtual bool ReadBlock() = 0;
100 virtual size_t SlowSeekForward(size_t code_unit_count) = 0; 104 virtual size_t SlowSeekForward(size_t code_unit_count) = 0;
101 105
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 position_ -= delta * (is_one_byte_ ? kOneByteSize : kUC16Size); 265 position_ -= delta * (is_one_byte_ ? kOneByteSize : kUC16Size);
262 } 266 }
263 267
264 void Reset() { 268 void Reset() {
265 position_ = 0; 269 position_ = 0;
266 is_one_byte_ = true; 270 is_one_byte_ = true;
267 } 271 }
268 272
269 Handle<String> Internalize(Isolate* isolate) const; 273 Handle<String> Internalize(Isolate* isolate) const;
270 274
275 void CopyFrom(const LiteralBuffer* other) {
276 if (other == nullptr) {
277 Reset();
278 } else {
279 is_one_byte_ = other->is_one_byte_;
280 position_ = other->position_;
281 backing_store_.Dispose();
282 backing_store_ = other->backing_store_.Clone();
283 }
284 }
285
271 private: 286 private:
272 static const int kInitialCapacity = 16; 287 static const int kInitialCapacity = 16;
273 static const int kGrowthFactory = 4; 288 static const int kGrowthFactory = 4;
274 static const int kMinConversionSlack = 256; 289 static const int kMinConversionSlack = 256;
275 static const int kMaxGrowth = 1 * MB; 290 static const int kMaxGrowth = 1 * MB;
276 inline int NewCapacity(int min_capacity) { 291 inline int NewCapacity(int min_capacity) {
277 int capacity = Max(min_capacity, backing_store_.length()); 292 int capacity = Max(min_capacity, backing_store_.length());
278 int new_capacity = Min(capacity * kGrowthFactory, capacity + kMaxGrowth); 293 int new_capacity = Min(capacity * kGrowthFactory, capacity + kMaxGrowth);
279 return new_capacity; 294 return new_capacity;
280 } 295 }
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 } 350 }
336 void Complete() { 351 void Complete() {
337 complete_ = true; 352 complete_ = true;
338 } 353 }
339 354
340 private: 355 private:
341 Scanner* scanner_; 356 Scanner* scanner_;
342 bool complete_; 357 bool complete_;
343 }; 358 };
344 359
360 // Scoped helper for a re-settable bookmark.
361 class BookmarkScope {
362 public:
363 explicit BookmarkScope(Scanner* scanner) : scanner_(scanner) {
364 DCHECK_NOT_NULL(scanner_);
365 }
366 ~BookmarkScope() { scanner_->DropBookmark(); }
367
368 bool Set() { return scanner_->SetBookmark(); }
369 bool Reset() { return scanner_->ResetToBookmark(); }
370 bool CanReset() { return scanner_->CanResetBookmark(); }
371 bool HasBeenSet() { return scanner_->BookmarkHasBeenSet(); }
372 bool HasBeenReset() { return scanner_->BookmarkHasBeenReset(); }
373
374 private:
375 Scanner* scanner_;
376 };
377
345 // Representation of an interval of source positions. 378 // Representation of an interval of source positions.
346 struct Location { 379 struct Location {
347 Location(int b, int e) : beg_pos(b), end_pos(e) { } 380 Location(int b, int e) : beg_pos(b), end_pos(e) { }
348 Location() : beg_pos(0), end_pos(0) { } 381 Location() : beg_pos(0), end_pos(0) { }
349 382
350 bool IsValid() const { 383 bool IsValid() const {
351 return beg_pos >= 0 && end_pos >= beg_pos; 384 return beg_pos >= 0 && end_pos >= beg_pos;
352 } 385 }
353 386
354 static Location invalid() { return Location(-1, -1); } 387 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. 535 // Call this after setting source_ to the input.
503 void Init() { 536 void Init() {
504 // Set c0_ (one character ahead) 537 // Set c0_ (one character ahead)
505 STATIC_ASSERT(kCharacterLookaheadBufferSize == 1); 538 STATIC_ASSERT(kCharacterLookaheadBufferSize == 1);
506 Advance(); 539 Advance();
507 // Initialize current_ to not refer to a literal. 540 // Initialize current_ to not refer to a literal.
508 current_.literal_chars = NULL; 541 current_.literal_chars = NULL;
509 current_.raw_literal_chars = NULL; 542 current_.raw_literal_chars = NULL;
510 } 543 }
511 544
545 // Support BookmarkScope functionality.
546 bool SetBookmark();
547 bool ResetToBookmark();
548 bool CanResetBookmark();
549 bool BookmarkHasBeenSet();
550 bool BookmarkHasBeenReset();
551 void DropBookmark();
552
512 // Literal buffer support 553 // Literal buffer support
513 inline void StartLiteral() { 554 inline void StartLiteral() {
514 LiteralBuffer* free_buffer = (current_.literal_chars == &literal_buffer1_) ? 555 LiteralBuffer* free_buffer = (current_.literal_chars == &literal_buffer1_) ?
515 &literal_buffer2_ : &literal_buffer1_; 556 &literal_buffer2_ : &literal_buffer1_;
516 free_buffer->Reset(); 557 free_buffer->Reset();
517 next_.literal_chars = free_buffer; 558 next_.literal_chars = free_buffer;
518 } 559 }
519 560
520 inline void StartRawLiteral() { 561 inline void StartRawLiteral() {
521 LiteralBuffer* free_buffer = 562 LiteralBuffer* free_buffer =
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
704 LiteralBuffer source_url_; 745 LiteralBuffer source_url_;
705 LiteralBuffer source_mapping_url_; 746 LiteralBuffer source_mapping_url_;
706 747
707 // Buffer to store raw string values 748 // Buffer to store raw string values
708 LiteralBuffer raw_literal_buffer1_; 749 LiteralBuffer raw_literal_buffer1_;
709 LiteralBuffer raw_literal_buffer2_; 750 LiteralBuffer raw_literal_buffer2_;
710 751
711 TokenDesc current_; // desc for current token (as returned by Next()) 752 TokenDesc current_; // desc for current token (as returned by Next())
712 TokenDesc next_; // desc for next token (one token look-ahead) 753 TokenDesc next_; // desc for next token (one token look-ahead)
713 754
755 // Variables for Scanner::BookmarkScope and the *Bookmark implementation.
756 // These variables contain the scanner state when a bookmark is set.
757 // We will use bookmark_c0_ as a 'control' variable, where:
758 // - bookmark_c0_ >= 0: A bookmark has been set and this contains c0_.
759 // - bookmark_c0_ == -1: No bookmark has been set.
760 // - bookmark_c0_ == -2: The bookmark has been applied (ResetToBookmark).
761 TokenDesc bookmark_current_;
762 TokenDesc bookmark_next_;
763 uc32 bookmark_c0_;
764 LiteralBuffer bookmark_literal_;
765 LiteralBuffer bookmark_raw_literal_;
marja 2015/04/22 16:40:56 Hmm, why can't we just retroactively recover all t
vogelheim 2015/04/22 17:08:35 Yeah... I went a bit trial-and-error here and didn
766
714 // Input stream. Must be initialized to an Utf16CharacterStream. 767 // Input stream. Must be initialized to an Utf16CharacterStream.
715 Utf16CharacterStream* source_; 768 Utf16CharacterStream* source_;
716 769
717 770
718 // Start position of the octal literal last scanned. 771 // Start position of the octal literal last scanned.
719 Location octal_pos_; 772 Location octal_pos_;
720 773
721 // Value of the last smi that was scanned. 774 // Value of the last smi that was scanned.
722 int smi_value_; 775 int smi_value_;
723 776
(...skipping 11 matching lines...) Expand all
735 bool harmony_modules_; 788 bool harmony_modules_;
736 // Whether we scan 'class', 'extends', 'static' and 'super' as keywords. 789 // Whether we scan 'class', 'extends', 'static' and 'super' as keywords.
737 bool harmony_classes_; 790 bool harmony_classes_;
738 // Whether we allow \u{xxxxx}. 791 // Whether we allow \u{xxxxx}.
739 bool harmony_unicode_; 792 bool harmony_unicode_;
740 }; 793 };
741 794
742 } } // namespace v8::internal 795 } } // namespace v8::internal
743 796
744 #endif // V8_SCANNER_H_ 797 #endif // V8_SCANNER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698