// Copyright 2010 the V8 project authors. All rights reserved. // 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_ #include "allocation.h" #include "ast.h" #include "scanner.h" namespace v8 { namespace internal { class FuncNameInferrer; class ParserFactory; class ParserLog; class PositionStack; class Target; class TemporaryScope; template class ZoneListWrapper; class ParserMessage : public Malloced { public: ParserMessage(Scanner::Location loc, const char* message, Vector args) : loc_(loc), message_(message), args_(args) { } ~ParserMessage(); Scanner::Location location() { return loc_; } const char* message() { return message_; } Vector args() { return args_; } private: Scanner::Location loc_; const char* message_; Vector args_; }; class FunctionEntry BASE_EMBEDDED { public: explicit FunctionEntry(Vector backing) : backing_(backing) { } FunctionEntry() : backing_(Vector::empty()) { } int start_pos() { return backing_[kStartPosOffset]; } void set_start_pos(int value) { backing_[kStartPosOffset] = value; } int end_pos() { return backing_[kEndPosOffset]; } void set_end_pos(int value) { backing_[kEndPosOffset] = value; } int literal_count() { return backing_[kLiteralCountOffset]; } void set_literal_count(int value) { backing_[kLiteralCountOffset] = value; } int property_count() { return backing_[kPropertyCountOffset]; } void set_property_count(int value) { backing_[kPropertyCountOffset] = value; } bool is_valid() { return backing_.length() > 0; } static const int kSize = 4; private: Vector backing_; static const int kStartPosOffset = 0; static const int kEndPosOffset = 1; static const int kLiteralCountOffset = 2; static const int kPropertyCountOffset = 3; }; class ScriptDataImpl : public ScriptData { public: explicit ScriptDataImpl(Vector store) : store_(store), owns_store_(true) { } // Create an empty ScriptDataImpl that is guaranteed to not satisfy // a SanityCheck. ScriptDataImpl() : store_(Vector()), owns_store_(false) { } virtual ~ScriptDataImpl(); virtual int Length(); virtual const char* Data(); virtual bool HasError(); void Initialize(); void ReadNextSymbolPosition(); FunctionEntry GetFunctionEntry(int start); int GetSymbolIdentifier(); bool SanityCheck(); Scanner::Location MessageLocation(); const char* BuildMessage(); Vector BuildArgs(); int symbol_count() { return (store_.length() > kHeaderSize) ? store_[kSymbolCountOffset] : 0; } // The following functions should only be called if SanityCheck has // returned true. bool has_error() { return store_[kHasErrorOffset]; } unsigned magic() { return store_[kMagicOffset]; } unsigned version() { return store_[kVersionOffset]; } static const unsigned kMagicNumber = 0xBadDead; static const unsigned kCurrentVersion = 4; static const int kMagicOffset = 0; static const int kVersionOffset = 1; static const int kHasErrorOffset = 2; static const int kFunctionsSizeOffset = 3; static const int kSymbolCountOffset = 4; static const int kSizeOffset = 5; static const int kHeaderSize = 6; // If encoding a message, the following positions are fixed. static const int kMessageStartPos = 0; static const int kMessageEndPos = 1; static const int kMessageArgCountPos = 2; static const int kMessageTextPos = 3; static const byte kNumberTerminator = 0x80u; private: Vector store_; unsigned char* symbol_data_; unsigned char* symbol_data_end_; int function_index_; bool owns_store_; unsigned Read(int position); unsigned* ReadAddress(int position); // Reads a number from the current symbols int ReadNumber(byte** source); ScriptDataImpl(const char* backing_store, int length) : store_(reinterpret_cast(const_cast(backing_store)), length / static_cast(sizeof(unsigned))), owns_store_(false) { ASSERT_EQ(0, static_cast( reinterpret_cast(backing_store) % sizeof(unsigned))); } // Read strings written by ParserRecorder::WriteString. static const char* ReadString(unsigned* start, int* chars); friend class ScriptData; }; class Parser { public: Parser(Handle