blob: e4590b1261ecfe6d2159fa45a0564b57008b1216 [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)
lrn@chromium.org1c092762011-05-09 09:42:16 +000077 : Scanner(scanner_contants), octal_pos_(Location::invalid()) { }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +000078
79
lrn@chromium.orgac2828d2011-06-23 06:29:21 +000080void JavaScriptScanner::Initialize(UC16CharacterStream* source) {
81 source_ = source;
82 // Need to capture identifiers in order to recognize "get" and "set"
83 // in object literals.
84 Init();
85 // Skip initial whitespace allowing HTML comment ends just like
86 // after a newline and scan first token.
87 has_line_terminator_before_next_ = true;
88 SkipWhiteSpace();
89 Scan();
90}
91
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +000092
93// Ensure that tokens can be stored in a byte.
94STATIC_ASSERT(Token::NUM_TOKENS <= 0x100);
95
96// Table of one-character tokens, by character (0x00..0x7f only).
97static const byte one_char_tokens[] = {
98 Token::ILLEGAL,
99 Token::ILLEGAL,
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::LPAREN, // 0x28
139 Token::RPAREN, // 0x29
140 Token::ILLEGAL,
141 Token::ILLEGAL,
142 Token::COMMA, // 0x2c
143 Token::ILLEGAL,
144 Token::ILLEGAL,
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::COLON, // 0x3a
157 Token::SEMICOLON, // 0x3b
158 Token::ILLEGAL,
159 Token::ILLEGAL,
160 Token::ILLEGAL,
161 Token::CONDITIONAL, // 0x3f
162 Token::ILLEGAL,
163 Token::ILLEGAL,
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::LBRACK, // 0x5b
190 Token::ILLEGAL,
191 Token::RBRACK, // 0x5d
192 Token::ILLEGAL,
193 Token::ILLEGAL,
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::LBRACE, // 0x7b
222 Token::ILLEGAL,
223 Token::RBRACE, // 0x7d
224 Token::BIT_NOT, // 0x7e
225 Token::ILLEGAL
226};
227
228
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000229Token::Value JavaScriptScanner::Next() {
230 current_ = next_;
231 has_line_terminator_before_next_ = false;
whesse@chromium.orgdf8c03c2011-06-21 14:36:03 +0000232 has_multiline_comment_before_next_ = false;
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000233 if (static_cast<unsigned>(c0_) <= 0x7f) {
234 Token::Value token = static_cast<Token::Value>(one_char_tokens[c0_]);
235 if (token != Token::ILLEGAL) {
236 int pos = source_pos();
237 next_.token = token;
238 next_.location.beg_pos = pos;
239 next_.location.end_pos = pos + 1;
240 Advance();
241 return current_.token;
242 }
243 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000244 Scan();
245 return current_.token;
246}
247
248
249static inline bool IsByteOrderMark(uc32 c) {
250 // The Unicode value U+FFFE is guaranteed never to be assigned as a
251 // Unicode character; this implies that in a Unicode context the
252 // 0xFF, 0xFE byte pattern can only be interpreted as the U+FEFF
253 // character expressed in little-endian byte order (since it could
254 // not be a U+FFFE character expressed in big-endian byte
255 // order). Nevertheless, we check for it to be compatible with
256 // Spidermonkey.
257 return c == 0xFEFF || c == 0xFFFE;
258}
259
260
261bool JavaScriptScanner::SkipWhiteSpace() {
262 int start_position = source_pos();
263
264 while (true) {
265 // We treat byte-order marks (BOMs) as whitespace for better
266 // compatibility with Spidermonkey and other JavaScript engines.
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000267 while (unicode_cache_->IsWhiteSpace(c0_) || IsByteOrderMark(c0_)) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000268 // IsWhiteSpace() includes line terminators!
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000269 if (unicode_cache_->IsLineTerminator(c0_)) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000270 // Ignore line terminators, but remember them. This is necessary
271 // for automatic semicolon insertion.
272 has_line_terminator_before_next_ = true;
273 }
274 Advance();
275 }
276
277 // If there is an HTML comment end '-->' at the beginning of a
278 // line (with only whitespace in front of it), we treat the rest
279 // of the line as a comment. This is in line with the way
280 // SpiderMonkey handles it.
281 if (c0_ == '-' && has_line_terminator_before_next_) {
282 Advance();
283 if (c0_ == '-') {
284 Advance();
285 if (c0_ == '>') {
286 // Treat the rest of the line as a comment.
287 SkipSingleLineComment();
288 // Continue skipping white space after the comment.
289 continue;
290 }
291 PushBack('-'); // undo Advance()
292 }
293 PushBack('-'); // undo Advance()
294 }
295 // Return whether or not we skipped any characters.
296 return source_pos() != start_position;
297 }
298}
299
300
301Token::Value JavaScriptScanner::SkipSingleLineComment() {
302 Advance();
303
304 // The line terminator at the end of the line is not considered
305 // to be part of the single-line comment; it is recognized
306 // separately by the lexical grammar and becomes part of the
307 // stream of input elements for the syntactic grammar (see
whesse@chromium.org7b260152011-06-20 15:33:18 +0000308 // ECMA-262, section 7.4).
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000309 while (c0_ >= 0 && !unicode_cache_->IsLineTerminator(c0_)) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000310 Advance();
311 }
312
313 return Token::WHITESPACE;
314}
315
316
317Token::Value JavaScriptScanner::SkipMultiLineComment() {
318 ASSERT(c0_ == '*');
319 Advance();
320
321 while (c0_ >= 0) {
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000322 uc32 ch = c0_;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000323 Advance();
whesse@chromium.org7b260152011-06-20 15:33:18 +0000324 if (unicode_cache_->IsLineTerminator(ch)) {
325 // Following ECMA-262, section 7.4, a comment containing
326 // a newline will make the comment count as a line-terminator.
whesse@chromium.orgdf8c03c2011-06-21 14:36:03 +0000327 has_multiline_comment_before_next_ = true;
whesse@chromium.org7b260152011-06-20 15:33:18 +0000328 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000329 // If we have reached the end of the multi-line comment, we
330 // consume the '/' and insert a whitespace. This way all
whesse@chromium.org7b260152011-06-20 15:33:18 +0000331 // multi-line comments are treated as whitespace.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000332 if (ch == '*' && c0_ == '/') {
333 c0_ = ' ';
334 return Token::WHITESPACE;
335 }
336 }
337
338 // Unterminated multi-line comment.
339 return Token::ILLEGAL;
340}
341
342
343Token::Value JavaScriptScanner::ScanHtmlComment() {
344 // Check for <!-- comments.
345 ASSERT(c0_ == '!');
346 Advance();
347 if (c0_ == '-') {
348 Advance();
349 if (c0_ == '-') return SkipSingleLineComment();
350 PushBack('-'); // undo Advance()
351 }
352 PushBack('!'); // undo Advance()
353 ASSERT(c0_ == '!');
354 return Token::LT;
355}
356
357
358void JavaScriptScanner::Scan() {
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000359 next_.literal_chars = NULL;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000360 Token::Value token;
361 do {
362 // Remember the position of the next token
363 next_.location.beg_pos = source_pos();
364
365 switch (c0_) {
366 case ' ':
367 case '\t':
368 Advance();
369 token = Token::WHITESPACE;
370 break;
371
372 case '\n':
373 Advance();
374 has_line_terminator_before_next_ = true;
375 token = Token::WHITESPACE;
376 break;
377
378 case '"': case '\'':
379 token = ScanString();
380 break;
381
382 case '<':
383 // < <= << <<= <!--
384 Advance();
385 if (c0_ == '=') {
386 token = Select(Token::LTE);
387 } else if (c0_ == '<') {
388 token = Select('=', Token::ASSIGN_SHL, Token::SHL);
389 } else if (c0_ == '!') {
390 token = ScanHtmlComment();
391 } else {
392 token = Token::LT;
393 }
394 break;
395
396 case '>':
397 // > >= >> >>= >>> >>>=
398 Advance();
399 if (c0_ == '=') {
400 token = Select(Token::GTE);
401 } else if (c0_ == '>') {
402 // >> >>= >>> >>>=
403 Advance();
404 if (c0_ == '=') {
405 token = Select(Token::ASSIGN_SAR);
406 } else if (c0_ == '>') {
407 token = Select('=', Token::ASSIGN_SHR, Token::SHR);
408 } else {
409 token = Token::SAR;
410 }
411 } else {
412 token = Token::GT;
413 }
414 break;
415
416 case '=':
417 // = == ===
418 Advance();
419 if (c0_ == '=') {
420 token = Select('=', Token::EQ_STRICT, Token::EQ);
421 } else {
422 token = Token::ASSIGN;
423 }
424 break;
425
426 case '!':
427 // ! != !==
428 Advance();
429 if (c0_ == '=') {
430 token = Select('=', Token::NE_STRICT, Token::NE);
431 } else {
432 token = Token::NOT;
433 }
434 break;
435
436 case '+':
437 // + ++ +=
438 Advance();
439 if (c0_ == '+') {
440 token = Select(Token::INC);
441 } else if (c0_ == '=') {
442 token = Select(Token::ASSIGN_ADD);
443 } else {
444 token = Token::ADD;
445 }
446 break;
447
448 case '-':
449 // - -- --> -=
450 Advance();
451 if (c0_ == '-') {
452 Advance();
453 if (c0_ == '>' && has_line_terminator_before_next_) {
454 // For compatibility with SpiderMonkey, we skip lines that
455 // start with an HTML comment end '-->'.
456 token = SkipSingleLineComment();
457 } else {
458 token = Token::DEC;
459 }
460 } else if (c0_ == '=') {
461 token = Select(Token::ASSIGN_SUB);
462 } else {
463 token = Token::SUB;
464 }
465 break;
466
467 case '*':
468 // * *=
469 token = Select('=', Token::ASSIGN_MUL, Token::MUL);
470 break;
471
472 case '%':
473 // % %=
474 token = Select('=', Token::ASSIGN_MOD, Token::MOD);
475 break;
476
477 case '/':
478 // / // /* /=
479 Advance();
480 if (c0_ == '/') {
481 token = SkipSingleLineComment();
482 } else if (c0_ == '*') {
483 token = SkipMultiLineComment();
484 } else if (c0_ == '=') {
485 token = Select(Token::ASSIGN_DIV);
486 } else {
487 token = Token::DIV;
488 }
489 break;
490
491 case '&':
492 // & && &=
493 Advance();
494 if (c0_ == '&') {
495 token = Select(Token::AND);
496 } else if (c0_ == '=') {
497 token = Select(Token::ASSIGN_BIT_AND);
498 } else {
499 token = Token::BIT_AND;
500 }
501 break;
502
503 case '|':
504 // | || |=
505 Advance();
506 if (c0_ == '|') {
507 token = Select(Token::OR);
508 } else if (c0_ == '=') {
509 token = Select(Token::ASSIGN_BIT_OR);
510 } else {
511 token = Token::BIT_OR;
512 }
513 break;
514
515 case '^':
516 // ^ ^=
517 token = Select('=', Token::ASSIGN_BIT_XOR, Token::BIT_XOR);
518 break;
519
520 case '.':
521 // . Number
522 Advance();
523 if (IsDecimalDigit(c0_)) {
524 token = ScanNumber(true);
525 } else {
526 token = Token::PERIOD;
527 }
528 break;
529
530 case ':':
531 token = Select(Token::COLON);
532 break;
533
534 case ';':
535 token = Select(Token::SEMICOLON);
536 break;
537
538 case ',':
539 token = Select(Token::COMMA);
540 break;
541
542 case '(':
543 token = Select(Token::LPAREN);
544 break;
545
546 case ')':
547 token = Select(Token::RPAREN);
548 break;
549
550 case '[':
551 token = Select(Token::LBRACK);
552 break;
553
554 case ']':
555 token = Select(Token::RBRACK);
556 break;
557
558 case '{':
559 token = Select(Token::LBRACE);
560 break;
561
562 case '}':
563 token = Select(Token::RBRACE);
564 break;
565
566 case '?':
567 token = Select(Token::CONDITIONAL);
568 break;
569
570 case '~':
571 token = Select(Token::BIT_NOT);
572 break;
573
574 default:
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000575 if (unicode_cache_->IsIdentifierStart(c0_)) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000576 token = ScanIdentifierOrKeyword();
577 } else if (IsDecimalDigit(c0_)) {
578 token = ScanNumber(false);
579 } else if (SkipWhiteSpace()) {
580 token = Token::WHITESPACE;
581 } else if (c0_ < 0) {
582 token = Token::EOS;
583 } else {
584 token = Select(Token::ILLEGAL);
585 }
586 break;
587 }
588
589 // Continue scanning for tokens as long as we're just skipping
590 // whitespace.
591 } while (token == Token::WHITESPACE);
592
593 next_.location.end_pos = source_pos();
594 next_.token = token;
595}
596
597
598void JavaScriptScanner::SeekForward(int pos) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000599 // After this call, we will have the token at the given position as
600 // the "next" token. The "current" token will be invalid.
601 if (pos == next_.location.beg_pos) return;
602 int current_pos = source_pos();
603 ASSERT_EQ(next_.location.end_pos, current_pos);
604 // Positions inside the lookahead token aren't supported.
605 ASSERT(pos >= current_pos);
606 if (pos != current_pos) {
607 source_->SeekForward(pos - source_->pos());
608 Advance();
609 // This function is only called to seek to the location
610 // of the end of a function (at the "}" token). It doesn't matter
611 // whether there was a line terminator in the part we skip.
612 has_line_terminator_before_next_ = false;
whesse@chromium.orgdf8c03c2011-06-21 14:36:03 +0000613 has_multiline_comment_before_next_ = false;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000614 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000615 Scan();
616}
617
618
619void JavaScriptScanner::ScanEscape() {
620 uc32 c = c0_;
621 Advance();
622
623 // Skip escaped newlines.
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000624 if (unicode_cache_->IsLineTerminator(c)) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000625 // Allow CR+LF newlines in multiline string literals.
626 if (IsCarriageReturn(c) && IsLineFeed(c0_)) Advance();
627 // Allow LF+CR newlines in multiline string literals.
628 if (IsLineFeed(c) && IsCarriageReturn(c0_)) Advance();
629 return;
630 }
631
632 switch (c) {
633 case '\'': // fall through
634 case '"' : // fall through
635 case '\\': break;
636 case 'b' : c = '\b'; break;
637 case 'f' : c = '\f'; break;
638 case 'n' : c = '\n'; break;
639 case 'r' : c = '\r'; break;
640 case 't' : c = '\t'; break;
641 case 'u' : c = ScanHexEscape(c, 4); break;
642 case 'v' : c = '\v'; break;
643 case 'x' : c = ScanHexEscape(c, 2); break;
644 case '0' : // fall through
645 case '1' : // fall through
646 case '2' : // fall through
647 case '3' : // fall through
648 case '4' : // fall through
649 case '5' : // fall through
650 case '6' : // fall through
651 case '7' : c = ScanOctalEscape(c, 2); break;
652 }
653
654 // According to ECMA-262, 3rd, 7.8.4 (p 18ff) these
655 // should be illegal, but they are commonly handled
656 // as non-escaped characters by JS VMs.
657 AddLiteralChar(c);
658}
659
660
lrn@chromium.org1c092762011-05-09 09:42:16 +0000661// Octal escapes of the forms '\0xx' and '\xxx' are not a part of
662// ECMA-262. Other JS VMs support them.
663uc32 JavaScriptScanner::ScanOctalEscape(uc32 c, int length) {
664 uc32 x = c - '0';
665 int i = 0;
666 for (; i < length; i++) {
667 int d = c0_ - '0';
668 if (d < 0 || d > 7) break;
669 int nx = x * 8 + d;
670 if (nx >= 256) break;
671 x = nx;
672 Advance();
673 }
674 // Anything except '\0' is an octal escape sequence, illegal in strict mode.
675 // Remember the position of octal escape sequences so that an error
676 // can be reported later (in strict mode).
677 // We don't report the error immediately, because the octal escape can
678 // occur before the "use strict" directive.
679 if (c != '0' || i > 0) {
680 octal_pos_ = Location(source_pos() - i - 1, source_pos() - 1);
681 }
682 return x;
683}
684
685
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000686Token::Value JavaScriptScanner::ScanString() {
687 uc32 quote = c0_;
688 Advance(); // consume quote
689
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000690 LiteralScope literal(this);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000691 while (c0_ != quote && c0_ >= 0
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000692 && !unicode_cache_->IsLineTerminator(c0_)) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000693 uc32 c = c0_;
694 Advance();
695 if (c == '\\') {
696 if (c0_ < 0) return Token::ILLEGAL;
697 ScanEscape();
698 } else {
699 AddLiteralChar(c);
700 }
701 }
702 if (c0_ != quote) return Token::ILLEGAL;
703 literal.Complete();
704
705 Advance(); // consume quote
706 return Token::STRING;
707}
708
709
710void JavaScriptScanner::ScanDecimalDigits() {
711 while (IsDecimalDigit(c0_))
712 AddLiteralCharAdvance();
713}
714
715
716Token::Value JavaScriptScanner::ScanNumber(bool seen_period) {
717 ASSERT(IsDecimalDigit(c0_)); // the first digit of the number or the fraction
718
719 enum { DECIMAL, HEX, OCTAL } kind = DECIMAL;
720
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000721 LiteralScope literal(this);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000722 if (seen_period) {
723 // we have already seen a decimal point of the float
724 AddLiteralChar('.');
725 ScanDecimalDigits(); // we know we have at least one digit
726
727 } else {
728 // if the first character is '0' we must check for octals and hex
729 if (c0_ == '0') {
lrn@chromium.org1c092762011-05-09 09:42:16 +0000730 int start_pos = source_pos(); // For reporting octal positions.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000731 AddLiteralCharAdvance();
732
733 // either 0, 0exxx, 0Exxx, 0.xxx, an octal number, or a hex number
734 if (c0_ == 'x' || c0_ == 'X') {
735 // hex number
736 kind = HEX;
737 AddLiteralCharAdvance();
738 if (!IsHexDigit(c0_)) {
739 // we must have at least one hex digit after 'x'/'X'
740 return Token::ILLEGAL;
741 }
742 while (IsHexDigit(c0_)) {
743 AddLiteralCharAdvance();
744 }
745 } else if ('0' <= c0_ && c0_ <= '7') {
746 // (possible) octal number
747 kind = OCTAL;
748 while (true) {
749 if (c0_ == '8' || c0_ == '9') {
750 kind = DECIMAL;
751 break;
752 }
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000753 if (c0_ < '0' || '7' < c0_) {
754 // Octal literal finished.
lrn@chromium.org1c092762011-05-09 09:42:16 +0000755 octal_pos_ = Location(start_pos, source_pos());
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000756 break;
757 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000758 AddLiteralCharAdvance();
759 }
760 }
761 }
762
763 // Parse decimal digits and allow trailing fractional part.
764 if (kind == DECIMAL) {
765 ScanDecimalDigits(); // optional
766 if (c0_ == '.') {
767 AddLiteralCharAdvance();
768 ScanDecimalDigits(); // optional
769 }
770 }
771 }
772
773 // scan exponent, if any
774 if (c0_ == 'e' || c0_ == 'E') {
775 ASSERT(kind != HEX); // 'e'/'E' must be scanned as part of the hex number
776 if (kind == OCTAL) return Token::ILLEGAL; // no exponent for octals allowed
777 // scan exponent
778 AddLiteralCharAdvance();
779 if (c0_ == '+' || c0_ == '-')
780 AddLiteralCharAdvance();
781 if (!IsDecimalDigit(c0_)) {
782 // we must have at least one decimal digit after 'e'/'E'
783 return Token::ILLEGAL;
784 }
785 ScanDecimalDigits();
786 }
787
788 // The source character immediately following a numeric literal must
789 // not be an identifier start or a decimal digit; see ECMA-262
790 // section 7.8.3, page 17 (note that we read only one decimal digit
791 // if the value is 0).
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000792 if (IsDecimalDigit(c0_) || unicode_cache_->IsIdentifierStart(c0_))
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000793 return Token::ILLEGAL;
794
795 literal.Complete();
796
797 return Token::NUMBER;
798}
799
800
801uc32 JavaScriptScanner::ScanIdentifierUnicodeEscape() {
802 Advance();
803 if (c0_ != 'u') return unibrow::Utf8::kBadChar;
804 Advance();
805 uc32 c = ScanHexEscape('u', 4);
806 // We do not allow a unicode escape sequence to start another
807 // unicode escape sequence.
808 if (c == '\\') return unibrow::Utf8::kBadChar;
809 return c;
810}
811
812
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000813// ----------------------------------------------------------------------------
814// Keyword Matcher
815
816#define KEYWORDS(KEYWORD_GROUP, KEYWORD) \
817 KEYWORD_GROUP('b') \
818 KEYWORD("break", BREAK) \
819 KEYWORD_GROUP('c') \
820 KEYWORD("case", CASE) \
821 KEYWORD("catch", CATCH) \
822 KEYWORD("class", FUTURE_RESERVED_WORD) \
823 KEYWORD("const", CONST) \
824 KEYWORD("continue", CONTINUE) \
825 KEYWORD_GROUP('d') \
826 KEYWORD("debugger", DEBUGGER) \
827 KEYWORD("default", DEFAULT) \
828 KEYWORD("delete", DELETE) \
829 KEYWORD("do", DO) \
830 KEYWORD_GROUP('e') \
831 KEYWORD("else", ELSE) \
832 KEYWORD("enum", FUTURE_RESERVED_WORD) \
833 KEYWORD("export", FUTURE_RESERVED_WORD) \
834 KEYWORD("extends", FUTURE_RESERVED_WORD) \
835 KEYWORD_GROUP('f') \
836 KEYWORD("false", FALSE_LITERAL) \
837 KEYWORD("finally", FINALLY) \
838 KEYWORD("for", FOR) \
839 KEYWORD("function", FUNCTION) \
840 KEYWORD_GROUP('i') \
841 KEYWORD("if", IF) \
842 KEYWORD("implements", FUTURE_STRICT_RESERVED_WORD) \
843 KEYWORD("import", FUTURE_RESERVED_WORD) \
844 KEYWORD("in", IN) \
845 KEYWORD("instanceof", INSTANCEOF) \
846 KEYWORD("interface", FUTURE_STRICT_RESERVED_WORD) \
847 KEYWORD_GROUP('l') \
848 KEYWORD("let", FUTURE_STRICT_RESERVED_WORD) \
849 KEYWORD_GROUP('n') \
850 KEYWORD("new", NEW) \
851 KEYWORD("null", NULL_LITERAL) \
852 KEYWORD_GROUP('p') \
853 KEYWORD("package", FUTURE_STRICT_RESERVED_WORD) \
854 KEYWORD("private", FUTURE_STRICT_RESERVED_WORD) \
855 KEYWORD("protected", FUTURE_STRICT_RESERVED_WORD) \
856 KEYWORD("public", FUTURE_STRICT_RESERVED_WORD) \
857 KEYWORD_GROUP('r') \
858 KEYWORD("return", RETURN) \
859 KEYWORD_GROUP('s') \
860 KEYWORD("static", FUTURE_STRICT_RESERVED_WORD) \
861 KEYWORD("super", FUTURE_RESERVED_WORD) \
862 KEYWORD("switch", SWITCH) \
863 KEYWORD_GROUP('t') \
864 KEYWORD("this", THIS) \
865 KEYWORD("throw", THROW) \
866 KEYWORD("true", TRUE_LITERAL) \
867 KEYWORD("try", TRY) \
868 KEYWORD("typeof", TYPEOF) \
869 KEYWORD_GROUP('v') \
870 KEYWORD("var", VAR) \
871 KEYWORD("void", VOID) \
872 KEYWORD_GROUP('w') \
873 KEYWORD("while", WHILE) \
874 KEYWORD("with", WITH) \
875 KEYWORD_GROUP('y') \
876 KEYWORD("yield", FUTURE_STRICT_RESERVED_WORD)
877
878
879static Token::Value KeywordOrIdentifierToken(const char* input,
880 int input_length) {
881 ASSERT(input_length >= 1);
882 const int kMinLength = 2;
883 const int kMaxLength = 10;
884 if (input_length < kMinLength || input_length > kMaxLength) {
885 return Token::IDENTIFIER;
886 }
887 switch (input[0]) {
888 default:
889#define KEYWORD_GROUP_CASE(ch) \
890 break; \
891 case ch:
892#define KEYWORD(keyword, token) \
893 { \
894 /* 'keyword' is a char array, so sizeof(keyword) is */ \
895 /* strlen(keyword) plus 1 for the NUL char. */ \
896 const int keyword_length = sizeof(keyword) - 1; \
897 STATIC_ASSERT(keyword_length >= kMinLength); \
898 STATIC_ASSERT(keyword_length <= kMaxLength); \
899 if (input_length == keyword_length && \
900 input[1] == keyword[1] && \
901 (keyword_length <= 2 || input[2] == keyword[2]) && \
902 (keyword_length <= 3 || input[3] == keyword[3]) && \
903 (keyword_length <= 4 || input[4] == keyword[4]) && \
904 (keyword_length <= 5 || input[5] == keyword[5]) && \
905 (keyword_length <= 6 || input[6] == keyword[6]) && \
906 (keyword_length <= 7 || input[7] == keyword[7]) && \
907 (keyword_length <= 8 || input[8] == keyword[8]) && \
908 (keyword_length <= 9 || input[9] == keyword[9])) { \
909 return Token::token; \
910 } \
911 }
912 KEYWORDS(KEYWORD_GROUP_CASE, KEYWORD)
913 }
914 return Token::IDENTIFIER;
915}
916
917
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000918Token::Value JavaScriptScanner::ScanIdentifierOrKeyword() {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000919 ASSERT(unicode_cache_->IsIdentifierStart(c0_));
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000920 LiteralScope literal(this);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000921 // Scan identifier start character.
922 if (c0_ == '\\') {
923 uc32 c = ScanIdentifierUnicodeEscape();
924 // Only allow legal identifier start characters.
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000925 if (!unicode_cache_->IsIdentifierStart(c)) return Token::ILLEGAL;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000926 AddLiteralChar(c);
927 return ScanIdentifierSuffix(&literal);
928 }
929
930 uc32 first_char = c0_;
931 Advance();
932 AddLiteralChar(first_char);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000933
934 // Scan the rest of the identifier characters.
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000935 while (unicode_cache_->IsIdentifierPart(c0_)) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000936 if (c0_ != '\\') {
937 uc32 next_char = c0_;
938 Advance();
939 AddLiteralChar(next_char);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000940 continue;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000941 }
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000942 // Fallthrough if no longer able to complete keyword.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000943 return ScanIdentifierSuffix(&literal);
944 }
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000945
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000946 literal.Complete();
947
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000948 if (next_.literal_chars->is_ascii()) {
949 Vector<const char> chars = next_.literal_chars->ascii_literal();
950 return KeywordOrIdentifierToken(chars.start(), chars.length());
951 }
952
953 return Token::IDENTIFIER;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000954}
955
956
957Token::Value JavaScriptScanner::ScanIdentifierSuffix(LiteralScope* literal) {
958 // Scan the rest of the identifier characters.
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000959 while (unicode_cache_->IsIdentifierPart(c0_)) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000960 if (c0_ == '\\') {
961 uc32 c = ScanIdentifierUnicodeEscape();
962 // Only allow legal identifier part characters.
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000963 if (!unicode_cache_->IsIdentifierPart(c)) return Token::ILLEGAL;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000964 AddLiteralChar(c);
965 } else {
966 AddLiteralChar(c0_);
967 Advance();
968 }
969 }
970 literal->Complete();
971
972 return Token::IDENTIFIER;
973}
974
975
976bool JavaScriptScanner::ScanRegExpPattern(bool seen_equal) {
977 // Scan: ('/' | '/=') RegularExpressionBody '/' RegularExpressionFlags
978 bool in_character_class = false;
979
980 // Previous token is either '/' or '/=', in the second case, the
981 // pattern starts at =.
982 next_.location.beg_pos = source_pos() - (seen_equal ? 2 : 1);
983 next_.location.end_pos = source_pos() - (seen_equal ? 1 : 0);
984
985 // Scan regular expression body: According to ECMA-262, 3rd, 7.8.5,
986 // the scanner should pass uninterpreted bodies to the RegExp
987 // constructor.
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000988 LiteralScope literal(this);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000989 if (seen_equal)
990 AddLiteralChar('=');
991
992 while (c0_ != '/' || in_character_class) {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000993 if (unicode_cache_->IsLineTerminator(c0_) || c0_ < 0) return false;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000994 if (c0_ == '\\') { // Escape sequence.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000995 AddLiteralCharAdvance();
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000996 if (unicode_cache_->IsLineTerminator(c0_) || c0_ < 0) return false;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000997 AddLiteralCharAdvance();
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000998 // If the escape allows more characters, i.e., \x??, \u????, or \c?,
999 // only "safe" characters are allowed (letters, digits, underscore),
1000 // otherwise the escape isn't valid and the invalid character has
1001 // its normal meaning. I.e., we can just continue scanning without
1002 // worrying whether the following characters are part of the escape
1003 // or not, since any '/', '\\' or '[' is guaranteed to not be part
1004 // of the escape sequence.
lrn@chromium.org1c092762011-05-09 09:42:16 +00001005
1006 // TODO(896): At some point, parse RegExps more throughly to capture
1007 // octal esacpes in strict mode.
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001008 } else { // Unescaped character.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001009 if (c0_ == '[') in_character_class = true;
1010 if (c0_ == ']') in_character_class = false;
1011 AddLiteralCharAdvance();
1012 }
1013 }
1014 Advance(); // consume '/'
1015
1016 literal.Complete();
1017
1018 return true;
1019}
1020
1021
1022bool JavaScriptScanner::ScanRegExpFlags() {
1023 // Scan regular expression flags.
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +00001024 LiteralScope literal(this);
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00001025 while (unicode_cache_->IsIdentifierPart(c0_)) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001026 if (c0_ == '\\') {
1027 uc32 c = ScanIdentifierUnicodeEscape();
1028 if (c != static_cast<uc32>(unibrow::Utf8::kBadChar)) {
1029 // We allow any escaped character, unlike the restriction on
1030 // IdentifierPart when it is used to build an IdentifierName.
1031 AddLiteralChar(c);
1032 continue;
1033 }
1034 }
1035 AddLiteralCharAdvance();
1036 }
1037 literal.Complete();
1038
1039 next_.location.end_pos = source_pos() - 1;
1040 return true;
1041}
1042
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001043} } // namespace v8::internal