• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 //     * Redistributions of source code must retain the above copyright
7 //       notice, this list of conditions and the following disclaimer.
8 //     * Redistributions in binary form must reproduce the above
9 //       copyright notice, this list of conditions and the following
10 //       disclaimer in the documentation and/or other materials provided
11 //       with the distribution.
12 //     * Neither the name of Google Inc. nor the names of its
13 //       contributors may be used to endorse or promote products derived
14 //       from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 #ifndef V8_PARSER_H_
29 #define V8_PARSER_H_
30 
31 #include "scanner.h"
32 #include "allocation.h"
33 
34 namespace v8 {
35 namespace internal {
36 
37 
38 class ParserMessage : public Malloced {
39  public:
ParserMessage(Scanner::Location loc,const char * message,Vector<const char * > args)40   ParserMessage(Scanner::Location loc, const char* message,
41                 Vector<const char*> args)
42       : loc_(loc),
43         message_(message),
44         args_(args) { }
45   ~ParserMessage();
location()46   Scanner::Location location() { return loc_; }
message()47   const char* message() { return message_; }
args()48   Vector<const char*> args() { return args_; }
49  private:
50   Scanner::Location loc_;
51   const char* message_;
52   Vector<const char*> args_;
53 };
54 
55 
56 class FunctionEntry BASE_EMBEDDED {
57  public:
FunctionEntry(Vector<unsigned> backing)58   explicit FunctionEntry(Vector<unsigned> backing) : backing_(backing) { }
FunctionEntry()59   FunctionEntry() : backing_(Vector<unsigned>::empty()) { }
60 
start_pos()61   int start_pos() { return backing_[kStartPosOffset]; }
set_start_pos(int value)62   void set_start_pos(int value) { backing_[kStartPosOffset] = value; }
63 
end_pos()64   int end_pos() { return backing_[kEndPosOffset]; }
set_end_pos(int value)65   void set_end_pos(int value) { backing_[kEndPosOffset] = value; }
66 
literal_count()67   int literal_count() { return backing_[kLiteralCountOffset]; }
set_literal_count(int value)68   void set_literal_count(int value) { backing_[kLiteralCountOffset] = value; }
69 
property_count()70   int property_count() { return backing_[kPropertyCountOffset]; }
set_property_count(int value)71   void set_property_count(int value) { backing_[kPropertyCountOffset] = value; }
72 
contains_array_literal()73   bool contains_array_literal() {
74     return backing_[kContainsArrayLiteralOffset] != 0;
75   }
set_contains_array_literal(bool value)76   void set_contains_array_literal(bool value) {
77     backing_[kContainsArrayLiteralOffset] = value ? 1 : 0;
78   }
79 
is_valid()80   bool is_valid() { return backing_.length() > 0; }
81 
82   static const int kSize = 5;
83 
84  private:
85   Vector<unsigned> backing_;
86   static const int kStartPosOffset = 0;
87   static const int kEndPosOffset = 1;
88   static const int kLiteralCountOffset = 2;
89   static const int kPropertyCountOffset = 3;
90   static const int kContainsArrayLiteralOffset = 4;
91 };
92 
93 
94 class ScriptDataImpl : public ScriptData {
95  public:
ScriptDataImpl(Vector<unsigned> store)96   explicit ScriptDataImpl(Vector<unsigned> store)
97       : store_(store),
98         last_entry_(0) { }
99   virtual ~ScriptDataImpl();
100   virtual int Length();
101   virtual unsigned* Data();
102   FunctionEntry GetFunctionEnd(int start);
103   bool SanityCheck();
104 
105   Scanner::Location MessageLocation();
106   const char* BuildMessage();
107   Vector<const char*> BuildArgs();
108 
has_error()109   bool has_error() { return store_[kHasErrorOffset]; }
magic()110   unsigned magic() { return store_[kMagicOffset]; }
version()111   unsigned version() { return store_[kVersionOffset]; }
112 
113   static const unsigned kMagicNumber = 0xBadDead;
114   static const unsigned kCurrentVersion = 1;
115 
116   static const unsigned kMagicOffset = 0;
117   static const unsigned kVersionOffset = 1;
118   static const unsigned kHasErrorOffset = 2;
119   static const unsigned kSizeOffset = 3;
120   static const unsigned kHeaderSize = 4;
121 
122  private:
123   unsigned Read(int position);
124   unsigned* ReadAddress(int position);
125   int EntryCount();
126   FunctionEntry nth(int n);
127 
128   Vector<unsigned> store_;
129 
130   // The last entry returned.  This is used to make lookup faster:
131   // the next entry to return is typically the next entry so lookup
132   // will usually be much faster if we start from the last entry.
133   int last_entry_;
134 };
135 
136 
137 // The parser: Takes a script and and context information, and builds a
138 // FunctionLiteral AST node. Returns NULL and deallocates any allocated
139 // AST nodes if parsing failed.
140 FunctionLiteral* MakeAST(bool compile_in_global_context,
141                          Handle<Script> script,
142                          v8::Extension* extension,
143                          ScriptDataImpl* pre_data);
144 
145 
146 ScriptDataImpl* PreParse(Handle<String> source,
147                          unibrow::CharacterStream* stream,
148                          v8::Extension* extension);
149 
150 
151 bool ParseRegExp(FlatStringReader* input,
152                  bool multiline,
153                  RegExpCompileData* result);
154 
155 
156 // Support for doing lazy compilation. The script is the script containing full
157 // source of the script where the function is declared. The start_position and
158 // end_position specifies the part of the script source which has the source
159 // for the function declaration in the form:
160 //
161 //    (<formal parameters>) { <function body> }
162 //
163 // without any function keyword or name.
164 //
165 FunctionLiteral* MakeLazyAST(Handle<Script> script,
166                              Handle<String> name,
167                              int start_position,
168                              int end_position,
169                              bool is_expression);
170 
171 
172 // Support for handling complex values (array and object literals) that
173 // can be fully handled at compile time.
174 class CompileTimeValue: public AllStatic {
175  public:
176   enum Type {
177     OBJECT_LITERAL,
178     ARRAY_LITERAL
179   };
180 
181   static bool IsCompileTimeValue(Expression* expression);
182 
183   // Get the value as a compile time value.
184   static Handle<FixedArray> GetValue(Expression* expression);
185 
186   // Get the type of a compile time value returned by GetValue().
187   static Type GetType(Handle<FixedArray> value);
188 
189   // Get the elements array of a compile time value returned by GetValue().
190   static Handle<FixedArray> GetElements(Handle<FixedArray> value);
191 
192  private:
193   static const int kTypeSlot = 0;
194   static const int kElementsSlot = 1;
195 
196   DISALLOW_IMPLICIT_CONSTRUCTORS(CompileTimeValue);
197 };
198 
199 
200 } }  // namespace v8::internal
201 
202 #endif  // V8_PARSER_H_
203