Introduce Parser::errorAt

This reduces reporting an error at a particular offset to its bare
essentials, simplifying calls.
This commit is contained in:
wolfbeast 2019-04-06 06:39:27 +02:00 • committed by Roy Tam
commit 07c1fa20ea
2 changed files with 42 additions and 32 deletions

View file

@ -608,6 +608,20 @@ Parser<ParseHandler>::error(unsigned errorNumber, ...)
va_end(args); va_end(args);
} }
template <typename ParseHandler>
void
Parser<ParseHandler>::errorAt(uint32_t offset, unsigned errorNumber, ...)
{
va_list args;
va_start(args, errorNumber);
#ifdef DEBUG
bool result =
#endif
reportHelper(ParseError, false, offset, errorNumber, args);
MOZ_ASSERT(!result, "reporting an error returned true?");
va_end(args);
}
template <typename ParseHandler> template <typename ParseHandler>
bool bool
Parser<ParseHandler>::warning(unsigned errorNumber, ...) Parser<ParseHandler>::warning(unsigned errorNumber, ...)
@ -940,8 +954,7 @@ Parser<ParseHandler>::checkStrictBinding(PropertyName* name, TokenPos pos)
JSAutoByteString bytes; JSAutoByteString bytes;
if (!AtomToPrintableString(context, name, &bytes)) if (!AtomToPrintableString(context, name, &bytes))
return false; return false;
return reportWithOffset(ParseStrictError, pc->sc()->strict(), pos.begin, return strictModeErrorAt(pos.begin, JSMSG_BAD_BINDING, bytes.ptr());
JSMSG_BAD_BINDING, bytes.ptr());
} }
return true; return true;
@ -977,8 +990,7 @@ Parser<ParseHandler>::reportRedeclaration(HandlePropertyName name, DeclarationKi
JSAutoByteString bytes; JSAutoByteString bytes;
if (!AtomToPrintableString(context, name, &bytes)) if (!AtomToPrintableString(context, name, &bytes))
return; return;
reportWithOffset(ParseError, false, pos.begin, JSMSG_REDECLARED_VAR, errorAt(pos.begin, JSMSG_REDECLARED_VAR, DeclarationKindString(kind), bytes.ptr());
DeclarationKindString(kind), bytes.ptr());
} }
// notePositionalFormalParameter is called for both the arguments of a regular // notePositionalFormalParameter is called for both the arguments of a regular
@ -1247,11 +1259,11 @@ Parser<ParseHandler>::checkLexicalDeclarationDirectlyWithinBlock(ParseContext::S
if (!StatementKindIsBraced(stmt.kind()) && if (!StatementKindIsBraced(stmt.kind()) &&
stmt.kind() != StatementKind::ForLoopLexicalHead) stmt.kind() != StatementKind::ForLoopLexicalHead)
{ {
reportWithOffset(ParseError, false, pos.begin, errorAt(pos.begin,
stmt.kind() == StatementKind::Label stmt.kind() == StatementKind::Label
? JSMSG_LEXICAL_DECL_LABEL ? JSMSG_LEXICAL_DECL_LABEL
: JSMSG_LEXICAL_DECL_NOT_IN_BLOCK, : JSMSG_LEXICAL_DECL_NOT_IN_BLOCK,
DeclarationKindString(kind)); DeclarationKindString(kind));
return false; return false;
} }
@ -1341,7 +1353,7 @@ Parser<ParseHandler>::noteDeclaredName(HandlePropertyName name, DeclarationKind
// contain 'let'. (CatchParameter is the only lexical binding form // contain 'let'. (CatchParameter is the only lexical binding form
// without this restriction.) // without this restriction.)
if (name == context->names().let) { if (name == context->names().let) {
reportWithOffset(ParseError, false, pos.begin, JSMSG_LEXICAL_DECL_DEFINES_LET); errorAt(pos.begin, JSMSG_LEXICAL_DECL_DEFINES_LET);
return false; return false;
} }
@ -3730,7 +3742,7 @@ Parser<ParseHandler>::checkUnescapedName()
if (!token.nameContainsEscape()) if (!token.nameContainsEscape())
return true; return true;
reportWithOffset(ParseError, false, token.pos.begin, JSMSG_ESCAPED_KEYWORD); errorAt(token.pos.begin, JSMSG_ESCAPED_KEYWORD);
return false; return false;
} }
@ -3840,8 +3852,7 @@ Parser<ParseHandler>::maybeParseDirective(Node list, Node possibleDirective, boo
: funbox->hasParameterExprs : funbox->hasParameterExprs
? "default" ? "default"
: "rest"; : "rest";
reportWithOffset(ParseError, false, directivePos.begin, errorAt(directivePos.begin, JSMSG_STRICT_NON_SIMPLE_PARAMS, parameterKind);
JSMSG_STRICT_NON_SIMPLE_PARAMS, parameterKind);
return false; return false;
} }
} }
@ -4038,7 +4049,7 @@ Parser<ParseHandler>::PossibleError::checkForError(ErrorKind kind)
return true; return true;
Error& err = error(kind); Error& err = error(kind);
parser_.reportWithOffset(ParseError, false, err.offset_, err.errorNumber_); parser_.errorAt(err.offset_, err.errorNumber_);
return false; return false;
} }
@ -4327,11 +4338,11 @@ Parser<ParseHandler>::destructuringDeclarationWithoutYieldOrAwait(DeclarationKin
Node res = destructuringDeclaration(kind, yieldHandling, tt); Node res = destructuringDeclaration(kind, yieldHandling, tt);
if (res) { if (res) {
if (pc->lastYieldOffset != startYieldOffset) { if (pc->lastYieldOffset != startYieldOffset) {
reportWithOffset(ParseError, false, pc->lastYieldOffset, JSMSG_YIELD_IN_DEFAULT); errorAt(pc->lastYieldOffset, JSMSG_YIELD_IN_DEFAULT);
return null(); return null();
} }
if (pc->lastAwaitOffset != startAwaitOffset) { if (pc->lastAwaitOffset != startAwaitOffset) {
reportWithOffset(ParseError, false, pc->lastAwaitOffset, JSMSG_AWAIT_IN_DEFAULT); errorAt(pc->lastAwaitOffset, JSMSG_AWAIT_IN_DEFAULT);
return null(); return null();
} }
} }
@ -4779,7 +4790,7 @@ Parser<FullParseHandler>::namedImportsOrNamespaceImport(TokenKind tt, Node impor
return false; return false;
if (tt != TOK_NAME || tokenStream.currentName() != context->names().as) { if (tt != TOK_NAME || tokenStream.currentName() != context->names().as) {
reportWithOffset(ParseError, false, pos().begin, JSMSG_AS_AFTER_IMPORT_STAR); error(JSMSG_AS_AFTER_IMPORT_STAR);
return false; return false;
} }
@ -5662,7 +5673,7 @@ Parser<ParseHandler>::forStatement(YieldHandling yieldHandling)
Node init = startNode; Node init = startNode;
if (isForEach) { if (isForEach) {
reportWithOffset(ParseError, false, begin, JSMSG_BAD_FOR_EACH_LOOP); errorAt(begin, JSMSG_BAD_FOR_EACH_LOOP);
return null(); return null();
} }
@ -5897,7 +5908,7 @@ Parser<ParseHandler>::continueStatement(YieldHandling yieldHandling)
if (foundLoop) if (foundLoop)
error(JSMSG_LABEL_NOT_FOUND); error(JSMSG_LABEL_NOT_FOUND);
else else
reportWithOffset(ParseError, false, begin, JSMSG_BAD_CONTINUE); errorAt(begin, JSMSG_BAD_CONTINUE);
return null(); return null();
} }
@ -5956,7 +5967,7 @@ Parser<ParseHandler>::breakStatement(YieldHandling yieldHandling)
}; };
if (!pc->findInnermostStatement(isBreakTarget)) { if (!pc->findInnermostStatement(isBreakTarget)) {
reportWithOffset(ParseError, false, begin, JSMSG_TOUGH_BREAK); errorAt(begin, JSMSG_TOUGH_BREAK);
return null(); return null();
} }
} }
@ -6111,8 +6122,7 @@ Parser<ParseHandler>::yieldExpression(InHandling inHandling)
} }
if (pc->functionBox()->isArrow()) { if (pc->functionBox()->isArrow()) {
reportWithOffset(ParseError, false, begin, errorAt(begin, JSMSG_YIELD_IN_ARROW, js_yield_str);
JSMSG_YIELD_IN_ARROW, js_yield_str);
return null(); return null();
} }
@ -6120,8 +6130,7 @@ Parser<ParseHandler>::yieldExpression(InHandling inHandling)
pc->functionBox()->function()->isGetter() || pc->functionBox()->function()->isGetter() ||
pc->functionBox()->function()->isSetter()) pc->functionBox()->function()->isSetter())
{ {
reportWithOffset(ParseError, false, begin, errorAt(begin, JSMSG_YIELD_IN_METHOD, js_yield_str);
JSMSG_YIELD_IN_METHOD, js_yield_str);
return null(); return null();
} }
@ -6266,7 +6275,7 @@ Parser<ParseHandler>::labeledStatement(YieldHandling yieldHandling)
uint32_t begin = pos().begin; uint32_t begin = pos().begin;
if (pc->findInnermostStatement<ParseContext::LabelStatement>(hasSameLabel)) { if (pc->findInnermostStatement<ParseContext::LabelStatement>(hasSameLabel)) {
reportWithOffset(ParseError, false, begin, JSMSG_DUPLICATE_LABEL); errorAt(begin, JSMSG_DUPLICATE_LABEL);
return null(); return null();
} }
@ -8297,8 +8306,7 @@ Parser<ParseHandler>::comprehension(GeneratorKind comprehensionKind)
return null(); return null();
if (comprehensionKind != NotGenerator && pc->lastYieldOffset != startYieldOffset) { if (comprehensionKind != NotGenerator && pc->lastYieldOffset != startYieldOffset) {
reportWithOffset(ParseError, false, pc->lastYieldOffset, errorAt(pc->lastYieldOffset, JSMSG_BAD_GENEXP_BODY, js_yield_str);
JSMSG_BAD_GENEXP_BODY, js_yield_str);
return null(); return null();
} }
@ -8360,11 +8368,11 @@ Parser<ParseHandler>::assignExprWithoutYieldOrAwait(YieldHandling yieldHandling)
Node res = assignExpr(InAllowed, yieldHandling, TripledotProhibited); Node res = assignExpr(InAllowed, yieldHandling, TripledotProhibited);
if (res) { if (res) {
if (pc->lastYieldOffset != startYieldOffset) { if (pc->lastYieldOffset != startYieldOffset) {
reportWithOffset(ParseError, false, pc->lastYieldOffset, JSMSG_YIELD_IN_DEFAULT); errorAt(pc->lastYieldOffset, JSMSG_YIELD_IN_DEFAULT);
return null(); return null();
} }
if (pc->lastAwaitOffset != startAwaitOffset) { if (pc->lastAwaitOffset != startAwaitOffset) {
reportWithOffset(ParseError, false, pc->lastAwaitOffset, JSMSG_AWAIT_IN_DEFAULT); errorAt(pc->lastAwaitOffset, JSMSG_AWAIT_IN_DEFAULT);
return null(); return null();
} }
} }
@ -9211,8 +9219,7 @@ Parser<ParseHandler>::objectLiteral(YieldHandling yieldHandling, PossibleError*
// Directly report the error when we're not in a // Directly report the error when we're not in a
// destructuring context. // destructuring context.
if (!possibleError) { if (!possibleError) {
reportWithOffset(ParseError, false, namePos.begin, errorAt(namePos.begin, JSMSG_DUPLICATE_PROTO_PROPERTY);
JSMSG_DUPLICATE_PROTO_PROPERTY);
return null(); return null();
} }
@ -9459,7 +9466,7 @@ Parser<ParseHandler>::tryNewTarget(Node &newTarget)
return false; return false;
if (!pc->sc()->allowNewTarget()) { if (!pc->sc()->allowNewTarget()) {
reportWithOffset(ParseError, false, begin, JSMSG_BAD_NEWTARGET); errorAt(begin, JSMSG_BAD_NEWTARGET);
return false; return false;
} }

View file

@ -915,6 +915,9 @@ class Parser final : private JS::AutoGCRooter, public StrictModeGetter
/* Report the given error at the current offset. */ /* Report the given error at the current offset. */
void error(unsigned errorNumber, ...); void error(unsigned errorNumber, ...);
/* Report the given error at the given offset. */
void errorAt(uint32_t offset, unsigned errorNumber, ...);
/* /*
* Handle a strict mode error at the current offset. Report an error if in * Handle a strict mode error at the current offset. Report an error if in
* strict mode code, or warn if not, using the given error number and * strict mode code, or warn if not, using the given error number and