blob: b0c580712892c904bec78c5fa5a5ad3bd6a6acdd [file] [log] [blame]
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001// Copyright 2011 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000031#include "allocation.h"
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +000032#include "ast.h"
33#include "scanner.h"
lrn@chromium.orgfa943b72010-11-03 08:14:36 +000034#include "scopes.h"
lrn@chromium.org1c092762011-05-09 09:42:16 +000035#include "preparse-data-format.h"
ager@chromium.orgbeb25712010-11-29 08:02:25 +000036#include "preparse-data.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037
kasperl@chromium.org71affb52009-05-26 05:44:31 +000038namespace v8 {
39namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000041class CompilationInfo;
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +000042class FuncNameInferrer;
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +000043class ParserLog;
44class PositionStack;
45class Target;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000046class LexicalScope;
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +000047
48template <typename T> class ZoneListWrapper;
49
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000050
51class ParserMessage : public Malloced {
52 public:
53 ParserMessage(Scanner::Location loc, const char* message,
54 Vector<const char*> args)
55 : loc_(loc),
56 message_(message),
57 args_(args) { }
58 ~ParserMessage();
59 Scanner::Location location() { return loc_; }
60 const char* message() { return message_; }
61 Vector<const char*> args() { return args_; }
62 private:
63 Scanner::Location loc_;
64 const char* message_;
65 Vector<const char*> args_;
66};
67
68
69class FunctionEntry BASE_EMBEDDED {
70 public:
71 explicit FunctionEntry(Vector<unsigned> backing) : backing_(backing) { }
72 FunctionEntry() : backing_(Vector<unsigned>::empty()) { }
73
74 int start_pos() { return backing_[kStartPosOffset]; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000075 int end_pos() { return backing_[kEndPosOffset]; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000076 int literal_count() { return backing_[kLiteralCountOffset]; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000077 int property_count() { return backing_[kPropertyCountOffset]; }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000078 bool strict_mode() { return backing_[kStrictModeOffset] != 0; }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000079
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000080 bool is_valid() { return backing_.length() > 0; }
81
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000082 static const int kSize = 5;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000083
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;
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000090 static const int kStrictModeOffset = 4;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000091};
92
93
94class ScriptDataImpl : public ScriptData {
95 public:
96 explicit ScriptDataImpl(Vector<unsigned> store)
97 : store_(store),
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +000098 owns_store_(true) { }
ager@chromium.org5b2fbee2010-09-08 06:38:15 +000099
ager@chromium.org5b2fbee2010-09-08 06:38:15 +0000100 // Create an empty ScriptDataImpl that is guaranteed to not satisfy
101 // a SanityCheck.
102 ScriptDataImpl() : store_(Vector<unsigned>()), owns_store_(false) { }
103
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000104 virtual ~ScriptDataImpl();
105 virtual int Length();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000106 virtual const char* Data();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000107 virtual bool HasError();
ager@chromium.org5b2fbee2010-09-08 06:38:15 +0000108
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000109 void Initialize();
110 void ReadNextSymbolPosition();
111
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000112 FunctionEntry GetFunctionEntry(int start);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000113 int GetSymbolIdentifier();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000114 bool SanityCheck();
115
116 Scanner::Location MessageLocation();
117 const char* BuildMessage();
118 Vector<const char*> BuildArgs();
119
ager@chromium.org5b2fbee2010-09-08 06:38:15 +0000120 int symbol_count() {
ager@chromium.orgbeb25712010-11-29 08:02:25 +0000121 return (store_.length() > PreparseDataConstants::kHeaderSize)
122 ? store_[PreparseDataConstants::kSymbolCountOffset]
123 : 0;
ager@chromium.org5b2fbee2010-09-08 06:38:15 +0000124 }
125 // The following functions should only be called if SanityCheck has
126 // returned true.
ager@chromium.orgbeb25712010-11-29 08:02:25 +0000127 bool has_error() { return store_[PreparseDataConstants::kHasErrorOffset]; }
128 unsigned magic() { return store_[PreparseDataConstants::kMagicOffset]; }
129 unsigned version() { return store_[PreparseDataConstants::kVersionOffset]; }
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000130
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000131 private:
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000132 Vector<unsigned> store_;
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000133 unsigned char* symbol_data_;
134 unsigned char* symbol_data_end_;
ager@chromium.org5b2fbee2010-09-08 06:38:15 +0000135 int function_index_;
ager@chromium.org5b2fbee2010-09-08 06:38:15 +0000136 bool owns_store_;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000137
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138 unsigned Read(int position);
139 unsigned* ReadAddress(int position);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000140 // Reads a number from the current symbols
141 int ReadNumber(byte** source);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142
ager@chromium.org5b2fbee2010-09-08 06:38:15 +0000143 ScriptDataImpl(const char* backing_store, int length)
144 : store_(reinterpret_cast<unsigned*>(const_cast<char*>(backing_store)),
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000145 length / static_cast<int>(sizeof(unsigned))),
ager@chromium.org5b2fbee2010-09-08 06:38:15 +0000146 owns_store_(false) {
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000147 ASSERT_EQ(0, static_cast<int>(
148 reinterpret_cast<intptr_t>(backing_store) % sizeof(unsigned)));
ager@chromium.org5b2fbee2010-09-08 06:38:15 +0000149 }
150
ricow@chromium.org65fae842010-08-25 15:26:24 +0000151 // Read strings written by ParserRecorder::WriteString.
152 static const char* ReadString(unsigned* start, int* chars);
ager@chromium.org5b2fbee2010-09-08 06:38:15 +0000153
154 friend class ScriptData;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000155};
156
157
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000158class ParserApi {
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000159 public:
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000160 // Parses the source code represented by the compilation info and sets its
161 // function literal. Returns false (and deallocates any allocated AST
162 // nodes) if parsing failed.
163 static bool Parse(CompilationInfo* info);
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000164
165 // Generic preparser generating full preparse data.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000166 static ScriptDataImpl* PreParse(UC16CharacterStream* source,
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000167 v8::Extension* extension);
168
169 // Preparser that only does preprocessing that makes sense if only used
170 // immediately after.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000171 static ScriptDataImpl* PartialPreParse(UC16CharacterStream* source,
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000172 v8::Extension* extension);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000173};
174
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000175// ----------------------------------------------------------------------------
176// REGEXP PARSING
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000177
178// A BuffferedZoneList is an automatically growing list, just like (and backed
179// by) a ZoneList, that is optimized for the case of adding and removing
180// a single element. The last element added is stored outside the backing list,
181// and if no more than one element is ever added, the ZoneList isn't even
182// allocated.
183// Elements must not be NULL pointers.
184template <typename T, int initial_size>
185class BufferedZoneList {
186 public:
187 BufferedZoneList() : list_(NULL), last_(NULL) {}
188
189 // Adds element at end of list. This element is buffered and can
190 // be read using last() or removed using RemoveLast until a new Add or until
191 // RemoveLast or GetList has been called.
192 void Add(T* value) {
193 if (last_ != NULL) {
194 if (list_ == NULL) {
195 list_ = new ZoneList<T*>(initial_size);
196 }
197 list_->Add(last_);
198 }
199 last_ = value;
200 }
201
202 T* last() {
203 ASSERT(last_ != NULL);
204 return last_;
205 }
206
207 T* RemoveLast() {
208 ASSERT(last_ != NULL);
209 T* result = last_;
210 if ((list_ != NULL) && (list_->length() > 0))
211 last_ = list_->RemoveLast();
212 else
213 last_ = NULL;
214 return result;
215 }
216
217 T* Get(int i) {
218 ASSERT((0 <= i) && (i < length()));
219 if (list_ == NULL) {
220 ASSERT_EQ(0, i);
221 return last_;
222 } else {
223 if (i == list_->length()) {
224 ASSERT(last_ != NULL);
225 return last_;
226 } else {
227 return list_->at(i);
228 }
229 }
230 }
231
232 void Clear() {
233 list_ = NULL;
234 last_ = NULL;
235 }
236
237 int length() {
238 int length = (list_ == NULL) ? 0 : list_->length();
239 return length + ((last_ == NULL) ? 0 : 1);
240 }
241
242 ZoneList<T*>* GetList() {
243 if (list_ == NULL) {
244 list_ = new ZoneList<T*>(initial_size);
245 }
246 if (last_ != NULL) {
247 list_->Add(last_);
248 last_ = NULL;
249 }
250 return list_;
251 }
252
253 private:
254 ZoneList<T*>* list_;
255 T* last_;
256};
257
258
259// Accumulates RegExp atoms and assertions into lists of terms and alternatives.
260class RegExpBuilder: public ZoneObject {
261 public:
262 RegExpBuilder();
263 void AddCharacter(uc16 character);
264 // "Adds" an empty expression. Does nothing except consume a
265 // following quantifier
266 void AddEmpty();
267 void AddAtom(RegExpTree* tree);
268 void AddAssertion(RegExpTree* tree);
269 void NewAlternative(); // '|'
270 void AddQuantifierToAtom(int min, int max, RegExpQuantifier::Type type);
271 RegExpTree* ToRegExp();
272
273 private:
274 void FlushCharacters();
275 void FlushText();
276 void FlushTerms();
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000277 Zone* zone() { return zone_; }
278
279 Zone* zone_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000280 bool pending_empty_;
281 ZoneList<uc16>* characters_;
282 BufferedZoneList<RegExpTree, 2> terms_;
283 BufferedZoneList<RegExpTree, 2> text_;
284 BufferedZoneList<RegExpTree, 2> alternatives_;
285#ifdef DEBUG
286 enum {ADD_NONE, ADD_CHAR, ADD_TERM, ADD_ASSERT, ADD_ATOM} last_added_;
287#define LAST(x) last_added_ = x;
288#else
289#define LAST(x)
290#endif
291};
292
293
294class RegExpParser {
295 public:
296 RegExpParser(FlatStringReader* in,
297 Handle<String>* error,
298 bool multiline_mode);
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000299
300 static bool ParseRegExp(FlatStringReader* input,
301 bool multiline,
302 RegExpCompileData* result);
303
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000304 RegExpTree* ParsePattern();
305 RegExpTree* ParseDisjunction();
306 RegExpTree* ParseGroup();
307 RegExpTree* ParseCharacterClass();
308
309 // Parses a {...,...} quantifier and stores the range in the given
310 // out parameters.
311 bool ParseIntervalQuantifier(int* min_out, int* max_out);
312
313 // Parses and returns a single escaped character. The character
314 // must not be 'b' or 'B' since they are usually handle specially.
315 uc32 ParseClassCharacterEscape();
316
317 // Checks whether the following is a length-digit hexadecimal number,
318 // and sets the value if it is.
319 bool ParseHexEscape(int length, uc32* value);
320
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000321 uc32 ParseOctalLiteral();
322
323 // Tries to parse the input as a back reference. If successful it
324 // stores the result in the output parameter and returns true. If
325 // it fails it will push back the characters read so the same characters
326 // can be reparsed.
327 bool ParseBackReferenceIndex(int* index_out);
328
329 CharacterRange ParseClassAtom(uc16* char_class);
330 RegExpTree* ReportError(Vector<const char> message);
331 void Advance();
332 void Advance(int dist);
333 void Reset(int pos);
334
335 // Reports whether the pattern might be used as a literal search string.
336 // Only use if the result of the parse is a single atom node.
337 bool simple();
338 bool contains_anchor() { return contains_anchor_; }
339 void set_contains_anchor() { contains_anchor_ = true; }
340 int captures_started() { return captures_ == NULL ? 0 : captures_->length(); }
341 int position() { return next_pos_ - 1; }
342 bool failed() { return failed_; }
343
344 static const int kMaxCaptures = 1 << 16;
345 static const uc32 kEndMarker = (1 << 21);
346
347 private:
348 enum SubexpressionType {
349 INITIAL,
350 CAPTURE, // All positive values represent captures.
351 POSITIVE_LOOKAHEAD,
352 NEGATIVE_LOOKAHEAD,
353 GROUPING
354 };
355
356 class RegExpParserState : public ZoneObject {
357 public:
358 RegExpParserState(RegExpParserState* previous_state,
359 SubexpressionType group_type,
360 int disjunction_capture_index)
361 : previous_state_(previous_state),
362 builder_(new RegExpBuilder()),
363 group_type_(group_type),
364 disjunction_capture_index_(disjunction_capture_index) {}
365 // Parser state of containing expression, if any.
366 RegExpParserState* previous_state() { return previous_state_; }
367 bool IsSubexpression() { return previous_state_ != NULL; }
368 // RegExpBuilder building this regexp's AST.
369 RegExpBuilder* builder() { return builder_; }
370 // Type of regexp being parsed (parenthesized group or entire regexp).
371 SubexpressionType group_type() { return group_type_; }
372 // Index in captures array of first capture in this sub-expression, if any.
373 // Also the capture index of this sub-expression itself, if group_type
374 // is CAPTURE.
375 int capture_index() { return disjunction_capture_index_; }
376
377 private:
378 // Linked list implementation of stack of states.
379 RegExpParserState* previous_state_;
380 // Builder for the stored disjunction.
381 RegExpBuilder* builder_;
382 // Stored disjunction type (capture, look-ahead or grouping), if any.
383 SubexpressionType group_type_;
384 // Stored disjunction's capture index (if any).
385 int disjunction_capture_index_;
386 };
387
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000388 Isolate* isolate() { return isolate_; }
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000389 Zone* zone() { return isolate_->zone(); }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000390
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000391 uc32 current() { return current_; }
392 bool has_more() { return has_more_; }
393 bool has_next() { return next_pos_ < in()->length(); }
394 uc32 Next();
395 FlatStringReader* in() { return in_; }
396 void ScanForCaptures();
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000397
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000398 Isolate* isolate_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000399 Handle<String>* error_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000400 ZoneList<RegExpCapture*>* captures_;
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000401 FlatStringReader* in_;
402 uc32 current_;
403 int next_pos_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000404 // The capture count is only valid after we have scanned for captures.
405 int capture_count_;
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000406 bool has_more_;
407 bool multiline_;
408 bool simple_;
409 bool contains_anchor_;
410 bool is_scanned_for_captures_;
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000411 bool failed_;
412};
413
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000414// ----------------------------------------------------------------------------
415// JAVASCRIPT PARSING
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000416
417class Parser {
418 public:
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000419 Parser(Handle<Script> script,
420 bool allow_natives_syntax,
421 v8::Extension* extension,
422 ScriptDataImpl* pre_data);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000423 virtual ~Parser() { }
424
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000425 // Returns NULL if parsing failed.
426 FunctionLiteral* ParseProgram(Handle<String> source,
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000427 bool in_global_context,
428 StrictModeFlag strict_mode);
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000429
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000430 FunctionLiteral* ParseLazy(CompilationInfo* info);
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000431
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000432 void ReportMessageAt(Scanner::Location loc,
433 const char* message,
434 Vector<const char*> args);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000435 void ReportMessageAt(Scanner::Location loc,
436 const char* message,
437 Vector<Handle<String> > args);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000438 void SetHarmonyBlockScoping(bool block_scoping);
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000439
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000440 private:
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +0000441 // Limit on number of function parameters is chosen arbitrarily.
442 // Code::Flags uses only the low 17 bits of num-parameters to
443 // construct a hashable id, so if more than 2^17 are allowed, this
444 // should be checked.
445 static const int kMaxNumFunctionParameters = 32766;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000446 static const int kMaxNumFunctionLocals = 32767;
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000447 FunctionLiteral* ParseLazy(CompilationInfo* info,
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000448 UC16CharacterStream* source,
449 ZoneScope* zone_scope);
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000450 enum Mode {
451 PARSE_LAZILY,
452 PARSE_EAGERLY
453 };
454
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000455 Isolate* isolate() { return isolate_; }
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000456 Zone* zone() { return isolate_->zone(); }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000457
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000458 // Called by ParseProgram after setting up the scanner.
459 FunctionLiteral* DoParseProgram(Handle<String> source,
460 bool in_global_context,
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000461 StrictModeFlag strict_mode,
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000462 ZoneScope* zone_scope);
463
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000464 // Report syntax error
465 void ReportUnexpectedToken(Token::Value token);
466 void ReportInvalidPreparseData(Handle<String> name, bool* ok);
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000467 void ReportMessage(const char* message, Vector<const char*> args);
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000468
469 bool inside_with() const { return with_nesting_level_ > 0; }
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000470 JavaScriptScanner& scanner() { return scanner_; }
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000471 Mode mode() const { return mode_; }
472 ScriptDataImpl* pre_data() const { return pre_data_; }
473
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000474 // Check if the given string is 'eval' or 'arguments'.
475 bool IsEvalOrArguments(Handle<String> string);
476
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000477 // All ParseXXX functions take as the last argument an *ok parameter
478 // which is set to false if parsing failed; it is unchanged otherwise.
479 // By making the 'exception handling' explicit, we are forced to check
480 // for failure at the call sites.
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000481 void* ParseSourceElements(ZoneList<Statement*>* processor,
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000482 int end_token, bool* ok);
483 Statement* ParseStatement(ZoneStringList* labels, bool* ok);
484 Statement* ParseFunctionDeclaration(bool* ok);
485 Statement* ParseNativeDeclaration(bool* ok);
486 Block* ParseBlock(ZoneStringList* labels, bool* ok);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000487 Block* ParseScopedBlock(ZoneStringList* labels, bool* ok);
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000488 Block* ParseVariableStatement(bool* ok);
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000489 Block* ParseVariableDeclarations(bool accept_IN,
490 Handle<String>* out,
491 bool* ok);
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000492 Statement* ParseExpressionOrLabelledStatement(ZoneStringList* labels,
493 bool* ok);
494 IfStatement* ParseIfStatement(ZoneStringList* labels, bool* ok);
495 Statement* ParseContinueStatement(bool* ok);
496 Statement* ParseBreakStatement(ZoneStringList* labels, bool* ok);
497 Statement* ParseReturnStatement(bool* ok);
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000498 Statement* ParseWithStatement(ZoneStringList* labels, bool* ok);
499 CaseClause* ParseCaseClause(bool* default_seen_ptr, bool* ok);
500 SwitchStatement* ParseSwitchStatement(ZoneStringList* labels, bool* ok);
501 DoWhileStatement* ParseDoWhileStatement(ZoneStringList* labels, bool* ok);
502 WhileStatement* ParseWhileStatement(ZoneStringList* labels, bool* ok);
503 Statement* ParseForStatement(ZoneStringList* labels, bool* ok);
504 Statement* ParseThrowStatement(bool* ok);
505 Expression* MakeCatchContext(Handle<String> id, VariableProxy* value);
506 TryStatement* ParseTryStatement(bool* ok);
507 DebuggerStatement* ParseDebuggerStatement(bool* ok);
508
509 Expression* ParseExpression(bool accept_IN, bool* ok);
510 Expression* ParseAssignmentExpression(bool accept_IN, bool* ok);
511 Expression* ParseConditionalExpression(bool accept_IN, bool* ok);
512 Expression* ParseBinaryExpression(int prec, bool accept_IN, bool* ok);
513 Expression* ParseUnaryExpression(bool* ok);
514 Expression* ParsePostfixExpression(bool* ok);
515 Expression* ParseLeftHandSideExpression(bool* ok);
516 Expression* ParseNewExpression(bool* ok);
517 Expression* ParseMemberExpression(bool* ok);
518 Expression* ParseNewPrefix(PositionStack* stack, bool* ok);
519 Expression* ParseMemberWithNewPrefixesExpression(PositionStack* stack,
520 bool* ok);
521 Expression* ParsePrimaryExpression(bool* ok);
522 Expression* ParseArrayLiteral(bool* ok);
523 Expression* ParseObjectLiteral(bool* ok);
524 ObjectLiteral::Property* ParseObjectLiteralGetSet(bool is_getter, bool* ok);
525 Expression* ParseRegExpLiteral(bool seen_equal, bool* ok);
526
527 Expression* NewCompareNode(Token::Value op,
528 Expression* x,
529 Expression* y,
530 int position);
531
532 // Populate the constant properties fixed array for a materialized object
533 // literal.
534 void BuildObjectLiteralConstantProperties(
535 ZoneList<ObjectLiteral::Property*>* properties,
536 Handle<FixedArray> constants,
537 bool* is_simple,
538 bool* fast_elements,
539 int* depth);
540
541 // Populate the literals fixed array for a materialized array literal.
542 void BuildArrayLiteralBoilerplateLiterals(ZoneList<Expression*>* properties,
543 Handle<FixedArray> constants,
544 bool* is_simple,
545 int* depth);
546
547 // Decide if a property should be in the object boilerplate.
548 bool IsBoilerplateProperty(ObjectLiteral::Property* property);
549 // If the expression is a literal, return the literal value;
550 // if the expression is a materialized literal and is simple return a
551 // compile time value as encoded by CompileTimeValue::GetValue().
552 // Otherwise, return undefined literal as the placeholder
553 // in the object literal boilerplate.
554 Handle<Object> GetBoilerplateValue(Expression* expression);
555
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000556 ZoneList<Expression*>* ParseArguments(bool* ok);
557 FunctionLiteral* ParseFunctionLiteral(Handle<String> var_name,
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000558 bool name_is_reserved,
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000559 int function_token_position,
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000560 FunctionLiteral::Type type,
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000561 bool* ok);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000562
563
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000564 // Magical syntax support.
565 Expression* ParseV8Intrinsic(bool* ok);
566
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000567 INLINE(Token::Value peek()) {
568 if (stack_overflow_) return Token::ILLEGAL;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000569 return scanner().peek();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000570 }
571
572 INLINE(Token::Value Next()) {
573 // BUG 1215673: Find a thread safe way to set a stack limit in
574 // pre-parse mode. Otherwise, we cannot safely pre-parse from other
575 // threads.
576 if (stack_overflow_) {
577 return Token::ILLEGAL;
578 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000579 if (StackLimitCheck(isolate()).HasOverflowed()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000580 // Any further calls to Next or peek will return the illegal token.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000581 // The current call must return the next token, which might already
582 // have been peek'ed.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000583 stack_overflow_ = true;
584 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000585 return scanner().Next();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000586 }
587
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000588 bool peek_any_identifier();
589
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000590 INLINE(void Consume(Token::Value token));
591 void Expect(Token::Value token, bool* ok);
592 bool Check(Token::Value token);
593 void ExpectSemicolon(bool* ok);
594
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000595 Handle<String> LiteralString(PretenureFlag tenured) {
596 if (scanner().is_literal_ascii()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000597 return isolate_->factory()->NewStringFromAscii(
598 scanner().literal_ascii_string(), tenured);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000599 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000600 return isolate_->factory()->NewStringFromTwoByte(
601 scanner().literal_uc16_string(), tenured);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000602 }
603 }
604
605 Handle<String> NextLiteralString(PretenureFlag tenured) {
606 if (scanner().is_next_literal_ascii()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000607 return isolate_->factory()->NewStringFromAscii(
608 scanner().next_literal_ascii_string(), tenured);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000609 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000610 return isolate_->factory()->NewStringFromTwoByte(
611 scanner().next_literal_uc16_string(), tenured);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000612 }
613 }
614
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000615 Handle<String> GetSymbol(bool* ok);
616
617 // Get odd-ball literals.
618 Literal* GetLiteralUndefined();
619 Literal* GetLiteralTheHole();
620 Literal* GetLiteralNumber(double value);
621
622 Handle<String> ParseIdentifier(bool* ok);
ager@chromium.org04921a82011-06-27 13:21:41 +0000623 Handle<String> ParseIdentifierOrStrictReservedWord(
624 bool* is_strict_reserved, bool* ok);
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000625 Handle<String> ParseIdentifierName(bool* ok);
ager@chromium.org04921a82011-06-27 13:21:41 +0000626 Handle<String> ParseIdentifierNameOrGetOrSet(bool* is_get,
627 bool* is_set,
628 bool* ok);
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000629
ager@chromium.org378b34e2011-01-28 08:04:38 +0000630 // Strict mode validation of LValue expressions
631 void CheckStrictModeLValue(Expression* expression,
632 const char* error,
633 bool* ok);
634
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000635 // Strict mode octal literal validation.
636 void CheckOctalLiteral(int beg_pos, int end_pos, bool* ok);
637
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000638 // Parser support
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000639 VariableProxy* Declare(Handle<String> name, Variable::Mode mode,
640 FunctionLiteral* fun,
641 bool resolve,
642 bool* ok);
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000643
644 bool TargetStackContainsLabel(Handle<String> label);
645 BreakableStatement* LookupBreakTarget(Handle<String> label, bool* ok);
646 IterationStatement* LookupContinueTarget(Handle<String> label, bool* ok);
647
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000648 void RegisterTargetUse(Label* target, Target* stop);
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000649
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000650 // Factory methods.
651
652 Statement* EmptyStatement() {
653 static v8::internal::EmptyStatement empty;
654 return &empty;
655 }
656
657 Scope* NewScope(Scope* parent, Scope::Type type, bool inside_with);
658
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000659 Handle<String> LookupSymbol(int symbol_id);
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000660
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000661 Handle<String> LookupCachedSymbol(int symbol_id);
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000662
663 Expression* NewCall(Expression* expression,
664 ZoneList<Expression*>* arguments,
665 int pos) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000666 return new(zone()) Call(isolate(), expression, arguments, pos);
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000667 }
668
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000669 inline Literal* NewLiteral(Handle<Object> handle) {
670 return new(zone()) Literal(isolate(), handle);
671 }
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000672
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000673 // Create a number literal.
674 Literal* NewNumberLiteral(double value);
675
676 // Generate AST node that throw a ReferenceError with the given type.
677 Expression* NewThrowReferenceError(Handle<String> type);
678
679 // Generate AST node that throw a SyntaxError with the given
680 // type. The first argument may be null (in the handle sense) in
681 // which case no arguments are passed to the constructor.
682 Expression* NewThrowSyntaxError(Handle<String> type, Handle<Object> first);
683
684 // Generate AST node that throw a TypeError with the given
685 // type. Both arguments must be non-null (in the handle sense).
686 Expression* NewThrowTypeError(Handle<String> type,
687 Handle<Object> first,
688 Handle<Object> second);
689
690 // Generic AST generator for throwing errors from compiled code.
691 Expression* NewThrowError(Handle<String> constructor,
692 Handle<String> type,
693 Vector< Handle<Object> > arguments);
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000694
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000695 Isolate* isolate_;
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000696 ZoneList<Handle<String> > symbol_cache_;
697
698 Handle<Script> script_;
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000699 JavaScriptScanner scanner_;
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000700
701 Scope* top_scope_;
702 int with_nesting_level_;
703
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000704 LexicalScope* lexical_scope_;
lrn@chromium.orgfa943b72010-11-03 08:14:36 +0000705 Mode mode_;
706
707 Target* target_stack_; // for break, continue statements
708 bool allow_natives_syntax_;
709 v8::Extension* extension_;
710 bool is_pre_parsing_;
711 ScriptDataImpl* pre_data_;
712 FuncNameInferrer* fni_;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000713 bool stack_overflow_;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000714 // If true, the next (and immediately following) function literal is
715 // preceded by a parenthesis.
716 // Heuristically that means that the function will be called immediately,
717 // so never lazily compile it.
718 bool parenthesized_function_;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000719 bool harmony_block_scoping_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000720
721 friend class LexicalScope;
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000722};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000723
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000724
725// Support for handling complex values (array and object literals) that
726// can be fully handled at compile time.
727class CompileTimeValue: public AllStatic {
728 public:
729 enum Type {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000730 OBJECT_LITERAL_FAST_ELEMENTS,
731 OBJECT_LITERAL_SLOW_ELEMENTS,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000732 ARRAY_LITERAL
733 };
734
735 static bool IsCompileTimeValue(Expression* expression);
736
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000737 static bool ArrayLiteralElementNeedsInitialization(Expression* value);
738
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000739 // Get the value as a compile time value.
740 static Handle<FixedArray> GetValue(Expression* expression);
741
742 // Get the type of a compile time value returned by GetValue().
743 static Type GetType(Handle<FixedArray> value);
744
745 // Get the elements array of a compile time value returned by GetValue().
746 static Handle<FixedArray> GetElements(Handle<FixedArray> value);
747
748 private:
749 static const int kTypeSlot = 0;
750 static const int kElementsSlot = 1;
751
752 DISALLOW_IMPLICIT_CONSTRUCTORS(CompileTimeValue);
753};
754
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000755} } // namespace v8::internal
756
757#endif // V8_PARSER_H_