Commit 5b9465c2 authored by marja@chromium.org's avatar marja@chromium.org

(Pre)Parser: Move ParseExpression and ParseArrayLiteral to ParserBase.

Notes:
- The functions already did the same thing -> no changes in logic.
- One less glue function needed now.

R=ulan@chromium.org
BUG=v8:3126
LOG=N

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

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@19469 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 71be0fdb
......@@ -570,8 +570,10 @@ Expression* ParserTraits::ExpressionFromString(
}
Expression* ParserTraits::ParseArrayLiteral(bool* ok) {
return parser_->ParseArrayLiteral(ok);
Literal* ParserTraits::GetLiteralTheHole(
int position, AstNodeFactory<AstConstructionVisitor>* factory) {
return factory->NewLiteral(parser_->isolate()->factory()->the_hole_value(),
RelocInfo::kNoPosition);
}
......@@ -580,8 +582,8 @@ Expression* ParserTraits::ParseObjectLiteral(bool* ok) {
}
Expression* ParserTraits::ParseExpression(bool accept_IN, bool* ok) {
return parser_->ParseExpression(accept_IN, ok);
Expression* ParserTraits::ParseAssignmentExpression(bool accept_IN, bool* ok) {
return parser_->ParseAssignmentExpression(accept_IN, ok);
}
......@@ -594,6 +596,7 @@ Parser::Parser(CompilationInfo* info)
: ParserBase<ParserTraits>(&scanner_,
info->isolate()->stack_guard()->real_climit(),
info->extension(),
info->zone(),
this),
isolate_(info->isolate()),
symbol_cache_(0, info->zone()),
......@@ -604,7 +607,6 @@ Parser::Parser(CompilationInfo* info)
target_stack_(NULL),
pre_parse_data_(NULL),
fni_(NULL),
zone_(info->zone()),
info_(info) {
ASSERT(!script_.is_null());
isolate_->set_ast_node_id(0);
......@@ -2874,23 +2876,6 @@ Statement* Parser::ParseForStatement(ZoneStringList* labels, bool* ok) {
}
// Precedence = 1
Expression* Parser::ParseExpression(bool accept_IN, bool* ok) {
// Expression ::
// AssignmentExpression
// Expression ',' AssignmentExpression
Expression* result = ParseAssignmentExpression(accept_IN, CHECK_OK);
while (peek() == Token::COMMA) {
Expect(Token::COMMA, CHECK_OK);
int pos = position();
Expression* right = ParseAssignmentExpression(accept_IN, CHECK_OK);
result = factory()->NewBinaryOperation(Token::COMMA, result, right, pos);
}
return result;
}
// Precedence = 2
Expression* Parser::ParseAssignmentExpression(bool accept_IN, bool* ok) {
// AssignmentExpression ::
......@@ -3462,34 +3447,6 @@ void Parser::ReportInvalidPreparseData(Handle<String> name, bool* ok) {
}
Expression* Parser::ParseArrayLiteral(bool* ok) {
// ArrayLiteral ::
// '[' Expression? (',' Expression?)* ']'
int pos = peek_position();
ZoneList<Expression*>* values = new(zone()) ZoneList<Expression*>(4, zone());
Expect(Token::LBRACK, CHECK_OK);
while (peek() != Token::RBRACK) {
Expression* elem;
if (peek() == Token::COMMA) {
elem = GetLiteralTheHole(peek_position());
} else {
elem = ParseAssignmentExpression(true, CHECK_OK);
}
values->Add(elem, zone());
if (peek() != Token::RBRACK) {
Expect(Token::COMMA, CHECK_OK);
}
}
Expect(Token::RBRACK, CHECK_OK);
// Update the scope information before the pre-parsing bailout.
int literal_index = function_state_->NextMaterializedLiteralIndex();
return factory()->NewArrayLiteral(values, literal_index, pos);
}
bool CompileTimeValue::IsCompileTimeValue(Expression* expression) {
if (expression->AsLiteral() != NULL) return true;
MaterializedLiteral* lit = expression->AsMaterializedLiteral();
......@@ -4258,12 +4215,6 @@ Literal* Parser::GetLiteralUndefined(int position) {
}
Literal* Parser::GetLiteralTheHole(int position) {
return factory()->NewLiteral(
isolate()->factory()->the_hole_value(), RelocInfo::kNoPosition);
}
void Parser::MarkAsLValue(Expression* expression) {
VariableProxy* proxy = expression != NULL
? expression->AsVariableProxy()
......
......@@ -421,6 +421,7 @@ class ParserTraits {
// Return types for traversing functions.
typedef Handle<String> Identifier;
typedef v8::internal::Expression* Expression;
typedef ZoneList<v8::internal::Expression*>* ExpressionList;
};
explicit ParserTraits(Parser* parser) : parser_(parser) {}
......@@ -462,6 +463,10 @@ class ParserTraits {
return NULL;
}
// Odd-ball literal creators.
Literal* GetLiteralTheHole(int position,
AstNodeFactory<AstConstructionVisitor>* factory);
// Producing data during the recursive descent.
Handle<String> GetSymbol(Scanner* scanner = NULL);
Handle<String> NextLiteralString(Scanner* scanner,
......@@ -477,11 +482,13 @@ class ParserTraits {
Expression* ExpressionFromString(
int pos, Scanner* scanner,
AstNodeFactory<AstConstructionVisitor>* factory);
ZoneList<v8::internal::Expression*>* NewExpressionList(int size, Zone* zone) {
return new(zone) ZoneList<v8::internal::Expression*>(size, zone);
}
// Temporary glue; these functions will move to ParserBase.
Expression* ParseArrayLiteral(bool* ok);
Expression* ParseObjectLiteral(bool* ok);
Expression* ParseExpression(bool accept_IN, bool* ok);
Expression* ParseAssignmentExpression(bool accept_IN, bool* ok);
Expression* ParseV8Intrinsic(bool* ok);
private:
......@@ -554,7 +561,6 @@ class Parser : public ParserBase<ParserTraits> {
FunctionLiteral* ParseLazy(Utf16CharacterStream* source);
Isolate* isolate() { return isolate_; }
Zone* zone() const { return zone_; }
CompilationInfo* info() const { return info_; }
// Called by ParseProgram after setting up the scanner.
......@@ -630,7 +636,6 @@ class Parser : public ParserBase<ParserTraits> {
// Support for hamony block scoped bindings.
Block* ParseScopedBlock(ZoneStringList* labels, bool* ok);
Expression* ParseExpression(bool accept_IN, bool* ok);
Expression* ParseAssignmentExpression(bool accept_IN, bool* ok);
Expression* ParseYieldExpression(bool* ok);
Expression* ParseConditionalExpression(bool accept_IN, bool* ok);
......@@ -642,7 +647,6 @@ class Parser : public ParserBase<ParserTraits> {
Expression* ParseMemberExpression(bool* ok);
Expression* ParseMemberExpressionContinuation(Expression* expression,
bool* ok);
Expression* ParseArrayLiteral(bool* ok);
Expression* ParseObjectLiteral(bool* ok);
// Initialize the components of a for-in / for-of statement.
......@@ -678,7 +682,6 @@ class Parser : public ParserBase<ParserTraits> {
// Get odd-ball literals.
Literal* GetLiteralUndefined(int position);
Literal* GetLiteralTheHole(int position);
// Determine if the expression is a variable proxy and mark it as being used
// in an assignment or with a increment/decrement operator. This is currently
......@@ -755,7 +758,6 @@ class Parser : public ParserBase<ParserTraits> {
Mode mode_;
Zone* zone_;
CompilationInfo* info_;
};
......
......@@ -122,18 +122,14 @@ PreParserExpression PreParserTraits::ExpressionFromString(
}
PreParserExpression PreParserTraits::ParseArrayLiteral(bool* ok) {
return pre_parser_->ParseArrayLiteral(ok);
}
PreParserExpression PreParserTraits::ParseObjectLiteral(bool* ok) {
return pre_parser_->ParseObjectLiteral(ok);
}
PreParserExpression PreParserTraits::ParseExpression(bool accept_IN, bool* ok) {
return pre_parser_->ParseExpression(accept_IN, ok);
PreParserExpression PreParserTraits::ParseAssignmentExpression(bool accept_IN,
bool* ok) {
return pre_parser_->ParseAssignmentExpression(accept_IN, ok);
}
......@@ -834,22 +830,6 @@ PreParser::Statement PreParser::ParseDebuggerStatement(bool* ok) {
#undef DUMMY
// Precedence = 1
PreParser::Expression PreParser::ParseExpression(bool accept_IN, bool* ok) {
// Expression ::
// AssignmentExpression
// Expression ',' AssignmentExpression
Expression result = ParseAssignmentExpression(accept_IN, CHECK_OK);
while (peek() == Token::COMMA) {
Expect(Token::COMMA, CHECK_OK);
ParseAssignmentExpression(accept_IN, CHECK_OK);
result = Expression::Default();
}
return result;
}
// Precedence = 2
PreParser::Expression PreParser::ParseAssignmentExpression(bool accept_IN,
bool* ok) {
......@@ -1142,25 +1122,6 @@ PreParser::Expression PreParser::ParseMemberExpressionContinuation(
}
PreParser::Expression PreParser::ParseArrayLiteral(bool* ok) {
// ArrayLiteral ::
// '[' Expression? (',' Expression?)* ']'
Expect(Token::LBRACK, CHECK_OK);
while (peek() != Token::RBRACK) {
if (peek() != Token::COMMA) {
ParseAssignmentExpression(true, CHECK_OK);
}
if (peek() != Token::RBRACK) {
Expect(Token::COMMA, CHECK_OK);
}
}
Expect(Token::RBRACK, CHECK_OK);
function_state_->NextMaterializedLiteralIndex();
return Expression::Default();
}
PreParser::Expression PreParser::ParseObjectLiteral(bool* ok) {
// ObjectLiteral ::
// '{' (
......
......@@ -43,6 +43,7 @@ class ParserBase : public Traits {
public:
ParserBase(Scanner* scanner, uintptr_t stack_limit,
v8::Extension* extension,
typename Traits::Type::Zone* zone,
typename Traits::Type::Parser this_object)
: Traits(this_object),
parenthesized_function_(false),
......@@ -55,7 +56,8 @@ class ParserBase : public Traits {
allow_lazy_(false),
allow_natives_syntax_(false),
allow_generators_(false),
allow_for_of_(false) { }
allow_for_of_(false),
zone_(zone) { }
// Getters that indicate whether certain syntactical constructs are
// allowed to be parsed by this instance of the parser.
......@@ -185,6 +187,7 @@ class ParserBase : public Traits {
int peek_position() { return scanner_->peek_location().beg_pos; }
bool stack_overflow() const { return stack_overflow_; }
void set_stack_overflow() { stack_overflow_ = true; }
typename Traits::Type::Zone* zone() const { return zone_; }
INLINE(Token::Value peek()) {
if (stack_overflow_) return Token::ILLEGAL;
......@@ -335,6 +338,8 @@ class ParserBase : public Traits {
bool* ok);
typename Traits::Type::Expression ParsePrimaryExpression(bool* ok);
typename Traits::Type::Expression ParseExpression(bool accept_IN, bool* ok);
typename Traits::Type::Expression ParseArrayLiteral(bool* ok);
// Used to detect duplicates in object literals. Each of the values
// kGetterProperty, kSetterProperty and kValueProperty represents
......@@ -409,6 +414,8 @@ class ParserBase : public Traits {
bool allow_natives_syntax_;
bool allow_generators_;
bool allow_for_of_;
typename Traits::Type::Zone* zone_; // Only used by Parser.
};
......@@ -541,6 +548,16 @@ class PreParserExpression {
};
// PreParserExpressionList doesn't actually store the expressions because
// PreParser doesn't need to.
class PreParserExpressionList {
public:
// These functions make list->Add(some_expression) work (and do nothing).
PreParserExpressionList* operator->() { return this; }
void Add(PreParserExpression, void*) { }
};
class PreParserScope {
public:
explicit PreParserScope(PreParserScope* outer_scope, ScopeType scope_type)
......@@ -589,6 +606,16 @@ class PreParserFactory {
int pos) {
return PreParserExpression::Default();
}
PreParserExpression NewBinaryOperation(Token::Value op,
PreParserExpression left,
PreParserExpression right, int pos) {
return PreParserExpression::Default();
}
PreParserExpression NewArrayLiteral(PreParserExpressionList values,
int literal_index,
int pos) {
return PreParserExpression::Default();
}
};
......@@ -610,6 +637,7 @@ class PreParserTraits {
// Return types for traversing functions.
typedef PreParserIdentifier Identifier;
typedef PreParserExpression Expression;
typedef PreParserExpressionList ExpressionList;
};
explicit PreParserTraits(PreParser* pre_parser) : pre_parser_(pre_parser) {}
......@@ -646,6 +674,12 @@ class PreParserTraits {
return PreParserExpression::Default();
}
// Odd-ball literal creators.
static PreParserExpression GetLiteralTheHole(int position,
PreParserFactory* factory) {
return PreParserExpression::Default();
}
// Producing data during the recursive descent.
PreParserIdentifier GetSymbol(Scanner* scanner);
static PreParserIdentifier NextLiteralString(Scanner* scanner,
......@@ -674,10 +708,13 @@ class PreParserTraits {
Scanner* scanner,
PreParserFactory* factory = NULL);
static PreParserExpressionList NewExpressionList(int size, void* zone) {
return PreParserExpressionList();
}
// Temporary glue; these functions will move to ParserBase.
PreParserExpression ParseArrayLiteral(bool* ok);
PreParserExpression ParseAssignmentExpression(bool accept_IN, bool* ok);
PreParserExpression ParseObjectLiteral(bool* ok);
PreParserExpression ParseExpression(bool accept_IN, bool* ok);
PreParserExpression ParseV8Intrinsic(bool* ok);
private:
......@@ -710,7 +747,7 @@ class PreParser : public ParserBase<PreParserTraits> {
PreParser(Scanner* scanner,
ParserRecorder* log,
uintptr_t stack_limit)
: ParserBase<PreParserTraits>(scanner, stack_limit, NULL, this),
: ParserBase<PreParserTraits>(scanner, stack_limit, NULL, NULL, this),
log_(log) {}
// Pre-parse the program from the character stream; returns true on
......@@ -846,7 +883,6 @@ class PreParser : public ParserBase<PreParserTraits> {
Statement ParseTryStatement(bool* ok);
Statement ParseDebuggerStatement(bool* ok);
Expression ParseExpression(bool accept_IN, bool* ok);
Expression ParseAssignmentExpression(bool accept_IN, bool* ok);
Expression ParseYieldExpression(bool* ok);
Expression ParseConditionalExpression(bool accept_IN, bool* ok);
......@@ -858,7 +894,6 @@ class PreParser : public ParserBase<PreParserTraits> {
Expression ParseMemberExpressionContinuation(PreParserExpression expression,
bool* ok);
Expression ParseMemberWithNewPrefixesExpression(bool* ok);
Expression ParseArrayLiteral(bool* ok);
Expression ParseObjectLiteral(bool* ok);
Expression ParseV8Intrinsic(bool* ok);
......@@ -1152,6 +1187,57 @@ typename Traits::Type::Expression ParserBase<Traits>::ParsePrimaryExpression(
return result;
}
// Precedence = 1
template <class Traits>
typename Traits::Type::Expression ParserBase<Traits>::ParseExpression(
bool accept_IN, bool* ok) {
// Expression ::
// AssignmentExpression
// Expression ',' AssignmentExpression
typename Traits::Type::Expression result =
this->ParseAssignmentExpression(accept_IN, CHECK_OK);
while (peek() == Token::COMMA) {
Expect(Token::COMMA, CHECK_OK);
int pos = position();
typename Traits::Type::Expression right =
this->ParseAssignmentExpression(accept_IN, CHECK_OK);
result = factory()->NewBinaryOperation(Token::COMMA, result, right, pos);
}
return result;
}
template <class Traits>
typename Traits::Type::Expression ParserBase<Traits>::ParseArrayLiteral(
bool* ok) {
// ArrayLiteral ::
// '[' Expression? (',' Expression?)* ']'
int pos = peek_position();
typename Traits::Type::ExpressionList values =
this->NewExpressionList(4, zone_);
Expect(Token::LBRACK, CHECK_OK);
while (peek() != Token::RBRACK) {
typename Traits::Type::Expression elem = this->EmptyExpression();
if (peek() == Token::COMMA) {
elem = this->GetLiteralTheHole(peek_position(), factory());
} else {
elem = this->ParseAssignmentExpression(true, CHECK_OK);
}
values->Add(elem, zone_);
if (peek() != Token::RBRACK) {
Expect(Token::COMMA, CHECK_OK);
}
}
Expect(Token::RBRACK, CHECK_OK);
// Update the scope information before the pre-parsing bailout.
int literal_index = function_state_->NextMaterializedLiteralIndex();
return factory()->NewArrayLiteral(values, literal_index, pos);
}
#undef CHECK_OK
......
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