blob: 686dac85afa81bde06b5d07099e4268372e3e754 [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"
33#include "scanner.h"
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080034#include "scopes.h"
Ben Murdoch257744e2011-11-30 15:57:28 +000035#include "preparse-data-format.h"
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080036#include "preparse-data.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000037
38namespace v8 {
39namespace internal {
40
Ben Murdochf87a2032010-10-22 12:50:53 +010041class CompilationInfo;
42class FuncNameInferrer;
Ben Murdochf87a2032010-10-22 12:50:53 +010043class ParserLog;
44class PositionStack;
45class Target;
Steve Block44f0eee2011-05-26 01:26:41 +010046class LexicalScope;
Ben Murdochf87a2032010-10-22 12:50:53 +010047
48template <typename T> class ZoneListWrapper;
49
Steve Blocka7e24c12009-10-30 11:49:00 +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]; }
Steve Blocka7e24c12009-10-30 11:49:00 +000075 int end_pos() { return backing_[kEndPosOffset]; }
Steve Blocka7e24c12009-10-30 11:49:00 +000076 int literal_count() { return backing_[kLiteralCountOffset]; }
Steve Blocka7e24c12009-10-30 11:49:00 +000077 int property_count() { return backing_[kPropertyCountOffset]; }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000078 bool strict_mode() { return backing_[kStrictModeOffset] != 0; }
Kristian Monsen80d68ea2010-09-08 11:05:35 +010079
Steve Blocka7e24c12009-10-30 11:49:00 +000080 bool is_valid() { return backing_.length() > 0; }
81
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000082 static const int kSize = 5;
Steve Blocka7e24c12009-10-30 11:49:00 +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;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000090 static const int kStrictModeOffset = 4;
Steve Blocka7e24c12009-10-30 11:49:00 +000091};
92
93
94class ScriptDataImpl : public ScriptData {
95 public:
96 explicit ScriptDataImpl(Vector<unsigned> store)
97 : store_(store),
Kristian Monsen0d5e1162010-09-30 15:31:59 +010098 owns_store_(true) { }
Iain Merrick9ac36c92010-09-13 15:29:50 +010099
100 // Create an empty ScriptDataImpl that is guaranteed to not satisfy
101 // a SanityCheck.
102 ScriptDataImpl() : store_(Vector<unsigned>()), owns_store_(false) { }
103
Steve Blocka7e24c12009-10-30 11:49:00 +0000104 virtual ~ScriptDataImpl();
105 virtual int Length();
Leon Clarkef7060e22010-06-03 12:02:55 +0100106 virtual const char* Data();
Leon Clarkee46be812010-01-19 14:06:41 +0000107 virtual bool HasError();
Iain Merrick9ac36c92010-09-13 15:29:50 +0100108
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100109 void Initialize();
110 void ReadNextSymbolPosition();
111
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100112 FunctionEntry GetFunctionEntry(int start);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100113 int GetSymbolIdentifier();
Steve Blocka7e24c12009-10-30 11:49:00 +0000114 bool SanityCheck();
115
116 Scanner::Location MessageLocation();
117 const char* BuildMessage();
118 Vector<const char*> BuildArgs();
119
Iain Merrick9ac36c92010-09-13 15:29:50 +0100120 int symbol_count() {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800121 return (store_.length() > PreparseDataConstants::kHeaderSize)
122 ? store_[PreparseDataConstants::kSymbolCountOffset]
123 : 0;
Iain Merrick9ac36c92010-09-13 15:29:50 +0100124 }
125 // The following functions should only be called if SanityCheck has
126 // returned true.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800127 bool has_error() { return store_[PreparseDataConstants::kHasErrorOffset]; }
128 unsigned magic() { return store_[PreparseDataConstants::kMagicOffset]; }
129 unsigned version() { return store_[PreparseDataConstants::kVersionOffset]; }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100130
Steve Blocka7e24c12009-10-30 11:49:00 +0000131 private:
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100132 Vector<unsigned> store_;
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100133 unsigned char* symbol_data_;
134 unsigned char* symbol_data_end_;
Iain Merrick9ac36c92010-09-13 15:29:50 +0100135 int function_index_;
Iain Merrick9ac36c92010-09-13 15:29:50 +0100136 bool owns_store_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100137
Steve Blocka7e24c12009-10-30 11:49:00 +0000138 unsigned Read(int position);
139 unsigned* ReadAddress(int position);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100140 // Reads a number from the current symbols
141 int ReadNumber(byte** source);
Steve Blocka7e24c12009-10-30 11:49:00 +0000142
Iain Merrick9ac36c92010-09-13 15:29:50 +0100143 ScriptDataImpl(const char* backing_store, int length)
144 : store_(reinterpret_cast<unsigned*>(const_cast<char*>(backing_store)),
Ben Murdochf87a2032010-10-22 12:50:53 +0100145 length / static_cast<int>(sizeof(unsigned))),
Iain Merrick9ac36c92010-09-13 15:29:50 +0100146 owns_store_(false) {
Ben Murdochf87a2032010-10-22 12:50:53 +0100147 ASSERT_EQ(0, static_cast<int>(
148 reinterpret_cast<intptr_t>(backing_store) % sizeof(unsigned)));
Iain Merrick9ac36c92010-09-13 15:29:50 +0100149 }
150
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100151 // Read strings written by ParserRecorder::WriteString.
152 static const char* ReadString(unsigned* start, int* chars);
Iain Merrick9ac36c92010-09-13 15:29:50 +0100153
154 friend class ScriptData;
Steve Blocka7e24c12009-10-30 11:49:00 +0000155};
156
157
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800158class ParserApi {
159 public:
Ben Murdochf87a2032010-10-22 12:50:53 +0100160 // 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);
Steve Blocka7e24c12009-10-30 11:49:00 +0000164
Ben Murdochf87a2032010-10-22 12:50:53 +0100165 // Generic preparser generating full preparse data.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100166 static ScriptDataImpl* PreParse(UC16CharacterStream* source,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000167 v8::Extension* extension,
168 bool harmony_block_scoping);
Ben Murdochf87a2032010-10-22 12:50:53 +0100169
170 // Preparser that only does preprocessing that makes sense if only used
171 // immediately after.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100172 static ScriptDataImpl* PartialPreParse(UC16CharacterStream* source,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000173 v8::Extension* extension,
174 bool harmony_block_scoping);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800175};
176
177// ----------------------------------------------------------------------------
178// REGEXP PARSING
179
180// A BuffferedZoneList is an automatically growing list, just like (and backed
181// by) a ZoneList, that is optimized for the case of adding and removing
182// a single element. The last element added is stored outside the backing list,
183// and if no more than one element is ever added, the ZoneList isn't even
184// allocated.
185// Elements must not be NULL pointers.
186template <typename T, int initial_size>
187class BufferedZoneList {
188 public:
189 BufferedZoneList() : list_(NULL), last_(NULL) {}
190
191 // Adds element at end of list. This element is buffered and can
192 // be read using last() or removed using RemoveLast until a new Add or until
193 // RemoveLast or GetList has been called.
194 void Add(T* value) {
195 if (last_ != NULL) {
196 if (list_ == NULL) {
197 list_ = new ZoneList<T*>(initial_size);
198 }
199 list_->Add(last_);
200 }
201 last_ = value;
202 }
203
204 T* last() {
205 ASSERT(last_ != NULL);
206 return last_;
207 }
208
209 T* RemoveLast() {
210 ASSERT(last_ != NULL);
211 T* result = last_;
212 if ((list_ != NULL) && (list_->length() > 0))
213 last_ = list_->RemoveLast();
214 else
215 last_ = NULL;
216 return result;
217 }
218
219 T* Get(int i) {
220 ASSERT((0 <= i) && (i < length()));
221 if (list_ == NULL) {
222 ASSERT_EQ(0, i);
223 return last_;
224 } else {
225 if (i == list_->length()) {
226 ASSERT(last_ != NULL);
227 return last_;
228 } else {
229 return list_->at(i);
230 }
231 }
232 }
233
234 void Clear() {
235 list_ = NULL;
236 last_ = NULL;
237 }
238
239 int length() {
240 int length = (list_ == NULL) ? 0 : list_->length();
241 return length + ((last_ == NULL) ? 0 : 1);
242 }
243
244 ZoneList<T*>* GetList() {
245 if (list_ == NULL) {
246 list_ = new ZoneList<T*>(initial_size);
247 }
248 if (last_ != NULL) {
249 list_->Add(last_);
250 last_ = NULL;
251 }
252 return list_;
253 }
254
255 private:
256 ZoneList<T*>* list_;
257 T* last_;
258};
259
260
261// Accumulates RegExp atoms and assertions into lists of terms and alternatives.
262class RegExpBuilder: public ZoneObject {
263 public:
264 RegExpBuilder();
265 void AddCharacter(uc16 character);
266 // "Adds" an empty expression. Does nothing except consume a
267 // following quantifier
268 void AddEmpty();
269 void AddAtom(RegExpTree* tree);
270 void AddAssertion(RegExpTree* tree);
271 void NewAlternative(); // '|'
272 void AddQuantifierToAtom(int min, int max, RegExpQuantifier::Type type);
273 RegExpTree* ToRegExp();
274
275 private:
276 void FlushCharacters();
277 void FlushText();
278 void FlushTerms();
Ben Murdoch8b112d22011-06-08 16:22:53 +0100279 Zone* zone() { return zone_; }
280
281 Zone* zone_;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800282 bool pending_empty_;
283 ZoneList<uc16>* characters_;
284 BufferedZoneList<RegExpTree, 2> terms_;
285 BufferedZoneList<RegExpTree, 2> text_;
286 BufferedZoneList<RegExpTree, 2> alternatives_;
287#ifdef DEBUG
288 enum {ADD_NONE, ADD_CHAR, ADD_TERM, ADD_ASSERT, ADD_ATOM} last_added_;
289#define LAST(x) last_added_ = x;
290#else
291#define LAST(x)
292#endif
293};
294
295
296class RegExpParser {
297 public:
298 RegExpParser(FlatStringReader* in,
299 Handle<String>* error,
300 bool multiline_mode);
Ben Murdochf87a2032010-10-22 12:50:53 +0100301
302 static bool ParseRegExp(FlatStringReader* input,
303 bool multiline,
304 RegExpCompileData* result);
305
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800306 RegExpTree* ParsePattern();
307 RegExpTree* ParseDisjunction();
308 RegExpTree* ParseGroup();
309 RegExpTree* ParseCharacterClass();
Ben Murdochf87a2032010-10-22 12:50:53 +0100310
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800311 // Parses a {...,...} quantifier and stores the range in the given
312 // out parameters.
313 bool ParseIntervalQuantifier(int* min_out, int* max_out);
Steve Block59151502010-09-22 15:07:15 +0100314
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800315 // Parses and returns a single escaped character. The character
316 // must not be 'b' or 'B' since they are usually handle specially.
317 uc32 ParseClassCharacterEscape();
318
319 // Checks whether the following is a length-digit hexadecimal number,
320 // and sets the value if it is.
321 bool ParseHexEscape(int length, uc32* value);
322
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800323 uc32 ParseOctalLiteral();
324
325 // Tries to parse the input as a back reference. If successful it
326 // stores the result in the output parameter and returns true. If
327 // it fails it will push back the characters read so the same characters
328 // can be reparsed.
329 bool ParseBackReferenceIndex(int* index_out);
330
331 CharacterRange ParseClassAtom(uc16* char_class);
332 RegExpTree* ReportError(Vector<const char> message);
333 void Advance();
334 void Advance(int dist);
335 void Reset(int pos);
336
337 // Reports whether the pattern might be used as a literal search string.
338 // Only use if the result of the parse is a single atom node.
339 bool simple();
340 bool contains_anchor() { return contains_anchor_; }
341 void set_contains_anchor() { contains_anchor_ = true; }
342 int captures_started() { return captures_ == NULL ? 0 : captures_->length(); }
343 int position() { return next_pos_ - 1; }
344 bool failed() { return failed_; }
345
346 static const int kMaxCaptures = 1 << 16;
347 static const uc32 kEndMarker = (1 << 21);
348
349 private:
350 enum SubexpressionType {
351 INITIAL,
352 CAPTURE, // All positive values represent captures.
353 POSITIVE_LOOKAHEAD,
354 NEGATIVE_LOOKAHEAD,
355 GROUPING
356 };
357
358 class RegExpParserState : public ZoneObject {
359 public:
360 RegExpParserState(RegExpParserState* previous_state,
361 SubexpressionType group_type,
362 int disjunction_capture_index)
363 : previous_state_(previous_state),
364 builder_(new RegExpBuilder()),
365 group_type_(group_type),
366 disjunction_capture_index_(disjunction_capture_index) {}
367 // Parser state of containing expression, if any.
368 RegExpParserState* previous_state() { return previous_state_; }
369 bool IsSubexpression() { return previous_state_ != NULL; }
370 // RegExpBuilder building this regexp's AST.
371 RegExpBuilder* builder() { return builder_; }
372 // Type of regexp being parsed (parenthesized group or entire regexp).
373 SubexpressionType group_type() { return group_type_; }
374 // Index in captures array of first capture in this sub-expression, if any.
375 // Also the capture index of this sub-expression itself, if group_type
376 // is CAPTURE.
377 int capture_index() { return disjunction_capture_index_; }
378
379 private:
380 // Linked list implementation of stack of states.
381 RegExpParserState* previous_state_;
382 // Builder for the stored disjunction.
383 RegExpBuilder* builder_;
384 // Stored disjunction type (capture, look-ahead or grouping), if any.
385 SubexpressionType group_type_;
386 // Stored disjunction's capture index (if any).
387 int disjunction_capture_index_;
388 };
389
Steve Block44f0eee2011-05-26 01:26:41 +0100390 Isolate* isolate() { return isolate_; }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100391 Zone* zone() { return isolate_->zone(); }
Steve Block44f0eee2011-05-26 01:26:41 +0100392
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800393 uc32 current() { return current_; }
394 bool has_more() { return has_more_; }
395 bool has_next() { return next_pos_ < in()->length(); }
396 uc32 Next();
397 FlatStringReader* in() { return in_; }
398 void ScanForCaptures();
399
Steve Block44f0eee2011-05-26 01:26:41 +0100400 Isolate* isolate_;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800401 Handle<String>* error_;
402 ZoneList<RegExpCapture*>* captures_;
403 FlatStringReader* in_;
404 uc32 current_;
405 int next_pos_;
406 // The capture count is only valid after we have scanned for captures.
407 int capture_count_;
408 bool has_more_;
409 bool multiline_;
410 bool simple_;
411 bool contains_anchor_;
412 bool is_scanned_for_captures_;
413 bool failed_;
414};
415
416// ----------------------------------------------------------------------------
417// JAVASCRIPT PARSING
418
419class Parser {
420 public:
421 Parser(Handle<Script> script,
422 bool allow_natives_syntax,
423 v8::Extension* extension,
424 ScriptDataImpl* pre_data);
425 virtual ~Parser() { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000426
Ben Murdochf87a2032010-10-22 12:50:53 +0100427 // Returns NULL if parsing failed.
428 FunctionLiteral* ParseProgram(Handle<String> source,
Steve Block1e0659c2011-05-24 12:43:12 +0100429 bool in_global_context,
430 StrictModeFlag strict_mode);
Ben Murdochf87a2032010-10-22 12:50:53 +0100431
Steve Block44f0eee2011-05-26 01:26:41 +0100432 FunctionLiteral* ParseLazy(CompilationInfo* info);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800433
434 void ReportMessageAt(Scanner::Location loc,
435 const char* message,
436 Vector<const char*> args);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100437 void ReportMessageAt(Scanner::Location loc,
438 const char* message,
439 Vector<Handle<String> > args);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000440 void SetHarmonyBlockScoping(bool block_scoping);
Ben Murdochf87a2032010-10-22 12:50:53 +0100441
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000442 private:
Steve Block1e0659c2011-05-24 12:43:12 +0100443 // Limit on number of function parameters is chosen arbitrarily.
444 // Code::Flags uses only the low 17 bits of num-parameters to
445 // construct a hashable id, so if more than 2^17 are allowed, this
446 // should be checked.
447 static const int kMaxNumFunctionParameters = 32766;
Steve Block053d10c2011-06-13 19:13:29 +0100448 static const int kMaxNumFunctionLocals = 32767;
Steve Block44f0eee2011-05-26 01:26:41 +0100449 FunctionLiteral* ParseLazy(CompilationInfo* info,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100450 UC16CharacterStream* source,
451 ZoneScope* zone_scope);
Ben Murdochf87a2032010-10-22 12:50:53 +0100452 enum Mode {
453 PARSE_LAZILY,
454 PARSE_EAGERLY
455 };
456
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000457 enum VariableDeclarationContext {
458 kSourceElement,
459 kStatement,
460 kForStatement
461 };
462
Steve Block44f0eee2011-05-26 01:26:41 +0100463 Isolate* isolate() { return isolate_; }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100464 Zone* zone() { return isolate_->zone(); }
Steve Block44f0eee2011-05-26 01:26:41 +0100465
Ben Murdochb0fe1622011-05-05 13:52:32 +0100466 // Called by ParseProgram after setting up the scanner.
467 FunctionLiteral* DoParseProgram(Handle<String> source,
468 bool in_global_context,
Steve Block1e0659c2011-05-24 12:43:12 +0100469 StrictModeFlag strict_mode,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100470 ZoneScope* zone_scope);
471
Ben Murdochf87a2032010-10-22 12:50:53 +0100472 // Report syntax error
473 void ReportUnexpectedToken(Token::Value token);
474 void ReportInvalidPreparseData(Handle<String> name, bool* ok);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800475 void ReportMessage(const char* message, Vector<const char*> args);
Ben Murdochf87a2032010-10-22 12:50:53 +0100476
477 bool inside_with() const { return with_nesting_level_ > 0; }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000478 JavaScriptScanner& scanner() { return scanner_; }
Ben Murdochf87a2032010-10-22 12:50:53 +0100479 Mode mode() const { return mode_; }
480 ScriptDataImpl* pre_data() const { return pre_data_; }
481
Steve Block44f0eee2011-05-26 01:26:41 +0100482 // Check if the given string is 'eval' or 'arguments'.
483 bool IsEvalOrArguments(Handle<String> string);
484
Ben Murdochf87a2032010-10-22 12:50:53 +0100485 // All ParseXXX functions take as the last argument an *ok parameter
486 // which is set to false if parsing failed; it is unchanged otherwise.
487 // By making the 'exception handling' explicit, we are forced to check
488 // for failure at the call sites.
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800489 void* ParseSourceElements(ZoneList<Statement*>* processor,
Ben Murdochf87a2032010-10-22 12:50:53 +0100490 int end_token, bool* ok);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000491 Statement* ParseSourceElement(ZoneStringList* labels, bool* ok);
Ben Murdochf87a2032010-10-22 12:50:53 +0100492 Statement* ParseStatement(ZoneStringList* labels, bool* ok);
493 Statement* ParseFunctionDeclaration(bool* ok);
494 Statement* ParseNativeDeclaration(bool* ok);
495 Block* ParseBlock(ZoneStringList* labels, bool* ok);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000496 Block* ParseScopedBlock(ZoneStringList* labels, bool* ok);
497 Block* ParseVariableStatement(VariableDeclarationContext var_context,
498 bool* ok);
499 Block* ParseVariableDeclarations(VariableDeclarationContext var_context,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000500 Handle<String>* out,
501 bool* ok);
Ben Murdochf87a2032010-10-22 12:50:53 +0100502 Statement* ParseExpressionOrLabelledStatement(ZoneStringList* labels,
503 bool* ok);
504 IfStatement* ParseIfStatement(ZoneStringList* labels, bool* ok);
505 Statement* ParseContinueStatement(bool* ok);
506 Statement* ParseBreakStatement(ZoneStringList* labels, bool* ok);
507 Statement* ParseReturnStatement(bool* ok);
Ben Murdochf87a2032010-10-22 12:50:53 +0100508 Statement* ParseWithStatement(ZoneStringList* labels, bool* ok);
509 CaseClause* ParseCaseClause(bool* default_seen_ptr, bool* ok);
510 SwitchStatement* ParseSwitchStatement(ZoneStringList* labels, bool* ok);
511 DoWhileStatement* ParseDoWhileStatement(ZoneStringList* labels, bool* ok);
512 WhileStatement* ParseWhileStatement(ZoneStringList* labels, bool* ok);
513 Statement* ParseForStatement(ZoneStringList* labels, bool* ok);
514 Statement* ParseThrowStatement(bool* ok);
515 Expression* MakeCatchContext(Handle<String> id, VariableProxy* value);
516 TryStatement* ParseTryStatement(bool* ok);
517 DebuggerStatement* ParseDebuggerStatement(bool* ok);
518
519 Expression* ParseExpression(bool accept_IN, bool* ok);
520 Expression* ParseAssignmentExpression(bool accept_IN, bool* ok);
521 Expression* ParseConditionalExpression(bool accept_IN, bool* ok);
522 Expression* ParseBinaryExpression(int prec, bool accept_IN, bool* ok);
523 Expression* ParseUnaryExpression(bool* ok);
524 Expression* ParsePostfixExpression(bool* ok);
525 Expression* ParseLeftHandSideExpression(bool* ok);
526 Expression* ParseNewExpression(bool* ok);
527 Expression* ParseMemberExpression(bool* ok);
528 Expression* ParseNewPrefix(PositionStack* stack, bool* ok);
529 Expression* ParseMemberWithNewPrefixesExpression(PositionStack* stack,
530 bool* ok);
531 Expression* ParsePrimaryExpression(bool* ok);
532 Expression* ParseArrayLiteral(bool* ok);
533 Expression* ParseObjectLiteral(bool* ok);
534 ObjectLiteral::Property* ParseObjectLiteralGetSet(bool is_getter, bool* ok);
535 Expression* ParseRegExpLiteral(bool seen_equal, bool* ok);
536
537 Expression* NewCompareNode(Token::Value op,
538 Expression* x,
539 Expression* y,
540 int position);
541
542 // Populate the constant properties fixed array for a materialized object
543 // literal.
544 void BuildObjectLiteralConstantProperties(
545 ZoneList<ObjectLiteral::Property*>* properties,
546 Handle<FixedArray> constants,
547 bool* is_simple,
548 bool* fast_elements,
549 int* depth);
550
551 // Populate the literals fixed array for a materialized array literal.
552 void BuildArrayLiteralBoilerplateLiterals(ZoneList<Expression*>* properties,
553 Handle<FixedArray> constants,
554 bool* is_simple,
555 int* depth);
556
557 // Decide if a property should be in the object boilerplate.
558 bool IsBoilerplateProperty(ObjectLiteral::Property* property);
559 // If the expression is a literal, return the literal value;
560 // if the expression is a materialized literal and is simple return a
561 // compile time value as encoded by CompileTimeValue::GetValue().
562 // Otherwise, return undefined literal as the placeholder
563 // in the object literal boilerplate.
564 Handle<Object> GetBoilerplateValue(Expression* expression);
565
Ben Murdochf87a2032010-10-22 12:50:53 +0100566 ZoneList<Expression*>* ParseArguments(bool* ok);
567 FunctionLiteral* ParseFunctionLiteral(Handle<String> var_name,
Steve Block1e0659c2011-05-24 12:43:12 +0100568 bool name_is_reserved,
Ben Murdochf87a2032010-10-22 12:50:53 +0100569 int function_token_position,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000570 FunctionLiteral::Type type,
Ben Murdochf87a2032010-10-22 12:50:53 +0100571 bool* ok);
Steve Blocka7e24c12009-10-30 11:49:00 +0000572
573
Ben Murdochf87a2032010-10-22 12:50:53 +0100574 // Magical syntax support.
575 Expression* ParseV8Intrinsic(bool* ok);
576
Ben Murdochb0fe1622011-05-05 13:52:32 +0100577 INLINE(Token::Value peek()) {
578 if (stack_overflow_) return Token::ILLEGAL;
579 return scanner().peek();
580 }
581
582 INLINE(Token::Value Next()) {
583 // BUG 1215673: Find a thread safe way to set a stack limit in
584 // pre-parse mode. Otherwise, we cannot safely pre-parse from other
585 // threads.
586 if (stack_overflow_) {
587 return Token::ILLEGAL;
588 }
Steve Block44f0eee2011-05-26 01:26:41 +0100589 if (StackLimitCheck(isolate()).HasOverflowed()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100590 // Any further calls to Next or peek will return the illegal token.
591 // The current call must return the next token, which might already
592 // have been peek'ed.
593 stack_overflow_ = true;
594 }
595 return scanner().Next();
596 }
597
Steve Block1e0659c2011-05-24 12:43:12 +0100598 bool peek_any_identifier();
599
Ben Murdochf87a2032010-10-22 12:50:53 +0100600 INLINE(void Consume(Token::Value token));
601 void Expect(Token::Value token, bool* ok);
602 bool Check(Token::Value token);
603 void ExpectSemicolon(bool* ok);
604
Steve Block9fac8402011-05-12 15:51:54 +0100605 Handle<String> LiteralString(PretenureFlag tenured) {
606 if (scanner().is_literal_ascii()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100607 return isolate_->factory()->NewStringFromAscii(
608 scanner().literal_ascii_string(), tenured);
Steve Block9fac8402011-05-12 15:51:54 +0100609 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100610 return isolate_->factory()->NewStringFromTwoByte(
611 scanner().literal_uc16_string(), tenured);
Steve Block9fac8402011-05-12 15:51:54 +0100612 }
613 }
614
615 Handle<String> NextLiteralString(PretenureFlag tenured) {
616 if (scanner().is_next_literal_ascii()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100617 return isolate_->factory()->NewStringFromAscii(
618 scanner().next_literal_ascii_string(), tenured);
Steve Block9fac8402011-05-12 15:51:54 +0100619 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100620 return isolate_->factory()->NewStringFromTwoByte(
621 scanner().next_literal_uc16_string(), tenured);
Steve Block9fac8402011-05-12 15:51:54 +0100622 }
623 }
624
Ben Murdochf87a2032010-10-22 12:50:53 +0100625 Handle<String> GetSymbol(bool* ok);
626
627 // Get odd-ball literals.
628 Literal* GetLiteralUndefined();
629 Literal* GetLiteralTheHole();
630 Literal* GetLiteralNumber(double value);
631
632 Handle<String> ParseIdentifier(bool* ok);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000633 Handle<String> ParseIdentifierOrStrictReservedWord(
634 bool* is_strict_reserved, bool* ok);
Ben Murdochf87a2032010-10-22 12:50:53 +0100635 Handle<String> ParseIdentifierName(bool* ok);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000636 Handle<String> ParseIdentifierNameOrGetOrSet(bool* is_get,
637 bool* is_set,
638 bool* ok);
Ben Murdochf87a2032010-10-22 12:50:53 +0100639
Steve Block1e0659c2011-05-24 12:43:12 +0100640 // Strict mode validation of LValue expressions
641 void CheckStrictModeLValue(Expression* expression,
642 const char* error,
643 bool* ok);
644
645 // Strict mode octal literal validation.
646 void CheckOctalLiteral(int beg_pos, int end_pos, bool* ok);
647
Ben Murdochf87a2032010-10-22 12:50:53 +0100648 // Parser support
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800649 VariableProxy* Declare(Handle<String> name, Variable::Mode mode,
650 FunctionLiteral* fun,
651 bool resolve,
652 bool* ok);
Ben Murdochf87a2032010-10-22 12:50:53 +0100653
654 bool TargetStackContainsLabel(Handle<String> label);
655 BreakableStatement* LookupBreakTarget(Handle<String> label, bool* ok);
656 IterationStatement* LookupContinueTarget(Handle<String> label, bool* ok);
657
Ben Murdoch8b112d22011-06-08 16:22:53 +0100658 void RegisterTargetUse(Label* target, Target* stop);
Ben Murdochf87a2032010-10-22 12:50:53 +0100659
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800660 // Factory methods.
661
662 Statement* EmptyStatement() {
663 static v8::internal::EmptyStatement empty;
664 return &empty;
665 }
666
667 Scope* NewScope(Scope* parent, Scope::Type type, bool inside_with);
668
Steve Block9fac8402011-05-12 15:51:54 +0100669 Handle<String> LookupSymbol(int symbol_id);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800670
Steve Block9fac8402011-05-12 15:51:54 +0100671 Handle<String> LookupCachedSymbol(int symbol_id);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800672
673 Expression* NewCall(Expression* expression,
674 ZoneList<Expression*>* arguments,
675 int pos) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000676 return new(zone()) Call(isolate(), expression, arguments, pos);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800677 }
678
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000679 inline Literal* NewLiteral(Handle<Object> handle) {
680 return new(zone()) Literal(isolate(), handle);
681 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800682
Ben Murdochf87a2032010-10-22 12:50:53 +0100683 // Create a number literal.
684 Literal* NewNumberLiteral(double value);
685
686 // Generate AST node that throw a ReferenceError with the given type.
687 Expression* NewThrowReferenceError(Handle<String> type);
688
689 // Generate AST node that throw a SyntaxError with the given
690 // type. The first argument may be null (in the handle sense) in
691 // which case no arguments are passed to the constructor.
692 Expression* NewThrowSyntaxError(Handle<String> type, Handle<Object> first);
693
694 // Generate AST node that throw a TypeError with the given
695 // type. Both arguments must be non-null (in the handle sense).
696 Expression* NewThrowTypeError(Handle<String> type,
697 Handle<Object> first,
698 Handle<Object> second);
699
700 // Generic AST generator for throwing errors from compiled code.
701 Expression* NewThrowError(Handle<String> constructor,
702 Handle<String> type,
703 Vector< Handle<Object> > arguments);
704
Steve Block44f0eee2011-05-26 01:26:41 +0100705 Isolate* isolate_;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800706 ZoneList<Handle<String> > symbol_cache_;
Ben Murdochf87a2032010-10-22 12:50:53 +0100707
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800708 Handle<Script> script_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000709 JavaScriptScanner scanner_;
Ben Murdochf87a2032010-10-22 12:50:53 +0100710
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800711 Scope* top_scope_;
712 int with_nesting_level_;
Ben Murdochf87a2032010-10-22 12:50:53 +0100713
Steve Block44f0eee2011-05-26 01:26:41 +0100714 LexicalScope* lexical_scope_;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800715 Mode mode_;
716
717 Target* target_stack_; // for break, continue statements
718 bool allow_natives_syntax_;
719 v8::Extension* extension_;
720 bool is_pre_parsing_;
721 ScriptDataImpl* pre_data_;
722 FuncNameInferrer* fni_;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100723 bool stack_overflow_;
Ben Murdochb8e0da22011-05-16 14:20:40 +0100724 // If true, the next (and immediately following) function literal is
725 // preceded by a parenthesis.
726 // Heuristically that means that the function will be called immediately,
727 // so never lazily compile it.
728 bool parenthesized_function_;
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000729 bool harmony_block_scoping_;
Steve Block44f0eee2011-05-26 01:26:41 +0100730
731 friend class LexicalScope;
Ben Murdochf87a2032010-10-22 12:50:53 +0100732};
Steve Blocka7e24c12009-10-30 11:49:00 +0000733
734
735// Support for handling complex values (array and object literals) that
736// can be fully handled at compile time.
737class CompileTimeValue: public AllStatic {
738 public:
739 enum Type {
Steve Block6ded16b2010-05-10 14:33:55 +0100740 OBJECT_LITERAL_FAST_ELEMENTS,
741 OBJECT_LITERAL_SLOW_ELEMENTS,
Steve Blocka7e24c12009-10-30 11:49:00 +0000742 ARRAY_LITERAL
743 };
744
745 static bool IsCompileTimeValue(Expression* expression);
746
Iain Merrick75681382010-08-19 15:07:18 +0100747 static bool ArrayLiteralElementNeedsInitialization(Expression* value);
748
Steve Blocka7e24c12009-10-30 11:49:00 +0000749 // Get the value as a compile time value.
750 static Handle<FixedArray> GetValue(Expression* expression);
751
752 // Get the type of a compile time value returned by GetValue().
753 static Type GetType(Handle<FixedArray> value);
754
755 // Get the elements array of a compile time value returned by GetValue().
756 static Handle<FixedArray> GetElements(Handle<FixedArray> value);
757
758 private:
759 static const int kTypeSlot = 0;
760 static const int kElementsSlot = 1;
761
762 DISALLOW_IMPLICIT_CONSTRUCTORS(CompileTimeValue);
763};
764
Steve Blocka7e24c12009-10-30 11:49:00 +0000765} } // namespace v8::internal
766
767#endif // V8_PARSER_H_