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/v8.h" | 9 #include "src/v8.h" |
10 | 10 |
11 #include "include/v8stdint.h" | 11 #include "include/v8stdint.h" |
12 #include "src/ast-value-factory.h" | 12 #include "src/ast-value-factory.h" |
13 #include "src/char-predicates-inl.h" | 13 #include "src/char-predicates-inl.h" |
14 #include "src/conversions-inl.h" | 14 #include "src/conversions-inl.h" |
15 #include "src/list-inl.h" | 15 #include "src/list-inl.h" |
16 #include "src/parser.h" | 16 #include "src/parser.h" |
17 #include "src/scanner.h" | 17 #include "src/scanner.h" |
18 | 18 |
19 namespace v8 { | 19 namespace v8 { |
20 namespace internal { | 20 namespace internal { |
21 | 21 |
| 22 |
| 23 Handle<String> LiteralBuffer::Internalize(Isolate* isolate) const { |
| 24 if (is_one_byte()) { |
| 25 return isolate->factory()->InternalizeOneByteString(one_byte_literal()); |
| 26 } |
| 27 return isolate->factory()->InternalizeTwoByteString(two_byte_literal()); |
| 28 } |
| 29 |
| 30 |
22 // ---------------------------------------------------------------------------- | 31 // ---------------------------------------------------------------------------- |
23 // Scanner | 32 // Scanner |
24 | 33 |
25 Scanner::Scanner(UnicodeCache* unicode_cache) | 34 Scanner::Scanner(UnicodeCache* unicode_cache) |
26 : unicode_cache_(unicode_cache), | 35 : unicode_cache_(unicode_cache), |
27 octal_pos_(Location::invalid()), | 36 octal_pos_(Location::invalid()), |
28 harmony_scoping_(false), | 37 harmony_scoping_(false), |
29 harmony_modules_(false), | 38 harmony_modules_(false), |
30 harmony_numeric_literals_(false) { } | 39 harmony_numeric_literals_(false) { } |
31 | 40 |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 // stream of input elements for the syntactic grammar (see | 297 // stream of input elements for the syntactic grammar (see |
289 // ECMA-262, section 7.4). | 298 // ECMA-262, section 7.4). |
290 while (c0_ >= 0 && !unicode_cache_->IsLineTerminator(c0_)) { | 299 while (c0_ >= 0 && !unicode_cache_->IsLineTerminator(c0_)) { |
291 Advance(); | 300 Advance(); |
292 } | 301 } |
293 | 302 |
294 return Token::WHITESPACE; | 303 return Token::WHITESPACE; |
295 } | 304 } |
296 | 305 |
297 | 306 |
| 307 Token::Value Scanner::SkipSourceURLComment() { |
| 308 TryToParseSourceURLComment(); |
| 309 while (c0_ >= 0 && !unicode_cache_->IsLineTerminator(c0_)) { |
| 310 Advance(); |
| 311 } |
| 312 |
| 313 return Token::WHITESPACE; |
| 314 } |
| 315 |
| 316 |
| 317 void Scanner::TryToParseSourceURLComment() { |
| 318 // Magic comments are of the form: //[#@]\s<name>=\s*<value>\s*.* and this |
| 319 // function will just return if it cannot parse a magic comment. |
| 320 if (!unicode_cache_->IsWhiteSpace(c0_)) |
| 321 return; |
| 322 Advance(); |
| 323 LiteralBuffer name; |
| 324 while (c0_ >= 0 && !unicode_cache_->IsWhiteSpaceOrLineTerminator(c0_) && |
| 325 c0_ != '=') { |
| 326 name.AddChar(c0_); |
| 327 Advance(); |
| 328 } |
| 329 if (!name.is_one_byte()) return; |
| 330 Vector<const uint8_t> name_literal = name.one_byte_literal(); |
| 331 LiteralBuffer* value; |
| 332 if (name_literal == STATIC_ASCII_VECTOR("sourceURL")) { |
| 333 value = &source_url_; |
| 334 } else if (name_literal == STATIC_ASCII_VECTOR("sourceMappingURL")) { |
| 335 value = &source_mapping_url_; |
| 336 } else { |
| 337 return; |
| 338 } |
| 339 if (c0_ != '=') |
| 340 return; |
| 341 Advance(); |
| 342 value->Reset(); |
| 343 while (c0_ >= 0 && unicode_cache_->IsWhiteSpace(c0_)) { |
| 344 Advance(); |
| 345 } |
| 346 while (c0_ >= 0 && !unicode_cache_->IsLineTerminator(c0_)) { |
| 347 // Disallowed characters. |
| 348 if (c0_ == '"' || c0_ == '\'') { |
| 349 value->Reset(); |
| 350 return; |
| 351 } |
| 352 if (unicode_cache_->IsWhiteSpace(c0_)) { |
| 353 break; |
| 354 } |
| 355 value->AddChar(c0_); |
| 356 Advance(); |
| 357 } |
| 358 // Allow whitespace at the end. |
| 359 while (c0_ >= 0 && !unicode_cache_->IsLineTerminator(c0_)) { |
| 360 if (!unicode_cache_->IsWhiteSpace(c0_)) { |
| 361 value->Reset(); |
| 362 break; |
| 363 } |
| 364 Advance(); |
| 365 } |
| 366 } |
| 367 |
| 368 |
298 Token::Value Scanner::SkipMultiLineComment() { | 369 Token::Value Scanner::SkipMultiLineComment() { |
299 ASSERT(c0_ == '*'); | 370 ASSERT(c0_ == '*'); |
300 Advance(); | 371 Advance(); |
301 | 372 |
302 while (c0_ >= 0) { | 373 while (c0_ >= 0) { |
303 uc32 ch = c0_; | 374 uc32 ch = c0_; |
304 Advance(); | 375 Advance(); |
305 if (unicode_cache_->IsLineTerminator(ch)) { | 376 if (unicode_cache_->IsLineTerminator(ch)) { |
306 // Following ECMA-262, section 7.4, a comment containing | 377 // Following ECMA-262, section 7.4, a comment containing |
307 // a newline will make the comment count as a line-terminator. | 378 // a newline will make the comment count as a line-terminator. |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
452 | 523 |
453 case '%': | 524 case '%': |
454 // % %= | 525 // % %= |
455 token = Select('=', Token::ASSIGN_MOD, Token::MOD); | 526 token = Select('=', Token::ASSIGN_MOD, Token::MOD); |
456 break; | 527 break; |
457 | 528 |
458 case '/': | 529 case '/': |
459 // / // /* /= | 530 // / // /* /= |
460 Advance(); | 531 Advance(); |
461 if (c0_ == '/') { | 532 if (c0_ == '/') { |
462 token = SkipSingleLineComment(); | 533 Advance(); |
| 534 if (c0_ == '@' || c0_ == '#') { |
| 535 Advance(); |
| 536 token = SkipSourceURLComment(); |
| 537 } else { |
| 538 PushBack(c0_); |
| 539 token = SkipSingleLineComment(); |
| 540 } |
463 } else if (c0_ == '*') { | 541 } else if (c0_ == '*') { |
464 token = SkipMultiLineComment(); | 542 token = SkipMultiLineComment(); |
465 } else if (c0_ == '=') { | 543 } else if (c0_ == '=') { |
466 token = Select(Token::ASSIGN_DIV); | 544 token = Select(Token::ASSIGN_DIV); |
467 } else { | 545 } else { |
468 token = Token::DIV; | 546 token = Token::DIV; |
469 } | 547 } |
470 break; | 548 break; |
471 | 549 |
472 case '&': | 550 case '&': |
(...skipping 792 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1265 } | 1343 } |
1266 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)); |
1267 } | 1345 } |
1268 backing_store_.Add(static_cast<uint8_t>(one_byte_length & 0x7f)); | 1346 backing_store_.Add(static_cast<uint8_t>(one_byte_length & 0x7f)); |
1269 | 1347 |
1270 backing_store_.AddBlock(bytes); | 1348 backing_store_.AddBlock(bytes); |
1271 return backing_store_.EndSequence().start(); | 1349 return backing_store_.EndSequence().start(); |
1272 } | 1350 } |
1273 | 1351 |
1274 } } // namespace v8::internal | 1352 } } // namespace v8::internal |
OLD | NEW |