Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(251)

Side by Side Diff: src/scanner.cc

Issue 768203002: Simplify template literal raw string creation (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Add harmony unicode test Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/scanner.h ('k') | test/mjsunit/harmony/templates.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 16 matching lines...) Expand all
27 } 27 }
28 return isolate->factory()->InternalizeTwoByteString(two_byte_literal()); 28 return isolate->factory()->InternalizeTwoByteString(two_byte_literal());
29 } 29 }
30 30
31 31
32 // ---------------------------------------------------------------------------- 32 // ----------------------------------------------------------------------------
33 // Scanner 33 // Scanner
34 34
35 Scanner::Scanner(UnicodeCache* unicode_cache) 35 Scanner::Scanner(UnicodeCache* unicode_cache)
36 : unicode_cache_(unicode_cache), 36 : unicode_cache_(unicode_cache),
37 capturing_raw_literal_(false),
37 octal_pos_(Location::invalid()), 38 octal_pos_(Location::invalid()),
38 harmony_scoping_(false), 39 harmony_scoping_(false),
39 harmony_modules_(false), 40 harmony_modules_(false),
40 harmony_numeric_literals_(false), 41 harmony_numeric_literals_(false),
41 harmony_classes_(false), 42 harmony_classes_(false),
42 harmony_templates_(false), 43 harmony_templates_(false),
43 harmony_unicode_(false) {} 44 harmony_unicode_(false) {}
44 45
45 46
46 void Scanner::Initialize(Utf16CharacterStream* source) { 47 void Scanner::Initialize(Utf16CharacterStream* source) {
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 PushBack('-'); // undo Advance() 414 PushBack('-'); // undo Advance()
414 } 415 }
415 PushBack('!'); // undo Advance() 416 PushBack('!'); // undo Advance()
416 DCHECK(c0_ == '!'); 417 DCHECK(c0_ == '!');
417 return Token::LT; 418 return Token::LT;
418 } 419 }
419 420
420 421
421 void Scanner::Scan() { 422 void Scanner::Scan() {
422 next_.literal_chars = NULL; 423 next_.literal_chars = NULL;
424 next_.raw_literal_chars = NULL;
423 Token::Value token; 425 Token::Value token;
424 do { 426 do {
425 // Remember the position of the next token 427 // Remember the position of the next token
426 next_.location.beg_pos = source_pos(); 428 next_.location.beg_pos = source_pos();
427 429
428 switch (c0_) { 430 switch (c0_) {
429 case ' ': 431 case ' ':
430 case '\t': 432 case '\t':
431 Advance(); 433 Advance();
432 token = Token::WHITESPACE; 434 token = Token::WHITESPACE;
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after
812 // After parsing an Expression, the source position is incorrect due to 814 // After parsing an Expression, the source position is incorrect due to
813 // having scanned the brace. Push the RBRACE back into the stream. 815 // having scanned the brace. Push the RBRACE back into the stream.
814 PushBack('}'); 816 PushBack('}');
815 } 817 }
816 818
817 next_.location.beg_pos = source_pos(); 819 next_.location.beg_pos = source_pos();
818 Token::Value result = Token::TEMPLATE_SPAN; 820 Token::Value result = Token::TEMPLATE_SPAN;
819 DCHECK(c0_ == '`' || c0_ == '}'); 821 DCHECK(c0_ == '`' || c0_ == '}');
820 Advance(); // Consume ` or } 822 Advance(); // Consume ` or }
821 823
822 LiteralScope literal(this); 824 LiteralScope literal(this, true);
825
823 while (true) { 826 while (true) {
824 uc32 c = c0_; 827 uc32 c = c0_;
825 Advance(); 828 Advance();
826 if (c == '`') { 829 if (c == '`') {
827 result = Token::TEMPLATE_TAIL; 830 result = Token::TEMPLATE_TAIL;
831 ReduceRawLiteralLength(1);
828 break; 832 break;
829 } else if (c == '$' && c0_ == '{') { 833 } else if (c == '$' && c0_ == '{') {
830 Advance(); // Consume '{' 834 Advance(); // Consume '{'
835 ReduceRawLiteralLength(2);
831 break; 836 break;
832 } else if (c == '\\') { 837 } else if (c == '\\') {
833 if (unicode_cache_->IsLineTerminator(c0_)) { 838 if (unicode_cache_->IsLineTerminator(c0_)) {
834 // The TV of LineContinuation :: \ LineTerminatorSequence is the empty 839 // The TV of LineContinuation :: \ LineTerminatorSequence is the empty
835 // code unit sequence. 840 // code unit sequence.
836 uc32 lastChar = c0_; 841 uc32 lastChar = c0_;
837 Advance(); 842 Advance();
838 if (lastChar == '\r' && c0_ == '\n') Advance(); 843 if (lastChar == '\r') {
844 ReduceRawLiteralLength(1); // Remove \r
845 if (c0_ == '\n') {
846 Advance(); // Adds \n
847 } else {
848 AddRawLiteralChar('\n');
849 }
850 }
839 } else if (c0_ == '0') { 851 } else if (c0_ == '0') {
840 Advance(); 852 Advance();
841 AddLiteralChar('0'); 853 AddLiteralChar('0');
842 } else { 854 } else {
843 ScanEscape(); 855 ScanEscape();
844 } 856 }
845 } else if (c < 0) { 857 } else if (c < 0) {
846 // Unterminated template literal 858 // Unterminated template literal
847 PushBack(c); 859 PushBack(c);
848 break; 860 break;
849 } else { 861 } else {
850 // The TRV of LineTerminatorSequence :: <CR> is the CV 0x000A. 862 // The TRV of LineTerminatorSequence :: <CR> is the CV 0x000A.
851 // The TRV of LineTerminatorSequence :: <CR><LF> is the sequence 863 // The TRV of LineTerminatorSequence :: <CR><LF> is the sequence
852 // consisting of the CV 0x000A. 864 // consisting of the CV 0x000A.
853 if (c == '\r') { 865 if (c == '\r') {
854 if (c0_ == '\n') Advance(); 866 ReduceRawLiteralLength(1); // Remove \r
867 if (c0_ == '\n') {
868 Advance(); // Adds \n
869 } else {
870 AddRawLiteralChar('\n');
871 }
855 c = '\n'; 872 c = '\n';
856 } 873 }
857 AddLiteralChar(c); 874 AddLiteralChar(c);
858 } 875 }
859 } 876 }
860 literal.Complete(); 877 literal.Complete();
861 next_.location.end_pos = source_pos(); 878 next_.location.end_pos = source_pos();
862 next_.token = result; 879 next_.token = result;
863 return result; 880 return result;
864 } 881 }
(...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after
1278 1295
1279 1296
1280 const AstRawString* Scanner::NextSymbol(AstValueFactory* ast_value_factory) { 1297 const AstRawString* Scanner::NextSymbol(AstValueFactory* ast_value_factory) {
1281 if (is_next_literal_one_byte()) { 1298 if (is_next_literal_one_byte()) {
1282 return ast_value_factory->GetOneByteString(next_literal_one_byte_string()); 1299 return ast_value_factory->GetOneByteString(next_literal_one_byte_string());
1283 } 1300 }
1284 return ast_value_factory->GetTwoByteString(next_literal_two_byte_string()); 1301 return ast_value_factory->GetTwoByteString(next_literal_two_byte_string());
1285 } 1302 }
1286 1303
1287 1304
1305 const AstRawString* Scanner::CurrentRawSymbol(
1306 AstValueFactory* ast_value_factory) {
1307 if (is_raw_literal_one_byte()) {
1308 return ast_value_factory->GetOneByteString(raw_literal_one_byte_string());
1309 }
1310 return ast_value_factory->GetTwoByteString(raw_literal_two_byte_string());
1311 }
1312
1313
1288 double Scanner::DoubleValue() { 1314 double Scanner::DoubleValue() {
1289 DCHECK(is_literal_one_byte()); 1315 DCHECK(is_literal_one_byte());
1290 return StringToDouble( 1316 return StringToDouble(
1291 unicode_cache_, 1317 unicode_cache_,
1292 literal_one_byte_string(), 1318 literal_one_byte_string(),
1293 ALLOW_HEX | ALLOW_OCTAL | ALLOW_IMPLICIT_OCTAL | ALLOW_BINARY); 1319 ALLOW_HEX | ALLOW_OCTAL | ALLOW_IMPLICIT_OCTAL | ALLOW_BINARY);
1294 } 1320 }
1295 1321
1296 1322
1297 int Scanner::FindNumber(DuplicateFinder* finder, int value) { 1323 int Scanner::FindNumber(DuplicateFinder* finder, int value) {
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
1440 } 1466 }
1441 backing_store_.Add(static_cast<uint8_t>((one_byte_length >> 7) | 0x80u)); 1467 backing_store_.Add(static_cast<uint8_t>((one_byte_length >> 7) | 0x80u));
1442 } 1468 }
1443 backing_store_.Add(static_cast<uint8_t>(one_byte_length & 0x7f)); 1469 backing_store_.Add(static_cast<uint8_t>(one_byte_length & 0x7f));
1444 1470
1445 backing_store_.AddBlock(bytes); 1471 backing_store_.AddBlock(bytes);
1446 return backing_store_.EndSequence().start(); 1472 return backing_store_.EndSequence().start();
1447 } 1473 }
1448 1474
1449 } } // namespace v8::internal 1475 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/scanner.h ('k') | test/mjsunit/harmony/templates.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698