OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Features shared by parsing and pre-parsing scanners. | 5 // Features shared by parsing and pre-parsing scanners. |
6 | 6 |
7 #include "src/parsing/scanner.h" | 7 #include "src/parsing/scanner.h" |
8 | 8 |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
(...skipping 22 matching lines...) Expand all Loading... |
33 void Utf16CharacterStream::ResetToBookmark() { UNREACHABLE(); } | 33 void Utf16CharacterStream::ResetToBookmark() { UNREACHABLE(); } |
34 | 34 |
35 | 35 |
36 // ---------------------------------------------------------------------------- | 36 // ---------------------------------------------------------------------------- |
37 // Scanner | 37 // Scanner |
38 | 38 |
39 Scanner::Scanner(UnicodeCache* unicode_cache) | 39 Scanner::Scanner(UnicodeCache* unicode_cache) |
40 : unicode_cache_(unicode_cache), | 40 : unicode_cache_(unicode_cache), |
41 bookmark_c0_(kNoBookmark), | 41 bookmark_c0_(kNoBookmark), |
42 octal_pos_(Location::invalid()), | 42 octal_pos_(Location::invalid()), |
| 43 decimal_with_leading_zero_pos_(Location::invalid()), |
43 found_html_comment_(false), | 44 found_html_comment_(false), |
44 allow_harmony_exponentiation_operator_(false) { | 45 allow_harmony_exponentiation_operator_(false) { |
45 bookmark_current_.literal_chars = &bookmark_current_literal_; | 46 bookmark_current_.literal_chars = &bookmark_current_literal_; |
46 bookmark_current_.raw_literal_chars = &bookmark_current_raw_literal_; | 47 bookmark_current_.raw_literal_chars = &bookmark_current_raw_literal_; |
47 bookmark_next_.literal_chars = &bookmark_next_literal_; | 48 bookmark_next_.literal_chars = &bookmark_next_literal_; |
48 bookmark_next_.raw_literal_chars = &bookmark_next_raw_literal_; | 49 bookmark_next_.raw_literal_chars = &bookmark_next_raw_literal_; |
49 } | 50 } |
50 | 51 |
51 | 52 |
52 void Scanner::Initialize(Utf16CharacterStream* source) { | 53 void Scanner::Initialize(Utf16CharacterStream* source) { |
(...skipping 915 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
968 | 969 |
969 void Scanner::ScanDecimalDigits() { | 970 void Scanner::ScanDecimalDigits() { |
970 while (IsDecimalDigit(c0_)) | 971 while (IsDecimalDigit(c0_)) |
971 AddLiteralCharAdvance(); | 972 AddLiteralCharAdvance(); |
972 } | 973 } |
973 | 974 |
974 | 975 |
975 Token::Value Scanner::ScanNumber(bool seen_period) { | 976 Token::Value Scanner::ScanNumber(bool seen_period) { |
976 DCHECK(IsDecimalDigit(c0_)); // the first digit of the number or the fraction | 977 DCHECK(IsDecimalDigit(c0_)); // the first digit of the number or the fraction |
977 | 978 |
978 enum { DECIMAL, HEX, OCTAL, IMPLICIT_OCTAL, BINARY } kind = DECIMAL; | 979 enum { |
| 980 DECIMAL, |
| 981 DECIMAL_WITH_LEADING_ZERO, |
| 982 HEX, |
| 983 OCTAL, |
| 984 IMPLICIT_OCTAL, |
| 985 BINARY |
| 986 } kind = DECIMAL; |
979 | 987 |
980 LiteralScope literal(this); | 988 LiteralScope literal(this); |
981 bool at_start = !seen_period; | 989 bool at_start = !seen_period; |
| 990 int start_pos = source_pos(); // For reporting octal positions. |
982 if (seen_period) { | 991 if (seen_period) { |
983 // we have already seen a decimal point of the float | 992 // we have already seen a decimal point of the float |
984 AddLiteralChar('.'); | 993 AddLiteralChar('.'); |
985 ScanDecimalDigits(); // we know we have at least one digit | 994 ScanDecimalDigits(); // we know we have at least one digit |
986 | 995 |
987 } else { | 996 } else { |
988 // if the first character is '0' we must check for octals and hex | 997 // if the first character is '0' we must check for octals and hex |
989 if (c0_ == '0') { | 998 if (c0_ == '0') { |
990 int start_pos = source_pos(); // For reporting octal positions. | |
991 AddLiteralCharAdvance(); | 999 AddLiteralCharAdvance(); |
992 | 1000 |
993 // either 0, 0exxx, 0Exxx, 0.xxx, a hex number, a binary number or | 1001 // either 0, 0exxx, 0Exxx, 0.xxx, a hex number, a binary number or |
994 // an octal number. | 1002 // an octal number. |
995 if (c0_ == 'x' || c0_ == 'X') { | 1003 if (c0_ == 'x' || c0_ == 'X') { |
996 // hex number | 1004 // hex number |
997 kind = HEX; | 1005 kind = HEX; |
998 AddLiteralCharAdvance(); | 1006 AddLiteralCharAdvance(); |
999 if (!IsHexDigit(c0_)) { | 1007 if (!IsHexDigit(c0_)) { |
1000 // we must have at least one hex digit after 'x'/'X' | 1008 // we must have at least one hex digit after 'x'/'X' |
(...skipping 21 matching lines...) Expand all Loading... |
1022 } | 1030 } |
1023 while (IsBinaryDigit(c0_)) { | 1031 while (IsBinaryDigit(c0_)) { |
1024 AddLiteralCharAdvance(); | 1032 AddLiteralCharAdvance(); |
1025 } | 1033 } |
1026 } else if ('0' <= c0_ && c0_ <= '7') { | 1034 } else if ('0' <= c0_ && c0_ <= '7') { |
1027 // (possible) octal number | 1035 // (possible) octal number |
1028 kind = IMPLICIT_OCTAL; | 1036 kind = IMPLICIT_OCTAL; |
1029 while (true) { | 1037 while (true) { |
1030 if (c0_ == '8' || c0_ == '9') { | 1038 if (c0_ == '8' || c0_ == '9') { |
1031 at_start = false; | 1039 at_start = false; |
1032 kind = DECIMAL; | 1040 kind = DECIMAL_WITH_LEADING_ZERO; |
1033 break; | 1041 break; |
1034 } | 1042 } |
1035 if (c0_ < '0' || '7' < c0_) { | 1043 if (c0_ < '0' || '7' < c0_) { |
1036 // Octal literal finished. | 1044 // Octal literal finished. |
1037 octal_pos_ = Location(start_pos, source_pos()); | 1045 octal_pos_ = Location(start_pos, source_pos()); |
1038 break; | 1046 break; |
1039 } | 1047 } |
1040 AddLiteralCharAdvance(); | 1048 AddLiteralCharAdvance(); |
1041 } | 1049 } |
| 1050 } else if (c0_ == '8' || c0_ == '9') { |
| 1051 kind = DECIMAL_WITH_LEADING_ZERO; |
1042 } | 1052 } |
1043 } | 1053 } |
1044 | 1054 |
1045 // Parse decimal digits and allow trailing fractional part. | 1055 // Parse decimal digits and allow trailing fractional part. |
1046 if (kind == DECIMAL) { | 1056 if (kind == DECIMAL || kind == DECIMAL_WITH_LEADING_ZERO) { |
1047 if (at_start) { | 1057 if (at_start) { |
1048 uint64_t value = 0; | 1058 uint64_t value = 0; |
1049 while (IsDecimalDigit(c0_)) { | 1059 while (IsDecimalDigit(c0_)) { |
1050 value = 10 * value + (c0_ - '0'); | 1060 value = 10 * value + (c0_ - '0'); |
1051 | 1061 |
1052 uc32 first_char = c0_; | 1062 uc32 first_char = c0_; |
1053 Advance<false, false>(); | 1063 Advance<false, false>(); |
1054 AddLiteralChar(first_char); | 1064 AddLiteralChar(first_char); |
1055 } | 1065 } |
1056 | 1066 |
1057 if (next_.literal_chars->one_byte_literal().length() <= 10 && | 1067 if (next_.literal_chars->one_byte_literal().length() <= 10 && |
1058 value <= Smi::kMaxValue && c0_ != '.' && c0_ != 'e' && c0_ != 'E') { | 1068 value <= Smi::kMaxValue && c0_ != '.' && c0_ != 'e' && c0_ != 'E') { |
1059 next_.smi_value_ = static_cast<int>(value); | 1069 next_.smi_value_ = static_cast<int>(value); |
1060 literal.Complete(); | 1070 literal.Complete(); |
1061 HandleLeadSurrogate(); | 1071 HandleLeadSurrogate(); |
1062 | 1072 |
| 1073 if (kind == DECIMAL_WITH_LEADING_ZERO) |
| 1074 decimal_with_leading_zero_pos_ = Location(start_pos, source_pos()); |
1063 return Token::SMI; | 1075 return Token::SMI; |
1064 } | 1076 } |
1065 HandleLeadSurrogate(); | 1077 HandleLeadSurrogate(); |
1066 } | 1078 } |
1067 | 1079 |
1068 ScanDecimalDigits(); // optional | 1080 ScanDecimalDigits(); // optional |
1069 if (c0_ == '.') { | 1081 if (c0_ == '.') { |
1070 AddLiteralCharAdvance(); | 1082 AddLiteralCharAdvance(); |
1071 ScanDecimalDigits(); // optional | 1083 ScanDecimalDigits(); // optional |
1072 } | 1084 } |
1073 } | 1085 } |
1074 } | 1086 } |
1075 | 1087 |
1076 // scan exponent, if any | 1088 // scan exponent, if any |
1077 if (c0_ == 'e' || c0_ == 'E') { | 1089 if (c0_ == 'e' || c0_ == 'E') { |
1078 DCHECK(kind != HEX); // 'e'/'E' must be scanned as part of the hex number | 1090 DCHECK(kind != HEX); // 'e'/'E' must be scanned as part of the hex number |
1079 if (kind != DECIMAL) return Token::ILLEGAL; | 1091 if (!(kind == DECIMAL || kind == DECIMAL_WITH_LEADING_ZERO)) |
| 1092 return Token::ILLEGAL; |
1080 // scan exponent | 1093 // scan exponent |
1081 AddLiteralCharAdvance(); | 1094 AddLiteralCharAdvance(); |
1082 if (c0_ == '+' || c0_ == '-') | 1095 if (c0_ == '+' || c0_ == '-') |
1083 AddLiteralCharAdvance(); | 1096 AddLiteralCharAdvance(); |
1084 if (!IsDecimalDigit(c0_)) { | 1097 if (!IsDecimalDigit(c0_)) { |
1085 // we must have at least one decimal digit after 'e'/'E' | 1098 // we must have at least one decimal digit after 'e'/'E' |
1086 return Token::ILLEGAL; | 1099 return Token::ILLEGAL; |
1087 } | 1100 } |
1088 ScanDecimalDigits(); | 1101 ScanDecimalDigits(); |
1089 } | 1102 } |
1090 | 1103 |
1091 // The source character immediately following a numeric literal must | 1104 // The source character immediately following a numeric literal must |
1092 // not be an identifier start or a decimal digit; see ECMA-262 | 1105 // not be an identifier start or a decimal digit; see ECMA-262 |
1093 // section 7.8.3, page 17 (note that we read only one decimal digit | 1106 // section 7.8.3, page 17 (note that we read only one decimal digit |
1094 // if the value is 0). | 1107 // if the value is 0). |
1095 if (IsDecimalDigit(c0_) || | 1108 if (IsDecimalDigit(c0_) || |
1096 (c0_ >= 0 && unicode_cache_->IsIdentifierStart(c0_))) | 1109 (c0_ >= 0 && unicode_cache_->IsIdentifierStart(c0_))) |
1097 return Token::ILLEGAL; | 1110 return Token::ILLEGAL; |
1098 | 1111 |
1099 literal.Complete(); | 1112 literal.Complete(); |
1100 | 1113 |
| 1114 if (kind == DECIMAL_WITH_LEADING_ZERO) |
| 1115 decimal_with_leading_zero_pos_ = Location(start_pos, source_pos()); |
1101 return Token::NUMBER; | 1116 return Token::NUMBER; |
1102 } | 1117 } |
1103 | 1118 |
1104 | 1119 |
1105 uc32 Scanner::ScanIdentifierUnicodeEscape() { | 1120 uc32 Scanner::ScanIdentifierUnicodeEscape() { |
1106 Advance(); | 1121 Advance(); |
1107 if (c0_ != 'u') return -1; | 1122 if (c0_ != 'u') return -1; |
1108 Advance(); | 1123 Advance(); |
1109 return ScanUnicodeEscape<false>(); | 1124 return ScanUnicodeEscape<false>(); |
1110 } | 1125 } |
(...skipping 576 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1687 backing_store_.Add(static_cast<uint8_t>((one_byte_length >> 7) | 0x80u)); | 1702 backing_store_.Add(static_cast<uint8_t>((one_byte_length >> 7) | 0x80u)); |
1688 } | 1703 } |
1689 backing_store_.Add(static_cast<uint8_t>(one_byte_length & 0x7f)); | 1704 backing_store_.Add(static_cast<uint8_t>(one_byte_length & 0x7f)); |
1690 | 1705 |
1691 backing_store_.AddBlock(bytes); | 1706 backing_store_.AddBlock(bytes); |
1692 return backing_store_.EndSequence().start(); | 1707 return backing_store_.EndSequence().start(); |
1693 } | 1708 } |
1694 | 1709 |
1695 } // namespace internal | 1710 } // namespace internal |
1696 } // namespace v8 | 1711 } // namespace v8 |
OLD | NEW |