2012-02-08 09:56:33 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2008-07-03 15:10:15 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#ifndef V8_PARSER_H_
|
|
|
|
#define V8_PARSER_H_
|
|
|
|
|
2009-03-23 07:27:47 +00:00
|
|
|
#include "allocation.h"
|
2010-10-01 15:32:32 +00:00
|
|
|
#include "ast.h"
|
2011-05-06 11:41:15 +00:00
|
|
|
#include "preparse-data-format.h"
|
2010-11-23 11:46:36 +00:00
|
|
|
#include "preparse-data.h"
|
2011-09-08 13:06:44 +00:00
|
|
|
#include "scopes.h"
|
2011-10-17 12:45:52 +00:00
|
|
|
#include "preparser.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
|
|
|
|
2010-10-04 11:35:46 +00:00
|
|
|
class CompilationInfo;
|
2010-10-01 14:10:47 +00:00
|
|
|
class FuncNameInferrer;
|
|
|
|
class ParserLog;
|
|
|
|
class PositionStack;
|
|
|
|
class Target;
|
|
|
|
|
|
|
|
template <typename T> class ZoneListWrapper;
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
class FunctionEntry BASE_EMBEDDED {
|
|
|
|
public:
|
2011-11-09 13:54:26 +00:00
|
|
|
enum {
|
|
|
|
kStartPositionIndex,
|
|
|
|
kEndPositionIndex,
|
|
|
|
kLiteralCountIndex,
|
|
|
|
kPropertyCountIndex,
|
2011-11-24 15:17:04 +00:00
|
|
|
kLanguageModeIndex,
|
2011-11-09 13:54:26 +00:00
|
|
|
kSize
|
|
|
|
};
|
|
|
|
|
2011-11-25 09:36:31 +00:00
|
|
|
explicit FunctionEntry(Vector<unsigned> backing)
|
|
|
|
: backing_(backing) { }
|
|
|
|
|
|
|
|
FunctionEntry() : backing_() { }
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-11-09 13:54:26 +00:00
|
|
|
int start_pos() { return backing_[kStartPositionIndex]; }
|
|
|
|
int end_pos() { return backing_[kEndPositionIndex]; }
|
|
|
|
int literal_count() { return backing_[kLiteralCountIndex]; }
|
|
|
|
int property_count() { return backing_[kPropertyCountIndex]; }
|
2011-11-24 15:17:04 +00:00
|
|
|
LanguageMode language_mode() {
|
|
|
|
ASSERT(backing_[kLanguageModeIndex] == CLASSIC_MODE ||
|
|
|
|
backing_[kLanguageModeIndex] == STRICT_MODE ||
|
|
|
|
backing_[kLanguageModeIndex] == EXTENDED_MODE);
|
|
|
|
return static_cast<LanguageMode>(backing_[kLanguageModeIndex]);
|
2011-10-27 13:08:51 +00:00
|
|
|
}
|
2010-08-27 08:26:29 +00:00
|
|
|
|
2011-11-09 13:54:26 +00:00
|
|
|
bool is_valid() { return !backing_.is_empty(); }
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Vector<unsigned> backing_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ScriptDataImpl : public ScriptData {
|
|
|
|
public:
|
|
|
|
explicit ScriptDataImpl(Vector<unsigned> store)
|
|
|
|
: store_(store),
|
2010-09-17 12:55:27 +00:00
|
|
|
owns_store_(true) { }
|
2010-09-07 12:52:16 +00:00
|
|
|
|
|
|
|
// Create an empty ScriptDataImpl that is guaranteed to not satisfy
|
|
|
|
// a SanityCheck.
|
2011-11-11 13:48:14 +00:00
|
|
|
ScriptDataImpl() : owns_store_(false) { }
|
2010-09-07 12:52:16 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
virtual ~ScriptDataImpl();
|
|
|
|
virtual int Length();
|
2010-05-25 06:38:19 +00:00
|
|
|
virtual const char* Data();
|
2010-01-11 12:13:24 +00:00
|
|
|
virtual bool HasError();
|
2010-09-07 12:52:16 +00:00
|
|
|
|
2010-09-15 10:54:35 +00:00
|
|
|
void Initialize();
|
|
|
|
void ReadNextSymbolPosition();
|
|
|
|
|
2010-08-27 08:26:29 +00:00
|
|
|
FunctionEntry GetFunctionEntry(int start);
|
2010-09-15 10:54:35 +00:00
|
|
|
int GetSymbolIdentifier();
|
2008-07-03 15:10:15 +00:00
|
|
|
bool SanityCheck();
|
|
|
|
|
|
|
|
Scanner::Location MessageLocation();
|
|
|
|
const char* BuildMessage();
|
|
|
|
Vector<const char*> BuildArgs();
|
|
|
|
|
2010-09-07 12:52:16 +00:00
|
|
|
int symbol_count() {
|
2010-11-23 11:46:36 +00:00
|
|
|
return (store_.length() > PreparseDataConstants::kHeaderSize)
|
|
|
|
? store_[PreparseDataConstants::kSymbolCountOffset]
|
|
|
|
: 0;
|
2010-09-07 12:52:16 +00:00
|
|
|
}
|
|
|
|
// The following functions should only be called if SanityCheck has
|
|
|
|
// returned true.
|
2010-11-23 11:46:36 +00:00
|
|
|
bool has_error() { return store_[PreparseDataConstants::kHasErrorOffset]; }
|
|
|
|
unsigned magic() { return store_[PreparseDataConstants::kMagicOffset]; }
|
|
|
|
unsigned version() { return store_[PreparseDataConstants::kVersionOffset]; }
|
2010-09-15 10:54:35 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
private:
|
2010-08-27 08:26:29 +00:00
|
|
|
Vector<unsigned> store_;
|
2010-09-15 10:54:35 +00:00
|
|
|
unsigned char* symbol_data_;
|
|
|
|
unsigned char* symbol_data_end_;
|
2010-09-07 12:52:16 +00:00
|
|
|
int function_index_;
|
|
|
|
bool owns_store_;
|
2010-08-27 08:26:29 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
unsigned Read(int position);
|
|
|
|
unsigned* ReadAddress(int position);
|
2010-09-15 10:54:35 +00:00
|
|
|
// Reads a number from the current symbols
|
|
|
|
int ReadNumber(byte** source);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2010-09-07 12:52:16 +00:00
|
|
|
ScriptDataImpl(const char* backing_store, int length)
|
|
|
|
: store_(reinterpret_cast<unsigned*>(const_cast<char*>(backing_store)),
|
2010-09-30 07:22:53 +00:00
|
|
|
length / static_cast<int>(sizeof(unsigned))),
|
2010-09-07 12:52:16 +00:00
|
|
|
owns_store_(false) {
|
2010-09-30 07:22:53 +00:00
|
|
|
ASSERT_EQ(0, static_cast<int>(
|
|
|
|
reinterpret_cast<intptr_t>(backing_store) % sizeof(unsigned)));
|
2010-09-07 12:52:16 +00:00
|
|
|
}
|
|
|
|
|
2010-08-25 06:46:53 +00:00
|
|
|
// Read strings written by ParserRecorder::WriteString.
|
|
|
|
static const char* ReadString(unsigned* start, int* chars);
|
2010-09-07 12:52:16 +00:00
|
|
|
|
|
|
|
friend class ScriptData;
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
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
|
|
|
class PreParserApi {
|
2010-10-01 14:10:47 +00:00
|
|
|
public:
|
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
|
|
|
// Pre-parse a character stream and return full preparse data.
|
|
|
|
//
|
|
|
|
// This interface is here instead of in preparser.h because it instantiates a
|
|
|
|
// preparser recorder object that is suited to the parser's purposes. Also,
|
|
|
|
// the preparser doesn't know about ScriptDataImpl.
|
2013-09-04 07:05:11 +00:00
|
|
|
static ScriptDataImpl* PreParse(Isolate* isolate,
|
|
|
|
Utf16CharacterStream* source);
|
2010-10-27 12:33:48 +00:00
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2010-11-02 11:45:47 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// REGEXP PARSING
|
2010-10-27 12:33:48 +00:00
|
|
|
|
2012-01-16 12:38:59 +00:00
|
|
|
// A BufferedZoneList is an automatically growing list, just like (and backed
|
2010-10-27 12:33:48 +00:00
|
|
|
// by) a ZoneList, that is optimized for the case of adding and removing
|
|
|
|
// a single element. The last element added is stored outside the backing list,
|
|
|
|
// and if no more than one element is ever added, the ZoneList isn't even
|
|
|
|
// allocated.
|
|
|
|
// Elements must not be NULL pointers.
|
|
|
|
template <typename T, int initial_size>
|
|
|
|
class BufferedZoneList {
|
|
|
|
public:
|
|
|
|
BufferedZoneList() : list_(NULL), last_(NULL) {}
|
|
|
|
|
|
|
|
// Adds element at end of list. This element is buffered and can
|
|
|
|
// be read using last() or removed using RemoveLast until a new Add or until
|
|
|
|
// RemoveLast or GetList has been called.
|
2012-06-11 12:42:31 +00:00
|
|
|
void Add(T* value, Zone* zone) {
|
2010-10-27 12:33:48 +00:00
|
|
|
if (last_ != NULL) {
|
|
|
|
if (list_ == NULL) {
|
2012-06-11 12:42:31 +00:00
|
|
|
list_ = new(zone) ZoneList<T*>(initial_size, zone);
|
2010-10-27 12:33:48 +00:00
|
|
|
}
|
2012-06-11 12:42:31 +00:00
|
|
|
list_->Add(last_, zone);
|
2010-10-27 12:33:48 +00:00
|
|
|
}
|
|
|
|
last_ = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
T* last() {
|
|
|
|
ASSERT(last_ != NULL);
|
|
|
|
return last_;
|
|
|
|
}
|
|
|
|
|
|
|
|
T* RemoveLast() {
|
|
|
|
ASSERT(last_ != NULL);
|
|
|
|
T* result = last_;
|
|
|
|
if ((list_ != NULL) && (list_->length() > 0))
|
|
|
|
last_ = list_->RemoveLast();
|
|
|
|
else
|
|
|
|
last_ = NULL;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
T* Get(int i) {
|
|
|
|
ASSERT((0 <= i) && (i < length()));
|
|
|
|
if (list_ == NULL) {
|
|
|
|
ASSERT_EQ(0, i);
|
|
|
|
return last_;
|
|
|
|
} else {
|
|
|
|
if (i == list_->length()) {
|
|
|
|
ASSERT(last_ != NULL);
|
|
|
|
return last_;
|
|
|
|
} else {
|
|
|
|
return list_->at(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Clear() {
|
|
|
|
list_ = NULL;
|
|
|
|
last_ = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int length() {
|
|
|
|
int length = (list_ == NULL) ? 0 : list_->length();
|
|
|
|
return length + ((last_ == NULL) ? 0 : 1);
|
|
|
|
}
|
|
|
|
|
2012-06-11 12:42:31 +00:00
|
|
|
ZoneList<T*>* GetList(Zone* zone) {
|
2010-10-27 12:33:48 +00:00
|
|
|
if (list_ == NULL) {
|
2012-06-11 12:42:31 +00:00
|
|
|
list_ = new(zone) ZoneList<T*>(initial_size, zone);
|
2010-10-27 12:33:48 +00:00
|
|
|
}
|
|
|
|
if (last_ != NULL) {
|
2012-06-11 12:42:31 +00:00
|
|
|
list_->Add(last_, zone);
|
2010-10-27 12:33:48 +00:00
|
|
|
last_ = NULL;
|
|
|
|
}
|
|
|
|
return list_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ZoneList<T*>* list_;
|
|
|
|
T* last_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Accumulates RegExp atoms and assertions into lists of terms and alternatives.
|
|
|
|
class RegExpBuilder: public ZoneObject {
|
|
|
|
public:
|
2012-06-04 14:42:58 +00:00
|
|
|
explicit RegExpBuilder(Zone* zone);
|
2010-10-27 12:33:48 +00:00
|
|
|
void AddCharacter(uc16 character);
|
|
|
|
// "Adds" an empty expression. Does nothing except consume a
|
|
|
|
// following quantifier
|
|
|
|
void AddEmpty();
|
|
|
|
void AddAtom(RegExpTree* tree);
|
|
|
|
void AddAssertion(RegExpTree* tree);
|
|
|
|
void NewAlternative(); // '|'
|
2013-06-06 13:28:22 +00:00
|
|
|
void AddQuantifierToAtom(
|
|
|
|
int min, int max, RegExpQuantifier::QuantifierType type);
|
2010-10-27 12:33:48 +00:00
|
|
|
RegExpTree* ToRegExp();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void FlushCharacters();
|
|
|
|
void FlushText();
|
|
|
|
void FlushTerms();
|
2012-06-11 12:42:31 +00:00
|
|
|
Zone* zone() const { return zone_; }
|
2011-04-04 06:29:02 +00:00
|
|
|
|
|
|
|
Zone* zone_;
|
2010-10-27 12:33:48 +00:00
|
|
|
bool pending_empty_;
|
|
|
|
ZoneList<uc16>* characters_;
|
|
|
|
BufferedZoneList<RegExpTree, 2> terms_;
|
|
|
|
BufferedZoneList<RegExpTree, 2> text_;
|
|
|
|
BufferedZoneList<RegExpTree, 2> alternatives_;
|
|
|
|
#ifdef DEBUG
|
|
|
|
enum {ADD_NONE, ADD_CHAR, ADD_TERM, ADD_ASSERT, ADD_ATOM} last_added_;
|
|
|
|
#define LAST(x) last_added_ = x;
|
|
|
|
#else
|
|
|
|
#define LAST(x)
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-04-08 11:53:50 +00:00
|
|
|
class RegExpParser BASE_EMBEDDED {
|
2010-10-27 12:33:48 +00:00
|
|
|
public:
|
|
|
|
RegExpParser(FlatStringReader* in,
|
|
|
|
Handle<String>* error,
|
2012-06-20 08:58:41 +00:00
|
|
|
bool multiline_mode,
|
|
|
|
Zone* zone);
|
2010-10-01 14:10:47 +00:00
|
|
|
|
|
|
|
static bool ParseRegExp(FlatStringReader* input,
|
|
|
|
bool multiline,
|
2012-06-20 08:58:41 +00:00
|
|
|
RegExpCompileData* result,
|
|
|
|
Zone* zone);
|
2010-10-01 14:10:47 +00:00
|
|
|
|
2010-10-27 12:33:48 +00:00
|
|
|
RegExpTree* ParsePattern();
|
|
|
|
RegExpTree* ParseDisjunction();
|
|
|
|
RegExpTree* ParseGroup();
|
|
|
|
RegExpTree* ParseCharacterClass();
|
|
|
|
|
|
|
|
// Parses a {...,...} quantifier and stores the range in the given
|
|
|
|
// out parameters.
|
|
|
|
bool ParseIntervalQuantifier(int* min_out, int* max_out);
|
|
|
|
|
|
|
|
// Parses and returns a single escaped character. The character
|
|
|
|
// must not be 'b' or 'B' since they are usually handle specially.
|
|
|
|
uc32 ParseClassCharacterEscape();
|
|
|
|
|
|
|
|
// Checks whether the following is a length-digit hexadecimal number,
|
|
|
|
// and sets the value if it is.
|
|
|
|
bool ParseHexEscape(int length, uc32* value);
|
|
|
|
|
|
|
|
uc32 ParseOctalLiteral();
|
|
|
|
|
|
|
|
// Tries to parse the input as a back reference. If successful it
|
|
|
|
// stores the result in the output parameter and returns true. If
|
|
|
|
// it fails it will push back the characters read so the same characters
|
|
|
|
// can be reparsed.
|
|
|
|
bool ParseBackReferenceIndex(int* index_out);
|
|
|
|
|
|
|
|
CharacterRange ParseClassAtom(uc16* char_class);
|
|
|
|
RegExpTree* ReportError(Vector<const char> message);
|
|
|
|
void Advance();
|
|
|
|
void Advance(int dist);
|
|
|
|
void Reset(int pos);
|
|
|
|
|
|
|
|
// Reports whether the pattern might be used as a literal search string.
|
|
|
|
// Only use if the result of the parse is a single atom node.
|
|
|
|
bool simple();
|
|
|
|
bool contains_anchor() { return contains_anchor_; }
|
|
|
|
void set_contains_anchor() { contains_anchor_ = true; }
|
|
|
|
int captures_started() { return captures_ == NULL ? 0 : captures_->length(); }
|
|
|
|
int position() { return next_pos_ - 1; }
|
|
|
|
bool failed() { return failed_; }
|
|
|
|
|
|
|
|
static const int kMaxCaptures = 1 << 16;
|
|
|
|
static const uc32 kEndMarker = (1 << 21);
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum SubexpressionType {
|
|
|
|
INITIAL,
|
|
|
|
CAPTURE, // All positive values represent captures.
|
|
|
|
POSITIVE_LOOKAHEAD,
|
|
|
|
NEGATIVE_LOOKAHEAD,
|
|
|
|
GROUPING
|
|
|
|
};
|
|
|
|
|
|
|
|
class RegExpParserState : public ZoneObject {
|
|
|
|
public:
|
|
|
|
RegExpParserState(RegExpParserState* previous_state,
|
|
|
|
SubexpressionType group_type,
|
2012-06-04 14:42:58 +00:00
|
|
|
int disjunction_capture_index,
|
|
|
|
Zone* zone)
|
2010-10-27 12:33:48 +00:00
|
|
|
: previous_state_(previous_state),
|
2012-06-11 12:42:31 +00:00
|
|
|
builder_(new(zone) RegExpBuilder(zone)),
|
2010-10-27 12:33:48 +00:00
|
|
|
group_type_(group_type),
|
|
|
|
disjunction_capture_index_(disjunction_capture_index) {}
|
|
|
|
// Parser state of containing expression, if any.
|
|
|
|
RegExpParserState* previous_state() { return previous_state_; }
|
|
|
|
bool IsSubexpression() { return previous_state_ != NULL; }
|
|
|
|
// RegExpBuilder building this regexp's AST.
|
|
|
|
RegExpBuilder* builder() { return builder_; }
|
|
|
|
// Type of regexp being parsed (parenthesized group or entire regexp).
|
|
|
|
SubexpressionType group_type() { return group_type_; }
|
|
|
|
// Index in captures array of first capture in this sub-expression, if any.
|
|
|
|
// Also the capture index of this sub-expression itself, if group_type
|
|
|
|
// is CAPTURE.
|
|
|
|
int capture_index() { return disjunction_capture_index_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Linked list implementation of stack of states.
|
|
|
|
RegExpParserState* previous_state_;
|
|
|
|
// Builder for the stored disjunction.
|
|
|
|
RegExpBuilder* builder_;
|
|
|
|
// Stored disjunction type (capture, look-ahead or grouping), if any.
|
|
|
|
SubexpressionType group_type_;
|
|
|
|
// Stored disjunction's capture index (if any).
|
|
|
|
int disjunction_capture_index_;
|
|
|
|
};
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
Isolate* isolate() { return isolate_; }
|
2012-06-20 08:58:41 +00:00
|
|
|
Zone* zone() const { return zone_; }
|
2011-03-18 20:35:07 +00:00
|
|
|
|
2010-10-27 12:33:48 +00:00
|
|
|
uc32 current() { return current_; }
|
|
|
|
bool has_more() { return has_more_; }
|
|
|
|
bool has_next() { return next_pos_ < in()->length(); }
|
|
|
|
uc32 Next();
|
|
|
|
FlatStringReader* in() { return in_; }
|
|
|
|
void ScanForCaptures();
|
2010-11-02 11:45:47 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
Isolate* isolate_;
|
2012-06-20 08:58:41 +00:00
|
|
|
Zone* zone_;
|
2010-11-02 11:45:47 +00:00
|
|
|
Handle<String>* error_;
|
|
|
|
ZoneList<RegExpCapture*>* captures_;
|
|
|
|
FlatStringReader* in_;
|
2010-10-27 12:33:48 +00:00
|
|
|
uc32 current_;
|
2010-11-02 11:45:47 +00:00
|
|
|
int next_pos_;
|
|
|
|
// The capture count is only valid after we have scanned for captures.
|
|
|
|
int capture_count_;
|
2010-10-27 12:33:48 +00:00
|
|
|
bool has_more_;
|
|
|
|
bool multiline_;
|
|
|
|
bool simple_;
|
|
|
|
bool contains_anchor_;
|
|
|
|
bool is_scanned_for_captures_;
|
|
|
|
bool failed_;
|
|
|
|
};
|
|
|
|
|
2010-11-02 11:45:47 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// JAVASCRIPT PARSING
|
2010-10-27 12:33:48 +00:00
|
|
|
|
2014-02-11 09:35:32 +00:00
|
|
|
class Parser;
|
2011-11-25 09:36:31 +00:00
|
|
|
class SingletonLogger;
|
|
|
|
|
2014-02-11 09:35:32 +00:00
|
|
|
class ParserTraits {
|
|
|
|
public:
|
|
|
|
typedef Parser* ParserType;
|
|
|
|
// Return types for traversing functions.
|
|
|
|
typedef Handle<String> IdentifierType;
|
2014-02-11 11:51:01 +00:00
|
|
|
typedef Expression* ExpressionType;
|
2014-02-11 09:35:32 +00:00
|
|
|
|
|
|
|
explicit ParserTraits(Parser* parser) : parser_(parser) {}
|
|
|
|
|
|
|
|
// Helper functions for recursive descent.
|
|
|
|
bool is_classic_mode() const;
|
|
|
|
bool is_generator() const;
|
|
|
|
bool IsEvalOrArguments(Handle<String> identifier) const;
|
2014-02-11 11:51:01 +00:00
|
|
|
int NextMaterializedLiteralIndex();
|
2014-02-11 09:35:32 +00:00
|
|
|
|
|
|
|
// Reporting errors.
|
|
|
|
void ReportMessageAt(Scanner::Location source_location,
|
|
|
|
const char* message,
|
|
|
|
Vector<const char*> args);
|
|
|
|
void ReportMessage(const char* message, Vector<Handle<String> > args);
|
|
|
|
void ReportMessageAt(Scanner::Location source_location,
|
|
|
|
const char* message,
|
|
|
|
Vector<Handle<String> > args);
|
|
|
|
|
2014-02-11 11:51:01 +00:00
|
|
|
// "null" return type creators.
|
2014-02-11 09:35:32 +00:00
|
|
|
static IdentifierType EmptyIdentifier() {
|
|
|
|
return Handle<String>();
|
|
|
|
}
|
2014-02-11 11:51:01 +00:00
|
|
|
static ExpressionType EmptyExpression() {
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-02-11 09:35:32 +00:00
|
|
|
|
2014-02-11 11:51:01 +00:00
|
|
|
// Producing data during the recursive descent.
|
2014-02-11 09:35:32 +00:00
|
|
|
IdentifierType GetSymbol();
|
2014-02-11 11:51:01 +00:00
|
|
|
IdentifierType NextLiteralString(PretenureFlag tenured);
|
|
|
|
ExpressionType NewRegExpLiteral(IdentifierType js_pattern,
|
|
|
|
IdentifierType js_flags,
|
|
|
|
int literal_index,
|
|
|
|
int pos);
|
2014-02-11 09:35:32 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Parser* parser_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Parser : public ParserBase<ParserTraits> {
|
2010-10-27 12:33:48 +00:00
|
|
|
public:
|
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
|
|
|
explicit Parser(CompilationInfo* info);
|
2013-04-08 11:53:50 +00:00
|
|
|
~Parser() {
|
2012-02-08 09:56:33 +00:00
|
|
|
delete reusable_preparser_;
|
|
|
|
reusable_preparser_ = NULL;
|
2011-11-25 09:36:31 +00:00
|
|
|
}
|
2010-10-27 12:33:48 +00:00
|
|
|
|
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
|
|
|
// Parses the source code represented by the compilation info and sets its
|
|
|
|
// function literal. Returns false (and deallocates any allocated AST
|
|
|
|
// nodes) if parsing failed.
|
2013-12-23 14:30:35 +00:00
|
|
|
static bool Parse(CompilationInfo* info,
|
|
|
|
bool allow_lazy = false) {
|
|
|
|
Parser parser(info);
|
|
|
|
parser.set_allow_lazy(allow_lazy);
|
|
|
|
return parser.Parse();
|
|
|
|
}
|
Refactor parser mode configuration for correctness
This patch refactors the parser and preparser interface to be more
readable and type-safe. It has no behavior changes.
Previously, parsers and preparsers were configured via bitfield called
parser_flags in the Parser constructor, and flags in
PreParser::PreParseProgram, ParserApi::Parse, and ParserApi::PreParse.
This was error-prone in practice: six call sites passed incorrectly
typed values to this interface (a boolean FLAG value, a boolean false
and a boolean true value). None of these errors were caught by the
compiler because it's just an "int".
The parser flags interface was also awkward because it encoded a
language mode, but the language mode was only used to turn on harmony
scoping or not -- it wasn't used to actually set the parser's language
mode.
Fundamentally these errors came in because of the desire for a
procedural parser interface, in ParserApi. Because we need to be able
to configure the parser in various ways, the flags argument got added;
but no one understood how to use the flags properly. Also they were
only used by constructors: callers packed bits, and the constructors
unpacked them into booleans on the parser or preparser.
The solution is to allow parser construction, configuration, and
invocation to be separated. This patch does that.
It passes the existing tests.
BUG=
Review URL: https://codereview.chromium.org/13450007
Patch from Andy Wingo <wingo@igalia.com>.
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@14151 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2013-04-05 13:01:06 +00:00
|
|
|
bool Parse();
|
|
|
|
|
2011-06-30 14:37:55 +00:00
|
|
|
private:
|
2014-02-11 09:35:32 +00:00
|
|
|
friend class ParserTraits;
|
|
|
|
|
2012-10-11 11:40:10 +00:00
|
|
|
static const int kMaxNumFunctionLocals = 131071; // 2^17-1
|
2011-11-09 13:54:26 +00:00
|
|
|
|
2010-10-01 14:10:47 +00:00
|
|
|
enum Mode {
|
|
|
|
PARSE_LAZILY,
|
|
|
|
PARSE_EAGERLY
|
|
|
|
};
|
2008-11-25 11:07:48 +00:00
|
|
|
|
2011-08-16 14:24:12 +00:00
|
|
|
enum VariableDeclarationContext {
|
2012-02-20 14:02:59 +00:00
|
|
|
kModuleElement,
|
|
|
|
kBlockElement,
|
2011-08-16 14:24:12 +00:00
|
|
|
kStatement,
|
|
|
|
kForStatement
|
|
|
|
};
|
|
|
|
|
2011-10-17 12:19:06 +00:00
|
|
|
// If a list of variable declarations includes any initializers.
|
|
|
|
enum VariableDeclarationProperties {
|
|
|
|
kHasInitializers,
|
|
|
|
kHasNoInitializers
|
|
|
|
};
|
|
|
|
|
2011-11-09 13:54:26 +00:00
|
|
|
class BlockState;
|
2012-02-08 09:56:33 +00:00
|
|
|
|
|
|
|
class FunctionState BASE_EMBEDDED {
|
|
|
|
public:
|
2014-01-21 16:22:52 +00:00
|
|
|
FunctionState(Parser* parser, Scope* scope);
|
2012-02-08 09:56:33 +00:00
|
|
|
~FunctionState();
|
|
|
|
|
|
|
|
int NextMaterializedLiteralIndex() {
|
|
|
|
return next_materialized_literal_index_++;
|
|
|
|
}
|
|
|
|
int materialized_literal_count() {
|
|
|
|
return next_materialized_literal_index_ - JSFunction::kLiteralsPrefixSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
int NextHandlerIndex() { return next_handler_index_++; }
|
|
|
|
int handler_count() { return next_handler_index_; }
|
|
|
|
|
|
|
|
void AddProperty() { expected_property_count_++; }
|
|
|
|
int expected_property_count() { return expected_property_count_; }
|
|
|
|
|
2013-04-15 12:29:44 +00:00
|
|
|
void set_generator_object_variable(Variable *variable) {
|
|
|
|
ASSERT(variable != NULL);
|
|
|
|
ASSERT(!is_generator());
|
|
|
|
generator_object_variable_ = variable;
|
|
|
|
}
|
|
|
|
Variable* generator_object_variable() const {
|
|
|
|
return generator_object_variable_;
|
|
|
|
}
|
|
|
|
bool is_generator() const {
|
|
|
|
return generator_object_variable_ != NULL;
|
|
|
|
}
|
2013-04-02 17:34:59 +00:00
|
|
|
|
2012-02-08 09:56:33 +00:00
|
|
|
AstNodeFactory<AstConstructionVisitor>* factory() { return &factory_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Used to assign an index to each literal that needs materialization in
|
|
|
|
// the function. Includes regexp literals, and boilerplate for object and
|
|
|
|
// array literals.
|
|
|
|
int next_materialized_literal_index_;
|
|
|
|
|
|
|
|
// Used to assign a per-function index to try and catch handlers.
|
|
|
|
int next_handler_index_;
|
|
|
|
|
|
|
|
// Properties count estimation.
|
|
|
|
int expected_property_count_;
|
|
|
|
|
2013-04-15 12:29:44 +00:00
|
|
|
// For generators, the variable that holds the generator object. This
|
|
|
|
// variable is used by yield expressions and return statements. NULL
|
|
|
|
// indicates that this function is not a generator.
|
|
|
|
Variable* generator_object_variable_;
|
|
|
|
|
2012-02-08 09:56:33 +00:00
|
|
|
Parser* parser_;
|
|
|
|
FunctionState* outer_function_state_;
|
|
|
|
Scope* outer_scope_;
|
|
|
|
int saved_ast_node_id_;
|
|
|
|
AstNodeFactory<AstConstructionVisitor> factory_;
|
|
|
|
};
|
|
|
|
|
2012-08-07 14:47:36 +00:00
|
|
|
class ParsingModeScope BASE_EMBEDDED {
|
|
|
|
public:
|
|
|
|
ParsingModeScope(Parser* parser, Mode mode)
|
|
|
|
: parser_(parser),
|
|
|
|
old_mode_(parser->mode()) {
|
|
|
|
parser_->mode_ = mode;
|
|
|
|
}
|
|
|
|
~ParsingModeScope() {
|
|
|
|
parser_->mode_ = old_mode_;
|
|
|
|
}
|
2012-02-08 09:56:33 +00:00
|
|
|
|
2012-08-07 14:47:36 +00:00
|
|
|
private:
|
|
|
|
Parser* parser_;
|
|
|
|
Mode old_mode_;
|
|
|
|
};
|
2011-11-09 13:54:26 +00:00
|
|
|
|
2013-10-01 09:27:03 +00:00
|
|
|
// Returns NULL if parsing failed.
|
|
|
|
FunctionLiteral* ParseProgram();
|
|
|
|
|
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
|
|
|
FunctionLiteral* ParseLazy();
|
2013-06-26 08:05:41 +00:00
|
|
|
FunctionLiteral* ParseLazy(Utf16CharacterStream* source);
|
2011-11-09 13:54:26 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
Isolate* isolate() { return isolate_; }
|
2012-06-11 12:42:31 +00:00
|
|
|
Zone* zone() const { return zone_; }
|
2012-06-20 10:56:53 +00:00
|
|
|
CompilationInfo* info() const { return info_; }
|
2011-03-18 20:35:07 +00:00
|
|
|
|
2010-12-07 14:03:59 +00:00
|
|
|
// Called by ParseProgram after setting up the scanner.
|
2011-11-15 13:48:40 +00:00
|
|
|
FunctionLiteral* DoParseProgram(CompilationInfo* info,
|
2013-06-26 08:05:41 +00:00
|
|
|
Handle<String> source);
|
2010-12-07 14:03:59 +00:00
|
|
|
|
2010-10-01 14:10:47 +00:00
|
|
|
// Report syntax error
|
|
|
|
void ReportInvalidPreparseData(Handle<String> name, bool* ok);
|
|
|
|
|
Refactor parser mode configuration for correctness
This patch refactors the parser and preparser interface to be more
readable and type-safe. It has no behavior changes.
Previously, parsers and preparsers were configured via bitfield called
parser_flags in the Parser constructor, and flags in
PreParser::PreParseProgram, ParserApi::Parse, and ParserApi::PreParse.
This was error-prone in practice: six call sites passed incorrectly
typed values to this interface (a boolean FLAG value, a boolean false
and a boolean true value). None of these errors were caught by the
compiler because it's just an "int".
The parser flags interface was also awkward because it encoded a
language mode, but the language mode was only used to turn on harmony
scoping or not -- it wasn't used to actually set the parser's language
mode.
Fundamentally these errors came in because of the desire for a
procedural parser interface, in ParserApi. Because we need to be able
to configure the parser in various ways, the flags argument got added;
but no one understood how to use the flags properly. Also they were
only used by constructors: callers packed bits, and the constructors
unpacked them into booleans on the parser or preparser.
The solution is to allow parser construction, configuration, and
invocation to be separated. This patch does that.
It passes the existing tests.
BUG=
Review URL: https://codereview.chromium.org/13450007
Patch from Andy Wingo <wingo@igalia.com>.
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@14151 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2013-04-05 13:01:06 +00:00
|
|
|
void set_pre_parse_data(ScriptDataImpl *data) {
|
|
|
|
pre_parse_data_ = data;
|
|
|
|
symbol_cache_.Initialize(data ? data->symbol_count() : 0, zone());
|
|
|
|
}
|
|
|
|
|
2011-10-17 09:29:37 +00:00
|
|
|
bool inside_with() const { return top_scope_->inside_with(); }
|
2011-11-01 07:47:15 +00:00
|
|
|
Scanner& scanner() { return scanner_; }
|
2010-10-01 14:10:47 +00:00
|
|
|
Mode mode() const { return mode_; }
|
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
|
|
|
ScriptDataImpl* pre_parse_data() const { return pre_parse_data_; }
|
2011-11-24 15:58:09 +00:00
|
|
|
bool is_extended_mode() {
|
|
|
|
ASSERT(top_scope_ != NULL);
|
|
|
|
return top_scope_->is_extended_mode();
|
|
|
|
}
|
2012-02-28 10:12:39 +00:00
|
|
|
Scope* DeclarationScope(VariableMode mode) {
|
2012-08-29 09:19:53 +00:00
|
|
|
return IsLexicalVariableMode(mode)
|
2012-02-28 10:12:39 +00:00
|
|
|
? top_scope_ : top_scope_->DeclarationScope();
|
|
|
|
}
|
2010-10-01 14:10:47 +00:00
|
|
|
|
|
|
|
// All ParseXXX functions take as the last argument an *ok parameter
|
|
|
|
// which is set to false if parsing failed; it is unchanged otherwise.
|
|
|
|
// By making the 'exception handling' explicit, we are forced to check
|
|
|
|
// for failure at the call sites.
|
2012-10-05 09:14:08 +00:00
|
|
|
void* ParseSourceElements(ZoneList<Statement*>* processor, int end_token,
|
|
|
|
bool is_eval, bool is_global, bool* ok);
|
2012-02-20 14:02:59 +00:00
|
|
|
Statement* ParseModuleElement(ZoneStringList* labels, bool* ok);
|
2012-07-09 08:59:03 +00:00
|
|
|
Statement* ParseModuleDeclaration(ZoneStringList* names, bool* ok);
|
2012-02-20 14:02:59 +00:00
|
|
|
Module* ParseModule(bool* ok);
|
|
|
|
Module* ParseModuleLiteral(bool* ok);
|
|
|
|
Module* ParseModulePath(bool* ok);
|
|
|
|
Module* ParseModuleVariable(bool* ok);
|
|
|
|
Module* ParseModuleUrl(bool* ok);
|
2012-02-29 12:12:52 +00:00
|
|
|
Module* ParseModuleSpecifier(bool* ok);
|
2012-02-20 14:02:59 +00:00
|
|
|
Block* ParseImportDeclaration(bool* ok);
|
2012-02-29 12:12:52 +00:00
|
|
|
Statement* ParseExportDeclaration(bool* ok);
|
2012-02-20 14:02:59 +00:00
|
|
|
Statement* ParseBlockElement(ZoneStringList* labels, bool* ok);
|
2010-10-01 14:10:47 +00:00
|
|
|
Statement* ParseStatement(ZoneStringList* labels, bool* ok);
|
2012-02-29 12:12:52 +00:00
|
|
|
Statement* ParseFunctionDeclaration(ZoneStringList* names, bool* ok);
|
2010-10-01 14:10:47 +00:00
|
|
|
Statement* ParseNativeDeclaration(bool* ok);
|
|
|
|
Block* ParseBlock(ZoneStringList* labels, bool* ok);
|
2011-08-16 14:24:12 +00:00
|
|
|
Block* ParseVariableStatement(VariableDeclarationContext var_context,
|
2012-02-29 12:12:52 +00:00
|
|
|
ZoneStringList* names,
|
2011-08-16 14:24:12 +00:00
|
|
|
bool* ok);
|
|
|
|
Block* ParseVariableDeclarations(VariableDeclarationContext var_context,
|
2011-10-17 12:19:06 +00:00
|
|
|
VariableDeclarationProperties* decl_props,
|
2012-02-29 12:12:52 +00:00
|
|
|
ZoneStringList* names,
|
2011-06-30 14:37:55 +00:00
|
|
|
Handle<String>* out,
|
|
|
|
bool* ok);
|
2010-10-01 14:10:47 +00:00
|
|
|
Statement* ParseExpressionOrLabelledStatement(ZoneStringList* labels,
|
|
|
|
bool* ok);
|
|
|
|
IfStatement* ParseIfStatement(ZoneStringList* labels, bool* ok);
|
|
|
|
Statement* ParseContinueStatement(bool* ok);
|
|
|
|
Statement* ParseBreakStatement(ZoneStringList* labels, bool* ok);
|
|
|
|
Statement* ParseReturnStatement(bool* ok);
|
|
|
|
Statement* ParseWithStatement(ZoneStringList* labels, bool* ok);
|
|
|
|
CaseClause* ParseCaseClause(bool* default_seen_ptr, bool* ok);
|
|
|
|
SwitchStatement* ParseSwitchStatement(ZoneStringList* labels, bool* ok);
|
|
|
|
DoWhileStatement* ParseDoWhileStatement(ZoneStringList* labels, bool* ok);
|
|
|
|
WhileStatement* ParseWhileStatement(ZoneStringList* labels, bool* ok);
|
|
|
|
Statement* ParseForStatement(ZoneStringList* labels, bool* ok);
|
|
|
|
Statement* ParseThrowStatement(bool* ok);
|
|
|
|
Expression* MakeCatchContext(Handle<String> id, VariableProxy* value);
|
|
|
|
TryStatement* ParseTryStatement(bool* ok);
|
|
|
|
DebuggerStatement* ParseDebuggerStatement(bool* ok);
|
2011-10-25 08:33:08 +00:00
|
|
|
|
|
|
|
// Support for hamony block scoped bindings.
|
|
|
|
Block* ParseScopedBlock(ZoneStringList* labels, bool* ok);
|
2010-10-01 14:10:47 +00:00
|
|
|
|
|
|
|
Expression* ParseExpression(bool accept_IN, bool* ok);
|
|
|
|
Expression* ParseAssignmentExpression(bool accept_IN, bool* ok);
|
2013-04-02 17:34:59 +00:00
|
|
|
Expression* ParseYieldExpression(bool* ok);
|
2010-10-01 14:10:47 +00:00
|
|
|
Expression* ParseConditionalExpression(bool accept_IN, bool* ok);
|
|
|
|
Expression* ParseBinaryExpression(int prec, bool accept_IN, bool* ok);
|
|
|
|
Expression* ParseUnaryExpression(bool* ok);
|
|
|
|
Expression* ParsePostfixExpression(bool* ok);
|
|
|
|
Expression* ParseLeftHandSideExpression(bool* ok);
|
|
|
|
Expression* ParseNewExpression(bool* ok);
|
|
|
|
Expression* ParseMemberExpression(bool* ok);
|
|
|
|
Expression* ParseNewPrefix(PositionStack* stack, bool* ok);
|
|
|
|
Expression* ParseMemberWithNewPrefixesExpression(PositionStack* stack,
|
|
|
|
bool* ok);
|
|
|
|
Expression* ParsePrimaryExpression(bool* ok);
|
|
|
|
Expression* ParseArrayLiteral(bool* ok);
|
|
|
|
Expression* ParseObjectLiteral(bool* ok);
|
|
|
|
|
2013-06-07 11:12:21 +00:00
|
|
|
// Initialize the components of a for-in / for-of statement.
|
|
|
|
void InitializeForEachStatement(ForEachStatement* stmt,
|
|
|
|
Expression* each,
|
|
|
|
Expression* subject,
|
|
|
|
Statement* body);
|
|
|
|
|
2010-10-01 14:10:47 +00:00
|
|
|
ZoneList<Expression*>* ParseArguments(bool* ok);
|
2014-02-04 11:26:19 +00:00
|
|
|
FunctionLiteral* ParseFunctionLiteral(
|
2014-02-07 10:47:01 +00:00
|
|
|
Handle<String> name,
|
2014-02-04 11:26:19 +00:00
|
|
|
Scanner::Location function_name_location,
|
2014-02-07 10:47:01 +00:00
|
|
|
bool name_is_strict_reserved,
|
2014-02-04 11:26:19 +00:00
|
|
|
bool is_generator,
|
|
|
|
int function_token_position,
|
|
|
|
FunctionLiteral::FunctionType type,
|
|
|
|
bool* ok);
|
2010-10-01 14:10:47 +00:00
|
|
|
|
|
|
|
// Magical syntax support.
|
|
|
|
Expression* ParseV8Intrinsic(bool* ok);
|
|
|
|
|
2013-06-12 12:37:44 +00:00
|
|
|
bool CheckInOrOf(bool accept_OF, ForEachStatement::VisitMode* visit_mode);
|
2013-06-06 14:38:26 +00:00
|
|
|
|
2010-12-22 20:14:19 +00:00
|
|
|
Handle<String> LiteralString(PretenureFlag tenured) {
|
|
|
|
if (scanner().is_literal_ascii()) {
|
2011-03-18 20:35:07 +00:00
|
|
|
return isolate_->factory()->NewStringFromAscii(
|
|
|
|
scanner().literal_ascii_string(), tenured);
|
2010-12-22 20:14:19 +00:00
|
|
|
} else {
|
2011-03-18 20:35:07 +00:00
|
|
|
return isolate_->factory()->NewStringFromTwoByte(
|
2012-03-12 12:35:28 +00:00
|
|
|
scanner().literal_utf16_string(), tenured);
|
2010-12-22 20:14:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-01 14:10:47 +00:00
|
|
|
// Get odd-ball literals.
|
2013-10-14 09:24:58 +00:00
|
|
|
Literal* GetLiteralUndefined(int position);
|
|
|
|
Literal* GetLiteralTheHole(int position);
|
2010-10-01 14:10:47 +00:00
|
|
|
|
2011-12-05 14:43:28 +00:00
|
|
|
// Determine if the expression is a variable proxy and mark it as being used
|
|
|
|
// in an assignment or with a increment/decrement operator. This is currently
|
|
|
|
// used on for the statically checking assignments to harmony const bindings.
|
|
|
|
void MarkAsLValue(Expression* expression);
|
|
|
|
|
2011-01-26 19:21:46 +00:00
|
|
|
// Strict mode validation of LValue expressions
|
|
|
|
void CheckStrictModeLValue(Expression* expression,
|
|
|
|
bool* ok);
|
|
|
|
|
2011-09-01 12:31:18 +00:00
|
|
|
// For harmony block scoping mode: Check if the scope has conflicting var/let
|
|
|
|
// declarations from different scopes. It covers for example
|
|
|
|
//
|
|
|
|
// function f() { { { var x; } let x; } }
|
|
|
|
// function g() { { var x; let x; } }
|
|
|
|
//
|
|
|
|
// The var declarations are hoisted to the function scope, but originate from
|
|
|
|
// a scope where the name has also been let bound or the var declaration is
|
|
|
|
// hoisted over such a scope.
|
|
|
|
void CheckConflictingVarDeclarations(Scope* scope, bool* ok);
|
|
|
|
|
2010-10-01 14:10:47 +00:00
|
|
|
// Parser support
|
2012-03-08 13:03:07 +00:00
|
|
|
VariableProxy* NewUnresolved(Handle<String> name,
|
|
|
|
VariableMode mode,
|
2012-07-13 09:29:43 +00:00
|
|
|
Interface* interface);
|
2012-02-28 10:12:39 +00:00
|
|
|
void Declare(Declaration* declaration, bool resolve, bool* ok);
|
2010-10-01 14:10:47 +00:00
|
|
|
|
|
|
|
bool TargetStackContainsLabel(Handle<String> label);
|
|
|
|
BreakableStatement* LookupBreakTarget(Handle<String> label, bool* ok);
|
|
|
|
IterationStatement* LookupContinueTarget(Handle<String> label, bool* ok);
|
|
|
|
|
2011-04-07 14:42:37 +00:00
|
|
|
void RegisterTargetUse(Label* target, Target* stop);
|
2010-10-01 14:10:47 +00:00
|
|
|
|
2010-11-02 11:45:47 +00:00
|
|
|
// Factory methods.
|
|
|
|
|
2011-10-21 10:26:59 +00:00
|
|
|
Scope* NewScope(Scope* parent, ScopeType type);
|
2010-11-02 11:45:47 +00:00
|
|
|
|
2010-12-22 20:14:19 +00:00
|
|
|
Handle<String> LookupSymbol(int symbol_id);
|
2010-11-02 11:45:47 +00:00
|
|
|
|
2010-12-22 20:14:19 +00:00
|
|
|
Handle<String> LookupCachedSymbol(int symbol_id);
|
2010-11-02 11:45:47 +00:00
|
|
|
|
2010-10-01 14:10:47 +00:00
|
|
|
// Generate AST node that throw a ReferenceError with the given type.
|
|
|
|
Expression* NewThrowReferenceError(Handle<String> type);
|
|
|
|
|
|
|
|
// Generate AST node that throw a SyntaxError with the given
|
|
|
|
// type. The first argument may be null (in the handle sense) in
|
|
|
|
// which case no arguments are passed to the constructor.
|
|
|
|
Expression* NewThrowSyntaxError(Handle<String> type, Handle<Object> first);
|
|
|
|
|
|
|
|
// Generate AST node that throw a TypeError with the given
|
|
|
|
// type. Both arguments must be non-null (in the handle sense).
|
|
|
|
Expression* NewThrowTypeError(Handle<String> type,
|
|
|
|
Handle<Object> first,
|
|
|
|
Handle<Object> second);
|
|
|
|
|
|
|
|
// Generic AST generator for throwing errors from compiled code.
|
|
|
|
Expression* NewThrowError(Handle<String> constructor,
|
|
|
|
Handle<String> type,
|
|
|
|
Vector< Handle<Object> > arguments);
|
2010-11-02 11:45:47 +00:00
|
|
|
|
2013-10-14 13:07:20 +00:00
|
|
|
PreParser::PreParseResult LazyParseFunctionLiteral(
|
2011-11-25 09:36:31 +00:00
|
|
|
SingletonLogger* logger);
|
|
|
|
|
2012-02-08 09:56:33 +00:00
|
|
|
AstNodeFactory<AstConstructionVisitor>* factory() {
|
|
|
|
return current_function_state_->factory();
|
|
|
|
}
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
Isolate* isolate_;
|
2010-11-02 11:45:47 +00:00
|
|
|
ZoneList<Handle<String> > symbol_cache_;
|
|
|
|
|
|
|
|
Handle<Script> script_;
|
2011-11-01 07:47:15 +00:00
|
|
|
Scanner scanner_;
|
2013-10-14 13:07:20 +00:00
|
|
|
PreParser* reusable_preparser_;
|
2010-11-02 11:45:47 +00:00
|
|
|
Scope* top_scope_;
|
2013-08-23 09:25:37 +00:00
|
|
|
Scope* original_scope_; // for ES5 function declarations in sloppy eval
|
2011-11-09 13:54:26 +00:00
|
|
|
FunctionState* current_function_state_;
|
2010-11-02 11:45:47 +00:00
|
|
|
Target* target_stack_; // for break, continue statements
|
|
|
|
v8::Extension* extension_;
|
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
|
|
|
ScriptDataImpl* pre_parse_data_;
|
2010-11-02 11:45:47 +00:00
|
|
|
FuncNameInferrer* fni_;
|
2011-11-11 13:48:14 +00:00
|
|
|
|
|
|
|
Mode mode_;
|
2011-03-22 18:00:03 +00:00
|
|
|
|
2012-06-04 14:42:58 +00:00
|
|
|
Zone* zone_;
|
2012-06-20 10:56:53 +00:00
|
|
|
CompilationInfo* info_;
|
2011-11-09 13:54:26 +00:00
|
|
|
friend class BlockState;
|
|
|
|
friend class FunctionState;
|
2010-10-01 14:10:47 +00:00
|
|
|
};
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-03-23 07:27:47 +00:00
|
|
|
|
|
|
|
// Support for handling complex values (array and object literals) that
|
|
|
|
// can be fully handled at compile time.
|
|
|
|
class CompileTimeValue: public AllStatic {
|
|
|
|
public:
|
2013-06-06 13:28:22 +00:00
|
|
|
enum LiteralType {
|
2010-03-11 10:34:29 +00:00
|
|
|
OBJECT_LITERAL_FAST_ELEMENTS,
|
|
|
|
OBJECT_LITERAL_SLOW_ELEMENTS,
|
2009-03-23 07:27:47 +00:00
|
|
|
ARRAY_LITERAL
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool IsCompileTimeValue(Expression* expression);
|
|
|
|
|
|
|
|
// Get the value as a compile time value.
|
2013-09-04 07:05:11 +00:00
|
|
|
static Handle<FixedArray> GetValue(Isolate* isolate, Expression* expression);
|
2009-03-23 07:27:47 +00:00
|
|
|
|
|
|
|
// Get the type of a compile time value returned by GetValue().
|
2013-06-06 13:28:22 +00:00
|
|
|
static LiteralType GetLiteralType(Handle<FixedArray> value);
|
2009-03-23 07:27:47 +00:00
|
|
|
|
|
|
|
// Get the elements array of a compile time value returned by GetValue().
|
|
|
|
static Handle<FixedArray> GetElements(Handle<FixedArray> value);
|
|
|
|
|
|
|
|
private:
|
2013-06-06 13:28:22 +00:00
|
|
|
static const int kLiteralTypeSlot = 0;
|
2009-03-23 07:27:47 +00:00
|
|
|
static const int kElementsSlot = 1;
|
|
|
|
|
|
|
|
DISALLOW_IMPLICIT_CONSTRUCTORS(CompileTimeValue);
|
|
|
|
};
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
} } // namespace v8::internal
|
|
|
|
|
|
|
|
#endif // V8_PARSER_H_
|