summaryrefslogtreecommitdiffstats
path: root/js/src/frontend/TokenStream.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'js/src/frontend/TokenStream.cpp')
-rw-r--r--js/src/frontend/TokenStream.cpp484
1 files changed, 309 insertions, 175 deletions
diff --git a/js/src/frontend/TokenStream.cpp b/js/src/frontend/TokenStream.cpp
index 179a7c244..b8623d545 100644
--- a/js/src/frontend/TokenStream.cpp
+++ b/js/src/frontend/TokenStream.cpp
@@ -8,6 +8,7 @@
#include "frontend/TokenStream.h"
+#include "mozilla/ArrayUtils.h"
#include "mozilla/IntegerTypeTraits.h"
#include "mozilla/PodOperations.h"
@@ -23,80 +24,81 @@
#include "jsnum.h"
#include "frontend/BytecodeCompiler.h"
+#include "frontend/ReservedWords.h"
#include "js/CharacterEncoding.h"
#include "js/UniquePtr.h"
#include "vm/HelperThreads.h"
-#include "vm/Keywords.h"
#include "vm/StringBuffer.h"
#include "vm/Unicode.h"
using namespace js;
using namespace js::frontend;
+using mozilla::ArrayLength;
using mozilla::Maybe;
using mozilla::PodAssign;
using mozilla::PodCopy;
using mozilla::PodZero;
-struct KeywordInfo {
- const char* chars; // C string with keyword text
+struct ReservedWordInfo {
+ const char* chars; // C string with reserved word text
TokenKind tokentype;
};
-static const KeywordInfo keywords[] = {
-#define KEYWORD_INFO(keyword, name, type) \
- {js_##keyword##_str, type},
- FOR_EACH_JAVASCRIPT_KEYWORD(KEYWORD_INFO)
-#undef KEYWORD_INFO
+static const ReservedWordInfo reservedWords[] = {
+#define RESERVED_WORD_INFO(word, name, type) \
+ {js_##word##_str, type},
+ FOR_EACH_JAVASCRIPT_RESERVED_WORD(RESERVED_WORD_INFO)
+#undef RESERVED_WORD_INFO
};
-// Returns a KeywordInfo for the specified characters, or nullptr if the string
-// is not a keyword.
+// Returns a ReservedWordInfo for the specified characters, or nullptr if the
+// string is not a reserved word.
template <typename CharT>
-static const KeywordInfo*
-FindKeyword(const CharT* s, size_t length)
+static const ReservedWordInfo*
+FindReservedWord(const CharT* s, size_t length)
{
MOZ_ASSERT(length != 0);
size_t i;
- const KeywordInfo* kw;
+ const ReservedWordInfo* rw;
const char* chars;
-#define JSKW_LENGTH() length
-#define JSKW_AT(column) s[column]
-#define JSKW_GOT_MATCH(index) i = (index); goto got_match;
-#define JSKW_TEST_GUESS(index) i = (index); goto test_guess;
-#define JSKW_NO_MATCH() goto no_match;
-#include "jsautokw.h"
-#undef JSKW_NO_MATCH
-#undef JSKW_TEST_GUESS
-#undef JSKW_GOT_MATCH
-#undef JSKW_AT
-#undef JSKW_LENGTH
+#define JSRW_LENGTH() length
+#define JSRW_AT(column) s[column]
+#define JSRW_GOT_MATCH(index) i = (index); goto got_match;
+#define JSRW_TEST_GUESS(index) i = (index); goto test_guess;
+#define JSRW_NO_MATCH() goto no_match;
+#include "frontend/ReservedWordsGenerated.h"
+#undef JSRW_NO_MATCH
+#undef JSRW_TEST_GUESS
+#undef JSRW_GOT_MATCH
+#undef JSRW_AT
+#undef JSRW_LENGTH
got_match:
- return &keywords[i];
+ return &reservedWords[i];
test_guess:
- kw = &keywords[i];
- chars = kw->chars;
+ rw = &reservedWords[i];
+ chars = rw->chars;
do {
if (*s++ != (unsigned char)(*chars++))
goto no_match;
} while (--length != 0);
- return kw;
+ return rw;
no_match:
return nullptr;
}
-static const KeywordInfo*
-FindKeyword(JSLinearString* str)
+static const ReservedWordInfo*
+FindReservedWord(JSLinearString* str)
{
JS::AutoCheckCannotGC nogc;
return str->hasLatin1Chars()
- ? FindKeyword(str->latin1Chars(nogc), str->length())
- : FindKeyword(str->twoByteChars(nogc), str->length());
+ ? FindReservedWord(str->latin1Chars(nogc), str->length())
+ : FindReservedWord(str->twoByteChars(nogc), str->length());
}
template <typename CharT>
@@ -172,6 +174,12 @@ frontend::IsIdentifier(JSLinearString* str)
}
bool
+frontend::IsIdentifier(const char* chars, size_t length)
+{
+ return ::IsIdentifier(chars, length);
+}
+
+bool
frontend::IsIdentifier(const char16_t* chars, size_t length)
{
return ::IsIdentifier(chars, length);
@@ -180,7 +188,68 @@ frontend::IsIdentifier(const char16_t* chars, size_t length)
bool
frontend::IsKeyword(JSLinearString* str)
{
- return FindKeyword(str) != nullptr;
+ if (const ReservedWordInfo* rw = FindReservedWord(str))
+ return TokenKindIsKeyword(rw->tokentype);
+
+ return false;
+}
+
+bool
+frontend::IsFutureReservedWord(JSLinearString* str)
+{
+ if (const ReservedWordInfo* rw = FindReservedWord(str))
+ return TokenKindIsFutureReservedWord(rw->tokentype);
+
+ return false;
+}
+
+bool
+frontend::IsStrictReservedWord(JSLinearString* str)
+{
+ if (const ReservedWordInfo* rw = FindReservedWord(str))
+ return TokenKindIsStrictReservedWord(rw->tokentype);
+
+ return false;
+}
+
+bool
+frontend::IsReservedWordLiteral(JSLinearString* str)
+{
+ if (const ReservedWordInfo* rw = FindReservedWord(str))
+ return TokenKindIsReservedWordLiteral(rw->tokentype);
+
+ return false;
+}
+
+const char*
+frontend::ReservedWordToCharZ(PropertyName* str)
+{
+ const ReservedWordInfo* rw = FindReservedWord(str);
+ if (rw == nullptr)
+ return nullptr;
+
+ switch (rw->tokentype) {
+#define EMIT_CASE(word, name, type) case type: return js_##word##_str;
+ FOR_EACH_JAVASCRIPT_RESERVED_WORD(EMIT_CASE)
+#undef EMIT_CASE
+ default:
+ MOZ_ASSERT_UNREACHABLE("Not a reserved word PropertyName.");
+ }
+ return nullptr;
+}
+
+PropertyName*
+TokenStream::reservedWordToPropertyName(TokenKind tt) const
+{
+ MOZ_ASSERT(tt != TOK_NAME);
+ switch (tt) {
+#define EMIT_CASE(word, name, type) case type: return cx->names().name;
+ FOR_EACH_JAVASCRIPT_RESERVED_WORD(EMIT_CASE)
+#undef EMIT_CASE
+ default:
+ MOZ_ASSERT_UNREACHABLE("Not a reserved word TokenKind.");
+ }
+ return nullptr;
}
TokenStream::SourceCoords::SourceCoords(ExclusiveContext* cx, uint32_t ln)
@@ -217,8 +286,13 @@ TokenStream::SourceCoords::add(uint32_t lineNum, uint32_t lineStartOffset)
// only if lineStartOffsets_.append succeeds, to keep sentinel.
// Otherwise return false to tell TokenStream about OOM.
uint32_t maxPtr = MAX_PTR;
- if (!lineStartOffsets_.append(maxPtr))
+ if (!lineStartOffsets_.append(maxPtr)) {
+ static_assert(mozilla::IsSame<decltype(lineStartOffsets_.allocPolicy()),
+ TempAllocPolicy&>::value,
+ "this function's caller depends on it reporting an "
+ "error on failure, as TempAllocPolicy ensures");
return false;
+ }
lineStartOffsets_[lineIndex] = lineStartOffset;
} else {
@@ -548,8 +622,9 @@ TokenStream::advance(size_t position)
MOZ_MAKE_MEM_UNDEFINED(&cur->type, sizeof(cur->type));
lookahead = 0;
- if (flags.hitOOM)
- return reportError(JSMSG_OUT_OF_MEMORY);
+ if (flags.hitOOM) {
+ return false;
+ }
return true;
}
@@ -593,8 +668,8 @@ TokenStream::seek(const Position& pos, const TokenStream& other)
}
bool
-TokenStream::reportStrictModeErrorNumberVA(uint32_t offset, bool strictMode, unsigned errorNumber,
- va_list args)
+TokenStream::reportStrictModeErrorNumberVA(UniquePtr<JSErrorNotes> notes, uint32_t offset,
+ bool strictMode, unsigned errorNumber, va_list args)
{
// In strict mode code, this is an error, not merely a warning.
unsigned flags;
@@ -605,7 +680,7 @@ TokenStream::reportStrictModeErrorNumberVA(uint32_t offset, bool strictMode, uns
else
return true;
- return reportCompileErrorNumberVA(offset, flags, errorNumber, args);
+ return reportCompileErrorNumberVA(Move(notes), offset, flags, errorNumber, args);
}
void
@@ -631,8 +706,8 @@ CompileError::throwError(JSContext* cx)
}
bool
-TokenStream::reportCompileErrorNumberVA(uint32_t offset, unsigned flags, unsigned errorNumber,
- va_list args)
+TokenStream::reportCompileErrorNumberVA(UniquePtr<JSErrorNotes> notes, uint32_t offset,
+ unsigned flags, unsigned errorNumber, va_list args)
{
bool warning = JSREPORT_IS_WARNING(flags);
@@ -649,6 +724,7 @@ TokenStream::reportCompileErrorNumberVA(uint32_t offset, unsigned flags, unsigne
return false;
CompileError& err = *tempErrPtr;
+ err.notes = Move(notes);
err.flags = flags;
err.errorNumber = errorNumber;
err.filename = filename;
@@ -740,7 +816,7 @@ TokenStream::reportStrictModeError(unsigned errorNumber, ...)
{
va_list args;
va_start(args, errorNumber);
- bool result = reportStrictModeErrorNumberVA(currentToken().pos.begin, strictMode(),
+ bool result = reportStrictModeErrorNumberVA(nullptr, currentToken().pos.begin, strictMode(),
errorNumber, args);
va_end(args);
return result;
@@ -751,8 +827,8 @@ TokenStream::reportError(unsigned errorNumber, ...)
{
va_list args;
va_start(args, errorNumber);
- bool result = reportCompileErrorNumberVA(currentToken().pos.begin, JSREPORT_ERROR, errorNumber,
- args);
+ bool result = reportCompileErrorNumberVA(nullptr, currentToken().pos.begin, JSREPORT_ERROR,
+ errorNumber, args);
va_end(args);
return result;
}
@@ -762,30 +838,32 @@ TokenStream::reportErrorNoOffset(unsigned errorNumber, ...)
{
va_list args;
va_start(args, errorNumber);
- bool result = reportCompileErrorNumberVA(NoOffset, JSREPORT_ERROR, errorNumber,
- args);
+ bool result = reportCompileErrorNumberVA(nullptr, NoOffset, JSREPORT_ERROR,
+ errorNumber, args);
va_end(args);
return result;
}
bool
-TokenStream::reportWarning(unsigned errorNumber, ...)
+TokenStream::warning(unsigned errorNumber, ...)
{
va_list args;
va_start(args, errorNumber);
- bool result = reportCompileErrorNumberVA(currentToken().pos.begin, JSREPORT_WARNING,
+ bool result = reportCompileErrorNumberVA(nullptr, currentToken().pos.begin, JSREPORT_WARNING,
errorNumber, args);
va_end(args);
return result;
}
bool
-TokenStream::reportStrictWarningErrorNumberVA(uint32_t offset, unsigned errorNumber, va_list args)
+TokenStream::reportExtraWarningErrorNumberVA(UniquePtr<JSErrorNotes> notes, uint32_t offset,
+ unsigned errorNumber, va_list args)
{
if (!options().extraWarningsOption)
return true;
- return reportCompileErrorNumberVA(offset, JSREPORT_STRICT|JSREPORT_WARNING, errorNumber, args);
+ return reportCompileErrorNumberVA(Move(notes), offset, JSREPORT_STRICT|JSREPORT_WARNING,
+ errorNumber, args);
}
void
@@ -796,7 +874,34 @@ TokenStream::reportAsmJSError(uint32_t offset, unsigned errorNumber, ...)
unsigned flags = options().throwOnAsmJSValidationFailureOption
? JSREPORT_ERROR
: JSREPORT_WARNING;
- reportCompileErrorNumberVA(offset, flags, errorNumber, args);
+ reportCompileErrorNumberVA(nullptr, offset, flags, errorNumber, args);
+ va_end(args);
+}
+
+void
+TokenStream::error(unsigned errorNumber, ...)
+{
+ va_list args;
+ va_start(args, errorNumber);
+#ifdef DEBUG
+ bool result =
+#endif
+ reportCompileErrorNumberVA(nullptr, currentToken().pos.begin, JSREPORT_ERROR,
+ errorNumber, args);
+ MOZ_ASSERT(!result, "reporting an error returned true?");
+ va_end(args);
+}
+
+void
+TokenStream::errorAt(uint32_t offset, unsigned errorNumber, ...)
+{
+ va_list args;
+ va_start(args, errorNumber);
+#ifdef DEBUG
+ bool result =
+#endif
+ reportCompileErrorNumberVA(nullptr, offset, JSREPORT_ERROR, errorNumber, args);
+ MOZ_ASSERT(!result, "reporting an error returned true?");
va_end(args);
}
@@ -928,34 +1033,49 @@ TokenStream::getDirectives(bool isMultiline, bool shouldWarnDeprecated)
bool
TokenStream::getDirective(bool isMultiline, bool shouldWarnDeprecated,
- const char* directive, int directiveLength,
+ const char* directive, uint8_t directiveLength,
const char* errorMsgPragma,
UniqueTwoByteChars* destination)
{
MOZ_ASSERT(directiveLength <= 18);
char16_t peeked[18];
- int32_t c;
if (peekChars(directiveLength, peeked) && CharsMatch(peeked, directive)) {
- if (shouldWarnDeprecated &&
- !reportWarning(JSMSG_DEPRECATED_PRAGMA, errorMsgPragma))
- return false;
+ if (shouldWarnDeprecated) {
+ if (!warning(JSMSG_DEPRECATED_PRAGMA, errorMsgPragma))
+ return false;
+ }
skipChars(directiveLength);
tokenbuf.clear();
- while ((c = peekChar()) && c != EOF && !unicode::IsSpaceOrBOM2(c)) {
- getChar();
+ do {
+ int32_t c;
+ if (!peekChar(&c))
+ return false;
+
+ if (c == EOF || unicode::IsSpaceOrBOM2(c))
+ break;
+
+ consumeKnownChar(c);
+
// Debugging directives can occur in both single- and multi-line
// comments. If we're currently inside a multi-line comment, we also
// need to recognize multi-line comment terminators.
- if (isMultiline && c == '*' && peekChar() == '/') {
- ungetChar('*');
- break;
+ if (isMultiline && c == '*') {
+ int32_t c2;
+ if (!peekChar(&c2))
+ return false;
+
+ if (c2 == '/') {
+ ungetChar('*');
+ break;
+ }
}
+
if (!tokenbuf.append(c))
return false;
- }
+ } while (true);
if (tokenbuf.empty()) {
// The directive's URL was missing, but this is not quite an
@@ -987,7 +1107,10 @@ TokenStream::getDisplayURL(bool isMultiline, bool shouldWarnDeprecated)
// developer would like to refer to the source as from the source's actual
// URL.
- return getDirective(isMultiline, shouldWarnDeprecated, " sourceURL=", 11,
+ static const char sourceURLDirective[] = " sourceURL=";
+ constexpr uint8_t sourceURLDirectiveLength = ArrayLength(sourceURLDirective) - 1;
+ return getDirective(isMultiline, shouldWarnDeprecated,
+ sourceURLDirective, sourceURLDirectiveLength,
"sourceURL", &displayURL_);
}
@@ -997,7 +1120,10 @@ TokenStream::getSourceMappingURL(bool isMultiline, bool shouldWarnDeprecated)
// Match comments of the form "//# sourceMappingURL=<url>" or
// "/\* //# sourceMappingURL=<url> *\/"
- return getDirective(isMultiline, shouldWarnDeprecated, " sourceMappingURL=", 18,
+ static const char sourceMappingURLDirective[] = " sourceMappingURL=";
+ constexpr uint8_t sourceMappingURLDirectiveLength = ArrayLength(sourceMappingURLDirective) - 1;
+ return getDirective(isMultiline, shouldWarnDeprecated,
+ sourceMappingURLDirective, sourceMappingURLDirectiveLength,
"sourceMappingURL", &sourceMapURL_);
}
@@ -1104,36 +1230,6 @@ TokenStream::putIdentInTokenbuf(const char16_t* identStart)
return true;
}
-bool
-TokenStream::checkForKeyword(const KeywordInfo* kw, TokenKind* ttp)
-{
- if (!awaitIsKeyword && kw->tokentype == TOK_AWAIT) {
- if (ttp)
- *ttp = TOK_NAME;
- return true;
- }
-
- if (kw->tokentype == TOK_RESERVED)
- return reportError(JSMSG_RESERVED_ID, kw->chars);
-
- if (kw->tokentype == TOK_STRICT_RESERVED)
- return reportStrictModeError(JSMSG_RESERVED_ID, kw->chars);
-
- // Working keyword.
- *ttp = kw->tokentype;
- return true;
-}
-
-bool
-TokenStream::checkForKeyword(JSAtom* atom, TokenKind* ttp)
-{
- const KeywordInfo* kw = FindKeyword(atom);
- if (!kw)
- return true;
-
- return checkForKeyword(kw, ttp);
-}
-
enum FirstCharKind {
// A char16_t has the 'OneChar' kind if it, by itself, constitutes a valid
// token that cannot also be a prefix of a longer token. E.g. ';' has the
@@ -1357,36 +1453,18 @@ TokenStream::getTokenInternal(TokenKind* ttp, Modifier modifier)
length = userbuf.addressOfNextRawChar() - identStart;
}
- // Represent keywords as keyword tokens unless told otherwise.
- if (modifier != KeywordIsName) {
- if (const KeywordInfo* kw = FindKeyword(chars, length)) {
- // That said, keywords can't contain escapes. (Contexts where
- // keywords are treated as names, that also sometimes treat
- // keywords as keywords, must manually check this requirement.)
- // There are two exceptions
- // 1) StrictReservedWords: These keywords need to be treated as
- // names in non-strict mode.
- // 2) yield is also treated as a name if it contains an escape
- // sequence. The parser must handle this case separately.
- if (hadUnicodeEscape && !(
- (kw->tokentype == TOK_STRICT_RESERVED && !strictMode()) ||
- kw->tokentype == TOK_YIELD))
- {
- reportError(JSMSG_ESCAPED_KEYWORD);
- goto error;
- }
-
- tp->type = TOK_NAME;
- if (!checkForKeyword(kw, &tp->type))
- goto error;
- if (tp->type != TOK_NAME && !hadUnicodeEscape)
- goto out;
+ // Represent reserved words as reserved word tokens.
+ if (!hadUnicodeEscape) {
+ if (const ReservedWordInfo* rw = FindReservedWord(chars, length)) {
+ tp->type = rw->tokentype;
+ goto out;
}
}
JSAtom* atom = AtomizeChars(cx, chars, length);
- if (!atom)
+ if (!atom) {
goto error;
+ }
tp->type = TOK_NAME;
tp->setName(atom->asPropertyName());
goto out;
@@ -1532,10 +1610,11 @@ TokenStream::getTokenInternal(TokenKind* ttp, Modifier modifier)
// grammar. We might not always be so permissive, so we warn
// about it.
if (c >= '8') {
- if (!reportWarning(JSMSG_BAD_OCTAL, c == '8' ? "08" : "09")) {
+ if (!warning(JSMSG_BAD_OCTAL, c == '8' ? "08" : "09"))
goto error;
- }
- goto decimal; // use the decimal scanner for the rest of the number
+
+ // Use the decimal scanner for the rest of the number.
+ goto decimal;
}
c = getCharIgnoreEOL();
}
@@ -1684,7 +1763,8 @@ TokenStream::getTokenInternal(TokenKind* ttp, Modifier modifier)
case '/':
// Look for a single-line comment.
if (matchChar('/')) {
- c = peekChar();
+ if (!peekChar(&c))
+ goto error;
if (c == '@' || c == '#') {
bool shouldWarn = getChar() == '@';
if (!getDirectives(false, shouldWarn))
@@ -1751,7 +1831,8 @@ TokenStream::getTokenInternal(TokenKind* ttp, Modifier modifier)
RegExpFlag reflags = NoFlags;
unsigned length = tokenbuf.length() + 1;
while (true) {
- c = peekChar();
+ if (!peekChar(&c))
+ goto error;
if (c == 'g' && !(reflags & GlobalFlag))
reflags = RegExpFlag(reflags | GlobalFlag);
else if (c == 'i' && !(reflags & IgnoreCaseFlag))
@@ -1768,7 +1849,8 @@ TokenStream::getTokenInternal(TokenKind* ttp, Modifier modifier)
length++;
}
- c = peekChar();
+ if (!peekChar(&c))
+ goto error;
if (JS7_ISLET(c)) {
char buf[2] = { '\0', '\0' };
tp->pos.begin += length + 1;
@@ -1791,8 +1873,13 @@ TokenStream::getTokenInternal(TokenKind* ttp, Modifier modifier)
case '-':
if (matchChar('-')) {
- if (peekChar() == '>' && !flags.isDirtyLine)
+ int32_t c2;
+ if (!peekChar(&c2))
+ goto error;
+
+ if (c2 == '>' && !flags.isDirtyLine)
goto skipline;
+
tp->type = TOK_DEC;
} else {
tp->type = matchChar('=') ? TOK_SUBASSIGN : TOK_SUB;
@@ -1808,8 +1895,9 @@ TokenStream::getTokenInternal(TokenKind* ttp, Modifier modifier)
MOZ_CRASH("should have jumped to |out| or |error|");
out:
- if (flags.hitOOM)
- return reportError(JSMSG_OUT_OF_MEMORY);
+ if (flags.hitOOM) {
+ return false;
+ }
flags.isDirtyLine = true;
tp->pos.end = userbuf.offset();
@@ -1825,8 +1913,9 @@ TokenStream::getTokenInternal(TokenKind* ttp, Modifier modifier)
return true;
error:
- if (flags.hitOOM)
- return reportError(JSMSG_OUT_OF_MEMORY);
+ if (flags.hitOOM) {
+ return false;
+ }
flags.isDirtyLine = true;
tp->pos.end = userbuf.offset();
@@ -1844,37 +1933,6 @@ TokenStream::getTokenInternal(TokenKind* ttp, Modifier modifier)
}
bool
-TokenStream::getBracedUnicode(uint32_t* cp)
-{
- consumeKnownChar('{');
-
- bool first = true;
- int32_t c;
- uint32_t code = 0;
- while (true) {
- c = getCharIgnoreEOL();
- if (c == EOF)
- return false;
- if (c == '}') {
- if (first)
- return false;
- break;
- }
-
- if (!JS7_ISHEX(c))
- return false;
-
- code = (code << 4) | JS7_UNHEX(c);
- if (code > unicode::NonBMPMax)
- return false;
- first = false;
- }
-
- *cp = code;
- return true;
-}
-
-bool
TokenStream::getStringOrTemplateToken(int untilChar, Token** tp)
{
int c;
@@ -1891,11 +1949,15 @@ TokenStream::getStringOrTemplateToken(int untilChar, Token** tp)
while ((c = getCharIgnoreEOL()) != untilChar) {
if (c == EOF) {
ungetCharIgnoreEOL(c);
- reportError(JSMSG_UNTERMINATED_STRING);
+ error(JSMSG_UNTERMINATED_STRING);
return false;
}
if (c == '\\') {
+ // When parsing templates, we don't immediately report errors for
+ // invalid escapes; these are handled by the parser.
+ // In those cases we don't append to tokenbuf, since it won't be
+ // read.
switch (c = getChar()) {
case 'b': c = '\b'; break;
case 'f': c = '\f'; break;
@@ -1911,12 +1973,73 @@ TokenStream::getStringOrTemplateToken(int untilChar, Token** tp)
// Unicode character specification.
case 'u': {
- if (peekChar() == '{') {
- uint32_t code;
- if (!getBracedUnicode(&code)) {
- reportError(JSMSG_MALFORMED_ESCAPE, "Unicode");
- return false;
- }
+ uint32_t code = 0;
+
+ int32_t c2;
+ if (!peekChar(&c2))
+ return false;
+
+ uint32_t start = userbuf.offset() - 2;
+
+ if (c2 == '{') {
+ consumeKnownChar('{');
+
+ bool first = true;
+ bool valid = true;
+ do {
+ int32_t c = getCharIgnoreEOL();
+ if (c == EOF) {
+ if (parsingTemplate) {
+ setInvalidTemplateEscape(start, InvalidEscapeType::Unicode);
+ valid = false;
+ break;
+ }
+ reportInvalidEscapeError(start, InvalidEscapeType::Unicode);
+ return false;
+ }
+ if (c == '}') {
+ if (first) {
+ if (parsingTemplate) {
+ setInvalidTemplateEscape(start, InvalidEscapeType::Unicode);
+ valid = false;
+ break;
+ }
+ reportInvalidEscapeError(start, InvalidEscapeType::Unicode);
+ return false;
+ }
+ break;
+ }
+
+ if (!JS7_ISHEX(c)) {
+ if (parsingTemplate) {
+ // We put the character back so that we read
+ // it on the next pass, which matters if it
+ // was '`' or '\'.
+ ungetCharIgnoreEOL(c);
+ setInvalidTemplateEscape(start, InvalidEscapeType::Unicode);
+ valid = false;
+ break;
+ }
+ reportInvalidEscapeError(start, InvalidEscapeType::Unicode);
+ return false;
+ }
+
+ code = (code << 4) | JS7_UNHEX(c);
+ if (code > unicode::NonBMPMax) {
+ if (parsingTemplate) {
+ setInvalidTemplateEscape(start + 3, InvalidEscapeType::UnicodeOverflow);
+ valid = false;
+ break;
+ }
+ reportInvalidEscapeError(start + 3, InvalidEscapeType::UnicodeOverflow);
+ return false;
+ }
+
+ first = false;
+ } while (true);
+
+ if (!valid)
+ continue;
MOZ_ASSERT(code <= unicode::NonBMPMax);
if (code < unicode::NonBMPMin) {
@@ -1939,7 +2062,11 @@ TokenStream::getStringOrTemplateToken(int untilChar, Token** tp)
c = (c << 4) + JS7_UNHEX(cp[3]);
skipChars(4);
} else {
- reportError(JSMSG_MALFORMED_ESCAPE, "Unicode");
+ if (parsingTemplate) {
+ setInvalidTemplateEscape(start, InvalidEscapeType::Unicode);
+ continue;
+ }
+ reportInvalidEscapeError(start, InvalidEscapeType::Unicode);
return false;
}
break;
@@ -1952,7 +2079,12 @@ TokenStream::getStringOrTemplateToken(int untilChar, Token** tp)
c = (JS7_UNHEX(cp[0]) << 4) + JS7_UNHEX(cp[1]);
skipChars(2);
} else {
- reportError(JSMSG_MALFORMED_ESCAPE, "hexadecimal");
+ uint32_t start = userbuf.offset() - 2;
+ if (parsingTemplate) {
+ setInvalidTemplateEscape(start, InvalidEscapeType::Hexadecimal);
+ continue;
+ }
+ reportInvalidEscapeError(start, InvalidEscapeType::Hexadecimal);
return false;
}
break;
@@ -1963,13 +2095,14 @@ TokenStream::getStringOrTemplateToken(int untilChar, Token** tp)
if (JS7_ISOCT(c)) {
int32_t val = JS7_UNOCT(c);
- c = peekChar();
+ if (!peekChar(&c))
+ return false;
// Strict mode code allows only \0, then a non-digit.
if (val != 0 || JS7_ISDEC(c)) {
if (parsingTemplate) {
- reportError(JSMSG_DEPRECATED_OCTAL);
- return false;
+ setInvalidTemplateEscape(userbuf.offset() - 2, InvalidEscapeType::Octal);
+ continue;
}
if (!reportStrictModeError(JSMSG_DEPRECATED_OCTAL))
return false;
@@ -1979,7 +2112,8 @@ TokenStream::getStringOrTemplateToken(int untilChar, Token** tp)
if (JS7_ISOCT(c)) {
val = 8 * val + JS7_UNOCT(c);
getChar();
- c = peekChar();
+ if (!peekChar(&c))
+ return false;
if (JS7_ISOCT(c)) {
int32_t save = val;
val = 8 * val + JS7_UNOCT(c);
@@ -1997,7 +2131,7 @@ TokenStream::getStringOrTemplateToken(int untilChar, Token** tp)
} else if (TokenBuf::isRawEOLChar(c)) {
if (!parsingTemplate) {
ungetCharIgnoreEOL(c);
- reportError(JSMSG_UNTERMINATED_STRING);
+ error(JSMSG_UNTERMINATED_STRING);
return false;
}
if (c == '\r') {