blob: 2ecbfd2a95be8281c3bd38f93c3720ca0df55318 [file] [log] [blame]
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00001// Copyright 2011 the V8 project authors. All rights reserved.
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
27
28// Features shared by parsing and pre-parsing scanners.
29
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +000030#include "../include/v8stdint.h"
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000031#include "scanner-base.h"
vegorov@chromium.org21b5e952010-11-23 10:24:40 +000032#include "char-predicates-inl.h"
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000033
34namespace v8 {
35namespace internal {
36
37// ----------------------------------------------------------------------------
vegorov@chromium.org21b5e952010-11-23 10:24:40 +000038// Scanner
39
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000040Scanner::Scanner(UnicodeCache* unicode_cache)
lrn@chromium.org1c092762011-05-09 09:42:16 +000041 : unicode_cache_(unicode_cache) { }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +000042
43
44uc32 Scanner::ScanHexEscape(uc32 c, int length) {
45 ASSERT(length <= 4); // prevent overflow
46
47 uc32 digits[4];
48 uc32 x = 0;
49 for (int i = 0; i < length; i++) {
50 digits[i] = c0_;
51 int d = HexValue(c0_);
52 if (d < 0) {
53 // According to ECMA-262, 3rd, 7.8.4, page 18, these hex escapes
54 // should be illegal, but other JS VMs just return the
55 // non-escaped version of the original character.
56
57 // Push back digits read, except the last one (in c0_).
58 for (int j = i-1; j >= 0; j--) {
59 PushBack(digits[j]);
60 }
61 // Notice: No handling of error - treat it as "\u"->"u".
62 return c;
63 }
64 x = x * 16 + d;
65 Advance();
66 }
67
68 return x;
69}
70
71
vegorov@chromium.org21b5e952010-11-23 10:24:40 +000072
73// ----------------------------------------------------------------------------
74// JavaScriptScanner
75
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000076JavaScriptScanner::JavaScriptScanner(UnicodeCache* scanner_contants)
danno@chromium.orgb6451162011-08-17 14:33:23 +000077 : Scanner(scanner_contants),
78 octal_pos_(Location::invalid()),
79 harmony_block_scoping_(false) { }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +000080
81
lrn@chromium.orgac2828d2011-06-23 06:29:21 +000082void JavaScriptScanner::Initialize(UC16CharacterStream* source) {
83 source_ = source;
84 // Need to capture identifiers in order to recognize "get" and "set"
85 // in object literals.
86 Init();
87 // Skip initial whitespace allowing HTML comment ends just like
88 // after a newline and scan first token.
89 has_line_terminator_before_next_ = true;
90 SkipWhiteSpace();
91 Scan();
92}
93
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +000094
95// Ensure that tokens can be stored in a byte.
96STATIC_ASSERT(Token::NUM_TOKENS <= 0x100);
97
98// Table of one-character tokens, by character (0x00..0x7f only).
99static const byte one_char_tokens[] = {
100 Token::ILLEGAL,
101 Token::ILLEGAL,
102 Token::ILLEGAL,
103 Token::ILLEGAL,
104 Token::ILLEGAL,
105 Token::ILLEGAL,
106 Token::ILLEGAL,
107 Token::ILLEGAL,
108 Token::ILLEGAL,
109 Token::ILLEGAL,
110 Token::ILLEGAL,
111 Token::ILLEGAL,
112 Token::ILLEGAL,
113 Token::ILLEGAL,
114 Token::ILLEGAL,
115 Token::ILLEGAL,
116 Token::ILLEGAL,
117 Token::ILLEGAL,
118 Token::ILLEGAL,
119 Token::ILLEGAL,
120 Token::ILLEGAL,
121 Token::ILLEGAL,
122 Token::ILLEGAL,
123 Token::ILLEGAL,
124 Token::ILLEGAL,
125 Token::ILLEGAL,
126 Token::ILLEGAL,
127 Token::ILLEGAL,
128 Token::ILLEGAL,
129 Token::ILLEGAL,
130 Token::ILLEGAL,
131 Token::ILLEGAL,
132 Token::ILLEGAL,
133 Token::ILLEGAL,
134 Token::ILLEGAL,
135 Token::ILLEGAL,
136 Token::ILLEGAL,
137 Token::ILLEGAL,
138 Token::ILLEGAL,
139 Token::ILLEGAL,
140 Token::LPAREN, // 0x28
141 Token::RPAREN, // 0x29
142 Token::ILLEGAL,
143 Token::ILLEGAL,
144 Token::COMMA, // 0x2c
145 Token::ILLEGAL,
146 Token::ILLEGAL,
147 Token::ILLEGAL,
148 Token::ILLEGAL,
149 Token::ILLEGAL,
150 Token::ILLEGAL,
151 Token::ILLEGAL,
152 Token::ILLEGAL,
153 Token::ILLEGAL,
154 Token::ILLEGAL,
155 Token::ILLEGAL,
156 Token::ILLEGAL,
157 Token::ILLEGAL,
158 Token::COLON, // 0x3a
159 Token::SEMICOLON, // 0x3b
160 Token::ILLEGAL,
161 Token::ILLEGAL,
162 Token::ILLEGAL,
163 Token::CONDITIONAL, // 0x3f
164 Token::ILLEGAL,
165 Token::ILLEGAL,
166 Token::ILLEGAL,
167 Token::ILLEGAL,
168 Token::ILLEGAL,
169 Token::ILLEGAL,
170 Token::ILLEGAL,
171 Token::ILLEGAL,
172 Token::ILLEGAL,
173 Token::ILLEGAL,
174 Token::ILLEGAL,
175 Token::ILLEGAL,
176 Token::ILLEGAL,
177 Token::ILLEGAL,
178 Token::ILLEGAL,
179 Token::ILLEGAL,
180 Token::ILLEGAL,
181 Token::ILLEGAL,
182 Token::ILLEGAL,
183 Token::ILLEGAL,
184 Token::ILLEGAL,
185 Token::ILLEGAL,
186 Token::ILLEGAL,
187 Token::ILLEGAL,
188 Token::ILLEGAL,
189 Token::ILLEGAL,
190 Token::ILLEGAL,
191 Token::LBRACK, // 0x5b
192 Token::ILLEGAL,
193 Token::RBRACK, // 0x5d
194 Token::ILLEGAL,
195 Token::ILLEGAL,
196 Token::ILLEGAL,
197 Token::ILLEGAL,
198 Token::ILLEGAL,
199 Token::ILLEGAL,
200 Token::ILLEGAL,
201 Token::ILLEGAL,
202 Token::ILLEGAL,
203 Token::ILLEGAL,
204 Token::ILLEGAL,
205 Token::ILLEGAL,
206 Token::ILLEGAL,
207 Token::ILLEGAL,
208 Token::ILLEGAL,
209 Token::ILLEGAL,
210 Token::ILLEGAL,
211 Token::ILLEGAL,
212 Token::ILLEGAL,
213 Token::ILLEGAL,
214 Token::ILLEGAL,
215 Token::ILLEGAL,
216 Token::ILLEGAL,
217 Token::ILLEGAL,
218 Token::ILLEGAL,
219 Token::ILLEGAL,
220 Token::ILLEGAL,
221 Token::ILLEGAL,
222 Token::ILLEGAL,
223 Token::LBRACE, // 0x7b
224 Token::ILLEGAL,
225 Token::RBRACE, // 0x7d
226 Token::BIT_NOT, // 0x7e
227 Token::ILLEGAL
228};
229
230
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000231Token::Value JavaScriptScanner::Next() {
232 current_ = next_;
233 has_line_terminator_before_next_ = false;
whesse@chromium.orgdf8c03c2011-06-21 14:36:03 +0000234 has_multiline_comment_before_next_ = false;
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000235 if (static_cast<unsigned>(c0_) <= 0x7f) {
236 Token::Value token = static_cast<Token::Value>(one_char_tokens[c0_]);
237 if (token != Token::ILLEGAL) {
238 int pos = source_pos();
239 next_.token = token;
240 next_.location.beg_pos = pos;
241 next_.location.end_pos = pos + 1;
242 Advance();
243 return current_.token;
244 }
245 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000246 Scan();
247 return current_.token;
248}
249
250
251static inline bool IsByteOrderMark(uc32 c) {
252 // The Unicode value U+FFFE is guaranteed never to be assigned as a
253 // Unicode character; this implies that in a Unicode context the
254 // 0xFF, 0xFE byte pattern can only be interpreted as the U+FEFF
255 // character expressed in little-endian byte order (since it could
256 // not be a U+FFFE character expressed in big-endian byte
257 // order). Nevertheless, we check for it to be compatible with
258 // Spidermonkey.
259 return c == 0xFEFF || c == 0xFFFE;
260}
261
262
263bool JavaScriptScanner::SkipWhiteSpace() {
264 int start_position = source_pos();
265
266 while (true) {
267 // We treat byte-order marks (BOMs) as whitespace for better
268 // compatibility with Spidermonkey and other JavaScript engines.
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000269 while (unicode_cache_->IsWhiteSpace(c0_) || IsByteOrderMark(c0_)) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000270 // IsWhiteSpace() includes line terminators!
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000271 if (unicode_cache_->IsLineTerminator(c0_)) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000272 // Ignore line terminators, but remember them. This is necessary
273 // for automatic semicolon insertion.
274 has_line_terminator_before_next_ = true;
275 }
276 Advance();
277 }
278
279 // If there is an HTML comment end '-->' at the beginning of a
280 // line (with only whitespace in front of it), we treat the rest
281 // of the line as a comment. This is in line with the way
282 // SpiderMonkey handles it.
283 if (c0_ == '-' && has_line_terminator_before_next_) {
284 Advance();
285 if (c0_ == '-') {
286 Advance();
287 if (c0_ == '>') {
288 // Treat the rest of the line as a comment.
289 SkipSingleLineComment();
290 // Continue skipping white space after the comment.
291 continue;
292 }
293 PushBack('-'); // undo Advance()
294 }
295 PushBack('-'); // undo Advance()
296 }
297 // Return whether or not we skipped any characters.
298 return source_pos() != start_position;
299 }
300}
301
302
303Token::Value JavaScriptScanner::SkipSingleLineComment() {
304 Advance();
305
306 // The line terminator at the end of the line is not considered
307 // to be part of the single-line comment; it is recognized
308 // separately by the lexical grammar and becomes part of the
309 // stream of input elements for the syntactic grammar (see
whesse@chromium.org7b260152011-06-20 15:33:18 +0000310 // ECMA-262, section 7.4).
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000311 while (c0_ >= 0 && !unicode_cache_->IsLineTerminator(c0_)) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000312 Advance();
313 }
314
315 return Token::WHITESPACE;
316}
317
318
319Token::Value JavaScriptScanner::SkipMultiLineComment() {
320 ASSERT(c0_ == '*');
321 Advance();
322
323 while (c0_ >= 0) {
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000324 uc32 ch = c0_;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000325 Advance();
whesse@chromium.org7b260152011-06-20 15:33:18 +0000326 if (unicode_cache_->IsLineTerminator(ch)) {
327 // Following ECMA-262, section 7.4, a comment containing
328 // a newline will make the comment count as a line-terminator.
whesse@chromium.orgdf8c03c2011-06-21 14:36:03 +0000329 has_multiline_comment_before_next_ = true;
whesse@chromium.org7b260152011-06-20 15:33:18 +0000330 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000331 // If we have reached the end of the multi-line comment, we
332 // consume the '/' and insert a whitespace. This way all
whesse@chromium.org7b260152011-06-20 15:33:18 +0000333 // multi-line comments are treated as whitespace.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000334 if (ch == '*' && c0_ == '/') {
335 c0_ = ' ';
336 return Token::WHITESPACE;
337 }
338 }
339
340 // Unterminated multi-line comment.
341 return Token::ILLEGAL;
342}
343
344
345Token::Value JavaScriptScanner::ScanHtmlComment() {
346 // Check for <!-- comments.
347 ASSERT(c0_ == '!');
348 Advance();
349 if (c0_ == '-') {
350 Advance();
351 if (c0_ == '-') return SkipSingleLineComment();
352 PushBack('-'); // undo Advance()
353 }
354 PushBack('!'); // undo Advance()
355 ASSERT(c0_ == '!');
356 return Token::LT;
357}
358
359
360void JavaScriptScanner::Scan() {
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000361 next_.literal_chars = NULL;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000362 Token::Value token;
363 do {
364 // Remember the position of the next token
365 next_.location.beg_pos = source_pos();
366
367 switch (c0_) {
368 case ' ':
369 case '\t':
370 Advance();
371 token = Token::WHITESPACE;
372 break;
373
374 case '\n':
375 Advance();
376 has_line_terminator_before_next_ = true;
377 token = Token::WHITESPACE;
378 break;
379
380 case '"': case '\'':
381 token = ScanString();
382 break;
383
384 case '<':
385 // < <= << <<= <!--
386 Advance();
387 if (c0_ == '=') {
388 token = Select(Token::LTE);
389 } else if (c0_ == '<') {
390 token = Select('=', Token::ASSIGN_SHL, Token::SHL);
391 } else if (c0_ == '!') {
392 token = ScanHtmlComment();
393 } else {
394 token = Token::LT;
395 }
396 break;
397
398 case '>':
399 // > >= >> >>= >>> >>>=
400 Advance();
401 if (c0_ == '=') {
402 token = Select(Token::GTE);
403 } else if (c0_ == '>') {
404 // >> >>= >>> >>>=
405 Advance();
406 if (c0_ == '=') {
407 token = Select(Token::ASSIGN_SAR);
408 } else if (c0_ == '>') {
409 token = Select('=', Token::ASSIGN_SHR, Token::SHR);
410 } else {
411 token = Token::SAR;
412 }
413 } else {
414 token = Token::GT;
415 }
416 break;
417
418 case '=':
419 // = == ===
420 Advance();
421 if (c0_ == '=') {
422 token = Select('=', Token::EQ_STRICT, Token::EQ);
423 } else {
424 token = Token::ASSIGN;
425 }
426 break;
427
428 case '!':
429 // ! != !==
430 Advance();
431 if (c0_ == '=') {
432 token = Select('=', Token::NE_STRICT, Token::NE);
433 } else {
434 token = Token::NOT;
435 }
436 break;
437
438 case '+':
439 // + ++ +=
440 Advance();
441 if (c0_ == '+') {
442 token = Select(Token::INC);
443 } else if (c0_ == '=') {
444 token = Select(Token::ASSIGN_ADD);
445 } else {
446 token = Token::ADD;
447 }
448 break;
449
450 case '-':
451 // - -- --> -=
452 Advance();
453 if (c0_ == '-') {
454 Advance();
455 if (c0_ == '>' && has_line_terminator_before_next_) {
456 // For compatibility with SpiderMonkey, we skip lines that
457 // start with an HTML comment end '-->'.
458 token = SkipSingleLineComment();
459 } else {
460 token = Token::DEC;
461 }
462 } else if (c0_ == '=') {
463 token = Select(Token::ASSIGN_SUB);
464 } else {
465 token = Token::SUB;
466 }
467 break;
468
469 case '*':
470 // * *=
471 token = Select('=', Token::ASSIGN_MUL, Token::MUL);
472 break;
473
474 case '%':
475 // % %=
476 token = Select('=', Token::ASSIGN_MOD, Token::MOD);
477 break;
478
479 case '/':
480 // / // /* /=
481 Advance();
482 if (c0_ == '/') {
483 token = SkipSingleLineComment();
484 } else if (c0_ == '*') {
485 token = SkipMultiLineComment();
486 } else if (c0_ == '=') {
487 token = Select(Token::ASSIGN_DIV);
488 } else {
489 token = Token::DIV;
490 }
491 break;
492
493 case '&':
494 // & && &=
495 Advance();
496 if (c0_ == '&') {
497 token = Select(Token::AND);
498 } else if (c0_ == '=') {
499 token = Select(Token::ASSIGN_BIT_AND);
500 } else {
501 token = Token::BIT_AND;
502 }
503 break;
504
505 case '|':
506 // | || |=
507 Advance();
508 if (c0_ == '|') {
509 token = Select(Token::OR);
510 } else if (c0_ == '=') {
511 token = Select(Token::ASSIGN_BIT_OR);
512 } else {
513 token = Token::BIT_OR;
514 }
515 break;
516
517 case '^':
518 // ^ ^=
519 token = Select('=', Token::ASSIGN_BIT_XOR, Token::BIT_XOR);
520 break;
521
522 case '.':
523 // . Number
524 Advance();
525 if (IsDecimalDigit(c0_)) {
526 token = ScanNumber(true);
527 } else {
528 token = Token::PERIOD;
529 }
530 break;
531
532 case ':':
533 token = Select(Token::COLON);
534 break;
535
536 case ';':
537 token = Select(Token::SEMICOLON);
538 break;
539
540 case ',':
541 token = Select(Token::COMMA);
542 break;
543
544 case '(':
545 token = Select(Token::LPAREN);
546 break;
547
548 case ')':
549 token = Select(Token::RPAREN);
550 break;
551
552 case '[':
553 token = Select(Token::LBRACK);
554 break;
555
556 case ']':
557 token = Select(Token::RBRACK);
558 break;
559
560 case '{':
561 token = Select(Token::LBRACE);
562 break;
563
564 case '}':
565 token = Select(Token::RBRACE);
566 break;
567
568 case '?':
569 token = Select(Token::CONDITIONAL);
570 break;
571
572 case '~':
573 token = Select(Token::BIT_NOT);
574 break;
575
576 default:
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000577 if (unicode_cache_->IsIdentifierStart(c0_)) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000578 token = ScanIdentifierOrKeyword();
579 } else if (IsDecimalDigit(c0_)) {
580 token = ScanNumber(false);
581 } else if (SkipWhiteSpace()) {
582 token = Token::WHITESPACE;
583 } else if (c0_ < 0) {
584 token = Token::EOS;
585 } else {
586 token = Select(Token::ILLEGAL);
587 }
588 break;
589 }
590
591 // Continue scanning for tokens as long as we're just skipping
592 // whitespace.
593 } while (token == Token::WHITESPACE);
594
595 next_.location.end_pos = source_pos();
596 next_.token = token;
597}
598
599
600void JavaScriptScanner::SeekForward(int pos) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000601 // After this call, we will have the token at the given position as
602 // the "next" token. The "current" token will be invalid.
603 if (pos == next_.location.beg_pos) return;
604 int current_pos = source_pos();
605 ASSERT_EQ(next_.location.end_pos, current_pos);
606 // Positions inside the lookahead token aren't supported.
607 ASSERT(pos >= current_pos);
608 if (pos != current_pos) {
609 source_->SeekForward(pos - source_->pos());
610 Advance();
611 // This function is only called to seek to the location
612 // of the end of a function (at the "}" token). It doesn't matter
613 // whether there was a line terminator in the part we skip.
614 has_line_terminator_before_next_ = false;
whesse@chromium.orgdf8c03c2011-06-21 14:36:03 +0000615 has_multiline_comment_before_next_ = false;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000616 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000617 Scan();
618}
619
620
621void JavaScriptScanner::ScanEscape() {
622 uc32 c = c0_;
623 Advance();
624
625 // Skip escaped newlines.
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000626 if (unicode_cache_->IsLineTerminator(c)) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000627 // Allow CR+LF newlines in multiline string literals.
628 if (IsCarriageReturn(c) && IsLineFeed(c0_)) Advance();
629 // Allow LF+CR newlines in multiline string literals.
630 if (IsLineFeed(c) && IsCarriageReturn(c0_)) Advance();
631 return;
632 }
633
634 switch (c) {
635 case '\'': // fall through
636 case '"' : // fall through
637 case '\\': break;
638 case 'b' : c = '\b'; break;
639 case 'f' : c = '\f'; break;
640 case 'n' : c = '\n'; break;
641 case 'r' : c = '\r'; break;
642 case 't' : c = '\t'; break;
643 case 'u' : c = ScanHexEscape(c, 4); break;
644 case 'v' : c = '\v'; break;
645 case 'x' : c = ScanHexEscape(c, 2); break;
646 case '0' : // fall through
647 case '1' : // fall through
648 case '2' : // fall through
649 case '3' : // fall through
650 case '4' : // fall through
651 case '5' : // fall through
652 case '6' : // fall through
653 case '7' : c = ScanOctalEscape(c, 2); break;
654 }
655
656 // According to ECMA-262, 3rd, 7.8.4 (p 18ff) these
657 // should be illegal, but they are commonly handled
658 // as non-escaped characters by JS VMs.
659 AddLiteralChar(c);
660}
661
662
lrn@chromium.org1c092762011-05-09 09:42:16 +0000663// Octal escapes of the forms '\0xx' and '\xxx' are not a part of
664// ECMA-262. Other JS VMs support them.
665uc32 JavaScriptScanner::ScanOctalEscape(uc32 c, int length) {
666 uc32 x = c - '0';
667 int i = 0;
668 for (; i < length; i++) {
669 int d = c0_ - '0';
670 if (d < 0 || d > 7) break;
671 int nx = x * 8 + d;
672 if (nx >= 256) break;
673 x = nx;
674 Advance();
675 }
676 // Anything except '\0' is an octal escape sequence, illegal in strict mode.
677 // Remember the position of octal escape sequences so that an error
678 // can be reported later (in strict mode).
679 // We don't report the error immediately, because the octal escape can
680 // occur before the "use strict" directive.
681 if (c != '0' || i > 0) {
682 octal_pos_ = Location(source_pos() - i - 1, source_pos() - 1);
683 }
684 return x;
685}
686
687
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000688Token::Value JavaScriptScanner::ScanString() {
689 uc32 quote = c0_;
690 Advance(); // consume quote
691
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000692 LiteralScope literal(this);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000693 while (c0_ != quote && c0_ >= 0
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000694 && !unicode_cache_->IsLineTerminator(c0_)) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000695 uc32 c = c0_;
696 Advance();
697 if (c == '\\') {
698 if (c0_ < 0) return Token::ILLEGAL;
699 ScanEscape();
700 } else {
701 AddLiteralChar(c);
702 }
703 }
704 if (c0_ != quote) return Token::ILLEGAL;
705 literal.Complete();
706
707 Advance(); // consume quote
708 return Token::STRING;
709}
710
711
712void JavaScriptScanner::ScanDecimalDigits() {
713 while (IsDecimalDigit(c0_))
714 AddLiteralCharAdvance();
715}
716
717
718Token::Value JavaScriptScanner::ScanNumber(bool seen_period) {
719 ASSERT(IsDecimalDigit(c0_)); // the first digit of the number or the fraction
720
721 enum { DECIMAL, HEX, OCTAL } kind = DECIMAL;
722
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000723 LiteralScope literal(this);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000724 if (seen_period) {
725 // we have already seen a decimal point of the float
726 AddLiteralChar('.');
727 ScanDecimalDigits(); // we know we have at least one digit
728
729 } else {
730 // if the first character is '0' we must check for octals and hex
731 if (c0_ == '0') {
lrn@chromium.org1c092762011-05-09 09:42:16 +0000732 int start_pos = source_pos(); // For reporting octal positions.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000733 AddLiteralCharAdvance();
734
735 // either 0, 0exxx, 0Exxx, 0.xxx, an octal number, or a hex number
736 if (c0_ == 'x' || c0_ == 'X') {
737 // hex number
738 kind = HEX;
739 AddLiteralCharAdvance();
740 if (!IsHexDigit(c0_)) {
741 // we must have at least one hex digit after 'x'/'X'
742 return Token::ILLEGAL;
743 }
744 while (IsHexDigit(c0_)) {
745 AddLiteralCharAdvance();
746 }
747 } else if ('0' <= c0_ && c0_ <= '7') {
748 // (possible) octal number
749 kind = OCTAL;
750 while (true) {
751 if (c0_ == '8' || c0_ == '9') {
752 kind = DECIMAL;
753 break;
754 }
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000755 if (c0_ < '0' || '7' < c0_) {
756 // Octal literal finished.
lrn@chromium.org1c092762011-05-09 09:42:16 +0000757 octal_pos_ = Location(start_pos, source_pos());
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000758 break;
759 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000760 AddLiteralCharAdvance();
761 }
762 }
763 }
764
765 // Parse decimal digits and allow trailing fractional part.
766 if (kind == DECIMAL) {
767 ScanDecimalDigits(); // optional
768 if (c0_ == '.') {
769 AddLiteralCharAdvance();
770 ScanDecimalDigits(); // optional
771 }
772 }
773 }
774
775 // scan exponent, if any
776 if (c0_ == 'e' || c0_ == 'E') {
777 ASSERT(kind != HEX); // 'e'/'E' must be scanned as part of the hex number
778 if (kind == OCTAL) return Token::ILLEGAL; // no exponent for octals allowed
779 // scan exponent
780 AddLiteralCharAdvance();
781 if (c0_ == '+' || c0_ == '-')
782 AddLiteralCharAdvance();
783 if (!IsDecimalDigit(c0_)) {
784 // we must have at least one decimal digit after 'e'/'E'
785 return Token::ILLEGAL;
786 }
787 ScanDecimalDigits();
788 }
789
790 // The source character immediately following a numeric literal must
791 // not be an identifier start or a decimal digit; see ECMA-262
792 // section 7.8.3, page 17 (note that we read only one decimal digit
793 // if the value is 0).
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000794 if (IsDecimalDigit(c0_) || unicode_cache_->IsIdentifierStart(c0_))
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000795 return Token::ILLEGAL;
796
797 literal.Complete();
798
799 return Token::NUMBER;
800}
801
802
803uc32 JavaScriptScanner::ScanIdentifierUnicodeEscape() {
804 Advance();
805 if (c0_ != 'u') return unibrow::Utf8::kBadChar;
806 Advance();
807 uc32 c = ScanHexEscape('u', 4);
808 // We do not allow a unicode escape sequence to start another
809 // unicode escape sequence.
810 if (c == '\\') return unibrow::Utf8::kBadChar;
811 return c;
812}
813
814
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000815// ----------------------------------------------------------------------------
816// Keyword Matcher
817
danno@chromium.orgb6451162011-08-17 14:33:23 +0000818#define KEYWORDS(KEYWORD_GROUP, KEYWORD) \
819 KEYWORD_GROUP('b') \
820 KEYWORD("break", Token::BREAK) \
821 KEYWORD_GROUP('c') \
822 KEYWORD("case", Token::CASE) \
823 KEYWORD("catch", Token::CATCH) \
824 KEYWORD("class", Token::FUTURE_RESERVED_WORD) \
825 KEYWORD("const", Token::CONST) \
826 KEYWORD("continue", Token::CONTINUE) \
827 KEYWORD_GROUP('d') \
828 KEYWORD("debugger", Token::DEBUGGER) \
829 KEYWORD("default", Token::DEFAULT) \
830 KEYWORD("delete", Token::DELETE) \
831 KEYWORD("do", Token::DO) \
832 KEYWORD_GROUP('e') \
833 KEYWORD("else", Token::ELSE) \
834 KEYWORD("enum", Token::FUTURE_RESERVED_WORD) \
835 KEYWORD("export", Token::FUTURE_RESERVED_WORD) \
836 KEYWORD("extends", Token::FUTURE_RESERVED_WORD) \
837 KEYWORD_GROUP('f') \
838 KEYWORD("false", Token::FALSE_LITERAL) \
839 KEYWORD("finally", Token::FINALLY) \
840 KEYWORD("for", Token::FOR) \
841 KEYWORD("function", Token::FUNCTION) \
842 KEYWORD_GROUP('i') \
843 KEYWORD("if", Token::IF) \
844 KEYWORD("implements", Token::FUTURE_STRICT_RESERVED_WORD) \
845 KEYWORD("import", Token::FUTURE_RESERVED_WORD) \
846 KEYWORD("in", Token::IN) \
847 KEYWORD("instanceof", Token::INSTANCEOF) \
848 KEYWORD("interface", Token::FUTURE_STRICT_RESERVED_WORD) \
849 KEYWORD_GROUP('l') \
850 KEYWORD("let", harmony_block_scoping \
851 ? Token::LET : Token::FUTURE_STRICT_RESERVED_WORD) \
852 KEYWORD_GROUP('n') \
853 KEYWORD("new", Token::NEW) \
854 KEYWORD("null", Token::NULL_LITERAL) \
855 KEYWORD_GROUP('p') \
856 KEYWORD("package", Token::FUTURE_STRICT_RESERVED_WORD) \
857 KEYWORD("private", Token::FUTURE_STRICT_RESERVED_WORD) \
858 KEYWORD("protected", Token::FUTURE_STRICT_RESERVED_WORD) \
859 KEYWORD("public", Token::FUTURE_STRICT_RESERVED_WORD) \
860 KEYWORD_GROUP('r') \
861 KEYWORD("return", Token::RETURN) \
862 KEYWORD_GROUP('s') \
863 KEYWORD("static", Token::FUTURE_STRICT_RESERVED_WORD) \
864 KEYWORD("super", Token::FUTURE_RESERVED_WORD) \
865 KEYWORD("switch", Token::SWITCH) \
866 KEYWORD_GROUP('t') \
867 KEYWORD("this", Token::THIS) \
868 KEYWORD("throw", Token::THROW) \
869 KEYWORD("true", Token::TRUE_LITERAL) \
870 KEYWORD("try", Token::TRY) \
871 KEYWORD("typeof", Token::TYPEOF) \
872 KEYWORD_GROUP('v') \
873 KEYWORD("var", Token::VAR) \
874 KEYWORD("void", Token::VOID) \
875 KEYWORD_GROUP('w') \
876 KEYWORD("while", Token::WHILE) \
877 KEYWORD("with", Token::WITH) \
878 KEYWORD_GROUP('y') \
879 KEYWORD("yield", Token::FUTURE_STRICT_RESERVED_WORD)
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000880
881
882static Token::Value KeywordOrIdentifierToken(const char* input,
danno@chromium.orgb6451162011-08-17 14:33:23 +0000883 int input_length,
884 bool harmony_block_scoping) {
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000885 ASSERT(input_length >= 1);
886 const int kMinLength = 2;
887 const int kMaxLength = 10;
888 if (input_length < kMinLength || input_length > kMaxLength) {
889 return Token::IDENTIFIER;
890 }
891 switch (input[0]) {
892 default:
893#define KEYWORD_GROUP_CASE(ch) \
894 break; \
895 case ch:
896#define KEYWORD(keyword, token) \
897 { \
898 /* 'keyword' is a char array, so sizeof(keyword) is */ \
899 /* strlen(keyword) plus 1 for the NUL char. */ \
900 const int keyword_length = sizeof(keyword) - 1; \
901 STATIC_ASSERT(keyword_length >= kMinLength); \
902 STATIC_ASSERT(keyword_length <= kMaxLength); \
903 if (input_length == keyword_length && \
904 input[1] == keyword[1] && \
905 (keyword_length <= 2 || input[2] == keyword[2]) && \
906 (keyword_length <= 3 || input[3] == keyword[3]) && \
907 (keyword_length <= 4 || input[4] == keyword[4]) && \
908 (keyword_length <= 5 || input[5] == keyword[5]) && \
909 (keyword_length <= 6 || input[6] == keyword[6]) && \
910 (keyword_length <= 7 || input[7] == keyword[7]) && \
911 (keyword_length <= 8 || input[8] == keyword[8]) && \
912 (keyword_length <= 9 || input[9] == keyword[9])) { \
danno@chromium.orgb6451162011-08-17 14:33:23 +0000913 return token; \
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000914 } \
915 }
916 KEYWORDS(KEYWORD_GROUP_CASE, KEYWORD)
917 }
918 return Token::IDENTIFIER;
919}
920
921
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000922Token::Value JavaScriptScanner::ScanIdentifierOrKeyword() {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000923 ASSERT(unicode_cache_->IsIdentifierStart(c0_));
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000924 LiteralScope literal(this);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000925 // Scan identifier start character.
926 if (c0_ == '\\') {
927 uc32 c = ScanIdentifierUnicodeEscape();
928 // Only allow legal identifier start characters.
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000929 if (!unicode_cache_->IsIdentifierStart(c)) return Token::ILLEGAL;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000930 AddLiteralChar(c);
931 return ScanIdentifierSuffix(&literal);
932 }
933
934 uc32 first_char = c0_;
935 Advance();
936 AddLiteralChar(first_char);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000937
938 // Scan the rest of the identifier characters.
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000939 while (unicode_cache_->IsIdentifierPart(c0_)) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000940 if (c0_ != '\\') {
941 uc32 next_char = c0_;
942 Advance();
943 AddLiteralChar(next_char);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000944 continue;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000945 }
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000946 // Fallthrough if no longer able to complete keyword.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000947 return ScanIdentifierSuffix(&literal);
948 }
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000949
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000950 literal.Complete();
951
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000952 if (next_.literal_chars->is_ascii()) {
953 Vector<const char> chars = next_.literal_chars->ascii_literal();
danno@chromium.orgb6451162011-08-17 14:33:23 +0000954 return KeywordOrIdentifierToken(chars.start(),
955 chars.length(),
956 harmony_block_scoping_);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000957 }
958
959 return Token::IDENTIFIER;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000960}
961
962
963Token::Value JavaScriptScanner::ScanIdentifierSuffix(LiteralScope* literal) {
964 // Scan the rest of the identifier characters.
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000965 while (unicode_cache_->IsIdentifierPart(c0_)) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000966 if (c0_ == '\\') {
967 uc32 c = ScanIdentifierUnicodeEscape();
968 // Only allow legal identifier part characters.
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000969 if (!unicode_cache_->IsIdentifierPart(c)) return Token::ILLEGAL;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000970 AddLiteralChar(c);
971 } else {
972 AddLiteralChar(c0_);
973 Advance();
974 }
975 }
976 literal->Complete();
977
978 return Token::IDENTIFIER;
979}
980
981
982bool JavaScriptScanner::ScanRegExpPattern(bool seen_equal) {
983 // Scan: ('/' | '/=') RegularExpressionBody '/' RegularExpressionFlags
984 bool in_character_class = false;
985
986 // Previous token is either '/' or '/=', in the second case, the
987 // pattern starts at =.
988 next_.location.beg_pos = source_pos() - (seen_equal ? 2 : 1);
989 next_.location.end_pos = source_pos() - (seen_equal ? 1 : 0);
990
991 // Scan regular expression body: According to ECMA-262, 3rd, 7.8.5,
992 // the scanner should pass uninterpreted bodies to the RegExp
993 // constructor.
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000994 LiteralScope literal(this);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000995 if (seen_equal)
996 AddLiteralChar('=');
997
998 while (c0_ != '/' || in_character_class) {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000999 if (unicode_cache_->IsLineTerminator(c0_) || c0_ < 0) return false;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001000 if (c0_ == '\\') { // Escape sequence.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001001 AddLiteralCharAdvance();
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00001002 if (unicode_cache_->IsLineTerminator(c0_) || c0_ < 0) return false;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001003 AddLiteralCharAdvance();
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001004 // If the escape allows more characters, i.e., \x??, \u????, or \c?,
1005 // only "safe" characters are allowed (letters, digits, underscore),
1006 // otherwise the escape isn't valid and the invalid character has
1007 // its normal meaning. I.e., we can just continue scanning without
1008 // worrying whether the following characters are part of the escape
1009 // or not, since any '/', '\\' or '[' is guaranteed to not be part
1010 // of the escape sequence.
lrn@chromium.org1c092762011-05-09 09:42:16 +00001011
1012 // TODO(896): At some point, parse RegExps more throughly to capture
1013 // octal esacpes in strict mode.
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001014 } else { // Unescaped character.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001015 if (c0_ == '[') in_character_class = true;
1016 if (c0_ == ']') in_character_class = false;
1017 AddLiteralCharAdvance();
1018 }
1019 }
1020 Advance(); // consume '/'
1021
1022 literal.Complete();
1023
1024 return true;
1025}
1026
1027
1028bool JavaScriptScanner::ScanRegExpFlags() {
1029 // Scan regular expression flags.
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +00001030 LiteralScope literal(this);
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00001031 while (unicode_cache_->IsIdentifierPart(c0_)) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001032 if (c0_ == '\\') {
1033 uc32 c = ScanIdentifierUnicodeEscape();
1034 if (c != static_cast<uc32>(unibrow::Utf8::kBadChar)) {
1035 // We allow any escaped character, unlike the restriction on
1036 // IdentifierPart when it is used to build an IdentifierName.
1037 AddLiteralChar(c);
1038 continue;
1039 }
1040 }
1041 AddLiteralCharAdvance();
1042 }
1043 literal.Complete();
1044
1045 next_.location.end_pos = source_pos() - 1;
1046 return true;
1047}
1048
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001049} } // namespace v8::internal