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_PARSING_SCANNER_H_ | 7 #ifndef V8_PARSING_SCANNER_H_ |
8 #define V8_PARSING_SCANNER_H_ | 8 #define V8_PARSING_SCANNER_H_ |
9 | 9 |
10 #include "src/allocation.h" | 10 #include "src/allocation.h" |
11 #include "src/base/hashmap.h" | |
12 #include "src/base/logging.h" | 11 #include "src/base/logging.h" |
13 #include "src/char-predicates.h" | 12 #include "src/char-predicates.h" |
14 #include "src/collector.h" | |
15 #include "src/globals.h" | 13 #include "src/globals.h" |
16 #include "src/list.h" | 14 #include "src/list.h" |
17 #include "src/messages.h" | 15 #include "src/messages.h" |
18 #include "src/parsing/token.h" | 16 #include "src/parsing/token.h" |
19 #include "src/unicode-decoder.h" | 17 #include "src/unicode-decoder.h" |
20 #include "src/unicode.h" | 18 #include "src/unicode.h" |
21 | 19 |
22 namespace v8 { | 20 namespace v8 { |
23 namespace internal { | 21 namespace internal { |
24 | 22 |
25 | 23 |
26 class AstRawString; | 24 class AstRawString; |
27 class AstValueFactory; | 25 class AstValueFactory; |
| 26 class DuplicateFinder; |
28 class ParserRecorder; | 27 class ParserRecorder; |
29 class UnicodeCache; | 28 class UnicodeCache; |
30 | 29 |
31 | 30 |
32 // --------------------------------------------------------------------- | 31 // --------------------------------------------------------------------- |
33 // Buffered stream of UTF-16 code units, using an internal UTF-16 buffer. | 32 // Buffered stream of UTF-16 code units, using an internal UTF-16 buffer. |
34 // A code unit is a 16 bit value representing either a 16 bit code point | 33 // A code unit is a 16 bit value representing either a 16 bit code point |
35 // or one part of a surrogate pair that make a single 21 bit code point. | 34 // or one part of a surrogate pair that make a single 21 bit code point. |
36 | 35 |
37 class Utf16CharacterStream { | 36 class Utf16CharacterStream { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 // are more code_units available, return true. | 91 // are more code_units available, return true. |
93 virtual bool ReadBlock() = 0; | 92 virtual bool ReadBlock() = 0; |
94 virtual size_t SlowSeekForward(size_t code_unit_count) = 0; | 93 virtual size_t SlowSeekForward(size_t code_unit_count) = 0; |
95 | 94 |
96 const uint16_t* buffer_cursor_; | 95 const uint16_t* buffer_cursor_; |
97 const uint16_t* buffer_end_; | 96 const uint16_t* buffer_end_; |
98 size_t pos_; | 97 size_t pos_; |
99 }; | 98 }; |
100 | 99 |
101 | 100 |
102 // --------------------------------------------------------------------- | |
103 // DuplicateFinder discovers duplicate symbols. | |
104 | |
105 class DuplicateFinder { | |
106 public: | |
107 explicit DuplicateFinder(UnicodeCache* constants) | |
108 : unicode_constants_(constants), | |
109 backing_store_(16), | |
110 map_(&Match) { } | |
111 | |
112 int AddOneByteSymbol(Vector<const uint8_t> key, int value); | |
113 int AddTwoByteSymbol(Vector<const uint16_t> key, int value); | |
114 // Add a a number literal by converting it (if necessary) | |
115 // to the string that ToString(ToNumber(literal)) would generate. | |
116 // and then adding that string with AddOneByteSymbol. | |
117 // This string is the actual value used as key in an object literal, | |
118 // and the one that must be different from the other keys. | |
119 int AddNumber(Vector<const uint8_t> key, int value); | |
120 | |
121 private: | |
122 int AddSymbol(Vector<const uint8_t> key, bool is_one_byte, int value); | |
123 // Backs up the key and its length in the backing store. | |
124 // The backup is stored with a base 127 encoding of the | |
125 // length (plus a bit saying whether the string is one byte), | |
126 // followed by the bytes of the key. | |
127 uint8_t* BackupKey(Vector<const uint8_t> key, bool is_one_byte); | |
128 | |
129 // Compare two encoded keys (both pointing into the backing store) | |
130 // for having the same base-127 encoded lengths and representation. | |
131 // and then having the same 'length' bytes following. | |
132 static bool Match(void* first, void* second); | |
133 // Creates a hash from a sequence of bytes. | |
134 static uint32_t Hash(Vector<const uint8_t> key, bool is_one_byte); | |
135 // Checks whether a string containing a JS number is its canonical | |
136 // form. | |
137 static bool IsNumberCanonical(Vector<const uint8_t> key); | |
138 | |
139 // Size of buffer. Sufficient for using it to call DoubleToCString in | |
140 // from conversions.h. | |
141 static const int kBufferSize = 100; | |
142 | |
143 UnicodeCache* unicode_constants_; | |
144 // Backing store used to store strings used as hashmap keys. | |
145 SequenceCollector<unsigned char> backing_store_; | |
146 base::HashMap map_; | |
147 // Buffer used for string->number->canonical string conversions. | |
148 char number_buffer_[kBufferSize]; | |
149 }; | |
150 | |
151 | |
152 // ---------------------------------------------------------------------------- | 101 // ---------------------------------------------------------------------------- |
153 // JavaScript Scanner. | 102 // JavaScript Scanner. |
154 | 103 |
155 class Scanner { | 104 class Scanner { |
156 public: | 105 public: |
157 // Scoped helper for a re-settable bookmark. | 106 // Scoped helper for a re-settable bookmark. |
158 class BookmarkScope { | 107 class BookmarkScope { |
159 public: | 108 public: |
160 explicit BookmarkScope(Scanner* scanner) : scanner_(scanner) { | 109 explicit BookmarkScope(Scanner* scanner) : scanner_(scanner) { |
161 DCHECK_NOT_NULL(scanner_); | 110 DCHECK_NOT_NULL(scanner_); |
(...skipping 682 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
844 bool found_html_comment_; | 793 bool found_html_comment_; |
845 | 794 |
846 MessageTemplate::Template scanner_error_; | 795 MessageTemplate::Template scanner_error_; |
847 Location scanner_error_location_; | 796 Location scanner_error_location_; |
848 }; | 797 }; |
849 | 798 |
850 } // namespace internal | 799 } // namespace internal |
851 } // namespace v8 | 800 } // namespace v8 |
852 | 801 |
853 #endif // V8_PARSING_SCANNER_H_ | 802 #endif // V8_PARSING_SCANNER_H_ |
OLD | NEW |