Chromium Code Reviews| 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 "src/parsing/scanner.h" | 7 #include "src/parsing/scanner.h" |
| 8 | 8 |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| (...skipping 930 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 941 case '2': // fall through | 941 case '2': // fall through |
| 942 case '3': // fall through | 942 case '3': // fall through |
| 943 case '4': // fall through | 943 case '4': // fall through |
| 944 case '5': // fall through | 944 case '5': // fall through |
| 945 case '6': // fall through | 945 case '6': // fall through |
| 946 case '7': | 946 case '7': |
| 947 c = ScanOctalEscape<capture_raw>(c, 2); | 947 c = ScanOctalEscape<capture_raw>(c, 2); |
| 948 break; | 948 break; |
| 949 } | 949 } |
| 950 | 950 |
| 951 // According to ECMA-262, section 7.8.4, characters not covered by the | 951 // Other escaped characters are interpreted as their non-escaped version. |
| 952 // above cases should be illegal, but they are commonly handled as | |
| 953 // non-escaped characters by JS VMs. | |
| 954 AddLiteralChar(c); | 952 AddLiteralChar(c); |
| 955 return true; | 953 return true; |
| 956 } | 954 } |
| 957 | 955 |
| 958 | 956 |
| 959 // Octal escapes of the forms '\0xx' and '\xxx' are not a part of | |
| 960 // ECMA-262. Other JS VMs support them. | |
| 961 template <bool capture_raw> | 957 template <bool capture_raw> |
| 962 uc32 Scanner::ScanOctalEscape(uc32 c, int length) { | 958 uc32 Scanner::ScanOctalEscape(uc32 c, int length) { |
| 963 uc32 x = c - '0'; | 959 uc32 x = c - '0'; |
| 964 int i = 0; | 960 int i = 0; |
| 965 for (; i < length; i++) { | 961 for (; i < length; i++) { |
| 966 int d = c0_ - '0'; | 962 int d = c0_ - '0'; |
| 967 if (d < 0 || d > 7) break; | 963 if (d < 0 || d > 7) break; |
| 968 int nx = x * 8 + d; | 964 int nx = x * 8 + d; |
| 969 if (nx >= 256) break; | 965 if (nx >= 256) break; |
| 970 x = nx; | 966 x = nx; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1032 // | } LiteralChars* ${ | 1028 // | } LiteralChars* ${ |
| 1033 // | 1029 // |
| 1034 // TEMPLATE_TAIL :: | 1030 // TEMPLATE_TAIL :: |
| 1035 // ` LiteralChars* ` | 1031 // ` LiteralChars* ` |
| 1036 // | } LiteralChar* ` | 1032 // | } LiteralChar* ` |
| 1037 // | 1033 // |
| 1038 // A TEMPLATE_SPAN should always be followed by an Expression, while a | 1034 // A TEMPLATE_SPAN should always be followed by an Expression, while a |
| 1039 // TEMPLATE_TAIL terminates a TemplateLiteral and does not need to be | 1035 // TEMPLATE_TAIL terminates a TemplateLiteral and does not need to be |
| 1040 // followed by an Expression. | 1036 // followed by an Expression. |
| 1041 | 1037 |
| 1038 // These scoped helpers save and restore the original error state, so that we | |
| 1039 // can specially treat invalid escape sequences in templates (which are | |
| 1040 // handled by the parser). | |
| 1041 ErrorState scanner_error_state(&scanner_error_, &scanner_error_location_); | |
| 1042 ErrorState octal_error_state(&octal_message_, &octal_pos_); | |
| 1043 | |
| 1042 Token::Value result = Token::TEMPLATE_SPAN; | 1044 Token::Value result = Token::TEMPLATE_SPAN; |
| 1043 LiteralScope literal(this); | 1045 LiteralScope literal(this); |
| 1044 StartRawLiteral(); | 1046 StartRawLiteral(); |
| 1045 const bool capture_raw = true; | 1047 const bool capture_raw = true; |
| 1046 const bool in_template_literal = true; | 1048 const bool in_template_literal = true; |
| 1047 while (true) { | 1049 while (true) { |
| 1048 uc32 c = c0_; | 1050 uc32 c = c0_; |
| 1049 Advance<capture_raw>(); | 1051 Advance<capture_raw>(); |
| 1050 if (c == '`') { | 1052 if (c == '`') { |
| 1051 result = Token::TEMPLATE_TAIL; | 1053 result = Token::TEMPLATE_TAIL; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 1062 uc32 lastChar = c0_; | 1064 uc32 lastChar = c0_; |
| 1063 Advance<capture_raw>(); | 1065 Advance<capture_raw>(); |
| 1064 if (lastChar == '\r') { | 1066 if (lastChar == '\r') { |
| 1065 ReduceRawLiteralLength(1); // Remove \r | 1067 ReduceRawLiteralLength(1); // Remove \r |
| 1066 if (c0_ == '\n') { | 1068 if (c0_ == '\n') { |
| 1067 Advance<capture_raw>(); // Adds \n | 1069 Advance<capture_raw>(); // Adds \n |
| 1068 } else { | 1070 } else { |
| 1069 AddRawLiteralChar('\n'); | 1071 AddRawLiteralChar('\n'); |
| 1070 } | 1072 } |
| 1071 } | 1073 } |
| 1072 } else if (!ScanEscape<capture_raw, in_template_literal>()) { | 1074 } else { |
| 1073 return Token::ILLEGAL; | 1075 ScanEscape<capture_raw, in_template_literal>(); |
|
vogelheim
2017/02/22 10:03:02
Maybe:
bool success = ScanEscape<...>
DCHECK_
bakkot1
2017/02/22 20:23:53
Seems worthwhile; done.
bakkot1
2017/02/22 20:41:13
Update: undone, since having `success` not be used
| |
| 1076 // For templates, invalid escape sequence checking is handled in the | |
| 1077 // parser. | |
| 1078 scanner_error_state.MoveErrorTo(&invalid_template_escape_message_, | |
| 1079 &invalid_template_escape_location_); | |
| 1080 octal_error_state.MoveErrorTo(&invalid_template_escape_message_, | |
| 1081 &invalid_template_escape_location_); | |
| 1074 } | 1082 } |
| 1075 } else if (c < 0) { | 1083 } else if (c < 0) { |
| 1076 // Unterminated template literal | 1084 // Unterminated template literal |
| 1077 PushBack(c); | 1085 PushBack(c); |
| 1078 break; | 1086 break; |
| 1079 } else { | 1087 } else { |
| 1080 // The TRV of LineTerminatorSequence :: <CR> is the CV 0x000A. | 1088 // The TRV of LineTerminatorSequence :: <CR> is the CV 0x000A. |
| 1081 // The TRV of LineTerminatorSequence :: <CR><LF> is the sequence | 1089 // The TRV of LineTerminatorSequence :: <CR><LF> is the sequence |
| 1082 // consisting of the CV 0x000A. | 1090 // consisting of the CV 0x000A. |
| 1083 if (c == '\r') { | 1091 if (c == '\r') { |
| 1084 ReduceRawLiteralLength(1); // Remove \r | 1092 ReduceRawLiteralLength(1); // Remove \r |
| 1085 if (c0_ == '\n') { | 1093 if (c0_ == '\n') { |
| 1086 Advance<capture_raw>(); // Adds \n | 1094 Advance<capture_raw>(); // Adds \n |
| 1087 } else { | 1095 } else { |
| 1088 AddRawLiteralChar('\n'); | 1096 AddRawLiteralChar('\n'); |
| 1089 } | 1097 } |
| 1090 c = '\n'; | 1098 c = '\n'; |
| 1091 } | 1099 } |
| 1092 AddLiteralChar(c); | 1100 AddLiteralChar(c); |
| 1093 } | 1101 } |
| 1094 } | 1102 } |
| 1095 literal.Complete(); | 1103 literal.Complete(); |
| 1096 next_.location.end_pos = source_pos(); | 1104 next_.location.end_pos = source_pos(); |
| 1097 next_.token = result; | 1105 next_.token = result; |
| 1106 | |
| 1098 return result; | 1107 return result; |
| 1099 } | 1108 } |
| 1100 | 1109 |
| 1101 | 1110 |
| 1102 Token::Value Scanner::ScanTemplateStart() { | 1111 Token::Value Scanner::ScanTemplateStart() { |
| 1103 DCHECK(next_next_.token == Token::UNINITIALIZED); | 1112 DCHECK(next_next_.token == Token::UNINITIALIZED); |
| 1104 DCHECK(c0_ == '`'); | 1113 DCHECK(c0_ == '`'); |
| 1105 next_.location.beg_pos = source_pos(); | 1114 next_.location.beg_pos = source_pos(); |
| 1106 Advance(); // Consume ` | 1115 Advance(); // Consume ` |
| 1107 return ScanTemplateSpan(); | 1116 return ScanTemplateSpan(); |
| (...skipping 583 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1691 // 2, reset the source to the desired position, | 1700 // 2, reset the source to the desired position, |
| 1692 source_->Seek(position); | 1701 source_->Seek(position); |
| 1693 // 3, re-scan, by scanning the look-ahead char + 1 token (next_). | 1702 // 3, re-scan, by scanning the look-ahead char + 1 token (next_). |
| 1694 c0_ = source_->Advance(); | 1703 c0_ = source_->Advance(); |
| 1695 Next(); | 1704 Next(); |
| 1696 DCHECK_EQ(next_.location.beg_pos, static_cast<int>(position)); | 1705 DCHECK_EQ(next_.location.beg_pos, static_cast<int>(position)); |
| 1697 } | 1706 } |
| 1698 | 1707 |
| 1699 } // namespace internal | 1708 } // namespace internal |
| 1700 } // namespace v8 | 1709 } // namespace v8 |
| OLD | NEW |