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 #include "src/parsing/scanner.h" | 7 #include "src/parsing/scanner.h" |
8 | 8 |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <cmath> | 11 #include <cmath> |
12 | 12 |
13 #include "src/ast/ast-value-factory.h" | 13 #include "src/ast/ast-value-factory.h" |
14 #include "src/char-predicates-inl.h" | 14 #include "src/char-predicates-inl.h" |
15 #include "src/conversions-inl.h" | 15 #include "src/conversions-inl.h" |
16 #include "src/list-inl.h" | 16 #include "src/list-inl.h" |
17 #include "src/parsing/duplicate-finder.h" // For Scanner::FindSymbol | 17 #include "src/parsing/duplicate-finder.h" // For Scanner::FindSymbol |
18 | 18 |
19 namespace v8 { | 19 namespace v8 { |
20 namespace internal { | 20 namespace internal { |
21 | 21 |
22 const size_t Utf16CharacterStream::kNoBookmark = | |
23 std::numeric_limits<size_t>::max(); | |
24 | |
25 Handle<String> Scanner::LiteralBuffer::Internalize(Isolate* isolate) const { | 22 Handle<String> Scanner::LiteralBuffer::Internalize(Isolate* isolate) const { |
26 if (is_one_byte()) { | 23 if (is_one_byte()) { |
27 return isolate->factory()->InternalizeOneByteString(one_byte_literal()); | 24 return isolate->factory()->InternalizeOneByteString(one_byte_literal()); |
28 } | 25 } |
29 return isolate->factory()->InternalizeTwoByteString(two_byte_literal()); | 26 return isolate->factory()->InternalizeTwoByteString(two_byte_literal()); |
30 } | 27 } |
31 | 28 |
29 // ---------------------------------------------------------------------------- | |
30 // Scanner::BookmarkScope | |
32 | 31 |
32 const size_t Scanner::BookmarkScope::kNoBookmark = | |
33 std::numeric_limits<size_t>::max() - 1; | |
34 const size_t Scanner::BookmarkScope::kBookmarkWasApplied = | |
35 std::numeric_limits<size_t>::max(); | |
36 | |
37 void Scanner::BookmarkScope::Set() { | |
38 DCHECK_EQ(bookmark_, kNoBookmark); | |
39 DCHECK_EQ(scanner_->next_next_.token, Token::UNINITIALIZED); | |
40 bookmark_ = scanner_->location().beg_pos; | |
41 } | |
42 | |
43 void Scanner::BookmarkScope::Reset() { | |
marja
2016/09/19 07:50:22
IMO "Reset" is not the correct name for this funct
vogelheim
2016/09/20 11:52:44
Done.
| |
44 DCHECK(HasBeenSet()); // Caller hasn't called SetBookmark. | |
45 scanner_->Seek(bookmark_); | |
46 bookmark_ = kBookmarkWasApplied; | |
47 } | |
48 | |
49 bool Scanner::BookmarkScope::HasBeenSet() { | |
50 return bookmark_ != kNoBookmark && bookmark_ != kBookmarkWasApplied; | |
51 } | |
52 | |
53 bool Scanner::BookmarkScope::HasBeenReset() { | |
54 return bookmark_ == kBookmarkWasApplied; | |
marja
2016/09/19 07:50:22
... here you already use the term "apply" :)
vogelheim
2016/09/20 11:52:44
Done.
| |
55 } | |
33 | 56 |
34 // ---------------------------------------------------------------------------- | 57 // ---------------------------------------------------------------------------- |
35 // Scanner | 58 // Scanner |
36 | 59 |
37 Scanner::Scanner(UnicodeCache* unicode_cache) | 60 Scanner::Scanner(UnicodeCache* unicode_cache) |
38 : unicode_cache_(unicode_cache), | 61 : unicode_cache_(unicode_cache), |
39 bookmark_c0_(kNoBookmark), | |
40 octal_pos_(Location::invalid()), | 62 octal_pos_(Location::invalid()), |
41 decimal_with_leading_zero_pos_(Location::invalid()), | 63 decimal_with_leading_zero_pos_(Location::invalid()), |
42 found_html_comment_(false) { | 64 found_html_comment_(false) { |
43 bookmark_current_.literal_chars = &bookmark_current_literal_; | |
44 bookmark_current_.raw_literal_chars = &bookmark_current_raw_literal_; | |
45 bookmark_next_.literal_chars = &bookmark_next_literal_; | |
46 bookmark_next_.raw_literal_chars = &bookmark_next_raw_literal_; | |
47 } | 65 } |
48 | 66 |
49 | 67 |
50 void Scanner::Initialize(Utf16CharacterStream* source) { | 68 void Scanner::Initialize(Utf16CharacterStream* source) { |
51 source_ = source; | 69 source_ = source; |
52 // Need to capture identifiers in order to recognize "get" and "set" | 70 // Need to capture identifiers in order to recognize "get" and "set" |
53 // in object literals. | 71 // in object literals. |
54 Init(); | 72 Init(); |
55 // Skip initial whitespace allowing HTML comment ends just like | 73 // Skip initial whitespace allowing HTML comment ends just like |
56 // after a newline and scan first token. | 74 // after a newline and scan first token. |
(...skipping 1520 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1577 | 1595 |
1578 int Scanner::FindSymbol(DuplicateFinder* finder, int value) { | 1596 int Scanner::FindSymbol(DuplicateFinder* finder, int value) { |
1579 // TODO(vogelheim): Move this logic into the calling class; this can be fully | 1597 // TODO(vogelheim): Move this logic into the calling class; this can be fully |
1580 // implemented using the public interface. | 1598 // implemented using the public interface. |
1581 if (is_literal_one_byte()) { | 1599 if (is_literal_one_byte()) { |
1582 return finder->AddOneByteSymbol(literal_one_byte_string(), value); | 1600 return finder->AddOneByteSymbol(literal_one_byte_string(), value); |
1583 } | 1601 } |
1584 return finder->AddTwoByteSymbol(literal_two_byte_string(), value); | 1602 return finder->AddTwoByteSymbol(literal_two_byte_string(), value); |
1585 } | 1603 } |
1586 | 1604 |
1605 void Scanner::Seek(size_t position) { | |
1606 // Use with care: This cleanly resets most, but not all scanner state. | |
1607 // TODO(vogelheim): Fix this, or at least DCHECK the relevant conditions. | |
1587 | 1608 |
1588 bool Scanner::SetBookmark() { | 1609 // To re-scan from a given character position, we need to: |
1589 if (c0_ != kNoBookmark && bookmark_c0_ == kNoBookmark && | 1610 // 1, Reset the next_ and next_next_ tokens (since they'll be used by Next(), |
1590 next_next_.token == Token::UNINITIALIZED && source_->SetBookmark()) { | 1611 next_.token = Token::UNINITIALIZED; |
1591 bookmark_c0_ = c0_; | 1612 next_next_.token = Token::UNINITIALIZED; |
1592 CopyTokenDesc(&bookmark_current_, ¤t_); | 1613 // 2, reset the source to the desired position, |
1593 CopyTokenDesc(&bookmark_next_, &next_); | 1614 source_->Seek(position); |
1594 return true; | 1615 // 3, re-scan, by scanning the look-ahead char + 2 tokens (current_ + next_). |
1595 } | 1616 c0_ = source_->Advance(); |
1596 return false; | 1617 Scan(); |
1618 Scan(); | |
1597 } | 1619 } |
1598 | 1620 |
1599 | |
1600 void Scanner::ResetToBookmark() { | |
1601 DCHECK(BookmarkHasBeenSet()); // Caller hasn't called SetBookmark. | |
1602 | |
1603 source_->ResetToBookmark(); | |
1604 c0_ = bookmark_c0_; | |
1605 CopyToNextTokenDesc(&bookmark_current_); | |
1606 current_ = next_; | |
1607 CopyToNextTokenDesc(&bookmark_next_); | |
1608 bookmark_c0_ = kBookmarkWasApplied; | |
1609 } | |
1610 | |
1611 | |
1612 bool Scanner::BookmarkHasBeenSet() { return bookmark_c0_ >= 0; } | |
1613 | |
1614 | |
1615 bool Scanner::BookmarkHasBeenReset() { | |
1616 return bookmark_c0_ == kBookmarkWasApplied; | |
1617 } | |
1618 | |
1619 | |
1620 void Scanner::DropBookmark() { bookmark_c0_ = kNoBookmark; } | |
1621 | |
1622 void Scanner::CopyToNextTokenDesc(TokenDesc* from) { | |
1623 StartLiteral(); | |
1624 StartRawLiteral(); | |
1625 CopyTokenDesc(&next_, from); | |
1626 if (next_.literal_chars->length() == 0) next_.literal_chars = nullptr; | |
1627 if (next_.raw_literal_chars->length() == 0) next_.raw_literal_chars = nullptr; | |
1628 } | |
1629 | |
1630 void Scanner::CopyTokenDesc(TokenDesc* to, TokenDesc* from) { | |
1631 DCHECK_NOT_NULL(to); | |
1632 DCHECK_NOT_NULL(from); | |
1633 to->token = from->token; | |
1634 to->location = from->location; | |
1635 to->literal_chars->CopyFrom(from->literal_chars); | |
1636 to->raw_literal_chars->CopyFrom(from->raw_literal_chars); | |
1637 } | |
1638 | |
1639 | |
1640 | |
1641 } // namespace internal | 1621 } // namespace internal |
1642 } // namespace v8 | 1622 } // namespace v8 |
OLD | NEW |