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

View File

@ -81,63 +81,64 @@ FunctionLiteral* Parser::DefaultConstructor(const AstRawString* name,
return function_literal;
}
void Parser::GetUnexpectedTokenMessage(Token::Value token,
MessageTemplate* message,
Scanner::Location* location,
const char** arg) {
void Parser::ReportUnexpectedTokenAt(Scanner::Location location,
Token::Value token,
MessageTemplate message) {
const char* arg = nullptr;
switch (token) {
case Token::EOS:
*message = MessageTemplate::kUnexpectedEOS;
message = MessageTemplate::kUnexpectedEOS;
break;
case Token::SMI:
case Token::NUMBER:
case Token::BIGINT:
*message = MessageTemplate::kUnexpectedTokenNumber;
message = MessageTemplate::kUnexpectedTokenNumber;
break;
case Token::STRING:
*message = MessageTemplate::kUnexpectedTokenString;
message = MessageTemplate::kUnexpectedTokenString;
break;
case Token::PRIVATE_NAME:
case Token::IDENTIFIER:
*message = MessageTemplate::kUnexpectedTokenIdentifier;
message = MessageTemplate::kUnexpectedTokenIdentifier;
break;
case Token::AWAIT:
case Token::ENUM:
*message = MessageTemplate::kUnexpectedReserved;
message = MessageTemplate::kUnexpectedReserved;
break;
case Token::LET:
case Token::STATIC:
case Token::YIELD:
case Token::FUTURE_STRICT_RESERVED_WORD:
*message = is_strict(language_mode())
? MessageTemplate::kUnexpectedStrictReserved
: MessageTemplate::kUnexpectedTokenIdentifier;
message = is_strict(language_mode())
? MessageTemplate::kUnexpectedStrictReserved
: MessageTemplate::kUnexpectedTokenIdentifier;
break;
case Token::TEMPLATE_SPAN:
case Token::TEMPLATE_TAIL:
*message = MessageTemplate::kUnexpectedTemplateString;
message = MessageTemplate::kUnexpectedTemplateString;
break;
case Token::ESCAPED_STRICT_RESERVED_WORD:
case Token::ESCAPED_KEYWORD:
*message = MessageTemplate::kInvalidEscapedReservedWord;
message = MessageTemplate::kInvalidEscapedReservedWord;
break;
case Token::ILLEGAL:
if (scanner()->has_error()) {
*message = scanner()->error();
*location = scanner()->error_location();
message = scanner()->error();
location = scanner()->error_location();
} else {
*message = MessageTemplate::kInvalidOrUnexpectedToken;
message = MessageTemplate::kInvalidOrUnexpectedToken;
}
break;
case Token::REGEXP_LITERAL:
*message = MessageTemplate::kUnexpectedTokenRegExp;
message = MessageTemplate::kUnexpectedTokenRegExp;
break;
default:
const char* name = Token::String(token);
DCHECK_NOT_NULL(name);
*arg = name;
arg = name;
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,
const SourceRange& finally_range,
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,
ScopedPtrList<Statement>* body);
void ParseAndRewriteAsyncGeneratorFunctionBody(
@ -755,6 +753,10 @@ class V8_EXPORT_PRIVATE Parser : public NON_EXPORTED_BASE(ParserBase<Parser>) {
scanner_.set_parser_error();
}
void ReportUnexpectedTokenAt(
Scanner::Location location, Token::Value token,
MessageTemplate message = MessageTemplate::kUnexpectedToken);
// "null" return type creators.
V8_INLINE static std::nullptr_t NullIdentifier() { 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();
}
V8_INLINE void GetUnexpectedTokenMessage(Token::Value token,
MessageTemplate* message,
Scanner::Location* location,
const char** arg) {}
V8_INLINE void ReportUnexpectedTokenAt(
Scanner::Location location, Token::Value token,
MessageTemplate message = MessageTemplate::kUnexpectedToken) {
ReportUnidentifiableError();
}
V8_INLINE void ParseAndRewriteGeneratorFunctionBody(
int pos, FunctionKind kind, PreParserScopedStatementList* body) {
ParseStatementList(body, Token::RBRACE);