[parser] Give ReportUnexpectedTokenAt different impls in Parser/PreParser

This follows the "CRTP" pattern used elsewhere in the Parser rather than
a branch on IsPreParser(). Also merge GetUnexpectedTokenMessage()
into ReportUnexpectedTokenAt().

Change-Id: I8eaa5cc3230c4660624a48c705f80d1a60a2710b
Reviewed-on: https://chromium-review.googlesource.com/c/1423094
Reviewed-by: Toon Verwaest <verwaest@chromium.org>
Commit-Queue: Adam Klein <adamk@chromium.org>
Cr-Commit-Position: refs/heads/master@{#59006}
This commit is contained in:
Adam Klein 2019-01-18 15:55:49 -08:00 committed by Commit Bot
parent 4c9bc648f3
commit fa43dd9118
4 changed files with 33 additions and 45 deletions

View File

@ -947,9 +947,6 @@ class ParserBase {
} }
V8_NOINLINE void ReportUnexpectedToken(Token::Value token); V8_NOINLINE void ReportUnexpectedToken(Token::Value token);
V8_NOINLINE void ReportUnexpectedTokenAt(
Scanner::Location location, Token::Value token,
MessageTemplate message = MessageTemplate::kUnexpectedToken);
void ValidateFormalParameters(LanguageMode language_mode, void ValidateFormalParameters(LanguageMode language_mode,
const FormalParametersT& parameters, const FormalParametersT& parameters,
@ -1457,20 +1454,7 @@ ParserBase<Impl>::FunctionState::~FunctionState() {
template <typename Impl> template <typename Impl>
void ParserBase<Impl>::ReportUnexpectedToken(Token::Value token) { void ParserBase<Impl>::ReportUnexpectedToken(Token::Value token) {
return ReportUnexpectedTokenAt(scanner_->location(), token); return impl()->ReportUnexpectedTokenAt(scanner_->location(), token);
}
template <typename Impl>
void ParserBase<Impl>::ReportUnexpectedTokenAt(
Scanner::Location source_location, Token::Value token,
MessageTemplate message) {
const char* arg = nullptr;
impl()->GetUnexpectedTokenMessage(token, &message, &source_location, &arg);
if (Impl::IsPreParser()) {
impl()->ReportUnidentifiableError();
} else {
impl()->ReportMessageAt(source_location, message, arg);
}
} }
template <typename Impl> template <typename Impl>
@ -1858,7 +1842,7 @@ ParserBase<Impl>::ParseArrowParametersWithRest(
// as the formal parameters of'(x, y, ...z) => foo', and is not itself a // as the formal parameters of'(x, y, ...z) => foo', and is not itself a
// valid expression. // valid expression.
if (peek() != Token::RPAREN || PeekAhead() != Token::ARROW) { if (peek() != Token::RPAREN || PeekAhead() != Token::ARROW) {
ReportUnexpectedTokenAt(ellipsis, Token::ELLIPSIS); impl()->ReportUnexpectedTokenAt(ellipsis, Token::ELLIPSIS);
return impl()->FailureExpression(); return impl()->FailureExpression();
} }
@ -2225,7 +2209,7 @@ ParserBase<Impl>::ParseClassPropertyDefinition(ClassInfo* class_info,
case ParsePropertyKind::kValue: case ParsePropertyKind::kValue:
case ParsePropertyKind::kShorthand: case ParsePropertyKind::kShorthand:
case ParsePropertyKind::kSpread: case ParsePropertyKind::kSpread:
ReportUnexpectedTokenAt( impl()->ReportUnexpectedTokenAt(
Scanner::Location(name_token_position, name_expression->position()), Scanner::Location(name_token_position, name_expression->position()),
name_token); name_token);
return impl()->NullLiteralProperty(); return impl()->NullLiteralProperty();
@ -3928,7 +3912,7 @@ ParserBase<Impl>::ParseArrowFunctionLiteral(
// ASI inserts `;` after arrow parameters if a line terminator is found. // ASI inserts `;` after arrow parameters if a line terminator is found.
// `=> ...` is never a valid expression, so report as syntax error. // `=> ...` is never a valid expression, so report as syntax error.
// If next token is not `=>`, it's a syntax error anyways. // If next token is not `=>`, it's a syntax error anyways.
ReportUnexpectedTokenAt(scanner_->peek_location(), Token::ARROW); impl()->ReportUnexpectedTokenAt(scanner_->peek_location(), Token::ARROW);
return impl()->FailureExpression(); return impl()->FailureExpression();
} }

View File

@ -81,63 +81,64 @@ FunctionLiteral* Parser::DefaultConstructor(const AstRawString* name,
return function_literal; return function_literal;
} }
void Parser::GetUnexpectedTokenMessage(Token::Value token, void Parser::ReportUnexpectedTokenAt(Scanner::Location location,
MessageTemplate* message, Token::Value token,
Scanner::Location* location, MessageTemplate message) {
const char** arg) { const char* arg = nullptr;
switch (token) { switch (token) {
case Token::EOS: case Token::EOS:
*message = MessageTemplate::kUnexpectedEOS; message = MessageTemplate::kUnexpectedEOS;
break; break;
case Token::SMI: case Token::SMI:
case Token::NUMBER: case Token::NUMBER:
case Token::BIGINT: case Token::BIGINT:
*message = MessageTemplate::kUnexpectedTokenNumber; message = MessageTemplate::kUnexpectedTokenNumber;
break; break;
case Token::STRING: case Token::STRING:
*message = MessageTemplate::kUnexpectedTokenString; message = MessageTemplate::kUnexpectedTokenString;
break; break;
case Token::PRIVATE_NAME: case Token::PRIVATE_NAME:
case Token::IDENTIFIER: case Token::IDENTIFIER:
*message = MessageTemplate::kUnexpectedTokenIdentifier; message = MessageTemplate::kUnexpectedTokenIdentifier;
break; break;
case Token::AWAIT: case Token::AWAIT:
case Token::ENUM: case Token::ENUM:
*message = MessageTemplate::kUnexpectedReserved; message = MessageTemplate::kUnexpectedReserved;
break; break;
case Token::LET: case Token::LET:
case Token::STATIC: case Token::STATIC:
case Token::YIELD: case Token::YIELD:
case Token::FUTURE_STRICT_RESERVED_WORD: case Token::FUTURE_STRICT_RESERVED_WORD:
*message = is_strict(language_mode()) message = is_strict(language_mode())
? MessageTemplate::kUnexpectedStrictReserved ? MessageTemplate::kUnexpectedStrictReserved
: MessageTemplate::kUnexpectedTokenIdentifier; : MessageTemplate::kUnexpectedTokenIdentifier;
break; break;
case Token::TEMPLATE_SPAN: case Token::TEMPLATE_SPAN:
case Token::TEMPLATE_TAIL: case Token::TEMPLATE_TAIL:
*message = MessageTemplate::kUnexpectedTemplateString; message = MessageTemplate::kUnexpectedTemplateString;
break; break;
case Token::ESCAPED_STRICT_RESERVED_WORD: case Token::ESCAPED_STRICT_RESERVED_WORD:
case Token::ESCAPED_KEYWORD: case Token::ESCAPED_KEYWORD:
*message = MessageTemplate::kInvalidEscapedReservedWord; message = MessageTemplate::kInvalidEscapedReservedWord;
break; break;
case Token::ILLEGAL: case Token::ILLEGAL:
if (scanner()->has_error()) { if (scanner()->has_error()) {
*message = scanner()->error(); message = scanner()->error();
*location = scanner()->error_location(); location = scanner()->error_location();
} else { } else {
*message = MessageTemplate::kInvalidOrUnexpectedToken; message = MessageTemplate::kInvalidOrUnexpectedToken;
} }
break; break;
case Token::REGEXP_LITERAL: case Token::REGEXP_LITERAL:
*message = MessageTemplate::kUnexpectedTokenRegExp; message = MessageTemplate::kUnexpectedTokenRegExp;
break; break;
default: default:
const char* name = Token::String(token); const char* name = Token::String(token);
DCHECK_NOT_NULL(name); DCHECK_NOT_NULL(name);
*arg = name; arg = name;
break; break;
} }
ReportMessageAt(location, message, arg);
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@ -331,8 +331,6 @@ class V8_EXPORT_PRIVATE Parser : public NON_EXPORTED_BASE(ParserBase<Parser>) {
Block* finally_block, Block* finally_block,
const SourceRange& finally_range, const SourceRange& finally_range,
const CatchInfo& catch_info, int pos); const CatchInfo& catch_info, int pos);
void GetUnexpectedTokenMessage(Token::Value token, MessageTemplate* message,
Scanner::Location* location, const char** arg);
void ParseAndRewriteGeneratorFunctionBody(int pos, FunctionKind kind, void ParseAndRewriteGeneratorFunctionBody(int pos, FunctionKind kind,
ScopedPtrList<Statement>* body); ScopedPtrList<Statement>* body);
void ParseAndRewriteAsyncGeneratorFunctionBody( void ParseAndRewriteAsyncGeneratorFunctionBody(
@ -755,6 +753,10 @@ class V8_EXPORT_PRIVATE Parser : public NON_EXPORTED_BASE(ParserBase<Parser>) {
scanner_.set_parser_error(); scanner_.set_parser_error();
} }
void ReportUnexpectedTokenAt(
Scanner::Location location, Token::Value token,
MessageTemplate message = MessageTemplate::kUnexpectedToken);
// "null" return type creators. // "null" return type creators.
V8_INLINE static std::nullptr_t NullIdentifier() { return nullptr; } V8_INLINE static std::nullptr_t NullIdentifier() { return nullptr; }
V8_INLINE static std::nullptr_t NullExpression() { return nullptr; } V8_INLINE static std::nullptr_t NullExpression() { return nullptr; }

View File

@ -1112,10 +1112,11 @@ class PreParser : public ParserBase<PreParser> {
return PreParserStatement::Default(); return PreParserStatement::Default();
} }
V8_INLINE void GetUnexpectedTokenMessage(Token::Value token, V8_INLINE void ReportUnexpectedTokenAt(
MessageTemplate* message, Scanner::Location location, Token::Value token,
Scanner::Location* location, MessageTemplate message = MessageTemplate::kUnexpectedToken) {
const char** arg) {} ReportUnidentifiableError();
}
V8_INLINE void ParseAndRewriteGeneratorFunctionBody( V8_INLINE void ParseAndRewriteGeneratorFunctionBody(
int pos, FunctionKind kind, PreParserScopedStatementList* body) { int pos, FunctionKind kind, PreParserScopedStatementList* body) {
ParseStatementList(body, Token::RBRACE); ParseStatementList(body, Token::RBRACE);