blob: 3312f2f56a85de7c66c0d7db34768157eeb8752a [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +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
Steve Blocka7e24c12009-10-30 11:49:00 +000031#include "allocation.h"
Ben Murdochf87a2032010-10-22 12:50:53 +010032#include "ast.h"
Ben Murdoch257744e2011-11-30 15:57:28 +000033#include "preparse-data-format.h"
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080034#include "preparse-data.h"
Ben Murdoch589d6972011-11-30 16:04:58 +000035#include "scopes.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000036
37namespace v8 {
38namespace internal {
39
Ben Murdochf87a2032010-10-22 12:50:53 +010040class CompilationInfo;
41class FuncNameInferrer;
Ben Murdochf87a2032010-10-22 12:50:53 +010042class ParserLog;
43class PositionStack;
44class Target;
Steve Block44f0eee2011-05-26 01:26:41 +010045class LexicalScope;
Ben Murdochf87a2032010-10-22 12:50:53 +010046
47template <typename T> class ZoneListWrapper;
48
Steve Blocka7e24c12009-10-30 11:49:00 +000049
50class ParserMessage : public Malloced {
51 public:
52 ParserMessage(Scanner::Location loc, const char* message,
53 Vector<const char*> args)
54 : loc_(loc),
55 message_(message),
56 args_(args) { }
57 ~ParserMessage();
58 Scanner::Location location() { return loc_; }
59 const char* message() { return message_; }
60 Vector<const char*> args() { return args_; }
61 private:
62 Scanner::Location loc_;
63 const char* message_;
64 Vector<const char*> args_;
65};
66
67
68class FunctionEntry BASE_EMBEDDED {
69 public:
70 explicit FunctionEntry(Vector<unsigned> backing) : backing_(backing) { }
71 FunctionEntry() : backing_(Vector<unsigned>::empty()) { }
72
73 int start_pos() { return backing_[kStartPosOffset]; }
Steve Blocka7e24c12009-10-30 11:49:00 +000074 int end_pos() { return backing_[kEndPosOffset]; }
Steve Blocka7e24c12009-10-30 11:49:00 +000075 int literal_count() { return backing_[kLiteralCountOffset]; }
Steve Blocka7e24c12009-10-30 11:49:00 +000076 int property_count() { return backing_[kPropertyCountOffset]; }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000077 bool strict_mode() { return backing_[kStrictModeOffset] != 0; }
Kristian Monsen80d68ea2010-09-08 11:05:35 +010078
Steve Blocka7e24c12009-10-30 11:49:00 +000079 bool is_valid() { return backing_.length() > 0; }
80
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000081 static const int kSize = 5;
Steve Blocka7e24c12009-10-30 11:49:00 +000082
83 private:
84 Vector<unsigned> backing_;
85 static const int kStartPosOffset = 0;
86 static const int kEndPosOffset = 1;
87 static const int kLiteralCountOffset = 2;
88 static const int kPropertyCountOffset = 3;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000089 static const int kStrictModeOffset = 4;
Steve Blocka7e24c12009-10-30 11:49:00 +000090};
91
92
93class ScriptDataImpl : public ScriptData {
94 public:
95 explicit ScriptDataImpl(Vector<unsigned> store)
96 : store_(store),
Kristian Monsen0d5e1162010-09-30 15:31:59 +010097 owns_store_(true) { }
Iain Merrick9ac36c92010-09-13 15:29:50 +010098
99 // Create an empty ScriptDataImpl that is guaranteed to not satisfy
100 // a SanityCheck.
101 ScriptDataImpl() : store_(Vector<unsigned>()), owns_store_(false) { }
102
Steve Blocka7e24c12009-10-30 11:49:00 +0000103 virtual ~ScriptDataImpl();
104 virtual int Length();
Leon Clarkef7060e22010-06-03 12:02:55 +0100105 virtual const char* Data();
Leon Clarkee46be812010-01-19 14:06:41 +0000106 virtual bool HasError();
Iain Merrick9ac36c92010-09-13 15:29:50 +0100107
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100108 void Initialize();
109 void ReadNextSymbolPosition();
110
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100111 FunctionEntry GetFunctionEntry(int start);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100112 int GetSymbolIdentifier();
Steve Blocka7e24c12009-10-30 11:49:00 +0000113 bool SanityCheck();
114
115 Scanner::Location MessageLocation();
116 const char* BuildMessage();
117 Vector<const char*> BuildArgs();
118
Iain Merrick9ac36c92010-09-13 15:29:50 +0100119 int symbol_count() {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800120 return (store_.length() > PreparseDataConstants::kHeaderSize)
121 ? store_[PreparseDataConstants::kSymbolCountOffset]
122 : 0;
Iain Merrick9ac36c92010-09-13 15:29:50 +0100123 }
124 // The following functions should only be called if SanityCheck has
125 // returned true.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800126 bool has_error() { return store_[PreparseDataConstants::kHasErrorOffset]; }
127 unsigned magic() { return store_[PreparseDataConstants::kMagicOffset]; }
128 unsigned version() { return store_[PreparseDataConstants::kVersionOffset]; }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100129
Steve Blocka7e24c12009-10-30 11:49:00 +0000130 private:
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100131 Vector<unsigned> store_;
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100132 unsigned char* symbol_data_;
133 unsigned char* symbol_data_end_;
Iain Merrick9ac36c92010-09-13 15:29:50 +0100134 int function_index_;
Iain Merrick9ac36c92010-09-13 15:29:50 +0100135 bool owns_store_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100136
Steve Blocka7e24c12009-10-30 11:49:00 +0000137 unsigned Read(int position);
138 unsigned* ReadAddress(int position);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100139 // Reads a number from the current symbols
140 int ReadNumber(byte** source);
Steve Blocka7e24c12009-10-30 11:49:00 +0000141
Iain Merrick9ac36c92010-09-13 15:29:50 +0100142 ScriptDataImpl(const char* backing_store, int length)
143 : store_(reinterpret_cast<unsigned*>(const_cast<char*>(backing_store)),
Ben Murdochf87a2032010-10-22 12:50:53 +0100144 length / static_cast<int>(sizeof(unsigned))),
Iain Merrick9ac36c92010-09-13 15:29:50 +0100145 owns_store_(false) {
Ben Murdochf87a2032010-10-22 12:50:53 +0100146 ASSERT_EQ(0, static_cast<int>(
147 reinterpret_cast<intptr_t>(backing_store) % sizeof(unsigned)));
Iain Merrick9ac36c92010-09-13 15:29:50 +0100148 }
149
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100150 // Read strings written by ParserRecorder::WriteString.
151 static const char* ReadString(unsigned* start, int* chars);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100152
153 friend class ScriptData;
Steve Blocka7e24c12009-10-30 11:49:00 +0000154};
155
156
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800157class ParserApi {
158 public:
Ben Murdochf87a2032010-10-22 12:50:53 +0100159 // Parses the source code represented by the compilation info and sets its
160 // function literal. Returns false (and deallocates any allocated AST
161 // nodes) if parsing failed.
162 static bool Parse(CompilationInfo* info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000163
Ben Murdochf87a2032010-10-22 12:50:53 +0100164 // Generic preparser generating full preparse data.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100165 static ScriptDataImpl* PreParse(UC16CharacterStream* source,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000166 v8::Extension* extension,
167 bool harmony_block_scoping);
Ben Murdochf87a2032010-10-22 12:50:53 +0100168
169 // Preparser that only does preprocessing that makes sense if only used
170 // immediately after.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100171 static ScriptDataImpl* PartialPreParse(UC16CharacterStream* source,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000172 v8::Extension* extension,
173 bool harmony_block_scoping);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800174};
175
176// ----------------------------------------------------------------------------
177// REGEXP PARSING
178
179// A BuffferedZoneList is an automatically growing list, just like (and backed
180// by) a ZoneList, that is optimized for the case of adding and removing
181// a single element. The last element added is stored outside the backing list,
182// and if no more than one element is ever added, the ZoneList isn't even
183// allocated.
184// Elements must not be NULL pointers.
185template <typename T, int initial_size>
186class BufferedZoneList {
187 public:
188 BufferedZoneList() : list_(NULL), last_(NULL) {}
189
190 // Adds element at end of list. This element is buffered and can
191 // be read using last() or removed using RemoveLast until a new Add or until
192 // RemoveLast or GetList has been called.
193 void Add(T* value) {
194 if (last_ != NULL) {
195 if (list_ == NULL) {
196 list_ = new ZoneList<T*>(initial_size);
197 }
198 list_->Add(last_);
199 }
200 last_ = value;
201 }
202
203 T* last() {
204 ASSERT(last_ != NULL);
205 return last_;
206 }
207
208 T* RemoveLast() {
209 ASSERT(last_ != NULL);
210 T* result = last_;
211 if ((list_ != NULL) && (list_->length() > 0))
212 last_ = list_->RemoveLast();
213 else
214 last_ = NULL;
215 return result;
216 }
217
218 T* Get(int i) {
219 ASSERT((0 <= i) && (i < length()));
220 if (list_ == NULL) {
221 ASSERT_EQ(0, i);
222 return last_;
223 } else {
224 if (i == list_->length()) {
225 ASSERT(last_ != NULL);
226 return last_;
227 } else {
228 return list_->at(i);
229 }
230 }
231 }
232
233 void Clear() {
234 list_ = NULL;
235 last_ = NULL;
236 }
237
238 int length() {
239 int length = (list_ == NULL) ? 0 : list_->length();
240 return length + ((last_ == NULL) ? 0 : 1);
241 }
242
243 ZoneList<T*>* GetList() {
244 if (list_ == NULL) {
245 list_ = new ZoneList<T*>(initial_size);
246 }
247 if (last_ != NULL) {
248 list_->Add(last_);
249 last_ = NULL;
250 }
251 return list_;
252 }
253
254 private:
255 ZoneList<T*>* list_;
256 T* last_;
257};
258
259
260// Accumulates RegExp atoms and assertions into lists of terms and alternatives.
261class RegExpBuilder: public ZoneObject {
262 public:
263 RegExpBuilder();
264 void AddCharacter(uc16 character);
265 // "Adds" an empty expression. Does nothing except consume a
266 // following quantifier
267 void AddEmpty();
268 void AddAtom(RegExpTree* tree);
269 void AddAssertion(RegExpTree* tree);
270 void NewAlternative(); // '|'
271 void AddQuantifierToAtom(int min, int max, RegExpQuantifier::Type type);
272 RegExpTree* ToRegExp();
273
274 private:
275 void FlushCharacters();
276 void FlushText();
277 void FlushTerms();
Ben Murdoch8b112d22011-06-08 16:22:53 +0100278 Zone* zone() { return zone_; }
279
280 Zone* zone_;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800281 bool pending_empty_;
282 ZoneList<uc16>* characters_;
283 BufferedZoneList<RegExpTree, 2> terms_;
284 BufferedZoneList<RegExpTree, 2> text_;
285 BufferedZoneList<RegExpTree, 2> alternatives_;
286#ifdef DEBUG
287 enum {ADD_NONE, ADD_CHAR, ADD_TERM, ADD_ASSERT, ADD_ATOM} last_added_;
288#define LAST(x) last_added_ = x;
289#else
290#define LAST(x)
291#endif
292};
293
294
295class RegExpParser {
296 public:
297 RegExpParser(FlatStringReader* in,
298 Handle<String>* error,
299 bool multiline_mode);
Ben Murdochf87a2032010-10-22 12:50:53 +0100300
301 static bool ParseRegExp(FlatStringReader* input,
302 bool multiline,
303 RegExpCompileData* result);
304
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800305 RegExpTree* ParsePattern();
306 RegExpTree* ParseDisjunction();
307 RegExpTree* ParseGroup();
308 RegExpTree* ParseCharacterClass();
Ben Murdochf87a2032010-10-22 12:50:53 +0100309
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800310 // Parses a {...,...} quantifier and stores the range in the given
311 // out parameters.
312 bool ParseIntervalQuantifier(int* min_out, int* max_out);
Steve Block59151502010-09-22 15:07:15 +0100313
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800314 // Parses and returns a single escaped character. The character
315 // must not be 'b' or 'B' since they are usually handle specially.
316 uc32 ParseClassCharacterEscape();
317
318 // Checks whether the following is a length-digit hexadecimal number,
319 // and sets the value if it is.
320 bool ParseHexEscape(int length, uc32* value);
321
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800322 uc32 ParseOctalLiteral();
323
324 // Tries to parse the input as a back reference. If successful it
325 // stores the result in the output parameter and returns true. If
326 // it fails it will push back the characters read so the same characters
327 // can be reparsed.
328 bool ParseBackReferenceIndex(int* index_out);
329
330 CharacterRange ParseClassAtom(uc16* char_class);
331 RegExpTree* ReportError(Vector<const char> message);
332 void Advance();
333 void Advance(int dist);
334 void Reset(int pos);
335
336 // Reports whether the pattern might be used as a literal search string.
337 // Only use if the result of the parse is a single atom node.
338 bool simple();
339 bool contains_anchor() { return contains_anchor_; }
340 void set_contains_anchor() { contains_anchor_ = true; }
341 int captures_started() { return captures_ == NULL ? 0 : captures_->length(); }
342 int position() { return next_pos_ - 1; }
343 bool failed() { return failed_; }
344
345 static const int kMaxCaptures = 1 << 16;
346 static const uc32 kEndMarker = (1 << 21);
347
348 private:
349 enum SubexpressionType {
350 INITIAL,
351 CAPTURE, // All positive values represent captures.
352 POSITIVE_LOOKAHEAD,
353 NEGATIVE_LOOKAHEAD,
354 GROUPING
355 };
356
357 class RegExpParserState : public ZoneObject {
358 public:
359 RegExpParserState(RegExpParserState* previous_state,
360 SubexpressionType group_type,
361 int disjunction_capture_index)
362 : previous_state_(previous_state),
363 builder_(new RegExpBuilder()),
364 group_type_(group_type),
365 disjunction_capture_index_(disjunction_capture_index) {}
366 // Parser state of containing expression, if any.
367 RegExpParserState* previous_state() { return previous_state_; }
368 bool IsSubexpression() { return previous_state_ != NULL; }
369 // RegExpBuilder building this regexp's AST.
370 RegExpBuilder* builder() { return builder_; }
371 // Type of regexp being parsed (parenthesized group or entire regexp).
372 SubexpressionType group_type() { return group_type_; }
373 // Index in captures array of first capture in this sub-expression, if any.
374 // Also the capture index of this sub-expression itself, if group_type
375 // is CAPTURE.
376 int capture_index() { return disjunction_capture_index_; }
377
378 private:
379 // Linked list implementation of stack of states.
380 RegExpParserState* previous_state_;
381 // Builder for the stored disjunction.
382 RegExpBuilder* builder_;
383 // Stored disjunction type (capture, look-ahead or grouping), if any.
384 SubexpressionType group_type_;
385 // Stored disjunction's capture index (if any).
386 int disjunction_capture_index_;
387 };
388
Steve Block44f0eee2011-05-26 01:26:41 +0100389 Isolate* isolate() { return isolate_; }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100390 Zone* zone() { return isolate_->zone(); }
Steve Block44f0eee2011-05-26 01:26:41 +0100391
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800392 uc32 current() { return current_; }
393 bool has_more() { return has_more_; }
394 bool has_next() { return next_pos_ < in()->length(); }
395 uc32 Next();
396 FlatStringReader* in() { return in_; }
397 void ScanForCaptures();
398
Steve Block44f0eee2011-05-26 01:26:41 +0100399 Isolate* isolate_;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800400 Handle<String>* error_;
401 ZoneList<RegExpCapture*>* captures_;
402 FlatStringReader* in_;
403 uc32 current_;
404 int next_pos_;
405 // The capture count is only valid after we have scanned for captures.
406 int capture_count_;
407 bool has_more_;
408 bool multiline_;
409 bool simple_;
410 bool contains_anchor_;
411 bool is_scanned_for_captures_;
412 bool failed_;
413};
414
415// ----------------------------------------------------------------------------
416// JAVASCRIPT PARSING
417
418class Parser {
419 public:
420 Parser(Handle<Script> script,
421 bool allow_natives_syntax,
422 v8::Extension* extension,
423 ScriptDataImpl* pre_data);
424 virtual ~Parser() { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000425
Ben Murdochf87a2032010-10-22 12:50:53 +0100426 // Returns NULL if parsing failed.
427 FunctionLiteral* ParseProgram(Handle<String> source,
Steve Block1e0659c2011-05-24 12:43:12 +0100428 bool in_global_context,
429 StrictModeFlag strict_mode);
Ben Murdochf87a2032010-10-22 12:50:53 +0100430
Steve Block44f0eee2011-05-26 01:26:41 +0100431 FunctionLiteral* ParseLazy(CompilationInfo* info);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800432
433 void ReportMessageAt(Scanner::Location loc,
434 const char* message,
435 Vector<const char*> args);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100436 void ReportMessageAt(Scanner::Location loc,
437 const char* message,
438 Vector<Handle<String> > args);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000439 void SetHarmonyBlockScoping(bool block_scoping);
Ben Murdochf87a2032010-10-22 12:50:53 +0100440
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000441 private:
Steve Block1e0659c2011-05-24 12:43:12 +0100442 // Limit on number of function parameters is chosen arbitrarily.
443 // Code::Flags uses only the low 17 bits of num-parameters to
444 // construct a hashable id, so if more than 2^17 are allowed, this
445 // should be checked.
446 static const int kMaxNumFunctionParameters = 32766;
Steve Block053d10c2011-06-13 19:13:29 +0100447 static const int kMaxNumFunctionLocals = 32767;
Steve Block44f0eee2011-05-26 01:26:41 +0100448 FunctionLiteral* ParseLazy(CompilationInfo* info,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100449 UC16CharacterStream* source,
450 ZoneScope* zone_scope);
Ben Murdochf87a2032010-10-22 12:50:53 +0100451 enum Mode {
452 PARSE_LAZILY,
453 PARSE_EAGERLY
454 };
455
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000456 enum VariableDeclarationContext {
457 kSourceElement,
458 kStatement,
459 kForStatement
460 };
461
Steve Block44f0eee2011-05-26 01:26:41 +0100462 Isolate* isolate() { return isolate_; }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100463 Zone* zone() { return isolate_->zone(); }
Steve Block44f0eee2011-05-26 01:26:41 +0100464
Ben Murdochb0fe1622011-05-05 13:52:32 +0100465 // Called by ParseProgram after setting up the scanner.
466 FunctionLiteral* DoParseProgram(Handle<String> source,
467 bool in_global_context,
Steve Block1e0659c2011-05-24 12:43:12 +0100468 StrictModeFlag strict_mode,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100469 ZoneScope* zone_scope);
470
Ben Murdochf87a2032010-10-22 12:50:53 +0100471 // Report syntax error
472 void ReportUnexpectedToken(Token::Value token);
473 void ReportInvalidPreparseData(Handle<String> name, bool* ok);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800474 void ReportMessage(const char* message, Vector<const char*> args);
Ben Murdochf87a2032010-10-22 12:50:53 +0100475
476 bool inside_with() const { return with_nesting_level_ > 0; }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000477 JavaScriptScanner& scanner() { return scanner_; }
Ben Murdochf87a2032010-10-22 12:50:53 +0100478 Mode mode() const { return mode_; }
479 ScriptDataImpl* pre_data() const { return pre_data_; }
480
Steve Block44f0eee2011-05-26 01:26:41 +0100481 // Check if the given string is 'eval' or 'arguments'.
482 bool IsEvalOrArguments(Handle<String> string);
483
Ben Murdochf87a2032010-10-22 12:50:53 +0100484 // All ParseXXX functions take as the last argument an *ok parameter
485 // which is set to false if parsing failed; it is unchanged otherwise.
486 // By making the 'exception handling' explicit, we are forced to check
487 // for failure at the call sites.
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800488 void* ParseSourceElements(ZoneList<Statement*>* processor,
Ben Murdochf87a2032010-10-22 12:50:53 +0100489 int end_token, bool* ok);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000490 Statement* ParseSourceElement(ZoneStringList* labels, bool* ok);
Ben Murdochf87a2032010-10-22 12:50:53 +0100491 Statement* ParseStatement(ZoneStringList* labels, bool* ok);
492 Statement* ParseFunctionDeclaration(bool* ok);
493 Statement* ParseNativeDeclaration(bool* ok);
494 Block* ParseBlock(ZoneStringList* labels, bool* ok);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000495 Block* ParseScopedBlock(ZoneStringList* labels, bool* ok);
496 Block* ParseVariableStatement(VariableDeclarationContext var_context,
497 bool* ok);
498 Block* ParseVariableDeclarations(VariableDeclarationContext var_context,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000499 Handle<String>* out,
500 bool* ok);
Ben Murdochf87a2032010-10-22 12:50:53 +0100501 Statement* ParseExpressionOrLabelledStatement(ZoneStringList* labels,
502 bool* ok);
503 IfStatement* ParseIfStatement(ZoneStringList* labels, bool* ok);
504 Statement* ParseContinueStatement(bool* ok);
505 Statement* ParseBreakStatement(ZoneStringList* labels, bool* ok);
506 Statement* ParseReturnStatement(bool* ok);
Ben Murdochf87a2032010-10-22 12:50:53 +0100507 Statement* ParseWithStatement(ZoneStringList* labels, bool* ok);
508 CaseClause* ParseCaseClause(bool* default_seen_ptr, bool* ok);
509 SwitchStatement* ParseSwitchStatement(ZoneStringList* labels, bool* ok);
510 DoWhileStatement* ParseDoWhileStatement(ZoneStringList* labels, bool* ok);
511 WhileStatement* ParseWhileStatement(ZoneStringList* labels, bool* ok);
512 Statement* ParseForStatement(ZoneStringList* labels, bool* ok);
513 Statement* ParseThrowStatement(bool* ok);
514 Expression* MakeCatchContext(Handle<String> id, VariableProxy* value);
515 TryStatement* ParseTryStatement(bool* ok);
516 DebuggerStatement* ParseDebuggerStatement(bool* ok);
517
518 Expression* ParseExpression(bool accept_IN, bool* ok);
519 Expression* ParseAssignmentExpression(bool accept_IN, bool* ok);
520 Expression* ParseConditionalExpression(bool accept_IN, bool* ok);
521 Expression* ParseBinaryExpression(int prec, bool accept_IN, bool* ok);
522 Expression* ParseUnaryExpression(bool* ok);
523 Expression* ParsePostfixExpression(bool* ok);
524 Expression* ParseLeftHandSideExpression(bool* ok);
525 Expression* ParseNewExpression(bool* ok);
526 Expression* ParseMemberExpression(bool* ok);
527 Expression* ParseNewPrefix(PositionStack* stack, bool* ok);
528 Expression* ParseMemberWithNewPrefixesExpression(PositionStack* stack,
529 bool* ok);
530 Expression* ParsePrimaryExpression(bool* ok);
531 Expression* ParseArrayLiteral(bool* ok);
532 Expression* ParseObjectLiteral(bool* ok);
533 ObjectLiteral::Property* ParseObjectLiteralGetSet(bool is_getter, bool* ok);
534 Expression* ParseRegExpLiteral(bool seen_equal, bool* ok);
535
536 Expression* NewCompareNode(Token::Value op,
537 Expression* x,
538 Expression* y,
539 int position);
540
541 // Populate the constant properties fixed array for a materialized object
542 // literal.
543 void BuildObjectLiteralConstantProperties(
544 ZoneList<ObjectLiteral::Property*>* properties,
545 Handle<FixedArray> constants,
546 bool* is_simple,
547 bool* fast_elements,
548 int* depth);
549
550 // Populate the literals fixed array for a materialized array literal.
551 void BuildArrayLiteralBoilerplateLiterals(ZoneList<Expression*>* properties,
552 Handle<FixedArray> constants,
553 bool* is_simple,
554 int* depth);
555
556 // Decide if a property should be in the object boilerplate.
557 bool IsBoilerplateProperty(ObjectLiteral::Property* property);
558 // If the expression is a literal, return the literal value;
559 // if the expression is a materialized literal and is simple return a
560 // compile time value as encoded by CompileTimeValue::GetValue().
561 // Otherwise, return undefined literal as the placeholder
562 // in the object literal boilerplate.
563 Handle<Object> GetBoilerplateValue(Expression* expression);
564
Ben Murdochf87a2032010-10-22 12:50:53 +0100565 ZoneList<Expression*>* ParseArguments(bool* ok);
566 FunctionLiteral* ParseFunctionLiteral(Handle<String> var_name,
Steve Block1e0659c2011-05-24 12:43:12 +0100567 bool name_is_reserved,
Ben Murdochf87a2032010-10-22 12:50:53 +0100568 int function_token_position,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000569 FunctionLiteral::Type type,
Ben Murdochf87a2032010-10-22 12:50:53 +0100570 bool* ok);
Steve Blocka7e24c12009-10-30 11:49:00 +0000571
572
Ben Murdochf87a2032010-10-22 12:50:53 +0100573 // Magical syntax support.
574 Expression* ParseV8Intrinsic(bool* ok);
575
Ben Murdochb0fe1622011-05-05 13:52:32 +0100576 INLINE(Token::Value peek()) {
577 if (stack_overflow_) return Token::ILLEGAL;
578 return scanner().peek();
579 }
580
581 INLINE(Token::Value Next()) {
582 // BUG 1215673: Find a thread safe way to set a stack limit in
583 // pre-parse mode. Otherwise, we cannot safely pre-parse from other
584 // threads.
585 if (stack_overflow_) {
586 return Token::ILLEGAL;
587 }
Steve Block44f0eee2011-05-26 01:26:41 +0100588 if (StackLimitCheck(isolate()).HasOverflowed()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100589 // Any further calls to Next or peek will return the illegal token.
590 // The current call must return the next token, which might already
591 // have been peek'ed.
592 stack_overflow_ = true;
593 }
594 return scanner().Next();
595 }
596
Steve Block1e0659c2011-05-24 12:43:12 +0100597 bool peek_any_identifier();
598
Ben Murdochf87a2032010-10-22 12:50:53 +0100599 INLINE(void Consume(Token::Value token));
600 void Expect(Token::Value token, bool* ok);
601 bool Check(Token::Value token);
602 void ExpectSemicolon(bool* ok);
603
Steve Block9fac8402011-05-12 15:51:54 +0100604 Handle<String> LiteralString(PretenureFlag tenured) {
605 if (scanner().is_literal_ascii()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100606 return isolate_->factory()->NewStringFromAscii(
607 scanner().literal_ascii_string(), tenured);
Steve Block9fac8402011-05-12 15:51:54 +0100608 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100609 return isolate_->factory()->NewStringFromTwoByte(
610 scanner().literal_uc16_string(), tenured);
Steve Block9fac8402011-05-12 15:51:54 +0100611 }
612 }
613
614 Handle<String> NextLiteralString(PretenureFlag tenured) {
615 if (scanner().is_next_literal_ascii()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100616 return isolate_->factory()->NewStringFromAscii(
617 scanner().next_literal_ascii_string(), tenured);
Steve Block9fac8402011-05-12 15:51:54 +0100618 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100619 return isolate_->factory()->NewStringFromTwoByte(
620 scanner().next_literal_uc16_string(), tenured);
Steve Block9fac8402011-05-12 15:51:54 +0100621 }
622 }
623
Ben Murdochf87a2032010-10-22 12:50:53 +0100624 Handle<String> GetSymbol(bool* ok);
625
626 // Get odd-ball literals.
627 Literal* GetLiteralUndefined();
628 Literal* GetLiteralTheHole();
629 Literal* GetLiteralNumber(double value);
630
631 Handle<String> ParseIdentifier(bool* ok);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000632 Handle<String> ParseIdentifierOrStrictReservedWord(
633 bool* is_strict_reserved, bool* ok);
Ben Murdochf87a2032010-10-22 12:50:53 +0100634 Handle<String> ParseIdentifierName(bool* ok);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000635 Handle<String> ParseIdentifierNameOrGetOrSet(bool* is_get,
636 bool* is_set,
637 bool* ok);
Ben Murdochf87a2032010-10-22 12:50:53 +0100638
Steve Block1e0659c2011-05-24 12:43:12 +0100639 // Strict mode validation of LValue expressions
640 void CheckStrictModeLValue(Expression* expression,
641 const char* error,
642 bool* ok);
643
644 // Strict mode octal literal validation.
645 void CheckOctalLiteral(int beg_pos, int end_pos, bool* ok);
646
Ben Murdoch589d6972011-11-30 16:04:58 +0000647 // For harmony block scoping mode: Check if the scope has conflicting var/let
648 // declarations from different scopes. It covers for example
649 //
650 // function f() { { { var x; } let x; } }
651 // function g() { { var x; let x; } }
652 //
653 // The var declarations are hoisted to the function scope, but originate from
654 // a scope where the name has also been let bound or the var declaration is
655 // hoisted over such a scope.
656 void CheckConflictingVarDeclarations(Scope* scope, bool* ok);
657
Ben Murdochf87a2032010-10-22 12:50:53 +0100658 // Parser support
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800659 VariableProxy* Declare(Handle<String> name, Variable::Mode mode,
660 FunctionLiteral* fun,
661 bool resolve,
662 bool* ok);
Ben Murdochf87a2032010-10-22 12:50:53 +0100663
664 bool TargetStackContainsLabel(Handle<String> label);
665 BreakableStatement* LookupBreakTarget(Handle<String> label, bool* ok);
666 IterationStatement* LookupContinueTarget(Handle<String> label, bool* ok);
667
Ben Murdoch8b112d22011-06-08 16:22:53 +0100668 void RegisterTargetUse(Label* target, Target* stop);
Ben Murdochf87a2032010-10-22 12:50:53 +0100669
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800670 // Factory methods.
671
672 Statement* EmptyStatement() {
673 static v8::internal::EmptyStatement empty;
674 return &empty;
675 }
676
677 Scope* NewScope(Scope* parent, Scope::Type type, bool inside_with);
678
Steve Block9fac8402011-05-12 15:51:54 +0100679 Handle<String> LookupSymbol(int symbol_id);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800680
Steve Block9fac8402011-05-12 15:51:54 +0100681 Handle<String> LookupCachedSymbol(int symbol_id);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800682
683 Expression* NewCall(Expression* expression,
684 ZoneList<Expression*>* arguments,
685 int pos) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000686 return new(zone()) Call(isolate(), expression, arguments, pos);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800687 }
688
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000689 inline Literal* NewLiteral(Handle<Object> handle) {
690 return new(zone()) Literal(isolate(), handle);
691 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800692
Ben Murdochf87a2032010-10-22 12:50:53 +0100693 // Create a number literal.
694 Literal* NewNumberLiteral(double value);
695
696 // Generate AST node that throw a ReferenceError with the given type.
697 Expression* NewThrowReferenceError(Handle<String> type);
698
699 // Generate AST node that throw a SyntaxError with the given
700 // type. The first argument may be null (in the handle sense) in
701 // which case no arguments are passed to the constructor.
702 Expression* NewThrowSyntaxError(Handle<String> type, Handle<Object> first);
703
704 // Generate AST node that throw a TypeError with the given
705 // type. Both arguments must be non-null (in the handle sense).
706 Expression* NewThrowTypeError(Handle<String> type,
707 Handle<Object> first,
708 Handle<Object> second);
709
710 // Generic AST generator for throwing errors from compiled code.
711 Expression* NewThrowError(Handle<String> constructor,
712 Handle<String> type,
713 Vector< Handle<Object> > arguments);
714
Steve Block44f0eee2011-05-26 01:26:41 +0100715 Isolate* isolate_;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800716 ZoneList<Handle<String> > symbol_cache_;
Ben Murdochf87a2032010-10-22 12:50:53 +0100717
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800718 Handle<Script> script_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000719 JavaScriptScanner scanner_;
Ben Murdochf87a2032010-10-22 12:50:53 +0100720
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800721 Scope* top_scope_;
722 int with_nesting_level_;
Ben Murdochf87a2032010-10-22 12:50:53 +0100723
Steve Block44f0eee2011-05-26 01:26:41 +0100724 LexicalScope* lexical_scope_;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800725 Mode mode_;
726
727 Target* target_stack_; // for break, continue statements
728 bool allow_natives_syntax_;
729 v8::Extension* extension_;
730 bool is_pre_parsing_;
731 ScriptDataImpl* pre_data_;
732 FuncNameInferrer* fni_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100733 bool stack_overflow_;
Ben Murdochb8e0da22011-05-16 14:20:40 +0100734 // If true, the next (and immediately following) function literal is
735 // preceded by a parenthesis.
736 // Heuristically that means that the function will be called immediately,
737 // so never lazily compile it.
738 bool parenthesized_function_;
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000739 bool harmony_block_scoping_;
Steve Block44f0eee2011-05-26 01:26:41 +0100740
741 friend class LexicalScope;
Ben Murdochf87a2032010-10-22 12:50:53 +0100742};
Steve Blocka7e24c12009-10-30 11:49:00 +0000743
744
745// Support for handling complex values (array and object literals) that
746// can be fully handled at compile time.
747class CompileTimeValue: public AllStatic {
748 public:
749 enum Type {
Steve Block6ded16b2010-05-10 14:33:55 +0100750 OBJECT_LITERAL_FAST_ELEMENTS,
751 OBJECT_LITERAL_SLOW_ELEMENTS,
Steve Blocka7e24c12009-10-30 11:49:00 +0000752 ARRAY_LITERAL
753 };
754
755 static bool IsCompileTimeValue(Expression* expression);
756
Iain Merrick75681382010-08-19 15:07:18 +0100757 static bool ArrayLiteralElementNeedsInitialization(Expression* value);
758
Steve Blocka7e24c12009-10-30 11:49:00 +0000759 // Get the value as a compile time value.
760 static Handle<FixedArray> GetValue(Expression* expression);
761
762 // Get the type of a compile time value returned by GetValue().
763 static Type GetType(Handle<FixedArray> value);
764
765 // Get the elements array of a compile time value returned by GetValue().
766 static Handle<FixedArray> GetElements(Handle<FixedArray> value);
767
768 private:
769 static const int kTypeSlot = 0;
770 static const int kElementsSlot = 1;
771
772 DISALLOW_IMPLICIT_CONSTRUCTORS(CompileTimeValue);
773};
774
Steve Blocka7e24c12009-10-30 11:49:00 +0000775} } // namespace v8::internal
776
777#endif // V8_PARSER_H_