2012-02-23 09:12:57 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2010-11-02 07:21:37 +00:00
|
|
|
|
|
|
|
#ifndef V8_PREPARSER_H
|
|
|
|
#define V8_PREPARSER_H
|
|
|
|
|
2014-06-20 08:40:11 +00:00
|
|
|
#include "src/v8.h"
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/func-name-inferrer.h"
|
|
|
|
#include "src/hashmap.h"
|
2014-06-20 08:40:11 +00:00
|
|
|
#include "src/scanner.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/scopes.h"
|
|
|
|
#include "src/token.h"
|
2011-09-08 13:06:44 +00:00
|
|
|
|
2010-11-02 07:21:37 +00:00
|
|
|
namespace v8 {
|
2011-09-08 13:06:44 +00:00
|
|
|
namespace internal {
|
2013-10-10 11:58:16 +00:00
|
|
|
|
2014-03-12 19:15:17 +00:00
|
|
|
// Common base class shared between parser and pre-parser. Traits encapsulate
|
|
|
|
// the differences between Parser and PreParser:
|
|
|
|
|
|
|
|
// - Return types: For example, Parser functions return Expression* and
|
|
|
|
// PreParser functions return PreParserExpression.
|
|
|
|
|
|
|
|
// - Creating parse tree nodes: Parser generates an AST during the recursive
|
|
|
|
// descent. PreParser doesn't create a tree. Instead, it passes around minimal
|
|
|
|
// data objects (PreParserExpression, PreParserIdentifier etc.) which contain
|
|
|
|
// just enough data for the upper layer functions. PreParserFactory is
|
|
|
|
// responsible for creating these dummy objects. It provides a similar kind of
|
|
|
|
// interface as AstNodeFactory, so ParserBase doesn't need to care which one is
|
|
|
|
// used.
|
|
|
|
|
|
|
|
// - Miscellanous other tasks interleaved with the recursive descent. For
|
|
|
|
// example, Parser keeps track of which function literals should be marked as
|
|
|
|
// pretenured, and PreParser doesn't care.
|
|
|
|
|
|
|
|
// The traits are expected to contain the following typedefs:
|
|
|
|
// struct Traits {
|
|
|
|
// // In particular...
|
|
|
|
// struct Type {
|
|
|
|
// // Used by FunctionState and BlockState.
|
|
|
|
// typedef Scope;
|
|
|
|
// typedef GeneratorVariable;
|
|
|
|
// typedef Zone;
|
|
|
|
// // Return types for traversing functions.
|
|
|
|
// typedef Identifier;
|
|
|
|
// typedef Expression;
|
|
|
|
// typedef FunctionLiteral;
|
|
|
|
// typedef ObjectLiteralProperty;
|
|
|
|
// typedef Literal;
|
|
|
|
// typedef ExpressionList;
|
|
|
|
// typedef PropertyList;
|
|
|
|
// // For constructing objects returned by the traversing functions.
|
|
|
|
// typedef Factory;
|
|
|
|
// };
|
|
|
|
// // ...
|
|
|
|
// };
|
|
|
|
|
2014-02-11 09:35:32 +00:00
|
|
|
template <typename Traits>
|
|
|
|
class ParserBase : public Traits {
|
2013-10-14 16:46:51 +00:00
|
|
|
public:
|
2014-03-18 17:05:38 +00:00
|
|
|
// Shorten type names defined by Traits.
|
|
|
|
typedef typename Traits::Type::Expression ExpressionT;
|
|
|
|
typedef typename Traits::Type::Identifier IdentifierT;
|
|
|
|
|
2014-02-11 09:35:32 +00:00
|
|
|
ParserBase(Scanner* scanner, uintptr_t stack_limit,
|
2014-02-14 11:24:26 +00:00
|
|
|
v8::Extension* extension,
|
2014-03-19 13:24:13 +00:00
|
|
|
ParserRecorder* log,
|
2014-02-19 08:56:11 +00:00
|
|
|
typename Traits::Type::Zone* zone,
|
2014-02-13 16:17:55 +00:00
|
|
|
typename Traits::Type::Parser this_object)
|
2014-02-11 09:35:32 +00:00
|
|
|
: Traits(this_object),
|
2014-02-12 11:56:07 +00:00
|
|
|
parenthesized_function_(false),
|
2014-02-13 16:17:55 +00:00
|
|
|
scope_(NULL),
|
|
|
|
function_state_(NULL),
|
2014-02-14 11:24:26 +00:00
|
|
|
extension_(extension),
|
2014-03-11 15:40:41 +00:00
|
|
|
fni_(NULL),
|
2014-03-19 13:24:13 +00:00
|
|
|
log_(log),
|
2014-03-21 09:51:33 +00:00
|
|
|
mode_(PARSE_EAGERLY), // Lazy mode must be set explicitly.
|
2014-02-11 09:35:32 +00:00
|
|
|
scanner_(scanner),
|
2013-10-14 16:46:51 +00:00
|
|
|
stack_limit_(stack_limit),
|
|
|
|
stack_overflow_(false),
|
|
|
|
allow_lazy_(false),
|
|
|
|
allow_natives_syntax_(false),
|
|
|
|
allow_generators_(false),
|
2014-02-19 08:56:11 +00:00
|
|
|
allow_for_of_(false),
|
|
|
|
zone_(zone) { }
|
2013-10-14 16:46:51 +00:00
|
|
|
|
|
|
|
// Getters that indicate whether certain syntactical constructs are
|
|
|
|
// allowed to be parsed by this instance of the parser.
|
|
|
|
bool allow_lazy() const { return allow_lazy_; }
|
|
|
|
bool allow_natives_syntax() const { return allow_natives_syntax_; }
|
|
|
|
bool allow_generators() const { return allow_generators_; }
|
|
|
|
bool allow_for_of() const { return allow_for_of_; }
|
|
|
|
bool allow_modules() const { return scanner()->HarmonyModules(); }
|
|
|
|
bool allow_harmony_scoping() const { return scanner()->HarmonyScoping(); }
|
|
|
|
bool allow_harmony_numeric_literals() const {
|
|
|
|
return scanner()->HarmonyNumericLiterals();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setters that determine whether certain syntactical constructs are
|
|
|
|
// allowed to be parsed by this instance of the parser.
|
|
|
|
void set_allow_lazy(bool allow) { allow_lazy_ = allow; }
|
|
|
|
void set_allow_natives_syntax(bool allow) { allow_natives_syntax_ = allow; }
|
|
|
|
void set_allow_generators(bool allow) { allow_generators_ = allow; }
|
|
|
|
void set_allow_for_of(bool allow) { allow_for_of_ = allow; }
|
|
|
|
void set_allow_modules(bool allow) { scanner()->SetHarmonyModules(allow); }
|
|
|
|
void set_allow_harmony_scoping(bool allow) {
|
|
|
|
scanner()->SetHarmonyScoping(allow);
|
|
|
|
}
|
|
|
|
void set_allow_harmony_numeric_literals(bool allow) {
|
|
|
|
scanner()->SetHarmonyNumericLiterals(allow);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2014-02-05 16:26:48 +00:00
|
|
|
enum AllowEvalOrArgumentsAsIdentifier {
|
|
|
|
kAllowEvalOrArguments,
|
|
|
|
kDontAllowEvalOrArguments
|
|
|
|
};
|
|
|
|
|
2014-03-21 09:51:33 +00:00
|
|
|
enum Mode {
|
|
|
|
PARSE_LAZILY,
|
|
|
|
PARSE_EAGERLY
|
|
|
|
};
|
|
|
|
|
2014-02-13 16:17:55 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// FunctionState and BlockState together implement the parser's scope stack.
|
|
|
|
// The parser's current scope is in scope_. BlockState and FunctionState
|
|
|
|
// constructors push on the scope stack and the destructors pop. They are also
|
|
|
|
// used to hold the parser's per-function and per-block state.
|
|
|
|
class BlockState BASE_EMBEDDED {
|
|
|
|
public:
|
|
|
|
BlockState(typename Traits::Type::Scope** scope_stack,
|
|
|
|
typename Traits::Type::Scope* scope)
|
|
|
|
: scope_stack_(scope_stack),
|
|
|
|
outer_scope_(*scope_stack),
|
|
|
|
scope_(scope) {
|
|
|
|
*scope_stack_ = scope_;
|
|
|
|
}
|
|
|
|
~BlockState() { *scope_stack_ = outer_scope_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
typename Traits::Type::Scope** scope_stack_;
|
|
|
|
typename Traits::Type::Scope* outer_scope_;
|
|
|
|
typename Traits::Type::Scope* scope_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FunctionState BASE_EMBEDDED {
|
|
|
|
public:
|
|
|
|
FunctionState(
|
|
|
|
FunctionState** function_state_stack,
|
|
|
|
typename Traits::Type::Scope** scope_stack,
|
|
|
|
typename Traits::Type::Scope* scope,
|
2014-06-18 07:30:56 +00:00
|
|
|
typename Traits::Type::Zone* zone = NULL);
|
2014-02-13 16:17:55 +00:00
|
|
|
~FunctionState();
|
|
|
|
|
|
|
|
int NextMaterializedLiteralIndex() {
|
|
|
|
return next_materialized_literal_index_++;
|
|
|
|
}
|
|
|
|
int materialized_literal_count() {
|
|
|
|
return next_materialized_literal_index_ - JSFunction::kLiteralsPrefixSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
int NextHandlerIndex() { return next_handler_index_++; }
|
|
|
|
int handler_count() { return next_handler_index_; }
|
|
|
|
|
|
|
|
void AddProperty() { expected_property_count_++; }
|
|
|
|
int expected_property_count() { return expected_property_count_; }
|
|
|
|
|
|
|
|
void set_is_generator(bool is_generator) { is_generator_ = is_generator; }
|
|
|
|
bool is_generator() const { return is_generator_; }
|
|
|
|
|
|
|
|
void set_generator_object_variable(
|
|
|
|
typename Traits::Type::GeneratorVariable* variable) {
|
|
|
|
ASSERT(variable != NULL);
|
|
|
|
ASSERT(!is_generator());
|
|
|
|
generator_object_variable_ = variable;
|
|
|
|
is_generator_ = true;
|
|
|
|
}
|
|
|
|
typename Traits::Type::GeneratorVariable* generator_object_variable()
|
|
|
|
const {
|
|
|
|
return generator_object_variable_;
|
|
|
|
}
|
|
|
|
|
|
|
|
typename Traits::Type::Factory* factory() { return &factory_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Used to assign an index to each literal that needs materialization in
|
|
|
|
// the function. Includes regexp literals, and boilerplate for object and
|
|
|
|
// array literals.
|
|
|
|
int next_materialized_literal_index_;
|
|
|
|
|
|
|
|
// Used to assign a per-function index to try and catch handlers.
|
|
|
|
int next_handler_index_;
|
|
|
|
|
|
|
|
// Properties count estimation.
|
|
|
|
int expected_property_count_;
|
|
|
|
|
|
|
|
// Whether the function is a generator.
|
|
|
|
bool is_generator_;
|
|
|
|
// For generators, this variable may hold the generator object. It variable
|
|
|
|
// is used by yield expressions and return statements. It is not necessary
|
|
|
|
// for generator functions to have this variable set.
|
|
|
|
Variable* generator_object_variable_;
|
|
|
|
|
|
|
|
FunctionState** function_state_stack_;
|
|
|
|
FunctionState* outer_function_state_;
|
|
|
|
typename Traits::Type::Scope** scope_stack_;
|
|
|
|
typename Traits::Type::Scope* outer_scope_;
|
|
|
|
int saved_ast_node_id_; // Only used by ParserTraits.
|
2014-03-28 12:49:34 +00:00
|
|
|
typename Traits::Type::Zone* extra_param_;
|
2014-02-13 16:17:55 +00:00
|
|
|
typename Traits::Type::Factory factory_;
|
|
|
|
|
|
|
|
friend class ParserTraits;
|
|
|
|
};
|
|
|
|
|
2014-03-21 09:51:33 +00:00
|
|
|
class ParsingModeScope BASE_EMBEDDED {
|
|
|
|
public:
|
|
|
|
ParsingModeScope(ParserBase* parser, Mode mode)
|
|
|
|
: parser_(parser),
|
|
|
|
old_mode_(parser->mode()) {
|
|
|
|
parser_->mode_ = mode;
|
|
|
|
}
|
|
|
|
~ParsingModeScope() {
|
|
|
|
parser_->mode_ = old_mode_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ParserBase* parser_;
|
|
|
|
Mode old_mode_;
|
|
|
|
};
|
|
|
|
|
2013-10-14 16:46:51 +00:00
|
|
|
Scanner* scanner() const { return scanner_; }
|
2013-10-15 08:32:58 +00:00
|
|
|
int position() { return scanner_->location().beg_pos; }
|
|
|
|
int peek_position() { return scanner_->peek_location().beg_pos; }
|
2013-10-14 16:46:51 +00:00
|
|
|
bool stack_overflow() const { return stack_overflow_; }
|
|
|
|
void set_stack_overflow() { stack_overflow_ = true; }
|
2014-03-21 09:51:33 +00:00
|
|
|
Mode mode() const { return mode_; }
|
2014-02-19 08:56:11 +00:00
|
|
|
typename Traits::Type::Zone* zone() const { return zone_; }
|
2013-10-14 16:46:51 +00:00
|
|
|
|
|
|
|
INLINE(Token::Value peek()) {
|
|
|
|
if (stack_overflow_) return Token::ILLEGAL;
|
|
|
|
return scanner()->peek();
|
|
|
|
}
|
|
|
|
|
|
|
|
INLINE(Token::Value Next()) {
|
|
|
|
if (stack_overflow_) return Token::ILLEGAL;
|
|
|
|
{
|
|
|
|
int marker;
|
|
|
|
if (reinterpret_cast<uintptr_t>(&marker) < stack_limit_) {
|
|
|
|
// Any further calls to Next or peek will return the illegal token.
|
|
|
|
// The current call must return the next token, which might already
|
|
|
|
// have been peek'ed.
|
|
|
|
stack_overflow_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return scanner()->Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Consume(Token::Value token) {
|
|
|
|
Token::Value next = Next();
|
|
|
|
USE(next);
|
|
|
|
USE(token);
|
|
|
|
ASSERT(next == token);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check(Token::Value token) {
|
|
|
|
Token::Value next = peek();
|
|
|
|
if (next == token) {
|
|
|
|
Consume(next);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Expect(Token::Value token, bool* ok) {
|
|
|
|
Token::Value next = Next();
|
|
|
|
if (next != token) {
|
|
|
|
ReportUnexpectedToken(next);
|
|
|
|
*ok = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-11 09:35:32 +00:00
|
|
|
void ExpectSemicolon(bool* ok) {
|
|
|
|
// Check for automatic semicolon insertion according to
|
|
|
|
// the rules given in ECMA-262, section 7.9, page 21.
|
|
|
|
Token::Value tok = peek();
|
|
|
|
if (tok == Token::SEMICOLON) {
|
|
|
|
Next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (scanner()->HasAnyLineTerminatorBeforeNext() ||
|
|
|
|
tok == Token::RBRACE ||
|
|
|
|
tok == Token::EOS) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Expect(Token::SEMICOLON, ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool peek_any_identifier() {
|
|
|
|
Token::Value next = peek();
|
|
|
|
return next == Token::IDENTIFIER ||
|
|
|
|
next == Token::FUTURE_RESERVED_WORD ||
|
|
|
|
next == Token::FUTURE_STRICT_RESERVED_WORD ||
|
|
|
|
next == Token::YIELD;
|
|
|
|
}
|
2013-10-15 08:32:58 +00:00
|
|
|
|
2014-02-11 09:35:32 +00:00
|
|
|
bool CheckContextualKeyword(Vector<const char> keyword) {
|
|
|
|
if (peek() == Token::IDENTIFIER &&
|
|
|
|
scanner()->is_next_contextual_keyword(keyword)) {
|
|
|
|
Consume(Token::IDENTIFIER);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExpectContextualKeyword(Vector<const char> keyword, bool* ok) {
|
|
|
|
Expect(Token::IDENTIFIER, ok);
|
|
|
|
if (!*ok) return;
|
|
|
|
if (!scanner()->is_literal_contextual_keyword(keyword)) {
|
|
|
|
ReportUnexpectedToken(scanner()->current_token());
|
|
|
|
*ok = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks whether an octal literal was last seen between beg_pos and end_pos.
|
|
|
|
// If so, reports an error. Only called for strict mode.
|
|
|
|
void CheckOctalLiteral(int beg_pos, int end_pos, bool* ok) {
|
|
|
|
Scanner::Location octal = scanner()->octal_position();
|
|
|
|
if (octal.IsValid() && beg_pos <= octal.beg_pos &&
|
|
|
|
octal.end_pos <= end_pos) {
|
|
|
|
ReportMessageAt(octal, "strict_octal_literal");
|
|
|
|
scanner()->clear_octal_position();
|
|
|
|
*ok = false;
|
|
|
|
}
|
|
|
|
}
|
2013-10-15 08:32:58 +00:00
|
|
|
|
|
|
|
// Determine precedence of given token.
|
2014-02-11 09:35:32 +00:00
|
|
|
static int Precedence(Token::Value token, bool accept_IN) {
|
|
|
|
if (token == Token::IN && !accept_IN)
|
|
|
|
return 0; // 0 precedence will terminate binary expression parsing
|
|
|
|
return Token::Precedence(token);
|
|
|
|
}
|
2013-10-14 16:46:51 +00:00
|
|
|
|
2014-02-13 16:17:55 +00:00
|
|
|
typename Traits::Type::Factory* factory() {
|
|
|
|
return function_state_->factory();
|
|
|
|
}
|
|
|
|
|
2014-03-11 14:41:22 +00:00
|
|
|
StrictMode strict_mode() { return scope_->strict_mode(); }
|
2014-02-13 16:17:55 +00:00
|
|
|
bool is_generator() const { return function_state_->is_generator(); }
|
|
|
|
|
2013-10-14 16:46:51 +00:00
|
|
|
// Report syntax errors.
|
2014-05-15 09:44:57 +00:00
|
|
|
void ReportMessage(const char* message, const char* arg = NULL,
|
2014-03-17 10:21:01 +00:00
|
|
|
bool is_reference_error = false) {
|
2014-02-11 09:35:32 +00:00
|
|
|
Scanner::Location source_location = scanner()->location();
|
2014-05-15 09:44:57 +00:00
|
|
|
Traits::ReportMessageAt(source_location, message, arg, is_reference_error);
|
2014-02-10 16:21:11 +00:00
|
|
|
}
|
2014-02-11 09:35:32 +00:00
|
|
|
|
2014-03-17 10:21:01 +00:00
|
|
|
void ReportMessageAt(Scanner::Location location, const char* message,
|
|
|
|
bool is_reference_error = false) {
|
2014-06-18 07:30:56 +00:00
|
|
|
Traits::ReportMessageAt(location, message, NULL, is_reference_error);
|
2014-02-11 09:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReportUnexpectedToken(Token::Value token);
|
|
|
|
|
|
|
|
// Recursive descent functions:
|
|
|
|
|
|
|
|
// Parses an identifier that is valid for the current scope, in particular it
|
|
|
|
// fails on strict mode future reserved keywords in a strict scope. If
|
|
|
|
// allow_eval_or_arguments is kAllowEvalOrArguments, we allow "eval" or
|
|
|
|
// "arguments" as identifier even in strict mode (this is needed in cases like
|
|
|
|
// "var foo = eval;").
|
2014-03-18 17:05:38 +00:00
|
|
|
IdentifierT ParseIdentifier(
|
2014-02-11 09:35:32 +00:00
|
|
|
AllowEvalOrArgumentsAsIdentifier,
|
|
|
|
bool* ok);
|
|
|
|
// Parses an identifier or a strict mode future reserved word, and indicate
|
|
|
|
// whether it is strict mode future reserved.
|
2014-03-18 17:05:38 +00:00
|
|
|
IdentifierT ParseIdentifierOrStrictReservedWord(
|
2014-02-11 09:35:32 +00:00
|
|
|
bool* is_strict_reserved,
|
|
|
|
bool* ok);
|
2014-03-18 17:05:38 +00:00
|
|
|
IdentifierT ParseIdentifierName(bool* ok);
|
2014-02-11 09:35:32 +00:00
|
|
|
// Parses an identifier and determines whether or not it is 'get' or 'set'.
|
2014-03-18 17:05:38 +00:00
|
|
|
IdentifierT ParseIdentifierNameOrGetOrSet(bool* is_get,
|
|
|
|
bool* is_set,
|
|
|
|
bool* ok);
|
2013-10-15 08:32:58 +00:00
|
|
|
|
2014-03-18 17:05:38 +00:00
|
|
|
ExpressionT ParseRegExpLiteral(bool seen_equal, bool* ok);
|
2014-02-14 11:24:26 +00:00
|
|
|
|
2014-03-18 17:05:38 +00:00
|
|
|
ExpressionT ParsePrimaryExpression(bool* ok);
|
|
|
|
ExpressionT ParseExpression(bool accept_IN, bool* ok);
|
|
|
|
ExpressionT ParseArrayLiteral(bool* ok);
|
|
|
|
ExpressionT ParseObjectLiteral(bool* ok);
|
2014-03-11 16:30:47 +00:00
|
|
|
typename Traits::Type::ExpressionList ParseArguments(bool* ok);
|
2014-03-18 17:05:38 +00:00
|
|
|
ExpressionT ParseAssignmentExpression(bool accept_IN, bool* ok);
|
|
|
|
ExpressionT ParseYieldExpression(bool* ok);
|
|
|
|
ExpressionT ParseConditionalExpression(bool accept_IN, bool* ok);
|
|
|
|
ExpressionT ParseBinaryExpression(int prec, bool accept_IN, bool* ok);
|
2014-03-19 14:08:47 +00:00
|
|
|
ExpressionT ParseUnaryExpression(bool* ok);
|
2014-03-21 09:46:18 +00:00
|
|
|
ExpressionT ParsePostfixExpression(bool* ok);
|
2014-03-21 09:51:33 +00:00
|
|
|
ExpressionT ParseLeftHandSideExpression(bool* ok);
|
2014-03-21 10:34:51 +00:00
|
|
|
ExpressionT ParseMemberWithNewPrefixesExpression(bool* ok);
|
|
|
|
ExpressionT ParseMemberExpression(bool* ok);
|
|
|
|
ExpressionT ParseMemberExpressionContinuation(ExpressionT expression,
|
|
|
|
bool* ok);
|
2014-02-11 11:51:01 +00:00
|
|
|
|
2014-04-02 11:03:05 +00:00
|
|
|
// Checks if the expression is a valid reference expression (e.g., on the
|
|
|
|
// left-hand side of assignments). Although ruled out by ECMA as early errors,
|
|
|
|
// we allow calls for web compatibility and rewrite them to a runtime throw.
|
|
|
|
ExpressionT CheckAndRewriteReferenceExpression(
|
|
|
|
ExpressionT expression,
|
|
|
|
Scanner::Location location, const char* message, bool* ok);
|
|
|
|
|
2013-10-15 08:32:58 +00:00
|
|
|
// Used to detect duplicates in object literals. Each of the values
|
|
|
|
// kGetterProperty, kSetterProperty and kValueProperty represents
|
|
|
|
// a type of object literal property. When parsing a property, its
|
|
|
|
// type value is stored in the DuplicateFinder for the property name.
|
|
|
|
// Values are chosen so that having intersection bits means the there is
|
|
|
|
// an incompatibility.
|
|
|
|
// I.e., you can add a getter to a property that already has a setter, since
|
|
|
|
// kGetterProperty and kSetterProperty doesn't intersect, but not if it
|
|
|
|
// already has a getter or a value. Adding the getter to an existing
|
|
|
|
// setter will store the value (kGetterProperty | kSetterProperty), which
|
|
|
|
// is incompatible with adding any further properties.
|
|
|
|
enum PropertyKind {
|
|
|
|
kNone = 0,
|
|
|
|
// Bit patterns representing different object literal property types.
|
|
|
|
kGetterProperty = 1,
|
|
|
|
kSetterProperty = 2,
|
|
|
|
kValueProperty = 7,
|
|
|
|
// Helper constants.
|
|
|
|
kValueFlag = 4
|
|
|
|
};
|
|
|
|
|
|
|
|
// Validation per ECMA 262 - 11.1.5 "Object Initialiser".
|
|
|
|
class ObjectLiteralChecker {
|
|
|
|
public:
|
2014-03-11 14:41:22 +00:00
|
|
|
ObjectLiteralChecker(ParserBase* parser, StrictMode strict_mode)
|
2013-10-15 08:32:58 +00:00
|
|
|
: parser_(parser),
|
|
|
|
finder_(scanner()->unicode_cache()),
|
2014-03-11 14:41:22 +00:00
|
|
|
strict_mode_(strict_mode) { }
|
2013-10-15 08:32:58 +00:00
|
|
|
|
|
|
|
void CheckProperty(Token::Value property, PropertyKind type, bool* ok);
|
|
|
|
|
|
|
|
private:
|
|
|
|
ParserBase* parser() const { return parser_; }
|
|
|
|
Scanner* scanner() const { return parser_->scanner(); }
|
|
|
|
|
|
|
|
// Checks the type of conflict based on values coming from PropertyType.
|
|
|
|
bool HasConflict(PropertyKind type1, PropertyKind type2) {
|
|
|
|
return (type1 & type2) != 0;
|
|
|
|
}
|
|
|
|
bool IsDataDataConflict(PropertyKind type1, PropertyKind type2) {
|
|
|
|
return ((type1 & type2) & kValueFlag) != 0;
|
|
|
|
}
|
|
|
|
bool IsDataAccessorConflict(PropertyKind type1, PropertyKind type2) {
|
|
|
|
return ((type1 ^ type2) & kValueFlag) != 0;
|
|
|
|
}
|
|
|
|
bool IsAccessorAccessorConflict(PropertyKind type1, PropertyKind type2) {
|
|
|
|
return ((type1 | type2) & kValueFlag) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ParserBase* parser_;
|
|
|
|
DuplicateFinder finder_;
|
2014-03-11 14:41:22 +00:00
|
|
|
StrictMode strict_mode_;
|
2013-10-15 08:32:58 +00:00
|
|
|
};
|
2013-10-14 16:46:51 +00:00
|
|
|
|
2014-02-12 11:56:07 +00:00
|
|
|
// If true, the next (and immediately following) function literal is
|
|
|
|
// preceded by a parenthesis.
|
|
|
|
// Heuristically that means that the function will be called immediately,
|
|
|
|
// so never lazily compile it.
|
|
|
|
bool parenthesized_function_;
|
|
|
|
|
2014-02-13 16:17:55 +00:00
|
|
|
typename Traits::Type::Scope* scope_; // Scope stack.
|
|
|
|
FunctionState* function_state_; // Function state stack.
|
2014-02-14 11:24:26 +00:00
|
|
|
v8::Extension* extension_;
|
2014-03-11 15:40:41 +00:00
|
|
|
FuncNameInferrer* fni_;
|
2014-03-19 13:24:13 +00:00
|
|
|
ParserRecorder* log_;
|
2014-03-21 09:51:33 +00:00
|
|
|
Mode mode_;
|
2014-02-13 16:17:55 +00:00
|
|
|
|
2013-10-14 16:46:51 +00:00
|
|
|
private:
|
|
|
|
Scanner* scanner_;
|
|
|
|
uintptr_t stack_limit_;
|
|
|
|
bool stack_overflow_;
|
|
|
|
|
|
|
|
bool allow_lazy_;
|
|
|
|
bool allow_natives_syntax_;
|
|
|
|
bool allow_generators_;
|
|
|
|
bool allow_for_of_;
|
2014-02-19 08:56:11 +00:00
|
|
|
|
|
|
|
typename Traits::Type::Zone* zone_; // Only used by Parser.
|
2013-10-14 16:46:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-02-11 09:35:32 +00:00
|
|
|
class PreParserIdentifier {
|
|
|
|
public:
|
2014-03-21 10:34:51 +00:00
|
|
|
PreParserIdentifier() : type_(kUnknownIdentifier) {}
|
2014-02-11 09:35:32 +00:00
|
|
|
static PreParserIdentifier Default() {
|
|
|
|
return PreParserIdentifier(kUnknownIdentifier);
|
|
|
|
}
|
|
|
|
static PreParserIdentifier Eval() {
|
|
|
|
return PreParserIdentifier(kEvalIdentifier);
|
|
|
|
}
|
|
|
|
static PreParserIdentifier Arguments() {
|
|
|
|
return PreParserIdentifier(kArgumentsIdentifier);
|
|
|
|
}
|
|
|
|
static PreParserIdentifier FutureReserved() {
|
|
|
|
return PreParserIdentifier(kFutureReservedIdentifier);
|
|
|
|
}
|
|
|
|
static PreParserIdentifier FutureStrictReserved() {
|
|
|
|
return PreParserIdentifier(kFutureStrictReservedIdentifier);
|
|
|
|
}
|
|
|
|
static PreParserIdentifier Yield() {
|
|
|
|
return PreParserIdentifier(kYieldIdentifier);
|
|
|
|
}
|
|
|
|
bool IsEval() { return type_ == kEvalIdentifier; }
|
|
|
|
bool IsArguments() { return type_ == kArgumentsIdentifier; }
|
|
|
|
bool IsEvalOrArguments() { return type_ >= kEvalIdentifier; }
|
|
|
|
bool IsYield() { return type_ == kYieldIdentifier; }
|
|
|
|
bool IsFutureReserved() { return type_ == kFutureReservedIdentifier; }
|
|
|
|
bool IsFutureStrictReserved() {
|
|
|
|
return type_ == kFutureStrictReservedIdentifier;
|
|
|
|
}
|
|
|
|
bool IsValidStrictVariable() { return type_ == kUnknownIdentifier; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum Type {
|
|
|
|
kUnknownIdentifier,
|
|
|
|
kFutureReservedIdentifier,
|
|
|
|
kFutureStrictReservedIdentifier,
|
|
|
|
kYieldIdentifier,
|
|
|
|
kEvalIdentifier,
|
|
|
|
kArgumentsIdentifier
|
|
|
|
};
|
|
|
|
explicit PreParserIdentifier(Type type) : type_(type) {}
|
|
|
|
Type type_;
|
|
|
|
|
|
|
|
friend class PreParserExpression;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Bits 0 and 1 are used to identify the type of expression:
|
|
|
|
// If bit 0 is set, it's an identifier.
|
|
|
|
// if bit 1 is set, it's a string literal.
|
|
|
|
// If neither is set, it's no particular type, and both set isn't
|
|
|
|
// use yet.
|
|
|
|
class PreParserExpression {
|
|
|
|
public:
|
|
|
|
static PreParserExpression Default() {
|
|
|
|
return PreParserExpression(kUnknownExpression);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PreParserExpression FromIdentifier(PreParserIdentifier id) {
|
|
|
|
return PreParserExpression(kIdentifierFlag |
|
|
|
|
(id.type_ << kIdentifierShift));
|
|
|
|
}
|
|
|
|
|
|
|
|
static PreParserExpression StringLiteral() {
|
|
|
|
return PreParserExpression(kUnknownStringLiteral);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PreParserExpression UseStrictStringLiteral() {
|
|
|
|
return PreParserExpression(kUseStrictString);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PreParserExpression This() {
|
|
|
|
return PreParserExpression(kThisExpression);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PreParserExpression ThisProperty() {
|
|
|
|
return PreParserExpression(kThisPropertyExpression);
|
|
|
|
}
|
|
|
|
|
2014-03-20 13:18:15 +00:00
|
|
|
static PreParserExpression Property() {
|
|
|
|
return PreParserExpression(kPropertyExpression);
|
2014-02-11 09:35:32 +00:00
|
|
|
}
|
|
|
|
|
2014-04-02 11:03:05 +00:00
|
|
|
static PreParserExpression Call() {
|
|
|
|
return PreParserExpression(kCallExpression);
|
|
|
|
}
|
|
|
|
|
2014-02-11 09:35:32 +00:00
|
|
|
bool IsIdentifier() { return (code_ & kIdentifierFlag) != 0; }
|
|
|
|
|
|
|
|
PreParserIdentifier AsIdentifier() {
|
2014-04-02 11:03:05 +00:00
|
|
|
ASSERT(IsIdentifier());
|
2014-02-11 09:35:32 +00:00
|
|
|
return PreParserIdentifier(
|
|
|
|
static_cast<PreParserIdentifier::Type>(code_ >> kIdentifierShift));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsStringLiteral() { return (code_ & kStringLiteralFlag) != 0; }
|
|
|
|
|
|
|
|
bool IsUseStrictLiteral() {
|
|
|
|
return (code_ & kStringLiteralMask) == kUseStrictString;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsThis() { return code_ == kThisExpression; }
|
|
|
|
|
|
|
|
bool IsThisProperty() { return code_ == kThisPropertyExpression; }
|
|
|
|
|
2014-03-20 13:18:15 +00:00
|
|
|
bool IsProperty() {
|
|
|
|
return code_ == kPropertyExpression || code_ == kThisPropertyExpression;
|
|
|
|
}
|
2014-02-11 09:35:32 +00:00
|
|
|
|
2014-04-02 11:03:05 +00:00
|
|
|
bool IsCall() { return code_ == kCallExpression; }
|
|
|
|
|
|
|
|
bool IsValidReferenceExpression() {
|
2014-03-21 09:46:18 +00:00
|
|
|
return IsIdentifier() || IsProperty();
|
|
|
|
}
|
|
|
|
|
2014-03-21 09:51:33 +00:00
|
|
|
// At the moment PreParser doesn't track these expression types.
|
|
|
|
bool IsFunctionLiteral() const { return false; }
|
|
|
|
bool IsCallNew() const { return false; }
|
|
|
|
|
|
|
|
PreParserExpression AsFunctionLiteral() { return *this; }
|
|
|
|
|
2014-03-21 09:46:18 +00:00
|
|
|
// Dummy implementation for making expression->somefunc() work in both Parser
|
|
|
|
// and PreParser.
|
2014-03-14 09:43:04 +00:00
|
|
|
PreParserExpression* operator->() { return this; }
|
|
|
|
|
2014-03-14 09:51:22 +00:00
|
|
|
// More dummy implementations of things PreParser doesn't need to track:
|
|
|
|
void set_index(int index) {} // For YieldExpressions
|
2014-03-21 09:51:33 +00:00
|
|
|
void set_parenthesized() {}
|
2014-03-14 09:51:22 +00:00
|
|
|
|
2014-02-11 09:35:32 +00:00
|
|
|
private:
|
2014-03-20 13:18:15 +00:00
|
|
|
// Least significant 2 bits are used as flags. Bits 0 and 1 represent
|
|
|
|
// identifiers or strings literals, and are mutually exclusive, but can both
|
|
|
|
// be absent. If the expression is an identifier or a string literal, the
|
|
|
|
// other bits describe the type (see PreParserIdentifier::Type and string
|
|
|
|
// literal constants below).
|
2014-02-11 09:35:32 +00:00
|
|
|
enum {
|
|
|
|
kUnknownExpression = 0,
|
|
|
|
// Identifiers
|
|
|
|
kIdentifierFlag = 1, // Used to detect labels.
|
|
|
|
kIdentifierShift = 3,
|
|
|
|
|
|
|
|
kStringLiteralFlag = 2, // Used to detect directive prologue.
|
|
|
|
kUnknownStringLiteral = kStringLiteralFlag,
|
|
|
|
kUseStrictString = kStringLiteralFlag | 8,
|
|
|
|
kStringLiteralMask = kUseStrictString,
|
|
|
|
|
2014-03-20 13:18:15 +00:00
|
|
|
// Below here applies if neither identifier nor string literal. Reserve the
|
|
|
|
// 2 least significant bits for flags.
|
|
|
|
kThisExpression = 1 << 2,
|
|
|
|
kThisPropertyExpression = 2 << 2,
|
2014-04-02 11:03:05 +00:00
|
|
|
kPropertyExpression = 3 << 2,
|
|
|
|
kCallExpression = 4 << 2
|
2014-02-11 09:35:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
explicit PreParserExpression(int expression_code) : code_(expression_code) {}
|
|
|
|
|
|
|
|
int code_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-02-19 08:56:11 +00:00
|
|
|
// 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).
|
2014-03-11 16:30:47 +00:00
|
|
|
PreParserExpressionList() : length_(0) {}
|
2014-02-19 08:56:11 +00:00
|
|
|
PreParserExpressionList* operator->() { return this; }
|
2014-03-11 16:30:47 +00:00
|
|
|
void Add(PreParserExpression, void*) { ++length_; }
|
|
|
|
int length() const { return length_; }
|
|
|
|
private:
|
|
|
|
int length_;
|
2014-02-19 08:56:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-04-25 09:44:20 +00:00
|
|
|
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*) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-02-13 16:17:55 +00:00
|
|
|
class PreParserScope {
|
|
|
|
public:
|
|
|
|
explicit PreParserScope(PreParserScope* outer_scope, ScopeType scope_type)
|
|
|
|
: scope_type_(scope_type) {
|
2014-03-24 12:16:09 +00:00
|
|
|
strict_mode_ = outer_scope ? outer_scope->strict_mode() : SLOPPY;
|
2014-02-13 16:17:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ScopeType type() { return scope_type_; }
|
2014-03-11 14:41:22 +00:00
|
|
|
StrictMode strict_mode() const { return strict_mode_; }
|
|
|
|
void SetStrictMode(StrictMode strict_mode) { strict_mode_ = strict_mode; }
|
2014-02-13 16:17:55 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
ScopeType scope_type_;
|
2014-03-11 14:41:22 +00:00
|
|
|
StrictMode strict_mode_;
|
2014-02-13 16:17:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class PreParserFactory {
|
|
|
|
public:
|
2014-06-18 07:30:56 +00:00
|
|
|
explicit PreParserFactory(void* extra_param) {}
|
|
|
|
PreParserExpression NewLiteral(PreParserIdentifier identifier,
|
|
|
|
int pos) {
|
2014-03-17 13:54:42 +00:00
|
|
|
return PreParserExpression::Default();
|
|
|
|
}
|
2014-03-21 09:51:33 +00:00
|
|
|
PreParserExpression NewNumberLiteral(double number,
|
|
|
|
int pos) {
|
2014-02-19 08:56:11 +00:00
|
|
|
return PreParserExpression::Default();
|
|
|
|
}
|
2014-03-21 09:51:33 +00:00
|
|
|
PreParserExpression NewRegExpLiteral(PreParserIdentifier js_pattern,
|
|
|
|
PreParserIdentifier js_flags,
|
|
|
|
int literal_index,
|
|
|
|
int pos) {
|
2014-03-17 13:54:42 +00:00
|
|
|
return PreParserExpression::Default();
|
|
|
|
}
|
2014-02-19 08:56:11 +00:00
|
|
|
PreParserExpression NewArrayLiteral(PreParserExpressionList values,
|
|
|
|
int literal_index,
|
|
|
|
int pos) {
|
|
|
|
return PreParserExpression::Default();
|
|
|
|
}
|
2014-03-11 15:40:41 +00:00
|
|
|
PreParserExpression NewObjectLiteralProperty(bool is_getter,
|
|
|
|
PreParserExpression value,
|
|
|
|
int pos) {
|
|
|
|
return PreParserExpression::Default();
|
|
|
|
}
|
|
|
|
PreParserExpression NewObjectLiteralProperty(PreParserExpression key,
|
|
|
|
PreParserExpression value) {
|
|
|
|
return PreParserExpression::Default();
|
|
|
|
}
|
|
|
|
PreParserExpression NewObjectLiteral(PreParserExpressionList properties,
|
|
|
|
int literal_index,
|
|
|
|
int boilerplate_properties,
|
|
|
|
bool has_function,
|
|
|
|
int pos) {
|
|
|
|
return PreParserExpression::Default();
|
|
|
|
}
|
2014-03-21 09:51:33 +00:00
|
|
|
PreParserExpression NewVariableProxy(void* generator_variable) {
|
2014-03-11 15:40:41 +00:00
|
|
|
return PreParserExpression::Default();
|
|
|
|
}
|
2014-03-21 09:51:33 +00:00
|
|
|
PreParserExpression NewProperty(PreParserExpression obj,
|
|
|
|
PreParserExpression key,
|
|
|
|
int pos) {
|
|
|
|
if (obj.IsThis()) {
|
|
|
|
return PreParserExpression::ThisProperty();
|
|
|
|
}
|
|
|
|
return PreParserExpression::Property();
|
|
|
|
}
|
|
|
|
PreParserExpression NewUnaryOperation(Token::Value op,
|
|
|
|
PreParserExpression expression,
|
|
|
|
int pos) {
|
|
|
|
return PreParserExpression::Default();
|
|
|
|
}
|
|
|
|
PreParserExpression NewBinaryOperation(Token::Value op,
|
|
|
|
PreParserExpression left,
|
|
|
|
PreParserExpression right, int pos) {
|
|
|
|
return PreParserExpression::Default();
|
|
|
|
}
|
|
|
|
PreParserExpression NewCompareOperation(Token::Value op,
|
|
|
|
PreParserExpression left,
|
|
|
|
PreParserExpression right, int pos) {
|
2014-03-11 15:40:41 +00:00
|
|
|
return PreParserExpression::Default();
|
|
|
|
}
|
2014-03-14 09:43:04 +00:00
|
|
|
PreParserExpression NewAssignment(Token::Value op,
|
|
|
|
PreParserExpression left,
|
|
|
|
PreParserExpression right,
|
|
|
|
int pos) {
|
|
|
|
return PreParserExpression::Default();
|
|
|
|
}
|
2014-03-14 09:51:22 +00:00
|
|
|
PreParserExpression NewYield(PreParserExpression generator_object,
|
|
|
|
PreParserExpression expression,
|
|
|
|
Yield::Kind yield_kind,
|
|
|
|
int pos) {
|
|
|
|
return PreParserExpression::Default();
|
|
|
|
}
|
2014-03-17 13:36:39 +00:00
|
|
|
PreParserExpression NewConditional(PreParserExpression condition,
|
|
|
|
PreParserExpression then_expression,
|
|
|
|
PreParserExpression else_expression,
|
|
|
|
int pos) {
|
|
|
|
return PreParserExpression::Default();
|
|
|
|
}
|
2014-03-19 14:08:47 +00:00
|
|
|
PreParserExpression NewCountOperation(Token::Value op,
|
|
|
|
bool is_prefix,
|
|
|
|
PreParserExpression expression,
|
|
|
|
int pos) {
|
|
|
|
return PreParserExpression::Default();
|
|
|
|
}
|
2014-03-21 09:51:33 +00:00
|
|
|
PreParserExpression NewCall(PreParserExpression expression,
|
|
|
|
PreParserExpressionList arguments,
|
|
|
|
int pos) {
|
2014-04-02 11:03:05 +00:00
|
|
|
return PreParserExpression::Call();
|
2014-03-21 09:51:33 +00:00
|
|
|
}
|
2014-03-21 10:34:51 +00:00
|
|
|
PreParserExpression NewCallNew(PreParserExpression expression,
|
|
|
|
PreParserExpressionList arguments,
|
|
|
|
int pos) {
|
|
|
|
return PreParserExpression::Default();
|
|
|
|
}
|
2014-02-13 16:17:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class PreParser;
|
2014-02-11 09:35:32 +00:00
|
|
|
|
|
|
|
class PreParserTraits {
|
|
|
|
public:
|
2014-02-13 16:17:55 +00:00
|
|
|
struct Type {
|
2014-03-12 19:15:17 +00:00
|
|
|
// TODO(marja): To be removed. The Traits object should contain all the data
|
|
|
|
// it needs.
|
2014-02-13 16:17:55 +00:00
|
|
|
typedef PreParser* Parser;
|
|
|
|
|
2014-03-12 19:15:17 +00:00
|
|
|
// Used by FunctionState and BlockState.
|
2014-02-13 16:17:55 +00:00
|
|
|
typedef PreParserScope Scope;
|
|
|
|
// PreParser doesn't need to store generator variables.
|
|
|
|
typedef void GeneratorVariable;
|
|
|
|
// No interaction with Zones.
|
|
|
|
typedef void Zone;
|
|
|
|
|
|
|
|
// Return types for traversing functions.
|
|
|
|
typedef PreParserIdentifier Identifier;
|
|
|
|
typedef PreParserExpression Expression;
|
2014-03-14 09:51:22 +00:00
|
|
|
typedef PreParserExpression YieldExpression;
|
2014-03-11 15:40:41 +00:00
|
|
|
typedef PreParserExpression FunctionLiteral;
|
|
|
|
typedef PreParserExpression ObjectLiteralProperty;
|
|
|
|
typedef PreParserExpression Literal;
|
2014-02-19 08:56:11 +00:00
|
|
|
typedef PreParserExpressionList ExpressionList;
|
2014-03-11 15:40:41 +00:00
|
|
|
typedef PreParserExpressionList PropertyList;
|
2014-04-25 09:44:20 +00:00
|
|
|
typedef PreParserStatementList StatementList;
|
2014-03-12 19:15:17 +00:00
|
|
|
|
|
|
|
// For constructing objects returned by the traversing functions.
|
|
|
|
typedef PreParserFactory Factory;
|
2014-02-13 16:17:55 +00:00
|
|
|
};
|
2014-02-11 09:35:32 +00:00
|
|
|
|
|
|
|
explicit PreParserTraits(PreParser* pre_parser) : pre_parser_(pre_parser) {}
|
|
|
|
|
2014-02-13 16:17:55 +00:00
|
|
|
// Custom operations executed when FunctionStates are created and
|
|
|
|
// destructed. (The PreParser doesn't need to do anything.)
|
2014-02-28 12:08:17 +00:00
|
|
|
template<typename FunctionState>
|
|
|
|
static void SetUpFunctionState(FunctionState* function_state, void*) {}
|
|
|
|
template<typename FunctionState>
|
2014-03-28 12:49:34 +00:00
|
|
|
static void TearDownFunctionState(FunctionState* function_state, void*) {}
|
2014-02-13 16:17:55 +00:00
|
|
|
|
2014-02-11 09:35:32 +00:00
|
|
|
// Helper functions for recursive descent.
|
2014-02-13 16:17:55 +00:00
|
|
|
static bool IsEvalOrArguments(PreParserIdentifier identifier) {
|
2014-02-11 09:35:32 +00:00
|
|
|
return identifier.IsEvalOrArguments();
|
|
|
|
}
|
|
|
|
|
2014-03-14 09:43:04 +00:00
|
|
|
// Returns true if the expression is of type "this.foo".
|
|
|
|
static bool IsThisProperty(PreParserExpression expression) {
|
|
|
|
return expression.IsThisProperty();
|
|
|
|
}
|
|
|
|
|
2014-03-19 14:08:47 +00:00
|
|
|
static bool IsIdentifier(PreParserExpression expression) {
|
|
|
|
return expression.IsIdentifier();
|
|
|
|
}
|
|
|
|
|
2014-04-02 11:03:05 +00:00
|
|
|
static PreParserIdentifier AsIdentifier(PreParserExpression expression) {
|
|
|
|
return expression.AsIdentifier();
|
|
|
|
}
|
|
|
|
|
2014-03-11 15:40:41 +00:00
|
|
|
static bool IsBoilerplateProperty(PreParserExpression property) {
|
|
|
|
// PreParser doesn't count boilerplate properties.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool IsArrayIndex(PreParserIdentifier string, uint32_t* index) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-14 09:43:04 +00:00
|
|
|
// Functions for encapsulating the differences between parsing and preparsing;
|
|
|
|
// operations interleaved with the recursive descent.
|
2014-03-11 15:40:41 +00:00
|
|
|
static void PushLiteralName(FuncNameInferrer* fni, PreParserIdentifier id) {
|
|
|
|
// PreParser should not use FuncNameInferrer.
|
2014-03-21 10:34:51 +00:00
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
static void PushPropertyName(FuncNameInferrer* fni,
|
|
|
|
PreParserExpression expression) {
|
|
|
|
// PreParser should not use FuncNameInferrer.
|
|
|
|
UNREACHABLE();
|
2014-03-11 15:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void CheckFunctionLiteralInsideTopLevelObjectLiteral(
|
|
|
|
PreParserScope* scope, PreParserExpression value, bool* has_function) {}
|
|
|
|
|
2014-03-14 09:43:04 +00:00
|
|
|
static void CheckAssigningFunctionLiteralToProperty(
|
|
|
|
PreParserExpression left, PreParserExpression right) {}
|
|
|
|
|
2014-03-21 09:51:33 +00:00
|
|
|
// PreParser doesn't need to keep track of eval calls.
|
|
|
|
static void CheckPossibleEvalCall(PreParserExpression expression,
|
|
|
|
PreParserScope* scope) {}
|
|
|
|
|
2014-03-14 09:43:04 +00:00
|
|
|
static PreParserExpression MarkExpressionAsLValue(
|
|
|
|
PreParserExpression expression) {
|
|
|
|
// TODO(marja): To be able to produce the same errors, the preparser needs
|
|
|
|
// to start tracking which expressions are variables and which are lvalues.
|
|
|
|
return expression;
|
|
|
|
}
|
|
|
|
|
2014-03-17 13:54:42 +00:00
|
|
|
bool ShortcutNumericLiteralBinaryExpression(PreParserExpression* x,
|
|
|
|
PreParserExpression y,
|
|
|
|
Token::Value op,
|
|
|
|
int pos,
|
|
|
|
PreParserFactory* factory) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-14 09:43:04 +00:00
|
|
|
|
2014-03-19 14:08:47 +00:00
|
|
|
PreParserExpression BuildUnaryExpression(PreParserExpression expression,
|
|
|
|
Token::Value op, int pos,
|
|
|
|
PreParserFactory* factory) {
|
|
|
|
return PreParserExpression::Default();
|
|
|
|
}
|
|
|
|
|
2014-04-02 11:03:05 +00:00
|
|
|
PreParserExpression NewThrowReferenceError(const char* type, int pos) {
|
|
|
|
return PreParserExpression::Default();
|
|
|
|
}
|
|
|
|
PreParserExpression NewThrowSyntaxError(
|
|
|
|
const char* type, Handle<Object> arg, int pos) {
|
|
|
|
return PreParserExpression::Default();
|
|
|
|
}
|
|
|
|
PreParserExpression NewThrowTypeError(
|
2014-04-02 12:38:01 +00:00
|
|
|
const char* type, Handle<Object> arg, int pos) {
|
2014-04-02 11:03:05 +00:00
|
|
|
return PreParserExpression::Default();
|
|
|
|
}
|
|
|
|
|
2014-02-11 09:35:32 +00:00
|
|
|
// Reporting errors.
|
|
|
|
void ReportMessageAt(Scanner::Location location,
|
|
|
|
const char* message,
|
2014-05-15 09:44:57 +00:00
|
|
|
const char* arg = NULL,
|
2014-03-17 10:21:01 +00:00
|
|
|
bool is_reference_error = false);
|
2014-02-11 09:35:32 +00:00
|
|
|
void ReportMessageAt(int start_pos,
|
|
|
|
int end_pos,
|
2014-05-15 09:44:57 +00:00
|
|
|
const char* message,
|
|
|
|
const char* arg = NULL,
|
2014-03-17 10:21:01 +00:00
|
|
|
bool is_reference_error = false);
|
2014-02-11 09:35:32 +00:00
|
|
|
|
2014-02-11 11:51:01 +00:00
|
|
|
// "null" return type creators.
|
2014-02-13 16:17:55 +00:00
|
|
|
static PreParserIdentifier EmptyIdentifier() {
|
2014-02-11 09:35:32 +00:00
|
|
|
return PreParserIdentifier::Default();
|
|
|
|
}
|
2014-02-13 16:17:55 +00:00
|
|
|
static PreParserExpression EmptyExpression() {
|
2014-02-11 11:51:01 +00:00
|
|
|
return PreParserExpression::Default();
|
|
|
|
}
|
2014-03-11 15:40:41 +00:00
|
|
|
static PreParserExpression EmptyLiteral() {
|
|
|
|
return PreParserExpression::Default();
|
|
|
|
}
|
2014-03-11 16:30:47 +00:00
|
|
|
static PreParserExpressionList NullExpressionList() {
|
|
|
|
return PreParserExpressionList();
|
|
|
|
}
|
2014-02-11 09:35:32 +00:00
|
|
|
|
2014-02-19 08:56:11 +00:00
|
|
|
// Odd-ball literal creators.
|
|
|
|
static PreParserExpression GetLiteralTheHole(int position,
|
|
|
|
PreParserFactory* factory) {
|
|
|
|
return PreParserExpression::Default();
|
|
|
|
}
|
|
|
|
|
2014-02-11 11:51:01 +00:00
|
|
|
// Producing data during the recursive descent.
|
2014-02-14 11:24:26 +00:00
|
|
|
PreParserIdentifier GetSymbol(Scanner* scanner);
|
2014-06-18 07:30:56 +00:00
|
|
|
static PreParserIdentifier NextLiteralString(Scanner* scanner,
|
|
|
|
PretenureFlag tenured) {
|
2014-02-11 11:51:01 +00:00
|
|
|
return PreParserIdentifier::Default();
|
|
|
|
}
|
2014-02-11 09:35:32 +00:00
|
|
|
|
2014-02-14 11:24:26 +00:00
|
|
|
static PreParserExpression ThisExpression(PreParserScope* scope,
|
|
|
|
PreParserFactory* factory) {
|
|
|
|
return PreParserExpression::This();
|
|
|
|
}
|
|
|
|
|
|
|
|
static PreParserExpression ExpressionFromLiteral(
|
|
|
|
Token::Value token, int pos, Scanner* scanner,
|
|
|
|
PreParserFactory* factory) {
|
|
|
|
return PreParserExpression::Default();
|
|
|
|
}
|
|
|
|
|
|
|
|
static PreParserExpression ExpressionFromIdentifier(
|
|
|
|
PreParserIdentifier name, int pos, PreParserScope* scope,
|
|
|
|
PreParserFactory* factory) {
|
|
|
|
return PreParserExpression::FromIdentifier(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
PreParserExpression ExpressionFromString(int pos,
|
|
|
|
Scanner* scanner,
|
|
|
|
PreParserFactory* factory = NULL);
|
|
|
|
|
2014-02-19 08:56:11 +00:00
|
|
|
static PreParserExpressionList NewExpressionList(int size, void* zone) {
|
|
|
|
return PreParserExpressionList();
|
|
|
|
}
|
|
|
|
|
2014-04-25 09:44:20 +00:00
|
|
|
static PreParserStatementList NewStatementList(int size, void* zone) {
|
|
|
|
return PreParserStatementList();
|
|
|
|
}
|
|
|
|
|
2014-03-11 15:40:41 +00:00
|
|
|
static PreParserExpressionList NewPropertyList(int size, void* zone) {
|
|
|
|
return PreParserExpressionList();
|
|
|
|
}
|
|
|
|
|
2014-02-14 11:24:26 +00:00
|
|
|
// Temporary glue; these functions will move to ParserBase.
|
|
|
|
PreParserExpression ParseV8Intrinsic(bool* ok);
|
2014-03-11 15:40:41 +00:00
|
|
|
PreParserExpression ParseFunctionLiteral(
|
|
|
|
PreParserIdentifier name,
|
|
|
|
Scanner::Location function_name_location,
|
|
|
|
bool name_is_strict_reserved,
|
|
|
|
bool is_generator,
|
|
|
|
int function_token_position,
|
|
|
|
FunctionLiteral::FunctionType type,
|
2014-06-17 07:23:26 +00:00
|
|
|
FunctionLiteral::ArityRestriction arity_restriction,
|
2014-03-11 15:40:41 +00:00
|
|
|
bool* ok);
|
2014-02-14 11:24:26 +00:00
|
|
|
|
2014-02-11 09:35:32 +00:00
|
|
|
private:
|
|
|
|
PreParser* pre_parser_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-11-02 07:21:37 +00:00
|
|
|
// Preparsing checks a JavaScript program and emits preparse-data that helps
|
|
|
|
// a later parsing to be faster.
|
2011-05-06 11:41:15 +00:00
|
|
|
// See preparse-data-format.h for the data format.
|
2010-11-02 07:21:37 +00:00
|
|
|
|
|
|
|
// The PreParser checks that the syntax follows the grammar for JavaScript,
|
|
|
|
// and collects some information about the program along the way.
|
|
|
|
// The grammar check is only performed in order to understand the program
|
|
|
|
// sufficiently to deduce some information about it, that can be used
|
|
|
|
// to speed up later parsing. Finding errors is not the goal of pre-parsing,
|
|
|
|
// rather it is to speed up properly written and correct programs.
|
|
|
|
// That means that contextual checks (like a label being declared where
|
|
|
|
// it is used) are generally omitted.
|
2014-02-11 09:35:32 +00:00
|
|
|
class PreParser : public ParserBase<PreParserTraits> {
|
2010-11-02 07:21:37 +00:00
|
|
|
public:
|
2014-02-11 09:35:32 +00:00
|
|
|
typedef PreParserIdentifier Identifier;
|
|
|
|
typedef PreParserExpression Expression;
|
2014-04-25 09:44:20 +00:00
|
|
|
typedef PreParserStatement Statement;
|
2014-02-11 09:35:32 +00:00
|
|
|
|
2010-11-29 13:24:37 +00:00
|
|
|
enum PreParseResult {
|
|
|
|
kPreParseStackOverflow,
|
|
|
|
kPreParseSuccess
|
|
|
|
};
|
|
|
|
|
2014-03-19 13:24:13 +00:00
|
|
|
PreParser(Scanner* scanner, ParserRecorder* log, uintptr_t stack_limit)
|
|
|
|
: ParserBase<PreParserTraits>(scanner, stack_limit, NULL, log, NULL,
|
|
|
|
this) {}
|
2010-11-02 07:21:37 +00:00
|
|
|
|
|
|
|
// Pre-parse the program from the character stream; returns true on
|
|
|
|
// success (even if parsing failed, the pre-parse data successfully
|
|
|
|
// captured the syntax error), and false if a stack-overflow happened
|
|
|
|
// during parsing.
|
Refactor parser mode configuration for correctness
This patch refactors the parser and preparser interface to be more
readable and type-safe. It has no behavior changes.
Previously, parsers and preparsers were configured via bitfield called
parser_flags in the Parser constructor, and flags in
PreParser::PreParseProgram, ParserApi::Parse, and ParserApi::PreParse.
This was error-prone in practice: six call sites passed incorrectly
typed values to this interface (a boolean FLAG value, a boolean false
and a boolean true value). None of these errors were caught by the
compiler because it's just an "int".
The parser flags interface was also awkward because it encoded a
language mode, but the language mode was only used to turn on harmony
scoping or not -- it wasn't used to actually set the parser's language
mode.
Fundamentally these errors came in because of the desire for a
procedural parser interface, in ParserApi. Because we need to be able
to configure the parser in various ways, the flags argument got added;
but no one understood how to use the flags properly. Also they were
only used by constructors: callers packed bits, and the constructors
unpacked them into booleans on the parser or preparser.
The solution is to allow parser construction, configuration, and
invocation to be separated. This patch does that.
It passes the existing tests.
BUG=
Review URL: https://codereview.chromium.org/13450007
Patch from Andy Wingo <wingo@igalia.com>.
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@14151 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2013-04-05 13:01:06 +00:00
|
|
|
PreParseResult PreParseProgram() {
|
2014-02-13 16:17:55 +00:00
|
|
|
PreParserScope scope(scope_, GLOBAL_SCOPE);
|
|
|
|
FunctionState top_scope(&function_state_, &scope_, &scope, NULL);
|
Refactor parser mode configuration for correctness
This patch refactors the parser and preparser interface to be more
readable and type-safe. It has no behavior changes.
Previously, parsers and preparsers were configured via bitfield called
parser_flags in the Parser constructor, and flags in
PreParser::PreParseProgram, ParserApi::Parse, and ParserApi::PreParse.
This was error-prone in practice: six call sites passed incorrectly
typed values to this interface (a boolean FLAG value, a boolean false
and a boolean true value). None of these errors were caught by the
compiler because it's just an "int".
The parser flags interface was also awkward because it encoded a
language mode, but the language mode was only used to turn on harmony
scoping or not -- it wasn't used to actually set the parser's language
mode.
Fundamentally these errors came in because of the desire for a
procedural parser interface, in ParserApi. Because we need to be able
to configure the parser in various ways, the flags argument got added;
but no one understood how to use the flags properly. Also they were
only used by constructors: callers packed bits, and the constructors
unpacked them into booleans on the parser or preparser.
The solution is to allow parser construction, configuration, and
invocation to be separated. This patch does that.
It passes the existing tests.
BUG=
Review URL: https://codereview.chromium.org/13450007
Patch from Andy Wingo <wingo@igalia.com>.
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@14151 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2013-04-05 13:01:06 +00:00
|
|
|
bool ok = true;
|
2013-10-14 16:46:51 +00:00
|
|
|
int start_position = scanner()->peek_location().beg_pos;
|
2013-10-15 08:32:58 +00:00
|
|
|
ParseSourceElements(Token::EOS, &ok);
|
2013-10-14 16:46:51 +00:00
|
|
|
if (stack_overflow()) return kPreParseStackOverflow;
|
Refactor parser mode configuration for correctness
This patch refactors the parser and preparser interface to be more
readable and type-safe. It has no behavior changes.
Previously, parsers and preparsers were configured via bitfield called
parser_flags in the Parser constructor, and flags in
PreParser::PreParseProgram, ParserApi::Parse, and ParserApi::PreParse.
This was error-prone in practice: six call sites passed incorrectly
typed values to this interface (a boolean FLAG value, a boolean false
and a boolean true value). None of these errors were caught by the
compiler because it's just an "int".
The parser flags interface was also awkward because it encoded a
language mode, but the language mode was only used to turn on harmony
scoping or not -- it wasn't used to actually set the parser's language
mode.
Fundamentally these errors came in because of the desire for a
procedural parser interface, in ParserApi. Because we need to be able
to configure the parser in various ways, the flags argument got added;
but no one understood how to use the flags properly. Also they were
only used by constructors: callers packed bits, and the constructors
unpacked them into booleans on the parser or preparser.
The solution is to allow parser construction, configuration, and
invocation to be separated. This patch does that.
It passes the existing tests.
BUG=
Review URL: https://codereview.chromium.org/13450007
Patch from Andy Wingo <wingo@igalia.com>.
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@14151 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2013-04-05 13:01:06 +00:00
|
|
|
if (!ok) {
|
2013-10-14 16:46:51 +00:00
|
|
|
ReportUnexpectedToken(scanner()->current_token());
|
2014-03-11 14:41:22 +00:00
|
|
|
} else if (scope_->strict_mode() == STRICT) {
|
2013-10-14 16:46:51 +00:00
|
|
|
CheckOctalLiteral(start_position, scanner()->location().end_pos, &ok);
|
Refactor parser mode configuration for correctness
This patch refactors the parser and preparser interface to be more
readable and type-safe. It has no behavior changes.
Previously, parsers and preparsers were configured via bitfield called
parser_flags in the Parser constructor, and flags in
PreParser::PreParseProgram, ParserApi::Parse, and ParserApi::PreParse.
This was error-prone in practice: six call sites passed incorrectly
typed values to this interface (a boolean FLAG value, a boolean false
and a boolean true value). None of these errors were caught by the
compiler because it's just an "int".
The parser flags interface was also awkward because it encoded a
language mode, but the language mode was only used to turn on harmony
scoping or not -- it wasn't used to actually set the parser's language
mode.
Fundamentally these errors came in because of the desire for a
procedural parser interface, in ParserApi. Because we need to be able
to configure the parser in various ways, the flags argument got added;
but no one understood how to use the flags properly. Also they were
only used by constructors: callers packed bits, and the constructors
unpacked them into booleans on the parser or preparser.
The solution is to allow parser construction, configuration, and
invocation to be separated. This patch does that.
It passes the existing tests.
BUG=
Review URL: https://codereview.chromium.org/13450007
Patch from Andy Wingo <wingo@igalia.com>.
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@14151 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2013-04-05 13:01:06 +00:00
|
|
|
}
|
|
|
|
return kPreParseSuccess;
|
2010-11-02 07:21:37 +00:00
|
|
|
}
|
|
|
|
|
2011-11-25 09:36:31 +00:00
|
|
|
// Parses a single function literal, from the opening parentheses before
|
|
|
|
// parameters to the closing brace after the body.
|
2012-12-07 10:35:50 +00:00
|
|
|
// Returns a FunctionEntry describing the body of the function in enough
|
2011-11-25 09:36:31 +00:00
|
|
|
// detail that it can be lazily compiled.
|
2013-04-02 17:34:59 +00:00
|
|
|
// The scanner is expected to have matched the "function" or "function*"
|
|
|
|
// keyword and parameters, and have consumed the initial '{'.
|
2012-12-07 10:35:50 +00:00
|
|
|
// At return, unless an error occurred, the scanner is positioned before the
|
2011-11-25 09:36:31 +00:00
|
|
|
// the final '}'.
|
2014-03-11 14:41:22 +00:00
|
|
|
PreParseResult PreParseLazyFunction(StrictMode strict_mode,
|
2013-04-02 17:34:59 +00:00
|
|
|
bool is_generator,
|
2013-10-15 08:32:58 +00:00
|
|
|
ParserRecorder* log);
|
2011-11-25 09:36:31 +00:00
|
|
|
|
2010-11-02 07:21:37 +00:00
|
|
|
private:
|
2014-02-11 09:35:32 +00:00
|
|
|
friend class PreParserTraits;
|
|
|
|
|
2011-05-19 09:01:46 +00:00
|
|
|
// These types form an algebra over syntactic categories that is just
|
|
|
|
// rich enough to let us recognize and propagate the constructs that
|
|
|
|
// are either being counted in the preparser data, or is important
|
|
|
|
// to throw the correct syntax error exceptions.
|
|
|
|
|
2011-08-16 14:24:12 +00:00
|
|
|
enum VariableDeclarationContext {
|
|
|
|
kSourceElement,
|
|
|
|
kStatement,
|
|
|
|
kForStatement
|
|
|
|
};
|
|
|
|
|
2011-10-17 12:19:06 +00:00
|
|
|
// If a list of variable declarations includes any initializers.
|
|
|
|
enum VariableDeclarationProperties {
|
|
|
|
kHasInitializers,
|
|
|
|
kHasNoInitializers
|
|
|
|
};
|
|
|
|
|
2010-11-29 13:24:37 +00:00
|
|
|
|
2011-05-19 09:01:46 +00:00
|
|
|
enum SourceElements {
|
|
|
|
kUnknownSourceElements
|
2010-11-29 13:24:37 +00:00
|
|
|
};
|
|
|
|
|
2010-11-02 07:21:37 +00:00
|
|
|
// All ParseXXX functions take as the last argument an *ok parameter
|
|
|
|
// which is set to false if parsing failed; it is unchanged otherwise.
|
|
|
|
// By making the 'exception handling' explicit, we are forced to check
|
|
|
|
// for failure at the call sites.
|
2011-08-16 14:24:12 +00:00
|
|
|
Statement ParseSourceElement(bool* ok);
|
2010-11-02 07:21:37 +00:00
|
|
|
SourceElements ParseSourceElements(int end_token, bool* ok);
|
|
|
|
Statement ParseStatement(bool* ok);
|
|
|
|
Statement ParseFunctionDeclaration(bool* ok);
|
|
|
|
Statement ParseBlock(bool* ok);
|
2011-08-16 14:24:12 +00:00
|
|
|
Statement ParseVariableStatement(VariableDeclarationContext var_context,
|
|
|
|
bool* ok);
|
|
|
|
Statement ParseVariableDeclarations(VariableDeclarationContext var_context,
|
2011-10-17 12:19:06 +00:00
|
|
|
VariableDeclarationProperties* decl_props,
|
2011-08-16 14:24:12 +00:00
|
|
|
int* num_decl,
|
|
|
|
bool* ok);
|
2010-11-02 07:21:37 +00:00
|
|
|
Statement ParseExpressionOrLabelledStatement(bool* ok);
|
|
|
|
Statement ParseIfStatement(bool* ok);
|
|
|
|
Statement ParseContinueStatement(bool* ok);
|
|
|
|
Statement ParseBreakStatement(bool* ok);
|
|
|
|
Statement ParseReturnStatement(bool* ok);
|
|
|
|
Statement ParseWithStatement(bool* ok);
|
|
|
|
Statement ParseSwitchStatement(bool* ok);
|
|
|
|
Statement ParseDoWhileStatement(bool* ok);
|
|
|
|
Statement ParseWhileStatement(bool* ok);
|
|
|
|
Statement ParseForStatement(bool* ok);
|
|
|
|
Statement ParseThrowStatement(bool* ok);
|
|
|
|
Statement ParseTryStatement(bool* ok);
|
|
|
|
Statement ParseDebuggerStatement(bool* ok);
|
|
|
|
Expression ParseConditionalExpression(bool accept_IN, bool* ok);
|
|
|
|
Expression ParseObjectLiteral(bool* ok);
|
|
|
|
Expression ParseV8Intrinsic(bool* ok);
|
|
|
|
|
2014-02-07 10:47:01 +00:00
|
|
|
Expression ParseFunctionLiteral(
|
|
|
|
Identifier name,
|
|
|
|
Scanner::Location function_name_location,
|
|
|
|
bool name_is_strict_reserved,
|
|
|
|
bool is_generator,
|
2014-03-10 15:19:20 +00:00
|
|
|
int function_token_pos,
|
|
|
|
FunctionLiteral::FunctionType function_type,
|
2014-06-17 07:23:26 +00:00
|
|
|
FunctionLiteral::ArityRestriction arity_restriction,
|
2014-02-07 10:47:01 +00:00
|
|
|
bool* ok);
|
2011-11-25 09:36:31 +00:00
|
|
|
void ParseLazyFunctionLiteralBody(bool* ok);
|
2010-11-02 07:21:37 +00:00
|
|
|
|
2013-06-12 12:37:44 +00:00
|
|
|
bool CheckInOrOf(bool accept_OF);
|
2010-11-02 07:21:37 +00:00
|
|
|
};
|
2013-10-10 11:58:16 +00:00
|
|
|
|
2014-02-13 16:17:55 +00:00
|
|
|
template<class Traits>
|
|
|
|
ParserBase<Traits>::FunctionState::FunctionState(
|
|
|
|
FunctionState** function_state_stack,
|
|
|
|
typename Traits::Type::Scope** scope_stack,
|
|
|
|
typename Traits::Type::Scope* scope,
|
2014-06-18 07:30:56 +00:00
|
|
|
typename Traits::Type::Zone* extra_param)
|
2014-02-13 16:17:55 +00:00
|
|
|
: next_materialized_literal_index_(JSFunction::kLiteralsPrefixSize),
|
|
|
|
next_handler_index_(0),
|
|
|
|
expected_property_count_(0),
|
|
|
|
is_generator_(false),
|
|
|
|
generator_object_variable_(NULL),
|
|
|
|
function_state_stack_(function_state_stack),
|
|
|
|
outer_function_state_(*function_state_stack),
|
|
|
|
scope_stack_(scope_stack),
|
|
|
|
outer_scope_(*scope_stack),
|
|
|
|
saved_ast_node_id_(0),
|
2014-03-28 12:49:34 +00:00
|
|
|
extra_param_(extra_param),
|
2014-06-18 07:30:56 +00:00
|
|
|
factory_(extra_param) {
|
2014-02-13 16:17:55 +00:00
|
|
|
*scope_stack_ = scope;
|
|
|
|
*function_state_stack = this;
|
|
|
|
Traits::SetUpFunctionState(this, extra_param);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class Traits>
|
|
|
|
ParserBase<Traits>::FunctionState::~FunctionState() {
|
|
|
|
*scope_stack_ = outer_scope_;
|
|
|
|
*function_state_stack_ = outer_function_state_;
|
2014-03-28 12:49:34 +00:00
|
|
|
Traits::TearDownFunctionState(this, extra_param_);
|
2014-02-13 16:17:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-11 09:35:32 +00:00
|
|
|
template<class Traits>
|
|
|
|
void ParserBase<Traits>::ReportUnexpectedToken(Token::Value token) {
|
|
|
|
Scanner::Location source_location = scanner()->location();
|
|
|
|
|
|
|
|
// Four of the tokens are treated specially
|
|
|
|
switch (token) {
|
|
|
|
case Token::EOS:
|
|
|
|
return ReportMessageAt(source_location, "unexpected_eos");
|
|
|
|
case Token::NUMBER:
|
|
|
|
return ReportMessageAt(source_location, "unexpected_token_number");
|
|
|
|
case Token::STRING:
|
|
|
|
return ReportMessageAt(source_location, "unexpected_token_string");
|
|
|
|
case Token::IDENTIFIER:
|
|
|
|
return ReportMessageAt(source_location, "unexpected_token_identifier");
|
|
|
|
case Token::FUTURE_RESERVED_WORD:
|
|
|
|
return ReportMessageAt(source_location, "unexpected_reserved");
|
|
|
|
case Token::YIELD:
|
|
|
|
case Token::FUTURE_STRICT_RESERVED_WORD:
|
2014-03-11 14:41:22 +00:00
|
|
|
return ReportMessageAt(source_location, strict_mode() == SLOPPY
|
|
|
|
? "unexpected_token_identifier" : "unexpected_strict_reserved");
|
2014-02-11 09:35:32 +00:00
|
|
|
default:
|
|
|
|
const char* name = Token::String(token);
|
|
|
|
ASSERT(name != NULL);
|
2014-06-03 16:12:48 +00:00
|
|
|
Traits::ReportMessageAt(source_location, "unexpected_token", name);
|
2014-02-11 09:35:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class Traits>
|
2014-03-18 17:05:38 +00:00
|
|
|
typename ParserBase<Traits>::IdentifierT ParserBase<Traits>::ParseIdentifier(
|
2014-02-11 09:35:32 +00:00
|
|
|
AllowEvalOrArgumentsAsIdentifier allow_eval_or_arguments,
|
|
|
|
bool* ok) {
|
|
|
|
Token::Value next = Next();
|
|
|
|
if (next == Token::IDENTIFIER) {
|
2014-03-18 17:05:38 +00:00
|
|
|
IdentifierT name = this->GetSymbol(scanner());
|
2014-02-11 09:35:32 +00:00
|
|
|
if (allow_eval_or_arguments == kDontAllowEvalOrArguments &&
|
2014-03-11 14:41:22 +00:00
|
|
|
strict_mode() == STRICT && this->IsEvalOrArguments(name)) {
|
2014-06-03 16:12:48 +00:00
|
|
|
ReportMessage("strict_eval_arguments");
|
2014-02-11 09:35:32 +00:00
|
|
|
*ok = false;
|
|
|
|
}
|
|
|
|
return name;
|
2014-03-11 14:41:22 +00:00
|
|
|
} else if (strict_mode() == SLOPPY &&
|
|
|
|
(next == Token::FUTURE_STRICT_RESERVED_WORD ||
|
|
|
|
(next == Token::YIELD && !is_generator()))) {
|
2014-02-14 11:24:26 +00:00
|
|
|
return this->GetSymbol(scanner());
|
2014-02-11 09:35:32 +00:00
|
|
|
} else {
|
|
|
|
this->ReportUnexpectedToken(next);
|
|
|
|
*ok = false;
|
|
|
|
return Traits::EmptyIdentifier();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class Traits>
|
2014-03-18 17:05:38 +00:00
|
|
|
typename ParserBase<Traits>::IdentifierT ParserBase<
|
2014-02-11 09:35:32 +00:00
|
|
|
Traits>::ParseIdentifierOrStrictReservedWord(bool* is_strict_reserved,
|
|
|
|
bool* ok) {
|
|
|
|
Token::Value next = Next();
|
|
|
|
if (next == Token::IDENTIFIER) {
|
|
|
|
*is_strict_reserved = false;
|
|
|
|
} else if (next == Token::FUTURE_STRICT_RESERVED_WORD ||
|
|
|
|
(next == Token::YIELD && !this->is_generator())) {
|
|
|
|
*is_strict_reserved = true;
|
|
|
|
} else {
|
|
|
|
ReportUnexpectedToken(next);
|
|
|
|
*ok = false;
|
|
|
|
return Traits::EmptyIdentifier();
|
|
|
|
}
|
2014-02-14 11:24:26 +00:00
|
|
|
return this->GetSymbol(scanner());
|
2014-02-11 09:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class Traits>
|
2014-03-18 17:05:38 +00:00
|
|
|
typename ParserBase<Traits>::IdentifierT
|
|
|
|
ParserBase<Traits>::ParseIdentifierName(bool* ok) {
|
2014-02-11 09:35:32 +00:00
|
|
|
Token::Value next = Next();
|
|
|
|
if (next != Token::IDENTIFIER && next != Token::FUTURE_RESERVED_WORD &&
|
|
|
|
next != Token::FUTURE_STRICT_RESERVED_WORD && !Token::IsKeyword(next)) {
|
|
|
|
this->ReportUnexpectedToken(next);
|
|
|
|
*ok = false;
|
|
|
|
return Traits::EmptyIdentifier();
|
|
|
|
}
|
2014-02-14 11:24:26 +00:00
|
|
|
return this->GetSymbol(scanner());
|
2014-02-11 09:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class Traits>
|
2014-03-18 17:05:38 +00:00
|
|
|
typename ParserBase<Traits>::IdentifierT
|
2014-02-11 09:35:32 +00:00
|
|
|
ParserBase<Traits>::ParseIdentifierNameOrGetOrSet(bool* is_get,
|
|
|
|
bool* is_set,
|
|
|
|
bool* ok) {
|
2014-03-18 17:05:38 +00:00
|
|
|
IdentifierT result = ParseIdentifierName(ok);
|
2014-02-11 09:35:32 +00:00
|
|
|
if (!*ok) return Traits::EmptyIdentifier();
|
2014-03-12 14:03:25 +00:00
|
|
|
scanner()->IsGetOrSet(is_get, is_set);
|
2014-02-11 09:35:32 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-11 11:51:01 +00:00
|
|
|
template <class Traits>
|
2014-03-18 17:05:38 +00:00
|
|
|
typename ParserBase<Traits>::ExpressionT ParserBase<Traits>::ParseRegExpLiteral(
|
|
|
|
bool seen_equal, bool* ok) {
|
2014-02-11 11:51:01 +00:00
|
|
|
int pos = peek_position();
|
|
|
|
if (!scanner()->ScanRegExpPattern(seen_equal)) {
|
|
|
|
Next();
|
2014-05-15 09:44:57 +00:00
|
|
|
ReportMessage("unterminated_regexp");
|
2014-02-11 11:51:01 +00:00
|
|
|
*ok = false;
|
|
|
|
return Traits::EmptyExpression();
|
|
|
|
}
|
|
|
|
|
2014-02-13 16:17:55 +00:00
|
|
|
int literal_index = function_state_->NextMaterializedLiteralIndex();
|
2014-02-11 11:51:01 +00:00
|
|
|
|
2014-06-18 07:30:56 +00:00
|
|
|
IdentifierT js_pattern = this->NextLiteralString(scanner(), TENURED);
|
2014-02-11 11:51:01 +00:00
|
|
|
if (!scanner()->ScanRegExpFlags()) {
|
|
|
|
Next();
|
2014-06-03 16:12:48 +00:00
|
|
|
ReportMessage("invalid_regexp_flags");
|
2014-02-11 11:51:01 +00:00
|
|
|
*ok = false;
|
|
|
|
return Traits::EmptyExpression();
|
|
|
|
}
|
2014-06-18 07:30:56 +00:00
|
|
|
IdentifierT js_flags = this->NextLiteralString(scanner(), TENURED);
|
2014-02-11 11:51:01 +00:00
|
|
|
Next();
|
2014-02-13 16:17:55 +00:00
|
|
|
return factory()->NewRegExpLiteral(js_pattern, js_flags, literal_index, pos);
|
2014-02-11 11:51:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-14 11:24:26 +00:00
|
|
|
#define CHECK_OK ok); \
|
|
|
|
if (!*ok) return this->EmptyExpression(); \
|
|
|
|
((void)0
|
|
|
|
#define DUMMY ) // to make indentation work
|
|
|
|
#undef DUMMY
|
|
|
|
|
2014-03-18 17:05:38 +00:00
|
|
|
// Used in functions where the return type is not ExpressionT.
|
2014-03-11 16:30:47 +00:00
|
|
|
#define CHECK_OK_CUSTOM(x) ok); \
|
|
|
|
if (!*ok) return this->x(); \
|
|
|
|
((void)0
|
|
|
|
#define DUMMY ) // to make indentation work
|
|
|
|
#undef DUMMY
|
|
|
|
|
2014-02-14 11:24:26 +00:00
|
|
|
template <class Traits>
|
2014-03-18 17:05:38 +00:00
|
|
|
typename ParserBase<Traits>::ExpressionT
|
|
|
|
ParserBase<Traits>::ParsePrimaryExpression(bool* ok) {
|
2014-02-14 11:24:26 +00:00
|
|
|
// PrimaryExpression ::
|
|
|
|
// 'this'
|
|
|
|
// 'null'
|
|
|
|
// 'true'
|
|
|
|
// 'false'
|
|
|
|
// Identifier
|
|
|
|
// Number
|
|
|
|
// String
|
|
|
|
// ArrayLiteral
|
|
|
|
// ObjectLiteral
|
|
|
|
// RegExpLiteral
|
|
|
|
// '(' Expression ')'
|
|
|
|
|
|
|
|
int pos = peek_position();
|
2014-03-18 17:05:38 +00:00
|
|
|
ExpressionT result = this->EmptyExpression();
|
2014-02-14 11:24:26 +00:00
|
|
|
Token::Value token = peek();
|
|
|
|
switch (token) {
|
|
|
|
case Token::THIS: {
|
|
|
|
Consume(Token::THIS);
|
|
|
|
result = this->ThisExpression(scope_, factory());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Token::NULL_LITERAL:
|
|
|
|
case Token::TRUE_LITERAL:
|
|
|
|
case Token::FALSE_LITERAL:
|
|
|
|
case Token::NUMBER:
|
|
|
|
Next();
|
|
|
|
result = this->ExpressionFromLiteral(token, pos, scanner(), factory());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Token::IDENTIFIER:
|
|
|
|
case Token::YIELD:
|
|
|
|
case Token::FUTURE_STRICT_RESERVED_WORD: {
|
|
|
|
// Using eval or arguments in this context is OK even in strict mode.
|
2014-03-18 17:05:38 +00:00
|
|
|
IdentifierT name = ParseIdentifier(kAllowEvalOrArguments, CHECK_OK);
|
|
|
|
result = this->ExpressionFromIdentifier(name, pos, scope_, factory());
|
2014-02-14 11:24:26 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Token::STRING: {
|
|
|
|
Consume(Token::STRING);
|
|
|
|
result = this->ExpressionFromString(pos, scanner(), factory());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Token::ASSIGN_DIV:
|
|
|
|
result = this->ParseRegExpLiteral(true, CHECK_OK);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Token::DIV:
|
|
|
|
result = this->ParseRegExpLiteral(false, CHECK_OK);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Token::LBRACK:
|
|
|
|
result = this->ParseArrayLiteral(CHECK_OK);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Token::LBRACE:
|
|
|
|
result = this->ParseObjectLiteral(CHECK_OK);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Token::LPAREN:
|
|
|
|
Consume(Token::LPAREN);
|
|
|
|
// Heuristically try to detect immediately called functions before
|
|
|
|
// seeing the call parentheses.
|
|
|
|
parenthesized_function_ = (peek() == Token::FUNCTION);
|
|
|
|
result = this->ParseExpression(true, CHECK_OK);
|
|
|
|
Expect(Token::RPAREN, CHECK_OK);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Token::MOD:
|
|
|
|
if (allow_natives_syntax() || extension_ != NULL) {
|
|
|
|
result = this->ParseV8Intrinsic(CHECK_OK);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// If we're not allowing special syntax we fall-through to the
|
|
|
|
// default case.
|
|
|
|
|
|
|
|
default: {
|
|
|
|
Next();
|
|
|
|
ReportUnexpectedToken(token);
|
|
|
|
*ok = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-02-19 08:56:11 +00:00
|
|
|
// Precedence = 1
|
|
|
|
template <class Traits>
|
2014-03-18 17:05:38 +00:00
|
|
|
typename ParserBase<Traits>::ExpressionT ParserBase<Traits>::ParseExpression(
|
2014-02-19 08:56:11 +00:00
|
|
|
bool accept_IN, bool* ok) {
|
|
|
|
// Expression ::
|
|
|
|
// AssignmentExpression
|
|
|
|
// Expression ',' AssignmentExpression
|
|
|
|
|
2014-03-18 17:05:38 +00:00
|
|
|
ExpressionT result = this->ParseAssignmentExpression(accept_IN, CHECK_OK);
|
2014-02-19 08:56:11 +00:00
|
|
|
while (peek() == Token::COMMA) {
|
|
|
|
Expect(Token::COMMA, CHECK_OK);
|
|
|
|
int pos = position();
|
2014-03-18 17:05:38 +00:00
|
|
|
ExpressionT right = this->ParseAssignmentExpression(accept_IN, CHECK_OK);
|
2014-02-19 08:56:11 +00:00
|
|
|
result = factory()->NewBinaryOperation(Token::COMMA, result, right, pos);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class Traits>
|
2014-03-18 17:05:38 +00:00
|
|
|
typename ParserBase<Traits>::ExpressionT ParserBase<Traits>::ParseArrayLiteral(
|
2014-02-19 08:56:11 +00:00
|
|
|
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) {
|
2014-03-18 17:05:38 +00:00
|
|
|
ExpressionT elem = this->EmptyExpression();
|
2014-02-19 08:56:11 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-03-11 15:40:41 +00:00
|
|
|
|
|
|
|
template <class Traits>
|
2014-03-18 17:05:38 +00:00
|
|
|
typename ParserBase<Traits>::ExpressionT ParserBase<Traits>::ParseObjectLiteral(
|
2014-03-11 15:40:41 +00:00
|
|
|
bool* ok) {
|
|
|
|
// ObjectLiteral ::
|
|
|
|
// '{' ((
|
|
|
|
// ((IdentifierName | String | Number) ':' AssignmentExpression) |
|
|
|
|
// (('get' | 'set') (IdentifierName | String | Number) FunctionLiteral)
|
|
|
|
// ) ',')* '}'
|
|
|
|
// (Except that trailing comma is not required and not allowed.)
|
|
|
|
|
|
|
|
int pos = peek_position();
|
|
|
|
typename Traits::Type::PropertyList properties =
|
|
|
|
this->NewPropertyList(4, zone_);
|
|
|
|
int number_of_boilerplate_properties = 0;
|
|
|
|
bool has_function = false;
|
|
|
|
|
|
|
|
ObjectLiteralChecker checker(this, strict_mode());
|
|
|
|
|
|
|
|
Expect(Token::LBRACE, CHECK_OK);
|
|
|
|
|
|
|
|
while (peek() != Token::RBRACE) {
|
|
|
|
if (fni_ != NULL) fni_->Enter();
|
|
|
|
|
|
|
|
typename Traits::Type::Literal key = this->EmptyLiteral();
|
|
|
|
Token::Value next = peek();
|
|
|
|
int next_pos = peek_position();
|
|
|
|
|
|
|
|
switch (next) {
|
|
|
|
case Token::FUTURE_RESERVED_WORD:
|
|
|
|
case Token::FUTURE_STRICT_RESERVED_WORD:
|
|
|
|
case Token::IDENTIFIER: {
|
|
|
|
bool is_getter = false;
|
|
|
|
bool is_setter = false;
|
2014-03-18 17:05:38 +00:00
|
|
|
IdentifierT id =
|
2014-03-11 15:40:41 +00:00
|
|
|
ParseIdentifierNameOrGetOrSet(&is_getter, &is_setter, CHECK_OK);
|
|
|
|
if (fni_ != NULL) this->PushLiteralName(fni_, id);
|
|
|
|
|
|
|
|
if ((is_getter || is_setter) && peek() != Token::COLON) {
|
|
|
|
// Special handling of getter and setter syntax:
|
|
|
|
// { ... , get foo() { ... }, ... , set foo(v) { ... v ... } , ... }
|
|
|
|
// We have already read the "get" or "set" keyword.
|
|
|
|
Token::Value next = Next();
|
|
|
|
if (next != i::Token::IDENTIFIER &&
|
|
|
|
next != i::Token::FUTURE_RESERVED_WORD &&
|
|
|
|
next != i::Token::FUTURE_STRICT_RESERVED_WORD &&
|
|
|
|
next != i::Token::NUMBER &&
|
|
|
|
next != i::Token::STRING &&
|
|
|
|
!Token::IsKeyword(next)) {
|
|
|
|
ReportUnexpectedToken(next);
|
|
|
|
*ok = false;
|
|
|
|
return this->EmptyLiteral();
|
|
|
|
}
|
|
|
|
// Validate the property.
|
|
|
|
PropertyKind type = is_getter ? kGetterProperty : kSetterProperty;
|
|
|
|
checker.CheckProperty(next, type, CHECK_OK);
|
2014-03-18 17:05:38 +00:00
|
|
|
IdentifierT name = this->GetSymbol(scanner_);
|
2014-03-11 15:40:41 +00:00
|
|
|
typename Traits::Type::FunctionLiteral value =
|
|
|
|
this->ParseFunctionLiteral(
|
|
|
|
name, scanner()->location(),
|
|
|
|
false, // reserved words are allowed here
|
|
|
|
false, // not a generator
|
|
|
|
RelocInfo::kNoPosition, FunctionLiteral::ANONYMOUS_EXPRESSION,
|
2014-06-17 07:23:26 +00:00
|
|
|
is_getter ? FunctionLiteral::GETTER_ARITY
|
|
|
|
: FunctionLiteral::SETTER_ARITY,
|
2014-03-11 15:40:41 +00:00
|
|
|
CHECK_OK);
|
|
|
|
typename Traits::Type::ObjectLiteralProperty property =
|
|
|
|
factory()->NewObjectLiteralProperty(is_getter, value, next_pos);
|
|
|
|
if (this->IsBoilerplateProperty(property)) {
|
|
|
|
number_of_boilerplate_properties++;
|
|
|
|
}
|
|
|
|
properties->Add(property, zone());
|
2014-03-11 16:30:47 +00:00
|
|
|
if (peek() != Token::RBRACE) {
|
|
|
|
// Need {} because of the CHECK_OK macro.
|
|
|
|
Expect(Token::COMMA, CHECK_OK);
|
|
|
|
}
|
2014-03-11 15:40:41 +00:00
|
|
|
|
|
|
|
if (fni_ != NULL) {
|
|
|
|
fni_->Infer();
|
|
|
|
fni_->Leave();
|
|
|
|
}
|
|
|
|
continue; // restart the while
|
|
|
|
}
|
|
|
|
// Failed to parse as get/set property, so it's just a normal property
|
|
|
|
// (which might be called "get" or "set" or something else).
|
2014-06-18 07:30:56 +00:00
|
|
|
key = factory()->NewLiteral(id, next_pos);
|
2014-03-11 15:40:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Token::STRING: {
|
|
|
|
Consume(Token::STRING);
|
2014-03-18 17:05:38 +00:00
|
|
|
IdentifierT string = this->GetSymbol(scanner_);
|
2014-03-11 15:40:41 +00:00
|
|
|
if (fni_ != NULL) this->PushLiteralName(fni_, string);
|
|
|
|
uint32_t index;
|
|
|
|
if (this->IsArrayIndex(string, &index)) {
|
|
|
|
key = factory()->NewNumberLiteral(index, next_pos);
|
|
|
|
break;
|
|
|
|
}
|
2014-06-18 07:30:56 +00:00
|
|
|
key = factory()->NewLiteral(string, next_pos);
|
2014-03-11 15:40:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Token::NUMBER: {
|
|
|
|
Consume(Token::NUMBER);
|
|
|
|
key = this->ExpressionFromLiteral(Token::NUMBER, next_pos, scanner_,
|
|
|
|
factory());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if (Token::IsKeyword(next)) {
|
|
|
|
Consume(next);
|
2014-03-18 17:05:38 +00:00
|
|
|
IdentifierT string = this->GetSymbol(scanner_);
|
2014-06-18 07:30:56 +00:00
|
|
|
key = factory()->NewLiteral(string, next_pos);
|
2014-03-11 15:40:41 +00:00
|
|
|
} else {
|
|
|
|
Token::Value next = Next();
|
|
|
|
ReportUnexpectedToken(next);
|
|
|
|
*ok = false;
|
|
|
|
return this->EmptyLiteral();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the property
|
|
|
|
checker.CheckProperty(next, kValueProperty, CHECK_OK);
|
|
|
|
|
|
|
|
Expect(Token::COLON, CHECK_OK);
|
2014-03-18 17:05:38 +00:00
|
|
|
ExpressionT value = this->ParseAssignmentExpression(true, CHECK_OK);
|
2014-03-11 15:40:41 +00:00
|
|
|
|
|
|
|
typename Traits::Type::ObjectLiteralProperty property =
|
|
|
|
factory()->NewObjectLiteralProperty(key, value);
|
|
|
|
|
|
|
|
// Mark top-level object literals that contain function literals and
|
|
|
|
// pretenure the literal so it can be added as a constant function
|
|
|
|
// property. (Parser only.)
|
|
|
|
this->CheckFunctionLiteralInsideTopLevelObjectLiteral(scope_, value,
|
|
|
|
&has_function);
|
|
|
|
|
|
|
|
// Count CONSTANT or COMPUTED properties to maintain the enumeration order.
|
|
|
|
if (this->IsBoilerplateProperty(property)) {
|
|
|
|
number_of_boilerplate_properties++;
|
|
|
|
}
|
|
|
|
properties->Add(property, zone());
|
|
|
|
|
|
|
|
// TODO(1240767): Consider allowing trailing comma.
|
2014-03-11 16:30:47 +00:00
|
|
|
if (peek() != Token::RBRACE) {
|
|
|
|
// Need {} because of the CHECK_OK macro.
|
|
|
|
Expect(Token::COMMA, CHECK_OK);
|
|
|
|
}
|
2014-03-11 15:40:41 +00:00
|
|
|
|
|
|
|
if (fni_ != NULL) {
|
|
|
|
fni_->Infer();
|
|
|
|
fni_->Leave();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Expect(Token::RBRACE, CHECK_OK);
|
|
|
|
|
|
|
|
// Computation of literal_index must happen before pre parse bailout.
|
|
|
|
int literal_index = function_state_->NextMaterializedLiteralIndex();
|
|
|
|
|
|
|
|
return factory()->NewObjectLiteral(properties,
|
|
|
|
literal_index,
|
|
|
|
number_of_boilerplate_properties,
|
|
|
|
has_function,
|
|
|
|
pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-11 16:30:47 +00:00
|
|
|
template <class Traits>
|
|
|
|
typename Traits::Type::ExpressionList ParserBase<Traits>::ParseArguments(
|
|
|
|
bool* ok) {
|
|
|
|
// Arguments ::
|
|
|
|
// '(' (AssignmentExpression)*[','] ')'
|
|
|
|
|
|
|
|
typename Traits::Type::ExpressionList result =
|
|
|
|
this->NewExpressionList(4, zone_);
|
|
|
|
Expect(Token::LPAREN, CHECK_OK_CUSTOM(NullExpressionList));
|
|
|
|
bool done = (peek() == Token::RPAREN);
|
|
|
|
while (!done) {
|
2014-03-18 17:05:38 +00:00
|
|
|
ExpressionT argument = this->ParseAssignmentExpression(
|
|
|
|
true, CHECK_OK_CUSTOM(NullExpressionList));
|
2014-03-11 16:30:47 +00:00
|
|
|
result->Add(argument, zone_);
|
|
|
|
if (result->length() > Code::kMaxArguments) {
|
2014-06-03 16:12:48 +00:00
|
|
|
ReportMessage("too_many_arguments");
|
2014-03-11 16:30:47 +00:00
|
|
|
*ok = false;
|
|
|
|
return this->NullExpressionList();
|
|
|
|
}
|
|
|
|
done = (peek() == Token::RPAREN);
|
|
|
|
if (!done) {
|
|
|
|
// Need {} because of the CHECK_OK_CUSTOM macro.
|
|
|
|
Expect(Token::COMMA, CHECK_OK_CUSTOM(NullExpressionList));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Expect(Token::RPAREN, CHECK_OK_CUSTOM(NullExpressionList));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-03-14 09:43:04 +00:00
|
|
|
// Precedence = 2
|
|
|
|
template <class Traits>
|
2014-03-18 17:05:38 +00:00
|
|
|
typename ParserBase<Traits>::ExpressionT
|
|
|
|
ParserBase<Traits>::ParseAssignmentExpression(bool accept_IN, bool* ok) {
|
2014-03-14 09:43:04 +00:00
|
|
|
// AssignmentExpression ::
|
|
|
|
// ConditionalExpression
|
|
|
|
// YieldExpression
|
|
|
|
// LeftHandSideExpression AssignmentOperator AssignmentExpression
|
|
|
|
|
2014-03-17 10:21:01 +00:00
|
|
|
Scanner::Location lhs_location = scanner()->peek_location();
|
|
|
|
|
2014-03-14 09:43:04 +00:00
|
|
|
if (peek() == Token::YIELD && is_generator()) {
|
|
|
|
return this->ParseYieldExpression(ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fni_ != NULL) fni_->Enter();
|
2014-03-18 17:05:38 +00:00
|
|
|
ExpressionT expression =
|
2014-03-14 09:43:04 +00:00
|
|
|
this->ParseConditionalExpression(accept_IN, CHECK_OK);
|
|
|
|
|
|
|
|
if (!Token::IsAssignmentOp(peek())) {
|
|
|
|
if (fni_ != NULL) fni_->Leave();
|
|
|
|
// Parsed conditional expression only (no assignment).
|
|
|
|
return expression;
|
|
|
|
}
|
|
|
|
|
2014-04-02 11:03:05 +00:00
|
|
|
expression = this->CheckAndRewriteReferenceExpression(
|
|
|
|
expression, lhs_location, "invalid_lhs_in_assignment", CHECK_OK);
|
2014-03-14 09:43:04 +00:00
|
|
|
expression = this->MarkExpressionAsLValue(expression);
|
|
|
|
|
|
|
|
Token::Value op = Next(); // Get assignment operator.
|
|
|
|
int pos = position();
|
2014-03-18 17:05:38 +00:00
|
|
|
ExpressionT right = this->ParseAssignmentExpression(accept_IN, CHECK_OK);
|
2014-03-14 09:43:04 +00:00
|
|
|
|
|
|
|
// TODO(1231235): We try to estimate the set of properties set by
|
|
|
|
// constructors. We define a new property whenever there is an
|
|
|
|
// assignment to a property of 'this'. We should probably only add
|
|
|
|
// properties if we haven't seen them before. Otherwise we'll
|
|
|
|
// probably overestimate the number of properties.
|
|
|
|
if (op == Token::ASSIGN && this->IsThisProperty(expression)) {
|
|
|
|
function_state_->AddProperty();
|
|
|
|
}
|
|
|
|
|
|
|
|
this->CheckAssigningFunctionLiteralToProperty(expression, right);
|
|
|
|
|
|
|
|
if (fni_ != NULL) {
|
|
|
|
// Check if the right hand side is a call to avoid inferring a
|
|
|
|
// name if we're dealing with "a = function(){...}();"-like
|
|
|
|
// expression.
|
|
|
|
if ((op == Token::INIT_VAR
|
|
|
|
|| op == Token::INIT_CONST_LEGACY
|
|
|
|
|| op == Token::ASSIGN)
|
2014-03-21 09:51:33 +00:00
|
|
|
&& (!right->IsCall() && !right->IsCallNew())) {
|
2014-03-14 09:43:04 +00:00
|
|
|
fni_->Infer();
|
|
|
|
} else {
|
|
|
|
fni_->RemoveLastFunction();
|
|
|
|
}
|
|
|
|
fni_->Leave();
|
|
|
|
}
|
|
|
|
|
|
|
|
return factory()->NewAssignment(op, expression, right, pos);
|
|
|
|
}
|
2014-03-11 15:40:41 +00:00
|
|
|
|
2014-03-14 09:51:22 +00:00
|
|
|
template <class Traits>
|
2014-03-18 17:05:38 +00:00
|
|
|
typename ParserBase<Traits>::ExpressionT
|
|
|
|
ParserBase<Traits>::ParseYieldExpression(bool* ok) {
|
2014-03-14 09:51:22 +00:00
|
|
|
// YieldExpression ::
|
|
|
|
// 'yield' '*'? AssignmentExpression
|
|
|
|
int pos = peek_position();
|
|
|
|
Expect(Token::YIELD, CHECK_OK);
|
|
|
|
Yield::Kind kind =
|
|
|
|
Check(Token::MUL) ? Yield::DELEGATING : Yield::SUSPEND;
|
2014-03-18 17:05:38 +00:00
|
|
|
ExpressionT generator_object =
|
2014-03-14 09:51:22 +00:00
|
|
|
factory()->NewVariableProxy(function_state_->generator_object_variable());
|
2014-03-18 17:05:38 +00:00
|
|
|
ExpressionT expression =
|
2014-03-14 09:51:22 +00:00
|
|
|
ParseAssignmentExpression(false, CHECK_OK);
|
|
|
|
typename Traits::Type::YieldExpression yield =
|
|
|
|
factory()->NewYield(generator_object, expression, kind, pos);
|
|
|
|
if (kind == Yield::DELEGATING) {
|
|
|
|
yield->set_index(function_state_->NextHandlerIndex());
|
|
|
|
}
|
|
|
|
return yield;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-17 13:36:39 +00:00
|
|
|
// Precedence = 3
|
|
|
|
template <class Traits>
|
2014-03-18 17:05:38 +00:00
|
|
|
typename ParserBase<Traits>::ExpressionT
|
2014-03-17 13:36:39 +00:00
|
|
|
ParserBase<Traits>::ParseConditionalExpression(bool accept_IN, bool* ok) {
|
|
|
|
// ConditionalExpression ::
|
|
|
|
// LogicalOrExpression
|
|
|
|
// LogicalOrExpression '?' AssignmentExpression ':' AssignmentExpression
|
|
|
|
|
|
|
|
int pos = peek_position();
|
|
|
|
// We start using the binary expression parser for prec >= 4 only!
|
2014-03-18 17:05:38 +00:00
|
|
|
ExpressionT expression = this->ParseBinaryExpression(4, accept_IN, CHECK_OK);
|
2014-03-17 13:36:39 +00:00
|
|
|
if (peek() != Token::CONDITIONAL) return expression;
|
|
|
|
Consume(Token::CONDITIONAL);
|
|
|
|
// In parsing the first assignment expression in conditional
|
|
|
|
// expressions we always accept the 'in' keyword; see ECMA-262,
|
|
|
|
// section 11.12, page 58.
|
2014-03-18 17:05:38 +00:00
|
|
|
ExpressionT left = ParseAssignmentExpression(true, CHECK_OK);
|
2014-03-17 13:36:39 +00:00
|
|
|
Expect(Token::COLON, CHECK_OK);
|
2014-03-18 17:05:38 +00:00
|
|
|
ExpressionT right = ParseAssignmentExpression(accept_IN, CHECK_OK);
|
2014-03-17 13:36:39 +00:00
|
|
|
return factory()->NewConditional(expression, left, right, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-17 13:54:42 +00:00
|
|
|
// Precedence >= 4
|
|
|
|
template <class Traits>
|
2014-03-18 17:05:38 +00:00
|
|
|
typename ParserBase<Traits>::ExpressionT
|
2014-03-17 13:54:42 +00:00
|
|
|
ParserBase<Traits>::ParseBinaryExpression(int prec, bool accept_IN, bool* ok) {
|
|
|
|
ASSERT(prec >= 4);
|
2014-03-18 17:05:38 +00:00
|
|
|
ExpressionT x = this->ParseUnaryExpression(CHECK_OK);
|
2014-03-17 13:54:42 +00:00
|
|
|
for (int prec1 = Precedence(peek(), accept_IN); prec1 >= prec; prec1--) {
|
|
|
|
// prec1 >= 4
|
|
|
|
while (Precedence(peek(), accept_IN) == prec1) {
|
|
|
|
Token::Value op = Next();
|
|
|
|
int pos = position();
|
2014-03-18 17:05:38 +00:00
|
|
|
ExpressionT y = ParseBinaryExpression(prec1 + 1, accept_IN, CHECK_OK);
|
2014-03-17 13:54:42 +00:00
|
|
|
|
|
|
|
if (this->ShortcutNumericLiteralBinaryExpression(&x, y, op, pos,
|
|
|
|
factory())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For now we distinguish between comparisons and other binary
|
|
|
|
// operations. (We could combine the two and get rid of this
|
|
|
|
// code and AST node eventually.)
|
|
|
|
if (Token::IsCompareOp(op)) {
|
|
|
|
// We have a comparison.
|
|
|
|
Token::Value cmp = op;
|
|
|
|
switch (op) {
|
|
|
|
case Token::NE: cmp = Token::EQ; break;
|
|
|
|
case Token::NE_STRICT: cmp = Token::EQ_STRICT; break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
x = factory()->NewCompareOperation(cmp, x, y, pos);
|
|
|
|
if (cmp != op) {
|
|
|
|
// The comparison was negated - add a NOT.
|
|
|
|
x = factory()->NewUnaryOperation(Token::NOT, x, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// We have a "normal" binary operation.
|
|
|
|
x = factory()->NewBinaryOperation(op, x, y, pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-19 14:08:47 +00:00
|
|
|
template <class Traits>
|
|
|
|
typename ParserBase<Traits>::ExpressionT
|
|
|
|
ParserBase<Traits>::ParseUnaryExpression(bool* ok) {
|
|
|
|
// UnaryExpression ::
|
|
|
|
// PostfixExpression
|
|
|
|
// 'delete' UnaryExpression
|
|
|
|
// 'void' UnaryExpression
|
|
|
|
// 'typeof' UnaryExpression
|
|
|
|
// '++' UnaryExpression
|
|
|
|
// '--' UnaryExpression
|
|
|
|
// '+' UnaryExpression
|
|
|
|
// '-' UnaryExpression
|
|
|
|
// '~' UnaryExpression
|
|
|
|
// '!' UnaryExpression
|
|
|
|
|
|
|
|
Token::Value op = peek();
|
|
|
|
if (Token::IsUnaryOp(op)) {
|
|
|
|
op = Next();
|
|
|
|
int pos = position();
|
|
|
|
ExpressionT expression = ParseUnaryExpression(CHECK_OK);
|
|
|
|
|
|
|
|
// "delete identifier" is a syntax error in strict mode.
|
|
|
|
if (op == Token::DELETE && strict_mode() == STRICT &&
|
|
|
|
this->IsIdentifier(expression)) {
|
2014-05-15 09:44:57 +00:00
|
|
|
ReportMessage("strict_delete");
|
2014-03-19 14:08:47 +00:00
|
|
|
*ok = false;
|
|
|
|
return this->EmptyExpression();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow Traits do rewrite the expression.
|
|
|
|
return this->BuildUnaryExpression(expression, op, pos, factory());
|
|
|
|
} else if (Token::IsCountOp(op)) {
|
|
|
|
op = Next();
|
|
|
|
Scanner::Location lhs_location = scanner()->peek_location();
|
2014-04-02 11:03:05 +00:00
|
|
|
ExpressionT expression = this->ParseUnaryExpression(CHECK_OK);
|
|
|
|
expression = this->CheckAndRewriteReferenceExpression(
|
|
|
|
expression, lhs_location, "invalid_lhs_in_prefix_op", CHECK_OK);
|
2014-03-19 14:08:47 +00:00
|
|
|
this->MarkExpressionAsLValue(expression);
|
|
|
|
|
|
|
|
return factory()->NewCountOperation(op,
|
|
|
|
true /* prefix */,
|
|
|
|
expression,
|
|
|
|
position());
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return this->ParsePostfixExpression(ok);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-21 09:46:18 +00:00
|
|
|
template <class Traits>
|
|
|
|
typename ParserBase<Traits>::ExpressionT
|
|
|
|
ParserBase<Traits>::ParsePostfixExpression(bool* ok) {
|
|
|
|
// PostfixExpression ::
|
|
|
|
// LeftHandSideExpression ('++' | '--')?
|
|
|
|
|
|
|
|
Scanner::Location lhs_location = scanner()->peek_location();
|
|
|
|
ExpressionT expression = this->ParseLeftHandSideExpression(CHECK_OK);
|
|
|
|
if (!scanner()->HasAnyLineTerminatorBeforeNext() &&
|
|
|
|
Token::IsCountOp(peek())) {
|
2014-04-02 11:03:05 +00:00
|
|
|
expression = this->CheckAndRewriteReferenceExpression(
|
|
|
|
expression, lhs_location, "invalid_lhs_in_postfix_op", CHECK_OK);
|
2014-03-21 09:46:18 +00:00
|
|
|
expression = this->MarkExpressionAsLValue(expression);
|
|
|
|
|
|
|
|
Token::Value next = Next();
|
|
|
|
expression =
|
|
|
|
factory()->NewCountOperation(next,
|
|
|
|
false /* postfix */,
|
|
|
|
expression,
|
|
|
|
position());
|
|
|
|
}
|
|
|
|
return expression;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-21 09:51:33 +00:00
|
|
|
template <class Traits>
|
|
|
|
typename ParserBase<Traits>::ExpressionT
|
|
|
|
ParserBase<Traits>::ParseLeftHandSideExpression(bool* ok) {
|
|
|
|
// LeftHandSideExpression ::
|
|
|
|
// (NewExpression | MemberExpression) ...
|
|
|
|
|
|
|
|
ExpressionT result = this->ParseMemberWithNewPrefixesExpression(CHECK_OK);
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
switch (peek()) {
|
|
|
|
case Token::LBRACK: {
|
|
|
|
Consume(Token::LBRACK);
|
|
|
|
int pos = position();
|
|
|
|
ExpressionT index = ParseExpression(true, CHECK_OK);
|
|
|
|
result = factory()->NewProperty(result, index, pos);
|
|
|
|
Expect(Token::RBRACK, CHECK_OK);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Token::LPAREN: {
|
|
|
|
int pos;
|
|
|
|
if (scanner()->current_token() == Token::IDENTIFIER) {
|
|
|
|
// For call of an identifier we want to report position of
|
|
|
|
// the identifier as position of the call in the stack trace.
|
|
|
|
pos = position();
|
|
|
|
} else {
|
|
|
|
// For other kinds of calls we record position of the parenthesis as
|
|
|
|
// position of the call. Note that this is extremely important for
|
|
|
|
// expressions of the form function(){...}() for which call position
|
|
|
|
// should not point to the closing brace otherwise it will intersect
|
|
|
|
// with positions recorded for function literal and confuse debugger.
|
|
|
|
pos = peek_position();
|
|
|
|
// Also the trailing parenthesis are a hint that the function will
|
|
|
|
// be called immediately. If we happen to have parsed a preceding
|
|
|
|
// function literal eagerly, we can also compile it eagerly.
|
|
|
|
if (result->IsFunctionLiteral() && mode() == PARSE_EAGERLY) {
|
|
|
|
result->AsFunctionLiteral()->set_parenthesized();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
typename Traits::Type::ExpressionList args = ParseArguments(CHECK_OK);
|
|
|
|
|
|
|
|
// Keep track of eval() calls since they disable all local variable
|
|
|
|
// optimizations.
|
|
|
|
// The calls that need special treatment are the
|
|
|
|
// direct eval calls. These calls are all of the form eval(...), with
|
|
|
|
// no explicit receiver.
|
|
|
|
// These calls are marked as potentially direct eval calls. Whether
|
|
|
|
// they are actually direct calls to eval is determined at run time.
|
|
|
|
this->CheckPossibleEvalCall(result, scope_);
|
|
|
|
result = factory()->NewCall(result, args, pos);
|
|
|
|
if (fni_ != NULL) fni_->RemoveLastFunction();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Token::PERIOD: {
|
|
|
|
Consume(Token::PERIOD);
|
|
|
|
int pos = position();
|
|
|
|
IdentifierT name = ParseIdentifierName(CHECK_OK);
|
|
|
|
result = factory()->NewProperty(
|
2014-06-18 07:30:56 +00:00
|
|
|
result, factory()->NewLiteral(name, pos), pos);
|
2014-03-21 09:51:33 +00:00
|
|
|
if (fni_ != NULL) this->PushLiteralName(fni_, name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-21 10:34:51 +00:00
|
|
|
template <class Traits>
|
|
|
|
typename ParserBase<Traits>::ExpressionT
|
|
|
|
ParserBase<Traits>::ParseMemberWithNewPrefixesExpression(bool* ok) {
|
|
|
|
// NewExpression ::
|
|
|
|
// ('new')+ MemberExpression
|
|
|
|
|
|
|
|
// The grammar for new expressions is pretty warped. We can have several 'new'
|
|
|
|
// keywords following each other, and then a MemberExpression. When we see '('
|
|
|
|
// after the MemberExpression, it's associated with the rightmost unassociated
|
|
|
|
// 'new' to create a NewExpression with arguments. However, a NewExpression
|
|
|
|
// can also occur without arguments.
|
|
|
|
|
|
|
|
// Examples of new expression:
|
|
|
|
// new foo.bar().baz means (new (foo.bar)()).baz
|
|
|
|
// new foo()() means (new foo())()
|
|
|
|
// new new foo()() means (new (new foo())())
|
|
|
|
// new new foo means new (new foo)
|
|
|
|
// new new foo() means new (new foo())
|
|
|
|
// new new foo().bar().baz means (new (new foo()).bar()).baz
|
|
|
|
|
|
|
|
if (peek() == Token::NEW) {
|
|
|
|
Consume(Token::NEW);
|
|
|
|
int new_pos = position();
|
|
|
|
ExpressionT result = this->ParseMemberWithNewPrefixesExpression(CHECK_OK);
|
|
|
|
if (peek() == Token::LPAREN) {
|
|
|
|
// NewExpression with arguments.
|
|
|
|
typename Traits::Type::ExpressionList args =
|
|
|
|
this->ParseArguments(CHECK_OK);
|
|
|
|
result = factory()->NewCallNew(result, args, new_pos);
|
|
|
|
// The expression can still continue with . or [ after the arguments.
|
|
|
|
result = this->ParseMemberExpressionContinuation(result, CHECK_OK);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
// NewExpression without arguments.
|
|
|
|
return factory()->NewCallNew(result, this->NewExpressionList(0, zone_),
|
|
|
|
new_pos);
|
|
|
|
}
|
|
|
|
// No 'new' keyword.
|
|
|
|
return this->ParseMemberExpression(ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class Traits>
|
|
|
|
typename ParserBase<Traits>::ExpressionT
|
|
|
|
ParserBase<Traits>::ParseMemberExpression(bool* ok) {
|
|
|
|
// MemberExpression ::
|
|
|
|
// (PrimaryExpression | FunctionLiteral)
|
|
|
|
// ('[' Expression ']' | '.' Identifier | Arguments)*
|
|
|
|
|
|
|
|
// The '[' Expression ']' and '.' Identifier parts are parsed by
|
|
|
|
// ParseMemberExpressionContinuation, and the Arguments part is parsed by the
|
|
|
|
// caller.
|
|
|
|
|
|
|
|
// Parse the initial primary or function expression.
|
|
|
|
ExpressionT result = this->EmptyExpression();
|
|
|
|
if (peek() == Token::FUNCTION) {
|
|
|
|
Consume(Token::FUNCTION);
|
|
|
|
int function_token_position = position();
|
|
|
|
bool is_generator = allow_generators() && Check(Token::MUL);
|
2014-04-17 09:23:04 +00:00
|
|
|
IdentifierT name = this->EmptyIdentifier();
|
2014-03-21 10:34:51 +00:00
|
|
|
bool is_strict_reserved_name = false;
|
|
|
|
Scanner::Location function_name_location = Scanner::Location::invalid();
|
|
|
|
FunctionLiteral::FunctionType function_type =
|
|
|
|
FunctionLiteral::ANONYMOUS_EXPRESSION;
|
|
|
|
if (peek_any_identifier()) {
|
|
|
|
name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved_name,
|
|
|
|
CHECK_OK);
|
|
|
|
function_name_location = scanner()->location();
|
|
|
|
function_type = FunctionLiteral::NAMED_EXPRESSION;
|
|
|
|
}
|
|
|
|
result = this->ParseFunctionLiteral(name,
|
|
|
|
function_name_location,
|
|
|
|
is_strict_reserved_name,
|
|
|
|
is_generator,
|
|
|
|
function_token_position,
|
|
|
|
function_type,
|
2014-06-17 07:23:26 +00:00
|
|
|
FunctionLiteral::NORMAL_ARITY,
|
2014-03-21 10:34:51 +00:00
|
|
|
CHECK_OK);
|
|
|
|
} else {
|
|
|
|
result = ParsePrimaryExpression(CHECK_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
result = ParseMemberExpressionContinuation(result, CHECK_OK);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class Traits>
|
|
|
|
typename ParserBase<Traits>::ExpressionT
|
|
|
|
ParserBase<Traits>::ParseMemberExpressionContinuation(ExpressionT expression,
|
|
|
|
bool* ok) {
|
|
|
|
// Parses this part of MemberExpression:
|
|
|
|
// ('[' Expression ']' | '.' Identifier)*
|
|
|
|
while (true) {
|
|
|
|
switch (peek()) {
|
|
|
|
case Token::LBRACK: {
|
|
|
|
Consume(Token::LBRACK);
|
|
|
|
int pos = position();
|
|
|
|
ExpressionT index = this->ParseExpression(true, CHECK_OK);
|
|
|
|
expression = factory()->NewProperty(expression, index, pos);
|
|
|
|
if (fni_ != NULL) {
|
|
|
|
this->PushPropertyName(fni_, index);
|
|
|
|
}
|
|
|
|
Expect(Token::RBRACK, CHECK_OK);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Token::PERIOD: {
|
|
|
|
Consume(Token::PERIOD);
|
|
|
|
int pos = position();
|
|
|
|
IdentifierT name = ParseIdentifierName(CHECK_OK);
|
|
|
|
expression = factory()->NewProperty(
|
2014-06-18 07:30:56 +00:00
|
|
|
expression, factory()->NewLiteral(name, pos), pos);
|
2014-03-21 10:34:51 +00:00
|
|
|
if (fni_ != NULL) {
|
|
|
|
this->PushLiteralName(fni_, name);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return expression;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ASSERT(false);
|
|
|
|
return this->EmptyExpression();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-02 11:03:05 +00:00
|
|
|
template <typename Traits>
|
|
|
|
typename ParserBase<Traits>::ExpressionT
|
|
|
|
ParserBase<Traits>::CheckAndRewriteReferenceExpression(
|
|
|
|
ExpressionT expression,
|
|
|
|
Scanner::Location location, const char* message, bool* ok) {
|
|
|
|
if (strict_mode() == STRICT && this->IsIdentifier(expression) &&
|
|
|
|
this->IsEvalOrArguments(this->AsIdentifier(expression))) {
|
|
|
|
this->ReportMessageAt(location, "strict_eval_arguments", false);
|
|
|
|
*ok = false;
|
|
|
|
return this->EmptyExpression();
|
|
|
|
} else if (expression->IsValidReferenceExpression()) {
|
|
|
|
return expression;
|
|
|
|
} else if (expression->IsCall()) {
|
|
|
|
// If it is a call, make it a runtime error for legacy web compatibility.
|
|
|
|
// Rewrite `expr' to `expr[throw ReferenceError]'.
|
|
|
|
int pos = location.beg_pos;
|
|
|
|
ExpressionT error = this->NewThrowReferenceError(message, pos);
|
|
|
|
return factory()->NewProperty(expression, error, pos);
|
|
|
|
} else {
|
|
|
|
this->ReportMessageAt(location, message, true);
|
|
|
|
*ok = false;
|
|
|
|
return this->EmptyExpression();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-14 11:24:26 +00:00
|
|
|
#undef CHECK_OK
|
2014-03-11 16:30:47 +00:00
|
|
|
#undef CHECK_OK_CUSTOM
|
2014-02-14 11:24:26 +00:00
|
|
|
|
|
|
|
|
2014-02-11 09:35:32 +00:00
|
|
|
template <typename Traits>
|
|
|
|
void ParserBase<Traits>::ObjectLiteralChecker::CheckProperty(
|
|
|
|
Token::Value property,
|
|
|
|
PropertyKind type,
|
|
|
|
bool* ok) {
|
|
|
|
int old;
|
|
|
|
if (property == Token::NUMBER) {
|
2014-03-13 08:29:31 +00:00
|
|
|
old = scanner()->FindNumber(&finder_, type);
|
2014-02-11 09:35:32 +00:00
|
|
|
} else {
|
2014-03-13 08:29:31 +00:00
|
|
|
old = scanner()->FindSymbol(&finder_, type);
|
2014-02-11 09:35:32 +00:00
|
|
|
}
|
|
|
|
PropertyKind old_type = static_cast<PropertyKind>(old);
|
|
|
|
if (HasConflict(old_type, type)) {
|
|
|
|
if (IsDataDataConflict(old_type, type)) {
|
|
|
|
// Both are data properties.
|
2014-03-11 14:41:22 +00:00
|
|
|
if (strict_mode_ == SLOPPY) return;
|
2014-06-03 16:12:48 +00:00
|
|
|
parser()->ReportMessage("strict_duplicate_property");
|
2014-02-11 09:35:32 +00:00
|
|
|
} else if (IsDataAccessorConflict(old_type, type)) {
|
|
|
|
// Both a data and an accessor property with the same name.
|
2014-06-03 16:12:48 +00:00
|
|
|
parser()->ReportMessage("accessor_data_property");
|
2014-02-11 09:35:32 +00:00
|
|
|
} else {
|
|
|
|
ASSERT(IsAccessorAccessorConflict(old_type, type));
|
|
|
|
// Both accessors of the same type.
|
2014-06-03 16:12:48 +00:00
|
|
|
parser()->ReportMessage("accessor_get_set");
|
2014-02-11 09:35:32 +00:00
|
|
|
}
|
|
|
|
*ok = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-14 13:07:20 +00:00
|
|
|
} } // v8::internal
|
2010-11-02 07:21:37 +00:00
|
|
|
|
|
|
|
#endif // V8_PREPARSER_H
|