Parser: Introduce StatementList + NewStatementList()

Adds new Traits::Type::StatementList definitions both for Parser and
PreParser, and the corresponding NewStatementList() factory function.
This is needed to be able to define in ParserBase parsing functions
that return and manipulate lists of statements.

Moving and renaming PreParser::Statement to PreParserStatement is also
needed so its definition is available earlier for PreParserStatementList
to use it.

R=marja@chromium.org

Review URL: https://codereview.chromium.org/252423007

Patch from Adrian Perez de Castro <aperez@igalia.com>.

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@20965 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
marja@chromium.org 2014-04-25 09:44:20 +00:00
parent df07a82771
commit 2dd4b5f938
2 changed files with 71 additions and 46 deletions

View File

@ -425,6 +425,7 @@ class ParserTraits {
typedef ObjectLiteral::Property* ObjectLiteralProperty; typedef ObjectLiteral::Property* ObjectLiteralProperty;
typedef ZoneList<v8::internal::Expression*>* ExpressionList; typedef ZoneList<v8::internal::Expression*>* ExpressionList;
typedef ZoneList<ObjectLiteral::Property*>* PropertyList; typedef ZoneList<ObjectLiteral::Property*>* PropertyList;
typedef ZoneList<v8::internal::Statement*>* StatementList;
// For constructing objects returned by the traversing functions. // For constructing objects returned by the traversing functions.
typedef AstNodeFactory<AstConstructionVisitor> Factory; typedef AstNodeFactory<AstConstructionVisitor> Factory;
@ -592,6 +593,9 @@ class ParserTraits {
ZoneList<ObjectLiteral::Property*>* NewPropertyList(int size, Zone* zone) { ZoneList<ObjectLiteral::Property*>* NewPropertyList(int size, Zone* zone) {
return new(zone) ZoneList<ObjectLiteral::Property*>(size, zone); return new(zone) ZoneList<ObjectLiteral::Property*>(size, zone);
} }
ZoneList<v8::internal::Statement*>* NewStatementList(int size, Zone* zone) {
return new(zone) ZoneList<v8::internal::Statement*>(size, zone);
}
// Temporary glue; these functions will move to ParserBase. // Temporary glue; these functions will move to ParserBase.
Expression* ParseV8Intrinsic(bool* ok); Expression* ParseV8Intrinsic(bool* ok);

View File

@ -687,6 +687,67 @@ class PreParserExpressionList {
}; };
class PreParserStatement {
public:
static PreParserStatement Default() {
return PreParserStatement(kUnknownStatement);
}
static PreParserStatement FunctionDeclaration() {
return PreParserStatement(kFunctionDeclaration);
}
// Creates expression statement from expression.
// Preserves being an unparenthesized string literal, possibly
// "use strict".
static PreParserStatement ExpressionStatement(
PreParserExpression expression) {
if (expression.IsUseStrictLiteral()) {
return PreParserStatement(kUseStrictExpressionStatement);
}
if (expression.IsStringLiteral()) {
return PreParserStatement(kStringLiteralExpressionStatement);
}
return Default();
}
bool IsStringLiteral() {
return code_ == kStringLiteralExpressionStatement;
}
bool IsUseStrictLiteral() {
return code_ == kUseStrictExpressionStatement;
}
bool IsFunctionDeclaration() {
return code_ == kFunctionDeclaration;
}
private:
enum Type {
kUnknownStatement,
kStringLiteralExpressionStatement,
kUseStrictExpressionStatement,
kFunctionDeclaration
};
explicit PreParserStatement(Type code) : code_(code) {}
Type code_;
};
// PreParserStatementList doesn't actually store the statements because
// the PreParser does not need them.
class PreParserStatementList {
public:
// These functions make list->Add(some_expression) work as no-ops.
PreParserStatementList() {}
PreParserStatementList* operator->() { return this; }
void Add(PreParserStatement, void*) {}
};
class PreParserScope { class PreParserScope {
public: public:
explicit PreParserScope(PreParserScope* outer_scope, ScopeType scope_type) explicit PreParserScope(PreParserScope* outer_scope, ScopeType scope_type)
@ -830,6 +891,7 @@ class PreParserTraits {
typedef PreParserExpression Literal; typedef PreParserExpression Literal;
typedef PreParserExpressionList ExpressionList; typedef PreParserExpressionList ExpressionList;
typedef PreParserExpressionList PropertyList; typedef PreParserExpressionList PropertyList;
typedef PreParserStatementList StatementList;
// For constructing objects returned by the traversing functions. // For constructing objects returned by the traversing functions.
typedef PreParserFactory Factory; typedef PreParserFactory Factory;
@ -993,6 +1055,10 @@ class PreParserTraits {
return PreParserExpressionList(); return PreParserExpressionList();
} }
static PreParserStatementList NewStatementList(int size, void* zone) {
return PreParserStatementList();
}
static PreParserExpressionList NewPropertyList(int size, void* zone) { static PreParserExpressionList NewPropertyList(int size, void* zone) {
return PreParserExpressionList(); return PreParserExpressionList();
} }
@ -1029,6 +1095,7 @@ class PreParser : public ParserBase<PreParserTraits> {
public: public:
typedef PreParserIdentifier Identifier; typedef PreParserIdentifier Identifier;
typedef PreParserExpression Expression; typedef PreParserExpression Expression;
typedef PreParserStatement Statement;
enum PreParseResult { enum PreParseResult {
kPreParseStackOverflow, kPreParseStackOverflow,
@ -1090,52 +1157,6 @@ class PreParser : public ParserBase<PreParserTraits> {
kHasNoInitializers kHasNoInitializers
}; };
class Statement {
public:
static Statement Default() {
return Statement(kUnknownStatement);
}
static Statement FunctionDeclaration() {
return Statement(kFunctionDeclaration);
}
// Creates expression statement from expression.
// Preserves being an unparenthesized string literal, possibly
// "use strict".
static Statement ExpressionStatement(Expression expression) {
if (expression.IsUseStrictLiteral()) {
return Statement(kUseStrictExpressionStatement);
}
if (expression.IsStringLiteral()) {
return Statement(kStringLiteralExpressionStatement);
}
return Default();
}
bool IsStringLiteral() {
return code_ == kStringLiteralExpressionStatement;
}
bool IsUseStrictLiteral() {
return code_ == kUseStrictExpressionStatement;
}
bool IsFunctionDeclaration() {
return code_ == kFunctionDeclaration;
}
private:
enum Type {
kUnknownStatement,
kStringLiteralExpressionStatement,
kUseStrictExpressionStatement,
kFunctionDeclaration
};
explicit Statement(Type code) : code_(code) {}
Type code_;
};
enum SourceElements { enum SourceElements {
kUnknownSourceElements kUnknownSourceElements