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 Handle<String> Scanner::LiteralBuffer::Internalize(Isolate* isolate) const { | 22 Handle<String> Scanner::LiteralBuffer::Internalize(Isolate* isolate) const { |
23 if (is_one_byte()) { | 23 if (is_one_byte()) { |
24 return isolate->factory()->InternalizeOneByteString(one_byte_literal()); | 24 return isolate->factory()->InternalizeOneByteString(one_byte_literal()); |
25 } | 25 } |
26 return isolate->factory()->InternalizeTwoByteString(two_byte_literal()); | 26 return isolate->factory()->InternalizeTwoByteString(two_byte_literal()); |
27 } | 27 } |
28 | 28 |
| 29 int Scanner::LiteralBuffer::NewCapacity(int min_capacity) { |
| 30 int capacity = Max(min_capacity, backing_store_.length()); |
| 31 int new_capacity = Min(capacity * kGrowthFactory, capacity + kMaxGrowth); |
| 32 return new_capacity; |
| 33 } |
| 34 |
| 35 void Scanner::LiteralBuffer::ExpandBuffer() { |
| 36 Vector<byte> new_store = Vector<byte>::New(NewCapacity(kInitialCapacity)); |
| 37 MemCopy(new_store.start(), backing_store_.start(), position_); |
| 38 backing_store_.Dispose(); |
| 39 backing_store_ = new_store; |
| 40 } |
| 41 |
| 42 void Scanner::LiteralBuffer::ConvertToTwoByte() { |
| 43 DCHECK(is_one_byte_); |
| 44 Vector<byte> new_store; |
| 45 int new_content_size = position_ * kUC16Size; |
| 46 if (new_content_size >= backing_store_.length()) { |
| 47 // Ensure room for all currently read code units as UC16 as well |
| 48 // as the code unit about to be stored. |
| 49 new_store = Vector<byte>::New(NewCapacity(new_content_size)); |
| 50 } else { |
| 51 new_store = backing_store_; |
| 52 } |
| 53 uint8_t* src = backing_store_.start(); |
| 54 uint16_t* dst = reinterpret_cast<uint16_t*>(new_store.start()); |
| 55 for (int i = position_ - 1; i >= 0; i--) { |
| 56 dst[i] = src[i]; |
| 57 } |
| 58 if (new_store.start() != backing_store_.start()) { |
| 59 backing_store_.Dispose(); |
| 60 backing_store_ = new_store; |
| 61 } |
| 62 position_ = new_content_size; |
| 63 is_one_byte_ = false; |
| 64 } |
| 65 |
| 66 void Scanner::LiteralBuffer::AddCharSlow(uc32 code_unit) { |
| 67 if (position_ >= backing_store_.length()) ExpandBuffer(); |
| 68 if (is_one_byte_) { |
| 69 if (code_unit <= static_cast<uc32>(unibrow::Latin1::kMaxChar)) { |
| 70 backing_store_[position_] = static_cast<byte>(code_unit); |
| 71 position_ += kOneByteSize; |
| 72 return; |
| 73 } |
| 74 ConvertToTwoByte(); |
| 75 } |
| 76 if (code_unit <= |
| 77 static_cast<uc32>(unibrow::Utf16::kMaxNonSurrogateCharCode)) { |
| 78 *reinterpret_cast<uint16_t*>(&backing_store_[position_]) = code_unit; |
| 79 position_ += kUC16Size; |
| 80 } else { |
| 81 *reinterpret_cast<uint16_t*>(&backing_store_[position_]) = |
| 82 unibrow::Utf16::LeadSurrogate(code_unit); |
| 83 position_ += kUC16Size; |
| 84 if (position_ >= backing_store_.length()) ExpandBuffer(); |
| 85 *reinterpret_cast<uint16_t*>(&backing_store_[position_]) = |
| 86 unibrow::Utf16::TrailSurrogate(code_unit); |
| 87 position_ += kUC16Size; |
| 88 } |
| 89 } |
| 90 |
29 // ---------------------------------------------------------------------------- | 91 // ---------------------------------------------------------------------------- |
30 // Scanner::BookmarkScope | 92 // Scanner::BookmarkScope |
31 | 93 |
32 const size_t Scanner::BookmarkScope::kBookmarkAtFirstPos = | 94 const size_t Scanner::BookmarkScope::kBookmarkAtFirstPos = |
33 std::numeric_limits<size_t>::max() - 2; | 95 std::numeric_limits<size_t>::max() - 2; |
34 const size_t Scanner::BookmarkScope::kNoBookmark = | 96 const size_t Scanner::BookmarkScope::kNoBookmark = |
35 std::numeric_limits<size_t>::max() - 1; | 97 std::numeric_limits<size_t>::max() - 1; |
36 const size_t Scanner::BookmarkScope::kBookmarkWasApplied = | 98 const size_t Scanner::BookmarkScope::kBookmarkWasApplied = |
37 std::numeric_limits<size_t>::max(); | 99 std::numeric_limits<size_t>::max(); |
38 | 100 |
(...skipping 1588 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1627 // 2, reset the source to the desired position, | 1689 // 2, reset the source to the desired position, |
1628 source_->Seek(position); | 1690 source_->Seek(position); |
1629 // 3, re-scan, by scanning the look-ahead char + 1 token (next_). | 1691 // 3, re-scan, by scanning the look-ahead char + 1 token (next_). |
1630 c0_ = source_->Advance(); | 1692 c0_ = source_->Advance(); |
1631 Next(); | 1693 Next(); |
1632 DCHECK_EQ(next_.location.beg_pos, static_cast<int>(position)); | 1694 DCHECK_EQ(next_.location.beg_pos, static_cast<int>(position)); |
1633 } | 1695 } |
1634 | 1696 |
1635 } // namespace internal | 1697 } // namespace internal |
1636 } // namespace v8 | 1698 } // namespace v8 |
OLD | NEW |