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

Side by Side Diff: src/lexer/lexer.h

Issue 185653004: Experimental parser: merge to r19637 (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Created 6 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « src/lexer/experimental-scanner.cc ('k') | src/lexer/lexer.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 10 matching lines...) Expand all
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 #ifndef V8_LEXER_EXPERIMENTAL_SCANNER_H 28 #ifndef V8_LEXER_EXPERIMENTAL_SCANNER_H
29 #define V8_LEXER_EXPERIMENTAL_SCANNER_H 29 #define V8_LEXER_EXPERIMENTAL_SCANNER_H
30 30
31 #include "compiler.h" 31 #include <set>
32 #include "isolate.h" 32 #include "handles.h"
33 #include "scanner.h" // UnicodeCache. 33 #include "scanner.h"
34 #include "token.h"
35 #include "utils.h"
36 #include "v8stdint.h"
37 #include "char-predicates-inl.h"
38 34
39 namespace v8 { 35 namespace v8 {
40 namespace internal { 36 namespace internal {
41 37
42 class UnicodeCache; 38 class LexerBase;
43 39
44 // Base class for scanners for different encodings. The meat is the pure virtual 40 class LexerGCHandler {
45 // Scan() which each of them specializes. 41 public:
46 class ScannerBase { 42 explicit LexerGCHandler(Isolate* isolate) : isolate_(isolate) {}
43 void AddLexer(LexerBase* lexer);
44 void RemoveLexer(LexerBase* lexer);
45 void UpdateLexersAfterGC();
46
47 private:
48 Isolate* isolate_;
49 std::set<LexerBase*> lexers_;
50 };
51
52
53 class LexerBase {
47 public: 54 public:
48 struct Location { 55 struct Location {
49 Location(int b, int e) : beg_pos(b), end_pos(e) { } 56 Location(int b, int e) : beg_pos(b), end_pos(e) { }
50 Location() : beg_pos(0), end_pos(0) { } 57 Location() : beg_pos(0), end_pos(0) { }
51 58
52 bool IsValid() const { 59 bool IsValid() const {
53 return beg_pos >= 0 && end_pos >= beg_pos; 60 return beg_pos >= 0 && end_pos >= beg_pos;
54 } 61 }
55 62
56 static Location invalid() { return Location(-1, -1); } 63 static Location invalid() { return Location(-1, -1); }
57 64
58 int beg_pos; 65 int beg_pos;
59 int end_pos; 66 int end_pos;
60 }; 67 };
61 68
62 explicit ScannerBase(Isolate* isolate) 69 explicit LexerBase(UnicodeCache* unicode_cache)
63 : isolate_(isolate), 70 : unicode_cache_(unicode_cache),
64 unicode_cache_(isolate->unicode_cache()), 71 has_line_terminator_before_next_(true),
65 has_line_terminator_before_next_(true), 72 has_multiline_comment_before_next_(false),
66 has_multiline_comment_before_next_(false), 73 current_literal_(&literals_[0]),
67 current_literal_(&literals_[0]), 74 next_literal_(&literals_[1]),
68 next_literal_(&literals_[1]), 75 harmony_numeric_literals_(false),
69 harmony_numeric_literals_(false), 76 harmony_modules_(false),
70 harmony_modules_(false), 77 harmony_scoping_(false) {
71 harmony_scoping_(false) {
72 isolate->AddScanner(this);
73 } 78 }
74 79
75 virtual ~ScannerBase() { 80 virtual ~LexerBase();
76 isolate_->RemoveScanner(this); 81
82 // Returns the next token and advances input.
83 Token::Value Next();
84
85 // Returns the current token again.
86 Token::Value current_token() const { return current_.token; }
87
88 // Returns the location information for the current token
89 // (the token last returned by Next()).
90 Location location() const {
91 return Location(current_.beg_pos, current_.end_pos);
77 } 92 }
78 93
79 // Has to be called after creating the scanner and setting the flags. 94 // One token look-ahead (past the token returned by Next()).
80 virtual void Init() = 0; 95 Token::Value peek() const { return next_.token; }
96
97 Location peek_location() const {
98 return Location(next_.beg_pos, next_.end_pos);
99 }
81 100
82 // Seek forward to the given position. This operation works for simple cases 101 // Seek forward to the given position. This operation works for simple cases
83 // such as seeking forward until simple delimiter tokens, which is what it is 102 // such as seeking forward until simple delimiter tokens, which is what it is
84 // used for. After this call, we will have the token at the given position as 103 // used for. After this call, we will have the token at the given position as
85 // the "next" token. The "current" token will be invalid. FIXME: for utf-8, 104 // the "next" token. The "current" token will be invalid. FIXME: for utf-8,
86 // we need to decide if pos is counted in characters or in bytes. 105 // we need to decide if pos is counted in characters or in bytes.
87 virtual void SeekForward(int pos) = 0; 106 virtual void SeekForward(int pos) = 0;
107
88 virtual void SetEnd(int pos) = 0; 108 virtual void SetEnd(int pos) = 0;
89 109
90 // Scans the input as a regular expression pattern, previous character(s) must 110 // Scans the input as a regular expression pattern, previous character(s) must
91 // be /(=). Returns true if a pattern is scanned. FIXME: this won't work for 111 // be /(=). Returns true if a pattern is scanned. FIXME: this won't work for
92 // utf-8 newlines. 112 // utf-8 newlines.
93 virtual bool ScanRegExpPattern(bool seen_equal) = 0; 113 virtual bool ScanRegExpPattern(bool seen_equal) = 0;
114
94 // Returns true if regexp flags are scanned (always since flags can 115 // Returns true if regexp flags are scanned (always since flags can
95 // be empty). 116 // be empty).
96 virtual bool ScanRegExpFlags() = 0; 117 virtual bool ScanRegExpFlags() = 0;
97 118
98 // Returns the location of the last seen octal literal. 119 // // Returns the location of the last seen octal literal.
99 virtual Location octal_position() const = 0; 120 virtual Location octal_position() const = 0;
121
100 virtual void clear_octal_position() = 0; 122 virtual void clear_octal_position() = 0;
101 123
102 // Sets the raw string pointer based on the string handle. Needs to be called
103 // right after GC.
104 virtual void UpdateBufferBasedOnHandle() = 0;
105
106 // Returns the next token and advances input.
107 Token::Value Next() {
108 has_line_terminator_before_next_ = false;
109 has_multiline_comment_before_next_ = false;
110 current_ = next_;
111 std::swap(current_literal_, next_literal_);
112 Scan(); // Virtual! Will fill in next_.
113 return current_.token;
114 }
115
116 // Returns the current token again.
117 Token::Value current_token() { return current_.token; }
118
119 // Returns the location information for the current token
120 // (the token last returned by Next()).
121 Location location() {
122 return Location(current_.beg_pos, current_.end_pos);
123 }
124
125 // One token look-ahead (past the token returned by Next()).
126 Token::Value peek() const { return next_.token; }
127
128 Location peek_location() const {
129 return Location(next_.beg_pos, next_.end_pos);
130 }
131
132 UnicodeCache* unicode_cache() { return unicode_cache_; }
133
134 bool HarmonyScoping() const {
135 return harmony_scoping_;
136 }
137 void SetHarmonyScoping(bool scoping) {
138 harmony_scoping_ = scoping;
139 }
140 bool HarmonyModules() const {
141 return harmony_modules_;
142 }
143 void SetHarmonyModules(bool modules) {
144 harmony_modules_ = modules;
145 }
146 bool HarmonyNumericLiterals() const {
147 return harmony_numeric_literals_;
148 }
149 void SetHarmonyNumericLiterals(bool numeric_literals) {
150 harmony_numeric_literals_ = numeric_literals;
151 }
152
153 // Returns true if there was a line terminator before the peek'ed token, 124 // Returns true if there was a line terminator before the peek'ed token,
154 // possibly inside a multi-line comment. 125 // possibly inside a multi-line comment.
155 bool HasAnyLineTerminatorBeforeNext() const { 126 bool HasAnyLineTerminatorBeforeNext() const {
156 return has_line_terminator_before_next_ || 127 return has_line_terminator_before_next_ ||
157 has_multiline_comment_before_next_; 128 has_multiline_comment_before_next_;
158 } 129 }
159 130
160 Handle<String> GetLiteralSymbol() { 131 Handle<String> GetLiteralSymbol() {
161 EnsureCurrentLiteralIsValid(); 132 EnsureCurrentLiteralIsValid();
162 return InternalizeLiteral(current_literal_); 133 return InternalizeLiteral(current_literal_);
(...skipping 17 matching lines...) Expand all
180 Vector<const uc16> literal_utf16_string() { 151 Vector<const uc16> literal_utf16_string() {
181 EnsureCurrentLiteralIsValid(); 152 EnsureCurrentLiteralIsValid();
182 return current_literal_->utf16_string; 153 return current_literal_->utf16_string;
183 } 154 }
184 155
185 int literal_length() { 156 int literal_length() {
186 EnsureCurrentLiteralIsValid(); 157 EnsureCurrentLiteralIsValid();
187 return current_literal_->length; 158 return current_literal_->length;
188 } 159 }
189 160
190 // This should be is_onebyte or is_latin1; it doesn't mean ASCII for real.
191 bool is_literal_ascii() { 161 bool is_literal_ascii() {
192 EnsureCurrentLiteralIsValid(); 162 EnsureCurrentLiteralIsValid();
193 return current_literal_->is_ascii; 163 return current_literal_->is_ascii;
194 } 164 }
195 165
196 bool is_literal_contextual_keyword(Vector<const char> keyword) { 166 bool is_literal_contextual_keyword(Vector<const char> keyword) {
197 if (!is_literal_ascii()) return false; 167 if (!is_literal_ascii()) return false;
198 Vector<const char> literal = literal_ascii_string(); 168 Vector<const char> literal = literal_ascii_string();
199 return literal.length() == keyword.length() && 169 return literal.length() == keyword.length() &&
200 (memcmp(literal.start(), keyword.start(), literal.length()) == 0); 170 (memcmp(literal.start(), keyword.start(), literal.length()) == 0);
(...skipping 23 matching lines...) Expand all
224 return next_literal_->is_ascii; 194 return next_literal_->is_ascii;
225 } 195 }
226 196
227 bool is_next_contextual_keyword(Vector<const char> keyword) { 197 bool is_next_contextual_keyword(Vector<const char> keyword) {
228 if (!is_next_literal_ascii()) return false; 198 if (!is_next_literal_ascii()) return false;
229 Vector<const char> literal = next_literal_ascii_string(); 199 Vector<const char> literal = next_literal_ascii_string();
230 return literal.length() == keyword.length() && 200 return literal.length() == keyword.length() &&
231 (memcmp(literal.start(), keyword.start(), literal.length()) == 0); 201 (memcmp(literal.start(), keyword.start(), literal.length()) == 0);
232 } 202 }
233 203
204 bool HarmonyScoping() const {
205 return harmony_scoping_;
206 }
207
208 void SetHarmonyScoping(bool scoping) {
209 harmony_scoping_ = scoping;
210 }
211
212 bool HarmonyModules() const {
213 return harmony_modules_;
214 }
215
216 void SetHarmonyModules(bool modules) {
217 harmony_modules_ = modules;
218 }
219
220 bool HarmonyNumericLiterals() const {
221 return harmony_numeric_literals_;
222 }
223
224 void SetHarmonyNumericLiterals(bool numeric_literals) {
225 harmony_numeric_literals_ = numeric_literals;
226 }
227
228 UnicodeCache* unicode_cache() { return unicode_cache_; }
229
234 protected: 230 protected:
235 struct TokenDesc { 231 struct TokenDesc {
236 Token::Value token; 232 Token::Value token;
237 int beg_pos; 233 int beg_pos;
238 int end_pos; 234 int end_pos;
239 bool has_escapes; 235 bool has_escapes;
240 bool is_onebyte; 236 bool is_onebyte;
241 }; 237 };
242 238
243 struct LiteralDesc { 239 struct LiteralDesc {
244 int beg_pos; 240 int beg_pos;
245 bool is_ascii; 241 bool is_ascii;
246 bool is_in_buffer; 242 bool is_in_buffer;
247 int offset; 243 int offset;
248 int length; 244 int length;
249 Vector<const char> ascii_string; 245 Vector<const char> ascii_string;
250 Vector<const uc16> utf16_string; 246 Vector<const uc16> utf16_string;
251 LiteralBuffer buffer; 247 LiteralBuffer buffer;
252 LiteralDesc() : beg_pos(-1), is_ascii(false), is_in_buffer(false), 248 LiteralDesc() : beg_pos(-1), is_ascii(false), is_in_buffer(false),
253 offset(0), length(0) { } 249 offset(0), length(0) { }
254 bool Valid(int pos) { return beg_pos == pos; } 250 bool Valid(int pos) { return beg_pos == pos; }
255 }; 251 };
256 252
257 virtual void Scan() = 0; 253 virtual void Scan() = 0;
254
255 virtual void UpdateBufferBasedOnHandle() = 0;
258 virtual bool FillLiteral(const TokenDesc& token, LiteralDesc* literal) = 0; 256 virtual bool FillLiteral(const TokenDesc& token, LiteralDesc* literal) = 0;
257 virtual Handle<String> InternalizeLiteral(LiteralDesc* literal) = 0;
258 virtual Handle<String> AllocateLiteral(LiteralDesc* literal,
259 PretenureFlag tenured) = 0;
259 260
260 void ResetLiterals() { 261 void ResetLiterals() {
261 if (!current_literal_->is_in_buffer) current_literal_->beg_pos = -1; 262 if (!current_literal_->is_in_buffer) current_literal_->beg_pos = -1;
262 if (!next_literal_->is_in_buffer) next_literal_->beg_pos = -1; 263 if (!next_literal_->is_in_buffer) next_literal_->beg_pos = -1;
263 } 264 }
264 265
265 void EnsureCurrentLiteralIsValid() { 266 void EnsureCurrentLiteralIsValid() {
266 if (!current_literal_->Valid(current_.beg_pos)) { 267 if (!current_literal_->Valid(current_.beg_pos)) {
267 FillLiteral(current_, current_literal_); 268 FillLiteral(current_, current_literal_);
268 } 269 }
269 } 270 }
270 271
271 void EnsureNextLiteralIsValid() { 272 void EnsureNextLiteralIsValid() {
272 if (!next_literal_->Valid(next_.beg_pos)) { 273 if (!next_literal_->Valid(next_.beg_pos)) {
273 FillLiteral(next_, next_literal_); 274 FillLiteral(next_, next_literal_);
274 } 275 }
275 } 276 }
276 277
277 virtual Handle<String> InternalizeLiteral(LiteralDesc* literal) = 0;
278 virtual Handle<String> AllocateLiteral(LiteralDesc* literal,
279 PretenureFlag tenured) = 0;
280
281 Isolate* isolate_;
282 UnicodeCache* unicode_cache_; 278 UnicodeCache* unicode_cache_;
283 279
284 bool has_line_terminator_before_next_; 280 bool has_line_terminator_before_next_;
285 // Whether there is a multiline comment *with a line break* before the next 281 // Whether there is a multiline comment *with a line break* before the next
286 // token. 282 // token.
287 bool has_multiline_comment_before_next_; 283 bool has_multiline_comment_before_next_;
288 284
289 TokenDesc current_; // desc for current token (as returned by Next()) 285 TokenDesc current_; // desc for current token (as returned by Next())
290 TokenDesc next_; // desc for next token (one token look-ahead) 286 TokenDesc next_; // desc for next token (one token look-ahead)
291 287
292 LiteralDesc* current_literal_; 288 LiteralDesc* current_literal_;
293 LiteralDesc* next_literal_; 289 LiteralDesc* next_literal_;
294 LiteralDesc literals_[2]; 290 LiteralDesc literals_[2];
295 291
296 bool harmony_numeric_literals_; 292 bool harmony_numeric_literals_;
297 bool harmony_modules_; 293 bool harmony_modules_;
298 bool harmony_scoping_; 294 bool harmony_scoping_;
295
296 friend class Scanner;
297 friend class LexerGCHandler;
299 }; 298 };
300 299
301 300
302 template<typename Char> 301 template<typename Char>
303 class ExperimentalScanner : public ScannerBase { 302 class Lexer : public LexerBase {
304 public: 303 public:
305 explicit ExperimentalScanner( 304 Lexer(UnicodeCache* unicode_cache,
306 Handle<String> source, 305 Handle<String> source,
307 Isolate* isolate) 306 int start_position_,
308 : ScannerBase(isolate), 307 int end_position_);
309 source_handle_(source), 308 Lexer(UnicodeCache* unicode_cache, const Char* source_ptr, int length);
310 buffer_(NULL), 309 virtual ~Lexer();
311 buffer_end_(NULL),
312 start_(NULL),
313 cursor_(NULL),
314 last_octal_end_(NULL) {
315 ASSERT(source->IsFlat());
316 UpdateBufferBasedOnHandle();
317 current_.beg_pos = current_.end_pos = next_.beg_pos = next_.end_pos = 0;
318 }
319
320 virtual void Init() {
321 Scan();
322 }
323
324 virtual ~ExperimentalScanner() { }
325 310
326 virtual void SeekForward(int pos); 311 virtual void SeekForward(int pos);
327 virtual void SetEnd(int pos); 312 virtual void SetEnd(int pos);
328 virtual bool ScanRegExpPattern(bool seen_equal); 313 virtual bool ScanRegExpPattern(bool seen_equal);
329 virtual bool ScanRegExpFlags(); 314 virtual bool ScanRegExpFlags();
330 virtual Location octal_position() const; 315 virtual Location octal_position() const;
331 virtual void clear_octal_position() { 316 virtual void clear_octal_position() { last_octal_end_ = NULL; }
332 last_octal_end_ = NULL;
333 }
334
335 virtual void UpdateBufferBasedOnHandle() {
336 // We get a raw pointer from the Handle, but we also update it every time
337 // there is a GC, so it is safe.
338 DisallowHeapAllocation no_gc;
339 const Char* new_buffer = GetNewBufferBasedOnHandle();
340 if (new_buffer != buffer_) {
341 int start_offset = start_ - buffer_;
342 int cursor_offset = cursor_ - buffer_;
343 int last_octal_end_offset = last_octal_end_ - buffer_;
344 buffer_ = new_buffer;
345 buffer_end_ = buffer_ + source_handle_->length();
346 start_ = buffer_ + start_offset;
347 cursor_ = buffer_ + cursor_offset;
348 if (last_octal_end_ != NULL) {
349 last_octal_end_ = buffer_ + last_octal_end_offset;
350 }
351 ResetLiterals();
352 }
353 }
354 317
355 protected: 318 protected:
356 virtual void Scan(); 319 virtual void Scan();
357 320
358 const Char* GetNewBufferBasedOnHandle() const; 321 const Char* GetNewBufferBasedOnHandle() const;
322 virtual void UpdateBufferBasedOnHandle();
359 323
360 virtual bool FillLiteral(const TokenDesc& token, LiteralDesc* literal); 324 virtual bool FillLiteral(const TokenDesc& token, LiteralDesc* literal);
361 virtual Handle<String> InternalizeLiteral(LiteralDesc* literal); 325 virtual Handle<String> InternalizeLiteral(LiteralDesc* literal);
362 virtual Handle<String> AllocateLiteral(LiteralDesc* literal, 326 virtual Handle<String> AllocateLiteral(LiteralDesc* literal,
363 PretenureFlag tenured); 327 PretenureFlag tenured);
364 328
329 private:
330 uc32 ScanHexNumber(int length);
365 331
366 private:
367 bool ValidIdentifierPart() {
368 return unicode_cache_->IsIdentifierPart(ScanHexNumber(4));
369 }
370
371 bool ValidIdentifierStart() {
372 return unicode_cache_->IsIdentifierStart(ScanHexNumber(4));
373 }
374
375 uc32 ScanHexNumber(int length);
376 bool ScanLiteralUnicodeEscape(); 332 bool ScanLiteralUnicodeEscape();
377 333
378 const Char* ScanHexNumber(const Char* start, 334 const Char* ScanHexNumber(const Char* start,
379 const Char* end, 335 const Char* end,
380 uc32* result); 336 uc32* result);
381 const Char* ScanOctalEscape(const Char* start, 337 const Char* ScanOctalEscape(const Char* start,
382 const Char* end, 338 const Char* end,
383 uc32* result); 339 uc32* result);
384 const Char* ScanIdentifierUnicodeEscape(const Char* start, 340 const Char* ScanIdentifierUnicodeEscape(const Char* start,
385 const Char* end, 341 const Char* end,
386 uc32* result); 342 uc32* result);
387 const Char* ScanEscape(const Char* start, 343 const Char* ScanEscape(const Char* start,
388 const Char* end, 344 const Char* end,
389 LiteralBuffer* literal); 345 LiteralBuffer* literal);
390 346
391 // Returns true if the literal of the token can be represented as a 347 // Returns true if the literal of the token can be represented as a
392 // substring of the source. 348 // substring of the source.
393 bool IsSubstringOfSource(const TokenDesc& token); 349 bool IsSubstringOfSource(const TokenDesc& token);
394 350
395 bool CopyToLiteralBuffer(const Char* start, 351 bool CopyToLiteralBuffer(const Char* start,
396 const Char* end, 352 const Char* end,
397 const TokenDesc& token, 353 const TokenDesc& token,
398 LiteralDesc* literal); 354 LiteralDesc* literal);
399 355
400 Handle<String> source_handle_; 356 Isolate* isolate_;
357 const Handle<String> source_handle_;
358 const Char* const source_ptr_;
359 const int start_position_;
360 const int end_position_;
401 const Char* buffer_; 361 const Char* buffer_;
402 const Char* buffer_end_; 362 const Char* buffer_end_;
403 const Char* start_; 363 const Char* start_;
404 const Char* cursor_; 364 const Char* cursor_;
405 365
406 // Where we have seen the last octal number or an octal escape inside a 366 // Where we have seen the last octal number or an octal escape inside a
407 // string. Used by octal_position(). 367 // string. Used by octal_position().
408 const Char* last_octal_end_; 368 const Char* last_octal_end_;
409 }; 369 };
410 370
411 371
412 template<typename Char> 372 #ifdef V8_USE_GENERATED_LEXER
413 void ExperimentalScanner<Char>::SeekForward(int pos) {
414 cursor_ = buffer_ + pos;
415 start_ = cursor_;
416 has_line_terminator_before_next_ = false;
417 has_multiline_comment_before_next_ = false;
418 Scan(); // Fills in next_.
419 }
420 373
421 374
422 template<typename Char> 375 // Match old scanner interface.
423 void ExperimentalScanner<Char>::SetEnd(int pos) { 376 class Scanner {
424 buffer_end_ = buffer_ + pos; 377 public:
425 } 378 typedef LexerBase::Location Location;
379
380 explicit Scanner(UnicodeCache* unicode_cache);
381
382 ~Scanner() { delete lexer_; }
383
384 void Initialize(Utf16CharacterStream* source);
385
386 inline void SeekForward(int pos) { lexer_->SeekForward(pos); }
387
388 inline void SetEnd(int pos) { lexer_->SetEnd(pos); }
389
390 inline bool ScanRegExpPattern(bool seen_equal) {
391 return lexer_->ScanRegExpPattern(seen_equal);
392 }
393
394 inline bool ScanRegExpFlags() { return lexer_->ScanRegExpFlags(); }
395
396 inline Location octal_position() const { return lexer_->octal_position(); }
397
398 inline void clear_octal_position() { lexer_->clear_octal_position(); }
399
400 inline Token::Value Next() { return lexer_->Next(); }
401
402 inline Token::Value current_token() { return lexer_->current_token(); }
403
404 inline Location location() { return lexer_->location(); }
405
406 inline Token::Value peek() const { return lexer_->peek(); }
407
408 inline Location peek_location() const { return lexer_->peek_location(); }
409
410 inline UnicodeCache* unicode_cache() { return lexer_->unicode_cache(); }
411
412 inline bool HarmonyScoping() const {
413 return harmony_scoping_;
414 }
415
416 inline void SetHarmonyScoping(bool scoping) {
417 harmony_scoping_ = scoping;
418 SyncSettings();
419 }
420
421 inline bool HarmonyModules() const {
422 return harmony_modules_;
423 }
424
425 inline void SetHarmonyModules(bool modules) {
426 harmony_modules_ = modules;
427 SyncSettings();
428 }
429
430 inline bool HarmonyNumericLiterals() const {
431 return harmony_numeric_literals_;
432 }
433
434 inline void SetHarmonyNumericLiterals(bool numeric_literals) {
435 harmony_numeric_literals_ = numeric_literals;
436 SyncSettings();
437 }
438
439 inline bool HasAnyLineTerminatorBeforeNext() const {
440 return lexer_->HasAnyLineTerminatorBeforeNext();
441 }
442
443 inline Handle<String> GetLiteralSymbol() {
444 return lexer_->GetLiteralSymbol();
445 }
446
447 inline Handle<String> GetLiteralString(PretenureFlag tenured) {
448 return lexer_->GetLiteralString(tenured);
449 }
450
451 inline Handle<String> GetNextLiteralString(PretenureFlag tenured) {
452 return lexer_->GetNextLiteralString(tenured);
453 }
454
455 inline Vector<const char> literal_ascii_string() {
456 return lexer_->literal_ascii_string();
457 }
458
459 inline Vector<const uc16> literal_utf16_string() {
460 return lexer_->literal_utf16_string();
461 }
462
463 inline int literal_length() {
464 return lexer_->literal_length();
465 }
466
467 inline bool is_literal_ascii() {
468 return lexer_->is_literal_ascii();
469 }
470
471 inline bool is_literal_contextual_keyword(Vector<const char> keyword) {
472 return lexer_->is_literal_contextual_keyword(keyword);
473 }
474
475 inline bool literal_contains_escapes() const {
476 return lexer_->literal_contains_escapes();
477 }
478
479 inline Vector<const char> next_literal_ascii_string() {
480 return lexer_->next_literal_ascii_string();
481 }
482
483 inline Vector<const uc16> next_literal_utf16_string() {
484 return lexer_->next_literal_utf16_string();
485 }
486
487 inline int next_literal_length() {
488 return lexer_->next_literal_length();
489 }
490
491 inline bool is_next_literal_ascii() {
492 return lexer_->is_next_literal_ascii();
493 }
494
495 inline bool is_next_contextual_keyword(Vector<const char> keyword) {
496 return lexer_->is_next_contextual_keyword(keyword);
497 }
498
499 private:
500 void SyncSettings();
501
502 UnicodeCache* unicode_cache_;
503 LexerBase* lexer_;
504 bool harmony_numeric_literals_;
505 bool harmony_modules_;
506 bool harmony_scoping_;
507 };
426 508
427 509
428 template<typename Char> 510 #endif
429 bool ExperimentalScanner<Char>::ScanRegExpPattern(bool seen_equal) {
430 // Scan: ('/' | '/=') RegularExpressionBody '/' RegularExpressionFlags
431 bool in_character_class = false;
432 511
433 // Previous token is either '/' or '/=', in the second case, the
434 // pattern starts at =.
435 next_.beg_pos = next_.end_pos = (cursor_ - buffer_) - (seen_equal ? 1 : 0);
436
437 // Scan regular expression body: According to ECMA-262, 3rd, 7.8.5,
438 // the scanner should pass uninterpreted bodies to the RegExp
439 // constructor.
440 if (cursor_ >= buffer_end_) return false;
441
442 while (*cursor_ != '/' || in_character_class) {
443 if (unicode_cache_->IsLineTerminator(*cursor_)) return false;
444 if (*cursor_ == '\\') { // Escape sequence.
445 ++cursor_;
446 if (cursor_ >= buffer_end_ || unicode_cache_->IsLineTerminator(*cursor_))
447 return false;
448 ++cursor_;
449 if (cursor_ >= buffer_end_) return false;
450 // If the escape allows more characters, i.e., \x??, \u????, or \c?,
451 // only "safe" characters are allowed (letters, digits, underscore),
452 // otherwise the escape isn't valid and the invalid character has
453 // its normal meaning. I.e., we can just continue scanning without
454 // worrying whether the following characters are part of the escape
455 // or not, since any '/', '\\' or '[' is guaranteed to not be part
456 // of the escape sequence.
457
458 // TODO(896): At some point, parse RegExps more throughly to capture
459 // octal esacpes in strict mode.
460 } else { // Unescaped character.
461 if (*cursor_ == '[') in_character_class = true;
462 if (*cursor_ == ']') in_character_class = false;
463 if (++cursor_ >= buffer_end_) return false;
464 }
465 }
466 next_.end_pos = (cursor_ - buffer_);
467 ++cursor_; // consume '/'
468 return true;
469 }
470
471
472 template<typename Char>
473 bool ExperimentalScanner<Char>::ScanRegExpFlags() {
474 next_.beg_pos = cursor_ - buffer_;
475 // Scan regular expression flags.
476 while (cursor_ < buffer_end_ && unicode_cache_->IsIdentifierPart(*cursor_)) {
477 if (*cursor_ != '\\') {
478 if (++cursor_ >= buffer_end_) break;
479 } else {
480 if (!ScanLiteralUnicodeEscape()) break;
481 if (++cursor_ >= buffer_end_) break;
482 }
483 }
484 next_.end_pos = cursor_ - buffer_;
485 return true;
486 }
487
488
489 template<typename Char>
490 uc32 ExperimentalScanner<Char>::ScanHexNumber(int length) {
491 // We have seen \uXXXX, let's see what it is.
492 uc32 x = 0;
493 for (const Char* s = cursor_ - length; s != cursor_; ++s) {
494 int d = HexValue(*s);
495 if (d < 0) {
496 return -1;
497 }
498 x = x * 16 + d;
499 }
500 return x;
501 }
502
503
504 template<typename Char>
505 const Char* ExperimentalScanner<Char>::ScanHexNumber(
506 const Char* cursor, const Char* end, uc32* result) {
507 uc32 x = 0;
508 for ( ; cursor < end; ++cursor) {
509 int d = HexValue(*cursor);
510 if (d < 0) {
511 *result = -1;
512 return NULL;
513 }
514 x = x * 16 + d;
515 }
516 *result = x;
517 return cursor;
518 }
519
520
521 // Octal escapes of the forms '\0xx' and '\xxx' are not a part of
522 // ECMA-262. Other JS VMs support them.
523 template<typename Char>
524 const Char* ExperimentalScanner<Char>::ScanOctalEscape(
525 const Char* start, const Char* end, uc32* result) {
526 uc32 x = *result - '0';
527 const Char* cursor;
528 for (cursor = start; cursor < end; cursor++) {
529 int d = *cursor - '0';
530 if (d < 0 || d > 7) break;
531 int nx = x * 8 + d;
532 if (nx >= 256) break;
533 x = nx;
534 }
535 *result = x;
536 return cursor;
537 }
538
539
540 template<typename Char>
541 bool ExperimentalScanner<Char>::ScanLiteralUnicodeEscape() {
542 ASSERT(cursor_ < buffer_end_);
543 Char primary_char = *(cursor_);
544 ASSERT(primary_char == '\\');
545 if (++cursor_ >= buffer_end_) return false;
546 primary_char = *(cursor_);
547 int i = 1;
548 if (primary_char == 'u') {
549 i++;
550 while (i < 6) {
551 if (++cursor_ >= buffer_end_) return false;
552 primary_char = *(cursor_);
553 if (!IsHexDigit(primary_char)) break;
554 i++;
555 }
556 }
557 return i == 6;
558 }
559
560
561 template<typename Char>
562 const Char* ExperimentalScanner<Char>::ScanIdentifierUnicodeEscape(
563 const Char* cursor, const Char* end, uc32* result) {
564 ASSERT(*cursor == '\\');
565 if (++cursor >= end) return NULL;
566 if (*cursor != 'u') return NULL;
567 ++cursor;
568 if (cursor + 4 > end) return NULL;
569 cursor = ScanHexNumber(cursor, cursor + 4, result);
570 return cursor;
571 }
572
573
574 template<typename Char>
575 const Char* ExperimentalScanner<Char>::ScanEscape(
576 const Char* cursor, const Char* end, LiteralBuffer* literal) {
577 ASSERT(*cursor == '\\');
578 if (++cursor >= end) return NULL;
579 uc32 c = *cursor;
580 if (++cursor > end) return NULL;
581 // Skip escaped newlines.
582 if (unicode_cache_->IsLineTerminator(c)) {
583 uc32 peek = *cursor;
584 // Allow CR+LF newlines in multiline string literals.
585 if (IsCarriageReturn(c) && IsLineFeed(peek)) cursor++;
586 // Allow LF+CR newlines in multiline string literals.
587 if (IsLineFeed(c) && IsCarriageReturn(peek)) cursor++;
588 return cursor;
589 }
590
591 switch (c) {
592 case '\'': // fall through
593 case '"' : // fall through
594 case '\\': break;
595 case 'b' : c = '\b'; break;
596 case 'f' : c = '\f'; break;
597 case 'n' : c = '\n'; break;
598 case 'r' : c = '\r'; break;
599 case 't' : c = '\t'; break;
600 case 'u' : {
601 ASSERT(cursor + 4 <= end);
602 cursor = ScanHexNumber(cursor, cursor + 4, &c);
603 if (cursor == NULL) return NULL;
604 break;
605 }
606 case 'v' : c = '\v'; break;
607 case 'x' : {
608 ASSERT(cursor + 2 <= end);
609 cursor = ScanHexNumber(cursor, cursor + 2, &c);
610 if (cursor == NULL) return NULL;
611 break;
612 }
613 case '0' : // fall through
614 case '1' : // fall through
615 case '2' : // fall through
616 case '3' : // fall through
617 case '4' : // fall through
618 case '5' : // fall through
619 case '6' : // fall through
620 case '7' :
621 if (end > cursor + 2) end = cursor + 2;
622 cursor = ScanOctalEscape(cursor, end, &c); break;
623 }
624
625 // According to ECMA-262, section 7.8.4, characters not covered by the
626 // above cases should be illegal, but they are commonly handled as
627 // non-escaped characters by JS VMs.
628 literal->AddChar(c);
629 return cursor;
630 }
631
632
633 template<typename Char>
634 ScannerBase::Location ExperimentalScanner<Char>::octal_position() const {
635 if (!last_octal_end_)
636 return Location::invalid();
637 // The last octal might be an octal escape or an octal number. Whichever it
638 // is, we'll find the start by just scanning back until we hit a non-octal
639 // character.
640 const Char* temp_cursor = last_octal_end_ - 1;
641 while (temp_cursor >= buffer_ && *temp_cursor >= '0' && *temp_cursor <= '7')
642 --temp_cursor;
643 return Location(temp_cursor - buffer_ + 1, last_octal_end_ - buffer_);
644 }
645 512
646 } } 513 } }
647 514
648 #endif // V8_LEXER_EXPERIMENTAL_SCANNER_H 515 #endif // V8_LEXER_EXPERIMENTAL_SCANNER_H
OLDNEW
« no previous file with comments | « src/lexer/experimental-scanner.cc ('k') | src/lexer/lexer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698