Commit 8dd835c2 authored by nikolaos's avatar nikolaos Committed by Commit bot

[parser] Refactor parser and preparser traits

This patch refactors the traits objects, used by the parser and the
preparser, so that they contain the same set of methods, with the same
signatures.

R=adamk@chromium.org
BUG=
LOG=N

Review-Url: https://codereview.chromium.org/2179423002
Cr-Commit-Position: refs/heads/master@{#38736}
parent d64bd5f5
......@@ -865,11 +865,31 @@ class ParserBase : public Traits {
Traits::ReportMessageAt(source_location, message, arg, error_type);
}
void ReportMessage(MessageTemplate::Template message, const AstRawString* arg,
ParseErrorType error_type = kSyntaxError) {
Scanner::Location source_location = scanner()->location();
Traits::ReportMessageAt(source_location, message, arg, error_type);
}
void ReportMessageAt(Scanner::Location location,
MessageTemplate::Template message,
const char* arg = NULL,
ParseErrorType error_type = kSyntaxError) {
Traits::ReportMessageAt(location, message, arg, error_type);
}
void ReportMessageAt(Scanner::Location location,
MessageTemplate::Template message,
const AstRawString* arg,
ParseErrorType error_type = kSyntaxError) {
Traits::ReportMessageAt(location, message, reinterpret_cast<const char*>(0),
error_type);
Traits::ReportMessageAt(location, message, arg, error_type);
}
void ReportMessageAt(Scanner::Location location,
MessageTemplate::Template message,
ParseErrorType error_type) {
ReportMessageAt(location, message, static_cast<const char*>(nullptr),
error_type);
}
void GetUnexpectedTokenMessage(
......
This diff is collapsed.
......@@ -494,49 +494,25 @@ class ParserTraits {
Expression* NewThrowTypeError(MessageTemplate::Template message,
const AstRawString* arg, int pos);
// Generic AST generator for throwing errors from compiled code.
Expression* NewThrowError(Runtime::FunctionId function_id,
MessageTemplate::Template message,
const AstRawString* arg, int pos);
void FinalizeIteratorUse(Variable* completion, Expression* condition,
Variable* iter, Block* iterator_use, Block* result);
Statement* FinalizeForOfStatement(ForOfStatement* loop, Variable* completion,
int pos);
// Reporting errors.
void ReportMessageAt(Scanner::Location source_location,
MessageTemplate::Template message,
const char* arg = NULL,
ParseErrorType error_type = kSyntaxError);
void ReportMessage(MessageTemplate::Template message, const char* arg = NULL,
ParseErrorType error_type = kSyntaxError);
void ReportMessage(MessageTemplate::Template message, const AstRawString* arg,
ParseErrorType error_type = kSyntaxError);
void ReportMessageAt(Scanner::Location source_location,
MessageTemplate::Template message,
const AstRawString* arg,
ParseErrorType error_type = kSyntaxError);
// "null" return type creators.
static const AstRawString* EmptyIdentifier() {
return NULL;
}
static Expression* EmptyExpression() {
return NULL;
}
static Literal* EmptyLiteral() {
return NULL;
}
static ObjectLiteralProperty* EmptyObjectLiteralProperty() { return NULL; }
static FunctionLiteral* EmptyFunctionLiteral() { return NULL; }
static const AstRawString* EmptyIdentifier() { return nullptr; }
static Expression* EmptyExpression() { return nullptr; }
static Literal* EmptyLiteral() { return nullptr; }
static ObjectLiteralProperty* EmptyObjectLiteralProperty() { return nullptr; }
static FunctionLiteral* EmptyFunctionLiteral() { return nullptr; }
// Used in error return values.
static ZoneList<Expression*>* NullExpressionList() {
return NULL;
}
static const AstRawString* EmptyFormalParameter() { return NULL; }
static ZoneList<Expression*>* NullExpressionList() { return nullptr; }
// Non-NULL empty string.
V8_INLINE const AstRawString* EmptyIdentifierString();
......@@ -581,10 +557,6 @@ class ParserTraits {
ZoneList<Statement*>* body, bool accept_IN,
Type::ExpressionClassifier* classifier, int pos, bool* ok);
V8_INLINE Scope* NewScope(ScopeType scope_type);
V8_INLINE DeclarationScope* NewFunctionScope(FunctionKind kind);
V8_INLINE Scope* NewScopeWithParent(Scope* parent, ScopeType scope_type);
V8_INLINE void AddFormalParameter(ParserFormalParameters* parameters,
Expression* pattern,
Expression* initializer,
......@@ -593,9 +565,6 @@ class ParserTraits {
DeclarationScope* scope,
const ParserFormalParameters::Parameter& parameter,
Type::ExpressionClassifier* classifier);
void ParseArrowFunctionFormalParameters(ParserFormalParameters* parameters,
Expression* params, int end_pos,
bool* ok);
void ParseArrowFunctionFormalParameterList(
ParserFormalParameters* parameters, Expression* params,
const Scanner::Location& params_loc, Scanner::Location* duplicate_loc,
......@@ -631,8 +600,7 @@ class ParserTraits {
V8_INLINE void MarkCollectedTailCallExpressions();
V8_INLINE void MarkTailPosition(Expression* expression);
V8_INLINE void CheckConflictingVarDeclarations(v8::internal::Scope* scope,
bool* ok);
V8_INLINE void CheckConflictingVarDeclarations(Scope* scope, bool* ok);
class TemplateLiteral : public ZoneObject {
public:
......@@ -707,7 +675,6 @@ class ParserTraits {
void SetFunctionNameFromIdentifierRef(Expression* value,
Expression* identifier);
void SetFunctionName(Expression* value, const AstRawString* name);
// Rewrite expressions that are not used as patterns
V8_INLINE void RewriteNonPattern(Type::ExpressionClassifier* classifier,
......@@ -719,18 +686,11 @@ class ParserTraits {
V8_INLINE ZoneList<Expression*>* GetNonPatternList() const;
Expression* RewriteYieldStar(
Expression* generator, Expression* expression, int pos);
V8_INLINE Expression* RewriteYieldStar(Expression* generator,
Expression* expression, int pos);
private:
Parser* parser_;
void BuildIteratorClose(ZoneList<Statement*>* statements, Variable* iterator,
Variable* input, Variable* output);
void BuildIteratorCloseForCompletion(ZoneList<Statement*>* statements,
Variable* iterator,
Expression* completion);
Statement* CheckCallable(Variable* var, Expression* error, int pos);
};
......@@ -1175,6 +1135,31 @@ class Parser : public ParserBase<ParserTraits> {
Expression* BuildPromiseResolve(Expression* value, int pos);
Expression* BuildPromiseReject(Expression* value, int pos);
// Generic AST generator for throwing errors from compiled code.
Expression* NewThrowError(Runtime::FunctionId function_id,
MessageTemplate::Template message,
const AstRawString* arg, int pos);
void FinalizeIteratorUse(Variable* completion, Expression* condition,
Variable* iter, Block* iterator_use, Block* result);
Statement* FinalizeForOfStatement(ForOfStatement* loop, Variable* completion,
int pos);
void BuildIteratorClose(ZoneList<Statement*>* statements, Variable* iterator,
Variable* input, Variable* output);
void BuildIteratorCloseForCompletion(ZoneList<Statement*>* statements,
Variable* iterator,
Expression* completion);
Statement* CheckCallable(Variable* var, Expression* error, int pos);
Expression* RewriteYieldStar(Expression* generator, Expression* expression,
int pos);
void ParseArrowFunctionFormalParameters(ParserFormalParameters* parameters,
Expression* params, int end_pos,
bool* ok);
void SetFunctionName(Expression* value, const AstRawString* name);
Scanner scanner_;
PreParser* reusable_preparser_;
Scope* original_scope_; // for ES5 function declarations in sloppy eval
......@@ -1203,18 +1188,6 @@ bool ParserTraits::IsFutureStrictReserved(
return parser_->scanner()->IdentifierIsFutureStrictReserved(identifier);
}
Scope* ParserTraits::NewScopeWithParent(Scope* parent, ScopeType scope_type) {
return parser_->NewScopeWithParent(parent, scope_type);
}
Scope* ParserTraits::NewScope(ScopeType scope_type) {
return parser_->NewScope(scope_type);
}
DeclarationScope* ParserTraits::NewFunctionScope(FunctionKind kind) {
return parser_->NewFunctionScope(kind);
}
const AstRawString* ParserTraits::EmptyIdentifierString() {
return parser_->ast_value_factory()->empty_string();
}
......@@ -1235,9 +1208,7 @@ ZoneList<Statement*>* ParserTraits::ParseEagerFunctionBody(
function_type, ok);
}
void ParserTraits::CheckConflictingVarDeclarations(v8::internal::Scope* scope,
bool* ok) {
void ParserTraits::CheckConflictingVarDeclarations(Scope* scope, bool* ok) {
parser_->CheckConflictingVarDeclarations(scope, ok);
}
......@@ -1379,6 +1350,10 @@ DoExpression* ParserTraits::ParseDoExpression(bool* ok) {
return parser_->ParseDoExpression(ok);
}
Expression* ParserTraits::RewriteYieldStar(Expression* generator,
Expression* iterable, int pos) {
return parser_->RewriteYieldStar(generator, iterable, pos);
}
} // namespace internal
} // namespace v8
......
......@@ -45,15 +45,15 @@ void PreParserTraits::ReportMessageAt(Scanner::Location location,
MessageTemplate::Template message,
const char* arg,
ParseErrorType error_type) {
ReportMessageAt(location.beg_pos, location.end_pos, message, arg, error_type);
pre_parser_->log_->LogMessage(location.beg_pos, location.end_pos, message,
arg, error_type);
}
void PreParserTraits::ReportMessageAt(int start_pos, int end_pos,
void PreParserTraits::ReportMessageAt(Scanner::Location location,
MessageTemplate::Template message,
const char* arg,
const AstRawString* arg,
ParseErrorType error_type) {
pre_parser_->log_->LogMessage(start_pos, end_pos, message, arg, error_type);
UNREACHABLE();
}
......@@ -93,11 +93,6 @@ PreParserIdentifier PreParserTraits::GetSymbol(Scanner* scanner) {
}
PreParserIdentifier PreParserTraits::GetNumberAsSymbol(Scanner* scanner) {
return PreParserIdentifier::Default();
}
PreParserExpression PreParserTraits::ExpressionFromString(
int pos, Scanner* scanner, PreParserFactory* factory) {
if (scanner->UnescapedLiteralMatches("use strict", 10)) {
......@@ -262,9 +257,9 @@ void PreParser::ParseStatementList(int end_token, bool* ok,
// TC39 deemed "use strict" directives to be an error when occurring
// in the body of a function with non-simple parameter list, on
// 29/7/2015. https://goo.gl/ueA7Ln
PreParserTraits::ReportMessageAt(
token_loc, MessageTemplate::kIllegalLanguageModeDirective,
"use strict");
ReportMessageAt(token_loc,
MessageTemplate::kIllegalLanguageModeDirective,
"use strict");
*ok = false;
return;
}
......@@ -589,7 +584,7 @@ PreParser::Statement PreParser::ParseVariableDeclarations(
}
} else if ((require_initializer || is_pattern) &&
(var_context != kForStatement || !PeekInOrOf())) {
PreParserTraits::ReportMessageAt(
ReportMessageAt(
Scanner::Location(decl_pos, scanner()->location().end_pos),
MessageTemplate::kDeclarationMissingInitializer,
is_pattern ? "destructuring" : "const");
......@@ -616,8 +611,8 @@ PreParser::Statement PreParser::ParseFunctionDeclaration(bool* ok) {
if (Check(Token::MUL)) {
flags |= ParseFunctionFlags::kIsGenerator;
if (allow_harmony_restrictive_declarations()) {
PreParserTraits::ReportMessageAt(
scanner()->location(), MessageTemplate::kGeneratorInLegacyContext);
ReportMessageAt(scanner()->location(),
MessageTemplate::kGeneratorInLegacyContext);
*ok = false;
return Statement::Default();
}
......@@ -884,9 +879,9 @@ PreParser::Statement PreParser::ParseForStatement(bool* ok) {
if (CheckInOrOf(&mode, ok)) {
if (!*ok) return Statement::Default();
if (decl_count != 1) {
PreParserTraits::ReportMessageAt(
bindings_loc, MessageTemplate::kForInOfLoopMultiBindings,
ForEachStatement::VisitModeString(mode));
ReportMessageAt(bindings_loc,
MessageTemplate::kForInOfLoopMultiBindings,
ForEachStatement::VisitModeString(mode));
*ok = false;
return Statement::Default();
}
......@@ -898,9 +893,9 @@ PreParser::Statement PreParser::ParseForStatement(bool* ok) {
if (use_counts_ != nullptr && allow_harmony_for_in()) {
++use_counts_[v8::Isolate::kForInInitializer];
}
PreParserTraits::ReportMessageAt(
first_initializer_loc, MessageTemplate::kForInOfLoopInitializer,
ForEachStatement::VisitModeString(mode));
ReportMessageAt(first_initializer_loc,
MessageTemplate::kForInOfLoopInitializer,
ForEachStatement::VisitModeString(mode));
*ok = false;
return Statement::Default();
}
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment