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 <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <cmath> | 9 #include <cmath> |
10 | 10 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 // after a newline and scan first token. | 50 // after a newline and scan first token. |
51 has_line_terminator_before_next_ = true; | 51 has_line_terminator_before_next_ = true; |
52 SkipWhiteSpace(); | 52 SkipWhiteSpace(); |
53 Scan(); | 53 Scan(); |
54 } | 54 } |
55 | 55 |
56 | 56 |
57 uc32 Scanner::ScanHexNumber(int expected_length) { | 57 uc32 Scanner::ScanHexNumber(int expected_length) { |
58 DCHECK(expected_length <= 4); // prevent overflow | 58 DCHECK(expected_length <= 4); // prevent overflow |
59 | 59 |
60 uc32 digits[4] = { 0, 0, 0, 0 }; | |
61 uc32 x = 0; | 60 uc32 x = 0; |
62 for (int i = 0; i < expected_length; i++) { | 61 for (int i = 0; i < expected_length; i++) { |
63 digits[i] = c0_; | |
64 int d = HexValue(c0_); | 62 int d = HexValue(c0_); |
65 if (d < 0) { | 63 if (d < 0) { |
66 // According to ECMA-262, 3rd, 7.8.4, page 18, these hex escapes | |
67 // should be illegal, but other JS VMs just return the | |
68 // non-escaped version of the original character. | |
69 | |
70 // Push back digits that we have advanced past. | |
71 for (int j = i-1; j >= 0; j--) { | |
72 PushBack(digits[j]); | |
73 } | |
74 return -1; | 64 return -1; |
75 } | 65 } |
76 x = x * 16 + d; | 66 x = x * 16 + d; |
77 Advance(); | 67 Advance(); |
78 } | 68 } |
79 | 69 |
80 return x; | 70 return x; |
81 } | 71 } |
82 | 72 |
83 | 73 |
(...skipping 803 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
887 literal.Complete(); | 877 literal.Complete(); |
888 | 878 |
889 return Token::NUMBER; | 879 return Token::NUMBER; |
890 } | 880 } |
891 | 881 |
892 | 882 |
893 uc32 Scanner::ScanIdentifierUnicodeEscape() { | 883 uc32 Scanner::ScanIdentifierUnicodeEscape() { |
894 Advance(); | 884 Advance(); |
895 if (c0_ != 'u') return -1; | 885 if (c0_ != 'u') return -1; |
896 Advance(); | 886 Advance(); |
897 uc32 result = ScanHexNumber(4); | 887 return ScanHexNumber(4); |
898 if (result < 0) PushBack('u'); | |
899 return result; | |
900 } | 888 } |
901 | 889 |
902 | 890 |
903 // ---------------------------------------------------------------------------- | 891 // ---------------------------------------------------------------------------- |
904 // Keyword Matcher | 892 // Keyword Matcher |
905 | 893 |
906 #define KEYWORDS(KEYWORD_GROUP, KEYWORD) \ | 894 #define KEYWORDS(KEYWORD_GROUP, KEYWORD) \ |
907 KEYWORD_GROUP('b') \ | 895 KEYWORD_GROUP('b') \ |
908 KEYWORD("break", Token::BREAK) \ | 896 KEYWORD("break", Token::BREAK) \ |
909 KEYWORD_GROUP('c') \ | 897 KEYWORD_GROUP('c') \ |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1138 Advance(); // consume '/' | 1126 Advance(); // consume '/' |
1139 | 1127 |
1140 literal.Complete(); | 1128 literal.Complete(); |
1141 | 1129 |
1142 return true; | 1130 return true; |
1143 } | 1131 } |
1144 | 1132 |
1145 | 1133 |
1146 bool Scanner::ScanLiteralUnicodeEscape() { | 1134 bool Scanner::ScanLiteralUnicodeEscape() { |
1147 DCHECK(c0_ == '\\'); | 1135 DCHECK(c0_ == '\\'); |
1148 uc32 chars_read[6] = {'\\', 'u', 0, 0, 0, 0}; | 1136 AddLiteralChar(c0_); |
1149 Advance(); | 1137 Advance(); |
1150 int i = 1; | 1138 int hex_digits_read = 0; |
1151 if (c0_ == 'u') { | 1139 if (c0_ == 'u') { |
1152 i++; | 1140 AddLiteralChar(c0_); |
1153 while (i < 6) { | 1141 while (hex_digits_read < 4) { |
1154 Advance(); | 1142 Advance(); |
1155 if (!IsHexDigit(c0_)) break; | 1143 if (!IsHexDigit(c0_)) break; |
1156 chars_read[i] = c0_; | 1144 AddLiteralChar(c0_); |
1157 i++; | 1145 ++hex_digits_read; |
1158 } | 1146 } |
1159 } | 1147 } |
1160 if (i < 6) { | 1148 return hex_digits_read == 4; |
1161 // Incomplete escape. Undo all advances and return false. | |
1162 while (i > 0) { | |
1163 i--; | |
1164 PushBack(chars_read[i]); | |
1165 } | |
1166 return false; | |
1167 } | |
1168 // Complete escape. Add all chars to current literal buffer. | |
1169 for (int i = 0; i < 6; i++) { | |
1170 AddLiteralChar(chars_read[i]); | |
1171 } | |
1172 return true; | |
1173 } | 1149 } |
1174 | 1150 |
1175 | 1151 |
1176 bool Scanner::ScanRegExpFlags() { | 1152 bool Scanner::ScanRegExpFlags() { |
1177 // Scan regular expression flags. | 1153 // Scan regular expression flags. |
1178 LiteralScope literal(this); | 1154 LiteralScope literal(this); |
1179 while (unicode_cache_->IsIdentifierPart(c0_)) { | 1155 while (unicode_cache_->IsIdentifierPart(c0_)) { |
1180 if (c0_ != '\\') { | 1156 if (c0_ != '\\') { |
1181 AddLiteralCharAdvance(); | 1157 AddLiteralCharAdvance(); |
1182 } else { | 1158 } else { |
1183 if (!ScanLiteralUnicodeEscape()) { | 1159 if (!ScanLiteralUnicodeEscape()) { |
1184 break; | 1160 return false; |
1185 } | 1161 } |
1186 Advance(); | 1162 Advance(); |
1187 } | 1163 } |
1188 } | 1164 } |
1189 literal.Complete(); | 1165 literal.Complete(); |
1190 | 1166 |
1191 next_.location.end_pos = source_pos() - 1; | 1167 next_.location.end_pos = source_pos() - 1; |
1192 return true; | 1168 return true; |
1193 } | 1169 } |
1194 | 1170 |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1364 } | 1340 } |
1365 backing_store_.Add(static_cast<uint8_t>((one_byte_length >> 7) | 0x80u)); | 1341 backing_store_.Add(static_cast<uint8_t>((one_byte_length >> 7) | 0x80u)); |
1366 } | 1342 } |
1367 backing_store_.Add(static_cast<uint8_t>(one_byte_length & 0x7f)); | 1343 backing_store_.Add(static_cast<uint8_t>(one_byte_length & 0x7f)); |
1368 | 1344 |
1369 backing_store_.AddBlock(bytes); | 1345 backing_store_.AddBlock(bytes); |
1370 return backing_store_.EndSequence().start(); | 1346 return backing_store_.EndSequence().start(); |
1371 } | 1347 } |
1372 | 1348 |
1373 } } // namespace v8::internal | 1349 } } // namespace v8::internal |
OLD | NEW |