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

Side by Side Diff: src/scanner.cc

Issue 706263002: Implement ES6 unicode escapes. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: . Created 6 years, 1 month 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 | Annotate | Revision Log
« no previous file with comments | « src/scanner.h ('k') | test/cctest/test-parsing.cc » ('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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 return -1; 64 return -1;
65 } 65 }
66 x = x * 16 + d; 66 x = x * 16 + d;
67 Advance(); 67 Advance();
68 } 68 }
69 69
70 return x; 70 return x;
71 } 71 }
72 72
73 73
74 uc32 Scanner::ScanUnlimitedLengthHexNumber(int max_value) {
75 uc32 x = 0;
76 int d = HexValue(c0_);
77 if (d < 0) {
78 return -1;
79 }
80 while (d >= 0) {
81 x = x * 16 + d;
82 if (x > max_value) return -1;
83 Advance();
84 d = HexValue(c0_);
85 }
86 return x;
87 }
88
89
74 // Ensure that tokens can be stored in a byte. 90 // Ensure that tokens can be stored in a byte.
75 STATIC_ASSERT(Token::NUM_TOKENS <= 0x100); 91 STATIC_ASSERT(Token::NUM_TOKENS <= 0x100);
76 92
77 // Table of one-character tokens, by character (0x00..0x7f only). 93 // Table of one-character tokens, by character (0x00..0x7f only).
78 static const byte one_char_tokens[] = { 94 static const byte one_char_tokens[] = {
79 Token::ILLEGAL, 95 Token::ILLEGAL,
80 Token::ILLEGAL, 96 Token::ILLEGAL,
81 Token::ILLEGAL, 97 Token::ILLEGAL,
82 Token::ILLEGAL, 98 Token::ILLEGAL,
83 Token::ILLEGAL, 99 Token::ILLEGAL,
(...skipping 582 matching lines...) Expand 10 before | Expand all | Expand 10 after
666 // whether there was a line terminator in the part we skip. 682 // whether there was a line terminator in the part we skip.
667 has_line_terminator_before_next_ = false; 683 has_line_terminator_before_next_ = false;
668 has_multiline_comment_before_next_ = false; 684 has_multiline_comment_before_next_ = false;
669 } 685 }
670 Scan(); 686 Scan();
671 } 687 }
672 688
673 689
674 bool Scanner::ScanEscape() { 690 bool Scanner::ScanEscape() {
675 uc32 c = c0_; 691 uc32 c = c0_;
692 uc32 c2 = -1;
676 Advance(); 693 Advance();
677 694
678 // Skip escaped newlines. 695 // Skip escaped newlines.
679 if (c0_ >= 0 && unicode_cache_->IsLineTerminator(c)) { 696 if (c0_ >= 0 && unicode_cache_->IsLineTerminator(c)) {
680 // Allow CR+LF newlines in multiline string literals. 697 // Allow CR+LF newlines in multiline string literals.
681 if (IsCarriageReturn(c) && IsLineFeed(c0_)) Advance(); 698 if (IsCarriageReturn(c) && IsLineFeed(c0_)) Advance();
682 // Allow LF+CR newlines in multiline string literals. 699 // Allow LF+CR newlines in multiline string literals.
683 if (IsLineFeed(c) && IsCarriageReturn(c0_)) Advance(); 700 if (IsLineFeed(c) && IsCarriageReturn(c0_)) Advance();
684 return true; 701 return true;
685 } 702 }
686 703
687 switch (c) { 704 switch (c) {
688 case '\'': // fall through 705 case '\'': // fall through
689 case '"' : // fall through 706 case '"' : // fall through
690 case '\\': break; 707 case '\\': break;
691 case 'b' : c = '\b'; break; 708 case 'b' : c = '\b'; break;
692 case 'f' : c = '\f'; break; 709 case 'f' : c = '\f'; break;
693 case 'n' : c = '\n'; break; 710 case 'n' : c = '\n'; break;
694 case 'r' : c = '\r'; break; 711 case 'r' : c = '\r'; break;
695 case 't' : c = '\t'; break; 712 case 't' : c = '\t'; break;
696 case 'u' : { 713 case 'u' : {
697 c = ScanHexNumber(4); 714 c = ScanUnicodeEscape();
698 if (c < 0) return false; 715 if (c < 0) return false;
699 break; 716 break;
700 } 717 }
701 case 'v' : c = '\v'; break; 718 case 'v' : c = '\v'; break;
702 case 'x' : { 719 case 'x' : {
703 c = ScanHexNumber(2); 720 c = ScanHexNumber(2);
704 if (c < 0) return false; 721 if (c < 0) return false;
705 break; 722 break;
706 } 723 }
707 case '0' : // fall through 724 case '0' : // fall through
708 case '1' : // fall through 725 case '1' : // fall through
709 case '2' : // fall through 726 case '2' : // fall through
710 case '3' : // fall through 727 case '3' : // fall through
711 case '4' : // fall through 728 case '4' : // fall through
712 case '5' : // fall through 729 case '5' : // fall through
713 case '6' : // fall through 730 case '6' : // fall through
714 case '7' : c = ScanOctalEscape(c, 2); break; 731 case '7' : c = ScanOctalEscape(c, 2); break;
715 } 732 }
716 733
717 // According to ECMA-262, section 7.8.4, characters not covered by the 734 // According to ECMA-262, section 7.8.4, characters not covered by the
718 // above cases should be illegal, but they are commonly handled as 735 // above cases should be illegal, but they are commonly handled as
719 // non-escaped characters by JS VMs. 736 // non-escaped characters by JS VMs.
720 AddLiteralChar(c); 737 AddLiteralChar(c);
738 if (c2 >= 0) {
739 AddLiteralChar(c2);
740 }
721 return true; 741 return true;
722 } 742 }
723 743
724 744
725 // Octal escapes of the forms '\0xx' and '\xxx' are not a part of 745 // Octal escapes of the forms '\0xx' and '\xxx' are not a part of
726 // ECMA-262. Other JS VMs support them. 746 // ECMA-262. Other JS VMs support them.
727 uc32 Scanner::ScanOctalEscape(uc32 c, int length) { 747 uc32 Scanner::ScanOctalEscape(uc32 c, int length) {
728 uc32 x = c - '0'; 748 uc32 x = c - '0';
729 int i = 0; 749 int i = 0;
730 for (; i < length; i++) { 750 for (; i < length; i++) {
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
880 literal.Complete(); 900 literal.Complete();
881 901
882 return Token::NUMBER; 902 return Token::NUMBER;
883 } 903 }
884 904
885 905
886 uc32 Scanner::ScanIdentifierUnicodeEscape() { 906 uc32 Scanner::ScanIdentifierUnicodeEscape() {
887 Advance(); 907 Advance();
888 if (c0_ != 'u') return -1; 908 if (c0_ != 'u') return -1;
889 Advance(); 909 Advance();
910 return ScanUnicodeEscape();
911 }
912
913
914 uc32 Scanner::ScanUnicodeEscape() {
915 // Accept both \uxxxx and \u{xxxxxx}. In the latter case, the number of hex
916 // digits between { } is arbitrary. \ and u have already been read.
917 if (c0_ == '{') {
918 Advance();
919 uc32 cp = ScanUnlimitedLengthHexNumber(0x10ffff);
920 if (cp < 0) {
921 return -1;
922 }
923 if (c0_ != '}') {
924 return -1;
925 }
926 Advance();
927 return cp;
928 }
890 return ScanHexNumber(4); 929 return ScanHexNumber(4);
891 } 930 }
892 931
893 932
894 // ---------------------------------------------------------------------------- 933 // ----------------------------------------------------------------------------
895 // Keyword Matcher 934 // Keyword Matcher
896 935
897 #define KEYWORDS(KEYWORD_GROUP, KEYWORD) \ 936 #define KEYWORDS(KEYWORD_GROUP, KEYWORD) \
898 KEYWORD_GROUP('b') \ 937 KEYWORD_GROUP('b') \
899 KEYWORD("break", Token::BREAK) \ 938 KEYWORD("break", Token::BREAK) \
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after
1326 } 1365 }
1327 backing_store_.Add(static_cast<uint8_t>((one_byte_length >> 7) | 0x80u)); 1366 backing_store_.Add(static_cast<uint8_t>((one_byte_length >> 7) | 0x80u));
1328 } 1367 }
1329 backing_store_.Add(static_cast<uint8_t>(one_byte_length & 0x7f)); 1368 backing_store_.Add(static_cast<uint8_t>(one_byte_length & 0x7f));
1330 1369
1331 backing_store_.AddBlock(bytes); 1370 backing_store_.AddBlock(bytes);
1332 return backing_store_.EndSequence().start(); 1371 return backing_store_.EndSequence().start();
1333 } 1372 }
1334 1373
1335 } } // namespace v8::internal 1374 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/scanner.h ('k') | test/cctest/test-parsing.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698