1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Features shared by parsing and pre-parsing scanners.
13 #include "src/ast-value-factory.h"
14 #include "src/char-predicates-inl.h"
15 #include "src/conversions-inl.h"
16 #include "src/list-inl.h"
17 #include "src/parser.h"
18 #include "src/scanner.h"
24 Handle<String> LiteralBuffer::Internalize(Isolate* isolate) const {
26 return isolate->factory()->InternalizeOneByteString(one_byte_literal());
28 return isolate->factory()->InternalizeTwoByteString(two_byte_literal());
32 // ----------------------------------------------------------------------------
35 Scanner::Scanner(UnicodeCache* unicode_cache)
36 : unicode_cache_(unicode_cache),
37 octal_pos_(Location::invalid()),
38 harmony_scoping_(false),
39 harmony_modules_(false),
40 harmony_numeric_literals_(false),
41 harmony_classes_(false) { }
44 void Scanner::Initialize(Utf16CharacterStream* source) {
46 // Need to capture identifiers in order to recognize "get" and "set"
47 // in object literals.
49 // Skip initial whitespace allowing HTML comment ends just like
50 // after a newline and scan first token.
51 has_line_terminator_before_next_ = true;
57 uc32 Scanner::ScanHexNumber(int expected_length) {
58 DCHECK(expected_length <= 4); // prevent overflow
61 for (int i = 0; i < expected_length; i++) {
62 int d = HexValue(c0_);
74 // Ensure that tokens can be stored in a byte.
75 STATIC_ASSERT(Token::NUM_TOKENS <= 0x100);
77 // Table of one-character tokens, by character (0x00..0x7f only).
78 static const byte one_char_tokens[] = {
119 Token::LPAREN, // 0x28
120 Token::RPAREN, // 0x29
123 Token::COMMA, // 0x2c
137 Token::COLON, // 0x3a
138 Token::SEMICOLON, // 0x3b
142 Token::CONDITIONAL, // 0x3f
170 Token::LBRACK, // 0x5b
172 Token::RBRACK, // 0x5d
202 Token::LBRACE, // 0x7b
204 Token::RBRACE, // 0x7d
205 Token::BIT_NOT, // 0x7e
210 Token::Value Scanner::Next() {
212 has_line_terminator_before_next_ = false;
213 has_multiline_comment_before_next_ = false;
214 if (static_cast<unsigned>(c0_) <= 0x7f) {
215 Token::Value token = static_cast<Token::Value>(one_char_tokens[c0_]);
216 if (token != Token::ILLEGAL) {
217 int pos = source_pos();
219 next_.location.beg_pos = pos;
220 next_.location.end_pos = pos + 1;
222 return current_.token;
226 return current_.token;
230 // TODO(yangguo): check whether this is actually necessary.
231 static inline bool IsLittleEndianByteOrderMark(uc32 c) {
232 // The Unicode value U+FFFE is guaranteed never to be assigned as a
233 // Unicode character; this implies that in a Unicode context the
234 // 0xFF, 0xFE byte pattern can only be interpreted as the U+FEFF
235 // character expressed in little-endian byte order (since it could
236 // not be a U+FFFE character expressed in big-endian byte
237 // order). Nevertheless, we check for it to be compatible with
243 bool Scanner::SkipWhiteSpace() {
244 int start_position = source_pos();
248 // Advance as long as character is a WhiteSpace or LineTerminator.
249 // Remember if the latter is the case.
250 if (unicode_cache_->IsLineTerminator(c0_)) {
251 has_line_terminator_before_next_ = true;
252 } else if (!unicode_cache_->IsWhiteSpace(c0_) &&
253 !IsLittleEndianByteOrderMark(c0_)) {
259 // If there is an HTML comment end '-->' at the beginning of a
260 // line (with only whitespace in front of it), we treat the rest
261 // of the line as a comment. This is in line with the way
262 // SpiderMonkey handles it.
263 if (c0_ == '-' && has_line_terminator_before_next_) {
268 // Treat the rest of the line as a comment.
269 SkipSingleLineComment();
270 // Continue skipping white space after the comment.
273 PushBack('-'); // undo Advance()
275 PushBack('-'); // undo Advance()
277 // Return whether or not we skipped any characters.
278 return source_pos() != start_position;
283 Token::Value Scanner::SkipSingleLineComment() {
286 // The line terminator at the end of the line is not considered
287 // to be part of the single-line comment; it is recognized
288 // separately by the lexical grammar and becomes part of the
289 // stream of input elements for the syntactic grammar (see
290 // ECMA-262, section 7.4).
291 while (c0_ >= 0 && !unicode_cache_->IsLineTerminator(c0_)) {
295 return Token::WHITESPACE;
299 Token::Value Scanner::SkipSourceURLComment() {
300 TryToParseSourceURLComment();
301 while (c0_ >= 0 && !unicode_cache_->IsLineTerminator(c0_)) {
305 return Token::WHITESPACE;
309 void Scanner::TryToParseSourceURLComment() {
310 // Magic comments are of the form: //[#@]\s<name>=\s*<value>\s*.* and this
311 // function will just return if it cannot parse a magic comment.
312 if (!unicode_cache_->IsWhiteSpace(c0_))
316 while (c0_ >= 0 && !unicode_cache_->IsWhiteSpaceOrLineTerminator(c0_) &&
321 if (!name.is_one_byte()) return;
322 Vector<const uint8_t> name_literal = name.one_byte_literal();
323 LiteralBuffer* value;
324 if (name_literal == STATIC_CHAR_VECTOR("sourceURL")) {
325 value = &source_url_;
326 } else if (name_literal == STATIC_CHAR_VECTOR("sourceMappingURL")) {
327 value = &source_mapping_url_;
335 while (c0_ >= 0 && unicode_cache_->IsWhiteSpace(c0_)) {
338 while (c0_ >= 0 && !unicode_cache_->IsLineTerminator(c0_)) {
339 // Disallowed characters.
340 if (c0_ == '"' || c0_ == '\'') {
344 if (unicode_cache_->IsWhiteSpace(c0_)) {
350 // Allow whitespace at the end.
351 while (c0_ >= 0 && !unicode_cache_->IsLineTerminator(c0_)) {
352 if (!unicode_cache_->IsWhiteSpace(c0_)) {
361 Token::Value Scanner::SkipMultiLineComment() {
368 if (unicode_cache_->IsLineTerminator(ch)) {
369 // Following ECMA-262, section 7.4, a comment containing
370 // a newline will make the comment count as a line-terminator.
371 has_multiline_comment_before_next_ = true;
373 // If we have reached the end of the multi-line comment, we
374 // consume the '/' and insert a whitespace. This way all
375 // multi-line comments are treated as whitespace.
376 if (ch == '*' && c0_ == '/') {
378 return Token::WHITESPACE;
382 // Unterminated multi-line comment.
383 return Token::ILLEGAL;
387 Token::Value Scanner::ScanHtmlComment() {
388 // Check for <!-- comments.
393 if (c0_ == '-') return SkipSingleLineComment();
394 PushBack('-'); // undo Advance()
396 PushBack('!'); // undo Advance()
402 void Scanner::Scan() {
403 next_.literal_chars = NULL;
406 // Remember the position of the next token
407 next_.location.beg_pos = source_pos();
413 token = Token::WHITESPACE;
418 has_line_terminator_before_next_ = true;
419 token = Token::WHITESPACE;
423 token = ScanString();
430 token = Select(Token::LTE);
431 } else if (c0_ == '<') {
432 token = Select('=', Token::ASSIGN_SHL, Token::SHL);
433 } else if (c0_ == '!') {
434 token = ScanHtmlComment();
441 // > >= >> >>= >>> >>>=
444 token = Select(Token::GTE);
445 } else if (c0_ == '>') {
449 token = Select(Token::ASSIGN_SAR);
450 } else if (c0_ == '>') {
451 token = Select('=', Token::ASSIGN_SHR, Token::SHR);
464 token = Select('=', Token::EQ_STRICT, Token::EQ);
465 } else if (c0_ == '>') {
466 token = Select(Token::ARROW);
468 token = Token::ASSIGN;
476 token = Select('=', Token::NE_STRICT, Token::NE);
486 token = Select(Token::INC);
487 } else if (c0_ == '=') {
488 token = Select(Token::ASSIGN_ADD);
499 if (c0_ == '>' && has_line_terminator_before_next_) {
500 // For compatibility with SpiderMonkey, we skip lines that
501 // start with an HTML comment end '-->'.
502 token = SkipSingleLineComment();
506 } else if (c0_ == '=') {
507 token = Select(Token::ASSIGN_SUB);
515 token = Select('=', Token::ASSIGN_MUL, Token::MUL);
520 token = Select('=', Token::ASSIGN_MOD, Token::MOD);
528 if (c0_ == '@' || c0_ == '#') {
530 token = SkipSourceURLComment();
533 token = SkipSingleLineComment();
535 } else if (c0_ == '*') {
536 token = SkipMultiLineComment();
537 } else if (c0_ == '=') {
538 token = Select(Token::ASSIGN_DIV);
548 token = Select(Token::AND);
549 } else if (c0_ == '=') {
550 token = Select(Token::ASSIGN_BIT_AND);
552 token = Token::BIT_AND;
560 token = Select(Token::OR);
561 } else if (c0_ == '=') {
562 token = Select(Token::ASSIGN_BIT_OR);
564 token = Token::BIT_OR;
570 token = Select('=', Token::ASSIGN_BIT_XOR, Token::BIT_XOR);
576 if (IsDecimalDigit(c0_)) {
577 token = ScanNumber(true);
579 token = Token::PERIOD;
584 token = Select(Token::COLON);
588 token = Select(Token::SEMICOLON);
592 token = Select(Token::COMMA);
596 token = Select(Token::LPAREN);
600 token = Select(Token::RPAREN);
604 token = Select(Token::LBRACK);
608 token = Select(Token::RBRACK);
612 token = Select(Token::LBRACE);
616 token = Select(Token::RBRACE);
620 token = Select(Token::CONDITIONAL);
624 token = Select(Token::BIT_NOT);
628 if (unicode_cache_->IsIdentifierStart(c0_)) {
629 token = ScanIdentifierOrKeyword();
630 } else if (IsDecimalDigit(c0_)) {
631 token = ScanNumber(false);
632 } else if (SkipWhiteSpace()) {
633 token = Token::WHITESPACE;
634 } else if (c0_ < 0) {
637 token = Select(Token::ILLEGAL);
642 // Continue scanning for tokens as long as we're just skipping
644 } while (token == Token::WHITESPACE);
646 next_.location.end_pos = source_pos();
651 void Scanner::SeekForward(int pos) {
652 // After this call, we will have the token at the given position as
653 // the "next" token. The "current" token will be invalid.
654 if (pos == next_.location.beg_pos) return;
655 int current_pos = source_pos();
656 DCHECK_EQ(next_.location.end_pos, current_pos);
657 // Positions inside the lookahead token aren't supported.
658 DCHECK(pos >= current_pos);
659 if (pos != current_pos) {
660 source_->SeekForward(pos - source_->pos());
662 // This function is only called to seek to the location
663 // of the end of a function (at the "}" token). It doesn't matter
664 // whether there was a line terminator in the part we skip.
665 has_line_terminator_before_next_ = false;
666 has_multiline_comment_before_next_ = false;
672 bool Scanner::ScanEscape() {
676 // Skip escaped newlines.
677 if (unicode_cache_->IsLineTerminator(c)) {
678 // Allow CR+LF newlines in multiline string literals.
679 if (IsCarriageReturn(c) && IsLineFeed(c0_)) Advance();
680 // Allow LF+CR newlines in multiline string literals.
681 if (IsLineFeed(c) && IsCarriageReturn(c0_)) Advance();
686 case '\'': // fall through
687 case '"' : // fall through
689 case 'b' : c = '\b'; break;
690 case 'f' : c = '\f'; break;
691 case 'n' : c = '\n'; break;
692 case 'r' : c = '\r'; break;
693 case 't' : c = '\t'; break;
695 c = ScanHexNumber(4);
696 if (c < 0) return false;
699 case 'v' : c = '\v'; break;
701 c = ScanHexNumber(2);
702 if (c < 0) return false;
705 case '0' : // fall through
706 case '1' : // fall through
707 case '2' : // fall through
708 case '3' : // fall through
709 case '4' : // fall through
710 case '5' : // fall through
711 case '6' : // fall through
712 case '7' : c = ScanOctalEscape(c, 2); break;
715 // According to ECMA-262, section 7.8.4, characters not covered by the
716 // above cases should be illegal, but they are commonly handled as
717 // non-escaped characters by JS VMs.
723 // Octal escapes of the forms '\0xx' and '\xxx' are not a part of
724 // ECMA-262. Other JS VMs support them.
725 uc32 Scanner::ScanOctalEscape(uc32 c, int length) {
728 for (; i < length; i++) {
730 if (d < 0 || d > 7) break;
732 if (nx >= 256) break;
736 // Anything except '\0' is an octal escape sequence, illegal in strict mode.
737 // Remember the position of octal escape sequences so that an error
738 // can be reported later (in strict mode).
739 // We don't report the error immediately, because the octal escape can
740 // occur before the "use strict" directive.
741 if (c != '0' || i > 0) {
742 octal_pos_ = Location(source_pos() - i - 1, source_pos() - 1);
748 Token::Value Scanner::ScanString() {
750 Advance(); // consume quote
752 LiteralScope literal(this);
753 while (c0_ != quote && c0_ >= 0
754 && !unicode_cache_->IsLineTerminator(c0_)) {
758 if (c0_ < 0 || !ScanEscape()) return Token::ILLEGAL;
763 if (c0_ != quote) return Token::ILLEGAL;
766 Advance(); // consume quote
767 return Token::STRING;
771 void Scanner::ScanDecimalDigits() {
772 while (IsDecimalDigit(c0_))
773 AddLiteralCharAdvance();
777 Token::Value Scanner::ScanNumber(bool seen_period) {
778 DCHECK(IsDecimalDigit(c0_)); // the first digit of the number or the fraction
780 enum { DECIMAL, HEX, OCTAL, IMPLICIT_OCTAL, BINARY } kind = DECIMAL;
782 LiteralScope literal(this);
784 // we have already seen a decimal point of the float
786 ScanDecimalDigits(); // we know we have at least one digit
789 // if the first character is '0' we must check for octals and hex
791 int start_pos = source_pos(); // For reporting octal positions.
792 AddLiteralCharAdvance();
794 // either 0, 0exxx, 0Exxx, 0.xxx, a hex number, a binary number or
796 if (c0_ == 'x' || c0_ == 'X') {
799 AddLiteralCharAdvance();
800 if (!IsHexDigit(c0_)) {
801 // we must have at least one hex digit after 'x'/'X'
802 return Token::ILLEGAL;
804 while (IsHexDigit(c0_)) {
805 AddLiteralCharAdvance();
807 } else if (harmony_numeric_literals_ && (c0_ == 'o' || c0_ == 'O')) {
809 AddLiteralCharAdvance();
810 if (!IsOctalDigit(c0_)) {
811 // we must have at least one octal digit after 'o'/'O'
812 return Token::ILLEGAL;
814 while (IsOctalDigit(c0_)) {
815 AddLiteralCharAdvance();
817 } else if (harmony_numeric_literals_ && (c0_ == 'b' || c0_ == 'B')) {
819 AddLiteralCharAdvance();
820 if (!IsBinaryDigit(c0_)) {
821 // we must have at least one binary digit after 'b'/'B'
822 return Token::ILLEGAL;
824 while (IsBinaryDigit(c0_)) {
825 AddLiteralCharAdvance();
827 } else if ('0' <= c0_ && c0_ <= '7') {
828 // (possible) octal number
829 kind = IMPLICIT_OCTAL;
831 if (c0_ == '8' || c0_ == '9') {
835 if (c0_ < '0' || '7' < c0_) {
836 // Octal literal finished.
837 octal_pos_ = Location(start_pos, source_pos());
840 AddLiteralCharAdvance();
845 // Parse decimal digits and allow trailing fractional part.
846 if (kind == DECIMAL) {
847 ScanDecimalDigits(); // optional
849 AddLiteralCharAdvance();
850 ScanDecimalDigits(); // optional
855 // scan exponent, if any
856 if (c0_ == 'e' || c0_ == 'E') {
857 DCHECK(kind != HEX); // 'e'/'E' must be scanned as part of the hex number
858 if (kind != DECIMAL) return Token::ILLEGAL;
860 AddLiteralCharAdvance();
861 if (c0_ == '+' || c0_ == '-')
862 AddLiteralCharAdvance();
863 if (!IsDecimalDigit(c0_)) {
864 // we must have at least one decimal digit after 'e'/'E'
865 return Token::ILLEGAL;
870 // The source character immediately following a numeric literal must
871 // not be an identifier start or a decimal digit; see ECMA-262
872 // section 7.8.3, page 17 (note that we read only one decimal digit
873 // if the value is 0).
874 if (IsDecimalDigit(c0_) || unicode_cache_->IsIdentifierStart(c0_))
875 return Token::ILLEGAL;
879 return Token::NUMBER;
883 uc32 Scanner::ScanIdentifierUnicodeEscape() {
885 if (c0_ != 'u') return -1;
887 return ScanHexNumber(4);
891 // ----------------------------------------------------------------------------
894 #define KEYWORDS(KEYWORD_GROUP, KEYWORD) \
896 KEYWORD("break", Token::BREAK) \
898 KEYWORD("case", Token::CASE) \
899 KEYWORD("catch", Token::CATCH) \
901 harmony_classes ? Token::CLASS : Token::FUTURE_RESERVED_WORD) \
902 KEYWORD("const", Token::CONST) \
903 KEYWORD("continue", Token::CONTINUE) \
905 KEYWORD("debugger", Token::DEBUGGER) \
906 KEYWORD("default", Token::DEFAULT) \
907 KEYWORD("delete", Token::DELETE) \
908 KEYWORD("do", Token::DO) \
910 KEYWORD("else", Token::ELSE) \
911 KEYWORD("enum", Token::FUTURE_RESERVED_WORD) \
913 harmony_modules ? Token::EXPORT : Token::FUTURE_RESERVED_WORD) \
915 harmony_classes ? Token::EXTENDS : Token::FUTURE_RESERVED_WORD) \
917 KEYWORD("false", Token::FALSE_LITERAL) \
918 KEYWORD("finally", Token::FINALLY) \
919 KEYWORD("for", Token::FOR) \
920 KEYWORD("function", Token::FUNCTION) \
922 KEYWORD("if", Token::IF) \
923 KEYWORD("implements", Token::FUTURE_STRICT_RESERVED_WORD) \
925 harmony_modules ? Token::IMPORT : Token::FUTURE_RESERVED_WORD) \
926 KEYWORD("in", Token::IN) \
927 KEYWORD("instanceof", Token::INSTANCEOF) \
928 KEYWORD("interface", Token::FUTURE_STRICT_RESERVED_WORD) \
931 harmony_scoping ? Token::LET : Token::FUTURE_STRICT_RESERVED_WORD) \
933 KEYWORD("new", Token::NEW) \
934 KEYWORD("null", Token::NULL_LITERAL) \
936 KEYWORD("package", Token::FUTURE_STRICT_RESERVED_WORD) \
937 KEYWORD("private", Token::FUTURE_STRICT_RESERVED_WORD) \
938 KEYWORD("protected", Token::FUTURE_STRICT_RESERVED_WORD) \
939 KEYWORD("public", Token::FUTURE_STRICT_RESERVED_WORD) \
941 KEYWORD("return", Token::RETURN) \
943 KEYWORD("static", harmony_classes ? Token::STATIC \
944 : Token::FUTURE_STRICT_RESERVED_WORD) \
946 harmony_classes ? Token::SUPER : Token::FUTURE_RESERVED_WORD) \
947 KEYWORD("switch", Token::SWITCH) \
949 KEYWORD("this", Token::THIS) \
950 KEYWORD("throw", Token::THROW) \
951 KEYWORD("true", Token::TRUE_LITERAL) \
952 KEYWORD("try", Token::TRY) \
953 KEYWORD("typeof", Token::TYPEOF) \
955 KEYWORD("var", Token::VAR) \
956 KEYWORD("void", Token::VOID) \
958 KEYWORD("while", Token::WHILE) \
959 KEYWORD("with", Token::WITH) \
961 KEYWORD("yield", Token::YIELD)
964 static Token::Value KeywordOrIdentifierToken(const uint8_t* input,
966 bool harmony_scoping,
967 bool harmony_modules,
968 bool harmony_classes) {
969 DCHECK(input_length >= 1);
970 const int kMinLength = 2;
971 const int kMaxLength = 10;
972 if (input_length < kMinLength || input_length > kMaxLength) {
973 return Token::IDENTIFIER;
977 #define KEYWORD_GROUP_CASE(ch) \
980 #define KEYWORD(keyword, token) \
982 /* 'keyword' is a char array, so sizeof(keyword) is */ \
983 /* strlen(keyword) plus 1 for the NUL char. */ \
984 const int keyword_length = sizeof(keyword) - 1; \
985 STATIC_ASSERT(keyword_length >= kMinLength); \
986 STATIC_ASSERT(keyword_length <= kMaxLength); \
987 if (input_length == keyword_length && \
988 input[1] == keyword[1] && \
989 (keyword_length <= 2 || input[2] == keyword[2]) && \
990 (keyword_length <= 3 || input[3] == keyword[3]) && \
991 (keyword_length <= 4 || input[4] == keyword[4]) && \
992 (keyword_length <= 5 || input[5] == keyword[5]) && \
993 (keyword_length <= 6 || input[6] == keyword[6]) && \
994 (keyword_length <= 7 || input[7] == keyword[7]) && \
995 (keyword_length <= 8 || input[8] == keyword[8]) && \
996 (keyword_length <= 9 || input[9] == keyword[9])) { \
1000 KEYWORDS(KEYWORD_GROUP_CASE, KEYWORD)
1002 return Token::IDENTIFIER;
1006 bool Scanner::IdentifierIsFutureStrictReserved(
1007 const AstRawString* string) const {
1008 // Keywords are always 1-byte strings.
1009 if (!string->is_one_byte()) return false;
1010 if (string->IsOneByteEqualTo("let") || string->IsOneByteEqualTo("static") ||
1011 string->IsOneByteEqualTo("yield")) {
1014 return Token::FUTURE_STRICT_RESERVED_WORD ==
1015 KeywordOrIdentifierToken(string->raw_data(), string->length(),
1016 harmony_scoping_, harmony_modules_,
1021 Token::Value Scanner::ScanIdentifierOrKeyword() {
1022 DCHECK(unicode_cache_->IsIdentifierStart(c0_));
1023 LiteralScope literal(this);
1024 // Scan identifier start character.
1026 uc32 c = ScanIdentifierUnicodeEscape();
1027 // Only allow legal identifier start characters.
1029 c == '\\' || // No recursive escapes.
1030 !unicode_cache_->IsIdentifierStart(c)) {
1031 return Token::ILLEGAL;
1034 return ScanIdentifierSuffix(&literal);
1037 uc32 first_char = c0_;
1039 AddLiteralChar(first_char);
1041 // Scan the rest of the identifier characters.
1042 while (unicode_cache_->IsIdentifierPart(c0_)) {
1044 uc32 next_char = c0_;
1046 AddLiteralChar(next_char);
1049 // Fallthrough if no longer able to complete keyword.
1050 return ScanIdentifierSuffix(&literal);
1055 if (next_.literal_chars->is_one_byte()) {
1056 Vector<const uint8_t> chars = next_.literal_chars->one_byte_literal();
1057 return KeywordOrIdentifierToken(chars.start(),
1064 return Token::IDENTIFIER;
1068 Token::Value Scanner::ScanIdentifierSuffix(LiteralScope* literal) {
1069 // Scan the rest of the identifier characters.
1070 while (unicode_cache_->IsIdentifierPart(c0_)) {
1072 uc32 c = ScanIdentifierUnicodeEscape();
1073 // Only allow legal identifier part characters.
1076 !unicode_cache_->IsIdentifierPart(c)) {
1077 return Token::ILLEGAL;
1081 AddLiteralChar(c0_);
1085 literal->Complete();
1087 return Token::IDENTIFIER;
1091 bool Scanner::ScanRegExpPattern(bool seen_equal) {
1092 // Scan: ('/' | '/=') RegularExpressionBody '/' RegularExpressionFlags
1093 bool in_character_class = false;
1095 // Previous token is either '/' or '/=', in the second case, the
1096 // pattern starts at =.
1097 next_.location.beg_pos = source_pos() - (seen_equal ? 2 : 1);
1098 next_.location.end_pos = source_pos() - (seen_equal ? 1 : 0);
1100 // Scan regular expression body: According to ECMA-262, 3rd, 7.8.5,
1101 // the scanner should pass uninterpreted bodies to the RegExp
1103 LiteralScope literal(this);
1105 AddLiteralChar('=');
1108 while (c0_ != '/' || in_character_class) {
1109 if (unicode_cache_->IsLineTerminator(c0_) || c0_ < 0) return false;
1110 if (c0_ == '\\') { // Escape sequence.
1111 AddLiteralCharAdvance();
1112 if (unicode_cache_->IsLineTerminator(c0_) || c0_ < 0) return false;
1113 AddLiteralCharAdvance();
1114 // If the escape allows more characters, i.e., \x??, \u????, or \c?,
1115 // only "safe" characters are allowed (letters, digits, underscore),
1116 // otherwise the escape isn't valid and the invalid character has
1117 // its normal meaning. I.e., we can just continue scanning without
1118 // worrying whether the following characters are part of the escape
1119 // or not, since any '/', '\\' or '[' is guaranteed to not be part
1120 // of the escape sequence.
1122 // TODO(896): At some point, parse RegExps more throughly to capture
1123 // octal esacpes in strict mode.
1124 } else { // Unescaped character.
1125 if (c0_ == '[') in_character_class = true;
1126 if (c0_ == ']') in_character_class = false;
1127 AddLiteralCharAdvance();
1130 Advance(); // consume '/'
1138 bool Scanner::ScanLiteralUnicodeEscape() {
1139 DCHECK(c0_ == '\\');
1140 AddLiteralChar(c0_);
1142 int hex_digits_read = 0;
1144 AddLiteralChar(c0_);
1145 while (hex_digits_read < 4) {
1147 if (!IsHexDigit(c0_)) break;
1148 AddLiteralChar(c0_);
1152 return hex_digits_read == 4;
1156 bool Scanner::ScanRegExpFlags() {
1157 // Scan regular expression flags.
1158 LiteralScope literal(this);
1159 while (unicode_cache_->IsIdentifierPart(c0_)) {
1161 AddLiteralCharAdvance();
1163 if (!ScanLiteralUnicodeEscape()) {
1171 next_.location.end_pos = source_pos() - 1;
1176 const AstRawString* Scanner::CurrentSymbol(AstValueFactory* ast_value_factory) {
1177 if (is_literal_one_byte()) {
1178 return ast_value_factory->GetOneByteString(literal_one_byte_string());
1180 return ast_value_factory->GetTwoByteString(literal_two_byte_string());
1184 const AstRawString* Scanner::NextSymbol(AstValueFactory* ast_value_factory) {
1185 if (is_next_literal_one_byte()) {
1186 return ast_value_factory->GetOneByteString(next_literal_one_byte_string());
1188 return ast_value_factory->GetTwoByteString(next_literal_two_byte_string());
1192 double Scanner::DoubleValue() {
1193 DCHECK(is_literal_one_byte());
1194 return StringToDouble(
1196 literal_one_byte_string(),
1197 ALLOW_HEX | ALLOW_OCTAL | ALLOW_IMPLICIT_OCTAL | ALLOW_BINARY);
1201 int Scanner::FindNumber(DuplicateFinder* finder, int value) {
1202 return finder->AddNumber(literal_one_byte_string(), value);
1206 int Scanner::FindSymbol(DuplicateFinder* finder, int value) {
1207 if (is_literal_one_byte()) {
1208 return finder->AddOneByteSymbol(literal_one_byte_string(), value);
1210 return finder->AddTwoByteSymbol(literal_two_byte_string(), value);
1214 int DuplicateFinder::AddOneByteSymbol(Vector<const uint8_t> key, int value) {
1215 return AddSymbol(key, true, value);
1219 int DuplicateFinder::AddTwoByteSymbol(Vector<const uint16_t> key, int value) {
1220 return AddSymbol(Vector<const uint8_t>::cast(key), false, value);
1224 int DuplicateFinder::AddSymbol(Vector<const uint8_t> key,
1227 uint32_t hash = Hash(key, is_one_byte);
1228 byte* encoding = BackupKey(key, is_one_byte);
1229 HashMap::Entry* entry = map_.Lookup(encoding, hash, true);
1230 int old_value = static_cast<int>(reinterpret_cast<intptr_t>(entry->value));
1232 reinterpret_cast<void*>(static_cast<intptr_t>(value | old_value));
1237 int DuplicateFinder::AddNumber(Vector<const uint8_t> key, int value) {
1238 DCHECK(key.length() > 0);
1239 // Quick check for already being in canonical form.
1240 if (IsNumberCanonical(key)) {
1241 return AddOneByteSymbol(key, value);
1244 int flags = ALLOW_HEX | ALLOW_OCTAL | ALLOW_IMPLICIT_OCTAL | ALLOW_BINARY;
1245 double double_value = StringToDouble(
1246 unicode_constants_, key, flags, 0.0);
1249 if (!std::isfinite(double_value)) {
1250 string = "Infinity";
1251 length = 8; // strlen("Infinity");
1253 string = DoubleToCString(double_value,
1254 Vector<char>(number_buffer_, kBufferSize));
1255 length = StrLength(string);
1257 return AddSymbol(Vector<const byte>(reinterpret_cast<const byte*>(string),
1258 length), true, value);
1262 bool DuplicateFinder::IsNumberCanonical(Vector<const uint8_t> number) {
1263 // Test for a safe approximation of number literals that are already
1264 // in canonical form: max 15 digits, no leading zeroes, except an
1265 // integer part that is a single zero, and no trailing zeros below
1266 // the decimal point.
1268 int length = number.length();
1269 if (number.length() > 15) return false;
1270 if (number[pos] == '0') {
1273 while (pos < length &&
1274 static_cast<unsigned>(number[pos] - '0') <= ('9' - '0')) pos++;
1276 if (length == pos) return true;
1277 if (number[pos] != '.') return false;
1279 bool invalid_last_digit = true;
1280 while (pos < length) {
1281 uint8_t digit = number[pos] - '0';
1282 if (digit > '9' - '0') return false;
1283 invalid_last_digit = (digit == 0);
1286 return !invalid_last_digit;
1290 uint32_t DuplicateFinder::Hash(Vector<const uint8_t> key, bool is_one_byte) {
1291 // Primitive hash function, almost identical to the one used
1292 // for strings (except that it's seeded by the length and representation).
1293 int length = key.length();
1294 uint32_t hash = (length << 1) | (is_one_byte ? 1 : 0) ;
1295 for (int i = 0; i < length; i++) {
1296 uint32_t c = key[i];
1297 hash = (hash + c) * 1025;
1298 hash ^= (hash >> 6);
1304 bool DuplicateFinder::Match(void* first, void* second) {
1306 // Length + representation is encoded as base 128, most significant heptet
1307 // first, with a 8th bit being non-zero while there are more heptets.
1308 // The value encodes the number of bytes following, and whether the original
1310 byte* s1 = reinterpret_cast<byte*>(first);
1311 byte* s2 = reinterpret_cast<byte*>(second);
1312 uint32_t length_one_byte_field = 0;
1316 if (c1 != *s2) return false;
1317 length_one_byte_field = (length_one_byte_field << 7) | (c1 & 0x7f);
1320 } while ((c1 & 0x80) != 0);
1321 int length = static_cast<int>(length_one_byte_field >> 1);
1322 return memcmp(s1, s2, length) == 0;
1326 byte* DuplicateFinder::BackupKey(Vector<const uint8_t> bytes,
1328 uint32_t one_byte_length = (bytes.length() << 1) | (is_one_byte ? 1 : 0);
1329 backing_store_.StartSequence();
1330 // Emit one_byte_length as base-128 encoded number, with the 7th bit set
1331 // on the byte of every heptet except the last, least significant, one.
1332 if (one_byte_length >= (1 << 7)) {
1333 if (one_byte_length >= (1 << 14)) {
1334 if (one_byte_length >= (1 << 21)) {
1335 if (one_byte_length >= (1 << 28)) {
1337 static_cast<uint8_t>((one_byte_length >> 28) | 0x80));
1340 static_cast<uint8_t>((one_byte_length >> 21) | 0x80u));
1343 static_cast<uint8_t>((one_byte_length >> 14) | 0x80u));
1345 backing_store_.Add(static_cast<uint8_t>((one_byte_length >> 7) | 0x80u));
1347 backing_store_.Add(static_cast<uint8_t>(one_byte_length & 0x7f));
1349 backing_store_.AddBlock(bytes);
1350 return backing_store_.EndSequence().start();
1353 } } // namespace v8::internal