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 <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "src/scanner.h" | 9 #include "src/scanner.h" |
10 | 10 |
11 #include "include/v8stdint.h" | 11 #include "include/v8stdint.h" |
12 #include "src/char-predicates-inl.h" | 12 #include "src/char-predicates-inl.h" |
13 #include "src/conversions-inl.h" | 13 #include "src/conversions-inl.h" |
14 #include "src/list-inl.h" | 14 #include "src/list-inl.h" |
15 #include "src/v8.h" | 15 #include "src/v8.h" |
16 #include "src/parser.h" | 16 #include "src/parser.h" |
17 | 17 |
18 namespace v8 { | 18 namespace v8 { |
19 namespace internal { | 19 namespace internal { |
20 | 20 |
| 21 |
| 22 Handle<String> LiteralBuffer::Internalize(Isolate* isolate) const { |
| 23 if (is_one_byte()) { |
| 24 return isolate->factory()->InternalizeOneByteString(one_byte_literal()); |
| 25 } |
| 26 return isolate->factory()->InternalizeTwoByteString(two_byte_literal()); |
| 27 } |
| 28 |
| 29 |
21 // ---------------------------------------------------------------------------- | 30 // ---------------------------------------------------------------------------- |
22 // Scanner | 31 // Scanner |
23 | 32 |
24 Scanner::Scanner(UnicodeCache* unicode_cache) | 33 Scanner::Scanner(UnicodeCache* unicode_cache) |
25 : unicode_cache_(unicode_cache), | 34 : unicode_cache_(unicode_cache), |
26 octal_pos_(Location::invalid()), | 35 octal_pos_(Location::invalid()), |
27 harmony_scoping_(false), | 36 harmony_scoping_(false), |
28 harmony_modules_(false), | 37 harmony_modules_(false), |
29 harmony_numeric_literals_(false) { } | 38 harmony_numeric_literals_(false) { } |
30 | 39 |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 // stream of input elements for the syntactic grammar (see | 296 // stream of input elements for the syntactic grammar (see |
288 // ECMA-262, section 7.4). | 297 // ECMA-262, section 7.4). |
289 while (c0_ >= 0 && !unicode_cache_->IsLineTerminator(c0_)) { | 298 while (c0_ >= 0 && !unicode_cache_->IsLineTerminator(c0_)) { |
290 Advance(); | 299 Advance(); |
291 } | 300 } |
292 | 301 |
293 return Token::WHITESPACE; | 302 return Token::WHITESPACE; |
294 } | 303 } |
295 | 304 |
296 | 305 |
| 306 Token::Value Scanner::SkipMagicComment() { |
| 307 TryToParseMagicComment(); |
| 308 while (c0_ >= 0 && !unicode_cache_->IsLineTerminator(c0_)) { |
| 309 Advance(); |
| 310 } |
| 311 |
| 312 return Token::WHITESPACE; |
| 313 } |
| 314 |
| 315 |
| 316 void Scanner::TryToParseMagicComment() { |
| 317 // Magic comments are of the form: //[#@]\s<name>=\s*<value>\s*.* and this |
| 318 // function will just return if it cannot parse a magic comment. |
| 319 if (!unicode_cache_->IsWhiteSpace(c0_)) |
| 320 return; |
| 321 Advance(); |
| 322 LiteralBuffer name; |
| 323 while (c0_ >= 0 && !unicode_cache_->IsWhiteSpaceOrLineTerminator(c0_) && |
| 324 c0_ != '=') { |
| 325 name.AddChar(c0_); |
| 326 Advance(); |
| 327 } |
| 328 if (!name.is_one_byte()) return; |
| 329 Vector<const uint8_t> name_literal = name.one_byte_literal(); |
| 330 LiteralBuffer* value; |
| 331 if (name_literal == STATIC_ASCII_VECTOR("sourceURL")) { |
| 332 value = &source_url_; |
| 333 } else if (name_literal == STATIC_ASCII_VECTOR("sourceMappingURL")) { |
| 334 value = &source_mapping_url_; |
| 335 } else { |
| 336 return; |
| 337 } |
| 338 if (c0_ != '=') |
| 339 return; |
| 340 Advance(); |
| 341 value->Reset(); |
| 342 while (c0_ >= 0 && unicode_cache_->IsWhiteSpace(c0_)) { |
| 343 Advance(); |
| 344 } |
| 345 while (c0_ >= 0 && !unicode_cache_->IsLineTerminator(c0_)) { |
| 346 // Disallowed characters. |
| 347 if (c0_ == '"' || c0_ == '\'') { |
| 348 value->Reset(); |
| 349 return; |
| 350 } |
| 351 if (unicode_cache_->IsWhiteSpace(c0_)) { |
| 352 break; |
| 353 } |
| 354 value->AddChar(c0_); |
| 355 Advance(); |
| 356 } |
| 357 // Allow whitespace at the end. |
| 358 while (c0_ >= 0 && !unicode_cache_->IsLineTerminator(c0_)) { |
| 359 if (!unicode_cache_->IsWhiteSpace(c0_)) { |
| 360 value->Reset(); |
| 361 break; |
| 362 } |
| 363 Advance(); |
| 364 } |
| 365 } |
| 366 |
| 367 |
297 Token::Value Scanner::SkipMultiLineComment() { | 368 Token::Value Scanner::SkipMultiLineComment() { |
298 ASSERT(c0_ == '*'); | 369 ASSERT(c0_ == '*'); |
299 Advance(); | 370 Advance(); |
300 | 371 |
301 while (c0_ >= 0) { | 372 while (c0_ >= 0) { |
302 uc32 ch = c0_; | 373 uc32 ch = c0_; |
303 Advance(); | 374 Advance(); |
304 if (unicode_cache_->IsLineTerminator(ch)) { | 375 if (unicode_cache_->IsLineTerminator(ch)) { |
305 // Following ECMA-262, section 7.4, a comment containing | 376 // Following ECMA-262, section 7.4, a comment containing |
306 // a newline will make the comment count as a line-terminator. | 377 // a newline will make the comment count as a line-terminator. |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 | 522 |
452 case '%': | 523 case '%': |
453 // % %= | 524 // % %= |
454 token = Select('=', Token::ASSIGN_MOD, Token::MOD); | 525 token = Select('=', Token::ASSIGN_MOD, Token::MOD); |
455 break; | 526 break; |
456 | 527 |
457 case '/': | 528 case '/': |
458 // / // /* /= | 529 // / // /* /= |
459 Advance(); | 530 Advance(); |
460 if (c0_ == '/') { | 531 if (c0_ == '/') { |
461 token = SkipSingleLineComment(); | 532 Advance(); |
| 533 if (c0_ == '@' || c0_ == '#') { |
| 534 Advance(); |
| 535 token = SkipMagicComment(); |
| 536 } else { |
| 537 PushBack(c0_); |
| 538 token = SkipSingleLineComment(); |
| 539 } |
462 } else if (c0_ == '*') { | 540 } else if (c0_ == '*') { |
463 token = SkipMultiLineComment(); | 541 token = SkipMultiLineComment(); |
464 } else if (c0_ == '=') { | 542 } else if (c0_ == '=') { |
465 token = Select(Token::ASSIGN_DIV); | 543 token = Select(Token::ASSIGN_DIV); |
466 } else { | 544 } else { |
467 token = Token::DIV; | 545 token = Token::DIV; |
468 } | 546 } |
469 break; | 547 break; |
470 | 548 |
471 case '&': | 549 case '&': |
(...skipping 627 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1099 return isolate->factory()->NewStringFromOneByte( | 1177 return isolate->factory()->NewStringFromOneByte( |
1100 next_literal_one_byte_string(), tenured).ToHandleChecked(); | 1178 next_literal_one_byte_string(), tenured).ToHandleChecked(); |
1101 } else { | 1179 } else { |
1102 return isolate->factory()->NewStringFromTwoByte( | 1180 return isolate->factory()->NewStringFromTwoByte( |
1103 next_literal_two_byte_string(), tenured).ToHandleChecked(); | 1181 next_literal_two_byte_string(), tenured).ToHandleChecked(); |
1104 } | 1182 } |
1105 } | 1183 } |
1106 | 1184 |
1107 | 1185 |
1108 Handle<String> Scanner::AllocateInternalizedString(Isolate* isolate) { | 1186 Handle<String> Scanner::AllocateInternalizedString(Isolate* isolate) { |
1109 if (is_literal_one_byte()) { | 1187 return current_.literal_chars->Internalize(isolate); |
1110 return isolate->factory()->InternalizeOneByteString( | |
1111 literal_one_byte_string()); | |
1112 } else { | |
1113 return isolate->factory()->InternalizeTwoByteString( | |
1114 literal_two_byte_string()); | |
1115 } | |
1116 } | 1188 } |
1117 | 1189 |
1118 | 1190 |
1119 double Scanner::DoubleValue() { | 1191 double Scanner::DoubleValue() { |
1120 ASSERT(is_literal_one_byte()); | 1192 ASSERT(is_literal_one_byte()); |
1121 return StringToDouble( | 1193 return StringToDouble( |
1122 unicode_cache_, | 1194 unicode_cache_, |
1123 literal_one_byte_string(), | 1195 literal_one_byte_string(), |
1124 ALLOW_HEX | ALLOW_OCTAL | ALLOW_IMPLICIT_OCTAL | ALLOW_BINARY); | 1196 ALLOW_HEX | ALLOW_OCTAL | ALLOW_IMPLICIT_OCTAL | ALLOW_BINARY); |
1125 } | 1197 } |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1271 } | 1343 } |
1272 backing_store_.Add(static_cast<uint8_t>((one_byte_length >> 7) | 0x80u)); | 1344 backing_store_.Add(static_cast<uint8_t>((one_byte_length >> 7) | 0x80u)); |
1273 } | 1345 } |
1274 backing_store_.Add(static_cast<uint8_t>(one_byte_length & 0x7f)); | 1346 backing_store_.Add(static_cast<uint8_t>(one_byte_length & 0x7f)); |
1275 | 1347 |
1276 backing_store_.AddBlock(bytes); | 1348 backing_store_.AddBlock(bytes); |
1277 return backing_store_.EndSequence().start(); | 1349 return backing_store_.EndSequence().start(); |
1278 } | 1350 } |
1279 | 1351 |
1280 } } // namespace v8::internal | 1352 } } // namespace v8::internal |
OLD | NEW |