2012-01-26 21:47: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.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/api.h"
|
|
|
|
#include "src/ast.h"
|
2015-06-26 21:39:43 +00:00
|
|
|
#include "src/ast-literal-reindexer.h"
|
2014-09-24 07:08:27 +00:00
|
|
|
#include "src/bailout-reason.h"
|
2014-06-30 13:25:46 +00:00
|
|
|
#include "src/base/platform/platform.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/bootstrapper.h"
|
|
|
|
#include "src/char-predicates-inl.h"
|
|
|
|
#include "src/codegen.h"
|
|
|
|
#include "src/compiler.h"
|
|
|
|
#include "src/messages.h"
|
|
|
|
#include "src/parser.h"
|
|
|
|
#include "src/preparser.h"
|
2014-09-25 07:16:15 +00:00
|
|
|
#include "src/runtime/runtime.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/scanner-character-streams.h"
|
|
|
|
#include "src/scopeinfo.h"
|
|
|
|
#include "src/string-stream.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2015-03-09 14:51:13 +00:00
|
|
|
ScriptData::ScriptData(const byte* data, int length)
|
|
|
|
: owns_data_(false), rejected_(false), data_(data), length_(length) {
|
|
|
|
if (!IsAligned(reinterpret_cast<intptr_t>(data), kPointerAlignment)) {
|
|
|
|
byte* copy = NewArray<byte>(length);
|
|
|
|
DCHECK(IsAligned(reinterpret_cast<intptr_t>(copy), kPointerAlignment));
|
|
|
|
CopyBytes(copy, data, length);
|
|
|
|
data_ = copy;
|
|
|
|
AcquireDataOwnership();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-12 11:46:12 +00:00
|
|
|
ParseInfo::ParseInfo(Zone* zone)
|
|
|
|
: zone_(zone),
|
|
|
|
flags_(0),
|
|
|
|
source_stream_(nullptr),
|
|
|
|
source_stream_encoding_(ScriptCompiler::StreamedSource::ONE_BYTE),
|
|
|
|
extension_(nullptr),
|
|
|
|
compile_options_(ScriptCompiler::kNoCompileOptions),
|
|
|
|
script_scope_(nullptr),
|
|
|
|
unicode_cache_(nullptr),
|
|
|
|
stack_limit_(0),
|
|
|
|
hash_seed_(0),
|
|
|
|
cached_data_(nullptr),
|
|
|
|
ast_value_factory_(nullptr),
|
|
|
|
literal_(nullptr),
|
|
|
|
scope_(nullptr) {}
|
|
|
|
|
|
|
|
|
|
|
|
ParseInfo::ParseInfo(Zone* zone, Handle<JSFunction> function)
|
|
|
|
: ParseInfo(zone, Handle<SharedFunctionInfo>(function->shared())) {
|
2015-03-09 14:51:13 +00:00
|
|
|
set_closure(function);
|
|
|
|
set_context(Handle<Context>(function->context()));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-12 11:46:12 +00:00
|
|
|
ParseInfo::ParseInfo(Zone* zone, Handle<SharedFunctionInfo> shared)
|
|
|
|
: ParseInfo(zone) {
|
2015-03-09 14:51:13 +00:00
|
|
|
isolate_ = shared->GetIsolate();
|
|
|
|
|
|
|
|
set_lazy();
|
|
|
|
set_hash_seed(isolate_->heap()->HashSeed());
|
|
|
|
set_stack_limit(isolate_->stack_guard()->real_climit());
|
|
|
|
set_unicode_cache(isolate_->unicode_cache());
|
|
|
|
set_language_mode(shared->language_mode());
|
|
|
|
set_shared_info(shared);
|
|
|
|
|
|
|
|
Handle<Script> script(Script::cast(shared->script()));
|
|
|
|
set_script(script);
|
|
|
|
if (!script.is_null() && script->type()->value() == Script::TYPE_NATIVE) {
|
|
|
|
set_native();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-12 11:46:12 +00:00
|
|
|
ParseInfo::ParseInfo(Zone* zone, Handle<Script> script) : ParseInfo(zone) {
|
2015-03-09 14:51:13 +00:00
|
|
|
isolate_ = script->GetIsolate();
|
|
|
|
|
|
|
|
set_hash_seed(isolate_->heap()->HashSeed());
|
|
|
|
set_stack_limit(isolate_->stack_guard()->real_climit());
|
|
|
|
set_unicode_cache(isolate_->unicode_cache());
|
|
|
|
set_script(script);
|
|
|
|
|
|
|
|
if (script->type()->value() == Script::TYPE_NATIVE) {
|
|
|
|
set_native();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-04 14:42:58 +00:00
|
|
|
RegExpBuilder::RegExpBuilder(Zone* zone)
|
|
|
|
: zone_(zone),
|
2011-04-04 06:29:02 +00:00
|
|
|
pending_empty_(false),
|
|
|
|
characters_(NULL),
|
|
|
|
terms_(),
|
|
|
|
alternatives_()
|
2008-11-25 11:07:48 +00:00
|
|
|
#ifdef DEBUG
|
2011-04-04 06:29:02 +00:00
|
|
|
, last_added_(ADD_NONE)
|
2008-11-25 11:07:48 +00:00
|
|
|
#endif
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
void RegExpBuilder::FlushCharacters() {
|
|
|
|
pending_empty_ = false;
|
|
|
|
if (characters_ != NULL) {
|
2011-04-04 06:29:02 +00:00
|
|
|
RegExpTree* atom = new(zone()) RegExpAtom(characters_->ToConstVector());
|
2008-11-25 11:07:48 +00:00
|
|
|
characters_ = NULL;
|
2012-06-11 12:42:31 +00:00
|
|
|
text_.Add(atom, zone());
|
2008-11-25 11:07:48 +00:00
|
|
|
LAST(ADD_ATOM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RegExpBuilder::FlushText() {
|
|
|
|
FlushCharacters();
|
|
|
|
int num_text = text_.length();
|
|
|
|
if (num_text == 0) {
|
|
|
|
return;
|
|
|
|
} else if (num_text == 1) {
|
2012-06-11 12:42:31 +00:00
|
|
|
terms_.Add(text_.last(), zone());
|
2008-11-25 11:07:48 +00:00
|
|
|
} else {
|
2012-06-11 12:42:31 +00:00
|
|
|
RegExpText* text = new(zone()) RegExpText(zone());
|
2008-11-25 11:07:48 +00:00
|
|
|
for (int i = 0; i < num_text; i++)
|
2012-06-11 12:42:31 +00:00
|
|
|
text_.Get(i)->AppendToText(text, zone());
|
|
|
|
terms_.Add(text, zone());
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
text_.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RegExpBuilder::AddCharacter(uc16 c) {
|
|
|
|
pending_empty_ = false;
|
|
|
|
if (characters_ == NULL) {
|
2012-06-11 12:42:31 +00:00
|
|
|
characters_ = new(zone()) ZoneList<uc16>(4, zone());
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
2012-06-11 12:42:31 +00:00
|
|
|
characters_->Add(c, zone());
|
2008-11-25 11:07:48 +00:00
|
|
|
LAST(ADD_CHAR);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RegExpBuilder::AddEmpty() {
|
|
|
|
pending_empty_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RegExpBuilder::AddAtom(RegExpTree* term) {
|
|
|
|
if (term->IsEmpty()) {
|
|
|
|
AddEmpty();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (term->IsTextElement()) {
|
|
|
|
FlushCharacters();
|
2012-06-11 12:42:31 +00:00
|
|
|
text_.Add(term, zone());
|
2008-11-25 11:07:48 +00:00
|
|
|
} else {
|
|
|
|
FlushText();
|
2012-06-11 12:42:31 +00:00
|
|
|
terms_.Add(term, zone());
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
LAST(ADD_ATOM);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RegExpBuilder::AddAssertion(RegExpTree* assert) {
|
|
|
|
FlushText();
|
2012-06-11 12:42:31 +00:00
|
|
|
terms_.Add(assert, zone());
|
2008-11-25 11:07:48 +00:00
|
|
|
LAST(ADD_ASSERT);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RegExpBuilder::NewAlternative() {
|
|
|
|
FlushTerms();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RegExpBuilder::FlushTerms() {
|
|
|
|
FlushText();
|
|
|
|
int num_terms = terms_.length();
|
|
|
|
RegExpTree* alternative;
|
|
|
|
if (num_terms == 0) {
|
2014-11-12 09:43:56 +00:00
|
|
|
alternative = new (zone()) RegExpEmpty();
|
2008-11-25 11:07:48 +00:00
|
|
|
} else if (num_terms == 1) {
|
|
|
|
alternative = terms_.last();
|
|
|
|
} else {
|
2012-06-11 12:42:31 +00:00
|
|
|
alternative = new(zone()) RegExpAlternative(terms_.GetList(zone()));
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
2012-06-11 12:42:31 +00:00
|
|
|
alternatives_.Add(alternative, zone());
|
2008-11-25 11:07:48 +00:00
|
|
|
terms_.Clear();
|
|
|
|
LAST(ADD_NONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RegExpTree* RegExpBuilder::ToRegExp() {
|
|
|
|
FlushTerms();
|
|
|
|
int num_alternatives = alternatives_.length();
|
2014-11-12 09:43:56 +00:00
|
|
|
if (num_alternatives == 0) return new (zone()) RegExpEmpty();
|
|
|
|
if (num_alternatives == 1) return alternatives_.last();
|
2012-06-11 12:42:31 +00:00
|
|
|
return new(zone()) RegExpDisjunction(alternatives_.GetList(zone()));
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-06 13:28:22 +00:00
|
|
|
void RegExpBuilder::AddQuantifierToAtom(
|
|
|
|
int min, int max, RegExpQuantifier::QuantifierType quantifier_type) {
|
2008-11-25 11:07:48 +00:00
|
|
|
if (pending_empty_) {
|
|
|
|
pending_empty_ = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
RegExpTree* atom;
|
|
|
|
if (characters_ != NULL) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(last_added_ == ADD_CHAR);
|
2008-11-25 11:07:48 +00:00
|
|
|
// Last atom was character.
|
|
|
|
Vector<const uc16> char_vector = characters_->ToConstVector();
|
|
|
|
int num_chars = char_vector.length();
|
|
|
|
if (num_chars > 1) {
|
|
|
|
Vector<const uc16> prefix = char_vector.SubVector(0, num_chars - 1);
|
2012-06-11 12:42:31 +00:00
|
|
|
text_.Add(new(zone()) RegExpAtom(prefix), zone());
|
2008-11-25 11:07:48 +00:00
|
|
|
char_vector = char_vector.SubVector(num_chars - 1, num_chars);
|
|
|
|
}
|
|
|
|
characters_ = NULL;
|
2011-04-04 06:29:02 +00:00
|
|
|
atom = new(zone()) RegExpAtom(char_vector);
|
2008-11-25 11:07:48 +00:00
|
|
|
FlushText();
|
|
|
|
} else if (text_.length() > 0) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(last_added_ == ADD_ATOM);
|
2008-11-25 11:07:48 +00:00
|
|
|
atom = text_.RemoveLast();
|
|
|
|
FlushText();
|
|
|
|
} else if (terms_.length() > 0) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(last_added_ == ADD_ATOM);
|
2008-11-25 11:07:48 +00:00
|
|
|
atom = terms_.RemoveLast();
|
2008-12-17 10:59:14 +00:00
|
|
|
if (atom->max_match() == 0) {
|
|
|
|
// Guaranteed to only match an empty string.
|
2008-11-25 11:07:48 +00:00
|
|
|
LAST(ADD_TERM);
|
|
|
|
if (min == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2012-06-11 12:42:31 +00:00
|
|
|
terms_.Add(atom, zone());
|
2008-11-25 11:07:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Only call immediately after adding an atom or character!
|
|
|
|
UNREACHABLE();
|
|
|
|
return;
|
|
|
|
}
|
2013-06-06 13:28:22 +00:00
|
|
|
terms_.Add(
|
|
|
|
new(zone()) RegExpQuantifier(min, max, quantifier_type, atom), zone());
|
2008-11-25 11:07:48 +00:00
|
|
|
LAST(ADD_TERM);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-10 10:28:05 +00:00
|
|
|
FunctionEntry ParseData::GetFunctionEntry(int start) {
|
2010-08-27 08:26:29 +00:00
|
|
|
// The current pre-data entry must be a FunctionEntry with the given
|
|
|
|
// start position.
|
2014-07-10 10:28:05 +00:00
|
|
|
if ((function_index_ + FunctionEntry::kSize <= Length()) &&
|
|
|
|
(static_cast<int>(Data()[function_index_]) == start)) {
|
2010-09-07 12:52:16 +00:00
|
|
|
int index = function_index_;
|
|
|
|
function_index_ += FunctionEntry::kSize;
|
2014-07-10 10:28:05 +00:00
|
|
|
Vector<unsigned> subvector(&(Data()[index]), FunctionEntry::kSize);
|
|
|
|
return FunctionEntry(subvector);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
return FunctionEntry();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-10 10:28:05 +00:00
|
|
|
int ParseData::FunctionCount() {
|
|
|
|
int functions_size = FunctionsSize();
|
|
|
|
if (functions_size < 0) return 0;
|
|
|
|
if (functions_size % FunctionEntry::kSize != 0) return 0;
|
|
|
|
return functions_size / FunctionEntry::kSize;
|
2010-09-07 12:52:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-10 10:28:05 +00:00
|
|
|
bool ParseData::IsSane() {
|
2014-11-17 12:16:27 +00:00
|
|
|
if (!IsAligned(script_data_->length(), sizeof(unsigned))) return false;
|
2010-09-07 12:52:16 +00:00
|
|
|
// Check that the header data is valid and doesn't specify
|
|
|
|
// point to positions outside the store.
|
2014-07-10 10:28:05 +00:00
|
|
|
int data_length = Length();
|
|
|
|
if (data_length < PreparseDataConstants::kHeaderSize) return false;
|
|
|
|
if (Magic() != PreparseDataConstants::kMagicNumber) return false;
|
|
|
|
if (Version() != PreparseDataConstants::kCurrentVersion) return false;
|
|
|
|
if (HasError()) return false;
|
2010-09-07 12:52:16 +00:00
|
|
|
// Check that the space allocated for function entries is sane.
|
2014-07-10 10:28:05 +00:00
|
|
|
int functions_size = FunctionsSize();
|
2010-09-07 12:52:16 +00:00
|
|
|
if (functions_size < 0) return false;
|
|
|
|
if (functions_size % FunctionEntry::kSize != 0) return false;
|
2010-09-15 10:54:35 +00:00
|
|
|
// Check that the total size has room for header and function entries.
|
2010-09-07 12:52:16 +00:00
|
|
|
int minimum_size =
|
2010-11-23 11:46:36 +00:00
|
|
|
PreparseDataConstants::kHeaderSize + functions_size;
|
2014-07-10 10:28:05 +00:00
|
|
|
if (data_length < minimum_size) return false;
|
2008-07-03 15:10:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-10 10:28:05 +00:00
|
|
|
void ParseData::Initialize() {
|
|
|
|
// Prepares state for use.
|
|
|
|
int data_length = Length();
|
|
|
|
if (data_length >= PreparseDataConstants::kHeaderSize) {
|
|
|
|
function_index_ = PreparseDataConstants::kHeaderSize;
|
2010-08-25 06:46:53 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-10 10:28:05 +00:00
|
|
|
bool ParseData::HasError() {
|
|
|
|
return Data()[PreparseDataConstants::kHasErrorOffset];
|
2014-04-01 14:17:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-10 10:28:05 +00:00
|
|
|
unsigned ParseData::Magic() {
|
|
|
|
return Data()[PreparseDataConstants::kMagicOffset];
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-10 10:28:05 +00:00
|
|
|
unsigned ParseData::Version() {
|
|
|
|
return Data()[PreparseDataConstants::kVersionOffset];
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-10 10:28:05 +00:00
|
|
|
int ParseData::FunctionsSize() {
|
|
|
|
return static_cast<int>(Data()[PreparseDataConstants::kFunctionsSizeOffset]);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-09 14:51:13 +00:00
|
|
|
void Parser::SetCachedData(ParseInfo* info) {
|
2015-02-12 13:02:30 +00:00
|
|
|
if (compile_options_ == ScriptCompiler::kNoCompileOptions) {
|
2014-07-10 10:28:05 +00:00
|
|
|
cached_parse_data_ = NULL;
|
|
|
|
} else {
|
2015-02-12 13:02:30 +00:00
|
|
|
DCHECK(info->cached_data() != NULL);
|
|
|
|
if (compile_options_ == ScriptCompiler::kConsumeParserCache) {
|
|
|
|
cached_parse_data_ = ParseData::FromCachedData(*info->cached_data());
|
2014-07-10 10:28:05 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-07 16:39:00 +00:00
|
|
|
FunctionLiteral* Parser::DefaultConstructor(bool call_super, Scope* scope,
|
|
|
|
int pos, int end_pos) {
|
|
|
|
int materialized_literal_count = -1;
|
|
|
|
int expected_property_count = -1;
|
|
|
|
int parameter_count = 0;
|
|
|
|
const AstRawString* name = ast_value_factory()->empty_string();
|
|
|
|
|
2015-02-11 17:22:50 +00:00
|
|
|
|
2015-02-12 20:06:52 +00:00
|
|
|
FunctionKind kind = call_super ? FunctionKind::kDefaultSubclassConstructor
|
|
|
|
: FunctionKind::kDefaultBaseConstructor;
|
2015-02-11 17:22:50 +00:00
|
|
|
Scope* function_scope = NewScope(scope, FUNCTION_SCOPE, kind);
|
2015-02-04 09:34:05 +00:00
|
|
|
function_scope->SetLanguageMode(
|
2015-02-05 14:11:34 +00:00
|
|
|
static_cast<LanguageMode>(scope->language_mode() | STRICT_BIT));
|
2014-11-07 16:39:00 +00:00
|
|
|
// Set start and end position to the same value
|
|
|
|
function_scope->set_start_position(pos);
|
|
|
|
function_scope->set_end_position(pos);
|
|
|
|
ZoneList<Statement*>* body = NULL;
|
|
|
|
|
|
|
|
{
|
2014-11-14 13:13:09 +00:00
|
|
|
AstNodeFactory function_factory(ast_value_factory());
|
2014-11-07 16:39:00 +00:00
|
|
|
FunctionState function_state(&function_state_, &scope_, function_scope,
|
2015-02-11 17:22:50 +00:00
|
|
|
kind, &function_factory);
|
2014-11-07 16:39:00 +00:00
|
|
|
|
2015-02-11 17:22:50 +00:00
|
|
|
body = new (zone()) ZoneList<Statement*>(call_super ? 2 : 1, zone());
|
|
|
|
AddAssertIsConstruct(body, pos);
|
2014-11-07 16:39:00 +00:00
|
|
|
if (call_super) {
|
2015-06-02 22:04:25 +00:00
|
|
|
// %_DefaultConstructorCallSuper(new.target, .this_function)
|
2014-11-07 16:39:00 +00:00
|
|
|
ZoneList<Expression*>* args =
|
2015-06-02 22:04:25 +00:00
|
|
|
new (zone()) ZoneList<Expression*>(2, zone());
|
|
|
|
VariableProxy* new_target_proxy = scope_->NewUnresolved(
|
|
|
|
factory(), ast_value_factory()->new_target_string(), Variable::NORMAL,
|
|
|
|
pos);
|
|
|
|
args->Add(new_target_proxy, zone());
|
|
|
|
VariableProxy* this_function_proxy = scope_->NewUnresolved(
|
|
|
|
factory(), ast_value_factory()->this_function_string(),
|
|
|
|
Variable::NORMAL, pos);
|
|
|
|
args->Add(this_function_proxy, zone());
|
2015-02-12 20:06:52 +00:00
|
|
|
CallRuntime* call = factory()->NewCallRuntime(
|
|
|
|
ast_value_factory()->empty_string(),
|
|
|
|
Runtime::FunctionForId(Runtime::kInlineDefaultConstructorCallSuper),
|
|
|
|
args, pos);
|
|
|
|
body->Add(factory()->NewReturnStatement(call, pos), zone());
|
2014-11-07 16:39:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
materialized_literal_count = function_state.materialized_literal_count();
|
|
|
|
expected_property_count = function_state.expected_property_count();
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionLiteral* function_literal = factory()->NewFunctionLiteral(
|
|
|
|
name, ast_value_factory(), function_scope, body,
|
2015-06-08 18:19:32 +00:00
|
|
|
materialized_literal_count, expected_property_count, parameter_count,
|
|
|
|
FunctionLiteral::kNoDuplicateParameters,
|
2014-11-07 16:39:00 +00:00
|
|
|
FunctionLiteral::ANONYMOUS_EXPRESSION, FunctionLiteral::kIsFunction,
|
2015-04-24 11:07:50 +00:00
|
|
|
FunctionLiteral::kShouldLazyCompile, kind, pos);
|
2014-11-07 16:39:00 +00:00
|
|
|
|
|
|
|
return function_literal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Target is a support class to facilitate manipulation of the
|
|
|
|
// Parser's target_stack_ (the stack of potential 'break' and
|
|
|
|
// 'continue' statement targets). Upon construction, a new target is
|
|
|
|
// added; it is removed upon destruction.
|
|
|
|
|
|
|
|
class Target BASE_EMBEDDED {
|
|
|
|
public:
|
2015-01-15 19:18:05 +00:00
|
|
|
Target(Target** variable, BreakableStatement* statement)
|
|
|
|
: variable_(variable), statement_(statement), previous_(*variable) {
|
2010-10-27 12:33:48 +00:00
|
|
|
*variable = this;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~Target() {
|
2010-10-27 12:33:48 +00:00
|
|
|
*variable_ = previous_;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2009-05-15 14:58:02 +00:00
|
|
|
Target* previous() { return previous_; }
|
2015-01-15 19:18:05 +00:00
|
|
|
BreakableStatement* statement() { return statement_; }
|
2009-05-15 14:58:02 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
private:
|
2010-10-27 12:33:48 +00:00
|
|
|
Target** variable_;
|
2015-01-15 19:18:05 +00:00
|
|
|
BreakableStatement* statement_;
|
2009-05-15 14:58:02 +00:00
|
|
|
Target* previous_;
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class TargetScope BASE_EMBEDDED {
|
|
|
|
public:
|
2010-10-27 12:33:48 +00:00
|
|
|
explicit TargetScope(Target** variable)
|
|
|
|
: variable_(variable), previous_(*variable) {
|
|
|
|
*variable = NULL;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~TargetScope() {
|
2010-10-27 12:33:48 +00:00
|
|
|
*variable_ = previous_;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2010-10-27 12:33:48 +00:00
|
|
|
Target** variable_;
|
2009-05-15 14:58:02 +00:00
|
|
|
Target* previous_;
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// The CHECK_OK macro is a convenient macro to enforce error
|
|
|
|
// handling for functions that may fail (by returning !*ok).
|
|
|
|
//
|
|
|
|
// CAUTION: This macro appends extra statements after a call,
|
|
|
|
// thus it must never be used where only a single statement
|
|
|
|
// is correct (e.g. an if statement branch w/o braces)!
|
|
|
|
|
|
|
|
#define CHECK_OK ok); \
|
|
|
|
if (!*ok) return NULL; \
|
|
|
|
((void)0
|
|
|
|
#define DUMMY ) // to make indentation work
|
|
|
|
#undef DUMMY
|
|
|
|
|
2008-12-01 15:42:35 +00:00
|
|
|
#define CHECK_FAILED /**/); \
|
2008-12-01 15:32:20 +00:00
|
|
|
if (failed_) return NULL; \
|
|
|
|
((void)0
|
|
|
|
#define DUMMY ) // to make indentation work
|
|
|
|
#undef DUMMY
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Implementation of Parser
|
|
|
|
|
2015-02-19 14:58:32 +00:00
|
|
|
bool ParserTraits::IsEval(const AstRawString* identifier) const {
|
|
|
|
return identifier == parser_->ast_value_factory()->eval_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ParserTraits::IsArguments(const AstRawString* identifier) const {
|
|
|
|
return identifier == parser_->ast_value_factory()->arguments_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
bool ParserTraits::IsEvalOrArguments(const AstRawString* identifier) const {
|
2015-02-19 14:58:32 +00:00
|
|
|
return IsEval(identifier) || IsArguments(identifier);
|
2014-02-11 09:35:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-10 12:04:51 +00:00
|
|
|
bool ParserTraits::IsUndefined(const AstRawString* identifier) const {
|
|
|
|
return identifier == parser_->ast_value_factory()->undefined_string();
|
|
|
|
}
|
2014-02-11 09:35:32 +00:00
|
|
|
|
2014-09-16 22:15:39 +00:00
|
|
|
bool ParserTraits::IsPrototype(const AstRawString* identifier) const {
|
|
|
|
return identifier == parser_->ast_value_factory()->prototype_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ParserTraits::IsConstructor(const AstRawString* identifier) const {
|
|
|
|
return identifier == parser_->ast_value_factory()->constructor_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-14 09:43:04 +00:00
|
|
|
bool ParserTraits::IsThisProperty(Expression* expression) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(expression != NULL);
|
2014-03-14 09:43:04 +00:00
|
|
|
Property* property = expression->AsProperty();
|
2015-01-15 20:02:20 +00:00
|
|
|
return property != NULL && property->obj()->IsVariableProxy() &&
|
|
|
|
property->obj()->AsVariableProxy()->is_this();
|
2014-03-14 09:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-19 14:08:47 +00:00
|
|
|
bool ParserTraits::IsIdentifier(Expression* expression) {
|
|
|
|
VariableProxy* operand = expression->AsVariableProxy();
|
|
|
|
return operand != NULL && !operand->is_this();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-21 10:34:51 +00:00
|
|
|
void ParserTraits::PushPropertyName(FuncNameInferrer* fni,
|
|
|
|
Expression* expression) {
|
|
|
|
if (expression->IsPropertyName()) {
|
2014-06-24 14:03:24 +00:00
|
|
|
fni->PushLiteralName(expression->AsLiteral()->AsRawPropertyName());
|
2014-03-21 10:34:51 +00:00
|
|
|
} else {
|
|
|
|
fni->PushLiteralName(
|
2014-09-11 09:52:36 +00:00
|
|
|
parser_->ast_value_factory()->anonymous_function_string());
|
2014-03-21 10:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-14 09:43:04 +00:00
|
|
|
void ParserTraits::CheckAssigningFunctionLiteralToProperty(Expression* left,
|
|
|
|
Expression* right) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(left != NULL);
|
2015-01-15 20:02:20 +00:00
|
|
|
if (left->IsProperty() && right->IsFunctionLiteral()) {
|
2014-03-14 09:43:04 +00:00
|
|
|
right->AsFunctionLiteral()->set_pretenure();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-21 09:51:33 +00:00
|
|
|
void ParserTraits::CheckPossibleEvalCall(Expression* expression,
|
|
|
|
Scope* scope) {
|
|
|
|
VariableProxy* callee = expression->AsVariableProxy();
|
|
|
|
if (callee != NULL &&
|
2014-09-11 09:52:36 +00:00
|
|
|
callee->raw_name() == parser_->ast_value_factory()->eval_string()) {
|
2014-03-21 09:51:33 +00:00
|
|
|
scope->DeclarationScope()->RecordEvalCall();
|
2015-04-22 13:22:37 +00:00
|
|
|
scope->RecordEvalCall();
|
2014-03-21 09:51:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-26 11:59:42 +00:00
|
|
|
Expression* ParserTraits::MarkExpressionAsAssigned(Expression* expression) {
|
|
|
|
VariableProxy* proxy =
|
|
|
|
expression != NULL ? expression->AsVariableProxy() : NULL;
|
|
|
|
if (proxy != NULL) proxy->set_is_assigned();
|
2014-03-14 09:43:04 +00:00
|
|
|
return expression;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-17 13:54:42 +00:00
|
|
|
bool ParserTraits::ShortcutNumericLiteralBinaryExpression(
|
|
|
|
Expression** x, Expression* y, Token::Value op, int pos,
|
2014-11-14 13:13:09 +00:00
|
|
|
AstNodeFactory* factory) {
|
2014-06-24 14:03:24 +00:00
|
|
|
if ((*x)->AsLiteral() && (*x)->AsLiteral()->raw_value()->IsNumber() &&
|
|
|
|
y->AsLiteral() && y->AsLiteral()->raw_value()->IsNumber()) {
|
|
|
|
double x_val = (*x)->AsLiteral()->raw_value()->AsNumber();
|
|
|
|
double y_val = y->AsLiteral()->raw_value()->AsNumber();
|
2014-03-17 13:54:42 +00:00
|
|
|
switch (op) {
|
|
|
|
case Token::ADD:
|
|
|
|
*x = factory->NewNumberLiteral(x_val + y_val, pos);
|
|
|
|
return true;
|
|
|
|
case Token::SUB:
|
|
|
|
*x = factory->NewNumberLiteral(x_val - y_val, pos);
|
|
|
|
return true;
|
|
|
|
case Token::MUL:
|
|
|
|
*x = factory->NewNumberLiteral(x_val * y_val, pos);
|
|
|
|
return true;
|
|
|
|
case Token::DIV:
|
|
|
|
*x = factory->NewNumberLiteral(x_val / y_val, pos);
|
|
|
|
return true;
|
|
|
|
case Token::BIT_OR: {
|
|
|
|
int value = DoubleToInt32(x_val) | DoubleToInt32(y_val);
|
|
|
|
*x = factory->NewNumberLiteral(value, pos);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case Token::BIT_AND: {
|
|
|
|
int value = DoubleToInt32(x_val) & DoubleToInt32(y_val);
|
|
|
|
*x = factory->NewNumberLiteral(value, pos);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case Token::BIT_XOR: {
|
|
|
|
int value = DoubleToInt32(x_val) ^ DoubleToInt32(y_val);
|
|
|
|
*x = factory->NewNumberLiteral(value, pos);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case Token::SHL: {
|
|
|
|
int value = DoubleToInt32(x_val) << (DoubleToInt32(y_val) & 0x1f);
|
|
|
|
*x = factory->NewNumberLiteral(value, pos);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case Token::SHR: {
|
|
|
|
uint32_t shift = DoubleToInt32(y_val) & 0x1f;
|
|
|
|
uint32_t value = DoubleToUint32(x_val) >> shift;
|
|
|
|
*x = factory->NewNumberLiteral(value, pos);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case Token::SAR: {
|
|
|
|
uint32_t shift = DoubleToInt32(y_val) & 0x1f;
|
|
|
|
int value = ArithmeticShiftRight(DoubleToInt32(x_val), shift);
|
|
|
|
*x = factory->NewNumberLiteral(value, pos);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-14 13:13:09 +00:00
|
|
|
Expression* ParserTraits::BuildUnaryExpression(Expression* expression,
|
|
|
|
Token::Value op, int pos,
|
|
|
|
AstNodeFactory* factory) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(expression != NULL);
|
2014-06-03 07:40:43 +00:00
|
|
|
if (expression->IsLiteral()) {
|
2014-06-24 14:03:24 +00:00
|
|
|
const AstValue* literal = expression->AsLiteral()->raw_value();
|
2014-03-19 14:08:47 +00:00
|
|
|
if (op == Token::NOT) {
|
|
|
|
// Convert the literal to a boolean condition and negate it.
|
|
|
|
bool condition = literal->BooleanValue();
|
2014-06-24 14:03:24 +00:00
|
|
|
return factory->NewBooleanLiteral(!condition, pos);
|
2014-03-19 14:08:47 +00:00
|
|
|
} else if (literal->IsNumber()) {
|
|
|
|
// Compute some expressions involving only number literals.
|
2014-06-24 14:03:24 +00:00
|
|
|
double value = literal->AsNumber();
|
2014-03-19 14:08:47 +00:00
|
|
|
switch (op) {
|
|
|
|
case Token::ADD:
|
|
|
|
return expression;
|
|
|
|
case Token::SUB:
|
|
|
|
return factory->NewNumberLiteral(-value, pos);
|
|
|
|
case Token::BIT_NOT:
|
|
|
|
return factory->NewNumberLiteral(~DoubleToInt32(value), pos);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Desugar '+foo' => 'foo*1'
|
|
|
|
if (op == Token::ADD) {
|
|
|
|
return factory->NewBinaryOperation(
|
|
|
|
Token::MUL, expression, factory->NewNumberLiteral(1, pos), pos);
|
|
|
|
}
|
|
|
|
// The same idea for '-foo' => 'foo*(-1)'.
|
|
|
|
if (op == Token::SUB) {
|
|
|
|
return factory->NewBinaryOperation(
|
|
|
|
Token::MUL, expression, factory->NewNumberLiteral(-1, pos), pos);
|
|
|
|
}
|
|
|
|
// ...and one more time for '~foo' => 'foo^(~0)'.
|
|
|
|
if (op == Token::BIT_NOT) {
|
|
|
|
return factory->NewBinaryOperation(
|
|
|
|
Token::BIT_XOR, expression, factory->NewNumberLiteral(~0, pos), pos);
|
|
|
|
}
|
|
|
|
return factory->NewUnaryOperation(op, expression, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-18 08:34:05 +00:00
|
|
|
Expression* ParserTraits::NewThrowReferenceError(
|
|
|
|
MessageTemplate::Template message, int pos) {
|
2015-06-24 16:54:47 +00:00
|
|
|
return NewThrowError(Runtime::kNewReferenceError, message,
|
|
|
|
parser_->ast_value_factory()->empty_string(), pos);
|
2014-04-02 11:03:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-18 08:34:05 +00:00
|
|
|
Expression* ParserTraits::NewThrowSyntaxError(MessageTemplate::Template message,
|
|
|
|
const AstRawString* arg,
|
|
|
|
int pos) {
|
2015-06-24 16:54:47 +00:00
|
|
|
return NewThrowError(Runtime::kNewSyntaxError, message, arg, pos);
|
2014-04-02 11:03:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-18 08:34:05 +00:00
|
|
|
Expression* ParserTraits::NewThrowTypeError(MessageTemplate::Template message,
|
|
|
|
const AstRawString* arg, int pos) {
|
2015-06-24 16:54:47 +00:00
|
|
|
return NewThrowError(Runtime::kNewTypeError, message, arg, pos);
|
2014-04-02 11:03:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-24 16:54:47 +00:00
|
|
|
Expression* ParserTraits::NewThrowError(Runtime::FunctionId id,
|
2015-05-18 08:34:05 +00:00
|
|
|
MessageTemplate::Template message,
|
|
|
|
const AstRawString* arg, int pos) {
|
2014-04-02 11:03:05 +00:00
|
|
|
Zone* zone = parser_->zone();
|
2014-06-24 14:03:24 +00:00
|
|
|
ZoneList<Expression*>* args = new (zone) ZoneList<Expression*>(2, zone);
|
2015-05-18 08:34:05 +00:00
|
|
|
args->Add(parser_->factory()->NewSmiLiteral(message, pos), zone);
|
2014-10-30 14:21:27 +00:00
|
|
|
args->Add(parser_->factory()->NewStringLiteral(arg, pos), zone);
|
2015-06-24 16:54:47 +00:00
|
|
|
CallRuntime* call_constructor = parser_->factory()->NewCallRuntime(
|
|
|
|
parser_->ast_value_factory()->empty_string(), Runtime::FunctionForId(id),
|
|
|
|
args, pos);
|
2014-04-02 11:03:05 +00:00
|
|
|
return parser_->factory()->NewThrow(call_constructor, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-11 09:35:32 +00:00
|
|
|
void ParserTraits::ReportMessageAt(Scanner::Location source_location,
|
2015-05-18 08:34:05 +00:00
|
|
|
MessageTemplate::Template message,
|
|
|
|
const char* arg, ParseErrorType error_type) {
|
2014-03-12 13:27:32 +00:00
|
|
|
if (parser_->stack_overflow()) {
|
|
|
|
// Suppress the error message (syntax error or such) in the presence of a
|
|
|
|
// stack overflow. The isolate allows only one pending exception at at time
|
|
|
|
// and we want to report the stack overflow later.
|
|
|
|
return;
|
|
|
|
}
|
2015-02-25 14:17:39 +00:00
|
|
|
parser_->pending_error_handler_.ReportMessageAt(source_location.beg_pos,
|
|
|
|
source_location.end_pos,
|
|
|
|
message, arg, error_type);
|
2014-02-11 09:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-18 08:34:05 +00:00
|
|
|
void ParserTraits::ReportMessage(MessageTemplate::Template message,
|
|
|
|
const char* arg, ParseErrorType error_type) {
|
2014-06-24 14:03:24 +00:00
|
|
|
Scanner::Location source_location = parser_->scanner()->location();
|
2015-02-20 21:19:43 +00:00
|
|
|
ReportMessageAt(source_location, message, arg, error_type);
|
2014-06-24 14:03:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-18 08:34:05 +00:00
|
|
|
void ParserTraits::ReportMessage(MessageTemplate::Template message,
|
|
|
|
const AstRawString* arg,
|
2015-02-20 21:19:43 +00:00
|
|
|
ParseErrorType error_type) {
|
2014-02-14 12:13:33 +00:00
|
|
|
Scanner::Location source_location = parser_->scanner()->location();
|
2015-02-20 21:19:43 +00:00
|
|
|
ReportMessageAt(source_location, message, arg, error_type);
|
2014-02-11 09:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ParserTraits::ReportMessageAt(Scanner::Location source_location,
|
2015-05-18 08:34:05 +00:00
|
|
|
MessageTemplate::Template message,
|
|
|
|
const AstRawString* arg,
|
2015-02-20 21:19:43 +00:00
|
|
|
ParseErrorType error_type) {
|
2014-03-12 13:27:32 +00:00
|
|
|
if (parser_->stack_overflow()) {
|
|
|
|
// Suppress the error message (syntax error or such) in the presence of a
|
|
|
|
// stack overflow. The isolate allows only one pending exception at at time
|
|
|
|
// and we want to report the stack overflow later.
|
|
|
|
return;
|
|
|
|
}
|
2015-02-25 14:17:39 +00:00
|
|
|
parser_->pending_error_handler_.ReportMessageAt(source_location.beg_pos,
|
|
|
|
source_location.end_pos,
|
|
|
|
message, arg, error_type);
|
2014-02-11 09:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
const AstRawString* ParserTraits::GetSymbol(Scanner* scanner) {
|
|
|
|
const AstRawString* result =
|
2014-09-11 09:52:36 +00:00
|
|
|
parser_->scanner()->CurrentSymbol(parser_->ast_value_factory());
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(result != NULL);
|
2014-03-25 09:09:24 +00:00
|
|
|
return result;
|
2014-02-11 09:35:32 +00:00
|
|
|
}
|
|
|
|
|
2014-02-11 11:51:01 +00:00
|
|
|
|
2014-08-22 14:40:38 +00:00
|
|
|
const AstRawString* ParserTraits::GetNumberAsSymbol(Scanner* scanner) {
|
|
|
|
double double_value = parser_->scanner()->DoubleValue();
|
|
|
|
char array[100];
|
|
|
|
const char* string =
|
2014-08-26 09:19:24 +00:00
|
|
|
DoubleToCString(double_value, Vector<char>(array, arraysize(array)));
|
2015-02-06 15:58:36 +00:00
|
|
|
return parser_->ast_value_factory()->GetOneByteString(string);
|
2014-08-22 14:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
const AstRawString* ParserTraits::GetNextSymbol(Scanner* scanner) {
|
2014-09-11 09:52:36 +00:00
|
|
|
return parser_->scanner()->NextSymbol(parser_->ast_value_factory());
|
2014-02-11 11:51:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-14 13:13:09 +00:00
|
|
|
Expression* ParserTraits::ThisExpression(Scope* scope, AstNodeFactory* factory,
|
|
|
|
int pos) {
|
2015-05-19 14:51:10 +00:00
|
|
|
return scope->NewUnresolved(factory,
|
|
|
|
parser_->ast_value_factory()->this_string(),
|
|
|
|
Variable::THIS, pos, pos + 4);
|
2014-02-14 11:24:26 +00:00
|
|
|
}
|
|
|
|
|
2015-06-09 15:43:07 +00:00
|
|
|
|
2015-06-02 22:04:25 +00:00
|
|
|
Expression* ParserTraits::SuperPropertyReference(Scope* scope,
|
|
|
|
AstNodeFactory* factory,
|
|
|
|
int pos) {
|
2015-06-04 16:22:29 +00:00
|
|
|
// this_function[home_object_symbol]
|
|
|
|
VariableProxy* this_function_proxy = scope->NewUnresolved(
|
|
|
|
factory, parser_->ast_value_factory()->this_function_string(),
|
2015-05-26 20:29:47 +00:00
|
|
|
Variable::NORMAL, pos);
|
2015-06-04 16:22:29 +00:00
|
|
|
Expression* home_object_symbol_literal =
|
|
|
|
factory->NewSymbolLiteral("home_object_symbol", RelocInfo::kNoPosition);
|
|
|
|
Expression* home_object = factory->NewProperty(
|
|
|
|
this_function_proxy, home_object_symbol_literal, pos);
|
2015-06-02 22:04:25 +00:00
|
|
|
return factory->NewSuperPropertyReference(
|
2015-06-04 16:22:29 +00:00
|
|
|
ThisExpression(scope, factory, pos)->AsVariableProxy(), home_object, pos);
|
2014-08-18 12:35:34 +00:00
|
|
|
}
|
2014-02-14 11:24:26 +00:00
|
|
|
|
2014-09-18 17:39:49 +00:00
|
|
|
|
2015-06-02 22:04:25 +00:00
|
|
|
Expression* ParserTraits::SuperCallReference(Scope* scope,
|
|
|
|
AstNodeFactory* factory, int pos) {
|
|
|
|
VariableProxy* new_target_proxy = scope->NewUnresolved(
|
|
|
|
factory, parser_->ast_value_factory()->new_target_string(),
|
|
|
|
Variable::NORMAL, pos);
|
|
|
|
VariableProxy* this_function_proxy = scope->NewUnresolved(
|
|
|
|
factory, parser_->ast_value_factory()->this_function_string(),
|
|
|
|
Variable::NORMAL, pos);
|
|
|
|
return factory->NewSuperCallReference(
|
|
|
|
ThisExpression(scope, factory, pos)->AsVariableProxy(), new_target_proxy,
|
|
|
|
this_function_proxy, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-09 15:43:07 +00:00
|
|
|
Expression* ParserTraits::NewTargetExpression(Scope* scope,
|
|
|
|
AstNodeFactory* factory,
|
|
|
|
int pos) {
|
|
|
|
static const int kNewTargetStringLength = 10;
|
|
|
|
return scope->NewUnresolved(
|
|
|
|
factory, parser_->ast_value_factory()->new_target_string(),
|
|
|
|
Variable::NORMAL, pos, pos + kNewTargetStringLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-07 16:39:00 +00:00
|
|
|
Expression* ParserTraits::DefaultConstructor(bool call_super, Scope* scope,
|
|
|
|
int pos, int end_pos) {
|
|
|
|
return parser_->DefaultConstructor(call_super, scope, pos, end_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-14 13:13:09 +00:00
|
|
|
Literal* ParserTraits::ExpressionFromLiteral(Token::Value token, int pos,
|
|
|
|
Scanner* scanner,
|
|
|
|
AstNodeFactory* factory) {
|
2014-02-14 11:24:26 +00:00
|
|
|
switch (token) {
|
|
|
|
case Token::NULL_LITERAL:
|
2014-06-24 14:03:24 +00:00
|
|
|
return factory->NewNullLiteral(pos);
|
2014-02-14 11:24:26 +00:00
|
|
|
case Token::TRUE_LITERAL:
|
2014-06-24 14:03:24 +00:00
|
|
|
return factory->NewBooleanLiteral(true, pos);
|
2014-02-14 11:24:26 +00:00
|
|
|
case Token::FALSE_LITERAL:
|
2014-06-24 14:03:24 +00:00
|
|
|
return factory->NewBooleanLiteral(false, pos);
|
2015-03-03 11:04:49 +00:00
|
|
|
case Token::SMI: {
|
|
|
|
int value = scanner->smi_value();
|
|
|
|
return factory->NewSmiLiteral(value, pos);
|
|
|
|
}
|
2014-02-14 11:24:26 +00:00
|
|
|
case Token::NUMBER: {
|
2015-06-30 21:12:12 +00:00
|
|
|
bool has_dot = scanner->ContainsDot();
|
2014-03-12 14:03:25 +00:00
|
|
|
double value = scanner->DoubleValue();
|
2015-06-30 21:12:12 +00:00
|
|
|
return factory->NewNumberLiteral(value, pos, has_dot);
|
2014-02-14 11:24:26 +00:00
|
|
|
}
|
|
|
|
default:
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(false);
|
2014-02-14 11:24:26 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-14 13:13:09 +00:00
|
|
|
Expression* ParserTraits::ExpressionFromIdentifier(const AstRawString* name,
|
2015-02-26 13:48:10 +00:00
|
|
|
int start_position,
|
|
|
|
int end_position,
|
|
|
|
Scope* scope,
|
2014-11-14 13:13:09 +00:00
|
|
|
AstNodeFactory* factory) {
|
2014-02-14 11:24:26 +00:00
|
|
|
if (parser_->fni_ != NULL) parser_->fni_->PushVariableName(name);
|
2015-05-19 14:51:10 +00:00
|
|
|
return scope->NewUnresolved(factory, name, Variable::NORMAL, start_position,
|
|
|
|
end_position);
|
2014-02-14 11:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-14 13:13:09 +00:00
|
|
|
Expression* ParserTraits::ExpressionFromString(int pos, Scanner* scanner,
|
|
|
|
AstNodeFactory* factory) {
|
2014-06-24 14:03:24 +00:00
|
|
|
const AstRawString* symbol = GetSymbol(scanner);
|
2014-02-14 11:24:26 +00:00
|
|
|
if (parser_->fni_ != NULL) parser_->fni_->PushLiteralName(symbol);
|
2014-06-24 14:03:24 +00:00
|
|
|
return factory->NewStringLiteral(symbol, pos);
|
2014-02-14 11:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-14 13:13:09 +00:00
|
|
|
Expression* ParserTraits::GetIterator(Expression* iterable,
|
|
|
|
AstNodeFactory* factory) {
|
2014-08-05 13:17:49 +00:00
|
|
|
Expression* iterator_symbol_literal =
|
2014-11-13 08:47:52 +00:00
|
|
|
factory->NewSymbolLiteral("iterator_symbol", RelocInfo::kNoPosition);
|
2014-08-05 13:17:49 +00:00
|
|
|
int pos = iterable->position();
|
|
|
|
Expression* prop =
|
|
|
|
factory->NewProperty(iterable, iterator_symbol_literal, pos);
|
|
|
|
Zone* zone = parser_->zone();
|
|
|
|
ZoneList<Expression*>* args = new (zone) ZoneList<Expression*>(0, zone);
|
|
|
|
return factory->NewCall(prop, args, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-14 13:13:09 +00:00
|
|
|
Literal* ParserTraits::GetLiteralTheHole(int position,
|
|
|
|
AstNodeFactory* factory) {
|
2014-06-24 14:03:24 +00:00
|
|
|
return factory->NewTheHoleLiteral(RelocInfo::kNoPosition);
|
2014-02-14 11:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Expression* ParserTraits::ParseV8Intrinsic(bool* ok) {
|
|
|
|
return parser_->ParseV8Intrinsic(ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-11 15:40:41 +00:00
|
|
|
FunctionLiteral* ParserTraits::ParseFunctionLiteral(
|
2014-09-10 16:39:42 +00:00
|
|
|
const AstRawString* name, Scanner::Location function_name_location,
|
2015-07-09 21:31:11 +00:00
|
|
|
FunctionNameValidity function_name_validity, FunctionKind kind,
|
2014-09-10 16:39:42 +00:00
|
|
|
int function_token_position, FunctionLiteral::FunctionType type,
|
|
|
|
FunctionLiteral::ArityRestriction arity_restriction, bool* ok) {
|
|
|
|
return parser_->ParseFunctionLiteral(
|
2015-07-09 21:31:11 +00:00
|
|
|
name, function_name_location, function_name_validity, kind,
|
2014-09-10 16:39:42 +00:00
|
|
|
function_token_position, type, arity_restriction, ok);
|
2014-03-11 15:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-14 15:05:05 +00:00
|
|
|
ClassLiteral* ParserTraits::ParseClassLiteral(
|
|
|
|
const AstRawString* name, Scanner::Location class_name_location,
|
|
|
|
bool name_is_strict_reserved, int pos, bool* ok) {
|
|
|
|
return parser_->ParseClassLiteral(name, class_name_location,
|
|
|
|
name_is_strict_reserved, pos, ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-09 14:51:13 +00:00
|
|
|
Parser::Parser(ParseInfo* info)
|
|
|
|
: ParserBase<ParserTraits>(info->zone(), &scanner_, info->stack_limit(),
|
2015-02-12 13:02:30 +00:00
|
|
|
info->extension(), info->ast_value_factory(),
|
|
|
|
NULL, this),
|
2015-03-09 14:51:13 +00:00
|
|
|
scanner_(info->unicode_cache()),
|
2011-11-25 09:36:31 +00:00
|
|
|
reusable_preparser_(NULL),
|
2013-08-23 09:25:37 +00:00
|
|
|
original_scope_(NULL),
|
2008-07-03 15:10:15 +00:00
|
|
|
target_stack_(NULL),
|
2015-02-12 13:02:30 +00:00
|
|
|
compile_options_(info->compile_options()),
|
2014-07-10 10:28:05 +00:00
|
|
|
cached_parse_data_(NULL),
|
2014-09-02 11:36:21 +00:00
|
|
|
total_preparse_skipped_(0),
|
2015-02-12 13:02:30 +00:00
|
|
|
pre_parse_timer_(NULL),
|
|
|
|
parsing_on_main_thread_(true) {
|
2015-03-09 14:51:13 +00:00
|
|
|
// Even though we were passed ParseInfo, we should not store it in
|
2015-02-12 13:02:30 +00:00
|
|
|
// Parser - this makes sure that Isolate is not accidentally accessed via
|
2015-03-09 14:51:13 +00:00
|
|
|
// ParseInfo during background parsing.
|
2015-02-12 13:02:30 +00:00
|
|
|
DCHECK(!info->script().is_null() || info->source_stream() != NULL);
|
2015-03-09 14:51:13 +00:00
|
|
|
set_allow_lazy(info->allow_lazy_parsing());
|
2014-11-20 10:51:49 +00:00
|
|
|
set_allow_natives(FLAG_allow_natives_syntax || info->is_native());
|
|
|
|
set_allow_harmony_modules(!info->is_native() && FLAG_harmony_modules);
|
|
|
|
set_allow_harmony_arrow_functions(FLAG_harmony_arrow_functions);
|
|
|
|
set_allow_harmony_sloppy(FLAG_harmony_sloppy);
|
2014-12-02 10:58:11 +00:00
|
|
|
set_allow_harmony_unicode(FLAG_harmony_unicode);
|
2015-01-15 20:02:20 +00:00
|
|
|
set_allow_harmony_computed_property_names(
|
|
|
|
FLAG_harmony_computed_property_names);
|
2015-01-30 15:21:41 +00:00
|
|
|
set_allow_harmony_rest_params(FLAG_harmony_rest_parameters);
|
2015-04-09 19:37:14 +00:00
|
|
|
set_allow_harmony_spreadcalls(FLAG_harmony_spreadcalls);
|
2015-04-28 15:15:03 +00:00
|
|
|
set_allow_harmony_destructuring(FLAG_harmony_destructuring);
|
2015-05-21 08:09:06 +00:00
|
|
|
set_allow_harmony_spread_arrays(FLAG_harmony_spread_arrays);
|
2015-06-09 15:43:07 +00:00
|
|
|
set_allow_harmony_new_target(FLAG_harmony_new_target);
|
2015-02-05 14:11:34 +00:00
|
|
|
set_allow_strong_mode(FLAG_strong_mode);
|
2015-07-07 21:57:09 +00:00
|
|
|
set_allow_legacy_const(FLAG_legacy_const);
|
2014-06-30 13:35:16 +00:00
|
|
|
for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount;
|
|
|
|
++feature) {
|
|
|
|
use_counts_[feature] = 0;
|
|
|
|
}
|
2014-09-11 09:52:36 +00:00
|
|
|
if (info->ast_value_factory() == NULL) {
|
|
|
|
// info takes ownership of AstValueFactory.
|
2015-03-09 14:51:13 +00:00
|
|
|
info->set_ast_value_factory(new AstValueFactory(zone(), info->hash_seed()));
|
|
|
|
info->set_ast_value_factory_owned();
|
2015-02-06 15:58:36 +00:00
|
|
|
ast_value_factory_ = info->ast_value_factory();
|
2014-09-02 11:36:21 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-09 14:51:13 +00:00
|
|
|
FunctionLiteral* Parser::ParseProgram(Isolate* isolate, ParseInfo* info) {
|
2013-10-29 11:44:04 +00:00
|
|
|
// TODO(bmeurer): We temporarily need to pass allow_nesting = true here,
|
|
|
|
// see comment for HistogramTimerScope class.
|
2014-09-02 11:36:21 +00:00
|
|
|
|
2015-02-12 13:02:30 +00:00
|
|
|
// It's OK to use the Isolate & counters here, since this function is only
|
|
|
|
// called in the main thread.
|
|
|
|
DCHECK(parsing_on_main_thread_);
|
|
|
|
|
|
|
|
HistogramTimerScope timer_scope(isolate->counters()->parse(), true);
|
|
|
|
Handle<String> source(String::cast(info->script()->source()));
|
|
|
|
isolate->counters()->total_parse_size()->Increment(source->length());
|
2014-06-30 13:25:46 +00:00
|
|
|
base::ElapsedTimer timer;
|
2013-08-29 09:15:13 +00:00
|
|
|
if (FLAG_trace_parse) {
|
|
|
|
timer.Start();
|
|
|
|
}
|
2014-09-11 09:52:36 +00:00
|
|
|
fni_ = new (zone()) FuncNameInferrer(ast_value_factory(), zone());
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Initialize parser state.
|
2014-03-19 13:24:13 +00:00
|
|
|
CompleteParserRecorder recorder;
|
Change ScriptCompiler::CompileOptions to allow for two 'cache' modes
(parser or code) and to be explicit about cache consumption or production
(rather than making presence of cached_data imply one or the other.)
Also add a --cache flag to d8, to allow testing the functionality.
-----------------------------
API change
Reason: Currently, V8 supports a 'parser cache' for repeatedly executing the same script. We'd like to add a 2nd mode that would cache code, and would like to let the embedder decide which mode they chose (if any).
Note: Previously, the 'use cached data' property was implied by the presence of the cached data itself. (That is, kNoCompileOptions and source->cached_data != NULL.) That is no longer sufficient, since the presence of data is no longer sufficient to determine /which kind/ of data is present.
Changes from old behaviour:
- If you previously didn't use caching, nothing changes.
Example:
v8::CompileUnbound(isolate, source, kNoCompileOptions);
- If you previously used caching, it worked like this:
- 1st run:
v8::CompileUnbound(isolate, source, kProduceToCache);
Then, source->cached_data would contain the
data-to-be cached. This remains the same, except you
need to tell V8 which type of data you want.
v8::CompileUnbound(isolate, source, kProduceParserCache);
- 2nd run:
v8::CompileUnbound(isolate, source, kNoCompileOptions);
with source->cached_data set to the data you received in
the first run. This will now ignore the cached data, and
you need to explicitly tell V8 to use it:
v8::CompileUnbound(isolate, source, kConsumeParserCache);
-----------------------------
BUG=
R=marja@chromium.org, yangguo@chromium.org
Review URL: https://codereview.chromium.org/389573006
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@22431 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2014-07-16 12:18:33 +00:00
|
|
|
|
2014-11-17 12:16:27 +00:00
|
|
|
if (produce_cached_parse_data()) {
|
2014-03-19 13:24:13 +00:00
|
|
|
log_ = &recorder;
|
2014-11-17 12:16:27 +00:00
|
|
|
} else if (consume_cached_parse_data()) {
|
2014-07-10 10:28:05 +00:00
|
|
|
cached_parse_data_->Initialize();
|
2014-03-19 13:24:13 +00:00
|
|
|
}
|
|
|
|
|
2014-04-08 09:49:49 +00:00
|
|
|
source = String::Flatten(source);
|
2012-07-18 11:22:46 +00:00
|
|
|
FunctionLiteral* result;
|
2014-09-12 09:12:08 +00:00
|
|
|
|
2010-12-07 14:03:59 +00:00
|
|
|
if (source->IsExternalTwoByteString()) {
|
|
|
|
// Notice that the stream is destroyed at the end of the branch block.
|
|
|
|
// The last line of the blocks can't be moved outside, even though they're
|
|
|
|
// identical calls.
|
2012-03-12 12:35:28 +00:00
|
|
|
ExternalTwoByteStringUtf16CharacterStream stream(
|
2010-12-07 14:03:59 +00:00
|
|
|
Handle<ExternalTwoByteString>::cast(source), 0, source->length());
|
2010-12-22 20:14:19 +00:00
|
|
|
scanner_.Initialize(&stream);
|
2015-04-16 12:42:43 +00:00
|
|
|
result = DoParseProgram(info);
|
2010-12-07 14:03:59 +00:00
|
|
|
} else {
|
2012-03-12 12:35:28 +00:00
|
|
|
GenericStringUtf16CharacterStream stream(source, 0, source->length());
|
2010-12-22 20:14:19 +00:00
|
|
|
scanner_.Initialize(&stream);
|
2015-04-16 12:42:43 +00:00
|
|
|
result = DoParseProgram(info);
|
2015-04-15 17:20:29 +00:00
|
|
|
}
|
2015-04-16 12:42:43 +00:00
|
|
|
if (result != NULL) {
|
|
|
|
DCHECK_EQ(scanner_.peek_location().beg_pos, source->length());
|
2010-12-07 14:03:59 +00:00
|
|
|
}
|
2015-02-20 09:39:32 +00:00
|
|
|
HandleSourceURLComments(isolate, info->script());
|
2012-07-18 11:22:46 +00:00
|
|
|
|
|
|
|
if (FLAG_trace_parse && result != NULL) {
|
2013-08-29 09:15:13 +00:00
|
|
|
double ms = timer.Elapsed().InMillisecondsF();
|
2015-02-12 13:02:30 +00:00
|
|
|
if (info->is_eval()) {
|
2012-07-18 11:22:46 +00:00
|
|
|
PrintF("[parsing eval");
|
2015-02-12 13:02:30 +00:00
|
|
|
} else if (info->script()->name()->IsString()) {
|
|
|
|
String* name = String::cast(info->script()->name());
|
2015-07-13 12:38:06 +00:00
|
|
|
base::SmartArrayPointer<char> name_chars = name->ToCString();
|
2013-12-09 07:41:20 +00:00
|
|
|
PrintF("[parsing script: %s", name_chars.get());
|
2012-07-18 11:22:46 +00:00
|
|
|
} else {
|
|
|
|
PrintF("[parsing script");
|
|
|
|
}
|
|
|
|
PrintF(" - took %0.3f ms]\n", ms);
|
|
|
|
}
|
2014-11-17 12:16:27 +00:00
|
|
|
if (produce_cached_parse_data()) {
|
2015-02-12 13:02:30 +00:00
|
|
|
if (result != NULL) *info->cached_data() = recorder.GetScriptData();
|
2014-03-19 13:24:13 +00:00
|
|
|
log_ = NULL;
|
|
|
|
}
|
2012-07-18 11:22:46 +00:00
|
|
|
return result;
|
2010-12-07 14:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-16 12:42:43 +00:00
|
|
|
FunctionLiteral* Parser::DoParseProgram(ParseInfo* info) {
|
2015-02-12 13:02:30 +00:00
|
|
|
// Note that this function can be called from the main thread or from a
|
|
|
|
// background thread. We should not access anything Isolate / heap dependent
|
2015-03-09 14:51:13 +00:00
|
|
|
// via ParseInfo, and also not pass it forward.
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(scope_ == NULL);
|
|
|
|
DCHECK(target_stack_ == NULL);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2015-04-27 12:13:45 +00:00
|
|
|
Mode parsing_mode = FLAG_lazy && allow_lazy() ? PARSE_LAZILY : PARSE_EAGERLY;
|
|
|
|
if (allow_natives() || extension_ != NULL) parsing_mode = PARSE_EAGERLY;
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
FunctionLiteral* result = NULL;
|
2014-09-12 09:12:08 +00:00
|
|
|
{
|
2015-05-19 14:51:10 +00:00
|
|
|
// TODO(wingo): Add an outer GLOBAL_SCOPE corresponding to the native
|
|
|
|
// context, which will have the "this" binding for script scopes.
|
2015-04-16 12:42:43 +00:00
|
|
|
Scope* scope = NewScope(scope_, SCRIPT_SCOPE);
|
|
|
|
info->set_script_scope(scope);
|
2014-07-09 14:50:23 +00:00
|
|
|
if (!info->context().is_null() && !info->context()->IsNativeContext()) {
|
2015-04-16 12:42:43 +00:00
|
|
|
scope = Scope::DeserializeScopeChain(info->isolate(), zone(),
|
|
|
|
*info->context(), scope);
|
2014-06-24 14:03:24 +00:00
|
|
|
// The Scope is backed up by ScopeInfo (which is in the V8 heap); this
|
|
|
|
// means the Parser cannot operate independent of the V8 heap. Tell the
|
|
|
|
// string table to internalize strings and values right after they're
|
2015-02-12 13:02:30 +00:00
|
|
|
// created. This kind of parsing can only be done in the main thread.
|
|
|
|
DCHECK(parsing_on_main_thread_);
|
|
|
|
ast_value_factory()->Internalize(info->isolate());
|
2012-08-28 11:25:08 +00:00
|
|
|
}
|
2015-04-16 12:42:43 +00:00
|
|
|
original_scope_ = scope;
|
2012-03-15 13:02:21 +00:00
|
|
|
if (info->is_eval()) {
|
2015-04-24 05:55:39 +00:00
|
|
|
if (!scope->is_script_scope() || is_strict(info->language_mode())) {
|
2015-04-27 12:13:45 +00:00
|
|
|
parsing_mode = PARSE_EAGERLY;
|
2015-04-24 05:55:39 +00:00
|
|
|
}
|
2015-04-27 12:13:45 +00:00
|
|
|
scope = NewScope(scope, EVAL_SCOPE);
|
2015-02-24 22:39:26 +00:00
|
|
|
} else if (info->is_module()) {
|
2015-04-16 12:42:43 +00:00
|
|
|
scope = NewScope(scope, MODULE_SCOPE);
|
2011-11-15 13:48:40 +00:00
|
|
|
}
|
2015-04-16 12:42:43 +00:00
|
|
|
|
|
|
|
scope->set_start_position(0);
|
2012-08-28 11:25:08 +00:00
|
|
|
|
2015-04-27 12:13:45 +00:00
|
|
|
// Enter 'scope' with the given parsing mode.
|
|
|
|
ParsingModeScope parsing_mode_scope(this, parsing_mode);
|
2014-11-14 13:13:09 +00:00
|
|
|
AstNodeFactory function_factory(ast_value_factory());
|
2015-04-16 12:42:43 +00:00
|
|
|
FunctionState function_state(&function_state_, &scope_, scope,
|
2015-02-06 10:34:50 +00:00
|
|
|
kNormalFunction, &function_factory);
|
2013-04-02 17:34:59 +00:00
|
|
|
|
2015-02-04 09:34:05 +00:00
|
|
|
scope_->SetLanguageMode(info->language_mode());
|
2012-06-11 12:42:31 +00:00
|
|
|
ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(16, zone());
|
2008-07-03 15:10:15 +00:00
|
|
|
bool ok = true;
|
2014-02-14 12:13:33 +00:00
|
|
|
int beg_pos = scanner()->location().beg_pos;
|
2015-01-27 21:06:36 +00:00
|
|
|
if (info->is_module()) {
|
|
|
|
DCHECK(allow_harmony_modules());
|
2015-02-25 23:00:10 +00:00
|
|
|
ParseModuleItemList(body, &ok);
|
2015-01-27 21:06:36 +00:00
|
|
|
} else {
|
2015-04-27 12:13:45 +00:00
|
|
|
ParseStatementList(body, Token::EOS, &ok);
|
2015-01-27 21:06:36 +00:00
|
|
|
}
|
2014-07-02 07:01:31 +00:00
|
|
|
|
2015-04-16 12:42:43 +00:00
|
|
|
// The parser will peek but not consume EOS. Our scope logically goes all
|
|
|
|
// the way to the EOS, though.
|
|
|
|
scope->set_end_position(scanner()->peek_location().beg_pos);
|
|
|
|
|
2015-02-04 09:34:05 +00:00
|
|
|
if (ok && is_strict(language_mode())) {
|
2014-12-18 22:01:25 +00:00
|
|
|
CheckStrictOctalLiteral(beg_pos, scanner()->location().end_pos, &ok);
|
2015-07-10 16:39:47 +00:00
|
|
|
}
|
|
|
|
if (ok && (is_strict(language_mode()) || allow_harmony_sloppy())) {
|
2014-02-12 12:02:07 +00:00
|
|
|
CheckConflictingVarDeclarations(scope_, &ok);
|
2011-09-01 12:31:18 +00:00
|
|
|
}
|
|
|
|
|
2013-03-07 15:46:14 +00:00
|
|
|
if (ok && info->parse_restriction() == ONLY_SINGLE_FUNCTION_LITERAL) {
|
|
|
|
if (body->length() != 1 ||
|
|
|
|
!body->at(0)->IsExpressionStatement() ||
|
|
|
|
!body->at(0)->AsExpressionStatement()->
|
|
|
|
expression()->IsFunctionLiteral()) {
|
2015-05-18 08:34:05 +00:00
|
|
|
ReportMessage(MessageTemplate::kSingleFunctionLiteral);
|
2013-03-07 15:46:14 +00:00
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
if (ok) {
|
2012-02-08 09:56:33 +00:00
|
|
|
result = factory()->NewFunctionLiteral(
|
2014-09-11 09:52:36 +00:00
|
|
|
ast_value_factory()->empty_string(), ast_value_factory(), scope_,
|
|
|
|
body, function_state.materialized_literal_count(),
|
2015-06-08 18:19:32 +00:00
|
|
|
function_state.expected_property_count(), 0,
|
2012-02-14 14:14:51 +00:00
|
|
|
FunctionLiteral::kNoDuplicateParameters,
|
2014-07-21 09:58:01 +00:00
|
|
|
FunctionLiteral::ANONYMOUS_EXPRESSION, FunctionLiteral::kGlobalOrEval,
|
2015-04-24 11:07:50 +00:00
|
|
|
FunctionLiteral::kShouldLazyCompile, FunctionKind::kNormalFunction,
|
|
|
|
0);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the target stack is empty.
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(target_stack_ == NULL);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-02-20 14:02:59 +00:00
|
|
|
|
2015-03-09 14:51:13 +00:00
|
|
|
FunctionLiteral* Parser::ParseLazy(Isolate* isolate, ParseInfo* info) {
|
2015-02-12 13:02:30 +00:00
|
|
|
// It's OK to use the Isolate & counters here, since this function is only
|
|
|
|
// called in the main thread.
|
|
|
|
DCHECK(parsing_on_main_thread_);
|
2015-02-20 09:39:32 +00:00
|
|
|
HistogramTimerScope timer_scope(isolate->counters()->parse_lazy());
|
2015-02-12 13:02:30 +00:00
|
|
|
Handle<String> source(String::cast(info->script()->source()));
|
2015-02-20 09:39:32 +00:00
|
|
|
isolate->counters()->total_parse_size()->Increment(source->length());
|
2014-06-30 13:25:46 +00:00
|
|
|
base::ElapsedTimer timer;
|
2013-08-29 09:15:13 +00:00
|
|
|
if (FLAG_trace_parse) {
|
|
|
|
timer.Start();
|
|
|
|
}
|
2015-02-12 13:02:30 +00:00
|
|
|
Handle<SharedFunctionInfo> shared_info = info->shared_info();
|
2012-07-18 11:22:46 +00:00
|
|
|
|
2010-12-07 14:03:59 +00:00
|
|
|
// Initialize parser state.
|
2014-04-08 09:49:49 +00:00
|
|
|
source = String::Flatten(source);
|
2012-07-18 11:22:46 +00:00
|
|
|
FunctionLiteral* result;
|
2010-12-07 14:03:59 +00:00
|
|
|
if (source->IsExternalTwoByteString()) {
|
2012-03-12 12:35:28 +00:00
|
|
|
ExternalTwoByteStringUtf16CharacterStream stream(
|
2010-12-07 14:03:59 +00:00
|
|
|
Handle<ExternalTwoByteString>::cast(source),
|
2011-03-09 16:57:03 +00:00
|
|
|
shared_info->start_position(),
|
|
|
|
shared_info->end_position());
|
2015-02-20 09:39:32 +00:00
|
|
|
result = ParseLazy(isolate, info, &stream);
|
2010-12-07 14:03:59 +00:00
|
|
|
} else {
|
2012-03-12 12:35:28 +00:00
|
|
|
GenericStringUtf16CharacterStream stream(source,
|
|
|
|
shared_info->start_position(),
|
|
|
|
shared_info->end_position());
|
2015-02-20 09:39:32 +00:00
|
|
|
result = ParseLazy(isolate, info, &stream);
|
2010-12-07 14:03:59 +00:00
|
|
|
}
|
2012-07-18 11:22:46 +00:00
|
|
|
|
|
|
|
if (FLAG_trace_parse && result != NULL) {
|
2013-08-29 09:15:13 +00:00
|
|
|
double ms = timer.Elapsed().InMillisecondsF();
|
2015-07-13 12:38:06 +00:00
|
|
|
base::SmartArrayPointer<char> name_chars =
|
|
|
|
result->debug_name()->ToCString();
|
2013-12-09 07:41:20 +00:00
|
|
|
PrintF("[parsing function: %s - took %0.3f ms]\n", name_chars.get(), ms);
|
2012-07-18 11:22:46 +00:00
|
|
|
}
|
|
|
|
return result;
|
2010-12-07 14:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-09 14:51:13 +00:00
|
|
|
FunctionLiteral* Parser::ParseLazy(Isolate* isolate, ParseInfo* info,
|
2015-02-12 13:02:30 +00:00
|
|
|
Utf16CharacterStream* source) {
|
|
|
|
Handle<SharedFunctionInfo> shared_info = info->shared_info();
|
2010-12-22 20:14:19 +00:00
|
|
|
scanner_.Initialize(source);
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(scope_ == NULL);
|
|
|
|
DCHECK(target_stack_ == NULL);
|
2010-12-07 14:03:59 +00:00
|
|
|
|
2011-03-09 16:57:03 +00:00
|
|
|
Handle<String> name(String::cast(shared_info->name()));
|
2014-09-11 09:52:36 +00:00
|
|
|
DCHECK(ast_value_factory());
|
|
|
|
fni_ = new (zone()) FuncNameInferrer(ast_value_factory(), zone());
|
|
|
|
const AstRawString* raw_name = ast_value_factory()->GetString(name);
|
2014-06-24 14:03:24 +00:00
|
|
|
fni_->PushEnclosingName(raw_name);
|
2010-08-23 13:26:03 +00:00
|
|
|
|
2012-08-07 14:47:36 +00:00
|
|
|
ParsingModeScope parsing_mode(this, PARSE_EAGERLY);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Place holder for the result.
|
|
|
|
FunctionLiteral* result = NULL;
|
|
|
|
|
|
|
|
{
|
|
|
|
// Parse the function literal.
|
2014-11-12 11:34:09 +00:00
|
|
|
Scope* scope = NewScope(scope_, SCRIPT_SCOPE);
|
2015-03-09 14:51:13 +00:00
|
|
|
info->set_script_scope(scope);
|
2015-02-12 13:02:30 +00:00
|
|
|
if (!info->closure().is_null()) {
|
|
|
|
// Ok to use Isolate here, since lazy function parsing is only done in the
|
|
|
|
// main thread.
|
|
|
|
DCHECK(parsing_on_main_thread_);
|
2015-02-20 09:39:32 +00:00
|
|
|
scope = Scope::DeserializeScopeChain(isolate, zone(),
|
2015-02-12 13:02:30 +00:00
|
|
|
info->closure()->context(), scope);
|
2011-03-09 16:57:03 +00:00
|
|
|
}
|
2013-08-23 09:25:37 +00:00
|
|
|
original_scope_ = scope;
|
2014-11-14 13:13:09 +00:00
|
|
|
AstNodeFactory function_factory(ast_value_factory());
|
2014-10-09 08:16:13 +00:00
|
|
|
FunctionState function_state(&function_state_, &scope_, scope,
|
2015-02-06 10:34:50 +00:00
|
|
|
shared_info->kind(), &function_factory);
|
2015-02-04 09:34:05 +00:00
|
|
|
DCHECK(is_sloppy(scope->language_mode()) ||
|
2015-02-12 13:02:30 +00:00
|
|
|
is_strict(info->language_mode()));
|
|
|
|
DCHECK(info->language_mode() == shared_info->language_mode());
|
2015-02-04 09:34:05 +00:00
|
|
|
scope->SetLanguageMode(shared_info->language_mode());
|
2013-06-06 13:28:22 +00:00
|
|
|
FunctionLiteral::FunctionType function_type = shared_info->is_expression()
|
2011-08-09 12:43:08 +00:00
|
|
|
? (shared_info->is_anonymous()
|
|
|
|
? FunctionLiteral::ANONYMOUS_EXPRESSION
|
|
|
|
: FunctionLiteral::NAMED_EXPRESSION)
|
|
|
|
: FunctionLiteral::DECLARATION;
|
2008-07-03 15:10:15 +00:00
|
|
|
bool ok = true;
|
2014-07-21 09:58:01 +00:00
|
|
|
|
|
|
|
if (shared_info->is_arrow()) {
|
2015-05-26 20:29:47 +00:00
|
|
|
Scope* scope =
|
|
|
|
NewScope(scope_, ARROW_SCOPE, FunctionKind::kArrowFunction);
|
2015-04-21 14:44:18 +00:00
|
|
|
scope->set_start_position(shared_info->start_position());
|
2015-05-13 11:45:04 +00:00
|
|
|
ExpressionClassifier formals_classifier;
|
2015-06-22 14:15:53 +00:00
|
|
|
ParserFormalParameterParsingState parsing_state(scope);
|
2015-06-26 21:39:43 +00:00
|
|
|
Checkpoint checkpoint(this);
|
2015-06-15 17:06:34 +00:00
|
|
|
{
|
|
|
|
// Parsing patterns as variable reference expression creates
|
|
|
|
// NewUnresolved references in current scope. Entrer arrow function
|
|
|
|
// scope for formal parameter parsing.
|
|
|
|
BlockState block_state(&scope_, scope);
|
|
|
|
if (Check(Token::LPAREN)) {
|
|
|
|
// '(' StrictFormalParameters ')'
|
2015-06-22 14:15:53 +00:00
|
|
|
ParseFormalParameterList(&parsing_state, &formals_classifier, &ok);
|
2015-06-15 17:06:34 +00:00
|
|
|
if (ok) ok = Check(Token::RPAREN);
|
|
|
|
} else {
|
|
|
|
// BindingIdentifier
|
2015-06-22 14:15:53 +00:00
|
|
|
const bool is_rest = false;
|
|
|
|
ParseFormalParameter(is_rest, &parsing_state, &formals_classifier,
|
|
|
|
&ok);
|
2015-06-15 17:06:34 +00:00
|
|
|
}
|
2015-04-21 14:44:18 +00:00
|
|
|
}
|
|
|
|
|
2015-03-20 00:17:41 +00:00
|
|
|
if (ok) {
|
2015-06-26 21:39:43 +00:00
|
|
|
checkpoint.Restore(&parsing_state.materialized_literals_count);
|
2015-05-13 11:45:04 +00:00
|
|
|
Expression* expression =
|
2015-06-22 14:15:53 +00:00
|
|
|
ParseArrowFunctionLiteral(parsing_state, formals_classifier, &ok);
|
2015-04-21 14:44:18 +00:00
|
|
|
if (ok) {
|
|
|
|
// Scanning must end at the same position that was recorded
|
|
|
|
// previously. If not, parsing has been interrupted due to a stack
|
|
|
|
// overflow, at which point the partially parsed arrow function
|
|
|
|
// concise body happens to be a valid expression. This is a problem
|
|
|
|
// only for arrow functions with single expression bodies, since there
|
|
|
|
// is no end token such as "}" for normal functions.
|
|
|
|
if (scanner()->location().end_pos == shared_info->end_position()) {
|
|
|
|
// The pre-parser saw an arrow function here, so the full parser
|
|
|
|
// must produce a FunctionLiteral.
|
|
|
|
DCHECK(expression->IsFunctionLiteral());
|
|
|
|
result = expression->AsFunctionLiteral();
|
|
|
|
} else {
|
|
|
|
ok = false;
|
|
|
|
}
|
2015-03-20 00:17:41 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-13 16:50:03 +00:00
|
|
|
} else if (shared_info->is_default_constructor()) {
|
2015-02-13 22:26:17 +00:00
|
|
|
result = DefaultConstructor(IsSubclassConstructor(shared_info->kind()),
|
2014-11-28 04:08:48 +00:00
|
|
|
scope, shared_info->start_position(),
|
2014-11-13 16:50:03 +00:00
|
|
|
shared_info->end_position());
|
2014-07-21 09:58:01 +00:00
|
|
|
} else {
|
|
|
|
result = ParseFunctionLiteral(raw_name, Scanner::Location::invalid(),
|
2015-07-09 21:31:11 +00:00
|
|
|
kSkipFunctionNameCheck, shared_info->kind(),
|
|
|
|
RelocInfo::kNoPosition, function_type,
|
2014-07-21 09:58:01 +00:00
|
|
|
FunctionLiteral::NORMAL_ARITY, &ok);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
// Make sure the results agree.
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(ok == (result != NULL));
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the target stack is empty.
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(target_stack_ == NULL);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-09-02 11:36:21 +00:00
|
|
|
if (result != NULL) {
|
2011-03-09 16:57:03 +00:00
|
|
|
Handle<String> inferred_name(shared_info->inferred_name());
|
2010-12-07 11:31:57 +00:00
|
|
|
result->set_inferred_name(inferred_name);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-09-30 09:28:58 +00:00
|
|
|
|
2015-01-27 21:06:36 +00:00
|
|
|
void* Parser::ParseStatementList(ZoneList<Statement*>* body, int end_token,
|
2015-04-27 12:13:45 +00:00
|
|
|
bool* ok) {
|
2015-01-27 21:06:36 +00:00
|
|
|
// StatementList ::
|
|
|
|
// (StatementListItem)* <end_token>
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Allocate a target stack to use for this set of source
|
|
|
|
// elements. This way, all scripts and functions get their own
|
|
|
|
// target stack thus avoiding illegal breaks and continues across
|
|
|
|
// functions.
|
2010-10-27 12:33:48 +00:00
|
|
|
TargetScope scope(&this->target_stack_);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2015-01-27 21:06:36 +00:00
|
|
|
DCHECK(body != NULL);
|
2011-01-20 18:51:47 +00:00
|
|
|
bool directive_prologue = true; // Parsing directive prologue.
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
while (peek() != end_token) {
|
2011-01-20 18:51:47 +00:00
|
|
|
if (directive_prologue && peek() != Token::STRING) {
|
|
|
|
directive_prologue = false;
|
|
|
|
}
|
|
|
|
|
2014-02-14 12:13:33 +00:00
|
|
|
Scanner::Location token_loc = scanner()->peek_location();
|
2015-04-22 11:04:25 +00:00
|
|
|
Scanner::Location old_this_loc = function_state_->this_location();
|
|
|
|
Scanner::Location old_super_loc = function_state_->super_location();
|
2015-01-27 21:06:36 +00:00
|
|
|
Statement* stat = ParseStatementListItem(CHECK_OK);
|
2015-03-13 16:26:38 +00:00
|
|
|
|
|
|
|
if (is_strong(language_mode()) &&
|
2015-04-22 11:04:25 +00:00
|
|
|
scope_->is_function_scope() &&
|
|
|
|
i::IsConstructor(function_state_->kind())) {
|
|
|
|
Scanner::Location this_loc = function_state_->this_location();
|
|
|
|
Scanner::Location super_loc = function_state_->super_location();
|
|
|
|
if (this_loc.beg_pos != old_this_loc.beg_pos &&
|
|
|
|
this_loc.beg_pos != token_loc.beg_pos) {
|
2015-05-18 08:34:05 +00:00
|
|
|
ReportMessageAt(this_loc, MessageTemplate::kStrongConstructorThis);
|
2015-04-22 11:04:25 +00:00
|
|
|
*ok = false;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (super_loc.beg_pos != old_super_loc.beg_pos &&
|
|
|
|
super_loc.beg_pos != token_loc.beg_pos) {
|
2015-05-18 08:34:05 +00:00
|
|
|
ReportMessageAt(super_loc, MessageTemplate::kStrongConstructorSuper);
|
2015-04-22 11:04:25 +00:00
|
|
|
*ok = false;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-03-13 16:26:38 +00:00
|
|
|
}
|
|
|
|
|
2011-01-20 18:51:47 +00:00
|
|
|
if (stat == NULL || stat->IsEmpty()) {
|
|
|
|
directive_prologue = false; // End of directive prologue.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (directive_prologue) {
|
|
|
|
// A shot at a directive.
|
2012-01-13 13:09:52 +00:00
|
|
|
ExpressionStatement* e_stat;
|
|
|
|
Literal* literal;
|
2011-01-20 18:51:47 +00:00
|
|
|
// Still processing directive prologue?
|
|
|
|
if ((e_stat = stat->AsExpressionStatement()) != NULL &&
|
|
|
|
(literal = e_stat->expression()->AsLiteral()) != NULL &&
|
2014-06-24 14:03:24 +00:00
|
|
|
literal->raw_value()->IsString()) {
|
2015-02-05 14:11:34 +00:00
|
|
|
// Check "use strict" directive (ES5 14.1), "use asm" directive, and
|
|
|
|
// "use strong" directive (experimental).
|
|
|
|
bool use_strict_found =
|
2014-06-24 14:03:24 +00:00
|
|
|
literal->raw_value()->AsString() ==
|
2014-09-11 09:52:36 +00:00
|
|
|
ast_value_factory()->use_strict_string() &&
|
2014-06-30 13:35:16 +00:00
|
|
|
token_loc.end_pos - token_loc.beg_pos ==
|
2015-02-05 14:11:34 +00:00
|
|
|
ast_value_factory()->use_strict_string()->length() + 2;
|
|
|
|
bool use_strong_found =
|
|
|
|
allow_strong_mode() &&
|
|
|
|
literal->raw_value()->AsString() ==
|
|
|
|
ast_value_factory()->use_strong_string() &&
|
|
|
|
token_loc.end_pos - token_loc.beg_pos ==
|
|
|
|
ast_value_factory()->use_strong_string()->length() + 2;
|
|
|
|
if (use_strict_found || use_strong_found) {
|
|
|
|
// Strong mode implies strict mode. If there are several "use strict"
|
|
|
|
// / "use strong" directives, do the strict mode changes only once.
|
|
|
|
if (is_sloppy(scope_->language_mode())) {
|
|
|
|
scope_->SetLanguageMode(static_cast<LanguageMode>(
|
|
|
|
scope_->language_mode() | STRICT_BIT));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (use_strong_found) {
|
|
|
|
scope_->SetLanguageMode(static_cast<LanguageMode>(
|
|
|
|
scope_->language_mode() | STRONG_BIT));
|
2012-03-15 13:02:21 +00:00
|
|
|
}
|
2015-04-27 12:13:45 +00:00
|
|
|
// Because declarations in strict eval code don't leak into the scope
|
|
|
|
// of the eval call, it is likely that functions declared in strict
|
|
|
|
// eval code will be used within the eval code, so lazy parsing is
|
|
|
|
// probably not a win. Also, resolution of "var" bindings defined in
|
|
|
|
// strict eval code from within nested functions is currently broken
|
|
|
|
// with the pre-parser; lazy parsing of strict eval code causes
|
|
|
|
// regress/regress-crbug-135066.js to fail.
|
|
|
|
if (scope_->is_eval_scope()) mode_ = PARSE_EAGERLY;
|
2014-06-30 13:35:16 +00:00
|
|
|
} else if (literal->raw_value()->AsString() ==
|
2014-09-11 09:52:36 +00:00
|
|
|
ast_value_factory()->use_asm_string() &&
|
2014-06-30 13:35:16 +00:00
|
|
|
token_loc.end_pos - token_loc.beg_pos ==
|
2014-09-11 09:52:36 +00:00
|
|
|
ast_value_factory()->use_asm_string()->length() + 2) {
|
2014-06-30 13:35:16 +00:00
|
|
|
// Store the usage count; The actual use counter on the isolate is
|
|
|
|
// incremented after parsing is done.
|
|
|
|
++use_counts_[v8::Isolate::kUseAsm];
|
2014-09-19 12:50:50 +00:00
|
|
|
scope_->SetAsmModule();
|
2011-01-20 18:51:47 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// End of the directive prologue.
|
|
|
|
directive_prologue = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-27 21:06:36 +00:00
|
|
|
body->Add(stat, zone());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2009-08-19 07:30:20 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-27 21:06:36 +00:00
|
|
|
Statement* Parser::ParseStatementListItem(bool* ok) {
|
|
|
|
// (Ecma 262 6th Edition, 13.1):
|
|
|
|
// StatementListItem:
|
2012-02-20 14:02:59 +00:00
|
|
|
// Statement
|
2015-01-27 21:06:36 +00:00
|
|
|
// Declaration
|
2012-02-20 14:02:59 +00:00
|
|
|
|
2015-04-23 14:05:03 +00:00
|
|
|
if (peek() != Token::CLASS) {
|
|
|
|
// No more classes follow; reset the start position for the consecutive
|
|
|
|
// class declaration group.
|
|
|
|
scope_->set_class_declaration_group_start(-1);
|
|
|
|
}
|
|
|
|
|
2012-02-20 14:02:59 +00:00
|
|
|
switch (peek()) {
|
|
|
|
case Token::FUNCTION:
|
2012-02-29 12:12:52 +00:00
|
|
|
return ParseFunctionDeclaration(NULL, ok);
|
2014-09-16 22:15:39 +00:00
|
|
|
case Token::CLASS:
|
2015-04-23 14:05:03 +00:00
|
|
|
if (scope_->class_declaration_group_start() < 0) {
|
|
|
|
scope_->set_class_declaration_group_start(
|
|
|
|
scanner()->peek_location().beg_pos);
|
|
|
|
}
|
2014-09-16 22:15:39 +00:00
|
|
|
return ParseClassDeclaration(NULL, ok);
|
2014-07-10 14:06:37 +00:00
|
|
|
case Token::CONST:
|
2015-07-07 21:57:09 +00:00
|
|
|
if (allow_const()) {
|
|
|
|
return ParseVariableStatement(kStatementListItem, NULL, ok);
|
|
|
|
}
|
|
|
|
break;
|
2015-01-27 21:06:36 +00:00
|
|
|
case Token::VAR:
|
|
|
|
return ParseVariableStatement(kStatementListItem, NULL, ok);
|
2014-07-10 14:06:37 +00:00
|
|
|
case Token::LET:
|
2015-07-08 15:04:04 +00:00
|
|
|
if (allow_let()) {
|
2015-01-27 21:06:36 +00:00
|
|
|
return ParseVariableStatement(kStatementListItem, NULL, ok);
|
2014-07-10 14:06:37 +00:00
|
|
|
}
|
2015-07-07 21:57:09 +00:00
|
|
|
break;
|
2015-01-27 21:06:36 +00:00
|
|
|
default:
|
2015-07-07 21:57:09 +00:00
|
|
|
break;
|
2012-02-20 14:02:59 +00:00
|
|
|
}
|
2015-07-07 21:57:09 +00:00
|
|
|
return ParseStatement(NULL, ok);
|
2012-02-20 14:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-27 21:06:36 +00:00
|
|
|
Statement* Parser::ParseModuleItem(bool* ok) {
|
|
|
|
// (Ecma 262 6th Edition, 15.2):
|
|
|
|
// ModuleItem :
|
|
|
|
// ImportDeclaration
|
|
|
|
// ExportDeclaration
|
|
|
|
// StatementListItem
|
2012-02-20 14:02:59 +00:00
|
|
|
|
|
|
|
switch (peek()) {
|
2015-01-27 21:06:36 +00:00
|
|
|
case Token::IMPORT:
|
|
|
|
return ParseImportDeclaration(ok);
|
|
|
|
case Token::EXPORT:
|
|
|
|
return ParseExportDeclaration(ok);
|
|
|
|
default:
|
|
|
|
return ParseStatementListItem(ok);
|
2012-02-20 14:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-25 23:00:10 +00:00
|
|
|
void* Parser::ParseModuleItemList(ZoneList<Statement*>* body, bool* ok) {
|
2015-01-27 21:06:36 +00:00
|
|
|
// (Ecma 262 6th Edition, 15.2):
|
|
|
|
// Module :
|
|
|
|
// ModuleBody?
|
|
|
|
//
|
|
|
|
// ModuleBody :
|
|
|
|
// ModuleItem*
|
2012-02-20 14:02:59 +00:00
|
|
|
|
2015-02-24 22:39:26 +00:00
|
|
|
DCHECK(scope_->is_module_scope());
|
|
|
|
scope_->SetLanguageMode(
|
|
|
|
static_cast<LanguageMode>(scope_->language_mode() | STRICT_BIT));
|
2012-02-20 14:02:59 +00:00
|
|
|
|
2015-02-24 22:39:26 +00:00
|
|
|
while (peek() != Token::EOS) {
|
|
|
|
Statement* stat = ParseModuleItem(CHECK_OK);
|
|
|
|
if (stat && !stat->IsEmpty()) {
|
|
|
|
body->Add(stat, zone());
|
2012-02-20 14:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-09 08:59:03 +00:00
|
|
|
// Check that all exports are bound.
|
2015-02-24 22:39:26 +00:00
|
|
|
ModuleDescriptor* descriptor = scope_->module();
|
2015-02-18 18:25:00 +00:00
|
|
|
for (ModuleDescriptor::Iterator it = descriptor->iterator(); !it.done();
|
|
|
|
it.Advance()) {
|
2015-02-24 22:39:26 +00:00
|
|
|
if (scope_->LookupLocal(it.local_name()) == NULL) {
|
2015-02-19 20:14:55 +00:00
|
|
|
// TODO(adamk): Pass both local_name and export_name once ParserTraits
|
|
|
|
// supports multiple arg error messages.
|
|
|
|
// Also try to report this at a better location.
|
2015-05-18 08:34:05 +00:00
|
|
|
ParserTraits::ReportMessage(MessageTemplate::kModuleExportUndefined,
|
|
|
|
it.local_name());
|
2012-07-09 08:59:03 +00:00
|
|
|
*ok = false;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-24 22:39:26 +00:00
|
|
|
scope_->module()->Freeze();
|
|
|
|
return NULL;
|
2012-02-20 14:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-26 18:40:50 +00:00
|
|
|
const AstRawString* Parser::ParseModuleSpecifier(bool* ok) {
|
2015-02-06 17:52:20 +00:00
|
|
|
// ModuleSpecifier :
|
|
|
|
// StringLiteral
|
2012-02-20 14:02:59 +00:00
|
|
|
|
|
|
|
Expect(Token::STRING, CHECK_OK);
|
2015-02-26 18:40:50 +00:00
|
|
|
return GetSymbol(scanner());
|
2012-02-20 14:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-19 20:14:55 +00:00
|
|
|
void* Parser::ParseExportClause(ZoneList<const AstRawString*>* export_names,
|
|
|
|
ZoneList<Scanner::Location>* export_locations,
|
|
|
|
ZoneList<const AstRawString*>* local_names,
|
2015-01-30 03:26:50 +00:00
|
|
|
Scanner::Location* reserved_loc, bool* ok) {
|
|
|
|
// ExportClause :
|
2015-01-28 19:18:37 +00:00
|
|
|
// '{' '}'
|
2015-01-30 03:26:50 +00:00
|
|
|
// '{' ExportsList '}'
|
|
|
|
// '{' ExportsList ',' '}'
|
2015-01-28 19:18:37 +00:00
|
|
|
//
|
2015-01-30 03:26:50 +00:00
|
|
|
// ExportsList :
|
|
|
|
// ExportSpecifier
|
|
|
|
// ExportsList ',' ExportSpecifier
|
2015-01-28 19:18:37 +00:00
|
|
|
//
|
2015-01-30 03:26:50 +00:00
|
|
|
// ExportSpecifier :
|
2015-01-28 19:18:37 +00:00
|
|
|
// IdentifierName
|
|
|
|
// IdentifierName 'as' IdentifierName
|
|
|
|
|
|
|
|
Expect(Token::LBRACE, CHECK_OK);
|
|
|
|
|
2015-01-30 03:26:50 +00:00
|
|
|
Token::Value name_tok;
|
|
|
|
while ((name_tok = peek()) != Token::RBRACE) {
|
|
|
|
// Keep track of the first reserved word encountered in case our
|
|
|
|
// caller needs to report an error.
|
|
|
|
if (!reserved_loc->IsValid() &&
|
|
|
|
!Token::IsIdentifier(name_tok, STRICT, false)) {
|
|
|
|
*reserved_loc = scanner()->location();
|
|
|
|
}
|
2015-02-19 20:14:55 +00:00
|
|
|
const AstRawString* local_name = ParseIdentifierName(CHECK_OK);
|
2015-01-28 19:18:37 +00:00
|
|
|
const AstRawString* export_name = NULL;
|
|
|
|
if (CheckContextualKeyword(CStrVector("as"))) {
|
|
|
|
export_name = ParseIdentifierName(CHECK_OK);
|
|
|
|
}
|
2015-02-19 20:14:55 +00:00
|
|
|
if (export_name == NULL) {
|
|
|
|
export_name = local_name;
|
|
|
|
}
|
|
|
|
export_names->Add(export_name, zone());
|
|
|
|
local_names->Add(local_name, zone());
|
|
|
|
export_locations->Add(scanner()->location(), zone());
|
2015-01-28 19:18:37 +00:00
|
|
|
if (peek() == Token::RBRACE) break;
|
|
|
|
Expect(Token::COMMA, CHECK_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
Expect(Token::RBRACE, CHECK_OK);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-26 18:40:50 +00:00
|
|
|
ZoneList<ImportDeclaration*>* Parser::ParseNamedImports(int pos, bool* ok) {
|
2015-01-30 03:26:50 +00:00
|
|
|
// NamedImports :
|
|
|
|
// '{' '}'
|
|
|
|
// '{' ImportsList '}'
|
|
|
|
// '{' ImportsList ',' '}'
|
|
|
|
//
|
|
|
|
// ImportsList :
|
|
|
|
// ImportSpecifier
|
|
|
|
// ImportsList ',' ImportSpecifier
|
|
|
|
//
|
|
|
|
// ImportSpecifier :
|
|
|
|
// BindingIdentifier
|
|
|
|
// IdentifierName 'as' BindingIdentifier
|
|
|
|
|
|
|
|
Expect(Token::LBRACE, CHECK_OK);
|
|
|
|
|
2015-02-26 18:40:50 +00:00
|
|
|
ZoneList<ImportDeclaration*>* result =
|
|
|
|
new (zone()) ZoneList<ImportDeclaration*>(1, zone());
|
2015-02-25 19:40:39 +00:00
|
|
|
while (peek() != Token::RBRACE) {
|
|
|
|
const AstRawString* import_name = ParseIdentifierName(CHECK_OK);
|
|
|
|
const AstRawString* local_name = import_name;
|
2015-01-30 03:26:50 +00:00
|
|
|
// In the presence of 'as', the left-side of the 'as' can
|
|
|
|
// be any IdentifierName. But without 'as', it must be a valid
|
2015-02-25 19:40:39 +00:00
|
|
|
// BindingIdentifier.
|
2015-01-30 03:26:50 +00:00
|
|
|
if (CheckContextualKeyword(CStrVector("as"))) {
|
2015-02-25 19:40:39 +00:00
|
|
|
local_name = ParseIdentifierName(CHECK_OK);
|
|
|
|
}
|
|
|
|
if (!Token::IsIdentifier(scanner()->current_token(), STRICT, false)) {
|
2015-01-30 03:26:50 +00:00
|
|
|
*ok = false;
|
2015-05-18 08:34:05 +00:00
|
|
|
ReportMessage(MessageTemplate::kUnexpectedReserved);
|
2015-01-30 03:26:50 +00:00
|
|
|
return NULL;
|
2015-02-25 19:40:39 +00:00
|
|
|
} else if (IsEvalOrArguments(local_name)) {
|
2015-01-30 03:26:50 +00:00
|
|
|
*ok = false;
|
2015-05-18 08:34:05 +00:00
|
|
|
ReportMessage(MessageTemplate::kStrictEvalArguments);
|
2015-01-30 03:26:50 +00:00
|
|
|
return NULL;
|
2015-04-10 12:04:51 +00:00
|
|
|
} else if (is_strong(language_mode()) && IsUndefined(local_name)) {
|
|
|
|
*ok = false;
|
2015-05-18 08:34:05 +00:00
|
|
|
ReportMessage(MessageTemplate::kStrongUndefined);
|
2015-04-10 12:04:51 +00:00
|
|
|
return NULL;
|
2015-01-30 03:26:50 +00:00
|
|
|
}
|
2015-02-26 18:40:50 +00:00
|
|
|
VariableProxy* proxy = NewUnresolved(local_name, IMPORT);
|
|
|
|
ImportDeclaration* declaration =
|
|
|
|
factory()->NewImportDeclaration(proxy, import_name, NULL, scope_, pos);
|
2015-06-22 14:15:53 +00:00
|
|
|
Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK);
|
2015-02-26 18:40:50 +00:00
|
|
|
result->Add(declaration, zone());
|
2015-01-30 03:26:50 +00:00
|
|
|
if (peek() == Token::RBRACE) break;
|
|
|
|
Expect(Token::COMMA, CHECK_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
Expect(Token::RBRACE, CHECK_OK);
|
|
|
|
|
2015-02-26 18:40:50 +00:00
|
|
|
return result;
|
2015-01-30 03:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-27 21:06:36 +00:00
|
|
|
Statement* Parser::ParseImportDeclaration(bool* ok) {
|
2015-01-30 03:26:50 +00:00
|
|
|
// ImportDeclaration :
|
|
|
|
// 'import' ImportClause 'from' ModuleSpecifier ';'
|
|
|
|
// 'import' ModuleSpecifier ';'
|
2012-02-29 12:12:52 +00:00
|
|
|
//
|
2015-01-30 03:26:50 +00:00
|
|
|
// ImportClause :
|
|
|
|
// NameSpaceImport
|
|
|
|
// NamedImports
|
|
|
|
// ImportedDefaultBinding
|
|
|
|
// ImportedDefaultBinding ',' NameSpaceImport
|
|
|
|
// ImportedDefaultBinding ',' NamedImports
|
|
|
|
//
|
|
|
|
// NameSpaceImport :
|
|
|
|
// '*' 'as' ImportedBinding
|
2012-02-29 12:12:52 +00:00
|
|
|
|
2013-10-14 09:24:58 +00:00
|
|
|
int pos = peek_position();
|
2012-02-29 12:12:52 +00:00
|
|
|
Expect(Token::IMPORT, CHECK_OK);
|
2015-01-30 03:26:50 +00:00
|
|
|
|
|
|
|
Token::Value tok = peek();
|
|
|
|
|
|
|
|
// 'import' ModuleSpecifier ';'
|
|
|
|
if (tok == Token::STRING) {
|
2015-02-26 18:40:50 +00:00
|
|
|
const AstRawString* module_specifier = ParseModuleSpecifier(CHECK_OK);
|
2015-04-17 22:45:15 +00:00
|
|
|
scope_->module()->AddModuleRequest(module_specifier, zone());
|
2015-01-30 03:26:50 +00:00
|
|
|
ExpectSemicolon(CHECK_OK);
|
|
|
|
return factory()->NewEmptyStatement(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse ImportedDefaultBinding if present.
|
2015-02-27 18:05:56 +00:00
|
|
|
ImportDeclaration* import_default_declaration = NULL;
|
2015-01-30 03:26:50 +00:00
|
|
|
if (tok != Token::MUL && tok != Token::LBRACE) {
|
2015-02-27 18:05:56 +00:00
|
|
|
const AstRawString* local_name =
|
2015-04-10 12:04:51 +00:00
|
|
|
ParseIdentifier(kDontAllowRestrictedIdentifiers, CHECK_OK);
|
2015-02-27 18:05:56 +00:00
|
|
|
VariableProxy* proxy = NewUnresolved(local_name, IMPORT);
|
|
|
|
import_default_declaration = factory()->NewImportDeclaration(
|
|
|
|
proxy, ast_value_factory()->default_string(), NULL, scope_, pos);
|
2015-06-22 14:15:53 +00:00
|
|
|
Declare(import_default_declaration, DeclarationDescriptor::NORMAL, true,
|
|
|
|
CHECK_OK);
|
2015-01-30 03:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const AstRawString* module_instance_binding = NULL;
|
2015-02-26 18:40:50 +00:00
|
|
|
ZoneList<ImportDeclaration*>* named_declarations = NULL;
|
2015-02-27 18:05:56 +00:00
|
|
|
if (import_default_declaration == NULL || Check(Token::COMMA)) {
|
2015-01-30 03:26:50 +00:00
|
|
|
switch (peek()) {
|
|
|
|
case Token::MUL: {
|
|
|
|
Consume(Token::MUL);
|
|
|
|
ExpectContextualKeyword(CStrVector("as"), CHECK_OK);
|
|
|
|
module_instance_binding =
|
2015-04-10 12:04:51 +00:00
|
|
|
ParseIdentifier(kDontAllowRestrictedIdentifiers, CHECK_OK);
|
2015-02-26 18:40:50 +00:00
|
|
|
// TODO(ES6): Add an appropriate declaration.
|
2015-01-30 03:26:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-02-29 12:12:52 +00:00
|
|
|
|
2015-01-30 03:26:50 +00:00
|
|
|
case Token::LBRACE:
|
2015-02-26 18:40:50 +00:00
|
|
|
named_declarations = ParseNamedImports(pos, CHECK_OK);
|
2015-01-30 03:26:50 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
*ok = false;
|
|
|
|
ReportUnexpectedToken(scanner()->current_token());
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-02-29 12:12:52 +00:00
|
|
|
}
|
|
|
|
|
2013-06-06 14:38:26 +00:00
|
|
|
ExpectContextualKeyword(CStrVector("from"), CHECK_OK);
|
2015-02-26 18:40:50 +00:00
|
|
|
const AstRawString* module_specifier = ParseModuleSpecifier(CHECK_OK);
|
2015-04-09 22:09:44 +00:00
|
|
|
scope_->module()->AddModuleRequest(module_specifier, zone());
|
|
|
|
|
2015-01-30 03:26:50 +00:00
|
|
|
if (module_instance_binding != NULL) {
|
2015-02-26 18:40:50 +00:00
|
|
|
// TODO(ES6): Set the module specifier for the module namespace binding.
|
2015-01-30 03:26:50 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 18:05:56 +00:00
|
|
|
if (import_default_declaration != NULL) {
|
|
|
|
import_default_declaration->set_module_specifier(module_specifier);
|
2015-01-30 03:26:50 +00:00
|
|
|
}
|
2015-01-27 21:06:36 +00:00
|
|
|
|
2015-02-26 18:40:50 +00:00
|
|
|
if (named_declarations != NULL) {
|
|
|
|
for (int i = 0; i < named_declarations->length(); ++i) {
|
|
|
|
named_declarations->at(i)->set_module_specifier(module_specifier);
|
|
|
|
}
|
2012-02-29 12:12:52 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 22:45:15 +00:00
|
|
|
ExpectSemicolon(CHECK_OK);
|
2015-01-27 21:06:36 +00:00
|
|
|
return factory()->NewEmptyStatement(pos);
|
2012-02-20 14:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-28 19:18:37 +00:00
|
|
|
Statement* Parser::ParseExportDefault(bool* ok) {
|
|
|
|
// Supports the following productions, starting after the 'default' token:
|
|
|
|
// 'export' 'default' FunctionDeclaration
|
|
|
|
// 'export' 'default' ClassDeclaration
|
|
|
|
// 'export' 'default' AssignmentExpression[In] ';'
|
|
|
|
|
2015-02-19 20:14:55 +00:00
|
|
|
Expect(Token::DEFAULT, CHECK_OK);
|
|
|
|
Scanner::Location default_loc = scanner()->location();
|
|
|
|
|
|
|
|
ZoneList<const AstRawString*> names(1, zone());
|
2015-01-28 19:18:37 +00:00
|
|
|
Statement* result = NULL;
|
|
|
|
switch (peek()) {
|
|
|
|
case Token::FUNCTION:
|
|
|
|
// TODO(ES6): Support parsing anonymous function declarations here.
|
2015-02-19 20:14:55 +00:00
|
|
|
result = ParseFunctionDeclaration(&names, CHECK_OK);
|
2015-01-28 19:18:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Token::CLASS:
|
|
|
|
// TODO(ES6): Support parsing anonymous class declarations here.
|
2015-02-19 20:14:55 +00:00
|
|
|
result = ParseClassDeclaration(&names, CHECK_OK);
|
2015-01-28 19:18:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default: {
|
|
|
|
int pos = peek_position();
|
2015-04-22 12:35:05 +00:00
|
|
|
ExpressionClassifier classifier;
|
|
|
|
Expression* expr = ParseAssignmentExpression(true, &classifier, CHECK_OK);
|
2015-04-27 14:35:45 +00:00
|
|
|
ValidateExpression(&classifier, CHECK_OK);
|
2015-04-22 12:35:05 +00:00
|
|
|
|
2015-01-28 19:18:37 +00:00
|
|
|
ExpectSemicolon(CHECK_OK);
|
|
|
|
result = factory()->NewExpressionStatement(expr, pos);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-19 20:14:55 +00:00
|
|
|
const AstRawString* default_string = ast_value_factory()->default_string();
|
|
|
|
|
|
|
|
DCHECK_LE(names.length(), 1);
|
|
|
|
if (names.length() == 1) {
|
|
|
|
scope_->module()->AddLocalExport(default_string, names.first(), zone(), ok);
|
|
|
|
if (!*ok) {
|
2015-05-18 08:34:05 +00:00
|
|
|
ParserTraits::ReportMessageAt(
|
|
|
|
default_loc, MessageTemplate::kDuplicateExport, default_string);
|
2015-02-19 20:14:55 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO(ES6): Assign result to a const binding with the name "*default*"
|
|
|
|
// and add an export entry with "*default*" as the local name.
|
|
|
|
}
|
2015-01-28 19:18:37 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-29 12:12:52 +00:00
|
|
|
Statement* Parser::ParseExportDeclaration(bool* ok) {
|
|
|
|
// ExportDeclaration:
|
2015-01-28 19:18:37 +00:00
|
|
|
// 'export' '*' 'from' ModuleSpecifier ';'
|
|
|
|
// 'export' ExportClause ('from' ModuleSpecifier)? ';'
|
|
|
|
// 'export' VariableStatement
|
|
|
|
// 'export' Declaration
|
|
|
|
// 'export' 'default' ... (handled in ParseExportDefault)
|
2012-02-29 12:12:52 +00:00
|
|
|
|
2015-01-28 19:18:37 +00:00
|
|
|
int pos = peek_position();
|
2012-02-29 12:12:52 +00:00
|
|
|
Expect(Token::EXPORT, CHECK_OK);
|
|
|
|
|
|
|
|
Statement* result = NULL;
|
2014-06-24 14:03:24 +00:00
|
|
|
ZoneList<const AstRawString*> names(1, zone());
|
2012-02-29 12:12:52 +00:00
|
|
|
switch (peek()) {
|
2015-01-28 19:18:37 +00:00
|
|
|
case Token::DEFAULT:
|
|
|
|
return ParseExportDefault(ok);
|
|
|
|
|
|
|
|
case Token::MUL: {
|
|
|
|
Consume(Token::MUL);
|
|
|
|
ExpectContextualKeyword(CStrVector("from"), CHECK_OK);
|
2015-02-26 18:40:50 +00:00
|
|
|
const AstRawString* module_specifier = ParseModuleSpecifier(CHECK_OK);
|
2015-04-17 22:45:15 +00:00
|
|
|
scope_->module()->AddModuleRequest(module_specifier, zone());
|
2015-02-19 20:14:55 +00:00
|
|
|
// TODO(ES6): scope_->module()->AddStarExport(...)
|
2015-04-17 22:45:15 +00:00
|
|
|
ExpectSemicolon(CHECK_OK);
|
2015-02-19 20:14:55 +00:00
|
|
|
return factory()->NewEmptyStatement(pos);
|
2012-02-29 12:12:52 +00:00
|
|
|
}
|
|
|
|
|
2015-01-30 03:26:50 +00:00
|
|
|
case Token::LBRACE: {
|
|
|
|
// There are two cases here:
|
|
|
|
//
|
|
|
|
// 'export' ExportClause ';'
|
|
|
|
// and
|
|
|
|
// 'export' ExportClause FromClause ';'
|
|
|
|
//
|
|
|
|
// In the first case, the exported identifiers in ExportClause must
|
|
|
|
// not be reserved words, while in the latter they may be. We
|
|
|
|
// pass in a location that gets filled with the first reserved word
|
|
|
|
// encountered, and then throw a SyntaxError if we are in the
|
|
|
|
// non-FromClause case.
|
|
|
|
Scanner::Location reserved_loc = Scanner::Location::invalid();
|
2015-02-19 20:14:55 +00:00
|
|
|
ZoneList<const AstRawString*> export_names(1, zone());
|
|
|
|
ZoneList<Scanner::Location> export_locations(1, zone());
|
|
|
|
ZoneList<const AstRawString*> local_names(1, zone());
|
|
|
|
ParseExportClause(&export_names, &export_locations, &local_names,
|
|
|
|
&reserved_loc, CHECK_OK);
|
2015-02-26 18:40:50 +00:00
|
|
|
const AstRawString* indirect_export_module_specifier = NULL;
|
2015-01-28 19:18:37 +00:00
|
|
|
if (CheckContextualKeyword(CStrVector("from"))) {
|
2015-02-19 20:14:55 +00:00
|
|
|
indirect_export_module_specifier = ParseModuleSpecifier(CHECK_OK);
|
2015-01-30 03:26:50 +00:00
|
|
|
} else if (reserved_loc.IsValid()) {
|
|
|
|
// No FromClause, so reserved words are invalid in ExportClause.
|
|
|
|
*ok = false;
|
2015-05-18 08:34:05 +00:00
|
|
|
ReportMessageAt(reserved_loc, MessageTemplate::kUnexpectedReserved);
|
2015-01-30 03:26:50 +00:00
|
|
|
return NULL;
|
2015-01-28 19:18:37 +00:00
|
|
|
}
|
|
|
|
ExpectSemicolon(CHECK_OK);
|
2015-02-19 20:14:55 +00:00
|
|
|
const int length = export_names.length();
|
|
|
|
DCHECK_EQ(length, local_names.length());
|
|
|
|
DCHECK_EQ(length, export_locations.length());
|
|
|
|
if (indirect_export_module_specifier == NULL) {
|
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
scope_->module()->AddLocalExport(export_names[i], local_names[i],
|
|
|
|
zone(), ok);
|
|
|
|
if (!*ok) {
|
|
|
|
ParserTraits::ReportMessageAt(export_locations[i],
|
2015-05-18 08:34:05 +00:00
|
|
|
MessageTemplate::kDuplicateExport,
|
|
|
|
export_names[i]);
|
2015-02-19 20:14:55 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2015-04-09 22:09:44 +00:00
|
|
|
scope_->module()->AddModuleRequest(indirect_export_module_specifier,
|
|
|
|
zone());
|
2015-02-19 20:14:55 +00:00
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
// TODO(ES6): scope_->module()->AddIndirectExport(...);(
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return factory()->NewEmptyStatement(pos);
|
2015-01-30 03:26:50 +00:00
|
|
|
}
|
2015-01-28 19:18:37 +00:00
|
|
|
|
2012-02-29 12:12:52 +00:00
|
|
|
case Token::FUNCTION:
|
|
|
|
result = ParseFunctionDeclaration(&names, CHECK_OK);
|
|
|
|
break;
|
|
|
|
|
2014-09-16 22:15:39 +00:00
|
|
|
case Token::CLASS:
|
|
|
|
result = ParseClassDeclaration(&names, CHECK_OK);
|
|
|
|
break;
|
|
|
|
|
2012-02-29 12:12:52 +00:00
|
|
|
case Token::VAR:
|
|
|
|
case Token::LET:
|
|
|
|
case Token::CONST:
|
2015-01-27 21:06:36 +00:00
|
|
|
result = ParseVariableStatement(kStatementListItem, &names, CHECK_OK);
|
2012-02-29 12:12:52 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
*ok = false;
|
2014-02-14 12:13:33 +00:00
|
|
|
ReportUnexpectedToken(scanner()->current_token());
|
2012-02-29 12:12:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-02-19 20:14:55 +00:00
|
|
|
// Extract declared names into export declarations.
|
|
|
|
ModuleDescriptor* descriptor = scope_->module();
|
2014-07-30 13:54:45 +00:00
|
|
|
for (int i = 0; i < names.length(); ++i) {
|
2015-02-19 20:14:55 +00:00
|
|
|
descriptor->AddLocalExport(names[i], names[i], zone(), ok);
|
|
|
|
if (!*ok) {
|
|
|
|
// TODO(adamk): Possibly report this error at the right place.
|
2015-05-18 08:34:05 +00:00
|
|
|
ParserTraits::ReportMessage(MessageTemplate::kDuplicateExport, names[i]);
|
2015-02-19 20:14:55 +00:00
|
|
|
return NULL;
|
2015-01-28 19:18:37 +00:00
|
|
|
}
|
2012-02-29 12:12:52 +00:00
|
|
|
}
|
|
|
|
|
2015-02-19 20:14:55 +00:00
|
|
|
DCHECK_NOT_NULL(result);
|
2012-02-29 12:12:52 +00:00
|
|
|
return result;
|
2012-02-20 14:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
Statement* Parser::ParseStatement(ZoneList<const AstRawString*>* labels,
|
|
|
|
bool* ok) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// Statement ::
|
2015-02-17 16:25:49 +00:00
|
|
|
// EmptyStatement
|
|
|
|
// ...
|
|
|
|
|
|
|
|
if (peek() == Token::SEMICOLON) {
|
|
|
|
Next();
|
|
|
|
return factory()->NewEmptyStatement(RelocInfo::kNoPosition);
|
|
|
|
}
|
|
|
|
return ParseSubStatement(labels, ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Statement* Parser::ParseSubStatement(ZoneList<const AstRawString*>* labels,
|
|
|
|
bool* ok) {
|
|
|
|
// Statement ::
|
2008-07-03 15:10:15 +00:00
|
|
|
// Block
|
|
|
|
// VariableStatement
|
|
|
|
// EmptyStatement
|
|
|
|
// ExpressionStatement
|
|
|
|
// IfStatement
|
|
|
|
// IterationStatement
|
|
|
|
// ContinueStatement
|
|
|
|
// BreakStatement
|
|
|
|
// ReturnStatement
|
|
|
|
// WithStatement
|
|
|
|
// LabelledStatement
|
|
|
|
// SwitchStatement
|
|
|
|
// ThrowStatement
|
|
|
|
// TryStatement
|
|
|
|
// DebuggerStatement
|
|
|
|
|
|
|
|
// Note: Since labels can only be used by 'break' and 'continue'
|
|
|
|
// statements, which themselves are only valid within blocks,
|
|
|
|
// iterations or 'switch' statements (i.e., BreakableStatements),
|
|
|
|
// labels can be simply ignored in all other cases; except for
|
2009-01-15 19:08:34 +00:00
|
|
|
// trivial labeled break statements 'label: break label' which is
|
2008-07-03 15:10:15 +00:00
|
|
|
// parsed into an empty statement.
|
|
|
|
switch (peek()) {
|
|
|
|
case Token::LBRACE:
|
|
|
|
return ParseBlock(labels, ok);
|
|
|
|
|
|
|
|
case Token::SEMICOLON:
|
2015-02-17 16:25:49 +00:00
|
|
|
if (is_strong(language_mode())) {
|
2015-05-18 08:34:05 +00:00
|
|
|
ReportMessageAt(scanner()->peek_location(),
|
|
|
|
MessageTemplate::kStrongEmpty);
|
2015-02-17 16:25:49 +00:00
|
|
|
*ok = false;
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
Next();
|
2013-10-14 09:24:58 +00:00
|
|
|
return factory()->NewEmptyStatement(RelocInfo::kNoPosition);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
case Token::IF:
|
2013-10-14 09:24:58 +00:00
|
|
|
return ParseIfStatement(labels, ok);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
case Token::DO:
|
2013-10-14 09:24:58 +00:00
|
|
|
return ParseDoWhileStatement(labels, ok);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
case Token::WHILE:
|
2013-10-14 09:24:58 +00:00
|
|
|
return ParseWhileStatement(labels, ok);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
case Token::FOR:
|
2013-10-14 09:24:58 +00:00
|
|
|
return ParseForStatement(labels, ok);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
case Token::CONTINUE:
|
|
|
|
case Token::BREAK:
|
|
|
|
case Token::RETURN:
|
2015-04-16 13:29:29 +00:00
|
|
|
case Token::THROW:
|
|
|
|
case Token::TRY: {
|
|
|
|
// These statements must have their labels preserved in an enclosing
|
|
|
|
// block
|
|
|
|
if (labels == NULL) {
|
|
|
|
return ParseStatementAsUnlabelled(labels, ok);
|
|
|
|
} else {
|
|
|
|
Block* result =
|
|
|
|
factory()->NewBlock(labels, 1, false, RelocInfo::kNoPosition);
|
|
|
|
Target target(&this->target_stack_, result);
|
|
|
|
Statement* statement = ParseStatementAsUnlabelled(labels, CHECK_OK);
|
|
|
|
if (result) result->AddStatement(statement, zone());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
case Token::WITH:
|
2013-10-14 09:24:58 +00:00
|
|
|
return ParseWithStatement(labels, ok);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
case Token::SWITCH:
|
2013-10-14 09:24:58 +00:00
|
|
|
return ParseSwitchStatement(labels, ok);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-02-28 19:07:02 +00:00
|
|
|
case Token::FUNCTION: {
|
2011-09-21 12:27:07 +00:00
|
|
|
// FunctionDeclaration is only allowed in the context of SourceElements
|
|
|
|
// (Ecma 262 5th Edition, clause 14):
|
|
|
|
// SourceElement:
|
|
|
|
// Statement
|
|
|
|
// FunctionDeclaration
|
|
|
|
// Common language extension is to allow function declaration in place
|
|
|
|
// of any statement. This language extension is disabled in strict mode.
|
2013-04-02 17:34:59 +00:00
|
|
|
//
|
|
|
|
// In Harmony mode, this case also handles the extension:
|
|
|
|
// Statement:
|
|
|
|
// GeneratorDeclaration
|
2015-02-04 09:34:05 +00:00
|
|
|
if (is_strict(language_mode())) {
|
2015-05-18 08:34:05 +00:00
|
|
|
ReportMessageAt(scanner()->peek_location(),
|
|
|
|
MessageTemplate::kStrictFunction);
|
2011-02-28 19:07:02 +00:00
|
|
|
*ok = false;
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-02-29 12:12:52 +00:00
|
|
|
return ParseFunctionDeclaration(NULL, ok);
|
2011-02-28 19:07:02 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
case Token::DEBUGGER:
|
2013-10-14 09:24:58 +00:00
|
|
|
return ParseDebuggerStatement(ok);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-07-10 14:06:37 +00:00
|
|
|
case Token::VAR:
|
|
|
|
return ParseVariableStatement(kStatement, NULL, ok);
|
|
|
|
|
2015-01-30 03:09:57 +00:00
|
|
|
case Token::CONST:
|
|
|
|
// In ES6 CONST is not allowed as a Statement, only as a
|
|
|
|
// LexicalDeclaration, however we continue to allow it in sloppy mode for
|
|
|
|
// backwards compatibility.
|
2015-07-07 21:57:09 +00:00
|
|
|
if (is_sloppy(language_mode()) && allow_legacy_const()) {
|
2014-07-10 14:06:37 +00:00
|
|
|
return ParseVariableStatement(kStatement, NULL, ok);
|
|
|
|
}
|
2015-01-30 03:09:57 +00:00
|
|
|
|
|
|
|
// Fall through.
|
2008-07-03 15:10:15 +00:00
|
|
|
default:
|
2013-10-14 09:24:58 +00:00
|
|
|
return ParseExpressionOrLabelledStatement(labels, ok);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 13:29:29 +00:00
|
|
|
Statement* Parser::ParseStatementAsUnlabelled(
|
|
|
|
ZoneList<const AstRawString*>* labels, bool* ok) {
|
|
|
|
switch (peek()) {
|
|
|
|
case Token::CONTINUE:
|
|
|
|
return ParseContinueStatement(ok);
|
|
|
|
|
|
|
|
case Token::BREAK:
|
|
|
|
return ParseBreakStatement(labels, ok);
|
|
|
|
|
|
|
|
case Token::RETURN:
|
|
|
|
return ParseReturnStatement(ok);
|
|
|
|
|
|
|
|
case Token::THROW:
|
|
|
|
return ParseThrowStatement(ok);
|
|
|
|
|
|
|
|
case Token::TRY:
|
|
|
|
return ParseTryStatement(ok);
|
|
|
|
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
VariableProxy* Parser::NewUnresolved(const AstRawString* name,
|
2015-02-17 20:51:24 +00:00
|
|
|
VariableMode mode) {
|
2011-08-16 14:24:12 +00:00
|
|
|
// If we are inside a function, a declaration of a var/const variable is a
|
|
|
|
// truly local variable, and the scope of the variable is always the function
|
|
|
|
// scope.
|
2011-10-25 08:33:08 +00:00
|
|
|
// Let/const variables in harmony mode are always added to the immediately
|
|
|
|
// enclosing scope.
|
2015-05-19 14:51:10 +00:00
|
|
|
return DeclarationScope(mode)->NewUnresolved(
|
|
|
|
factory(), name, Variable::NORMAL, scanner()->location().beg_pos,
|
|
|
|
scanner()->location().end_pos);
|
2012-02-28 10:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-22 14:15:53 +00:00
|
|
|
Variable* Parser::Declare(Declaration* declaration,
|
|
|
|
DeclarationDescriptor::Kind declaration_kind,
|
|
|
|
bool resolve, bool* ok) {
|
2012-03-08 13:03:07 +00:00
|
|
|
VariableProxy* proxy = declaration->proxy();
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(proxy->raw_name() != NULL);
|
2014-06-24 14:03:24 +00:00
|
|
|
const AstRawString* name = proxy->raw_name();
|
2012-02-28 10:12:39 +00:00
|
|
|
VariableMode mode = declaration->mode();
|
|
|
|
Scope* declaration_scope = DeclarationScope(mode);
|
|
|
|
Variable* var = NULL;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2012-07-09 08:59:03 +00:00
|
|
|
// If a suitable scope exists, then we can statically declare this
|
2008-07-03 15:10:15 +00:00
|
|
|
// variable and also set its mode. In any case, a Declaration node
|
|
|
|
// will be added to the scope so that the declaration can be added
|
|
|
|
// to the corresponding activation frame at runtime if necessary.
|
|
|
|
// For instance declarations inside an eval scope need to be added
|
|
|
|
// to the calling function context.
|
2011-04-29 15:31:39 +00:00
|
|
|
// Similarly, strict mode eval scope does not leak variable declarations to
|
|
|
|
// the caller's scope so we declare all locals, too.
|
2011-06-30 14:37:55 +00:00
|
|
|
if (declaration_scope->is_function_scope() ||
|
2014-03-11 14:41:22 +00:00
|
|
|
declaration_scope->is_strict_eval_scope() ||
|
2012-02-20 14:02:59 +00:00
|
|
|
declaration_scope->is_block_scope() ||
|
2012-03-08 13:03:07 +00:00
|
|
|
declaration_scope->is_module_scope() ||
|
2014-11-12 11:34:09 +00:00
|
|
|
declaration_scope->is_script_scope()) {
|
2012-07-09 08:59:03 +00:00
|
|
|
// Declare the variable in the declaration scope.
|
2014-11-12 11:34:09 +00:00
|
|
|
var = declaration_scope->LookupLocal(name);
|
2008-07-03 15:10:15 +00:00
|
|
|
if (var == NULL) {
|
|
|
|
// Declare the name.
|
2015-04-16 14:13:03 +00:00
|
|
|
Variable::Kind kind = Variable::NORMAL;
|
2015-04-23 14:05:03 +00:00
|
|
|
int declaration_group_start = -1;
|
2015-04-16 14:13:03 +00:00
|
|
|
if (declaration->IsFunctionDeclaration()) {
|
|
|
|
kind = Variable::FUNCTION;
|
|
|
|
} else if (declaration->IsVariableDeclaration() &&
|
|
|
|
declaration->AsVariableDeclaration()->is_class_declaration()) {
|
|
|
|
kind = Variable::CLASS;
|
2015-04-23 14:05:03 +00:00
|
|
|
declaration_group_start =
|
|
|
|
declaration->AsVariableDeclaration()->declaration_group_start();
|
2015-04-16 14:13:03 +00:00
|
|
|
}
|
2015-02-17 20:51:24 +00:00
|
|
|
var = declaration_scope->DeclareLocal(
|
2015-04-23 14:05:03 +00:00
|
|
|
name, mode, declaration->initialization(), kind, kNotAssigned,
|
|
|
|
declaration_group_start);
|
2015-02-26 13:48:10 +00:00
|
|
|
} else if (IsLexicalVariableMode(mode) ||
|
|
|
|
IsLexicalVariableMode(var->mode()) ||
|
|
|
|
((mode == CONST_LEGACY || var->mode() == CONST_LEGACY) &&
|
|
|
|
!declaration_scope->is_script_scope())) {
|
2011-09-01 12:31:18 +00:00
|
|
|
// The name was declared in this scope before; check for conflicting
|
|
|
|
// re-declarations. We have a conflict if either of the declarations is
|
2014-11-12 11:34:09 +00:00
|
|
|
// not a var (in script scope, we also have to ignore legacy const for
|
2012-08-28 11:25:08 +00:00
|
|
|
// compatibility). There is similar code in runtime.cc in the Declare
|
2014-07-09 11:35:05 +00:00
|
|
|
// functions. The function CheckConflictingVarDeclarations checks for
|
2011-09-01 12:31:18 +00:00
|
|
|
// var and let bindings from different scopes whereas this is a check for
|
|
|
|
// conflicting declarations within the same scope. This check also covers
|
2012-08-28 11:25:08 +00:00
|
|
|
// the special case
|
2011-09-01 12:31:18 +00:00
|
|
|
//
|
|
|
|
// function () { let x; { var x; } }
|
|
|
|
//
|
|
|
|
// because the var declaration is hoisted to the function scope where 'x'
|
|
|
|
// is already bound.
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(IsDeclaredVariableMode(var->mode()));
|
2015-07-08 15:04:04 +00:00
|
|
|
if (is_strict(language_mode()) || allow_harmony_sloppy()) {
|
2014-03-11 14:41:22 +00:00
|
|
|
// In harmony we treat re-declarations as early errors. See
|
2012-08-28 11:25:08 +00:00
|
|
|
// ES5 16 for a definition of early errors.
|
2015-06-22 14:15:53 +00:00
|
|
|
if (declaration_kind == DeclarationDescriptor::NORMAL) {
|
|
|
|
ParserTraits::ReportMessage(MessageTemplate::kVarRedeclaration, name);
|
|
|
|
} else {
|
2015-07-14 21:58:49 +00:00
|
|
|
ParserTraits::ReportMessage(MessageTemplate::kParamDupe);
|
2015-06-22 14:15:53 +00:00
|
|
|
}
|
2012-08-28 11:25:08 +00:00
|
|
|
*ok = false;
|
2015-02-26 13:48:10 +00:00
|
|
|
return nullptr;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2015-06-22 14:15:53 +00:00
|
|
|
Expression* expression = NewThrowSyntaxError(
|
2015-05-18 08:34:05 +00:00
|
|
|
MessageTemplate::kVarRedeclaration, name, declaration->position());
|
2012-08-28 11:25:08 +00:00
|
|
|
declaration_scope->SetIllegalRedeclaration(expression);
|
2014-06-26 11:59:42 +00:00
|
|
|
} else if (mode == VAR) {
|
|
|
|
var->set_maybe_assigned();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We add a declaration node for every declaration. The compiler
|
|
|
|
// will only generate code if necessary. In particular, declarations
|
|
|
|
// for inner local variables that do not represent functions won't
|
|
|
|
// result in any generated code.
|
|
|
|
//
|
|
|
|
// Note that we always add an unresolved proxy even if it's not
|
|
|
|
// used, simply because we don't know in this method (w/o extra
|
|
|
|
// parameters) if the proxy is needed or not. The proxy will be
|
|
|
|
// bound during variable resolution time unless it was pre-bound
|
|
|
|
// below.
|
|
|
|
//
|
|
|
|
// WARNING: This will lead to multiple declaration nodes for the
|
|
|
|
// same variable if it is declared several times. This is not a
|
|
|
|
// semantic issue as long as we keep the source order, but it may be
|
|
|
|
// a performance issue since it may lead to repeated
|
2014-07-14 14:01:04 +00:00
|
|
|
// RuntimeHidden_DeclareLookupSlot calls.
|
2012-02-28 10:12:39 +00:00
|
|
|
declaration_scope->AddDeclaration(declaration);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-11-12 11:34:09 +00:00
|
|
|
if (mode == CONST_LEGACY && declaration_scope->is_script_scope()) {
|
2011-11-15 13:48:40 +00:00
|
|
|
// For global const variables we bind the proxy to a variable.
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(resolve); // should be set by all callers
|
2009-06-24 08:01:38 +00:00
|
|
|
Variable::Kind kind = Variable::NORMAL;
|
2015-03-24 13:08:26 +00:00
|
|
|
var = new (zone()) Variable(declaration_scope, name, mode, kind,
|
2015-02-17 20:51:24 +00:00
|
|
|
kNeedsInitialization, kNotAssigned);
|
2011-11-15 13:48:40 +00:00
|
|
|
} else if (declaration_scope->is_eval_scope() &&
|
2015-02-04 09:34:05 +00:00
|
|
|
is_sloppy(declaration_scope->language_mode())) {
|
2014-03-11 14:39:08 +00:00
|
|
|
// For variable declarations in a sloppy eval scope the proxy is bound
|
2011-11-15 13:48:40 +00:00
|
|
|
// to a lookup variable to force a dynamic declaration using the
|
2014-07-14 14:01:04 +00:00
|
|
|
// DeclareLookupSlot runtime function.
|
2011-11-15 13:48:40 +00:00
|
|
|
Variable::Kind kind = Variable::NORMAL;
|
2014-07-30 13:54:45 +00:00
|
|
|
// TODO(sigurds) figure out if kNotAssigned is OK here
|
2015-03-24 13:08:26 +00:00
|
|
|
var = new (zone()) Variable(declaration_scope, name, mode, kind,
|
2015-02-17 20:51:24 +00:00
|
|
|
declaration->initialization(), kNotAssigned);
|
2015-07-06 16:36:28 +00:00
|
|
|
var->AllocateTo(VariableLocation::LOOKUP, -1);
|
2011-11-15 13:48:40 +00:00
|
|
|
resolve = true;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If requested and we have a local variable, bind the proxy to the variable
|
|
|
|
// at parse-time. This is used for functions (and consts) declared inside
|
|
|
|
// statements: the corresponding function (or const) variable must be in the
|
|
|
|
// function scope and not a statement-local scope, e.g. as provided with a
|
|
|
|
// 'with' statement:
|
|
|
|
//
|
|
|
|
// with (obj) {
|
|
|
|
// function f() {}
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// which is translated into:
|
|
|
|
//
|
|
|
|
// with (obj) {
|
|
|
|
// // in this case this is not: 'var f; f = function () {};'
|
|
|
|
// var f = function () {};
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Note that if 'f' is accessed from inside the 'with' statement, it
|
|
|
|
// will be allocated in the context (because we must be able to look
|
|
|
|
// it up dynamically) but it will also be accessed statically, i.e.,
|
|
|
|
// with a context slot index and a context chain length for this
|
|
|
|
// initialization code. Thus, inside the 'with' statement, we need
|
|
|
|
// both access to the static and the dynamic context chain; the
|
|
|
|
// runtime needs to provide both.
|
2012-03-08 13:03:07 +00:00
|
|
|
if (resolve && var != NULL) {
|
|
|
|
proxy->BindTo(var);
|
|
|
|
}
|
2015-02-26 13:48:10 +00:00
|
|
|
return var;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Language extension which is only enabled for source files loaded
|
|
|
|
// through the API's extension mechanism. A native function
|
|
|
|
// declaration is resolved by looking up the function through a
|
|
|
|
// callback provided by the extension.
|
|
|
|
Statement* Parser::ParseNativeDeclaration(bool* ok) {
|
2013-10-14 09:24:58 +00:00
|
|
|
int pos = peek_position();
|
2008-07-03 15:10:15 +00:00
|
|
|
Expect(Token::FUNCTION, CHECK_OK);
|
2014-02-05 16:26:48 +00:00
|
|
|
// Allow "eval" or "arguments" for backward compatibility.
|
2015-04-10 12:04:51 +00:00
|
|
|
const AstRawString* name =
|
|
|
|
ParseIdentifier(kAllowRestrictedIdentifiers, CHECK_OK);
|
2008-07-03 15:10:15 +00:00
|
|
|
Expect(Token::LPAREN, CHECK_OK);
|
|
|
|
bool done = (peek() == Token::RPAREN);
|
|
|
|
while (!done) {
|
2015-04-10 12:04:51 +00:00
|
|
|
ParseIdentifier(kAllowRestrictedIdentifiers, CHECK_OK);
|
2008-07-03 15:10:15 +00:00
|
|
|
done = (peek() == Token::RPAREN);
|
2010-11-02 07:21:37 +00:00
|
|
|
if (!done) {
|
|
|
|
Expect(Token::COMMA, CHECK_OK);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
Expect(Token::RPAREN, CHECK_OK);
|
|
|
|
Expect(Token::SEMICOLON, CHECK_OK);
|
|
|
|
|
|
|
|
// Make sure that the function containing the native declaration
|
|
|
|
// isn't lazily compiled. The extension structures are only
|
|
|
|
// accessible while parsing the first time not when reparsing
|
|
|
|
// because of lazy compilation.
|
2012-02-28 10:12:39 +00:00
|
|
|
DeclarationScope(VAR)->ForceEagerCompilation();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// TODO(1240846): It's weird that native function declarations are
|
|
|
|
// introduced dynamically when we meet their declarations, whereas
|
2012-01-13 13:09:52 +00:00
|
|
|
// other functions are set up when entering the surrounding scope.
|
2015-02-17 20:51:24 +00:00
|
|
|
VariableProxy* proxy = NewUnresolved(name, VAR);
|
2012-02-28 10:12:39 +00:00
|
|
|
Declaration* declaration =
|
2014-02-12 12:02:07 +00:00
|
|
|
factory()->NewVariableDeclaration(proxy, VAR, scope_, pos);
|
2015-06-22 14:15:53 +00:00
|
|
|
Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK);
|
2013-10-14 09:24:58 +00:00
|
|
|
NativeFunctionLiteral* lit = factory()->NewNativeFunctionLiteral(
|
|
|
|
name, extension_, RelocInfo::kNoPosition);
|
2012-02-08 09:56:33 +00:00
|
|
|
return factory()->NewExpressionStatement(
|
|
|
|
factory()->NewAssignment(
|
2013-10-14 09:24:58 +00:00
|
|
|
Token::INIT_VAR, proxy, lit, RelocInfo::kNoPosition),
|
|
|
|
pos);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
Statement* Parser::ParseFunctionDeclaration(
|
|
|
|
ZoneList<const AstRawString*>* names, bool* ok) {
|
2009-10-02 12:47:15 +00:00
|
|
|
// FunctionDeclaration ::
|
|
|
|
// 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}'
|
2013-04-02 17:34:59 +00:00
|
|
|
// GeneratorDeclaration ::
|
|
|
|
// 'function' '*' Identifier '(' FormalParameterListopt ')'
|
|
|
|
// '{' FunctionBody '}'
|
2008-07-03 15:10:15 +00:00
|
|
|
Expect(Token::FUNCTION, CHECK_OK);
|
2013-10-14 09:24:58 +00:00
|
|
|
int pos = position();
|
2014-09-16 12:30:39 +00:00
|
|
|
bool is_generator = Check(Token::MUL);
|
2011-06-24 14:59:51 +00:00
|
|
|
bool is_strict_reserved = false;
|
2014-06-24 14:03:24 +00:00
|
|
|
const AstRawString* name = ParseIdentifierOrStrictReservedWord(
|
2011-06-24 14:59:51 +00:00
|
|
|
&is_strict_reserved, CHECK_OK);
|
2014-09-10 16:39:42 +00:00
|
|
|
FunctionLiteral* fun =
|
2015-07-09 21:31:11 +00:00
|
|
|
ParseFunctionLiteral(name, scanner()->location(),
|
|
|
|
is_strict_reserved ? kFunctionNameIsStrictReserved
|
|
|
|
: kFunctionNameValidityUnknown,
|
2014-09-10 16:39:42 +00:00
|
|
|
is_generator ? FunctionKind::kGeneratorFunction
|
|
|
|
: FunctionKind::kNormalFunction,
|
|
|
|
pos, FunctionLiteral::DECLARATION,
|
|
|
|
FunctionLiteral::NORMAL_ARITY, CHECK_OK);
|
2009-10-02 12:47:15 +00:00
|
|
|
// Even if we're not at the top-level of the global or a function
|
2012-08-28 11:25:08 +00:00
|
|
|
// scope, we treat it as such and introduce the function with its
|
2009-10-02 12:47:15 +00:00
|
|
|
// initial value upon entering the corresponding scope.
|
2014-11-12 11:34:09 +00:00
|
|
|
// In ES6, a function behaves as a lexical binding, except in
|
|
|
|
// a script scope, or the initial scope of eval or another function.
|
2012-08-28 11:25:08 +00:00
|
|
|
VariableMode mode =
|
2015-03-13 15:15:42 +00:00
|
|
|
is_strong(language_mode())
|
|
|
|
? CONST
|
2015-07-08 15:04:04 +00:00
|
|
|
: (is_strict(language_mode()) || allow_harmony_sloppy()) &&
|
2015-03-13 15:15:42 +00:00
|
|
|
!(scope_->is_script_scope() || scope_->is_eval_scope() ||
|
|
|
|
scope_->is_function_scope())
|
|
|
|
? LET
|
|
|
|
: VAR;
|
2015-02-17 20:51:24 +00:00
|
|
|
VariableProxy* proxy = NewUnresolved(name, mode);
|
2012-02-28 10:12:39 +00:00
|
|
|
Declaration* declaration =
|
2014-02-12 12:02:07 +00:00
|
|
|
factory()->NewFunctionDeclaration(proxy, mode, fun, scope_, pos);
|
2015-06-22 14:15:53 +00:00
|
|
|
Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK);
|
2012-06-11 12:42:31 +00:00
|
|
|
if (names) names->Add(name, zone());
|
2013-10-14 09:24:58 +00:00
|
|
|
return factory()->NewEmptyStatement(RelocInfo::kNoPosition);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-16 22:15:39 +00:00
|
|
|
Statement* Parser::ParseClassDeclaration(ZoneList<const AstRawString*>* names,
|
|
|
|
bool* ok) {
|
|
|
|
// ClassDeclaration ::
|
|
|
|
// 'class' Identifier ('extends' LeftHandExpression)? '{' ClassBody '}'
|
|
|
|
//
|
|
|
|
// A ClassDeclaration
|
|
|
|
//
|
|
|
|
// class C { ... }
|
|
|
|
//
|
|
|
|
// has the same semantics as:
|
|
|
|
//
|
|
|
|
// let C = class C { ... };
|
|
|
|
//
|
|
|
|
// so rewrite it as such.
|
|
|
|
|
|
|
|
Expect(Token::CLASS, CHECK_OK);
|
2015-02-04 09:34:05 +00:00
|
|
|
if (!allow_harmony_sloppy() && is_sloppy(language_mode())) {
|
2015-05-18 08:34:05 +00:00
|
|
|
ReportMessage(MessageTemplate::kSloppyLexical);
|
2014-11-20 10:51:49 +00:00
|
|
|
*ok = false;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-09-16 22:15:39 +00:00
|
|
|
int pos = position();
|
|
|
|
bool is_strict_reserved = false;
|
|
|
|
const AstRawString* name =
|
|
|
|
ParseIdentifierOrStrictReservedWord(&is_strict_reserved, CHECK_OK);
|
2014-11-14 15:05:05 +00:00
|
|
|
ClassLiteral* value = ParseClassLiteral(name, scanner()->location(),
|
|
|
|
is_strict_reserved, pos, CHECK_OK);
|
2014-09-16 22:15:39 +00:00
|
|
|
|
2015-02-19 16:49:11 +00:00
|
|
|
VariableMode mode = is_strong(language_mode()) ? CONST : LET;
|
|
|
|
VariableProxy* proxy = NewUnresolved(name, mode);
|
2015-04-16 14:13:03 +00:00
|
|
|
const bool is_class_declaration = true;
|
|
|
|
Declaration* declaration = factory()->NewVariableDeclaration(
|
2015-04-23 14:05:03 +00:00
|
|
|
proxy, mode, scope_, pos, is_class_declaration,
|
|
|
|
scope_->class_declaration_group_start());
|
2015-06-22 14:15:53 +00:00
|
|
|
Variable* outer_class_variable =
|
|
|
|
Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK);
|
2015-02-26 13:48:10 +00:00
|
|
|
proxy->var()->set_initializer_position(position());
|
2015-04-24 13:05:17 +00:00
|
|
|
// This is needed because a class ("class Name { }") creates two bindings (one
|
|
|
|
// in the outer scope, and one in the class scope). The method is a function
|
|
|
|
// scope inside the inner scope (class scope). The consecutive class
|
|
|
|
// declarations are in the outer scope.
|
2015-04-23 14:05:03 +00:00
|
|
|
if (value->class_variable_proxy() && value->class_variable_proxy()->var() &&
|
|
|
|
outer_class_variable->is_class()) {
|
|
|
|
// In some cases, the outer variable is not detected as a class variable;
|
|
|
|
// this happens e.g., for lazy methods. They are excluded from strong mode
|
|
|
|
// checks for now. TODO(marja, rossberg): re-create variables with the
|
|
|
|
// correct Kind and remove this hack.
|
|
|
|
value->class_variable_proxy()
|
|
|
|
->var()
|
|
|
|
->AsClassVariable()
|
2015-04-24 13:05:17 +00:00
|
|
|
->set_declaration_group_start(
|
|
|
|
outer_class_variable->AsClassVariable()->declaration_group_start());
|
2015-04-23 14:05:03 +00:00
|
|
|
}
|
2014-09-16 22:15:39 +00:00
|
|
|
|
2015-02-19 16:49:11 +00:00
|
|
|
Token::Value init_op =
|
|
|
|
is_strong(language_mode()) ? Token::INIT_CONST : Token::INIT_LET;
|
2014-09-16 22:15:39 +00:00
|
|
|
Assignment* assignment = factory()->NewAssignment(init_op, proxy, value, pos);
|
2014-10-07 16:24:59 +00:00
|
|
|
Statement* assignment_statement =
|
|
|
|
factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition);
|
2014-09-16 22:15:39 +00:00
|
|
|
if (names) names->Add(name, zone());
|
2014-10-07 16:24:59 +00:00
|
|
|
return assignment_statement;
|
2014-09-16 22:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
Block* Parser::ParseBlock(ZoneList<const AstRawString*>* labels, bool* ok) {
|
2015-07-08 15:04:04 +00:00
|
|
|
if (is_strict(language_mode()) || allow_harmony_sloppy()) {
|
2014-03-11 14:41:22 +00:00
|
|
|
return ParseScopedBlock(labels, ok);
|
|
|
|
}
|
2011-08-11 16:29:28 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Block ::
|
|
|
|
// '{' Statement* '}'
|
|
|
|
|
|
|
|
// Note that a Block does not introduce a new execution scope!
|
|
|
|
// (ECMA-262, 3rd, 12.2)
|
|
|
|
//
|
|
|
|
// Construct block expecting 16 statements.
|
2013-10-14 09:24:58 +00:00
|
|
|
Block* result =
|
|
|
|
factory()->NewBlock(labels, 16, false, RelocInfo::kNoPosition);
|
2010-10-27 12:33:48 +00:00
|
|
|
Target target(&this->target_stack_, result);
|
2008-07-03 15:10:15 +00:00
|
|
|
Expect(Token::LBRACE, CHECK_OK);
|
|
|
|
while (peek() != Token::RBRACE) {
|
|
|
|
Statement* stat = ParseStatement(NULL, CHECK_OK);
|
2011-07-06 17:21:32 +00:00
|
|
|
if (stat && !stat->IsEmpty()) {
|
2012-06-04 14:42:58 +00:00
|
|
|
result->AddStatement(stat, zone());
|
2011-07-06 17:21:32 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
Expect(Token::RBRACE, CHECK_OK);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
Block* Parser::ParseScopedBlock(ZoneList<const AstRawString*>* labels,
|
|
|
|
bool* ok) {
|
2012-02-20 14:02:59 +00:00
|
|
|
// The harmony mode uses block elements instead of statements.
|
2011-09-21 12:27:07 +00:00
|
|
|
//
|
|
|
|
// Block ::
|
2015-02-06 23:26:18 +00:00
|
|
|
// '{' StatementList '}'
|
2011-09-21 12:27:07 +00:00
|
|
|
|
2011-08-11 16:29:28 +00:00
|
|
|
// Construct block expecting 16 statements.
|
2013-10-14 09:24:58 +00:00
|
|
|
Block* body =
|
|
|
|
factory()->NewBlock(labels, 16, false, RelocInfo::kNoPosition);
|
2014-02-12 12:02:07 +00:00
|
|
|
Scope* block_scope = NewScope(scope_, BLOCK_SCOPE);
|
2011-08-11 16:29:28 +00:00
|
|
|
|
|
|
|
// Parse the statements and collect escaping labels.
|
|
|
|
Expect(Token::LBRACE, CHECK_OK);
|
2014-02-14 12:13:33 +00:00
|
|
|
block_scope->set_start_position(scanner()->location().beg_pos);
|
2014-02-12 12:02:07 +00:00
|
|
|
{ BlockState block_state(&scope_, block_scope);
|
2015-01-15 19:18:05 +00:00
|
|
|
Target target(&this->target_stack_, body);
|
2011-08-11 16:29:28 +00:00
|
|
|
|
|
|
|
while (peek() != Token::RBRACE) {
|
2015-01-27 21:06:36 +00:00
|
|
|
Statement* stat = ParseStatementListItem(CHECK_OK);
|
2011-08-11 16:29:28 +00:00
|
|
|
if (stat && !stat->IsEmpty()) {
|
2012-06-04 14:42:58 +00:00
|
|
|
body->AddStatement(stat, zone());
|
2011-08-11 16:29:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Expect(Token::RBRACE, CHECK_OK);
|
2014-02-14 12:13:33 +00:00
|
|
|
block_scope->set_end_position(scanner()->location().end_pos);
|
2011-09-02 12:43:28 +00:00
|
|
|
block_scope = block_scope->FinalizeBlockScope();
|
2012-04-16 14:43:27 +00:00
|
|
|
body->set_scope(block_scope);
|
2011-09-06 22:00:59 +00:00
|
|
|
return body;
|
2011-08-11 16:29:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-15 09:56:31 +00:00
|
|
|
const AstRawString* Parser::DeclarationParsingResult::SingleName() const {
|
|
|
|
if (declarations.length() != 1) return nullptr;
|
|
|
|
const Declaration& declaration = declarations.at(0);
|
|
|
|
if (declaration.pattern->IsVariableProxy()) {
|
|
|
|
return declaration.pattern->AsVariableProxy()->raw_name();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Block* Parser::DeclarationParsingResult::BuildInitializationBlock(
|
|
|
|
ZoneList<const AstRawString*>* names, bool* ok) {
|
2015-05-21 17:47:07 +00:00
|
|
|
Block* result = descriptor.parser->factory()->NewBlock(
|
|
|
|
NULL, 1, true, descriptor.declaration_pos);
|
2015-05-15 09:56:31 +00:00
|
|
|
for (auto declaration : declarations) {
|
|
|
|
PatternRewriter::DeclareAndInitializeVariables(
|
|
|
|
result, &descriptor, &declaration, names, CHECK_OK);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-16 14:24:12 +00:00
|
|
|
Block* Parser::ParseVariableStatement(VariableDeclarationContext var_context,
|
2014-06-24 14:03:24 +00:00
|
|
|
ZoneList<const AstRawString*>* names,
|
2011-08-16 14:24:12 +00:00
|
|
|
bool* ok) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// VariableStatement ::
|
|
|
|
// VariableDeclarations ';'
|
|
|
|
|
2015-05-15 09:56:31 +00:00
|
|
|
// The scope of a var/const declared variable anywhere inside a function
|
|
|
|
// is the entire function (ECMA-262, 3rd, 10.1.3, and 12.2). Thus we can
|
|
|
|
// transform a source-level var/const declaration into a (Function)
|
|
|
|
// Scope declaration, and rewrite the source-level initialization into an
|
|
|
|
// assignment statement. We use a block to collect multiple assignments.
|
|
|
|
//
|
|
|
|
// We mark the block as initializer block because we don't want the
|
|
|
|
// rewriter to add a '.result' assignment to such a block (to get compliant
|
|
|
|
// behavior for code such as print(eval('var x = 7')), and for cosmetic
|
|
|
|
// reasons when pretty-printing. Also, unless an assignment (initialization)
|
|
|
|
// is inside an initializer block, it is ignored.
|
|
|
|
|
|
|
|
DeclarationParsingResult parsing_result;
|
|
|
|
ParseVariableDeclarations(var_context, &parsing_result, CHECK_OK);
|
2008-07-03 15:10:15 +00:00
|
|
|
ExpectSemicolon(CHECK_OK);
|
2015-05-15 09:56:31 +00:00
|
|
|
|
|
|
|
Block* result = parsing_result.BuildInitializationBlock(names, CHECK_OK);
|
2008-07-03 15:10:15 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-03-22 13:20:04 +00:00
|
|
|
|
2015-05-15 09:56:31 +00:00
|
|
|
void Parser::ParseVariableDeclarations(VariableDeclarationContext var_context,
|
|
|
|
DeclarationParsingResult* parsing_result,
|
|
|
|
bool* ok) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// VariableDeclarations ::
|
2011-10-25 08:33:08 +00:00
|
|
|
// ('var' | 'const' | 'let') (Identifier ('=' AssignmentExpression)?)+[',']
|
|
|
|
//
|
|
|
|
// The ES6 Draft Rev3 specifies the following grammar for const declarations
|
|
|
|
//
|
|
|
|
// ConstDeclaration ::
|
|
|
|
// const ConstBinding (',' ConstBinding)* ';'
|
|
|
|
// ConstBinding ::
|
|
|
|
// Identifier '=' AssignmentExpression
|
|
|
|
//
|
|
|
|
// TODO(ES6):
|
|
|
|
// ConstBinding ::
|
|
|
|
// BindingPattern '=' AssignmentExpression
|
2013-10-14 09:24:58 +00:00
|
|
|
|
2015-05-15 09:56:31 +00:00
|
|
|
parsing_result->descriptor.parser = this;
|
2015-06-22 14:15:53 +00:00
|
|
|
parsing_result->descriptor.declaration_kind = DeclarationDescriptor::NORMAL;
|
2015-05-21 17:47:07 +00:00
|
|
|
parsing_result->descriptor.declaration_pos = peek_position();
|
|
|
|
parsing_result->descriptor.initialization_pos = peek_position();
|
2015-05-15 09:56:31 +00:00
|
|
|
parsing_result->descriptor.mode = VAR;
|
2011-08-30 11:23:57 +00:00
|
|
|
// True if the binding needs initialization. 'let' and 'const' declared
|
|
|
|
// bindings are created uninitialized by their declaration nodes and
|
|
|
|
// need initialization. 'var' declared bindings are always initialized
|
|
|
|
// immediately by their declaration nodes.
|
2015-05-15 09:56:31 +00:00
|
|
|
parsing_result->descriptor.needs_init = false;
|
|
|
|
parsing_result->descriptor.is_const = false;
|
|
|
|
parsing_result->descriptor.init_op = Token::INIT_VAR;
|
2008-07-03 15:10:15 +00:00
|
|
|
if (peek() == Token::VAR) {
|
2015-02-17 15:41:15 +00:00
|
|
|
if (is_strong(language_mode())) {
|
|
|
|
Scanner::Location location = scanner()->peek_location();
|
2015-05-18 08:34:05 +00:00
|
|
|
ReportMessageAt(location, MessageTemplate::kStrongVar);
|
2015-02-17 15:41:15 +00:00
|
|
|
*ok = false;
|
2015-05-15 09:56:31 +00:00
|
|
|
return;
|
2015-02-17 15:41:15 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
Consume(Token::VAR);
|
2015-07-07 21:57:09 +00:00
|
|
|
} else if (peek() == Token::CONST && allow_const()) {
|
2008-07-03 15:10:15 +00:00
|
|
|
Consume(Token::CONST);
|
2015-07-07 21:57:09 +00:00
|
|
|
if (is_sloppy(language_mode()) && allow_legacy_const()) {
|
2015-05-15 09:56:31 +00:00
|
|
|
parsing_result->descriptor.mode = CONST_LEGACY;
|
|
|
|
parsing_result->descriptor.init_op = Token::INIT_CONST_LEGACY;
|
2015-03-30 16:20:53 +00:00
|
|
|
++use_counts_[v8::Isolate::kLegacyConst];
|
2015-02-04 09:34:05 +00:00
|
|
|
} else {
|
2015-07-08 15:04:04 +00:00
|
|
|
DCHECK(is_strict(language_mode()) || allow_harmony_sloppy());
|
2015-02-04 09:34:05 +00:00
|
|
|
DCHECK(var_context != kStatement);
|
2015-05-15 09:56:31 +00:00
|
|
|
parsing_result->descriptor.mode = CONST;
|
|
|
|
parsing_result->descriptor.init_op = Token::INIT_CONST;
|
2011-02-28 18:38:17 +00:00
|
|
|
}
|
2015-05-15 09:56:31 +00:00
|
|
|
parsing_result->descriptor.is_const = true;
|
|
|
|
parsing_result->descriptor.needs_init = true;
|
2015-07-08 15:04:04 +00:00
|
|
|
} else if (peek() == Token::LET && allow_let()) {
|
2011-08-16 14:24:12 +00:00
|
|
|
Consume(Token::LET);
|
2015-01-30 03:09:57 +00:00
|
|
|
DCHECK(var_context != kStatement);
|
2015-05-15 09:56:31 +00:00
|
|
|
parsing_result->descriptor.mode = LET;
|
|
|
|
parsing_result->descriptor.needs_init = true;
|
|
|
|
parsing_result->descriptor.init_op = Token::INIT_LET;
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
|
|
|
UNREACHABLE(); // by current callers
|
|
|
|
}
|
|
|
|
|
2015-05-15 09:56:31 +00:00
|
|
|
parsing_result->descriptor.declaration_scope =
|
|
|
|
DeclarationScope(parsing_result->descriptor.mode);
|
|
|
|
parsing_result->descriptor.scope = scope_;
|
2015-05-11 16:28:28 +00:00
|
|
|
|
2012-02-28 10:12:39 +00:00
|
|
|
|
2015-05-15 09:56:31 +00:00
|
|
|
bool first_declaration = true;
|
2015-04-08 18:47:36 +00:00
|
|
|
int bindings_start = peek_position();
|
2014-10-23 12:30:20 +00:00
|
|
|
bool is_for_iteration_variable;
|
2008-07-03 15:10:15 +00:00
|
|
|
do {
|
2010-08-23 13:26:03 +00:00
|
|
|
if (fni_ != NULL) fni_->Enter();
|
|
|
|
|
2015-05-15 09:56:31 +00:00
|
|
|
// Parse name.
|
|
|
|
if (!first_declaration) Consume(Token::COMMA);
|
2015-04-27 14:35:45 +00:00
|
|
|
|
2015-05-15 09:56:31 +00:00
|
|
|
Expression* pattern;
|
2015-04-27 14:35:45 +00:00
|
|
|
{
|
|
|
|
ExpressionClassifier pattern_classifier;
|
|
|
|
Token::Value next = peek();
|
2015-05-15 09:56:31 +00:00
|
|
|
pattern = ParsePrimaryExpression(&pattern_classifier, ok);
|
|
|
|
if (!*ok) return;
|
|
|
|
ValidateBindingPattern(&pattern_classifier, ok);
|
|
|
|
if (!*ok) return;
|
|
|
|
if (!allow_harmony_destructuring() && !pattern->IsVariableProxy()) {
|
2015-04-27 14:35:45 +00:00
|
|
|
ReportUnexpectedToken(next);
|
|
|
|
*ok = false;
|
2015-05-15 09:56:31 +00:00
|
|
|
return;
|
2015-04-27 14:35:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 19:28:33 +00:00
|
|
|
Scanner::Location variable_loc = scanner()->location();
|
2015-05-15 09:56:31 +00:00
|
|
|
const AstRawString* single_name =
|
|
|
|
pattern->IsVariableProxy() ? pattern->AsVariableProxy()->raw_name()
|
|
|
|
: nullptr;
|
|
|
|
if (single_name != nullptr) {
|
|
|
|
if (fni_ != NULL) fni_->PushVariableName(single_name);
|
2015-05-11 16:28:28 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 12:30:20 +00:00
|
|
|
is_for_iteration_variable =
|
|
|
|
var_context == kForStatement &&
|
|
|
|
(peek() == Token::IN || PeekContextualKeyword(CStrVector("of")));
|
2015-05-15 09:56:31 +00:00
|
|
|
if (is_for_iteration_variable && parsing_result->descriptor.mode == CONST) {
|
|
|
|
parsing_result->descriptor.needs_init = false;
|
2014-10-23 12:30:20 +00:00
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Expression* value = NULL;
|
2011-10-25 08:33:08 +00:00
|
|
|
// Harmony consts have non-optional initializers.
|
2015-05-15 09:56:31 +00:00
|
|
|
int initializer_position = RelocInfo::kNoPosition;
|
|
|
|
if (peek() == Token::ASSIGN || (parsing_result->descriptor.mode == CONST &&
|
|
|
|
!is_for_iteration_variable)) {
|
|
|
|
Expect(Token::ASSIGN, ok);
|
|
|
|
if (!*ok) return;
|
2015-04-22 12:35:05 +00:00
|
|
|
ExpressionClassifier classifier;
|
|
|
|
value = ParseAssignmentExpression(var_context != kForStatement,
|
2015-05-15 09:56:31 +00:00
|
|
|
&classifier, ok);
|
|
|
|
if (!*ok) return;
|
|
|
|
ValidateExpression(&classifier, ok);
|
|
|
|
if (!*ok) return;
|
2015-04-07 19:28:33 +00:00
|
|
|
variable_loc.end_pos = scanner()->location().end_pos;
|
|
|
|
|
2015-05-15 09:56:31 +00:00
|
|
|
if (!parsing_result->first_initializer_loc.IsValid()) {
|
|
|
|
parsing_result->first_initializer_loc = variable_loc;
|
2015-04-07 19:28:33 +00:00
|
|
|
}
|
|
|
|
|
2010-08-23 13:26:03 +00:00
|
|
|
// Don't infer if it is "a = function(){...}();"-like expression.
|
2015-05-11 16:28:28 +00:00
|
|
|
if (single_name) {
|
|
|
|
if (fni_ != NULL && value->AsCall() == NULL &&
|
|
|
|
value->AsCallNew() == NULL) {
|
|
|
|
fni_->Infer();
|
|
|
|
} else {
|
|
|
|
fni_->RemoveLastFunction();
|
|
|
|
}
|
2011-06-22 20:23:48 +00:00
|
|
|
}
|
2015-02-26 13:48:10 +00:00
|
|
|
// End position of the initializer is after the assignment expression.
|
2015-05-15 09:56:31 +00:00
|
|
|
initializer_position = scanner()->location().end_pos;
|
2015-02-26 13:48:10 +00:00
|
|
|
} else {
|
|
|
|
// End position of the initializer is after the variable.
|
2015-05-15 09:56:31 +00:00
|
|
|
initializer_position = position();
|
2011-11-08 13:28:53 +00:00
|
|
|
}
|
|
|
|
|
2011-08-30 11:23:57 +00:00
|
|
|
// Make sure that 'const x' and 'let x' initialize 'x' to undefined.
|
2015-05-15 09:56:31 +00:00
|
|
|
if (value == NULL && parsing_result->descriptor.needs_init) {
|
2013-10-14 09:24:58 +00:00
|
|
|
value = GetLiteralUndefined(position());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 16:28:28 +00:00
|
|
|
if (single_name && fni_ != NULL) fni_->Leave();
|
2015-05-15 09:56:31 +00:00
|
|
|
parsing_result->declarations.Add(DeclarationParsingResult::Declaration(
|
|
|
|
pattern, initializer_position, value));
|
|
|
|
first_declaration = false;
|
2008-07-03 15:10:15 +00:00
|
|
|
} while (peek() == Token::COMMA);
|
|
|
|
|
2015-05-15 09:56:31 +00:00
|
|
|
parsing_result->bindings_loc =
|
|
|
|
Scanner::Location(bindings_start, scanner()->location().end_pos);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
static bool ContainsLabel(ZoneList<const AstRawString*>* labels,
|
|
|
|
const AstRawString* label) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(label != NULL);
|
2014-06-05 08:41:29 +00:00
|
|
|
if (labels != NULL) {
|
|
|
|
for (int i = labels->length(); i-- > 0; ) {
|
2014-06-24 14:03:24 +00:00
|
|
|
if (labels->at(i) == label) {
|
2008-07-03 15:10:15 +00:00
|
|
|
return true;
|
2014-06-05 08:41:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
Statement* Parser::ParseExpressionOrLabelledStatement(
|
|
|
|
ZoneList<const AstRawString*>* labels, bool* ok) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// ExpressionStatement | LabelledStatement ::
|
|
|
|
// Expression ';'
|
|
|
|
// Identifier ':' Statement
|
2015-01-30 03:09:57 +00:00
|
|
|
//
|
|
|
|
// ExpressionStatement[Yield] :
|
|
|
|
// [lookahead ∉ {{, function, class, let [}] Expression[In, ?Yield] ;
|
|
|
|
|
2015-04-22 11:04:25 +00:00
|
|
|
int pos = peek_position();
|
|
|
|
|
2015-01-30 03:09:57 +00:00
|
|
|
switch (peek()) {
|
|
|
|
case Token::FUNCTION:
|
|
|
|
case Token::LBRACE:
|
|
|
|
UNREACHABLE(); // Always handled by the callers.
|
|
|
|
case Token::CLASS:
|
|
|
|
ReportUnexpectedToken(Next());
|
|
|
|
*ok = false;
|
|
|
|
return nullptr;
|
|
|
|
|
2015-04-22 11:04:25 +00:00
|
|
|
case Token::THIS:
|
2015-06-15 12:22:33 +00:00
|
|
|
if (!FLAG_strong_this) break;
|
|
|
|
// Fall through.
|
2015-04-22 11:04:25 +00:00
|
|
|
case Token::SUPER:
|
|
|
|
if (is_strong(language_mode()) &&
|
|
|
|
i::IsConstructor(function_state_->kind())) {
|
|
|
|
bool is_this = peek() == Token::THIS;
|
|
|
|
Expression* expr;
|
2015-04-22 12:35:05 +00:00
|
|
|
ExpressionClassifier classifier;
|
2015-04-22 11:04:25 +00:00
|
|
|
if (is_this) {
|
2015-04-22 12:35:05 +00:00
|
|
|
expr = ParseStrongInitializationExpression(&classifier, CHECK_OK);
|
2015-04-22 11:04:25 +00:00
|
|
|
} else {
|
2015-04-22 12:35:05 +00:00
|
|
|
expr = ParseStrongSuperCallExpression(&classifier, CHECK_OK);
|
2015-04-22 11:04:25 +00:00
|
|
|
}
|
2015-04-27 14:35:45 +00:00
|
|
|
ValidateExpression(&classifier, CHECK_OK);
|
2015-04-22 11:04:25 +00:00
|
|
|
switch (peek()) {
|
|
|
|
case Token::SEMICOLON:
|
|
|
|
Consume(Token::SEMICOLON);
|
|
|
|
break;
|
|
|
|
case Token::RBRACE:
|
|
|
|
case Token::EOS:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (!scanner()->HasAnyLineTerminatorBeforeNext()) {
|
|
|
|
ReportMessageAt(function_state_->this_location(),
|
2015-05-18 08:34:05 +00:00
|
|
|
is_this
|
|
|
|
? MessageTemplate::kStrongConstructorThis
|
|
|
|
: MessageTemplate::kStrongConstructorSuper);
|
2015-04-22 11:04:25 +00:00
|
|
|
*ok = false;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return factory()->NewExpressionStatement(expr, pos);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-01-30 03:09:57 +00:00
|
|
|
// TODO(arv): Handle `let [`
|
|
|
|
// https://code.google.com/p/v8/issues/detail?id=3847
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-02-04 18:36:37 +00:00
|
|
|
bool starts_with_idenfifier = peek_any_identifier();
|
2008-07-03 15:10:15 +00:00
|
|
|
Expression* expr = ParseExpression(true, CHECK_OK);
|
2011-06-20 10:20:57 +00:00
|
|
|
if (peek() == Token::COLON && starts_with_idenfifier && expr != NULL &&
|
2014-02-06 11:59:16 +00:00
|
|
|
expr->AsVariableProxy() != NULL &&
|
|
|
|
!expr->AsVariableProxy()->is_this()) {
|
2010-11-16 12:10:48 +00:00
|
|
|
// Expression is a single identifier, and not, e.g., a parenthesized
|
|
|
|
// identifier.
|
2008-07-03 15:10:15 +00:00
|
|
|
VariableProxy* var = expr->AsVariableProxy();
|
2014-06-24 14:03:24 +00:00
|
|
|
const AstRawString* label = var->raw_name();
|
2008-07-03 15:10:15 +00:00
|
|
|
// TODO(1240780): We don't check for redeclaration of labels
|
|
|
|
// during preparsing since keeping track of the set of active
|
|
|
|
// labels requires nontrivial changes to the way scopes are
|
|
|
|
// structured. However, these are probably changes we want to
|
|
|
|
// make later anyway so we should go back and fix this then.
|
2010-11-02 11:45:47 +00:00
|
|
|
if (ContainsLabel(labels, label) || TargetStackContainsLabel(label)) {
|
2015-05-18 08:34:05 +00:00
|
|
|
ParserTraits::ReportMessage(MessageTemplate::kLabelRedeclaration, label);
|
2010-11-02 11:45:47 +00:00
|
|
|
*ok = false;
|
|
|
|
return NULL;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2012-06-11 12:42:31 +00:00
|
|
|
if (labels == NULL) {
|
2014-06-24 14:03:24 +00:00
|
|
|
labels = new(zone()) ZoneList<const AstRawString*>(4, zone());
|
2012-06-11 12:42:31 +00:00
|
|
|
}
|
|
|
|
labels->Add(label, zone());
|
2010-11-02 11:45:47 +00:00
|
|
|
// Remove the "ghost" variable that turned out to be a label
|
|
|
|
// from the top scope. This way, we don't try to resolve it
|
|
|
|
// during the scope processing.
|
2014-02-12 12:02:07 +00:00
|
|
|
scope_->RemoveUnresolved(var);
|
2008-07-03 15:10:15 +00:00
|
|
|
Expect(Token::COLON, CHECK_OK);
|
|
|
|
return ParseStatement(labels, ok);
|
|
|
|
}
|
|
|
|
|
2011-06-20 10:20:57 +00:00
|
|
|
// If we have an extension, we allow a native function declaration.
|
|
|
|
// A native function declaration starts with "native function" with
|
|
|
|
// no line-terminator between the two words.
|
2014-09-11 09:52:36 +00:00
|
|
|
if (extension_ != NULL && peek() == Token::FUNCTION &&
|
|
|
|
!scanner()->HasAnyLineTerminatorBeforeNext() && expr != NULL &&
|
2014-02-06 11:59:16 +00:00
|
|
|
expr->AsVariableProxy() != NULL &&
|
2014-06-24 14:03:24 +00:00
|
|
|
expr->AsVariableProxy()->raw_name() ==
|
2014-09-11 09:52:36 +00:00
|
|
|
ast_value_factory()->native_string() &&
|
2014-02-14 12:13:33 +00:00
|
|
|
!scanner()->literal_contains_escapes()) {
|
2011-06-20 10:20:57 +00:00
|
|
|
return ParseNativeDeclaration(ok);
|
|
|
|
}
|
|
|
|
|
2015-01-27 21:06:36 +00:00
|
|
|
// Parsed expression statement, followed by semicolon.
|
|
|
|
// Detect attempts at 'let' declarations in sloppy mode.
|
|
|
|
if (peek() == Token::IDENTIFIER && expr->AsVariableProxy() != NULL &&
|
|
|
|
expr->AsVariableProxy()->raw_name() ==
|
|
|
|
ast_value_factory()->let_string()) {
|
2015-05-18 08:34:05 +00:00
|
|
|
ReportMessage(MessageTemplate::kSloppyLexical, NULL);
|
2015-01-27 21:06:36 +00:00
|
|
|
*ok = false;
|
|
|
|
return NULL;
|
2012-02-24 15:53:09 +00:00
|
|
|
}
|
2015-01-27 21:06:36 +00:00
|
|
|
ExpectSemicolon(CHECK_OK);
|
2013-10-14 09:24:58 +00:00
|
|
|
return factory()->NewExpressionStatement(expr, pos);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
IfStatement* Parser::ParseIfStatement(ZoneList<const AstRawString*>* labels,
|
|
|
|
bool* ok) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// IfStatement ::
|
|
|
|
// 'if' '(' Expression ')' Statement ('else' Statement)?
|
|
|
|
|
2013-10-14 09:24:58 +00:00
|
|
|
int pos = peek_position();
|
2008-07-03 15:10:15 +00:00
|
|
|
Expect(Token::IF, CHECK_OK);
|
|
|
|
Expect(Token::LPAREN, CHECK_OK);
|
|
|
|
Expression* condition = ParseExpression(true, CHECK_OK);
|
|
|
|
Expect(Token::RPAREN, CHECK_OK);
|
2015-02-17 16:25:49 +00:00
|
|
|
Statement* then_statement = ParseSubStatement(labels, CHECK_OK);
|
2008-07-03 15:10:15 +00:00
|
|
|
Statement* else_statement = NULL;
|
|
|
|
if (peek() == Token::ELSE) {
|
|
|
|
Next();
|
2015-02-17 16:25:49 +00:00
|
|
|
else_statement = ParseSubStatement(labels, CHECK_OK);
|
2010-11-02 11:45:47 +00:00
|
|
|
} else {
|
2013-10-14 09:24:58 +00:00
|
|
|
else_statement = factory()->NewEmptyStatement(RelocInfo::kNoPosition);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2013-10-14 09:24:58 +00:00
|
|
|
return factory()->NewIfStatement(
|
|
|
|
condition, then_statement, else_statement, pos);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Statement* Parser::ParseContinueStatement(bool* ok) {
|
|
|
|
// ContinueStatement ::
|
|
|
|
// 'continue' Identifier? ';'
|
|
|
|
|
2013-10-14 09:24:58 +00:00
|
|
|
int pos = peek_position();
|
2008-07-03 15:10:15 +00:00
|
|
|
Expect(Token::CONTINUE, CHECK_OK);
|
2014-06-24 14:03:24 +00:00
|
|
|
const AstRawString* label = NULL;
|
2008-07-03 15:10:15 +00:00
|
|
|
Token::Value tok = peek();
|
2014-02-14 12:13:33 +00:00
|
|
|
if (!scanner()->HasAnyLineTerminatorBeforeNext() &&
|
2008-09-08 07:58:54 +00:00
|
|
|
tok != Token::SEMICOLON && tok != Token::RBRACE && tok != Token::EOS) {
|
2014-02-05 16:26:48 +00:00
|
|
|
// ECMA allows "eval" or "arguments" as labels even in strict mode.
|
2015-04-10 12:04:51 +00:00
|
|
|
label = ParseIdentifier(kAllowRestrictedIdentifiers, CHECK_OK);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2014-06-24 14:03:24 +00:00
|
|
|
IterationStatement* target = LookupContinueTarget(label, CHECK_OK);
|
2010-11-02 11:45:47 +00:00
|
|
|
if (target == NULL) {
|
2011-01-17 09:36:10 +00:00
|
|
|
// Illegal continue statement.
|
2015-05-18 08:34:05 +00:00
|
|
|
MessageTemplate::Template message = MessageTemplate::kIllegalContinue;
|
2014-06-24 14:03:24 +00:00
|
|
|
if (label != NULL) {
|
2015-05-18 08:34:05 +00:00
|
|
|
message = MessageTemplate::kUnknownLabel;
|
2011-01-17 09:36:10 +00:00
|
|
|
}
|
2014-06-03 16:12:48 +00:00
|
|
|
ParserTraits::ReportMessage(message, label);
|
2011-01-17 09:36:10 +00:00
|
|
|
*ok = false;
|
|
|
|
return NULL;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
ExpectSemicolon(CHECK_OK);
|
2013-10-14 09:24:58 +00:00
|
|
|
return factory()->NewContinueStatement(target, pos);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
Statement* Parser::ParseBreakStatement(ZoneList<const AstRawString*>* labels,
|
|
|
|
bool* ok) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// BreakStatement ::
|
|
|
|
// 'break' Identifier? ';'
|
|
|
|
|
2013-10-14 09:24:58 +00:00
|
|
|
int pos = peek_position();
|
2008-07-03 15:10:15 +00:00
|
|
|
Expect(Token::BREAK, CHECK_OK);
|
2014-06-24 14:03:24 +00:00
|
|
|
const AstRawString* label = NULL;
|
2008-07-03 15:10:15 +00:00
|
|
|
Token::Value tok = peek();
|
2014-02-14 12:13:33 +00:00
|
|
|
if (!scanner()->HasAnyLineTerminatorBeforeNext() &&
|
2008-09-08 07:58:54 +00:00
|
|
|
tok != Token::SEMICOLON && tok != Token::RBRACE && tok != Token::EOS) {
|
2014-02-05 16:26:48 +00:00
|
|
|
// ECMA allows "eval" or "arguments" as labels even in strict mode.
|
2015-04-10 12:04:51 +00:00
|
|
|
label = ParseIdentifier(kAllowRestrictedIdentifiers, CHECK_OK);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2009-01-15 19:08:34 +00:00
|
|
|
// Parse labeled break statements that target themselves into
|
2008-07-03 15:10:15 +00:00
|
|
|
// empty statements, e.g. 'l1: l2: l3: break l2;'
|
2014-06-24 14:03:24 +00:00
|
|
|
if (label != NULL && ContainsLabel(labels, label)) {
|
2012-02-08 08:40:11 +00:00
|
|
|
ExpectSemicolon(CHECK_OK);
|
2013-10-14 09:24:58 +00:00
|
|
|
return factory()->NewEmptyStatement(pos);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
BreakableStatement* target = NULL;
|
2010-11-02 11:45:47 +00:00
|
|
|
target = LookupBreakTarget(label, CHECK_OK);
|
|
|
|
if (target == NULL) {
|
2011-01-17 09:36:10 +00:00
|
|
|
// Illegal break statement.
|
2015-05-18 08:34:05 +00:00
|
|
|
MessageTemplate::Template message = MessageTemplate::kIllegalBreak;
|
2014-06-24 14:03:24 +00:00
|
|
|
if (label != NULL) {
|
2015-05-18 08:34:05 +00:00
|
|
|
message = MessageTemplate::kUnknownLabel;
|
2011-01-17 09:36:10 +00:00
|
|
|
}
|
2014-06-03 16:12:48 +00:00
|
|
|
ParserTraits::ReportMessage(message, label);
|
2011-01-17 09:36:10 +00:00
|
|
|
*ok = false;
|
|
|
|
return NULL;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
ExpectSemicolon(CHECK_OK);
|
2013-10-14 09:24:58 +00:00
|
|
|
return factory()->NewBreakStatement(target, pos);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Statement* Parser::ParseReturnStatement(bool* ok) {
|
|
|
|
// ReturnStatement ::
|
|
|
|
// 'return' Expression? ';'
|
|
|
|
|
2013-10-14 09:24:58 +00:00
|
|
|
// Consume the return token. It is necessary to do that before
|
2008-07-03 15:10:15 +00:00
|
|
|
// reporting any errors on it, because of the way errors are
|
|
|
|
// reported (underlining).
|
|
|
|
Expect(Token::RETURN, CHECK_OK);
|
2014-04-02 12:38:01 +00:00
|
|
|
Scanner::Location loc = scanner()->location();
|
2015-03-19 19:39:53 +00:00
|
|
|
function_state_->set_return_location(loc);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-12-07 16:03:29 +00:00
|
|
|
Token::Value tok = peek();
|
|
|
|
Statement* result;
|
2013-04-19 14:11:23 +00:00
|
|
|
Expression* return_value;
|
2014-02-14 12:13:33 +00:00
|
|
|
if (scanner()->HasAnyLineTerminatorBeforeNext() ||
|
2011-12-07 16:03:29 +00:00
|
|
|
tok == Token::SEMICOLON ||
|
|
|
|
tok == Token::RBRACE ||
|
|
|
|
tok == Token::EOS) {
|
2015-02-12 20:06:52 +00:00
|
|
|
if (IsSubclassConstructor(function_state_->kind())) {
|
2015-02-06 10:34:50 +00:00
|
|
|
return_value = ThisExpression(scope_, factory(), loc.beg_pos);
|
|
|
|
} else {
|
|
|
|
return_value = GetLiteralUndefined(position());
|
|
|
|
}
|
2011-12-07 16:03:29 +00:00
|
|
|
} else {
|
2015-03-19 19:39:53 +00:00
|
|
|
if (is_strong(language_mode()) &&
|
|
|
|
i::IsConstructor(function_state_->kind())) {
|
|
|
|
int pos = peek_position();
|
|
|
|
ReportMessageAt(Scanner::Location(pos, pos + 1),
|
2015-05-18 08:34:05 +00:00
|
|
|
MessageTemplate::kStrongConstructorReturnValue);
|
2015-03-19 19:39:53 +00:00
|
|
|
*ok = false;
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-04-28 16:09:21 +00:00
|
|
|
|
|
|
|
int pos = peek_position();
|
2013-04-19 14:11:23 +00:00
|
|
|
return_value = ParseExpression(true, CHECK_OK);
|
2015-04-28 16:09:21 +00:00
|
|
|
|
|
|
|
if (IsSubclassConstructor(function_state_->kind())) {
|
|
|
|
// For subclass constructors we need to return this in case of undefined
|
|
|
|
// and throw an exception in case of a non object.
|
|
|
|
//
|
|
|
|
// return expr;
|
|
|
|
//
|
|
|
|
// Is rewritten as:
|
|
|
|
//
|
|
|
|
// return (temp = expr) === undefined ? this :
|
|
|
|
// %_IsSpecObject(temp) ? temp : throw new TypeError(...);
|
|
|
|
Variable* temp = scope_->DeclarationScope()->NewTemporary(
|
|
|
|
ast_value_factory()->empty_string());
|
|
|
|
Assignment* assign = factory()->NewAssignment(
|
|
|
|
Token::ASSIGN, factory()->NewVariableProxy(temp), return_value, pos);
|
|
|
|
|
|
|
|
Expression* throw_expression =
|
2015-05-18 08:34:05 +00:00
|
|
|
NewThrowTypeError(MessageTemplate::kDerivedConstructorReturn,
|
2015-04-28 16:09:21 +00:00
|
|
|
ast_value_factory()->empty_string(), pos);
|
|
|
|
|
|
|
|
// %_IsSpecObject(temp)
|
|
|
|
ZoneList<Expression*>* is_spec_object_args =
|
|
|
|
new (zone()) ZoneList<Expression*>(1, zone());
|
|
|
|
is_spec_object_args->Add(factory()->NewVariableProxy(temp), zone());
|
|
|
|
Expression* is_spec_object_call = factory()->NewCallRuntime(
|
|
|
|
ast_value_factory()->is_spec_object_string(),
|
|
|
|
Runtime::FunctionForId(Runtime::kInlineIsSpecObject),
|
|
|
|
is_spec_object_args, pos);
|
|
|
|
|
|
|
|
// %_IsSpecObject(temp) ? temp : throw_expression
|
|
|
|
Expression* is_object_conditional = factory()->NewConditional(
|
|
|
|
is_spec_object_call, factory()->NewVariableProxy(temp),
|
|
|
|
throw_expression, pos);
|
|
|
|
|
|
|
|
// temp === undefined
|
|
|
|
Expression* is_undefined = factory()->NewCompareOperation(
|
|
|
|
Token::EQ_STRICT, assign,
|
|
|
|
factory()->NewUndefinedLiteral(RelocInfo::kNoPosition), pos);
|
|
|
|
|
|
|
|
// is_undefined ? this : is_object_conditional
|
|
|
|
return_value = factory()->NewConditional(
|
|
|
|
is_undefined, ThisExpression(scope_, factory(), pos),
|
|
|
|
is_object_conditional, pos);
|
|
|
|
}
|
2013-04-19 14:11:23 +00:00
|
|
|
}
|
|
|
|
ExpectSemicolon(CHECK_OK);
|
2015-02-06 10:34:50 +00:00
|
|
|
|
2013-04-19 14:11:23 +00:00
|
|
|
if (is_generator()) {
|
|
|
|
Expression* generator = factory()->NewVariableProxy(
|
2014-02-12 12:02:07 +00:00
|
|
|
function_state_->generator_object_variable());
|
2013-04-19 14:11:23 +00:00
|
|
|
Expression* yield = factory()->NewYield(
|
2014-09-02 07:07:52 +00:00
|
|
|
generator, return_value, Yield::kFinal, loc.beg_pos);
|
2014-04-02 12:38:01 +00:00
|
|
|
result = factory()->NewExpressionStatement(yield, loc.beg_pos);
|
2013-04-19 14:11:23 +00:00
|
|
|
} else {
|
2014-04-02 12:38:01 +00:00
|
|
|
result = factory()->NewReturnStatement(return_value, loc.beg_pos);
|
2011-12-07 16:03:29 +00:00
|
|
|
}
|
|
|
|
|
2014-04-02 12:38:01 +00:00
|
|
|
Scope* decl_scope = scope_->DeclarationScope();
|
2014-11-12 11:34:09 +00:00
|
|
|
if (decl_scope->is_script_scope() || decl_scope->is_eval_scope()) {
|
2015-05-18 08:34:05 +00:00
|
|
|
ReportMessageAt(loc, MessageTemplate::kIllegalReturn);
|
2014-04-02 12:38:01 +00:00
|
|
|
*ok = false;
|
|
|
|
return NULL;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2011-12-07 16:03:29 +00:00
|
|
|
return result;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
Statement* Parser::ParseWithStatement(ZoneList<const AstRawString*>* labels,
|
|
|
|
bool* ok) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// WithStatement ::
|
|
|
|
// 'with' '(' Expression ')' Statement
|
|
|
|
|
|
|
|
Expect(Token::WITH, CHECK_OK);
|
2013-10-14 09:24:58 +00:00
|
|
|
int pos = position();
|
2011-01-20 18:51:47 +00:00
|
|
|
|
2015-02-04 09:34:05 +00:00
|
|
|
if (is_strict(language_mode())) {
|
2015-05-18 08:34:05 +00:00
|
|
|
ReportMessage(MessageTemplate::kStrictWith);
|
2011-01-20 18:51:47 +00:00
|
|
|
*ok = false;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Expect(Token::LPAREN, CHECK_OK);
|
|
|
|
Expression* expr = ParseExpression(true, CHECK_OK);
|
|
|
|
Expect(Token::RPAREN, CHECK_OK);
|
|
|
|
|
2014-02-12 12:02:07 +00:00
|
|
|
scope_->DeclarationScope()->RecordWithStatement();
|
|
|
|
Scope* with_scope = NewScope(scope_, WITH_SCOPE);
|
2011-10-17 09:29:37 +00:00
|
|
|
Statement* stmt;
|
2014-02-12 12:02:07 +00:00
|
|
|
{ BlockState block_state(&scope_, with_scope);
|
2014-02-14 12:13:33 +00:00
|
|
|
with_scope->set_start_position(scanner()->peek_location().beg_pos);
|
2015-02-17 16:25:49 +00:00
|
|
|
stmt = ParseSubStatement(labels, CHECK_OK);
|
2014-02-14 12:13:33 +00:00
|
|
|
with_scope->set_end_position(scanner()->location().end_pos);
|
2011-10-17 09:29:37 +00:00
|
|
|
}
|
2013-10-14 09:24:58 +00:00
|
|
|
return factory()->NewWithStatement(with_scope, expr, stmt, pos);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CaseClause* Parser::ParseCaseClause(bool* default_seen_ptr, bool* ok) {
|
|
|
|
// CaseClause ::
|
2015-03-13 16:57:25 +00:00
|
|
|
// 'case' Expression ':' StatementList
|
|
|
|
// 'default' ':' StatementList
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
Expression* label = NULL; // NULL expression indicates default case
|
|
|
|
if (peek() == Token::CASE) {
|
|
|
|
Expect(Token::CASE, CHECK_OK);
|
|
|
|
label = ParseExpression(true, CHECK_OK);
|
|
|
|
} else {
|
|
|
|
Expect(Token::DEFAULT, CHECK_OK);
|
|
|
|
if (*default_seen_ptr) {
|
2015-05-18 08:34:05 +00:00
|
|
|
ReportMessage(MessageTemplate::kMultipleDefaultsInSwitch);
|
2008-07-03 15:10:15 +00:00
|
|
|
*ok = false;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*default_seen_ptr = true;
|
|
|
|
}
|
|
|
|
Expect(Token::COLON, CHECK_OK);
|
2013-10-14 09:24:58 +00:00
|
|
|
int pos = position();
|
2012-06-11 12:42:31 +00:00
|
|
|
ZoneList<Statement*>* statements =
|
|
|
|
new(zone()) ZoneList<Statement*>(5, zone());
|
2015-04-16 13:29:29 +00:00
|
|
|
Statement* stat = NULL;
|
2008-07-03 15:10:15 +00:00
|
|
|
while (peek() != Token::CASE &&
|
|
|
|
peek() != Token::DEFAULT &&
|
|
|
|
peek() != Token::RBRACE) {
|
2015-04-16 13:29:29 +00:00
|
|
|
stat = ParseStatementListItem(CHECK_OK);
|
2012-06-11 12:42:31 +00:00
|
|
|
statements->Add(stat, zone());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2015-04-16 13:29:29 +00:00
|
|
|
if (is_strong(language_mode()) && stat != NULL && !stat->IsJump() &&
|
|
|
|
peek() != Token::RBRACE) {
|
2015-05-18 08:34:05 +00:00
|
|
|
ReportMessageAt(scanner()->location(),
|
|
|
|
MessageTemplate::kStrongSwitchFallthrough);
|
2015-04-16 13:29:29 +00:00
|
|
|
*ok = false;
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-10-14 11:06:15 +00:00
|
|
|
return factory()->NewCaseClause(label, statements, pos);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
SwitchStatement* Parser::ParseSwitchStatement(
|
|
|
|
ZoneList<const AstRawString*>* labels, bool* ok) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// SwitchStatement ::
|
|
|
|
// 'switch' '(' Expression ')' '{' CaseClause* '}'
|
|
|
|
|
2013-10-14 09:24:58 +00:00
|
|
|
SwitchStatement* statement =
|
|
|
|
factory()->NewSwitchStatement(labels, peek_position());
|
2010-10-27 12:33:48 +00:00
|
|
|
Target target(&this->target_stack_, statement);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
Expect(Token::SWITCH, CHECK_OK);
|
|
|
|
Expect(Token::LPAREN, CHECK_OK);
|
|
|
|
Expression* tag = ParseExpression(true, CHECK_OK);
|
|
|
|
Expect(Token::RPAREN, CHECK_OK);
|
|
|
|
|
|
|
|
bool default_seen = false;
|
2012-06-11 12:42:31 +00:00
|
|
|
ZoneList<CaseClause*>* cases = new(zone()) ZoneList<CaseClause*>(4, zone());
|
2008-07-03 15:10:15 +00:00
|
|
|
Expect(Token::LBRACE, CHECK_OK);
|
|
|
|
while (peek() != Token::RBRACE) {
|
|
|
|
CaseClause* clause = ParseCaseClause(&default_seen, CHECK_OK);
|
2012-06-11 12:42:31 +00:00
|
|
|
cases->Add(clause, zone());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
Expect(Token::RBRACE, CHECK_OK);
|
|
|
|
|
2010-11-02 11:45:47 +00:00
|
|
|
if (statement) statement->Initialize(tag, cases);
|
2008-07-03 15:10:15 +00:00
|
|
|
return statement;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Statement* Parser::ParseThrowStatement(bool* ok) {
|
|
|
|
// ThrowStatement ::
|
|
|
|
// 'throw' Expression ';'
|
|
|
|
|
|
|
|
Expect(Token::THROW, CHECK_OK);
|
2013-10-14 09:24:58 +00:00
|
|
|
int pos = position();
|
2014-02-14 12:13:33 +00:00
|
|
|
if (scanner()->HasAnyLineTerminatorBeforeNext()) {
|
2015-05-18 08:34:05 +00:00
|
|
|
ReportMessage(MessageTemplate::kNewlineAfterThrow);
|
2008-07-03 15:10:15 +00:00
|
|
|
*ok = false;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Expression* exception = ParseExpression(true, CHECK_OK);
|
|
|
|
ExpectSemicolon(CHECK_OK);
|
|
|
|
|
2013-10-14 09:24:58 +00:00
|
|
|
return factory()->NewExpressionStatement(
|
|
|
|
factory()->NewThrow(exception, pos), pos);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TryStatement* Parser::ParseTryStatement(bool* ok) {
|
|
|
|
// TryStatement ::
|
|
|
|
// 'try' Block Catch
|
|
|
|
// 'try' Block Finally
|
|
|
|
// 'try' Block Catch Finally
|
|
|
|
//
|
|
|
|
// Catch ::
|
|
|
|
// 'catch' '(' Identifier ')' Block
|
|
|
|
//
|
|
|
|
// Finally ::
|
|
|
|
// 'finally' Block
|
|
|
|
|
|
|
|
Expect(Token::TRY, CHECK_OK);
|
2013-10-14 09:24:58 +00:00
|
|
|
int pos = position();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2015-01-15 19:18:05 +00:00
|
|
|
Block* try_block = ParseBlock(NULL, CHECK_OK);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
Token::Value tok = peek();
|
|
|
|
if (tok != Token::CATCH && tok != Token::FINALLY) {
|
2015-05-18 08:34:05 +00:00
|
|
|
ReportMessage(MessageTemplate::kNoCatchOrFinally);
|
2008-07-03 15:10:15 +00:00
|
|
|
*ok = false;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-06-30 14:37:55 +00:00
|
|
|
Scope* catch_scope = NULL;
|
|
|
|
Variable* catch_variable = NULL;
|
2011-06-08 13:55:33 +00:00
|
|
|
Block* catch_block = NULL;
|
2014-06-24 14:03:24 +00:00
|
|
|
const AstRawString* name = NULL;
|
2008-07-03 15:10:15 +00:00
|
|
|
if (tok == Token::CATCH) {
|
|
|
|
Consume(Token::CATCH);
|
|
|
|
|
|
|
|
Expect(Token::LPAREN, CHECK_OK);
|
2014-02-12 12:02:07 +00:00
|
|
|
catch_scope = NewScope(scope_, CATCH_SCOPE);
|
2014-02-14 12:13:33 +00:00
|
|
|
catch_scope->set_start_position(scanner()->location().beg_pos);
|
2015-04-10 12:04:51 +00:00
|
|
|
name = ParseIdentifier(kDontAllowRestrictedIdentifiers, CHECK_OK);
|
2011-01-20 18:51:47 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Expect(Token::RPAREN, CHECK_OK);
|
|
|
|
|
2015-02-26 13:48:10 +00:00
|
|
|
catch_variable = catch_scope->DeclareLocal(name, VAR, kCreatedInitialized,
|
|
|
|
Variable::NORMAL);
|
2014-02-12 12:02:07 +00:00
|
|
|
BlockState block_state(&scope_, catch_scope);
|
2014-02-10 08:45:13 +00:00
|
|
|
catch_block = ParseBlock(NULL, CHECK_OK);
|
|
|
|
|
2014-02-14 12:13:33 +00:00
|
|
|
catch_scope->set_end_position(scanner()->location().end_pos);
|
2008-07-03 15:10:15 +00:00
|
|
|
tok = peek();
|
|
|
|
}
|
|
|
|
|
2011-06-08 13:55:33 +00:00
|
|
|
Block* finally_block = NULL;
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(tok == Token::FINALLY || catch_block != NULL);
|
2014-02-10 08:45:13 +00:00
|
|
|
if (tok == Token::FINALLY) {
|
2008-07-03 15:10:15 +00:00
|
|
|
Consume(Token::FINALLY);
|
|
|
|
finally_block = ParseBlock(NULL, CHECK_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simplify the AST nodes by converting:
|
2011-06-08 13:55:33 +00:00
|
|
|
// 'try B0 catch B1 finally B2'
|
2008-07-03 15:10:15 +00:00
|
|
|
// to:
|
2011-06-08 13:55:33 +00:00
|
|
|
// 'try { try B0 catch B1 } finally B2'
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2010-11-02 11:45:47 +00:00
|
|
|
if (catch_block != NULL && finally_block != NULL) {
|
2011-06-30 14:37:55 +00:00
|
|
|
// If we have both, create an inner try/catch.
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(catch_scope != NULL && catch_variable != NULL);
|
2015-06-08 18:19:32 +00:00
|
|
|
TryCatchStatement* statement =
|
|
|
|
factory()->NewTryCatchStatement(try_block, catch_scope, catch_variable,
|
|
|
|
catch_block, RelocInfo::kNoPosition);
|
2013-10-14 09:24:58 +00:00
|
|
|
try_block = factory()->NewBlock(NULL, 1, false, RelocInfo::kNoPosition);
|
2012-06-04 14:42:58 +00:00
|
|
|
try_block->AddStatement(statement, zone());
|
2011-06-30 14:37:55 +00:00
|
|
|
catch_block = NULL; // Clear to indicate it's been handled.
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TryStatement* result = NULL;
|
2010-11-02 11:45:47 +00:00
|
|
|
if (catch_block != NULL) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(finally_block == NULL);
|
|
|
|
DCHECK(catch_scope != NULL && catch_variable != NULL);
|
2015-06-08 18:19:32 +00:00
|
|
|
result = factory()->NewTryCatchStatement(try_block, catch_scope,
|
|
|
|
catch_variable, catch_block, pos);
|
2010-11-02 11:45:47 +00:00
|
|
|
} else {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(finally_block != NULL);
|
2015-06-08 18:19:32 +00:00
|
|
|
result = factory()->NewTryFinallyStatement(try_block, finally_block, pos);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
DoWhileStatement* Parser::ParseDoWhileStatement(
|
|
|
|
ZoneList<const AstRawString*>* labels, bool* ok) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// DoStatement ::
|
|
|
|
// 'do' Statement 'while' '(' Expression ')' ';'
|
|
|
|
|
2013-10-14 09:24:58 +00:00
|
|
|
DoWhileStatement* loop =
|
|
|
|
factory()->NewDoWhileStatement(labels, peek_position());
|
2010-10-27 12:33:48 +00:00
|
|
|
Target target(&this->target_stack_, loop);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
Expect(Token::DO, CHECK_OK);
|
2015-02-17 16:25:49 +00:00
|
|
|
Statement* body = ParseSubStatement(NULL, CHECK_OK);
|
2008-07-03 15:10:15 +00:00
|
|
|
Expect(Token::WHILE, CHECK_OK);
|
|
|
|
Expect(Token::LPAREN, CHECK_OK);
|
2009-11-16 21:59:31 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Expression* cond = ParseExpression(true, CHECK_OK);
|
|
|
|
Expect(Token::RPAREN, CHECK_OK);
|
|
|
|
|
|
|
|
// Allow do-statements to be terminated with and without
|
|
|
|
// semi-colons. This allows code such as 'do;while(0)return' to
|
|
|
|
// parse, which would not be the case if we had used the
|
|
|
|
// ExpectSemicolon() functionality here.
|
|
|
|
if (peek() == Token::SEMICOLON) Consume(Token::SEMICOLON);
|
|
|
|
|
2009-10-12 13:14:06 +00:00
|
|
|
if (loop != NULL) loop->Initialize(cond, body);
|
2008-07-03 15:10:15 +00:00
|
|
|
return loop;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
WhileStatement* Parser::ParseWhileStatement(
|
|
|
|
ZoneList<const AstRawString*>* labels, bool* ok) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// WhileStatement ::
|
|
|
|
// 'while' '(' Expression ')' Statement
|
|
|
|
|
2013-10-14 09:24:58 +00:00
|
|
|
WhileStatement* loop = factory()->NewWhileStatement(labels, peek_position());
|
2010-10-27 12:33:48 +00:00
|
|
|
Target target(&this->target_stack_, loop);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
Expect(Token::WHILE, CHECK_OK);
|
|
|
|
Expect(Token::LPAREN, CHECK_OK);
|
|
|
|
Expression* cond = ParseExpression(true, CHECK_OK);
|
|
|
|
Expect(Token::RPAREN, CHECK_OK);
|
2015-02-17 16:25:49 +00:00
|
|
|
Statement* body = ParseSubStatement(NULL, CHECK_OK);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-10-12 13:14:06 +00:00
|
|
|
if (loop != NULL) loop->Initialize(cond, body);
|
2008-07-03 15:10:15 +00:00
|
|
|
return loop;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-20 08:08:26 +00:00
|
|
|
// !%_IsSpecObject(result = iterator.next()) &&
|
|
|
|
// %ThrowIteratorResultNotAnObject(result)
|
|
|
|
Expression* Parser::BuildIteratorNextResult(Expression* iterator,
|
|
|
|
Variable* result, int pos) {
|
|
|
|
Expression* next_literal = factory()->NewStringLiteral(
|
|
|
|
ast_value_factory()->next_string(), RelocInfo::kNoPosition);
|
|
|
|
Expression* next_property =
|
|
|
|
factory()->NewProperty(iterator, next_literal, RelocInfo::kNoPosition);
|
|
|
|
ZoneList<Expression*>* next_arguments =
|
|
|
|
new (zone()) ZoneList<Expression*>(0, zone());
|
|
|
|
Expression* next_call =
|
|
|
|
factory()->NewCall(next_property, next_arguments, pos);
|
|
|
|
Expression* result_proxy = factory()->NewVariableProxy(result);
|
|
|
|
Expression* left =
|
|
|
|
factory()->NewAssignment(Token::ASSIGN, result_proxy, next_call, pos);
|
|
|
|
|
|
|
|
// %_IsSpecObject(...)
|
|
|
|
ZoneList<Expression*>* is_spec_object_args =
|
|
|
|
new (zone()) ZoneList<Expression*>(1, zone());
|
|
|
|
is_spec_object_args->Add(left, zone());
|
|
|
|
Expression* is_spec_object_call = factory()->NewCallRuntime(
|
|
|
|
ast_value_factory()->is_spec_object_string(),
|
|
|
|
Runtime::FunctionForId(Runtime::kInlineIsSpecObject), is_spec_object_args,
|
|
|
|
pos);
|
|
|
|
|
|
|
|
// %ThrowIteratorResultNotAnObject(result)
|
|
|
|
Expression* result_proxy_again = factory()->NewVariableProxy(result);
|
|
|
|
ZoneList<Expression*>* throw_arguments =
|
|
|
|
new (zone()) ZoneList<Expression*>(1, zone());
|
|
|
|
throw_arguments->Add(result_proxy_again, zone());
|
|
|
|
Expression* throw_call = factory()->NewCallRuntime(
|
|
|
|
ast_value_factory()->throw_iterator_result_not_an_object_string(),
|
|
|
|
Runtime::FunctionForId(Runtime::kThrowIteratorResultNotAnObject),
|
|
|
|
throw_arguments, pos);
|
|
|
|
|
|
|
|
return factory()->NewBinaryOperation(
|
|
|
|
Token::AND,
|
|
|
|
factory()->NewUnaryOperation(Token::NOT, is_spec_object_call, pos),
|
|
|
|
throw_call, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-07 11:12:21 +00:00
|
|
|
void Parser::InitializeForEachStatement(ForEachStatement* stmt,
|
|
|
|
Expression* each,
|
|
|
|
Expression* subject,
|
|
|
|
Statement* body) {
|
|
|
|
ForOfStatement* for_of = stmt->AsForOfStatement();
|
|
|
|
|
|
|
|
if (for_of != NULL) {
|
2014-02-12 12:02:07 +00:00
|
|
|
Variable* iterator = scope_->DeclarationScope()->NewTemporary(
|
2014-09-11 09:52:36 +00:00
|
|
|
ast_value_factory()->dot_iterator_string());
|
2014-02-12 12:02:07 +00:00
|
|
|
Variable* result = scope_->DeclarationScope()->NewTemporary(
|
2014-09-11 09:52:36 +00:00
|
|
|
ast_value_factory()->dot_result_string());
|
2013-06-07 11:12:21 +00:00
|
|
|
|
|
|
|
Expression* assign_iterator;
|
|
|
|
Expression* next_result;
|
|
|
|
Expression* result_done;
|
|
|
|
Expression* assign_each;
|
|
|
|
|
2015-02-23 23:34:26 +00:00
|
|
|
// iterator = subject[Symbol.iterator]()
|
2014-08-05 13:17:49 +00:00
|
|
|
assign_iterator = factory()->NewAssignment(
|
|
|
|
Token::ASSIGN, factory()->NewVariableProxy(iterator),
|
2014-11-12 08:25:59 +00:00
|
|
|
GetIterator(subject, factory()), subject->position());
|
2013-06-07 11:12:21 +00:00
|
|
|
|
2015-02-23 23:34:26 +00:00
|
|
|
// !%_IsSpecObject(result = iterator.next()) &&
|
|
|
|
// %ThrowIteratorResultNotAnObject(result)
|
2013-06-07 11:12:21 +00:00
|
|
|
{
|
2015-02-23 23:34:26 +00:00
|
|
|
// result = iterator.next()
|
2013-06-07 11:12:21 +00:00
|
|
|
Expression* iterator_proxy = factory()->NewVariableProxy(iterator);
|
2015-05-20 08:08:26 +00:00
|
|
|
next_result =
|
|
|
|
BuildIteratorNextResult(iterator_proxy, result, subject->position());
|
2013-06-07 11:12:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// result.done
|
|
|
|
{
|
2014-06-24 14:03:24 +00:00
|
|
|
Expression* done_literal = factory()->NewStringLiteral(
|
2014-09-11 09:52:36 +00:00
|
|
|
ast_value_factory()->done_string(), RelocInfo::kNoPosition);
|
2013-06-07 11:12:21 +00:00
|
|
|
Expression* result_proxy = factory()->NewVariableProxy(result);
|
|
|
|
result_done = factory()->NewProperty(
|
|
|
|
result_proxy, done_literal, RelocInfo::kNoPosition);
|
|
|
|
}
|
|
|
|
|
|
|
|
// each = result.value
|
|
|
|
{
|
2014-06-24 14:03:24 +00:00
|
|
|
Expression* value_literal = factory()->NewStringLiteral(
|
2014-09-11 09:52:36 +00:00
|
|
|
ast_value_factory()->value_string(), RelocInfo::kNoPosition);
|
2013-06-07 11:12:21 +00:00
|
|
|
Expression* result_proxy = factory()->NewVariableProxy(result);
|
|
|
|
Expression* result_value = factory()->NewProperty(
|
|
|
|
result_proxy, value_literal, RelocInfo::kNoPosition);
|
2014-11-12 08:25:59 +00:00
|
|
|
assign_each = factory()->NewAssignment(Token::ASSIGN, each, result_value,
|
2015-07-06 11:15:52 +00:00
|
|
|
RelocInfo::kNoPosition);
|
2013-06-07 11:12:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for_of->Initialize(each, subject, body,
|
2014-06-12 17:31:54 +00:00
|
|
|
assign_iterator,
|
|
|
|
next_result,
|
|
|
|
result_done,
|
|
|
|
assign_each);
|
2013-06-07 11:12:21 +00:00
|
|
|
} else {
|
|
|
|
stmt->Initialize(each, subject, body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-03 18:34:30 +00:00
|
|
|
Statement* Parser::DesugarLexicalBindingsInForStatement(
|
|
|
|
Scope* inner_scope, bool is_const, ZoneList<const AstRawString*>* names,
|
2014-06-24 14:03:24 +00:00
|
|
|
ForStatement* loop, Statement* init, Expression* cond, Statement* next,
|
|
|
|
Statement* body, bool* ok) {
|
2014-05-26 08:07:02 +00:00
|
|
|
// ES6 13.6.3.4 specifies that on each loop iteration the let variables are
|
|
|
|
// copied into a new environment. After copying, the "next" statement of the
|
|
|
|
// loop is executed to update the loop variables. The loop condition is
|
|
|
|
// checked and the loop body is executed.
|
|
|
|
//
|
|
|
|
// We rewrite a for statement of the form
|
|
|
|
//
|
2015-03-03 18:34:30 +00:00
|
|
|
// labels: for (let/const x = i; cond; next) body
|
2014-05-26 08:07:02 +00:00
|
|
|
//
|
|
|
|
// into
|
|
|
|
//
|
|
|
|
// {
|
2015-03-03 18:34:30 +00:00
|
|
|
// let/const x = i;
|
2014-11-14 19:32:53 +00:00
|
|
|
// temp_x = x;
|
|
|
|
// first = 1;
|
2015-06-18 11:54:03 +00:00
|
|
|
// undefined;
|
2014-11-14 19:32:53 +00:00
|
|
|
// outer: for (;;) {
|
2015-06-18 11:54:03 +00:00
|
|
|
// { // This block's only function is to ensure that the statements it
|
|
|
|
// // contains do not affect the normal completion value. This is
|
|
|
|
// // accomplished by setting its ignore_completion_value bit.
|
|
|
|
// // No new lexical scope is introduced, so lexically scoped variables
|
|
|
|
// // declared here will be scoped to the outer for loop.
|
|
|
|
// let/const x = temp_x;
|
|
|
|
// if (first == 1) {
|
|
|
|
// first = 0;
|
|
|
|
// } else {
|
|
|
|
// next;
|
|
|
|
// }
|
|
|
|
// flag = 1;
|
2014-11-14 19:32:53 +00:00
|
|
|
// }
|
|
|
|
// labels: for (; flag == 1; flag = 0, temp_x = x) {
|
2014-05-26 08:07:02 +00:00
|
|
|
// if (cond) {
|
2014-11-14 19:32:53 +00:00
|
|
|
// body
|
2014-05-26 08:07:02 +00:00
|
|
|
// } else {
|
2014-11-14 19:32:53 +00:00
|
|
|
// break outer;
|
2014-05-26 08:07:02 +00:00
|
|
|
// }
|
2014-11-14 19:32:53 +00:00
|
|
|
// }
|
|
|
|
// if (flag == 1) {
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// }
|
2014-05-26 08:07:02 +00:00
|
|
|
// }
|
|
|
|
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(names->length() > 0);
|
2014-05-26 08:07:02 +00:00
|
|
|
Scope* for_scope = scope_;
|
|
|
|
ZoneList<Variable*> temps(names->length(), zone());
|
|
|
|
|
|
|
|
Block* outer_block = factory()->NewBlock(NULL, names->length() + 3, false,
|
|
|
|
RelocInfo::kNoPosition);
|
2014-11-14 19:32:53 +00:00
|
|
|
|
2015-03-03 18:34:30 +00:00
|
|
|
// Add statement: let/const x = i.
|
2014-05-26 08:07:02 +00:00
|
|
|
outer_block->AddStatement(init, zone());
|
|
|
|
|
2014-09-11 09:52:36 +00:00
|
|
|
const AstRawString* temp_name = ast_value_factory()->dot_for_string();
|
2014-05-26 08:07:02 +00:00
|
|
|
|
2015-03-03 18:34:30 +00:00
|
|
|
// For each lexical variable x:
|
2014-05-26 08:07:02 +00:00
|
|
|
// make statement: temp_x = x.
|
|
|
|
for (int i = 0; i < names->length(); i++) {
|
2015-02-17 20:51:24 +00:00
|
|
|
VariableProxy* proxy = NewUnresolved(names->at(i), LET);
|
2014-05-26 08:07:02 +00:00
|
|
|
Variable* temp = scope_->DeclarationScope()->NewTemporary(temp_name);
|
|
|
|
VariableProxy* temp_proxy = factory()->NewVariableProxy(temp);
|
|
|
|
Assignment* assignment = factory()->NewAssignment(
|
|
|
|
Token::ASSIGN, temp_proxy, proxy, RelocInfo::kNoPosition);
|
|
|
|
Statement* assignment_statement = factory()->NewExpressionStatement(
|
|
|
|
assignment, RelocInfo::kNoPosition);
|
|
|
|
outer_block->AddStatement(assignment_statement, zone());
|
|
|
|
temps.Add(temp, zone());
|
|
|
|
}
|
|
|
|
|
2014-11-14 19:32:53 +00:00
|
|
|
Variable* first = NULL;
|
|
|
|
// Make statement: first = 1.
|
|
|
|
if (next) {
|
|
|
|
first = scope_->DeclarationScope()->NewTemporary(temp_name);
|
|
|
|
VariableProxy* first_proxy = factory()->NewVariableProxy(first);
|
2014-06-24 14:03:24 +00:00
|
|
|
Expression* const1 = factory()->NewSmiLiteral(1, RelocInfo::kNoPosition);
|
2014-05-26 08:07:02 +00:00
|
|
|
Assignment* assignment = factory()->NewAssignment(
|
2014-11-14 19:32:53 +00:00
|
|
|
Token::ASSIGN, first_proxy, const1, RelocInfo::kNoPosition);
|
|
|
|
Statement* assignment_statement =
|
|
|
|
factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition);
|
2014-05-26 08:07:02 +00:00
|
|
|
outer_block->AddStatement(assignment_statement, zone());
|
|
|
|
}
|
|
|
|
|
2015-06-18 11:54:03 +00:00
|
|
|
// make statement: undefined;
|
|
|
|
outer_block->AddStatement(
|
|
|
|
factory()->NewExpressionStatement(
|
|
|
|
factory()->NewUndefinedLiteral(RelocInfo::kNoPosition),
|
|
|
|
RelocInfo::kNoPosition),
|
|
|
|
zone());
|
|
|
|
|
2014-11-14 19:32:53 +00:00
|
|
|
// Make statement: outer: for (;;)
|
|
|
|
// Note that we don't actually create the label, or set this loop up as an
|
|
|
|
// explicit break target, instead handing it directly to those nodes that
|
|
|
|
// need to know about it. This should be safe because we don't run any code
|
|
|
|
// in this function that looks up break targets.
|
|
|
|
ForStatement* outer_loop =
|
|
|
|
factory()->NewForStatement(NULL, RelocInfo::kNoPosition);
|
|
|
|
outer_block->AddStatement(outer_loop, zone());
|
|
|
|
|
2014-05-26 08:07:02 +00:00
|
|
|
outer_block->set_scope(for_scope);
|
|
|
|
scope_ = inner_scope;
|
|
|
|
|
2015-06-18 11:54:03 +00:00
|
|
|
Block* inner_block =
|
|
|
|
factory()->NewBlock(NULL, 3, false, RelocInfo::kNoPosition);
|
|
|
|
Block* ignore_completion_block = factory()->NewBlock(
|
|
|
|
NULL, names->length() + 2, true, RelocInfo::kNoPosition);
|
2014-05-26 08:07:02 +00:00
|
|
|
ZoneList<Variable*> inner_vars(names->length(), zone());
|
|
|
|
// For each let variable x:
|
2015-03-03 18:34:30 +00:00
|
|
|
// make statement: let/const x = temp_x.
|
|
|
|
VariableMode mode = is_const ? CONST : LET;
|
2014-05-26 08:07:02 +00:00
|
|
|
for (int i = 0; i < names->length(); i++) {
|
2015-03-03 18:34:30 +00:00
|
|
|
VariableProxy* proxy = NewUnresolved(names->at(i), mode);
|
2015-02-26 13:48:10 +00:00
|
|
|
Declaration* declaration = factory()->NewVariableDeclaration(
|
2015-03-03 18:34:30 +00:00
|
|
|
proxy, mode, scope_, RelocInfo::kNoPosition);
|
2015-06-22 14:15:53 +00:00
|
|
|
Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK);
|
2014-05-26 08:07:02 +00:00
|
|
|
inner_vars.Add(declaration->proxy()->var(), zone());
|
|
|
|
VariableProxy* temp_proxy = factory()->NewVariableProxy(temps.at(i));
|
2015-03-24 17:16:45 +00:00
|
|
|
Assignment* assignment =
|
|
|
|
factory()->NewAssignment(is_const ? Token::INIT_CONST : Token::INIT_LET,
|
|
|
|
proxy, temp_proxy, RelocInfo::kNoPosition);
|
2015-02-26 13:48:10 +00:00
|
|
|
Statement* assignment_statement =
|
|
|
|
factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition);
|
2015-05-15 09:56:31 +00:00
|
|
|
DCHECK(init->position() != RelocInfo::kNoPosition);
|
2015-02-26 13:48:10 +00:00
|
|
|
proxy->var()->set_initializer_position(init->position());
|
2015-06-18 11:54:03 +00:00
|
|
|
ignore_completion_block->AddStatement(assignment_statement, zone());
|
2014-05-26 08:07:02 +00:00
|
|
|
}
|
|
|
|
|
2014-11-14 19:32:53 +00:00
|
|
|
// Make statement: if (first == 1) { first = 0; } else { next; }
|
2014-07-09 07:50:11 +00:00
|
|
|
if (next) {
|
2014-11-14 19:32:53 +00:00
|
|
|
DCHECK(first);
|
2014-05-26 08:07:02 +00:00
|
|
|
Expression* compare = NULL;
|
2014-11-14 19:32:53 +00:00
|
|
|
// Make compare expression: first == 1.
|
2014-05-26 08:07:02 +00:00
|
|
|
{
|
2014-06-24 14:03:24 +00:00
|
|
|
Expression* const1 = factory()->NewSmiLiteral(1, RelocInfo::kNoPosition);
|
2014-11-14 19:32:53 +00:00
|
|
|
VariableProxy* first_proxy = factory()->NewVariableProxy(first);
|
2015-03-24 17:16:45 +00:00
|
|
|
compare = factory()->NewCompareOperation(Token::EQ, first_proxy, const1,
|
|
|
|
RelocInfo::kNoPosition);
|
2014-05-26 08:07:02 +00:00
|
|
|
}
|
2014-11-14 19:32:53 +00:00
|
|
|
Statement* clear_first = NULL;
|
|
|
|
// Make statement: first = 0.
|
2014-05-26 08:07:02 +00:00
|
|
|
{
|
2014-11-14 19:32:53 +00:00
|
|
|
VariableProxy* first_proxy = factory()->NewVariableProxy(first);
|
2014-06-24 14:03:24 +00:00
|
|
|
Expression* const0 = factory()->NewSmiLiteral(0, RelocInfo::kNoPosition);
|
2014-05-26 08:07:02 +00:00
|
|
|
Assignment* assignment = factory()->NewAssignment(
|
2014-11-14 19:32:53 +00:00
|
|
|
Token::ASSIGN, first_proxy, const0, RelocInfo::kNoPosition);
|
|
|
|
clear_first =
|
|
|
|
factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition);
|
2014-05-26 08:07:02 +00:00
|
|
|
}
|
2015-04-22 13:22:37 +00:00
|
|
|
Statement* clear_first_or_next = factory()->NewIfStatement(
|
|
|
|
compare, clear_first, next, RelocInfo::kNoPosition);
|
2015-06-18 11:54:03 +00:00
|
|
|
ignore_completion_block->AddStatement(clear_first_or_next, zone());
|
2014-11-14 19:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Variable* flag = scope_->DeclarationScope()->NewTemporary(temp_name);
|
|
|
|
// Make statement: flag = 1.
|
|
|
|
{
|
|
|
|
VariableProxy* flag_proxy = factory()->NewVariableProxy(flag);
|
|
|
|
Expression* const1 = factory()->NewSmiLiteral(1, RelocInfo::kNoPosition);
|
|
|
|
Assignment* assignment = factory()->NewAssignment(
|
|
|
|
Token::ASSIGN, flag_proxy, const1, RelocInfo::kNoPosition);
|
|
|
|
Statement* assignment_statement =
|
|
|
|
factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition);
|
2015-06-18 11:54:03 +00:00
|
|
|
ignore_completion_block->AddStatement(assignment_statement, zone());
|
2014-11-14 19:32:53 +00:00
|
|
|
}
|
2015-06-18 11:54:03 +00:00
|
|
|
inner_block->AddStatement(ignore_completion_block, zone());
|
2014-11-14 19:32:53 +00:00
|
|
|
// Make cond expression for main loop: flag == 1.
|
|
|
|
Expression* flag_cond = NULL;
|
|
|
|
{
|
|
|
|
Expression* const1 = factory()->NewSmiLiteral(1, RelocInfo::kNoPosition);
|
|
|
|
VariableProxy* flag_proxy = factory()->NewVariableProxy(flag);
|
2015-03-24 17:16:45 +00:00
|
|
|
flag_cond = factory()->NewCompareOperation(Token::EQ, flag_proxy, const1,
|
|
|
|
RelocInfo::kNoPosition);
|
2014-05-26 08:07:02 +00:00
|
|
|
}
|
|
|
|
|
2014-11-14 19:32:53 +00:00
|
|
|
// Create chain of expressions "flag = 0, temp_x = x, ..."
|
|
|
|
Statement* compound_next_statement = NULL;
|
|
|
|
{
|
|
|
|
Expression* compound_next = NULL;
|
|
|
|
// Make expression: flag = 0.
|
|
|
|
{
|
|
|
|
VariableProxy* flag_proxy = factory()->NewVariableProxy(flag);
|
|
|
|
Expression* const0 = factory()->NewSmiLiteral(0, RelocInfo::kNoPosition);
|
|
|
|
compound_next = factory()->NewAssignment(Token::ASSIGN, flag_proxy,
|
|
|
|
const0, RelocInfo::kNoPosition);
|
|
|
|
}
|
2014-05-26 08:07:02 +00:00
|
|
|
|
2014-11-14 19:32:53 +00:00
|
|
|
// Make the comma-separated list of temp_x = x assignments.
|
2015-03-24 17:16:45 +00:00
|
|
|
int inner_var_proxy_pos = scanner()->location().beg_pos;
|
2014-11-14 19:32:53 +00:00
|
|
|
for (int i = 0; i < names->length(); i++) {
|
|
|
|
VariableProxy* temp_proxy = factory()->NewVariableProxy(temps.at(i));
|
2015-03-24 17:16:45 +00:00
|
|
|
VariableProxy* proxy =
|
|
|
|
factory()->NewVariableProxy(inner_vars.at(i), inner_var_proxy_pos);
|
2014-11-14 19:32:53 +00:00
|
|
|
Assignment* assignment = factory()->NewAssignment(
|
|
|
|
Token::ASSIGN, temp_proxy, proxy, RelocInfo::kNoPosition);
|
|
|
|
compound_next = factory()->NewBinaryOperation(
|
|
|
|
Token::COMMA, compound_next, assignment, RelocInfo::kNoPosition);
|
|
|
|
}
|
|
|
|
|
|
|
|
compound_next_statement = factory()->NewExpressionStatement(
|
|
|
|
compound_next, RelocInfo::kNoPosition);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make statement: if (cond) { body; } else { break outer; }
|
|
|
|
Statement* body_or_stop = body;
|
2014-07-09 07:50:11 +00:00
|
|
|
if (cond) {
|
2014-11-14 19:32:53 +00:00
|
|
|
Statement* stop =
|
|
|
|
factory()->NewBreakStatement(outer_loop, RelocInfo::kNoPosition);
|
|
|
|
body_or_stop =
|
|
|
|
factory()->NewIfStatement(cond, body, stop, cond->position());
|
2014-05-26 08:07:02 +00:00
|
|
|
}
|
|
|
|
|
2014-11-14 19:32:53 +00:00
|
|
|
// Make statement: labels: for (; flag == 1; flag = 0, temp_x = x)
|
2015-06-18 11:54:03 +00:00
|
|
|
// Note that we re-use the original loop node, which retains its labels
|
2014-11-14 19:32:53 +00:00
|
|
|
// and ensures that any break or continue statements in body point to
|
|
|
|
// the right place.
|
|
|
|
loop->Initialize(NULL, flag_cond, compound_next_statement, body_or_stop);
|
|
|
|
inner_block->AddStatement(loop, zone());
|
2014-05-26 08:07:02 +00:00
|
|
|
|
2014-11-14 19:32:53 +00:00
|
|
|
// Make statement: if (flag == 1) { break; }
|
|
|
|
{
|
|
|
|
Expression* compare = NULL;
|
|
|
|
// Make compare expresion: flag == 1.
|
|
|
|
{
|
|
|
|
Expression* const1 = factory()->NewSmiLiteral(1, RelocInfo::kNoPosition);
|
|
|
|
VariableProxy* flag_proxy = factory()->NewVariableProxy(flag);
|
2015-03-24 17:16:45 +00:00
|
|
|
compare = factory()->NewCompareOperation(Token::EQ, flag_proxy, const1,
|
|
|
|
RelocInfo::kNoPosition);
|
2014-11-14 19:32:53 +00:00
|
|
|
}
|
|
|
|
Statement* stop =
|
|
|
|
factory()->NewBreakStatement(outer_loop, RelocInfo::kNoPosition);
|
|
|
|
Statement* empty = factory()->NewEmptyStatement(RelocInfo::kNoPosition);
|
|
|
|
Statement* if_flag_break =
|
|
|
|
factory()->NewIfStatement(compare, stop, empty, RelocInfo::kNoPosition);
|
|
|
|
inner_block->AddStatement(if_flag_break, zone());
|
2014-05-26 08:07:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inner_scope->set_end_position(scanner()->location().end_pos);
|
|
|
|
inner_block->set_scope(inner_scope);
|
|
|
|
scope_ = for_scope;
|
|
|
|
|
2014-11-14 19:32:53 +00:00
|
|
|
outer_loop->Initialize(NULL, NULL, NULL, inner_block);
|
2014-05-26 08:07:02 +00:00
|
|
|
return outer_block;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
Statement* Parser::ParseForStatement(ZoneList<const AstRawString*>* labels,
|
|
|
|
bool* ok) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// ForStatement ::
|
|
|
|
// 'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement
|
|
|
|
|
2014-11-12 08:25:59 +00:00
|
|
|
int stmt_pos = peek_position();
|
2015-03-03 18:34:30 +00:00
|
|
|
bool is_const = false;
|
2008-07-03 15:10:15 +00:00
|
|
|
Statement* init = NULL;
|
2015-03-03 18:34:30 +00:00
|
|
|
ZoneList<const AstRawString*> lexical_bindings(1, zone());
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-10-17 12:19:06 +00:00
|
|
|
// Create an in-between scope for let-bound iteration variables.
|
2014-02-12 12:02:07 +00:00
|
|
|
Scope* saved_scope = scope_;
|
|
|
|
Scope* for_scope = NewScope(scope_, BLOCK_SCOPE);
|
|
|
|
scope_ = for_scope;
|
2008-07-03 15:10:15 +00:00
|
|
|
Expect(Token::FOR, CHECK_OK);
|
|
|
|
Expect(Token::LPAREN, CHECK_OK);
|
2014-02-14 12:13:33 +00:00
|
|
|
for_scope->set_start_position(scanner()->location().beg_pos);
|
2014-11-18 18:51:20 +00:00
|
|
|
bool is_let_identifier_expression = false;
|
2015-05-15 09:56:31 +00:00
|
|
|
DeclarationParsingResult parsing_result;
|
2008-07-03 15:10:15 +00:00
|
|
|
if (peek() != Token::SEMICOLON) {
|
2015-07-07 21:57:09 +00:00
|
|
|
if (peek() == Token::VAR || (peek() == Token::CONST && allow_const()) ||
|
2015-07-08 15:04:04 +00:00
|
|
|
(peek() == Token::LET && allow_let())) {
|
2015-05-15 09:56:31 +00:00
|
|
|
ParseVariableDeclarations(kForStatement, &parsing_result, CHECK_OK);
|
2015-05-21 17:47:07 +00:00
|
|
|
is_const = parsing_result.descriptor.mode == CONST;
|
2015-05-15 09:56:31 +00:00
|
|
|
|
|
|
|
int num_decl = parsing_result.declarations.length();
|
2015-04-08 18:47:36 +00:00
|
|
|
bool accept_IN = num_decl >= 1;
|
2015-04-07 19:28:33 +00:00
|
|
|
bool accept_OF = true;
|
2013-06-06 14:38:26 +00:00
|
|
|
ForEachStatement::VisitMode mode;
|
2015-02-26 13:48:10 +00:00
|
|
|
int each_beg_pos = scanner()->location().beg_pos;
|
|
|
|
int each_end_pos = scanner()->location().end_pos;
|
2011-06-30 14:37:55 +00:00
|
|
|
|
2015-04-08 18:47:36 +00:00
|
|
|
if (accept_IN && CheckInOrOf(accept_OF, &mode, ok)) {
|
2015-02-19 13:50:33 +00:00
|
|
|
if (!*ok) return nullptr;
|
2015-04-08 18:47:36 +00:00
|
|
|
if (num_decl != 1) {
|
|
|
|
const char* loop_type =
|
|
|
|
mode == ForEachStatement::ITERATE ? "for-of" : "for-in";
|
2015-05-18 08:34:05 +00:00
|
|
|
ParserTraits::ReportMessageAt(
|
|
|
|
parsing_result.bindings_loc,
|
|
|
|
MessageTemplate::kForInOfLoopMultiBindings, loop_type);
|
2015-04-08 18:47:36 +00:00
|
|
|
*ok = false;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-05-15 09:56:31 +00:00
|
|
|
if (parsing_result.first_initializer_loc.IsValid() &&
|
2015-04-07 19:28:33 +00:00
|
|
|
(is_strict(language_mode()) || mode == ForEachStatement::ITERATE)) {
|
|
|
|
if (mode == ForEachStatement::ITERATE) {
|
2015-05-15 09:56:31 +00:00
|
|
|
ReportMessageAt(parsing_result.first_initializer_loc,
|
2015-05-18 08:34:05 +00:00
|
|
|
MessageTemplate::kForOfLoopInitializer);
|
2015-04-07 19:28:33 +00:00
|
|
|
} else {
|
|
|
|
// TODO(caitp): This should be an error in sloppy mode too.
|
2015-05-15 09:56:31 +00:00
|
|
|
ReportMessageAt(parsing_result.first_initializer_loc,
|
2015-05-18 08:34:05 +00:00
|
|
|
MessageTemplate::kForInLoopInitializer);
|
2015-04-07 19:28:33 +00:00
|
|
|
}
|
|
|
|
*ok = false;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-05-15 09:56:31 +00:00
|
|
|
|
2015-05-21 17:47:07 +00:00
|
|
|
DCHECK(parsing_result.declarations.length() == 1);
|
|
|
|
Block* init_block = nullptr;
|
|
|
|
|
|
|
|
// special case for legacy for (var/const x =.... in)
|
2015-07-07 21:57:09 +00:00
|
|
|
if (!IsLexicalVariableMode(parsing_result.descriptor.mode) &&
|
2015-05-21 17:47:07 +00:00
|
|
|
parsing_result.declarations[0].initializer != nullptr) {
|
|
|
|
VariableProxy* single_var = scope_->NewUnresolved(
|
|
|
|
factory(), parsing_result.SingleName(), Variable::NORMAL,
|
|
|
|
each_beg_pos, each_end_pos);
|
|
|
|
init_block = factory()->NewBlock(
|
|
|
|
nullptr, 2, true, parsing_result.descriptor.declaration_pos);
|
|
|
|
init_block->AddStatement(
|
|
|
|
factory()->NewExpressionStatement(
|
|
|
|
factory()->NewAssignment(
|
|
|
|
Token::ASSIGN, single_var,
|
|
|
|
parsing_result.declarations[0].initializer,
|
|
|
|
RelocInfo::kNoPosition),
|
|
|
|
RelocInfo::kNoPosition),
|
|
|
|
zone());
|
2015-04-08 18:47:36 +00:00
|
|
|
}
|
2015-05-21 17:47:07 +00:00
|
|
|
|
|
|
|
// Rewrite a for-in/of statement of the form
|
2011-10-17 12:19:06 +00:00
|
|
|
//
|
2015-05-21 17:47:07 +00:00
|
|
|
// for (let/const/var x in/of e) b
|
2011-10-17 12:19:06 +00:00
|
|
|
//
|
|
|
|
// into
|
|
|
|
//
|
2015-07-01 00:27:15 +00:00
|
|
|
// {
|
|
|
|
// <let x' be a temporary variable>
|
|
|
|
// for (x' in/of e) {
|
|
|
|
// let/const/var x;
|
|
|
|
// x = x';
|
|
|
|
// b;
|
|
|
|
// }
|
|
|
|
// let x; // for TDZ
|
2011-10-17 12:19:06 +00:00
|
|
|
// }
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
Variable* temp = scope_->DeclarationScope()->NewTemporary(
|
2014-09-11 09:52:36 +00:00
|
|
|
ast_value_factory()->dot_for_string());
|
2013-10-14 09:24:58 +00:00
|
|
|
ForEachStatement* loop =
|
2014-11-12 08:25:59 +00:00
|
|
|
factory()->NewForEachStatement(mode, labels, stmt_pos);
|
2011-10-17 12:19:06 +00:00
|
|
|
Target target(&this->target_stack_, loop);
|
|
|
|
|
|
|
|
Expression* enumerable = ParseExpression(true, CHECK_OK);
|
2015-07-01 00:27:15 +00:00
|
|
|
|
2011-10-17 12:19:06 +00:00
|
|
|
Expect(Token::RPAREN, CHECK_OK);
|
|
|
|
|
2015-07-01 00:27:15 +00:00
|
|
|
Scope* body_scope = NewScope(scope_, BLOCK_SCOPE);
|
|
|
|
body_scope->set_start_position(scanner()->location().beg_pos);
|
|
|
|
scope_ = body_scope;
|
|
|
|
|
2015-02-17 16:25:49 +00:00
|
|
|
Statement* body = ParseSubStatement(NULL, CHECK_OK);
|
2015-05-21 17:47:07 +00:00
|
|
|
|
2013-10-14 09:24:58 +00:00
|
|
|
Block* body_block =
|
|
|
|
factory()->NewBlock(NULL, 3, false, RelocInfo::kNoPosition);
|
2015-05-21 12:36:11 +00:00
|
|
|
|
2015-05-21 17:47:07 +00:00
|
|
|
auto each_initialization_block =
|
|
|
|
factory()->NewBlock(nullptr, 1, true, RelocInfo::kNoPosition);
|
2015-05-21 12:36:11 +00:00
|
|
|
{
|
|
|
|
DCHECK(parsing_result.declarations.length() == 1);
|
|
|
|
DeclarationParsingResult::Declaration decl =
|
|
|
|
parsing_result.declarations[0];
|
2015-05-21 17:47:07 +00:00
|
|
|
auto descriptor = parsing_result.descriptor;
|
|
|
|
descriptor.declaration_pos = RelocInfo::kNoPosition;
|
2015-07-06 11:15:52 +00:00
|
|
|
descriptor.initialization_pos = RelocInfo::kNoPosition;
|
2015-05-21 17:47:07 +00:00
|
|
|
decl.initializer = factory()->NewVariableProxy(temp);
|
|
|
|
|
2015-05-21 12:36:11 +00:00
|
|
|
PatternRewriter::DeclareAndInitializeVariables(
|
2015-05-21 17:47:07 +00:00
|
|
|
each_initialization_block, &descriptor, &decl,
|
|
|
|
IsLexicalVariableMode(descriptor.mode) ? &lexical_bindings
|
|
|
|
: nullptr,
|
|
|
|
CHECK_OK);
|
2015-05-21 12:36:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
body_block->AddStatement(each_initialization_block, zone());
|
2012-06-04 14:42:58 +00:00
|
|
|
body_block->AddStatement(body, zone());
|
2015-05-21 17:47:07 +00:00
|
|
|
VariableProxy* temp_proxy =
|
|
|
|
factory()->NewVariableProxy(temp, each_beg_pos, each_end_pos);
|
2013-06-07 11:12:21 +00:00
|
|
|
InitializeForEachStatement(loop, temp_proxy, enumerable, body_block);
|
2015-07-01 00:27:15 +00:00
|
|
|
scope_ = for_scope;
|
|
|
|
body_scope->set_end_position(scanner()->location().end_pos);
|
|
|
|
body_scope = body_scope->FinalizeBlockScope();
|
|
|
|
if (body_scope != nullptr) {
|
|
|
|
body_block->set_scope(body_scope);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a TDZ for any lexically-bound names.
|
2015-07-07 21:57:09 +00:00
|
|
|
if (IsLexicalVariableMode(parsing_result.descriptor.mode)) {
|
2015-07-01 00:27:15 +00:00
|
|
|
DCHECK_NULL(init_block);
|
|
|
|
|
|
|
|
init_block =
|
|
|
|
factory()->NewBlock(nullptr, 1, false, RelocInfo::kNoPosition);
|
|
|
|
|
|
|
|
for (int i = 0; i < lexical_bindings.length(); ++i) {
|
|
|
|
// TODO(adamk): This needs to be some sort of special
|
|
|
|
// INTERNAL variable that's invisible to the debugger
|
|
|
|
// but visible to everything else.
|
|
|
|
VariableProxy* tdz_proxy = NewUnresolved(lexical_bindings[i], LET);
|
|
|
|
Declaration* tdz_decl = factory()->NewVariableDeclaration(
|
|
|
|
tdz_proxy, LET, scope_, RelocInfo::kNoPosition);
|
|
|
|
Variable* tdz_var = Declare(tdz_decl, DeclarationDescriptor::NORMAL,
|
|
|
|
true, CHECK_OK);
|
|
|
|
tdz_var->set_initializer_position(position());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 12:02:07 +00:00
|
|
|
scope_ = saved_scope;
|
2014-02-14 12:13:33 +00:00
|
|
|
for_scope->set_end_position(scanner()->location().end_pos);
|
2011-10-17 12:19:06 +00:00
|
|
|
for_scope = for_scope->FinalizeBlockScope();
|
2015-05-21 17:47:07 +00:00
|
|
|
// Parsed for-in loop w/ variable declarations.
|
|
|
|
if (init_block != nullptr) {
|
|
|
|
init_block->AddStatement(loop, zone());
|
2015-07-01 00:27:15 +00:00
|
|
|
if (for_scope != nullptr) {
|
|
|
|
init_block->set_scope(for_scope);
|
|
|
|
}
|
2015-05-21 17:47:07 +00:00
|
|
|
return init_block;
|
|
|
|
} else {
|
2015-07-01 00:27:15 +00:00
|
|
|
DCHECK_NULL(for_scope);
|
2015-05-21 17:47:07 +00:00
|
|
|
return loop;
|
|
|
|
}
|
2011-10-17 12:19:06 +00:00
|
|
|
} else {
|
2015-05-21 17:47:07 +00:00
|
|
|
init = parsing_result.BuildInitializationBlock(
|
|
|
|
IsLexicalVariableMode(parsing_result.descriptor.mode)
|
|
|
|
? &lexical_bindings
|
|
|
|
: nullptr,
|
|
|
|
CHECK_OK);
|
2011-10-17 12:19:06 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
2014-03-17 10:21:01 +00:00
|
|
|
Scanner::Location lhs_location = scanner()->peek_location();
|
2008-07-03 15:10:15 +00:00
|
|
|
Expression* expression = ParseExpression(false, CHECK_OK);
|
2013-06-06 14:38:26 +00:00
|
|
|
ForEachStatement::VisitMode mode;
|
2014-11-03 19:44:46 +00:00
|
|
|
bool accept_OF = expression->IsVariableProxy();
|
2014-11-18 18:51:20 +00:00
|
|
|
is_let_identifier_expression =
|
2015-05-21 17:47:07 +00:00
|
|
|
expression->IsVariableProxy() &&
|
|
|
|
expression->AsVariableProxy()->raw_name() ==
|
|
|
|
ast_value_factory()->let_string();
|
2013-06-06 14:38:26 +00:00
|
|
|
|
2015-02-19 13:50:33 +00:00
|
|
|
if (CheckInOrOf(accept_OF, &mode, ok)) {
|
|
|
|
if (!*ok) return nullptr;
|
2014-04-02 11:03:05 +00:00
|
|
|
expression = this->CheckAndRewriteReferenceExpression(
|
2015-05-18 08:34:05 +00:00
|
|
|
expression, lhs_location, MessageTemplate::kInvalidLhsInFor,
|
|
|
|
CHECK_OK);
|
2014-04-02 11:03:05 +00:00
|
|
|
|
2013-10-14 09:24:58 +00:00
|
|
|
ForEachStatement* loop =
|
2014-11-12 08:25:59 +00:00
|
|
|
factory()->NewForEachStatement(mode, labels, stmt_pos);
|
2010-10-27 12:33:48 +00:00
|
|
|
Target target(&this->target_stack_, loop);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
Expression* enumerable = ParseExpression(true, CHECK_OK);
|
|
|
|
Expect(Token::RPAREN, CHECK_OK);
|
|
|
|
|
2015-02-17 16:25:49 +00:00
|
|
|
Statement* body = ParseSubStatement(NULL, CHECK_OK);
|
2013-06-07 11:12:21 +00:00
|
|
|
InitializeForEachStatement(loop, expression, enumerable, body);
|
2014-02-12 12:02:07 +00:00
|
|
|
scope_ = saved_scope;
|
2014-02-14 12:13:33 +00:00
|
|
|
for_scope->set_end_position(scanner()->location().end_pos);
|
2011-10-17 12:19:06 +00:00
|
|
|
for_scope = for_scope->FinalizeBlockScope();
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(for_scope == NULL);
|
2008-07-03 15:10:15 +00:00
|
|
|
// Parsed for-in loop.
|
|
|
|
return loop;
|
|
|
|
|
|
|
|
} else {
|
2015-07-06 11:15:52 +00:00
|
|
|
init =
|
|
|
|
factory()->NewExpressionStatement(expression, lhs_location.beg_pos);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Standard 'for' loop
|
2014-11-12 08:25:59 +00:00
|
|
|
ForStatement* loop = factory()->NewForStatement(labels, stmt_pos);
|
2010-10-27 12:33:48 +00:00
|
|
|
Target target(&this->target_stack_, loop);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Parsed initializer at this point.
|
2014-11-18 18:51:20 +00:00
|
|
|
// Detect attempts at 'let' declarations in sloppy mode.
|
2015-02-04 09:34:05 +00:00
|
|
|
if (peek() == Token::IDENTIFIER && is_sloppy(language_mode()) &&
|
2014-11-18 18:51:20 +00:00
|
|
|
is_let_identifier_expression) {
|
2015-05-18 08:34:05 +00:00
|
|
|
ReportMessage(MessageTemplate::kSloppyLexical, NULL);
|
2014-11-18 18:51:20 +00:00
|
|
|
*ok = false;
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
Expect(Token::SEMICOLON, CHECK_OK);
|
|
|
|
|
2014-05-26 08:07:02 +00:00
|
|
|
// If there are let bindings, then condition and the next statement of the
|
|
|
|
// for loop must be parsed in a new scope.
|
|
|
|
Scope* inner_scope = NULL;
|
2015-03-03 18:34:30 +00:00
|
|
|
if (lexical_bindings.length() > 0) {
|
2014-05-26 08:07:02 +00:00
|
|
|
inner_scope = NewScope(for_scope, BLOCK_SCOPE);
|
|
|
|
inner_scope->set_start_position(scanner()->location().beg_pos);
|
|
|
|
scope_ = inner_scope;
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Expression* cond = NULL;
|
|
|
|
if (peek() != Token::SEMICOLON) {
|
|
|
|
cond = ParseExpression(true, CHECK_OK);
|
|
|
|
}
|
|
|
|
Expect(Token::SEMICOLON, CHECK_OK);
|
|
|
|
|
|
|
|
Statement* next = NULL;
|
|
|
|
if (peek() != Token::RPAREN) {
|
2014-11-12 08:25:59 +00:00
|
|
|
int next_pos = position();
|
2008-07-03 15:10:15 +00:00
|
|
|
Expression* exp = ParseExpression(true, CHECK_OK);
|
2014-11-12 08:25:59 +00:00
|
|
|
next = factory()->NewExpressionStatement(exp, next_pos);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
Expect(Token::RPAREN, CHECK_OK);
|
|
|
|
|
2015-02-17 16:25:49 +00:00
|
|
|
Statement* body = ParseSubStatement(NULL, CHECK_OK);
|
2014-05-26 08:07:02 +00:00
|
|
|
|
|
|
|
Statement* result = NULL;
|
2015-03-03 18:34:30 +00:00
|
|
|
if (lexical_bindings.length() > 0) {
|
2014-05-26 08:07:02 +00:00
|
|
|
scope_ = for_scope;
|
2015-03-03 18:34:30 +00:00
|
|
|
result = DesugarLexicalBindingsInForStatement(
|
|
|
|
inner_scope, is_const, &lexical_bindings, loop, init, cond,
|
|
|
|
next, body, CHECK_OK);
|
2014-05-26 08:07:02 +00:00
|
|
|
scope_ = saved_scope;
|
|
|
|
for_scope->set_end_position(scanner()->location().end_pos);
|
2011-10-17 12:19:06 +00:00
|
|
|
} else {
|
2014-05-26 08:07:02 +00:00
|
|
|
scope_ = saved_scope;
|
|
|
|
for_scope->set_end_position(scanner()->location().end_pos);
|
2014-07-09 07:50:11 +00:00
|
|
|
for_scope = for_scope->FinalizeBlockScope();
|
|
|
|
if (for_scope) {
|
|
|
|
// Rewrite a for statement of the form
|
|
|
|
// for (const x = i; c; n) b
|
|
|
|
//
|
|
|
|
// into
|
|
|
|
//
|
|
|
|
// {
|
|
|
|
// const x = i;
|
|
|
|
// for (; c; n) b
|
|
|
|
// }
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(init != NULL);
|
2014-07-09 07:50:11 +00:00
|
|
|
Block* block =
|
|
|
|
factory()->NewBlock(NULL, 2, false, RelocInfo::kNoPosition);
|
|
|
|
block->AddStatement(init, zone());
|
|
|
|
block->AddStatement(loop, zone());
|
|
|
|
block->set_scope(for_scope);
|
|
|
|
loop->Initialize(NULL, cond, next, body);
|
|
|
|
result = block;
|
|
|
|
} else {
|
|
|
|
loop->Initialize(init, cond, next, body);
|
|
|
|
result = loop;
|
|
|
|
}
|
2011-10-17 12:19:06 +00:00
|
|
|
}
|
2014-05-26 08:07:02 +00:00
|
|
|
return result;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DebuggerStatement* Parser::ParseDebuggerStatement(bool* ok) {
|
|
|
|
// In ECMA-262 'debugger' is defined as a reserved keyword. In some browser
|
|
|
|
// contexts this is used as a statement which invokes the debugger as i a
|
|
|
|
// break point is present.
|
|
|
|
// DebuggerStatement ::
|
|
|
|
// 'debugger' ';'
|
|
|
|
|
2013-10-14 09:24:58 +00:00
|
|
|
int pos = peek_position();
|
2008-07-03 15:10:15 +00:00
|
|
|
Expect(Token::DEBUGGER, CHECK_OK);
|
|
|
|
ExpectSemicolon(CHECK_OK);
|
2013-10-14 09:24:58 +00:00
|
|
|
return factory()->NewDebuggerStatement(pos);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-23 07:27:47 +00:00
|
|
|
bool CompileTimeValue::IsCompileTimeValue(Expression* expression) {
|
2014-06-03 07:40:43 +00:00
|
|
|
if (expression->IsLiteral()) return true;
|
2009-03-23 07:27:47 +00:00
|
|
|
MaterializedLiteral* lit = expression->AsMaterializedLiteral();
|
|
|
|
return lit != NULL && lit->is_simple();
|
|
|
|
}
|
|
|
|
|
2010-08-16 16:06:46 +00:00
|
|
|
|
2013-09-04 07:05:11 +00:00
|
|
|
Handle<FixedArray> CompileTimeValue::GetValue(Isolate* isolate,
|
|
|
|
Expression* expression) {
|
|
|
|
Factory* factory = isolate->factory();
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(IsCompileTimeValue(expression));
|
2013-06-04 10:30:05 +00:00
|
|
|
Handle<FixedArray> result = factory->NewFixedArray(2, TENURED);
|
2009-03-23 07:27:47 +00:00
|
|
|
ObjectLiteral* object_literal = expression->AsObjectLiteral();
|
|
|
|
if (object_literal != NULL) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(object_literal->is_simple());
|
2010-03-11 10:34:29 +00:00
|
|
|
if (object_literal->fast_elements()) {
|
2013-06-06 13:28:22 +00:00
|
|
|
result->set(kLiteralTypeSlot, Smi::FromInt(OBJECT_LITERAL_FAST_ELEMENTS));
|
2010-03-11 10:34:29 +00:00
|
|
|
} else {
|
2013-06-06 13:28:22 +00:00
|
|
|
result->set(kLiteralTypeSlot, Smi::FromInt(OBJECT_LITERAL_SLOW_ELEMENTS));
|
2010-03-11 10:34:29 +00:00
|
|
|
}
|
2009-03-23 07:27:47 +00:00
|
|
|
result->set(kElementsSlot, *object_literal->constant_properties());
|
|
|
|
} else {
|
|
|
|
ArrayLiteral* array_literal = expression->AsArrayLiteral();
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(array_literal != NULL && array_literal->is_simple());
|
2013-06-06 13:28:22 +00:00
|
|
|
result->set(kLiteralTypeSlot, Smi::FromInt(ARRAY_LITERAL));
|
2009-12-22 12:41:45 +00:00
|
|
|
result->set(kElementsSlot, *array_literal->constant_elements());
|
2009-03-23 07:27:47 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-06 13:28:22 +00:00
|
|
|
CompileTimeValue::LiteralType CompileTimeValue::GetLiteralType(
|
|
|
|
Handle<FixedArray> value) {
|
|
|
|
Smi* literal_type = Smi::cast(value->get(kLiteralTypeSlot));
|
|
|
|
return static_cast<LiteralType>(literal_type->value());
|
2009-03-23 07:27:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) {
|
|
|
|
return Handle<FixedArray>(FixedArray::cast(value->get(kElementsSlot)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-10 16:11:25 +00:00
|
|
|
void ParserTraits::ParseArrowFunctionFormalParameters(
|
2015-06-22 14:15:53 +00:00
|
|
|
ParserFormalParameterParsingState* parsing_state, Expression* expr,
|
|
|
|
const Scanner::Location& params_loc, Scanner::Location* duplicate_loc,
|
|
|
|
bool* ok) {
|
|
|
|
if (parsing_state->scope->num_parameters() >= Code::kMaxArguments) {
|
2015-05-18 08:34:05 +00:00
|
|
|
ReportMessageAt(params_loc, MessageTemplate::kMalformedArrowFunParamList);
|
2015-04-21 14:44:18 +00:00
|
|
|
*ok = false;
|
|
|
|
return;
|
|
|
|
}
|
2015-04-17 09:51:22 +00:00
|
|
|
|
2015-04-21 14:44:18 +00:00
|
|
|
// ArrowFunctionFormals ::
|
2015-06-10 16:11:25 +00:00
|
|
|
// Binary(Token::COMMA, NonTailArrowFunctionFormals, Tail)
|
|
|
|
// Tail
|
|
|
|
// NonTailArrowFunctionFormals ::
|
|
|
|
// Binary(Token::COMMA, NonTailArrowFunctionFormals, VariableProxy)
|
|
|
|
// VariableProxy
|
|
|
|
// Tail ::
|
2015-04-21 14:44:18 +00:00
|
|
|
// VariableProxy
|
2015-06-10 16:11:25 +00:00
|
|
|
// Spread(VariableProxy)
|
2015-04-21 14:44:18 +00:00
|
|
|
//
|
|
|
|
// As we need to visit the parameters in left-to-right order, we recurse on
|
|
|
|
// the left-hand side of comma expressions.
|
|
|
|
//
|
|
|
|
if (expr->IsBinaryOperation()) {
|
|
|
|
BinaryOperation* binop = expr->AsBinaryOperation();
|
2015-05-13 13:32:27 +00:00
|
|
|
// The classifier has already run, so we know that the expression is a valid
|
|
|
|
// arrow function formals production.
|
|
|
|
DCHECK_EQ(binop->op(), Token::COMMA);
|
2015-04-21 14:44:18 +00:00
|
|
|
Expression* left = binop->left();
|
|
|
|
Expression* right = binop->right();
|
2015-06-22 14:15:53 +00:00
|
|
|
ParseArrowFunctionFormalParameters(parsing_state, left, params_loc,
|
2015-06-10 16:11:25 +00:00
|
|
|
duplicate_loc, ok);
|
2015-04-21 14:44:18 +00:00
|
|
|
if (!*ok) return;
|
|
|
|
// LHS of comma expression should be unparenthesized.
|
|
|
|
expr = right;
|
|
|
|
}
|
2015-04-21 11:09:53 +00:00
|
|
|
|
2015-06-10 16:11:25 +00:00
|
|
|
// Only the right-most expression may be a rest parameter.
|
2015-06-22 14:15:53 +00:00
|
|
|
DCHECK(!parsing_state->has_rest);
|
2015-06-10 16:11:25 +00:00
|
|
|
|
2015-06-22 14:15:53 +00:00
|
|
|
bool is_rest = false;
|
2015-06-10 16:11:25 +00:00
|
|
|
if (expr->IsSpread()) {
|
2015-06-22 14:15:53 +00:00
|
|
|
is_rest = true;
|
2015-06-10 16:11:25 +00:00
|
|
|
expr = expr->AsSpread()->expression();
|
|
|
|
}
|
|
|
|
|
2015-06-22 14:15:53 +00:00
|
|
|
if (expr->IsVariableProxy()) {
|
|
|
|
// When the formal parameter was originally seen, it was parsed as a
|
|
|
|
// VariableProxy and recorded as unresolved in the scope. Here we undo that
|
|
|
|
// parse-time side-effect for parameters that are single-names (not
|
|
|
|
// patterns; for patterns that happens uniformly in
|
|
|
|
// PatternRewriter::VisitVariableProxy).
|
|
|
|
parser_->scope_->RemoveUnresolved(expr->AsVariableProxy());
|
2015-06-15 17:06:34 +00:00
|
|
|
}
|
2015-04-21 14:44:18 +00:00
|
|
|
|
2015-06-09 17:13:35 +00:00
|
|
|
ExpressionClassifier classifier;
|
2015-06-22 14:15:53 +00:00
|
|
|
DeclareFormalParameter(parsing_state, expr, &classifier, is_rest);
|
2015-06-10 16:11:25 +00:00
|
|
|
if (!duplicate_loc->IsValid()) {
|
|
|
|
*duplicate_loc = classifier.duplicate_formal_parameter_error().location;
|
|
|
|
}
|
Implement handling of arrow functions in the parser
Arrow functions are parsed from ParseAssignmentExpression(). Handling the
parameter list is done by letting ParseConditionalExpression() parse a comma
separated list of identifiers, and it returns a tree of BinaryOperation nodes
with VariableProxy leaves, or a single VariableProxy if there is only one
parameter. When the arrow token "=>" is found, the VariableProxy nodes are
passed to ParseArrowFunctionLiteral(), which will then skip parsing the
paramaeter list. This avoids having to rewind when the arrow is found and
restart parsing the parameter list.
Note that the empty parameter list "()" is handled directly in
ParsePrimaryExpression(): after is has consumed the opening parenthesis,
if a closing parenthesis follows, then the only valid input is an arrow
function. In this case, ParsePrimaryExpression() directly calls
ParseArrowFunctionLiteral(), to avoid needing to return a sentinel value
to signal the empty parameter list. Because it will consume the body of
the arrow function, ParseAssignmentExpression() will not see the arrow
"=>" token as next, and return the already-parser expression.
The implementation is done in ParserBase, so it was needed to do some
additions to ParserBase, ParserTraits and PreParserTraits. Some of the
glue code can be removed later on when more more functionality is moved
to ParserBase.
Additionally, this adds a runtime flag "harmony_arrow_functions"
(disabled by default); enabling "harmony" will enable it as well.
BUG=v8:2700
LOG=N
R=marja@chromium.org
Review URL: https://codereview.chromium.org/383983002
Patch from Adrián Pérez de Castro <aperez@igalia.com>.
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@22366 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2014-07-14 07:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-26 21:39:43 +00:00
|
|
|
void ParserTraits::ReindexLiterals(
|
|
|
|
const ParserFormalParameterParsingState& parsing_state) {
|
|
|
|
if (parser_->function_state_->materialized_literal_count() > 0) {
|
|
|
|
AstLiteralReindexer reindexer;
|
|
|
|
|
|
|
|
for (const auto p : parsing_state.params) {
|
|
|
|
if (p.pattern != nullptr) reindexer.Reindex(p.pattern);
|
|
|
|
}
|
|
|
|
DCHECK(reindexer.count() <=
|
|
|
|
parser_->function_state_->materialized_literal_count());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-06 13:28:22 +00:00
|
|
|
FunctionLiteral* Parser::ParseFunctionLiteral(
|
2014-09-10 16:39:42 +00:00
|
|
|
const AstRawString* function_name, Scanner::Location function_name_location,
|
2015-07-09 21:31:11 +00:00
|
|
|
FunctionNameValidity function_name_validity, FunctionKind kind,
|
|
|
|
int function_token_pos, FunctionLiteral::FunctionType function_type,
|
2014-09-10 16:39:42 +00:00
|
|
|
FunctionLiteral::ArityRestriction arity_restriction, bool* ok) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// Function ::
|
|
|
|
// '(' FormalParameterList? ')' '{' FunctionBody '}'
|
2014-06-17 07:23:26 +00:00
|
|
|
//
|
|
|
|
// Getter ::
|
|
|
|
// '(' ')' '{' FunctionBody '}'
|
|
|
|
//
|
|
|
|
// Setter ::
|
|
|
|
// '(' PropertySetParameterList ')' '{' FunctionBody '}'
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2013-10-14 09:24:58 +00:00
|
|
|
int pos = function_token_pos == RelocInfo::kNoPosition
|
|
|
|
? peek_position() : function_token_pos;
|
|
|
|
|
2014-09-10 16:39:42 +00:00
|
|
|
bool is_generator = IsGeneratorFunction(kind);
|
|
|
|
|
2011-08-08 16:14:46 +00:00
|
|
|
// Anonymous functions were passed either the empty symbol or a null
|
|
|
|
// handle as the function name. Remember if we were passed a non-empty
|
|
|
|
// handle to decide whether to invoke function name inference.
|
2014-06-24 14:03:24 +00:00
|
|
|
bool should_infer_name = function_name == NULL;
|
2011-08-08 16:14:46 +00:00
|
|
|
|
|
|
|
// We want a non-null handle as the function name.
|
|
|
|
if (should_infer_name) {
|
2014-09-11 09:52:36 +00:00
|
|
|
function_name = ast_value_factory()->empty_string();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int num_parameters = 0;
|
2011-08-16 14:24:12 +00:00
|
|
|
// Function declarations are function scoped in normal mode, so they are
|
|
|
|
// hoisted. In harmony block scoping mode they are block scoped, so they
|
|
|
|
// are not hoisted.
|
2013-08-23 09:25:37 +00:00
|
|
|
//
|
|
|
|
// One tricky case are function declarations in a local sloppy-mode eval:
|
|
|
|
// their declaration is hoisted, but they still see the local scope. E.g.,
|
|
|
|
//
|
|
|
|
// function() {
|
|
|
|
// var x = 0
|
|
|
|
// try { throw 1 } catch (x) { eval("function g() { return x }") }
|
|
|
|
// return g()
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// needs to return 1. To distinguish such cases, we need to detect
|
|
|
|
// (1) whether a function stems from a sloppy eval, and
|
|
|
|
// (2) whether it actually hoists across the eval.
|
|
|
|
// Unfortunately, we do not represent sloppy eval scopes, so we do not have
|
|
|
|
// either information available directly, especially not when lazily compiling
|
|
|
|
// a function like 'g'. We hence rely on the following invariants:
|
|
|
|
// - (1) is the case iff the innermost scope of the deserialized scope chain
|
|
|
|
// under which we compile is _not_ a declaration scope. This holds because
|
|
|
|
// in all normal cases, function declarations are fully hoisted to a
|
|
|
|
// declaration scope and compiled relative to that.
|
|
|
|
// - (2) is the case iff the current declaration scope is still the original
|
|
|
|
// one relative to the deserialized scope chain. Otherwise we must be
|
|
|
|
// compiling a function in an inner declaration scope in the eval, e.g. a
|
|
|
|
// nested function, and hoisting works normally relative to that.
|
2014-02-12 12:02:07 +00:00
|
|
|
Scope* declaration_scope = scope_->DeclarationScope();
|
2013-08-23 09:25:37 +00:00
|
|
|
Scope* original_declaration_scope = original_scope_->DeclarationScope();
|
2015-03-13 15:15:42 +00:00
|
|
|
Scope* scope = function_type == FunctionLiteral::DECLARATION &&
|
|
|
|
is_sloppy(language_mode()) &&
|
2015-07-08 15:04:04 +00:00
|
|
|
!allow_harmony_sloppy() &&
|
2015-03-13 15:15:42 +00:00
|
|
|
(original_scope_ == original_declaration_scope ||
|
|
|
|
declaration_scope != original_declaration_scope)
|
|
|
|
? NewScope(declaration_scope, FUNCTION_SCOPE, kind)
|
|
|
|
: NewScope(scope_, FUNCTION_SCOPE, kind);
|
2011-11-11 13:48:14 +00:00
|
|
|
ZoneList<Statement*>* body = NULL;
|
2011-11-25 09:36:31 +00:00
|
|
|
int materialized_literal_count = -1;
|
|
|
|
int expected_property_count = -1;
|
2015-06-22 14:15:53 +00:00
|
|
|
DuplicateFinder duplicate_finder(scanner()->unicode_cache());
|
|
|
|
ExpressionClassifier formals_classifier(&duplicate_finder);
|
2015-04-24 11:07:50 +00:00
|
|
|
FunctionLiteral::EagerCompileHint eager_compile_hint =
|
|
|
|
parenthesized_function_ ? FunctionLiteral::kShouldEagerCompile
|
|
|
|
: FunctionLiteral::kShouldLazyCompile;
|
2015-05-06 10:21:20 +00:00
|
|
|
bool should_be_used_once_hint = false;
|
2008-07-03 15:10:15 +00:00
|
|
|
// Parse function body.
|
2014-06-24 14:03:24 +00:00
|
|
|
{
|
2014-11-14 13:13:09 +00:00
|
|
|
AstNodeFactory function_factory(ast_value_factory());
|
2015-02-06 10:34:50 +00:00
|
|
|
FunctionState function_state(&function_state_, &scope_, scope, kind,
|
2014-10-09 08:16:13 +00:00
|
|
|
&function_factory);
|
2014-02-12 12:02:07 +00:00
|
|
|
scope_->SetScopeName(function_name);
|
2013-04-15 12:29:44 +00:00
|
|
|
|
|
|
|
if (is_generator) {
|
|
|
|
// For generators, allocating variables in contexts is currently a win
|
|
|
|
// because it minimizes the work needed to suspend and resume an
|
|
|
|
// activation.
|
2014-02-12 12:02:07 +00:00
|
|
|
scope_->ForceContextAllocation();
|
2013-04-15 12:29:44 +00:00
|
|
|
|
|
|
|
// Calling a generator returns a generator object. That object is stored
|
|
|
|
// in a temporary variable, a definition that is used by "yield"
|
2014-02-13 16:17:55 +00:00
|
|
|
// expressions. This also marks the FunctionState as a generator.
|
2014-02-12 12:02:07 +00:00
|
|
|
Variable* temp = scope_->DeclarationScope()->NewTemporary(
|
2014-09-11 09:52:36 +00:00
|
|
|
ast_value_factory()->dot_generator_object_string());
|
2013-04-15 12:29:44 +00:00
|
|
|
function_state.set_generator_object_variable(temp);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2015-04-13 08:07:05 +00:00
|
|
|
Expect(Token::LPAREN, CHECK_OK);
|
2015-04-21 11:09:53 +00:00
|
|
|
int start_position = scanner()->location().beg_pos;
|
|
|
|
scope_->set_start_position(start_position);
|
2015-06-22 14:15:53 +00:00
|
|
|
ParserFormalParameterParsingState parsing_state(scope);
|
|
|
|
num_parameters =
|
|
|
|
ParseFormalParameterList(&parsing_state, &formals_classifier, CHECK_OK);
|
2015-04-17 09:51:22 +00:00
|
|
|
Expect(Token::RPAREN, CHECK_OK);
|
2015-04-21 11:09:53 +00:00
|
|
|
int formals_end_position = scanner()->location().end_pos;
|
|
|
|
|
2015-06-22 14:15:53 +00:00
|
|
|
CheckArityRestrictions(num_parameters, arity_restriction,
|
|
|
|
parsing_state.has_rest, start_position,
|
|
|
|
formals_end_position, CHECK_OK);
|
2008-07-03 15:10:15 +00:00
|
|
|
Expect(Token::LBRACE, CHECK_OK);
|
|
|
|
|
|
|
|
// If we have a named function expression, we add a local variable
|
|
|
|
// declaration to the body of the function with the name of the
|
|
|
|
// function and let it refer to the function itself (closure).
|
|
|
|
// NOTE: We create a proxy and resolve it here so that in the
|
|
|
|
// future we can change the AST to only refer to VariableProxies
|
|
|
|
// instead of Variables and Proxis as is the case now.
|
2011-11-11 13:48:14 +00:00
|
|
|
Variable* fvar = NULL;
|
2014-03-11 14:41:22 +00:00
|
|
|
Token::Value fvar_init_op = Token::INIT_CONST_LEGACY;
|
2013-06-06 13:28:22 +00:00
|
|
|
if (function_type == FunctionLiteral::NAMED_EXPRESSION) {
|
2015-07-08 15:04:04 +00:00
|
|
|
bool use_strict_const = is_strict(language_mode()) ||
|
|
|
|
(!allow_legacy_const() && allow_harmony_sloppy());
|
|
|
|
if (use_strict_const) {
|
2014-03-11 14:41:22 +00:00
|
|
|
fvar_init_op = Token::INIT_CONST;
|
|
|
|
}
|
2015-07-08 15:04:04 +00:00
|
|
|
VariableMode fvar_mode = use_strict_const ? CONST : CONST_LEGACY;
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(function_name != NULL);
|
2014-07-30 13:54:45 +00:00
|
|
|
fvar = new (zone())
|
2015-03-24 13:08:26 +00:00
|
|
|
Variable(scope_, function_name, fvar_mode, Variable::NORMAL,
|
|
|
|
kCreatedInitialized, kNotAssigned);
|
2012-04-16 11:48:20 +00:00
|
|
|
VariableProxy* proxy = factory()->NewVariableProxy(fvar);
|
2013-10-14 09:24:58 +00:00
|
|
|
VariableDeclaration* fvar_declaration = factory()->NewVariableDeclaration(
|
2014-02-12 12:02:07 +00:00
|
|
|
proxy, fvar_mode, scope_, RelocInfo::kNoPosition);
|
|
|
|
scope_->DeclareFunctionVar(fvar_declaration);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2014-02-19 14:50:33 +00:00
|
|
|
// Determine if the function can be parsed lazily. Lazy parsing is different
|
|
|
|
// from lazy compilation; we need to parse more eagerly than we compile.
|
|
|
|
|
|
|
|
// We can only parse lazily if we also compile lazily. The heuristics for
|
|
|
|
// lazy compilation are:
|
2011-11-25 09:36:31 +00:00
|
|
|
// - It must not have been prohibited by the caller to Parse (some callers
|
|
|
|
// need a full AST).
|
2012-06-28 14:56:28 +00:00
|
|
|
// - The outer scope must allow lazy compilation of inner functions.
|
2011-11-25 09:36:31 +00:00
|
|
|
// - The function mustn't be a function expression with an open parenthesis
|
|
|
|
// before; we consider that a hint that the function will be called
|
|
|
|
// immediately, and it would be a waste of time to make it lazily
|
|
|
|
// compiled.
|
|
|
|
// These are all things we can know at this point, without looking at the
|
|
|
|
// function itself.
|
2014-02-19 14:50:33 +00:00
|
|
|
|
|
|
|
// In addition, we need to distinguish between these cases:
|
|
|
|
// (function foo() {
|
|
|
|
// bar = function() { return 1; }
|
|
|
|
// })();
|
|
|
|
// and
|
|
|
|
// (function foo() {
|
|
|
|
// var a = 1;
|
|
|
|
// bar = function() { return a; }
|
|
|
|
// })();
|
|
|
|
|
|
|
|
// Now foo will be parsed eagerly and compiled eagerly (optimization: assume
|
|
|
|
// parenthesis before the function means that it will be called
|
|
|
|
// immediately). The inner function *must* be parsed eagerly to resolve the
|
|
|
|
// possible reference to the variable in foo's scope. However, it's possible
|
|
|
|
// that it will be compiled lazily.
|
|
|
|
|
|
|
|
// To make this additional case work, both Parser and PreParser implement a
|
|
|
|
// logic where only top-level functions will be parsed lazily.
|
2015-06-29 16:16:05 +00:00
|
|
|
bool is_lazily_parsed = mode() == PARSE_LAZILY &&
|
|
|
|
scope_->AllowsLazyParsing() &&
|
|
|
|
!parenthesized_function_;
|
2011-01-14 10:50:13 +00:00
|
|
|
parenthesized_function_ = false; // The bit was set for this function only.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2015-05-06 10:21:20 +00:00
|
|
|
// Eager or lazy parse?
|
|
|
|
// If is_lazily_parsed, we'll parse lazy. If we can set a bookmark, we'll
|
|
|
|
// pass it to SkipLazyFunctionBody, which may use it to abort lazy
|
|
|
|
// parsing if it suspect that wasn't a good idea. If so, or if we didn't
|
|
|
|
// try to lazy parse in the first place, we'll have to parse eagerly.
|
|
|
|
Scanner::BookmarkScope bookmark(scanner());
|
2014-02-19 14:50:33 +00:00
|
|
|
if (is_lazily_parsed) {
|
2015-05-06 10:21:20 +00:00
|
|
|
Scanner::BookmarkScope* maybe_bookmark =
|
|
|
|
bookmark.Set() ? &bookmark : nullptr;
|
2015-04-22 11:04:25 +00:00
|
|
|
SkipLazyFunctionBody(&materialized_literal_count,
|
2015-05-06 10:21:20 +00:00
|
|
|
&expected_property_count, /*CHECK_OK*/ ok,
|
|
|
|
maybe_bookmark);
|
|
|
|
|
|
|
|
if (bookmark.HasBeenReset()) {
|
|
|
|
// Trigger eager (re-)parsing, just below this block.
|
|
|
|
is_lazily_parsed = false;
|
|
|
|
|
|
|
|
// This is probably an initialization function. Inform the compiler it
|
|
|
|
// should also eager-compile this function, and that we expect it to be
|
|
|
|
// used once.
|
|
|
|
eager_compile_hint = FunctionLiteral::kShouldEagerCompile;
|
|
|
|
should_be_used_once_hint = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!is_lazily_parsed) {
|
2015-06-22 14:15:53 +00:00
|
|
|
body = ParseEagerFunctionBody(function_name, pos, parsing_state, fvar,
|
|
|
|
fvar_init_op, kind, CHECK_OK);
|
2011-11-09 13:54:26 +00:00
|
|
|
materialized_literal_count = function_state.materialized_literal_count();
|
|
|
|
expected_property_count = function_state.expected_property_count();
|
2015-04-22 11:04:25 +00:00
|
|
|
|
|
|
|
if (is_strong(language_mode()) && IsSubclassConstructor(kind)) {
|
|
|
|
if (!function_state.super_location().IsValid()) {
|
|
|
|
ReportMessageAt(function_name_location,
|
2015-05-18 08:34:05 +00:00
|
|
|
MessageTemplate::kStrongSuperCallMissing,
|
|
|
|
kReferenceError);
|
2015-04-22 11:04:25 +00:00
|
|
|
*ok = false;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2015-02-06 18:04:11 +00:00
|
|
|
// Validate name and parameter names. We can do this only after parsing the
|
|
|
|
// function, since the function can declare itself strict.
|
2015-07-09 21:31:11 +00:00
|
|
|
CheckFunctionName(language_mode(), function_name, function_name_validity,
|
|
|
|
function_name_location, CHECK_OK);
|
2015-06-22 14:15:53 +00:00
|
|
|
const bool use_strict_params =
|
|
|
|
!parsing_state.is_simple_parameter_list || IsConciseMethod(kind);
|
2015-05-13 11:45:04 +00:00
|
|
|
const bool allow_duplicate_parameters =
|
|
|
|
is_sloppy(language_mode()) && !use_strict_params;
|
|
|
|
ValidateFormalParameters(&formals_classifier, language_mode(),
|
|
|
|
allow_duplicate_parameters, CHECK_OK);
|
2015-02-06 18:04:11 +00:00
|
|
|
|
2015-02-04 09:34:05 +00:00
|
|
|
if (is_strict(language_mode())) {
|
2014-12-18 22:01:25 +00:00
|
|
|
CheckStrictOctalLiteral(scope->start_position(), scope->end_position(),
|
|
|
|
CHECK_OK);
|
2015-07-10 16:39:47 +00:00
|
|
|
}
|
|
|
|
if (is_strict(language_mode()) || allow_harmony_sloppy()) {
|
2014-07-09 11:35:05 +00:00
|
|
|
CheckConflictingVarDeclarations(scope, CHECK_OK);
|
|
|
|
}
|
2011-09-01 12:31:18 +00:00
|
|
|
}
|
|
|
|
|
2015-05-13 11:45:04 +00:00
|
|
|
bool has_duplicate_parameters =
|
|
|
|
!formals_classifier.is_valid_formal_parameter_list_without_duplicates();
|
2015-04-21 11:09:53 +00:00
|
|
|
FunctionLiteral::ParameterFlag duplicate_parameters =
|
2015-05-13 11:45:04 +00:00
|
|
|
has_duplicate_parameters ? FunctionLiteral::kHasDuplicateParameters
|
|
|
|
: FunctionLiteral::kNoDuplicateParameters;
|
2015-04-21 11:09:53 +00:00
|
|
|
|
2014-07-21 09:58:01 +00:00
|
|
|
FunctionLiteral* function_literal = factory()->NewFunctionLiteral(
|
2014-09-11 09:52:36 +00:00
|
|
|
function_name, ast_value_factory(), scope, body,
|
2015-06-08 18:19:32 +00:00
|
|
|
materialized_literal_count, expected_property_count, num_parameters,
|
|
|
|
duplicate_parameters, function_type, FunctionLiteral::kIsFunction,
|
|
|
|
eager_compile_hint, kind, pos);
|
2013-10-14 09:24:58 +00:00
|
|
|
function_literal->set_function_token_position(function_token_pos);
|
2015-05-06 10:21:20 +00:00
|
|
|
if (should_be_used_once_hint)
|
|
|
|
function_literal->set_should_be_used_once_hint();
|
2011-04-07 14:45:34 +00:00
|
|
|
|
2015-02-14 00:14:46 +00:00
|
|
|
if (scope->has_rest_parameter()) {
|
|
|
|
// TODO(caitp): enable optimization of functions with rest params
|
|
|
|
function_literal->set_dont_optimize_reason(kRestParameter);
|
|
|
|
}
|
|
|
|
|
2011-08-08 16:14:46 +00:00
|
|
|
if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal);
|
2011-04-07 14:45:34 +00:00
|
|
|
return function_literal;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-22 11:04:25 +00:00
|
|
|
void Parser::SkipLazyFunctionBody(int* materialized_literal_count,
|
2015-05-06 10:21:20 +00:00
|
|
|
int* expected_property_count, bool* ok,
|
|
|
|
Scanner::BookmarkScope* bookmark) {
|
|
|
|
DCHECK_IMPLIES(bookmark, bookmark->HasBeenSet());
|
2014-11-17 12:16:27 +00:00
|
|
|
if (produce_cached_parse_data()) CHECK(log_);
|
2014-10-01 16:54:42 +00:00
|
|
|
|
2014-04-15 08:29:24 +00:00
|
|
|
int function_block_pos = position();
|
2014-12-08 11:47:44 +00:00
|
|
|
if (consume_cached_parse_data() && !cached_parse_data_->rejected()) {
|
2014-04-15 08:29:24 +00:00
|
|
|
// If we have cached data, we use it to skip parsing the function body. The
|
|
|
|
// data contains the information we need to construct the lazy function.
|
|
|
|
FunctionEntry entry =
|
2014-07-10 10:28:05 +00:00
|
|
|
cached_parse_data_->GetFunctionEntry(function_block_pos);
|
2014-12-08 11:47:44 +00:00
|
|
|
// Check that cached data is valid. If not, mark it as invalid (the embedder
|
|
|
|
// handles it). Note that end position greater than end of stream is safe,
|
|
|
|
// and hard to check.
|
|
|
|
if (entry.is_valid() && entry.end_pos() > function_block_pos) {
|
|
|
|
scanner()->SeekForward(entry.end_pos() - 1);
|
|
|
|
|
|
|
|
scope_->set_end_position(entry.end_pos());
|
|
|
|
Expect(Token::RBRACE, ok);
|
|
|
|
if (!*ok) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
total_preparse_skipped_ += scope_->end_position() - function_block_pos;
|
|
|
|
*materialized_literal_count = entry.literal_count();
|
|
|
|
*expected_property_count = entry.property_count();
|
2015-02-04 09:34:05 +00:00
|
|
|
scope_->SetLanguageMode(entry.language_mode());
|
2015-02-13 18:34:52 +00:00
|
|
|
if (entry.uses_super_property()) scope_->RecordSuperPropertyUsage();
|
2015-06-04 21:16:18 +00:00
|
|
|
if (entry.calls_eval()) scope_->RecordEvalCall();
|
2014-04-15 08:29:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-12-08 11:47:44 +00:00
|
|
|
cached_parse_data_->Reject();
|
|
|
|
}
|
|
|
|
// With no cached data, we partially parse the function, without building an
|
|
|
|
// AST. This gathers the data needed to build a lazy function.
|
|
|
|
SingletonLogger logger;
|
|
|
|
PreParser::PreParseResult result =
|
2015-05-06 10:21:20 +00:00
|
|
|
ParseLazyFunctionBodyWithPreParser(&logger, bookmark);
|
|
|
|
if (bookmark && bookmark->HasBeenReset()) {
|
|
|
|
return; // Return immediately if pre-parser devided to abort parsing.
|
|
|
|
}
|
2014-12-08 11:47:44 +00:00
|
|
|
if (result == PreParser::kPreParseStackOverflow) {
|
|
|
|
// Propagate stack overflow.
|
|
|
|
set_stack_overflow();
|
|
|
|
*ok = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (logger.has_error()) {
|
|
|
|
ParserTraits::ReportMessageAt(
|
|
|
|
Scanner::Location(logger.start(), logger.end()), logger.message(),
|
2015-02-20 21:19:43 +00:00
|
|
|
logger.argument_opt(), logger.error_type());
|
2014-12-08 11:47:44 +00:00
|
|
|
*ok = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
scope_->set_end_position(logger.end());
|
|
|
|
Expect(Token::RBRACE, ok);
|
|
|
|
if (!*ok) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
total_preparse_skipped_ += scope_->end_position() - function_block_pos;
|
|
|
|
*materialized_literal_count = logger.literals();
|
|
|
|
*expected_property_count = logger.properties();
|
2015-02-04 09:34:05 +00:00
|
|
|
scope_->SetLanguageMode(logger.language_mode());
|
2015-06-04 21:16:18 +00:00
|
|
|
if (logger.uses_super_property()) {
|
2015-02-13 18:34:52 +00:00
|
|
|
scope_->RecordSuperPropertyUsage();
|
|
|
|
}
|
2015-06-04 21:16:18 +00:00
|
|
|
if (logger.calls_eval()) {
|
|
|
|
scope_->RecordEvalCall();
|
|
|
|
}
|
2014-12-08 11:47:44 +00:00
|
|
|
if (produce_cached_parse_data()) {
|
|
|
|
DCHECK(log_);
|
|
|
|
// Position right after terminal '}'.
|
|
|
|
int body_end = scanner()->location().end_pos;
|
|
|
|
log_->LogFunction(function_block_pos, body_end, *materialized_literal_count,
|
2015-02-13 18:34:52 +00:00
|
|
|
*expected_property_count, scope_->language_mode(),
|
2015-06-04 21:16:18 +00:00
|
|
|
scope_->uses_super_property(), scope_->calls_eval());
|
2014-04-15 08:29:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-11 17:22:50 +00:00
|
|
|
void Parser::AddAssertIsConstruct(ZoneList<Statement*>* body, int pos) {
|
|
|
|
ZoneList<Expression*>* arguments =
|
|
|
|
new (zone()) ZoneList<Expression*>(0, zone());
|
|
|
|
CallRuntime* construct_check = factory()->NewCallRuntime(
|
|
|
|
ast_value_factory()->is_construct_call_string(),
|
|
|
|
Runtime::FunctionForId(Runtime::kInlineIsConstructCall), arguments, pos);
|
|
|
|
CallRuntime* non_callable_error = factory()->NewCallRuntime(
|
|
|
|
ast_value_factory()->empty_string(),
|
|
|
|
Runtime::FunctionForId(Runtime::kThrowConstructorNonCallableError),
|
|
|
|
arguments, pos);
|
|
|
|
IfStatement* if_statement = factory()->NewIfStatement(
|
|
|
|
factory()->NewUnaryOperation(Token::NOT, construct_check, pos),
|
|
|
|
factory()->NewReturnStatement(non_callable_error, pos),
|
|
|
|
factory()->NewEmptyStatement(pos), pos);
|
|
|
|
body->Add(if_statement, zone());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-18 20:15:08 +00:00
|
|
|
Statement* Parser::BuildAssertIsCoercible(Variable* var) {
|
|
|
|
// if (var === null || var === undefined)
|
|
|
|
// throw /* type error kNonCoercible) */;
|
|
|
|
|
|
|
|
Expression* condition = factory()->NewBinaryOperation(
|
|
|
|
Token::OR, factory()->NewCompareOperation(
|
|
|
|
Token::EQ_STRICT, factory()->NewVariableProxy(var),
|
|
|
|
factory()->NewUndefinedLiteral(RelocInfo::kNoPosition),
|
|
|
|
RelocInfo::kNoPosition),
|
|
|
|
factory()->NewCompareOperation(
|
|
|
|
Token::EQ_STRICT, factory()->NewVariableProxy(var),
|
|
|
|
factory()->NewNullLiteral(RelocInfo::kNoPosition),
|
|
|
|
RelocInfo::kNoPosition),
|
|
|
|
RelocInfo::kNoPosition);
|
|
|
|
Expression* throw_type_error = this->NewThrowTypeError(
|
|
|
|
MessageTemplate::kNonCoercible, ast_value_factory()->empty_string(),
|
|
|
|
RelocInfo::kNoPosition);
|
|
|
|
IfStatement* if_statement = factory()->NewIfStatement(
|
|
|
|
condition, factory()->NewExpressionStatement(throw_type_error,
|
|
|
|
RelocInfo::kNoPosition),
|
|
|
|
factory()->NewEmptyStatement(RelocInfo::kNoPosition),
|
|
|
|
RelocInfo::kNoPosition);
|
|
|
|
return if_statement;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-22 14:15:53 +00:00
|
|
|
Block* Parser::BuildParameterInitializationBlock(
|
|
|
|
const ParserFormalParameterParsingState& formal_parameters, bool* ok) {
|
|
|
|
DCHECK(scope_->is_function_scope());
|
|
|
|
Block* init_block = nullptr;
|
|
|
|
for (auto parameter : formal_parameters.params) {
|
|
|
|
if (parameter.pattern == nullptr) continue;
|
|
|
|
if (init_block == nullptr) {
|
|
|
|
init_block = factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition);
|
|
|
|
}
|
|
|
|
|
|
|
|
DeclarationDescriptor descriptor;
|
|
|
|
descriptor.declaration_kind = DeclarationDescriptor::PARAMETER;
|
|
|
|
descriptor.parser = this;
|
|
|
|
descriptor.declaration_scope = scope_;
|
|
|
|
descriptor.scope = scope_;
|
|
|
|
descriptor.mode = LET;
|
|
|
|
descriptor.is_const = false;
|
|
|
|
descriptor.needs_init = true;
|
|
|
|
descriptor.declaration_pos = parameter.pattern->position();
|
|
|
|
descriptor.initialization_pos = parameter.pattern->position();
|
|
|
|
descriptor.init_op = Token::INIT_LET;
|
|
|
|
DeclarationParsingResult::Declaration decl(
|
|
|
|
parameter.pattern, parameter.pattern->position(),
|
|
|
|
factory()->NewVariableProxy(parameter.var));
|
|
|
|
PatternRewriter::DeclareAndInitializeVariables(init_block, &descriptor,
|
|
|
|
&decl, nullptr, CHECK_OK);
|
|
|
|
}
|
|
|
|
return init_block;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-15 08:29:24 +00:00
|
|
|
ZoneList<Statement*>* Parser::ParseEagerFunctionBody(
|
2015-06-22 14:15:53 +00:00
|
|
|
const AstRawString* function_name, int pos,
|
|
|
|
const ParserFormalParameterParsingState& formal_parameters, Variable* fvar,
|
2015-02-06 10:34:50 +00:00
|
|
|
Token::Value fvar_init_op, FunctionKind kind, bool* ok) {
|
2014-04-15 08:29:24 +00:00
|
|
|
// Everything inside an eagerly parsed function will be parsed eagerly
|
|
|
|
// (see comment above).
|
|
|
|
ParsingModeScope parsing_mode(this, PARSE_EAGERLY);
|
|
|
|
ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(8, zone());
|
|
|
|
if (fvar != NULL) {
|
2015-02-17 20:51:24 +00:00
|
|
|
VariableProxy* fproxy = scope_->NewUnresolved(factory(), function_name);
|
2014-04-15 08:29:24 +00:00
|
|
|
fproxy->BindTo(fvar);
|
|
|
|
body->Add(factory()->NewExpressionStatement(
|
|
|
|
factory()->NewAssignment(fvar_init_op,
|
|
|
|
fproxy,
|
|
|
|
factory()->NewThisFunction(pos),
|
|
|
|
RelocInfo::kNoPosition),
|
|
|
|
RelocInfo::kNoPosition), zone());
|
|
|
|
}
|
|
|
|
|
2015-02-06 10:34:50 +00:00
|
|
|
|
|
|
|
// For concise constructors, check that they are constructed,
|
|
|
|
// not called.
|
2015-02-11 17:22:50 +00:00
|
|
|
if (i::IsConstructor(kind)) {
|
|
|
|
AddAssertIsConstruct(body, pos);
|
2015-02-06 10:34:50 +00:00
|
|
|
}
|
|
|
|
|
2015-06-22 14:15:53 +00:00
|
|
|
auto init_block =
|
|
|
|
BuildParameterInitializationBlock(formal_parameters, CHECK_OK);
|
|
|
|
if (init_block != nullptr) {
|
|
|
|
body->Add(init_block, zone());
|
|
|
|
}
|
|
|
|
|
2014-04-15 08:29:24 +00:00
|
|
|
// For generators, allocate and yield an iterator on function entry.
|
2015-02-06 10:34:50 +00:00
|
|
|
if (IsGeneratorFunction(kind)) {
|
2014-04-15 08:29:24 +00:00
|
|
|
ZoneList<Expression*>* arguments =
|
|
|
|
new(zone()) ZoneList<Expression*>(0, zone());
|
|
|
|
CallRuntime* allocation = factory()->NewCallRuntime(
|
2014-09-11 09:52:36 +00:00
|
|
|
ast_value_factory()->empty_string(),
|
|
|
|
Runtime::FunctionForId(Runtime::kCreateJSGeneratorObject), arguments,
|
|
|
|
pos);
|
2014-04-15 08:29:24 +00:00
|
|
|
VariableProxy* init_proxy = factory()->NewVariableProxy(
|
|
|
|
function_state_->generator_object_variable());
|
|
|
|
Assignment* assignment = factory()->NewAssignment(
|
|
|
|
Token::INIT_VAR, init_proxy, allocation, RelocInfo::kNoPosition);
|
|
|
|
VariableProxy* get_proxy = factory()->NewVariableProxy(
|
|
|
|
function_state_->generator_object_variable());
|
|
|
|
Yield* yield = factory()->NewYield(
|
2014-09-02 07:07:52 +00:00
|
|
|
get_proxy, assignment, Yield::kInitial, RelocInfo::kNoPosition);
|
2014-04-15 08:29:24 +00:00
|
|
|
body->Add(factory()->NewExpressionStatement(
|
|
|
|
yield, RelocInfo::kNoPosition), zone());
|
|
|
|
}
|
|
|
|
|
2015-04-27 12:13:45 +00:00
|
|
|
ParseStatementList(body, Token::RBRACE, CHECK_OK);
|
2014-04-15 08:29:24 +00:00
|
|
|
|
2015-02-06 10:34:50 +00:00
|
|
|
if (IsGeneratorFunction(kind)) {
|
2014-04-15 08:29:24 +00:00
|
|
|
VariableProxy* get_proxy = factory()->NewVariableProxy(
|
|
|
|
function_state_->generator_object_variable());
|
2014-06-24 14:03:24 +00:00
|
|
|
Expression* undefined =
|
|
|
|
factory()->NewUndefinedLiteral(RelocInfo::kNoPosition);
|
2014-09-02 07:07:52 +00:00
|
|
|
Yield* yield = factory()->NewYield(get_proxy, undefined, Yield::kFinal,
|
2014-06-24 14:03:24 +00:00
|
|
|
RelocInfo::kNoPosition);
|
2014-04-15 08:29:24 +00:00
|
|
|
body->Add(factory()->NewExpressionStatement(
|
|
|
|
yield, RelocInfo::kNoPosition), zone());
|
|
|
|
}
|
|
|
|
|
2015-02-12 20:06:52 +00:00
|
|
|
if (IsSubclassConstructor(kind)) {
|
2015-02-06 10:34:50 +00:00
|
|
|
body->Add(
|
|
|
|
factory()->NewReturnStatement(
|
|
|
|
this->ThisExpression(scope_, factory(), RelocInfo::kNoPosition),
|
|
|
|
RelocInfo::kNoPosition),
|
|
|
|
zone());
|
|
|
|
}
|
|
|
|
|
2014-04-15 08:29:24 +00:00
|
|
|
Expect(Token::RBRACE, CHECK_OK);
|
|
|
|
scope_->set_end_position(scanner()->location().end_pos);
|
|
|
|
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PreParser::PreParseResult Parser::ParseLazyFunctionBodyWithPreParser(
|
2015-05-06 10:21:20 +00:00
|
|
|
SingletonLogger* logger, Scanner::BookmarkScope* bookmark) {
|
2014-09-02 11:36:21 +00:00
|
|
|
// This function may be called on a background thread too; record only the
|
|
|
|
// main thread preparse times.
|
|
|
|
if (pre_parse_timer_ != NULL) {
|
|
|
|
pre_parse_timer_->Start();
|
|
|
|
}
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK_EQ(Token::LBRACE, scanner()->current_token());
|
2011-11-25 09:36:31 +00:00
|
|
|
|
|
|
|
if (reusable_preparser_ == NULL) {
|
2015-02-12 13:02:30 +00:00
|
|
|
reusable_preparser_ = new PreParser(zone(), &scanner_, ast_value_factory(),
|
|
|
|
NULL, stack_limit_);
|
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
|
|
|
reusable_preparser_->set_allow_lazy(true);
|
2015-06-09 15:43:07 +00:00
|
|
|
#define SET_ALLOW(name) reusable_preparser_->set_allow_##name(allow_##name());
|
|
|
|
SET_ALLOW(natives);
|
|
|
|
SET_ALLOW(harmony_modules);
|
|
|
|
SET_ALLOW(harmony_arrow_functions);
|
|
|
|
SET_ALLOW(harmony_sloppy);
|
|
|
|
SET_ALLOW(harmony_unicode);
|
|
|
|
SET_ALLOW(harmony_computed_property_names);
|
|
|
|
SET_ALLOW(harmony_rest_params);
|
|
|
|
SET_ALLOW(harmony_spreadcalls);
|
|
|
|
SET_ALLOW(harmony_destructuring);
|
|
|
|
SET_ALLOW(harmony_spread_arrays);
|
|
|
|
SET_ALLOW(harmony_new_target);
|
|
|
|
SET_ALLOW(strong_mode);
|
|
|
|
#undef SET_ALLOW
|
2011-11-25 09:36:31 +00:00
|
|
|
}
|
2015-02-04 09:34:05 +00:00
|
|
|
PreParser::PreParseResult result = reusable_preparser_->PreParseLazyFunction(
|
2015-05-06 10:21:20 +00:00
|
|
|
language_mode(), function_state_->kind(), logger, bookmark);
|
2014-09-02 11:36:21 +00:00
|
|
|
if (pre_parse_timer_ != NULL) {
|
|
|
|
pre_parse_timer_->Stop();
|
|
|
|
}
|
2011-11-25 09:36:31 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-14 15:05:05 +00:00
|
|
|
ClassLiteral* Parser::ParseClassLiteral(const AstRawString* name,
|
|
|
|
Scanner::Location class_name_location,
|
|
|
|
bool name_is_strict_reserved, int pos,
|
|
|
|
bool* ok) {
|
|
|
|
// All parts of a ClassDeclaration and ClassExpression are strict code.
|
|
|
|
if (name_is_strict_reserved) {
|
2015-05-18 08:34:05 +00:00
|
|
|
ReportMessageAt(class_name_location,
|
|
|
|
MessageTemplate::kUnexpectedStrictReserved);
|
2014-11-14 15:05:05 +00:00
|
|
|
*ok = false;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (IsEvalOrArguments(name)) {
|
2015-05-18 08:34:05 +00:00
|
|
|
ReportMessageAt(class_name_location, MessageTemplate::kStrictEvalArguments);
|
2014-11-14 15:05:05 +00:00
|
|
|
*ok = false;
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-04-10 12:04:51 +00:00
|
|
|
if (is_strong(language_mode()) && IsUndefined(name)) {
|
2015-05-18 08:34:05 +00:00
|
|
|
ReportMessageAt(class_name_location, MessageTemplate::kStrongUndefined);
|
2015-04-10 12:04:51 +00:00
|
|
|
*ok = false;
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-11-14 15:05:05 +00:00
|
|
|
|
|
|
|
Scope* block_scope = NewScope(scope_, BLOCK_SCOPE);
|
|
|
|
BlockState block_state(&scope_, block_scope);
|
2015-02-04 09:34:05 +00:00
|
|
|
scope_->SetLanguageMode(
|
2015-02-05 14:11:34 +00:00
|
|
|
static_cast<LanguageMode>(scope_->language_mode() | STRICT_BIT));
|
2014-11-14 15:05:05 +00:00
|
|
|
scope_->SetScopeName(name);
|
|
|
|
|
|
|
|
VariableProxy* proxy = NULL;
|
|
|
|
if (name != NULL) {
|
2015-02-17 20:51:24 +00:00
|
|
|
proxy = NewUnresolved(name, CONST);
|
2015-04-23 14:05:03 +00:00
|
|
|
const bool is_class_declaration = true;
|
|
|
|
Declaration* declaration = factory()->NewVariableDeclaration(
|
|
|
|
proxy, CONST, block_scope, pos, is_class_declaration,
|
|
|
|
scope_->class_declaration_group_start());
|
2015-06-22 14:15:53 +00:00
|
|
|
Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK);
|
2014-11-14 15:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Expression* extends = NULL;
|
|
|
|
if (Check(Token::EXTENDS)) {
|
|
|
|
block_scope->set_start_position(scanner()->location().end_pos);
|
2015-04-22 12:35:05 +00:00
|
|
|
ExpressionClassifier classifier;
|
|
|
|
extends = ParseLeftHandSideExpression(&classifier, CHECK_OK);
|
2015-04-27 14:35:45 +00:00
|
|
|
ValidateExpression(&classifier, CHECK_OK);
|
2014-11-14 15:05:05 +00:00
|
|
|
} else {
|
|
|
|
block_scope->set_start_position(scanner()->location().end_pos);
|
|
|
|
}
|
|
|
|
|
2015-01-29 23:12:25 +00:00
|
|
|
|
|
|
|
ClassLiteralChecker checker(this);
|
2014-11-14 15:05:05 +00:00
|
|
|
ZoneList<ObjectLiteral::Property*>* properties = NewPropertyList(4, zone());
|
2015-01-22 06:17:11 +00:00
|
|
|
FunctionLiteral* constructor = NULL;
|
2014-11-14 15:05:05 +00:00
|
|
|
bool has_seen_constructor = false;
|
|
|
|
|
|
|
|
Expect(Token::LBRACE, CHECK_OK);
|
2015-02-26 13:48:10 +00:00
|
|
|
|
2015-02-03 17:42:41 +00:00
|
|
|
const bool has_extends = extends != nullptr;
|
2014-11-14 15:05:05 +00:00
|
|
|
while (peek() != Token::RBRACE) {
|
|
|
|
if (Check(Token::SEMICOLON)) continue;
|
|
|
|
if (fni_ != NULL) fni_->Enter();
|
|
|
|
const bool in_class = true;
|
|
|
|
const bool is_static = false;
|
2015-01-15 20:02:20 +00:00
|
|
|
bool is_computed_name = false; // Classes do not care about computed
|
|
|
|
// property names here.
|
2015-04-22 12:35:05 +00:00
|
|
|
ExpressionClassifier classifier;
|
2015-01-29 23:12:25 +00:00
|
|
|
ObjectLiteral::Property* property = ParsePropertyDefinition(
|
2015-02-03 17:42:41 +00:00
|
|
|
&checker, in_class, has_extends, is_static, &is_computed_name,
|
2015-04-22 12:35:05 +00:00
|
|
|
&has_seen_constructor, &classifier, CHECK_OK);
|
2015-04-27 14:35:45 +00:00
|
|
|
ValidateExpression(&classifier, CHECK_OK);
|
2014-11-14 15:05:05 +00:00
|
|
|
|
|
|
|
if (has_seen_constructor && constructor == NULL) {
|
2015-01-22 06:17:11 +00:00
|
|
|
constructor = GetPropertyValue(property)->AsFunctionLiteral();
|
|
|
|
DCHECK_NOT_NULL(constructor);
|
2014-11-14 15:05:05 +00:00
|
|
|
} else {
|
|
|
|
properties->Add(property, zone());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fni_ != NULL) {
|
|
|
|
fni_->Infer();
|
|
|
|
fni_->Leave();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Expect(Token::RBRACE, CHECK_OK);
|
|
|
|
int end_pos = scanner()->location().end_pos;
|
|
|
|
|
|
|
|
if (constructor == NULL) {
|
|
|
|
constructor =
|
|
|
|
DefaultConstructor(extends != NULL, block_scope, pos, end_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
block_scope->set_end_position(end_pos);
|
|
|
|
|
|
|
|
if (name != NULL) {
|
|
|
|
DCHECK_NOT_NULL(proxy);
|
2015-02-26 18:36:59 +00:00
|
|
|
proxy->var()->set_initializer_position(end_pos);
|
2015-03-09 14:30:28 +00:00
|
|
|
} else {
|
|
|
|
// Unnamed classes should not have scopes (the scope will be empty).
|
|
|
|
DCHECK_EQ(block_scope->num_var_or_const(), 0);
|
|
|
|
block_scope = nullptr;
|
2014-11-14 15:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return factory()->NewClassLiteral(name, block_scope, proxy, extends,
|
|
|
|
constructor, properties, pos, end_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Expression* Parser::ParseV8Intrinsic(bool* ok) {
|
|
|
|
// CallRuntime ::
|
|
|
|
// '%' Identifier Arguments
|
|
|
|
|
2013-10-14 09:24:58 +00:00
|
|
|
int pos = peek_position();
|
2008-07-03 15:10:15 +00:00
|
|
|
Expect(Token::MOD, CHECK_OK);
|
2014-02-05 16:26:48 +00:00
|
|
|
// Allow "eval" or "arguments" for backward compatibility.
|
2015-04-10 12:04:51 +00:00
|
|
|
const AstRawString* name = ParseIdentifier(kAllowRestrictedIdentifiers,
|
|
|
|
CHECK_OK);
|
2015-04-09 19:37:14 +00:00
|
|
|
Scanner::Location spread_pos;
|
2015-04-22 12:35:05 +00:00
|
|
|
ExpressionClassifier classifier;
|
|
|
|
ZoneList<Expression*>* args =
|
|
|
|
ParseArguments(&spread_pos, &classifier, CHECK_OK);
|
2015-04-27 14:35:45 +00:00
|
|
|
ValidateExpression(&classifier, CHECK_OK);
|
2015-04-09 19:37:14 +00:00
|
|
|
|
|
|
|
DCHECK(!spread_pos.IsValid());
|
2010-09-14 14:52:53 +00:00
|
|
|
|
|
|
|
if (extension_ != NULL) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// The extension structures are only accessible while parsing the
|
|
|
|
// very first time not when reparsing because of lazy compilation.
|
2014-02-12 12:02:07 +00:00
|
|
|
scope_->DeclarationScope()->ForceEagerCompilation();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
const Runtime::Function* function = Runtime::FunctionForName(name->string());
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2010-09-14 14:52:53 +00:00
|
|
|
// Check for built-in IS_VAR macro.
|
|
|
|
if (function != NULL &&
|
|
|
|
function->intrinsic_type == Runtime::RUNTIME &&
|
|
|
|
function->function_id == Runtime::kIS_VAR) {
|
|
|
|
// %IS_VAR(x) evaluates to x if x is a variable,
|
|
|
|
// leads to a parse error otherwise. Could be implemented as an
|
|
|
|
// inline function %_IS_VAR(x) to eliminate this special case.
|
|
|
|
if (args->length() == 1 && args->at(0)->AsVariableProxy() != NULL) {
|
|
|
|
return args->at(0);
|
|
|
|
} else {
|
2015-05-18 08:34:05 +00:00
|
|
|
ReportMessage(MessageTemplate::kNotIsvar);
|
2010-09-14 14:52:53 +00:00
|
|
|
*ok = false;
|
2008-07-03 15:10:15 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-14 14:52:53 +00:00
|
|
|
// Check that the expected number of arguments are being passed.
|
|
|
|
if (function != NULL &&
|
|
|
|
function->nargs != -1 &&
|
|
|
|
function->nargs != args->length()) {
|
2015-05-18 08:34:05 +00:00
|
|
|
ReportMessage(MessageTemplate::kIllegalAccess);
|
2010-09-14 14:52:53 +00:00
|
|
|
*ok = false;
|
|
|
|
return NULL;
|
2012-08-14 10:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the function is defined if it's an inline runtime call.
|
2014-06-24 14:03:24 +00:00
|
|
|
if (function == NULL && name->FirstCharacter() == '_') {
|
2015-05-18 08:34:05 +00:00
|
|
|
ParserTraits::ReportMessage(MessageTemplate::kNotDefined, name);
|
2012-08-14 10:06:34 +00:00
|
|
|
*ok = false;
|
|
|
|
return NULL;
|
2010-03-11 09:27:12 +00:00
|
|
|
}
|
|
|
|
|
2010-09-14 14:52:53 +00:00
|
|
|
// We have a valid intrinsics call or a call to a builtin.
|
2013-10-14 09:24:58 +00:00
|
|
|
return factory()->NewCallRuntime(name, function, args, pos);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-14 09:24:58 +00:00
|
|
|
Literal* Parser::GetLiteralUndefined(int position) {
|
2014-06-24 14:03:24 +00:00
|
|
|
return factory()->NewUndefinedLiteral(position);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-01 12:31:18 +00:00
|
|
|
void Parser::CheckConflictingVarDeclarations(Scope* scope, bool* ok) {
|
|
|
|
Declaration* decl = scope->CheckConflictingVarDeclarations();
|
|
|
|
if (decl != NULL) {
|
|
|
|
// In harmony mode we treat conflicting variable bindinds as early
|
|
|
|
// errors. See ES5 16 for a definition of early errors.
|
2014-06-24 14:03:24 +00:00
|
|
|
const AstRawString* name = decl->proxy()->raw_name();
|
2011-09-01 12:31:18 +00:00
|
|
|
int position = decl->proxy()->position();
|
|
|
|
Scanner::Location location = position == RelocInfo::kNoPosition
|
|
|
|
? Scanner::Location::invalid()
|
|
|
|
: Scanner::Location(position, position + 1);
|
2015-05-18 08:34:05 +00:00
|
|
|
ParserTraits::ReportMessageAt(location, MessageTemplate::kVarRedeclaration,
|
|
|
|
name);
|
2011-09-01 12:31:18 +00:00
|
|
|
*ok = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Parser support
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
bool Parser::TargetStackContainsLabel(const AstRawString* label) {
|
2009-05-15 14:58:02 +00:00
|
|
|
for (Target* t = target_stack_; t != NULL; t = t->previous()) {
|
2015-01-15 19:18:05 +00:00
|
|
|
if (ContainsLabel(t->statement()->labels(), label)) return true;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
BreakableStatement* Parser::LookupBreakTarget(const AstRawString* label,
|
|
|
|
bool* ok) {
|
|
|
|
bool anonymous = label == NULL;
|
2009-05-15 14:58:02 +00:00
|
|
|
for (Target* t = target_stack_; t != NULL; t = t->previous()) {
|
2015-01-15 19:18:05 +00:00
|
|
|
BreakableStatement* stat = t->statement();
|
2008-07-03 15:10:15 +00:00
|
|
|
if ((anonymous && stat->is_target_for_anonymous()) ||
|
|
|
|
(!anonymous && ContainsLabel(stat->labels(), label))) {
|
|
|
|
return stat;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
IterationStatement* Parser::LookupContinueTarget(const AstRawString* label,
|
2008-07-03 15:10:15 +00:00
|
|
|
bool* ok) {
|
2014-06-24 14:03:24 +00:00
|
|
|
bool anonymous = label == NULL;
|
2009-05-15 14:58:02 +00:00
|
|
|
for (Target* t = target_stack_; t != NULL; t = t->previous()) {
|
2015-01-15 19:18:05 +00:00
|
|
|
IterationStatement* stat = t->statement()->AsIterationStatement();
|
2008-07-03 15:10:15 +00:00
|
|
|
if (stat == NULL) continue;
|
|
|
|
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(stat->is_target_for_anonymous());
|
2008-07-03 15:10:15 +00:00
|
|
|
if (anonymous || ContainsLabel(stat->labels(), label)) {
|
|
|
|
return stat;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-20 09:39:32 +00:00
|
|
|
void Parser::HandleSourceURLComments(Isolate* isolate, Handle<Script> script) {
|
2014-07-02 07:01:31 +00:00
|
|
|
if (scanner_.source_url()->length() > 0) {
|
2015-02-20 09:39:32 +00:00
|
|
|
Handle<String> source_url = scanner_.source_url()->Internalize(isolate);
|
|
|
|
script->set_source_url(*source_url);
|
2014-07-02 07:01:31 +00:00
|
|
|
}
|
|
|
|
if (scanner_.source_mapping_url()->length() > 0) {
|
2014-07-02 12:01:56 +00:00
|
|
|
Handle<String> source_mapping_url =
|
2015-02-20 09:39:32 +00:00
|
|
|
scanner_.source_mapping_url()->Internalize(isolate);
|
|
|
|
script->set_source_mapping_url(*source_mapping_url);
|
2014-07-02 07:01:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-20 09:39:32 +00:00
|
|
|
void Parser::Internalize(Isolate* isolate, Handle<Script> script, bool error) {
|
2014-09-02 11:36:21 +00:00
|
|
|
// Internalize strings.
|
2015-02-20 09:39:32 +00:00
|
|
|
ast_value_factory()->Internalize(isolate);
|
2014-09-02 11:36:21 +00:00
|
|
|
|
|
|
|
// Error processing.
|
2015-02-20 09:39:32 +00:00
|
|
|
if (error) {
|
2014-09-02 11:36:21 +00:00
|
|
|
if (stack_overflow()) {
|
2015-02-20 09:39:32 +00:00
|
|
|
isolate->StackOverflow();
|
2014-09-02 11:36:21 +00:00
|
|
|
} else {
|
2015-02-25 14:17:39 +00:00
|
|
|
DCHECK(pending_error_handler_.has_pending_error());
|
|
|
|
pending_error_handler_.ThrowPendingError(isolate, script);
|
2014-09-02 11:36:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move statistics to Isolate.
|
2014-06-30 13:35:16 +00:00
|
|
|
for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount;
|
|
|
|
++feature) {
|
|
|
|
for (int i = 0; i < use_counts_[feature]; ++i) {
|
2015-02-20 09:39:32 +00:00
|
|
|
isolate->CountUsage(v8::Isolate::UseCounterFeature(feature));
|
2014-06-30 13:35:16 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-20 09:39:32 +00:00
|
|
|
isolate->counters()->total_preparse_skipped()->Increment(
|
2014-09-02 11:36:21 +00:00
|
|
|
total_preparse_skipped_);
|
2014-06-30 13:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Regular expressions
|
|
|
|
|
|
|
|
|
2015-01-12 09:50:15 +00:00
|
|
|
RegExpParser::RegExpParser(FlatStringReader* in, Handle<String>* error,
|
2015-01-23 15:19:34 +00:00
|
|
|
bool multiline, bool unicode, Isolate* isolate,
|
|
|
|
Zone* zone)
|
|
|
|
: isolate_(isolate),
|
2012-06-20 08:58:41 +00:00
|
|
|
zone_(zone),
|
2011-03-18 20:35:07 +00:00
|
|
|
error_(error),
|
|
|
|
captures_(NULL),
|
|
|
|
in_(in),
|
|
|
|
current_(kEndMarker),
|
|
|
|
next_pos_(0),
|
|
|
|
capture_count_(0),
|
|
|
|
has_more_(true),
|
|
|
|
multiline_(multiline),
|
2015-01-12 09:50:15 +00:00
|
|
|
unicode_(unicode),
|
2011-03-18 20:35:07 +00:00
|
|
|
simple_(false),
|
|
|
|
contains_anchor_(false),
|
|
|
|
is_scanned_for_captures_(false),
|
|
|
|
failed_(false) {
|
2010-12-20 10:44:41 +00:00
|
|
|
Advance();
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uc32 RegExpParser::Next() {
|
|
|
|
if (has_next()) {
|
|
|
|
return in()->Get(next_pos_);
|
|
|
|
} else {
|
|
|
|
return kEndMarker;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RegExpParser::Advance() {
|
|
|
|
if (next_pos_ < in()->length()) {
|
2011-03-18 20:35:07 +00:00
|
|
|
StackLimitCheck check(isolate());
|
2008-12-01 15:32:20 +00:00
|
|
|
if (check.HasOverflowed()) {
|
2011-03-18 20:35:07 +00:00
|
|
|
ReportError(CStrVector(Isolate::kStackOverflowMessage));
|
2012-06-20 08:58:41 +00:00
|
|
|
} else if (zone()->excess_allocation()) {
|
2008-12-01 15:32:20 +00:00
|
|
|
ReportError(CStrVector("Regular expression too large"));
|
|
|
|
} else {
|
|
|
|
current_ = in()->Get(next_pos_);
|
|
|
|
next_pos_++;
|
|
|
|
}
|
2008-11-25 11:07:48 +00:00
|
|
|
} else {
|
|
|
|
current_ = kEndMarker;
|
2014-12-16 12:14:09 +00:00
|
|
|
// Advance so that position() points to 1-after-the-last-character. This is
|
|
|
|
// important so that Reset() to this position works correctly.
|
|
|
|
next_pos_ = in()->length() + 1;
|
2008-11-25 11:07:48 +00:00
|
|
|
has_more_ = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RegExpParser::Reset(int pos) {
|
|
|
|
next_pos_ = pos;
|
2013-05-27 10:53:37 +00:00
|
|
|
has_more_ = (pos < in()->length());
|
2008-11-25 11:07:48 +00:00
|
|
|
Advance();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RegExpParser::Advance(int dist) {
|
2010-12-20 10:44:41 +00:00
|
|
|
next_pos_ += dist - 1;
|
|
|
|
Advance();
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-12 10:22:56 +00:00
|
|
|
bool RegExpParser::simple() {
|
|
|
|
return simple_;
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2015-01-12 09:50:15 +00:00
|
|
|
bool RegExpParser::IsSyntaxCharacter(uc32 c) {
|
|
|
|
return c == '^' || c == '$' || c == '\\' || c == '.' || c == '*' ||
|
|
|
|
c == '+' || c == '?' || c == '(' || c == ')' || c == '[' || c == ']' ||
|
|
|
|
c == '{' || c == '}' || c == '|';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-01 15:32:20 +00:00
|
|
|
RegExpTree* RegExpParser::ReportError(Vector<const char> message) {
|
|
|
|
failed_ = true;
|
2014-04-23 15:43:39 +00:00
|
|
|
*error_ = isolate()->factory()->NewStringFromAscii(message).ToHandleChecked();
|
2008-12-01 15:32:20 +00:00
|
|
|
// Zip to the end to make sure the no more input is read.
|
|
|
|
current_ = kEndMarker;
|
|
|
|
next_pos_ = in()->length();
|
2008-11-25 11:07:48 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Pattern ::
|
|
|
|
// Disjunction
|
2008-12-01 15:32:20 +00:00
|
|
|
RegExpTree* RegExpParser::ParsePattern() {
|
|
|
|
RegExpTree* result = ParseDisjunction(CHECK_FAILED);
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(!has_more());
|
2009-03-04 09:52:01 +00:00
|
|
|
// If the result of parsing is a literal string atom, and it has the
|
|
|
|
// same length as the input, then the atom is identical to the input.
|
|
|
|
if (result->IsAtom() && result->AsAtom()->length() == in()->length()) {
|
|
|
|
simple_ = true;
|
|
|
|
}
|
2008-11-25 11:07:48 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Disjunction ::
|
|
|
|
// Alternative
|
|
|
|
// Alternative | Disjunction
|
|
|
|
// Alternative ::
|
|
|
|
// [empty]
|
|
|
|
// Term Alternative
|
|
|
|
// Term ::
|
|
|
|
// Assertion
|
|
|
|
// Atom
|
|
|
|
// Atom Quantifier
|
2008-12-01 15:32:20 +00:00
|
|
|
RegExpTree* RegExpParser::ParseDisjunction() {
|
2009-07-03 08:18:35 +00:00
|
|
|
// Used to store current state while parsing subexpressions.
|
2012-06-04 14:42:58 +00:00
|
|
|
RegExpParserState initial_state(NULL, INITIAL, 0, zone());
|
2009-07-03 08:18:35 +00:00
|
|
|
RegExpParserState* stored_state = &initial_state;
|
|
|
|
// Cache the builder in a local variable for quick access.
|
|
|
|
RegExpBuilder* builder = initial_state.builder();
|
2008-11-25 11:07:48 +00:00
|
|
|
while (true) {
|
|
|
|
switch (current()) {
|
|
|
|
case kEndMarker:
|
2009-07-03 08:18:35 +00:00
|
|
|
if (stored_state->IsSubexpression()) {
|
|
|
|
// Inside a parenthesized group when hitting end of input.
|
|
|
|
ReportError(CStrVector("Unterminated group") CHECK_FAILED);
|
|
|
|
}
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK_EQ(INITIAL, stored_state->group_type());
|
2009-07-03 08:18:35 +00:00
|
|
|
// Parsing completed successfully.
|
|
|
|
return builder->ToRegExp();
|
|
|
|
case ')': {
|
|
|
|
if (!stored_state->IsSubexpression()) {
|
2009-07-03 11:09:34 +00:00
|
|
|
ReportError(CStrVector("Unmatched ')'") CHECK_FAILED);
|
2009-07-03 08:18:35 +00:00
|
|
|
}
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK_NE(INITIAL, stored_state->group_type());
|
2009-07-03 08:18:35 +00:00
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
Advance();
|
2009-07-03 08:18:35 +00:00
|
|
|
// End disjunction parsing and convert builder content to new single
|
|
|
|
// regexp atom.
|
|
|
|
RegExpTree* body = builder->ToRegExp();
|
|
|
|
|
|
|
|
int end_capture_index = captures_started();
|
|
|
|
|
|
|
|
int capture_index = stored_state->capture_index();
|
2013-06-06 13:28:22 +00:00
|
|
|
SubexpressionType group_type = stored_state->group_type();
|
2009-07-03 08:18:35 +00:00
|
|
|
|
|
|
|
// Restore previous state.
|
|
|
|
stored_state = stored_state->previous_state();
|
|
|
|
builder = stored_state->builder();
|
|
|
|
|
|
|
|
// Build result of subexpression.
|
2013-06-06 13:28:22 +00:00
|
|
|
if (group_type == CAPTURE) {
|
2011-04-04 06:29:02 +00:00
|
|
|
RegExpCapture* capture = new(zone()) RegExpCapture(body, capture_index);
|
2009-07-03 08:18:35 +00:00
|
|
|
captures_->at(capture_index - 1) = capture;
|
|
|
|
body = capture;
|
2013-06-06 13:28:22 +00:00
|
|
|
} else if (group_type != GROUPING) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(group_type == POSITIVE_LOOKAHEAD ||
|
2013-06-06 13:28:22 +00:00
|
|
|
group_type == NEGATIVE_LOOKAHEAD);
|
|
|
|
bool is_positive = (group_type == POSITIVE_LOOKAHEAD);
|
2011-04-04 06:29:02 +00:00
|
|
|
body = new(zone()) RegExpLookahead(body,
|
2009-07-03 08:18:35 +00:00
|
|
|
is_positive,
|
|
|
|
end_capture_index - capture_index,
|
|
|
|
capture_index);
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
2009-07-03 08:18:35 +00:00
|
|
|
builder->AddAtom(body);
|
2011-02-16 08:10:47 +00:00
|
|
|
// For compatability with JSC and ES3, we allow quantifiers after
|
|
|
|
// lookaheads, and break in all cases.
|
2009-07-03 08:18:35 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '|': {
|
|
|
|
Advance();
|
|
|
|
builder->NewAlternative();
|
2008-11-25 11:07:48 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case '*':
|
|
|
|
case '+':
|
|
|
|
case '?':
|
2009-07-22 12:33:16 +00:00
|
|
|
return ReportError(CStrVector("Nothing to repeat"));
|
2008-11-25 11:07:48 +00:00
|
|
|
case '^': {
|
|
|
|
Advance();
|
2009-02-03 11:43:55 +00:00
|
|
|
if (multiline_) {
|
2009-07-03 08:18:35 +00:00
|
|
|
builder->AddAssertion(
|
2011-04-04 06:29:02 +00:00
|
|
|
new(zone()) RegExpAssertion(RegExpAssertion::START_OF_LINE));
|
2009-02-03 11:43:55 +00:00
|
|
|
} else {
|
2009-07-03 08:18:35 +00:00
|
|
|
builder->AddAssertion(
|
2011-04-04 06:29:02 +00:00
|
|
|
new(zone()) RegExpAssertion(RegExpAssertion::START_OF_INPUT));
|
2009-02-03 11:43:55 +00:00
|
|
|
set_contains_anchor();
|
|
|
|
}
|
2008-11-25 11:07:48 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case '$': {
|
|
|
|
Advance();
|
2013-06-06 13:28:22 +00:00
|
|
|
RegExpAssertion::AssertionType assertion_type =
|
2008-11-27 10:35:06 +00:00
|
|
|
multiline_ ? RegExpAssertion::END_OF_LINE :
|
|
|
|
RegExpAssertion::END_OF_INPUT;
|
2013-06-06 13:28:22 +00:00
|
|
|
builder->AddAssertion(new(zone()) RegExpAssertion(assertion_type));
|
2008-11-25 11:07:48 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case '.': {
|
|
|
|
Advance();
|
|
|
|
// everything except \x0a, \x0d, \u2028 and \u2029
|
2011-05-23 13:00:11 +00:00
|
|
|
ZoneList<CharacterRange>* ranges =
|
2012-06-11 12:42:31 +00:00
|
|
|
new(zone()) ZoneList<CharacterRange>(2, zone());
|
|
|
|
CharacterRange::AddClassEscape('.', ranges, zone());
|
2011-04-04 06:29:02 +00:00
|
|
|
RegExpTree* atom = new(zone()) RegExpCharacterClass(ranges, false);
|
2009-07-03 08:18:35 +00:00
|
|
|
builder->AddAtom(atom);
|
2008-11-25 11:07:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '(': {
|
2013-06-06 13:28:22 +00:00
|
|
|
SubexpressionType subexpr_type = CAPTURE;
|
2009-07-03 08:18:35 +00:00
|
|
|
Advance();
|
|
|
|
if (current() == '?') {
|
|
|
|
switch (Next()) {
|
|
|
|
case ':':
|
2013-06-06 13:28:22 +00:00
|
|
|
subexpr_type = GROUPING;
|
2009-07-03 08:18:35 +00:00
|
|
|
break;
|
|
|
|
case '=':
|
2013-06-06 13:28:22 +00:00
|
|
|
subexpr_type = POSITIVE_LOOKAHEAD;
|
2009-07-03 08:18:35 +00:00
|
|
|
break;
|
|
|
|
case '!':
|
2013-06-06 13:28:22 +00:00
|
|
|
subexpr_type = NEGATIVE_LOOKAHEAD;
|
2009-07-03 08:18:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ReportError(CStrVector("Invalid group") CHECK_FAILED);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Advance(2);
|
|
|
|
} else {
|
|
|
|
if (captures_ == NULL) {
|
2012-06-11 12:42:31 +00:00
|
|
|
captures_ = new(zone()) ZoneList<RegExpCapture*>(2, zone());
|
2009-07-03 08:18:35 +00:00
|
|
|
}
|
|
|
|
if (captures_started() >= kMaxCaptures) {
|
|
|
|
ReportError(CStrVector("Too many captures") CHECK_FAILED);
|
|
|
|
}
|
2012-06-11 12:42:31 +00:00
|
|
|
captures_->Add(NULL, zone());
|
2009-07-03 08:18:35 +00:00
|
|
|
}
|
|
|
|
// Store current state and begin new disjunction parsing.
|
2013-06-06 13:28:22 +00:00
|
|
|
stored_state = new(zone()) RegExpParserState(stored_state, subexpr_type,
|
2012-06-04 14:42:58 +00:00
|
|
|
captures_started(), zone());
|
2009-07-03 08:18:35 +00:00
|
|
|
builder = stored_state->builder();
|
2011-02-16 08:10:47 +00:00
|
|
|
continue;
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
case '[': {
|
2008-12-01 15:32:20 +00:00
|
|
|
RegExpTree* atom = ParseCharacterClass(CHECK_FAILED);
|
2009-07-03 08:18:35 +00:00
|
|
|
builder->AddAtom(atom);
|
2008-11-25 11:07:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Atom ::
|
|
|
|
// \ AtomEscape
|
|
|
|
case '\\':
|
|
|
|
switch (Next()) {
|
|
|
|
case kEndMarker:
|
2009-07-22 12:33:16 +00:00
|
|
|
return ReportError(CStrVector("\\ at end of pattern"));
|
2008-11-25 11:07:48 +00:00
|
|
|
case 'b':
|
|
|
|
Advance(2);
|
2009-07-03 08:18:35 +00:00
|
|
|
builder->AddAssertion(
|
2011-04-04 06:29:02 +00:00
|
|
|
new(zone()) RegExpAssertion(RegExpAssertion::BOUNDARY));
|
2008-11-25 11:07:48 +00:00
|
|
|
continue;
|
|
|
|
case 'B':
|
|
|
|
Advance(2);
|
2009-07-03 08:18:35 +00:00
|
|
|
builder->AddAssertion(
|
2011-04-04 06:29:02 +00:00
|
|
|
new(zone()) RegExpAssertion(RegExpAssertion::NON_BOUNDARY));
|
2008-11-25 11:07:48 +00:00
|
|
|
continue;
|
2011-02-16 08:10:47 +00:00
|
|
|
// AtomEscape ::
|
|
|
|
// CharacterClassEscape
|
|
|
|
//
|
|
|
|
// CharacterClassEscape :: one of
|
|
|
|
// d D s S w W
|
2008-11-25 11:07:48 +00:00
|
|
|
case 'd': case 'D': case 's': case 'S': case 'w': case 'W': {
|
|
|
|
uc32 c = Next();
|
|
|
|
Advance(2);
|
2011-05-23 13:00:11 +00:00
|
|
|
ZoneList<CharacterRange>* ranges =
|
2012-06-11 12:42:31 +00:00
|
|
|
new(zone()) ZoneList<CharacterRange>(2, zone());
|
|
|
|
CharacterRange::AddClassEscape(c, ranges, zone());
|
2011-04-04 06:29:02 +00:00
|
|
|
RegExpTree* atom = new(zone()) RegExpCharacterClass(ranges, false);
|
2009-07-03 08:18:35 +00:00
|
|
|
builder->AddAtom(atom);
|
2009-07-03 11:09:34 +00:00
|
|
|
break;
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
case '1': case '2': case '3': case '4': case '5': case '6':
|
|
|
|
case '7': case '8': case '9': {
|
|
|
|
int index = 0;
|
|
|
|
if (ParseBackReferenceIndex(&index)) {
|
2009-07-03 08:18:35 +00:00
|
|
|
RegExpCapture* capture = NULL;
|
|
|
|
if (captures_ != NULL && index <= captures_->length()) {
|
|
|
|
capture = captures_->at(index - 1);
|
|
|
|
}
|
|
|
|
if (capture == NULL) {
|
|
|
|
builder->AddEmpty();
|
2009-07-03 11:09:34 +00:00
|
|
|
break;
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
2011-04-04 06:29:02 +00:00
|
|
|
RegExpTree* atom = new(zone()) RegExpBackReference(capture);
|
2009-07-03 08:18:35 +00:00
|
|
|
builder->AddAtom(atom);
|
2009-07-03 11:09:34 +00:00
|
|
|
break;
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
uc32 first_digit = Next();
|
|
|
|
if (first_digit == '8' || first_digit == '9') {
|
2015-01-12 09:50:15 +00:00
|
|
|
// If the 'u' flag is present, only syntax characters can be escaped,
|
|
|
|
// no other identity escapes are allowed. If the 'u' flag is not
|
|
|
|
// present, all identity escapes are allowed.
|
2015-02-05 14:16:54 +00:00
|
|
|
if (!FLAG_harmony_unicode_regexps || !unicode_) {
|
2015-01-12 09:50:15 +00:00
|
|
|
builder->AddCharacter(first_digit);
|
|
|
|
Advance(2);
|
|
|
|
} else {
|
|
|
|
return ReportError(CStrVector("Invalid escape"));
|
|
|
|
}
|
2008-11-25 11:07:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// FALLTHROUGH
|
|
|
|
case '0': {
|
|
|
|
Advance();
|
|
|
|
uc32 octal = ParseOctalLiteral();
|
2009-07-03 08:18:35 +00:00
|
|
|
builder->AddCharacter(octal);
|
2008-11-25 11:07:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// ControlEscape :: one of
|
|
|
|
// f n r t v
|
|
|
|
case 'f':
|
|
|
|
Advance(2);
|
2009-07-03 08:18:35 +00:00
|
|
|
builder->AddCharacter('\f');
|
2008-11-25 11:07:48 +00:00
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
Advance(2);
|
2009-07-03 08:18:35 +00:00
|
|
|
builder->AddCharacter('\n');
|
2008-11-25 11:07:48 +00:00
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
Advance(2);
|
2009-07-03 08:18:35 +00:00
|
|
|
builder->AddCharacter('\r');
|
2008-11-25 11:07:48 +00:00
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
Advance(2);
|
2009-07-03 08:18:35 +00:00
|
|
|
builder->AddCharacter('\t');
|
2008-11-25 11:07:48 +00:00
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
Advance(2);
|
2009-07-03 08:18:35 +00:00
|
|
|
builder->AddCharacter('\v');
|
2008-11-25 11:07:48 +00:00
|
|
|
break;
|
|
|
|
case 'c': {
|
2011-01-07 12:35:42 +00:00
|
|
|
Advance();
|
|
|
|
uc32 controlLetter = Next();
|
|
|
|
// Special case if it is an ASCII letter.
|
|
|
|
// Convert lower case letters to uppercase.
|
|
|
|
uc32 letter = controlLetter & ~('a' ^ 'A');
|
|
|
|
if (letter < 'A' || 'Z' < letter) {
|
|
|
|
// controlLetter is not in range 'A'-'Z' or 'a'-'z'.
|
|
|
|
// This is outside the specification. We match JSC in
|
|
|
|
// reading the backslash as a literal character instead
|
|
|
|
// of as starting an escape.
|
|
|
|
builder->AddCharacter('\\');
|
|
|
|
} else {
|
|
|
|
Advance(2);
|
|
|
|
builder->AddCharacter(controlLetter & 0x1f);
|
|
|
|
}
|
2008-11-25 11:07:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'x': {
|
|
|
|
Advance(2);
|
|
|
|
uc32 value;
|
|
|
|
if (ParseHexEscape(2, &value)) {
|
2009-07-03 08:18:35 +00:00
|
|
|
builder->AddCharacter(value);
|
2015-02-05 14:16:54 +00:00
|
|
|
} else if (!FLAG_harmony_unicode_regexps || !unicode_) {
|
2009-07-03 08:18:35 +00:00
|
|
|
builder->AddCharacter('x');
|
2015-01-12 09:50:15 +00:00
|
|
|
} else {
|
|
|
|
// If the 'u' flag is present, invalid escapes are not treated as
|
|
|
|
// identity escapes.
|
|
|
|
return ReportError(CStrVector("Invalid escape"));
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'u': {
|
|
|
|
Advance(2);
|
|
|
|
uc32 value;
|
2015-01-12 09:50:15 +00:00
|
|
|
if (ParseUnicodeEscape(&value)) {
|
2009-07-03 08:18:35 +00:00
|
|
|
builder->AddCharacter(value);
|
2015-02-05 14:16:54 +00:00
|
|
|
} else if (!FLAG_harmony_unicode_regexps || !unicode_) {
|
2009-07-03 08:18:35 +00:00
|
|
|
builder->AddCharacter('u');
|
2015-01-12 09:50:15 +00:00
|
|
|
} else {
|
|
|
|
// If the 'u' flag is present, invalid escapes are not treated as
|
|
|
|
// identity escapes.
|
|
|
|
return ReportError(CStrVector("Invalid unicode escape"));
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2015-01-12 09:50:15 +00:00
|
|
|
Advance();
|
|
|
|
// If the 'u' flag is present, only syntax characters can be escaped, no
|
|
|
|
// other identity escapes are allowed. If the 'u' flag is not present,
|
|
|
|
// all identity escapes are allowed.
|
2015-02-05 14:16:54 +00:00
|
|
|
if (!FLAG_harmony_unicode_regexps || !unicode_ ||
|
2015-01-12 09:50:15 +00:00
|
|
|
IsSyntaxCharacter(current())) {
|
|
|
|
builder->AddCharacter(current());
|
|
|
|
Advance();
|
|
|
|
} else {
|
|
|
|
return ReportError(CStrVector("Invalid escape"));
|
|
|
|
}
|
2008-11-25 11:07:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '{': {
|
|
|
|
int dummy;
|
|
|
|
if (ParseIntervalQuantifier(&dummy, &dummy)) {
|
2008-12-01 15:32:20 +00:00
|
|
|
ReportError(CStrVector("Nothing to repeat") CHECK_FAILED);
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
// fallthrough
|
|
|
|
}
|
|
|
|
default:
|
2009-07-03 08:18:35 +00:00
|
|
|
builder->AddCharacter(current());
|
2008-11-25 11:07:48 +00:00
|
|
|
Advance();
|
|
|
|
break;
|
|
|
|
} // end switch(current())
|
|
|
|
|
|
|
|
int min;
|
|
|
|
int max;
|
|
|
|
switch (current()) {
|
|
|
|
// QuantifierPrefix ::
|
|
|
|
// *
|
|
|
|
// +
|
|
|
|
// ?
|
|
|
|
// {
|
|
|
|
case '*':
|
|
|
|
min = 0;
|
2008-12-17 10:59:14 +00:00
|
|
|
max = RegExpTree::kInfinity;
|
2008-11-25 11:07:48 +00:00
|
|
|
Advance();
|
|
|
|
break;
|
|
|
|
case '+':
|
|
|
|
min = 1;
|
2008-12-17 10:59:14 +00:00
|
|
|
max = RegExpTree::kInfinity;
|
2008-11-25 11:07:48 +00:00
|
|
|
Advance();
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
min = 0;
|
|
|
|
max = 1;
|
|
|
|
Advance();
|
|
|
|
break;
|
|
|
|
case '{':
|
|
|
|
if (ParseIntervalQuantifier(&min, &max)) {
|
2009-01-29 14:14:13 +00:00
|
|
|
if (max < min) {
|
|
|
|
ReportError(CStrVector("numbers out of order in {} quantifier.")
|
|
|
|
CHECK_FAILED);
|
|
|
|
}
|
2008-11-25 11:07:48 +00:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
2013-06-06 13:28:22 +00:00
|
|
|
RegExpQuantifier::QuantifierType quantifier_type = RegExpQuantifier::GREEDY;
|
2008-11-25 11:07:48 +00:00
|
|
|
if (current() == '?') {
|
2013-06-06 13:28:22 +00:00
|
|
|
quantifier_type = RegExpQuantifier::NON_GREEDY;
|
2010-01-07 19:01:23 +00:00
|
|
|
Advance();
|
|
|
|
} else if (FLAG_regexp_possessive_quantifier && current() == '+') {
|
|
|
|
// FLAG_regexp_possessive_quantifier is a debug-only flag.
|
2013-06-06 13:28:22 +00:00
|
|
|
quantifier_type = RegExpQuantifier::POSSESSIVE;
|
2008-11-25 11:07:48 +00:00
|
|
|
Advance();
|
|
|
|
}
|
2013-06-06 13:28:22 +00:00
|
|
|
builder->AddQuantifierToAtom(min, max, quantifier_type);
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-08-04 11:34:54 +00:00
|
|
|
// Currently only used in an DCHECK.
|
2008-11-25 11:07:48 +00:00
|
|
|
static bool IsSpecialClassEscape(uc32 c) {
|
|
|
|
switch (c) {
|
|
|
|
case 'd': case 'D':
|
|
|
|
case 's': case 'S':
|
|
|
|
case 'w': case 'W':
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// In order to know whether an escape is a backreference or not we have to scan
|
|
|
|
// the entire regexp and find the number of capturing parentheses. However we
|
|
|
|
// don't want to scan the regexp twice unless it is necessary. This mini-parser
|
|
|
|
// is called when needed. It can see the difference between capturing and
|
|
|
|
// noncapturing parentheses and can skip character classes and backslash-escaped
|
|
|
|
// characters.
|
|
|
|
void RegExpParser::ScanForCaptures() {
|
2008-11-25 14:14:04 +00:00
|
|
|
// Start with captures started previous to current position
|
|
|
|
int capture_count = captures_started();
|
|
|
|
// Add count of captures after this position.
|
2008-11-25 11:07:48 +00:00
|
|
|
int n;
|
|
|
|
while ((n = current()) != kEndMarker) {
|
|
|
|
Advance();
|
|
|
|
switch (n) {
|
|
|
|
case '\\':
|
|
|
|
Advance();
|
|
|
|
break;
|
|
|
|
case '[': {
|
|
|
|
int c;
|
|
|
|
while ((c = current()) != kEndMarker) {
|
|
|
|
Advance();
|
|
|
|
if (c == '\\') {
|
|
|
|
Advance();
|
|
|
|
} else {
|
|
|
|
if (c == ']') break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '(':
|
2008-11-25 14:14:04 +00:00
|
|
|
if (current() != '?') capture_count++;
|
2008-11-25 11:07:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-11-25 14:14:04 +00:00
|
|
|
capture_count_ = capture_count;
|
2008-11-25 11:07:48 +00:00
|
|
|
is_scanned_for_captures_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool RegExpParser::ParseBackReferenceIndex(int* index_out) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK_EQ('\\', current());
|
|
|
|
DCHECK('1' <= Next() && Next() <= '9');
|
2009-01-30 10:38:25 +00:00
|
|
|
// Try to parse a decimal literal that is no greater than the total number
|
|
|
|
// of left capturing parentheses in the input.
|
2008-11-25 11:07:48 +00:00
|
|
|
int start = position();
|
|
|
|
int value = Next() - '0';
|
|
|
|
Advance(2);
|
|
|
|
while (true) {
|
|
|
|
uc32 c = current();
|
|
|
|
if (IsDecimalDigit(c)) {
|
|
|
|
value = 10 * value + (c - '0');
|
2009-01-30 10:38:25 +00:00
|
|
|
if (value > kMaxCaptures) {
|
|
|
|
Reset(start);
|
|
|
|
return false;
|
|
|
|
}
|
2008-11-25 11:07:48 +00:00
|
|
|
Advance();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-11-25 14:14:04 +00:00
|
|
|
if (value > captures_started()) {
|
|
|
|
if (!is_scanned_for_captures_) {
|
|
|
|
int saved_position = position();
|
|
|
|
ScanForCaptures();
|
|
|
|
Reset(saved_position);
|
|
|
|
}
|
|
|
|
if (value > capture_count_) {
|
|
|
|
Reset(start);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2008-11-25 11:07:48 +00:00
|
|
|
*index_out = value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// QuantifierPrefix ::
|
|
|
|
// { DecimalDigits }
|
|
|
|
// { DecimalDigits , }
|
|
|
|
// { DecimalDigits , DecimalDigits }
|
2009-02-05 12:55:20 +00:00
|
|
|
//
|
|
|
|
// Returns true if parsing succeeds, and set the min_out and max_out
|
|
|
|
// values. Values are truncated to RegExpTree::kInfinity if they overflow.
|
2008-11-25 11:07:48 +00:00
|
|
|
bool RegExpParser::ParseIntervalQuantifier(int* min_out, int* max_out) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK_EQ(current(), '{');
|
2008-11-25 11:07:48 +00:00
|
|
|
int start = position();
|
|
|
|
Advance();
|
|
|
|
int min = 0;
|
|
|
|
if (!IsDecimalDigit(current())) {
|
|
|
|
Reset(start);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
while (IsDecimalDigit(current())) {
|
2009-02-05 12:55:20 +00:00
|
|
|
int next = current() - '0';
|
|
|
|
if (min > (RegExpTree::kInfinity - next) / 10) {
|
|
|
|
// Overflow. Skip past remaining decimal digits and return -1.
|
2009-02-05 13:24:13 +00:00
|
|
|
do {
|
|
|
|
Advance();
|
|
|
|
} while (IsDecimalDigit(current()));
|
2009-02-05 12:55:20 +00:00
|
|
|
min = RegExpTree::kInfinity;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
min = 10 * min + next;
|
2008-11-25 11:07:48 +00:00
|
|
|
Advance();
|
|
|
|
}
|
|
|
|
int max = 0;
|
|
|
|
if (current() == '}') {
|
|
|
|
max = min;
|
|
|
|
Advance();
|
|
|
|
} else if (current() == ',') {
|
|
|
|
Advance();
|
|
|
|
if (current() == '}') {
|
2008-12-17 10:59:14 +00:00
|
|
|
max = RegExpTree::kInfinity;
|
2008-11-25 11:07:48 +00:00
|
|
|
Advance();
|
|
|
|
} else {
|
|
|
|
while (IsDecimalDigit(current())) {
|
2009-02-05 12:55:20 +00:00
|
|
|
int next = current() - '0';
|
|
|
|
if (max > (RegExpTree::kInfinity - next) / 10) {
|
2009-02-05 13:24:13 +00:00
|
|
|
do {
|
|
|
|
Advance();
|
|
|
|
} while (IsDecimalDigit(current()));
|
|
|
|
max = RegExpTree::kInfinity;
|
2009-02-05 12:55:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
max = 10 * max + next;
|
2008-11-25 11:07:48 +00:00
|
|
|
Advance();
|
|
|
|
}
|
|
|
|
if (current() != '}') {
|
|
|
|
Reset(start);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Advance();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Reset(start);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*min_out = min;
|
|
|
|
*max_out = max;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uc32 RegExpParser::ParseOctalLiteral() {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(('0' <= current() && current() <= '7') || current() == kEndMarker);
|
2008-11-25 11:07:48 +00:00
|
|
|
// For compatibility with some other browsers (not all), we parse
|
|
|
|
// up to three octal digits with a value below 256.
|
|
|
|
uc32 value = current() - '0';
|
|
|
|
Advance();
|
|
|
|
if ('0' <= current() && current() <= '7') {
|
|
|
|
value = value * 8 + current() - '0';
|
|
|
|
Advance();
|
|
|
|
if (value < 32 && '0' <= current() && current() <= '7') {
|
|
|
|
value = value * 8 + current() - '0';
|
|
|
|
Advance();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-12 09:50:15 +00:00
|
|
|
bool RegExpParser::ParseHexEscape(int length, uc32* value) {
|
2008-11-25 11:07:48 +00:00
|
|
|
int start = position();
|
|
|
|
uc32 val = 0;
|
2015-01-12 09:50:15 +00:00
|
|
|
for (int i = 0; i < length; ++i) {
|
2008-11-25 11:07:48 +00:00
|
|
|
uc32 c = current();
|
|
|
|
int d = HexValue(c);
|
|
|
|
if (d < 0) {
|
|
|
|
Reset(start);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
val = val * 16 + d;
|
|
|
|
Advance();
|
|
|
|
}
|
|
|
|
*value = val;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-12 09:50:15 +00:00
|
|
|
bool RegExpParser::ParseUnicodeEscape(uc32* value) {
|
|
|
|
// Accept both \uxxxx and \u{xxxxxx} (if harmony unicode escapes are
|
|
|
|
// allowed). In the latter case, the number of hex digits between { } is
|
|
|
|
// arbitrary. \ and u have already been read.
|
2015-02-05 14:16:54 +00:00
|
|
|
if (current() == '{' && FLAG_harmony_unicode_regexps && unicode_) {
|
2015-01-12 09:50:15 +00:00
|
|
|
int start = position();
|
|
|
|
Advance();
|
|
|
|
if (ParseUnlimitedLengthHexNumber(0x10ffff, value)) {
|
|
|
|
if (current() == '}') {
|
|
|
|
Advance();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Reset(start);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// \u but no {, or \u{...} escapes not allowed.
|
|
|
|
return ParseHexEscape(4, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool RegExpParser::ParseUnlimitedLengthHexNumber(int max_value, uc32* value) {
|
|
|
|
uc32 x = 0;
|
|
|
|
int d = HexValue(current());
|
|
|
|
if (d < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
while (d >= 0) {
|
|
|
|
x = x * 16 + d;
|
|
|
|
if (x > max_value) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Advance();
|
|
|
|
d = HexValue(current());
|
|
|
|
}
|
|
|
|
*value = x;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-01 15:32:20 +00:00
|
|
|
uc32 RegExpParser::ParseClassCharacterEscape() {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(current() == '\\');
|
|
|
|
DCHECK(has_next() && !IsSpecialClassEscape(Next()));
|
2008-11-25 11:07:48 +00:00
|
|
|
Advance();
|
|
|
|
switch (current()) {
|
|
|
|
case 'b':
|
|
|
|
Advance();
|
|
|
|
return '\b';
|
|
|
|
// ControlEscape :: one of
|
|
|
|
// f n r t v
|
|
|
|
case 'f':
|
|
|
|
Advance();
|
|
|
|
return '\f';
|
|
|
|
case 'n':
|
|
|
|
Advance();
|
|
|
|
return '\n';
|
|
|
|
case 'r':
|
|
|
|
Advance();
|
|
|
|
return '\r';
|
|
|
|
case 't':
|
|
|
|
Advance();
|
|
|
|
return '\t';
|
|
|
|
case 'v':
|
|
|
|
Advance();
|
|
|
|
return '\v';
|
2011-01-07 12:35:42 +00:00
|
|
|
case 'c': {
|
|
|
|
uc32 controlLetter = Next();
|
|
|
|
uc32 letter = controlLetter & ~('A' ^ 'a');
|
|
|
|
// For compatibility with JSC, inside a character class
|
|
|
|
// we also accept digits and underscore as control characters.
|
|
|
|
if ((controlLetter >= '0' && controlLetter <= '9') ||
|
|
|
|
controlLetter == '_' ||
|
|
|
|
(letter >= 'A' && letter <= 'Z')) {
|
|
|
|
Advance(2);
|
|
|
|
// Control letters mapped to ASCII control characters in the range
|
|
|
|
// 0x00-0x1f.
|
|
|
|
return controlLetter & 0x1f;
|
|
|
|
}
|
|
|
|
// We match JSC in reading the backslash as a literal
|
|
|
|
// character instead of as starting an escape.
|
|
|
|
return '\\';
|
|
|
|
}
|
2008-11-25 11:07:48 +00:00
|
|
|
case '0': case '1': case '2': case '3': case '4': case '5':
|
|
|
|
case '6': case '7':
|
|
|
|
// For compatibility, we interpret a decimal escape that isn't
|
|
|
|
// a back reference (and therefore either \0 or not valid according
|
|
|
|
// to the specification) as a 1..3 digit octal character code.
|
|
|
|
return ParseOctalLiteral();
|
|
|
|
case 'x': {
|
|
|
|
Advance();
|
|
|
|
uc32 value;
|
|
|
|
if (ParseHexEscape(2, &value)) {
|
|
|
|
return value;
|
|
|
|
}
|
2015-02-05 14:16:54 +00:00
|
|
|
if (!FLAG_harmony_unicode_regexps || !unicode_) {
|
2015-01-12 09:50:15 +00:00
|
|
|
// If \x is not followed by a two-digit hexadecimal, treat it
|
|
|
|
// as an identity escape.
|
|
|
|
return 'x';
|
|
|
|
}
|
|
|
|
// If the 'u' flag is present, invalid escapes are not treated as
|
|
|
|
// identity escapes.
|
|
|
|
ReportError(CStrVector("Invalid escape"));
|
|
|
|
return 0;
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
case 'u': {
|
|
|
|
Advance();
|
|
|
|
uc32 value;
|
2015-01-12 09:50:15 +00:00
|
|
|
if (ParseUnicodeEscape(&value)) {
|
2008-11-25 11:07:48 +00:00
|
|
|
return value;
|
|
|
|
}
|
2015-02-05 14:16:54 +00:00
|
|
|
if (!FLAG_harmony_unicode_regexps || !unicode_) {
|
2015-01-12 09:50:15 +00:00
|
|
|
return 'u';
|
|
|
|
}
|
|
|
|
// If the 'u' flag is present, invalid escapes are not treated as
|
|
|
|
// identity escapes.
|
|
|
|
ReportError(CStrVector("Invalid unicode escape"));
|
|
|
|
return 0;
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
default: {
|
|
|
|
uc32 result = current();
|
2015-01-12 09:50:15 +00:00
|
|
|
// If the 'u' flag is present, only syntax characters can be escaped, no
|
|
|
|
// other identity escapes are allowed. If the 'u' flag is not present, all
|
|
|
|
// identity escapes are allowed.
|
2015-02-05 14:16:54 +00:00
|
|
|
if (!FLAG_harmony_unicode_regexps || !unicode_ ||
|
|
|
|
IsSyntaxCharacter(result)) {
|
2015-01-12 09:50:15 +00:00
|
|
|
Advance();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
ReportError(CStrVector("Invalid escape"));
|
|
|
|
return 0;
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-01 15:32:20 +00:00
|
|
|
CharacterRange RegExpParser::ParseClassAtom(uc16* char_class) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK_EQ(0, *char_class);
|
2008-11-25 11:07:48 +00:00
|
|
|
uc32 first = current();
|
|
|
|
if (first == '\\') {
|
|
|
|
switch (Next()) {
|
|
|
|
case 'w': case 'W': case 'd': case 'D': case 's': case 'S': {
|
2008-12-01 14:29:28 +00:00
|
|
|
*char_class = Next();
|
2008-11-25 11:07:48 +00:00
|
|
|
Advance(2);
|
2008-12-01 14:29:28 +00:00
|
|
|
return CharacterRange::Singleton(0); // Return dummy value.
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
2008-12-18 14:30:53 +00:00
|
|
|
case kEndMarker:
|
2009-07-22 12:33:16 +00:00
|
|
|
return ReportError(CStrVector("\\ at end of pattern"));
|
2008-11-25 11:07:48 +00:00
|
|
|
default:
|
2008-12-01 15:32:20 +00:00
|
|
|
uc32 c = ParseClassCharacterEscape(CHECK_FAILED);
|
2008-11-25 11:07:48 +00:00
|
|
|
return CharacterRange::Singleton(c);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Advance();
|
|
|
|
return CharacterRange::Singleton(first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-13 08:33:32 +00:00
|
|
|
static const uc16 kNoCharClass = 0;
|
|
|
|
|
|
|
|
// Adds range or pre-defined character class to character ranges.
|
|
|
|
// If char_class is not kInvalidClass, it's interpreted as a class
|
|
|
|
// escape (i.e., 's' means whitespace, from '\s').
|
|
|
|
static inline void AddRangeOrEscape(ZoneList<CharacterRange>* ranges,
|
|
|
|
uc16 char_class,
|
2012-06-11 12:42:31 +00:00
|
|
|
CharacterRange range,
|
|
|
|
Zone* zone) {
|
2010-12-13 08:33:32 +00:00
|
|
|
if (char_class != kNoCharClass) {
|
2012-06-11 12:42:31 +00:00
|
|
|
CharacterRange::AddClassEscape(char_class, ranges, zone);
|
2010-12-13 08:33:32 +00:00
|
|
|
} else {
|
2012-06-11 12:42:31 +00:00
|
|
|
ranges->Add(range, zone);
|
2010-12-13 08:33:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-01 15:32:20 +00:00
|
|
|
RegExpTree* RegExpParser::ParseCharacterClass() {
|
2008-11-25 11:07:48 +00:00
|
|
|
static const char* kUnterminated = "Unterminated character class";
|
|
|
|
static const char* kRangeOutOfOrder = "Range out of order in character class";
|
|
|
|
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK_EQ(current(), '[');
|
2008-11-25 11:07:48 +00:00
|
|
|
Advance();
|
|
|
|
bool is_negated = false;
|
|
|
|
if (current() == '^') {
|
|
|
|
is_negated = true;
|
|
|
|
Advance();
|
|
|
|
}
|
2012-06-11 12:42:31 +00:00
|
|
|
ZoneList<CharacterRange>* ranges =
|
|
|
|
new(zone()) ZoneList<CharacterRange>(2, zone());
|
2008-11-25 11:07:48 +00:00
|
|
|
while (has_more() && current() != ']') {
|
2010-12-13 08:33:32 +00:00
|
|
|
uc16 char_class = kNoCharClass;
|
2008-12-01 15:32:20 +00:00
|
|
|
CharacterRange first = ParseClassAtom(&char_class CHECK_FAILED);
|
2008-12-01 14:29:28 +00:00
|
|
|
if (current() == '-') {
|
|
|
|
Advance();
|
|
|
|
if (current() == kEndMarker) {
|
|
|
|
// If we reach the end we break out of the loop and let the
|
|
|
|
// following code report an error.
|
|
|
|
break;
|
|
|
|
} else if (current() == ']') {
|
2012-06-11 12:42:31 +00:00
|
|
|
AddRangeOrEscape(ranges, char_class, first, zone());
|
|
|
|
ranges->Add(CharacterRange::Singleton('-'), zone());
|
2008-12-01 14:29:28 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-12-13 08:33:32 +00:00
|
|
|
uc16 char_class_2 = kNoCharClass;
|
|
|
|
CharacterRange next = ParseClassAtom(&char_class_2 CHECK_FAILED);
|
|
|
|
if (char_class != kNoCharClass || char_class_2 != kNoCharClass) {
|
|
|
|
// Either end is an escaped character class. Treat the '-' verbatim.
|
2012-06-11 12:42:31 +00:00
|
|
|
AddRangeOrEscape(ranges, char_class, first, zone());
|
|
|
|
ranges->Add(CharacterRange::Singleton('-'), zone());
|
|
|
|
AddRangeOrEscape(ranges, char_class_2, next, zone());
|
2010-12-09 12:07:52 +00:00
|
|
|
continue;
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
2008-12-01 14:29:28 +00:00
|
|
|
if (first.from() > next.to()) {
|
2008-12-01 15:32:20 +00:00
|
|
|
return ReportError(CStrVector(kRangeOutOfOrder) CHECK_FAILED);
|
2008-12-01 14:29:28 +00:00
|
|
|
}
|
2012-06-11 12:42:31 +00:00
|
|
|
ranges->Add(CharacterRange::Range(first.from(), next.to()), zone());
|
2008-12-01 14:29:28 +00:00
|
|
|
} else {
|
2012-06-11 12:42:31 +00:00
|
|
|
AddRangeOrEscape(ranges, char_class, first, zone());
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!has_more()) {
|
2008-12-01 15:32:20 +00:00
|
|
|
return ReportError(CStrVector(kUnterminated) CHECK_FAILED);
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
Advance();
|
|
|
|
if (ranges->length() == 0) {
|
2012-06-11 12:42:31 +00:00
|
|
|
ranges->Add(CharacterRange::Everything(), zone());
|
2008-11-25 11:07:48 +00:00
|
|
|
is_negated = !is_negated;
|
|
|
|
}
|
2011-04-04 06:29:02 +00:00
|
|
|
return new(zone()) RegExpCharacterClass(ranges, is_negated);
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// The Parser interface.
|
|
|
|
|
2015-01-23 15:19:34 +00:00
|
|
|
bool RegExpParser::ParseRegExp(Isolate* isolate, Zone* zone,
|
|
|
|
FlatStringReader* input, bool multiline,
|
|
|
|
bool unicode, RegExpCompileData* result) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(result != NULL);
|
2015-01-23 15:19:34 +00:00
|
|
|
RegExpParser parser(input, &result->error, multiline, unicode, isolate, zone);
|
2008-12-12 10:22:56 +00:00
|
|
|
RegExpTree* tree = parser.ParsePattern();
|
2008-12-01 15:32:20 +00:00
|
|
|
if (parser.failed()) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(tree == NULL);
|
|
|
|
DCHECK(!result->error.is_null());
|
2008-11-25 11:07:48 +00:00
|
|
|
} else {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(tree != NULL);
|
|
|
|
DCHECK(result->error.is_null());
|
2008-12-12 10:22:56 +00:00
|
|
|
result->tree = tree;
|
|
|
|
int capture_count = parser.captures_started();
|
|
|
|
result->simple = tree->IsAtom() && parser.simple() && capture_count == 0;
|
2009-02-03 11:43:55 +00:00
|
|
|
result->contains_anchor = parser.contains_anchor();
|
2008-12-12 10:22:56 +00:00
|
|
|
result->capture_count = capture_count;
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
2008-12-01 15:32:20 +00:00
|
|
|
return !parser.failed();
|
2008-11-25 11:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-09 14:51:13 +00:00
|
|
|
bool Parser::ParseStatic(ParseInfo* info) {
|
|
|
|
Parser parser(info);
|
2015-02-12 13:02:30 +00:00
|
|
|
if (parser.Parse(info)) {
|
2015-03-09 14:51:13 +00:00
|
|
|
info->set_language_mode(info->function()->language_mode());
|
2015-02-12 11:56:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-09 14:51:13 +00:00
|
|
|
bool Parser::Parse(ParseInfo* info) {
|
2015-02-12 13:02:30 +00:00
|
|
|
DCHECK(info->function() == NULL);
|
2010-10-04 11:35:46 +00:00
|
|
|
FunctionLiteral* result = NULL;
|
2015-02-12 13:02:30 +00:00
|
|
|
// Ok to use Isolate here; this function is only called in the main thread.
|
|
|
|
DCHECK(parsing_on_main_thread_);
|
|
|
|
Isolate* isolate = info->isolate();
|
|
|
|
pre_parse_timer_ = isolate->counters()->pre_parse();
|
2014-11-20 10:51:49 +00:00
|
|
|
if (FLAG_trace_parse || allow_natives() || extension_ != NULL) {
|
2014-06-24 14:03:24 +00:00
|
|
|
// If intrinsics are allowed, the Parser cannot operate independent of the
|
2014-09-02 11:36:21 +00:00
|
|
|
// V8 heap because of Runtime. Tell the string table to internalize strings
|
2014-06-24 14:03:24 +00:00
|
|
|
// and values right after they're created.
|
2015-02-12 13:02:30 +00:00
|
|
|
ast_value_factory()->Internalize(isolate);
|
2014-06-24 14:03:24 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 13:02:30 +00:00
|
|
|
if (info->is_lazy()) {
|
|
|
|
DCHECK(!info->is_eval());
|
|
|
|
if (info->shared_info()->is_function()) {
|
2015-02-20 09:39:32 +00:00
|
|
|
result = ParseLazy(isolate, info);
|
2012-02-14 14:14:51 +00:00
|
|
|
} else {
|
2015-02-20 09:39:32 +00:00
|
|
|
result = ParseProgram(isolate, info);
|
2012-02-14 14:14:51 +00:00
|
|
|
}
|
2010-02-01 10:31:55 +00:00
|
|
|
} else {
|
2015-02-12 13:02:30 +00:00
|
|
|
SetCachedData(info);
|
2015-02-20 09:39:32 +00:00
|
|
|
result = ParseProgram(isolate, info);
|
2010-02-01 10:31:55 +00:00
|
|
|
}
|
2015-03-09 14:51:13 +00:00
|
|
|
info->set_literal(result);
|
2014-09-02 11:36:21 +00:00
|
|
|
|
2015-02-20 09:39:32 +00:00
|
|
|
Internalize(isolate, info->script(), result == NULL);
|
2014-09-11 09:52:36 +00:00
|
|
|
DCHECK(ast_value_factory()->IsInternalized());
|
2010-10-04 11:35:46 +00:00
|
|
|
return (result != NULL);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2014-09-12 09:12:08 +00:00
|
|
|
|
2015-03-09 14:51:13 +00:00
|
|
|
void Parser::ParseOnBackground(ParseInfo* info) {
|
2015-02-12 13:02:30 +00:00
|
|
|
parsing_on_main_thread_ = false;
|
|
|
|
|
|
|
|
DCHECK(info->function() == NULL);
|
2014-09-12 09:12:08 +00:00
|
|
|
FunctionLiteral* result = NULL;
|
|
|
|
fni_ = new (zone()) FuncNameInferrer(ast_value_factory(), zone());
|
|
|
|
|
|
|
|
CompleteParserRecorder recorder;
|
2014-11-17 12:16:27 +00:00
|
|
|
if (produce_cached_parse_data()) log_ = &recorder;
|
2014-09-12 09:12:08 +00:00
|
|
|
|
2015-02-12 13:02:30 +00:00
|
|
|
DCHECK(info->source_stream() != NULL);
|
|
|
|
ExternalStreamingStream stream(info->source_stream(),
|
|
|
|
info->source_stream_encoding());
|
2014-09-12 09:12:08 +00:00
|
|
|
scanner_.Initialize(&stream);
|
2015-02-12 13:02:30 +00:00
|
|
|
DCHECK(info->context().is_null() || info->context()->IsNativeContext());
|
2014-09-12 09:12:08 +00:00
|
|
|
|
|
|
|
// When streaming, we don't know the length of the source until we have parsed
|
|
|
|
// it. The raw data can be UTF-8, so we wouldn't know the source length until
|
|
|
|
// we have decoded it anyway even if we knew the raw data length (which we
|
|
|
|
// don't). We work around this by storing all the scopes which need their end
|
|
|
|
// position set at the end of the script (the top scope and possible eval
|
|
|
|
// scopes) and set their end position after we know the script length.
|
2015-04-16 12:42:43 +00:00
|
|
|
result = DoParseProgram(info);
|
2014-09-12 09:12:08 +00:00
|
|
|
|
2015-03-09 14:51:13 +00:00
|
|
|
info->set_literal(result);
|
2014-09-12 09:12:08 +00:00
|
|
|
|
|
|
|
// We cannot internalize on a background thread; a foreground task will take
|
|
|
|
// care of calling Parser::Internalize just before compilation.
|
|
|
|
|
2014-11-17 12:16:27 +00:00
|
|
|
if (produce_cached_parse_data()) {
|
2015-02-12 13:02:30 +00:00
|
|
|
if (result != NULL) *info->cached_data() = recorder.GetScriptData();
|
2014-09-12 09:12:08 +00:00
|
|
|
log_ = NULL;
|
|
|
|
}
|
|
|
|
}
|
2014-11-14 18:53:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
ParserTraits::TemplateLiteralState Parser::OpenTemplateLiteral(int pos) {
|
|
|
|
return new (zone()) ParserTraits::TemplateLiteral(zone(), pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Parser::AddTemplateSpan(TemplateLiteralState* state, bool tail) {
|
|
|
|
int pos = scanner()->location().beg_pos;
|
|
|
|
int end = scanner()->location().end_pos - (tail ? 1 : 2);
|
|
|
|
const AstRawString* tv = scanner()->CurrentSymbol(ast_value_factory());
|
2014-12-03 14:17:16 +00:00
|
|
|
const AstRawString* trv = scanner()->CurrentRawSymbol(ast_value_factory());
|
2014-11-14 18:53:41 +00:00
|
|
|
Literal* cooked = factory()->NewStringLiteral(tv, pos);
|
2014-12-03 14:17:16 +00:00
|
|
|
Literal* raw = factory()->NewStringLiteral(trv, pos);
|
|
|
|
(*state)->AddTemplateSpan(cooked, raw, end, zone());
|
2014-11-14 18:53:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Parser::AddTemplateExpression(TemplateLiteralState* state,
|
|
|
|
Expression* expression) {
|
|
|
|
(*state)->AddExpression(expression, zone());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Expression* Parser::CloseTemplateLiteral(TemplateLiteralState* state, int start,
|
|
|
|
Expression* tag) {
|
|
|
|
TemplateLiteral* lit = *state;
|
|
|
|
int pos = lit->position();
|
|
|
|
const ZoneList<Expression*>* cooked_strings = lit->cooked();
|
2014-12-03 14:17:16 +00:00
|
|
|
const ZoneList<Expression*>* raw_strings = lit->raw();
|
2014-11-14 18:53:41 +00:00
|
|
|
const ZoneList<Expression*>* expressions = lit->expressions();
|
2014-12-03 14:17:16 +00:00
|
|
|
DCHECK_EQ(cooked_strings->length(), raw_strings->length());
|
|
|
|
DCHECK_EQ(cooked_strings->length(), expressions->length() + 1);
|
2014-11-14 18:53:41 +00:00
|
|
|
|
|
|
|
if (!tag) {
|
|
|
|
// Build tree of BinaryOps to simplify code-generation
|
2015-03-24 12:43:50 +00:00
|
|
|
Expression* expr = cooked_strings->at(0);
|
|
|
|
int i = 0;
|
|
|
|
while (i < expressions->length()) {
|
|
|
|
Expression* sub = expressions->at(i++);
|
|
|
|
Expression* cooked_str = cooked_strings->at(i);
|
|
|
|
|
|
|
|
// Let middle be ToString(sub).
|
|
|
|
ZoneList<Expression*>* args =
|
|
|
|
new (zone()) ZoneList<Expression*>(1, zone());
|
|
|
|
args->Add(sub, zone());
|
|
|
|
Expression* middle = factory()->NewCallRuntime(
|
|
|
|
ast_value_factory()->to_string_string(), NULL, args,
|
|
|
|
sub->position());
|
2014-11-14 18:53:41 +00:00
|
|
|
|
|
|
|
expr = factory()->NewBinaryOperation(
|
2015-03-24 12:43:50 +00:00
|
|
|
Token::ADD, factory()->NewBinaryOperation(
|
|
|
|
Token::ADD, expr, middle, expr->position()),
|
|
|
|
cooked_str, sub->position());
|
2014-11-14 18:53:41 +00:00
|
|
|
}
|
|
|
|
return expr;
|
|
|
|
} else {
|
2014-12-03 14:17:16 +00:00
|
|
|
uint32_t hash = ComputeTemplateLiteralHash(lit);
|
2014-11-14 18:53:41 +00:00
|
|
|
|
|
|
|
int cooked_idx = function_state_->NextMaterializedLiteralIndex();
|
|
|
|
int raw_idx = function_state_->NextMaterializedLiteralIndex();
|
|
|
|
|
2015-04-16 12:17:46 +00:00
|
|
|
// $getTemplateCallSite
|
2014-11-14 18:53:41 +00:00
|
|
|
ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(4, zone());
|
|
|
|
args->Add(factory()->NewArrayLiteral(
|
|
|
|
const_cast<ZoneList<Expression*>*>(cooked_strings),
|
2015-05-20 07:07:54 +00:00
|
|
|
cooked_idx, is_strong(language_mode()), pos),
|
2014-11-14 18:53:41 +00:00
|
|
|
zone());
|
|
|
|
args->Add(
|
|
|
|
factory()->NewArrayLiteral(
|
2015-05-20 07:07:54 +00:00
|
|
|
const_cast<ZoneList<Expression*>*>(raw_strings), raw_idx,
|
|
|
|
is_strong(language_mode()), pos),
|
2014-11-14 18:53:41 +00:00
|
|
|
zone());
|
2014-11-20 22:37:31 +00:00
|
|
|
|
2014-12-03 14:17:16 +00:00
|
|
|
// Ensure hash is suitable as a Smi value
|
2014-11-20 22:37:31 +00:00
|
|
|
Smi* hash_obj = Smi::cast(Internals::IntToSmi(static_cast<int>(hash)));
|
|
|
|
args->Add(factory()->NewSmiLiteral(hash_obj->value(), pos), zone());
|
|
|
|
|
2014-11-14 18:53:41 +00:00
|
|
|
this->CheckPossibleEvalCall(tag, scope_);
|
|
|
|
Expression* call_site = factory()->NewCallRuntime(
|
|
|
|
ast_value_factory()->get_template_callsite_string(), NULL, args, start);
|
|
|
|
|
|
|
|
// Call TagFn
|
|
|
|
ZoneList<Expression*>* call_args =
|
|
|
|
new (zone()) ZoneList<Expression*>(expressions->length() + 1, zone());
|
|
|
|
call_args->Add(call_site, zone());
|
|
|
|
call_args->AddAll(*expressions, zone());
|
|
|
|
return factory()->NewCall(tag, call_args, pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-03 14:17:16 +00:00
|
|
|
uint32_t Parser::ComputeTemplateLiteralHash(const TemplateLiteral* lit) {
|
|
|
|
const ZoneList<Expression*>* raw_strings = lit->raw();
|
|
|
|
int total = raw_strings->length();
|
2014-11-14 18:53:41 +00:00
|
|
|
DCHECK(total);
|
|
|
|
|
2014-11-27 15:47:42 +00:00
|
|
|
uint32_t running_hash = 0;
|
2014-11-20 22:37:31 +00:00
|
|
|
|
2014-11-14 18:53:41 +00:00
|
|
|
for (int index = 0; index < total; ++index) {
|
2014-11-20 22:37:31 +00:00
|
|
|
if (index) {
|
2014-11-27 15:47:42 +00:00
|
|
|
running_hash = StringHasher::ComputeRunningHashOneByte(
|
|
|
|
running_hash, "${}", 3);
|
2014-11-20 22:37:31 +00:00
|
|
|
}
|
|
|
|
|
2014-12-03 14:17:16 +00:00
|
|
|
const AstRawString* raw_string =
|
|
|
|
raw_strings->at(index)->AsLiteral()->raw_value()->AsString();
|
|
|
|
if (raw_string->is_one_byte()) {
|
|
|
|
const char* data = reinterpret_cast<const char*>(raw_string->raw_data());
|
|
|
|
running_hash = StringHasher::ComputeRunningHashOneByte(
|
|
|
|
running_hash, data, raw_string->length());
|
2014-11-26 17:15:47 +00:00
|
|
|
} else {
|
2014-12-03 14:17:16 +00:00
|
|
|
const uc16* data = reinterpret_cast<const uc16*>(raw_string->raw_data());
|
|
|
|
running_hash = StringHasher::ComputeRunningHash(running_hash, data,
|
|
|
|
raw_string->length());
|
2014-11-26 17:15:47 +00:00
|
|
|
}
|
2014-11-14 18:53:41 +00:00
|
|
|
}
|
|
|
|
|
2014-12-03 14:17:16 +00:00
|
|
|
return running_hash;
|
2014-11-14 18:53:41 +00:00
|
|
|
}
|
2015-04-09 19:37:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
ZoneList<v8::internal::Expression*>* Parser::PrepareSpreadArguments(
|
|
|
|
ZoneList<v8::internal::Expression*>* list) {
|
|
|
|
ZoneList<v8::internal::Expression*>* args =
|
|
|
|
new (zone()) ZoneList<v8::internal::Expression*>(1, zone());
|
|
|
|
if (list->length() == 1) {
|
|
|
|
// Spread-call with single spread argument produces an InternalArray
|
|
|
|
// containing the values from the array.
|
|
|
|
//
|
|
|
|
// Function is called or constructed with the produced array of arguments
|
|
|
|
//
|
|
|
|
// EG: Apply(Func, Spread(spread0))
|
|
|
|
ZoneList<Expression*>* spread_list =
|
|
|
|
new (zone()) ZoneList<Expression*>(0, zone());
|
|
|
|
spread_list->Add(list->at(0)->AsSpread()->expression(), zone());
|
|
|
|
args->Add(
|
|
|
|
factory()->NewCallRuntime(ast_value_factory()->spread_iterable_string(),
|
|
|
|
NULL, spread_list, RelocInfo::kNoPosition),
|
|
|
|
zone());
|
|
|
|
return args;
|
|
|
|
} else {
|
|
|
|
// Spread-call with multiple arguments produces array literals for each
|
|
|
|
// sequences of unspread arguments, and converts each spread iterable to
|
|
|
|
// an Internal array. Finally, all of these produced arrays are flattened
|
|
|
|
// into a single InternalArray, containing the arguments for the call.
|
|
|
|
//
|
|
|
|
// EG: Apply(Func, Flatten([unspread0, unspread1], Spread(spread0),
|
|
|
|
// Spread(spread1), [unspread2, unspread3]))
|
|
|
|
int i = 0;
|
|
|
|
int n = list->length();
|
|
|
|
while (i < n) {
|
|
|
|
if (!list->at(i)->IsSpread()) {
|
|
|
|
ZoneList<v8::internal::Expression*>* unspread =
|
|
|
|
new (zone()) ZoneList<v8::internal::Expression*>(1, zone());
|
|
|
|
|
|
|
|
// Push array of unspread parameters
|
|
|
|
while (i < n && !list->at(i)->IsSpread()) {
|
|
|
|
unspread->Add(list->at(i++), zone());
|
|
|
|
}
|
|
|
|
int literal_index = function_state_->NextMaterializedLiteralIndex();
|
|
|
|
args->Add(factory()->NewArrayLiteral(unspread, literal_index,
|
2015-05-20 07:07:54 +00:00
|
|
|
is_strong(language_mode()),
|
2015-04-09 19:37:14 +00:00
|
|
|
RelocInfo::kNoPosition),
|
|
|
|
zone());
|
|
|
|
|
|
|
|
if (i == n) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push eagerly spread argument
|
|
|
|
ZoneList<v8::internal::Expression*>* spread_list =
|
|
|
|
new (zone()) ZoneList<v8::internal::Expression*>(1, zone());
|
|
|
|
spread_list->Add(list->at(i++)->AsSpread()->expression(), zone());
|
|
|
|
args->Add(factory()->NewCallRuntime(
|
|
|
|
ast_value_factory()->spread_iterable_string(), NULL,
|
|
|
|
spread_list, RelocInfo::kNoPosition),
|
|
|
|
zone());
|
|
|
|
}
|
|
|
|
|
|
|
|
list = new (zone()) ZoneList<v8::internal::Expression*>(1, zone());
|
|
|
|
list->Add(factory()->NewCallRuntime(
|
|
|
|
ast_value_factory()->spread_arguments_string(), NULL, args,
|
|
|
|
RelocInfo::kNoPosition),
|
|
|
|
zone());
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Expression* Parser::SpreadCall(Expression* function,
|
|
|
|
ZoneList<v8::internal::Expression*>* args,
|
|
|
|
int pos) {
|
2015-06-02 22:04:25 +00:00
|
|
|
if (function->IsSuperCallReference()) {
|
2015-04-09 19:37:14 +00:00
|
|
|
// Super calls
|
2015-06-02 22:04:25 +00:00
|
|
|
// %_CallSuperWithSpread(%ReflectConstruct(<super>, args, new.target))
|
2015-04-09 19:37:14 +00:00
|
|
|
args->InsertAt(0, function, zone());
|
2015-06-02 22:04:25 +00:00
|
|
|
args->Add(function->AsSuperCallReference()->new_target_var(), zone());
|
2015-04-09 19:37:14 +00:00
|
|
|
Expression* result = factory()->NewCallRuntime(
|
|
|
|
ast_value_factory()->reflect_construct_string(), NULL, args, pos);
|
2015-06-02 22:04:25 +00:00
|
|
|
args = new (zone()) ZoneList<Expression*>(1, zone());
|
2015-04-09 19:37:14 +00:00
|
|
|
args->Add(result, zone());
|
|
|
|
return factory()->NewCallRuntime(
|
|
|
|
ast_value_factory()->empty_string(),
|
|
|
|
Runtime::FunctionForId(Runtime::kInlineCallSuperWithSpread), args, pos);
|
|
|
|
} else {
|
|
|
|
if (function->IsProperty()) {
|
|
|
|
// Method calls
|
2015-05-14 22:59:22 +00:00
|
|
|
if (function->AsProperty()->IsSuperAccess()) {
|
2015-05-19 14:51:10 +00:00
|
|
|
Expression* home =
|
|
|
|
ThisExpression(scope_, factory(), RelocInfo::kNoPosition);
|
2015-05-14 22:59:22 +00:00
|
|
|
args->InsertAt(0, function, zone());
|
|
|
|
args->InsertAt(1, home, zone());
|
|
|
|
} else {
|
|
|
|
Variable* temp =
|
|
|
|
scope_->NewTemporary(ast_value_factory()->empty_string());
|
|
|
|
VariableProxy* obj = factory()->NewVariableProxy(temp);
|
|
|
|
Assignment* assign_obj = factory()->NewAssignment(
|
|
|
|
Token::ASSIGN, obj, function->AsProperty()->obj(),
|
|
|
|
RelocInfo::kNoPosition);
|
|
|
|
function = factory()->NewProperty(
|
|
|
|
assign_obj, function->AsProperty()->key(), RelocInfo::kNoPosition);
|
|
|
|
args->InsertAt(0, function, zone());
|
|
|
|
obj = factory()->NewVariableProxy(temp);
|
|
|
|
args->InsertAt(1, obj, zone());
|
|
|
|
}
|
2015-04-09 19:37:14 +00:00
|
|
|
} else {
|
|
|
|
// Non-method calls
|
|
|
|
args->InsertAt(0, function, zone());
|
|
|
|
args->InsertAt(1, factory()->NewUndefinedLiteral(RelocInfo::kNoPosition),
|
|
|
|
zone());
|
|
|
|
}
|
|
|
|
return factory()->NewCallRuntime(
|
|
|
|
ast_value_factory()->reflect_apply_string(), NULL, args, pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Expression* Parser::SpreadCallNew(Expression* function,
|
|
|
|
ZoneList<v8::internal::Expression*>* args,
|
|
|
|
int pos) {
|
|
|
|
args->InsertAt(0, function, zone());
|
|
|
|
|
|
|
|
return factory()->NewCallRuntime(
|
|
|
|
ast_value_factory()->reflect_construct_string(), NULL, args, pos);
|
|
|
|
}
|
2015-06-01 22:46:54 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|