}
-template <bool capture_raw>
+template <bool capture_raw, bool in_template_literal>
bool Scanner::ScanEscape() {
uc32 c = c0_;
Advance<capture_raw>();
// Skip escaped newlines.
- if (c0_ >= 0 && unicode_cache_->IsLineTerminator(c)) {
+ if (!in_template_literal && c0_ >= 0 && unicode_cache_->IsLineTerminator(c)) {
// Allow CR+LF newlines in multiline string literals.
if (IsCarriageReturn(c) && IsLineFeed(c0_)) Advance<capture_raw>();
// Allow LF+CR newlines in multiline string literals.
if (c < 0) return false;
break;
}
- case 'v' : c = '\v'; break;
- case 'x' : {
+ case 'v':
+ c = '\v';
+ break;
+ case 'x': {
c = ScanHexNumber<capture_raw>(2);
if (c < 0) return false;
break;
}
- case '0' : // fall through
- case '1' : // fall through
- case '2' : // fall through
- case '3' : // fall through
- case '4' : // fall through
- case '5' : // fall through
- case '6' : // fall through
+ case '0':
+ if (in_template_literal) {
+ // \ 0 DecimalDigit is never allowed in templates.
+ if (IsDecimalDigit(c0_)) {
+ Advance<capture_raw>(); // Advance to include the problematic char.
+ return false;
+ }
+
+ // The TV of TemplateCharacter :: \ EscapeSequence is the CV of
+ // EscapeSequence.
+ // The CV of EscapeSequence :: 0 is the code unit value 0.
+ c = 0;
+ break;
+ }
+ // Fall through.
+ case '1': // fall through
+ case '2': // fall through
+ case '3': // fall through
+ case '4': // fall through
+ case '5': // fall through
+ case '6': // fall through
case '7':
- c = ScanOctalEscape<capture_raw>(c, 2);
- break;
+ if (!in_template_literal) {
+ c = ScanOctalEscape<capture_raw>(c, 2);
+ break;
+ }
+ // Fall through
+ case '8':
+ case '9':
+ if (in_template_literal) return false;
}
// According to ECMA-262, section 7.8.4, characters not covered by the
uc32 c = c0_;
Advance();
if (c == '\\') {
- if (c0_ < 0 || !ScanEscape<false>()) return Token::ILLEGAL;
+ if (c0_ < 0 || !ScanEscape<false, false>()) return Token::ILLEGAL;
} else {
AddLiteralChar(c);
}
LiteralScope literal(this);
StartRawLiteral();
const bool capture_raw = true;
+ const bool in_template_literal = true;
while (true) {
uc32 c = c0_;
AddRawLiteralChar('\n');
}
}
- } else if (c0_ == '0') {
- Advance<capture_raw>();
- AddLiteralChar('0');
- } else {
- ScanEscape<true>();
+ } else if (!ScanEscape<capture_raw, in_template_literal>()) {
+ return Token::ILLEGAL;
}
} else if (c < 0) {
// Unterminated template literal
// The TRV of CharacterEscapeSequence :: NonEscapeCharacter is the CV of the
// NonEscapeCharacter.
calls = 0;
- (function(s) { calls++; assertEquals("\u005Cx", s.raw[0]); })`\x`;
+ (function(s) { calls++; assertEquals("\u005Cz", s.raw[0]); })`\z`;
assertEquals(1, calls);
// The TRV of LineTerminatorSequence :: <LF> is the code unit value 0x000A.
// block
}`jkl`;
})();
+
+
+(function testLegacyOctal() {
+ assertEquals('\u0000', `\0`);
+ assertEquals('\u0000a', `\0a`);
+ for (var i = 0; i < 10; i++) {
+ var code = "`\\0" + i + "`";
+ assertThrows(code, SyntaxError);
+ }
+
+ assertEquals('\\0', String.raw`\0`);
+})();
+
+
+(function testSyntaxErrorsNonEscapeCharacter() {
+ assertThrows("`\\x`", SyntaxError);
+ assertThrows("`\\u`", SyntaxError);
+ for (var i = 1; i < 10; i++) {
+ var code = "`\\" + i + "`";
+ assertThrows(code, SyntaxError);
+ }
+})();