Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / v8 / src / parser.h
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 //     * Redistributions of source code must retain the above copyright
7 //       notice, this list of conditions and the following disclaimer.
8 //     * Redistributions in binary form must reproduce the above
9 //       copyright notice, this list of conditions and the following
10 //       disclaimer in the documentation and/or other materials provided
11 //       with the distribution.
12 //     * Neither the name of Google Inc. nor the names of its
13 //       contributors may be used to endorse or promote products derived
14 //       from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #ifndef V8_PARSER_H_
29 #define V8_PARSER_H_
30
31 #include "allocation.h"
32 #include "ast.h"
33 #include "preparse-data-format.h"
34 #include "preparse-data.h"
35 #include "scopes.h"
36 #include "preparser.h"
37
38 namespace v8 {
39 namespace internal {
40
41 class CompilationInfo;
42 class FuncNameInferrer;
43 class ParserLog;
44 class PositionStack;
45 class Target;
46
47 template <typename T> class ZoneListWrapper;
48
49
50 class FunctionEntry BASE_EMBEDDED {
51  public:
52   enum {
53     kStartPositionIndex,
54     kEndPositionIndex,
55     kLiteralCountIndex,
56     kPropertyCountIndex,
57     kLanguageModeIndex,
58     kSize
59   };
60
61   explicit FunctionEntry(Vector<unsigned> backing)
62     : backing_(backing) { }
63
64   FunctionEntry() : backing_() { }
65
66   int start_pos() { return backing_[kStartPositionIndex]; }
67   int end_pos() { return backing_[kEndPositionIndex]; }
68   int literal_count() { return backing_[kLiteralCountIndex]; }
69   int property_count() { return backing_[kPropertyCountIndex]; }
70   LanguageMode language_mode() {
71     ASSERT(backing_[kLanguageModeIndex] == CLASSIC_MODE ||
72            backing_[kLanguageModeIndex] == STRICT_MODE ||
73            backing_[kLanguageModeIndex] == EXTENDED_MODE);
74     return static_cast<LanguageMode>(backing_[kLanguageModeIndex]);
75   }
76
77   bool is_valid() { return !backing_.is_empty(); }
78
79  private:
80   Vector<unsigned> backing_;
81 };
82
83
84 class ScriptDataImpl : public ScriptData {
85  public:
86   explicit ScriptDataImpl(Vector<unsigned> store)
87       : store_(store),
88         owns_store_(true) { }
89
90   // Create an empty ScriptDataImpl that is guaranteed to not satisfy
91   // a SanityCheck.
92   ScriptDataImpl() : owns_store_(false) { }
93
94   virtual ~ScriptDataImpl();
95   virtual int Length();
96   virtual const char* Data();
97   virtual bool HasError();
98
99   void Initialize();
100   void ReadNextSymbolPosition();
101
102   FunctionEntry GetFunctionEntry(int start);
103   int GetSymbolIdentifier();
104   bool SanityCheck();
105
106   Scanner::Location MessageLocation();
107   const char* BuildMessage();
108   Vector<const char*> BuildArgs();
109
110   int symbol_count() {
111     return (store_.length() > PreparseDataConstants::kHeaderSize)
112         ? store_[PreparseDataConstants::kSymbolCountOffset]
113         : 0;
114   }
115   // The following functions should only be called if SanityCheck has
116   // returned true.
117   bool has_error() { return store_[PreparseDataConstants::kHasErrorOffset]; }
118   unsigned magic() { return store_[PreparseDataConstants::kMagicOffset]; }
119   unsigned version() { return store_[PreparseDataConstants::kVersionOffset]; }
120
121  private:
122   Vector<unsigned> store_;
123   unsigned char* symbol_data_;
124   unsigned char* symbol_data_end_;
125   int function_index_;
126   bool owns_store_;
127
128   unsigned Read(int position);
129   unsigned* ReadAddress(int position);
130   // Reads a number from the current symbols
131   int ReadNumber(byte** source);
132
133   ScriptDataImpl(const char* backing_store, int length)
134       : store_(reinterpret_cast<unsigned*>(const_cast<char*>(backing_store)),
135                length / static_cast<int>(sizeof(unsigned))),
136         owns_store_(false) {
137     ASSERT_EQ(0, static_cast<int>(
138         reinterpret_cast<intptr_t>(backing_store) % sizeof(unsigned)));
139   }
140
141   // Read strings written by ParserRecorder::WriteString.
142   static const char* ReadString(unsigned* start, int* chars);
143
144   friend class ScriptData;
145 };
146
147
148 class PreParserApi {
149  public:
150   // Pre-parse a character stream and return full preparse data.
151   //
152   // This interface is here instead of in preparser.h because it instantiates a
153   // preparser recorder object that is suited to the parser's purposes.  Also,
154   // the preparser doesn't know about ScriptDataImpl.
155   static ScriptDataImpl* PreParse(Isolate* isolate,
156                                   Utf16CharacterStream* source);
157 };
158
159
160 // ----------------------------------------------------------------------------
161 // REGEXP PARSING
162
163 // A BufferedZoneList is an automatically growing list, just like (and backed
164 // by) a ZoneList, that is optimized for the case of adding and removing
165 // a single element. The last element added is stored outside the backing list,
166 // and if no more than one element is ever added, the ZoneList isn't even
167 // allocated.
168 // Elements must not be NULL pointers.
169 template <typename T, int initial_size>
170 class BufferedZoneList {
171  public:
172   BufferedZoneList() : list_(NULL), last_(NULL) {}
173
174   // Adds element at end of list. This element is buffered and can
175   // be read using last() or removed using RemoveLast until a new Add or until
176   // RemoveLast or GetList has been called.
177   void Add(T* value, Zone* zone) {
178     if (last_ != NULL) {
179       if (list_ == NULL) {
180         list_ = new(zone) ZoneList<T*>(initial_size, zone);
181       }
182       list_->Add(last_, zone);
183     }
184     last_ = value;
185   }
186
187   T* last() {
188     ASSERT(last_ != NULL);
189     return last_;
190   }
191
192   T* RemoveLast() {
193     ASSERT(last_ != NULL);
194     T* result = last_;
195     if ((list_ != NULL) && (list_->length() > 0))
196       last_ = list_->RemoveLast();
197     else
198       last_ = NULL;
199     return result;
200   }
201
202   T* Get(int i) {
203     ASSERT((0 <= i) && (i < length()));
204     if (list_ == NULL) {
205       ASSERT_EQ(0, i);
206       return last_;
207     } else {
208       if (i == list_->length()) {
209         ASSERT(last_ != NULL);
210         return last_;
211       } else {
212         return list_->at(i);
213       }
214     }
215   }
216
217   void Clear() {
218     list_ = NULL;
219     last_ = NULL;
220   }
221
222   int length() {
223     int length = (list_ == NULL) ? 0 : list_->length();
224     return length + ((last_ == NULL) ? 0 : 1);
225   }
226
227   ZoneList<T*>* GetList(Zone* zone) {
228     if (list_ == NULL) {
229       list_ = new(zone) ZoneList<T*>(initial_size, zone);
230     }
231     if (last_ != NULL) {
232       list_->Add(last_, zone);
233       last_ = NULL;
234     }
235     return list_;
236   }
237
238  private:
239   ZoneList<T*>* list_;
240   T* last_;
241 };
242
243
244 // Accumulates RegExp atoms and assertions into lists of terms and alternatives.
245 class RegExpBuilder: public ZoneObject {
246  public:
247   explicit RegExpBuilder(Zone* zone);
248   void AddCharacter(uc16 character);
249   // "Adds" an empty expression. Does nothing except consume a
250   // following quantifier
251   void AddEmpty();
252   void AddAtom(RegExpTree* tree);
253   void AddAssertion(RegExpTree* tree);
254   void NewAlternative();  // '|'
255   void AddQuantifierToAtom(
256       int min, int max, RegExpQuantifier::QuantifierType type);
257   RegExpTree* ToRegExp();
258
259  private:
260   void FlushCharacters();
261   void FlushText();
262   void FlushTerms();
263   Zone* zone() const { return zone_; }
264
265   Zone* zone_;
266   bool pending_empty_;
267   ZoneList<uc16>* characters_;
268   BufferedZoneList<RegExpTree, 2> terms_;
269   BufferedZoneList<RegExpTree, 2> text_;
270   BufferedZoneList<RegExpTree, 2> alternatives_;
271 #ifdef DEBUG
272   enum {ADD_NONE, ADD_CHAR, ADD_TERM, ADD_ASSERT, ADD_ATOM} last_added_;
273 #define LAST(x) last_added_ = x;
274 #else
275 #define LAST(x)
276 #endif
277 };
278
279
280 class RegExpParser BASE_EMBEDDED {
281  public:
282   RegExpParser(FlatStringReader* in,
283                Handle<String>* error,
284                bool multiline_mode,
285                Zone* zone);
286
287   static bool ParseRegExp(FlatStringReader* input,
288                           bool multiline,
289                           RegExpCompileData* result,
290                           Zone* zone);
291
292   RegExpTree* ParsePattern();
293   RegExpTree* ParseDisjunction();
294   RegExpTree* ParseGroup();
295   RegExpTree* ParseCharacterClass();
296
297   // Parses a {...,...} quantifier and stores the range in the given
298   // out parameters.
299   bool ParseIntervalQuantifier(int* min_out, int* max_out);
300
301   // Parses and returns a single escaped character.  The character
302   // must not be 'b' or 'B' since they are usually handle specially.
303   uc32 ParseClassCharacterEscape();
304
305   // Checks whether the following is a length-digit hexadecimal number,
306   // and sets the value if it is.
307   bool ParseHexEscape(int length, uc32* value);
308
309   uc32 ParseOctalLiteral();
310
311   // Tries to parse the input as a back reference.  If successful it
312   // stores the result in the output parameter and returns true.  If
313   // it fails it will push back the characters read so the same characters
314   // can be reparsed.
315   bool ParseBackReferenceIndex(int* index_out);
316
317   CharacterRange ParseClassAtom(uc16* char_class);
318   RegExpTree* ReportError(Vector<const char> message);
319   void Advance();
320   void Advance(int dist);
321   void Reset(int pos);
322
323   // Reports whether the pattern might be used as a literal search string.
324   // Only use if the result of the parse is a single atom node.
325   bool simple();
326   bool contains_anchor() { return contains_anchor_; }
327   void set_contains_anchor() { contains_anchor_ = true; }
328   int captures_started() { return captures_ == NULL ? 0 : captures_->length(); }
329   int position() { return next_pos_ - 1; }
330   bool failed() { return failed_; }
331
332   static const int kMaxCaptures = 1 << 16;
333   static const uc32 kEndMarker = (1 << 21);
334
335  private:
336   enum SubexpressionType {
337     INITIAL,
338     CAPTURE,  // All positive values represent captures.
339     POSITIVE_LOOKAHEAD,
340     NEGATIVE_LOOKAHEAD,
341     GROUPING
342   };
343
344   class RegExpParserState : public ZoneObject {
345    public:
346     RegExpParserState(RegExpParserState* previous_state,
347                       SubexpressionType group_type,
348                       int disjunction_capture_index,
349                       Zone* zone)
350         : previous_state_(previous_state),
351           builder_(new(zone) RegExpBuilder(zone)),
352           group_type_(group_type),
353           disjunction_capture_index_(disjunction_capture_index) {}
354     // Parser state of containing expression, if any.
355     RegExpParserState* previous_state() { return previous_state_; }
356     bool IsSubexpression() { return previous_state_ != NULL; }
357     // RegExpBuilder building this regexp's AST.
358     RegExpBuilder* builder() { return builder_; }
359     // Type of regexp being parsed (parenthesized group or entire regexp).
360     SubexpressionType group_type() { return group_type_; }
361     // Index in captures array of first capture in this sub-expression, if any.
362     // Also the capture index of this sub-expression itself, if group_type
363     // is CAPTURE.
364     int capture_index() { return disjunction_capture_index_; }
365
366    private:
367     // Linked list implementation of stack of states.
368     RegExpParserState* previous_state_;
369     // Builder for the stored disjunction.
370     RegExpBuilder* builder_;
371     // Stored disjunction type (capture, look-ahead or grouping), if any.
372     SubexpressionType group_type_;
373     // Stored disjunction's capture index (if any).
374     int disjunction_capture_index_;
375   };
376
377   Isolate* isolate() { return isolate_; }
378   Zone* zone() const { return zone_; }
379
380   uc32 current() { return current_; }
381   bool has_more() { return has_more_; }
382   bool has_next() { return next_pos_ < in()->length(); }
383   uc32 Next();
384   FlatStringReader* in() { return in_; }
385   void ScanForCaptures();
386
387   Isolate* isolate_;
388   Zone* zone_;
389   Handle<String>* error_;
390   ZoneList<RegExpCapture*>* captures_;
391   FlatStringReader* in_;
392   uc32 current_;
393   int next_pos_;
394   // The capture count is only valid after we have scanned for captures.
395   int capture_count_;
396   bool has_more_;
397   bool multiline_;
398   bool simple_;
399   bool contains_anchor_;
400   bool is_scanned_for_captures_;
401   bool failed_;
402 };
403
404 // ----------------------------------------------------------------------------
405 // JAVASCRIPT PARSING
406
407 // Forward declaration.
408 class SingletonLogger;
409
410 class Parser : public ParserBase {
411  public:
412   explicit Parser(CompilationInfo* info);
413   ~Parser() {
414     delete reusable_preparser_;
415     reusable_preparser_ = NULL;
416   }
417
418   // Parses the source code represented by the compilation info and sets its
419   // function literal.  Returns false (and deallocates any allocated AST
420   // nodes) if parsing failed.
421   static bool Parse(CompilationInfo* info,
422                     bool allow_lazy = false) {
423     Parser parser(info);
424     parser.set_allow_lazy(allow_lazy);
425     return parser.Parse();
426   }
427   bool Parse();
428
429  private:
430   static const int kMaxNumFunctionLocals = 131071;  // 2^17-1
431
432   enum Mode {
433     PARSE_LAZILY,
434     PARSE_EAGERLY
435   };
436
437   enum VariableDeclarationContext {
438     kModuleElement,
439     kBlockElement,
440     kStatement,
441     kForStatement
442   };
443
444   // If a list of variable declarations includes any initializers.
445   enum VariableDeclarationProperties {
446     kHasInitializers,
447     kHasNoInitializers
448   };
449
450   class BlockState;
451
452   class FunctionState BASE_EMBEDDED {
453    public:
454     FunctionState(Parser* parser, Scope* scope);
455     ~FunctionState();
456
457     int NextMaterializedLiteralIndex() {
458       return next_materialized_literal_index_++;
459     }
460     int materialized_literal_count() {
461       return next_materialized_literal_index_ - JSFunction::kLiteralsPrefixSize;
462     }
463
464     int NextHandlerIndex() { return next_handler_index_++; }
465     int handler_count() { return next_handler_index_; }
466
467     void AddProperty() { expected_property_count_++; }
468     int expected_property_count() { return expected_property_count_; }
469
470     void set_generator_object_variable(Variable *variable) {
471       ASSERT(variable != NULL);
472       ASSERT(!is_generator());
473       generator_object_variable_ = variable;
474     }
475     Variable* generator_object_variable() const {
476       return generator_object_variable_;
477     }
478     bool is_generator() const {
479       return generator_object_variable_ != NULL;
480     }
481
482     AstNodeFactory<AstConstructionVisitor>* factory() { return &factory_; }
483
484    private:
485     // Used to assign an index to each literal that needs materialization in
486     // the function.  Includes regexp literals, and boilerplate for object and
487     // array literals.
488     int next_materialized_literal_index_;
489
490     // Used to assign a per-function index to try and catch handlers.
491     int next_handler_index_;
492
493     // Properties count estimation.
494     int expected_property_count_;
495
496     // For generators, the variable that holds the generator object.  This
497     // variable is used by yield expressions and return statements.  NULL
498     // indicates that this function is not a generator.
499     Variable* generator_object_variable_;
500
501     Parser* parser_;
502     FunctionState* outer_function_state_;
503     Scope* outer_scope_;
504     int saved_ast_node_id_;
505     AstNodeFactory<AstConstructionVisitor> factory_;
506   };
507
508   class ParsingModeScope BASE_EMBEDDED {
509    public:
510     ParsingModeScope(Parser* parser, Mode mode)
511         : parser_(parser),
512           old_mode_(parser->mode()) {
513       parser_->mode_ = mode;
514     }
515     ~ParsingModeScope() {
516       parser_->mode_ = old_mode_;
517     }
518
519    private:
520     Parser* parser_;
521     Mode old_mode_;
522   };
523
524   virtual bool is_classic_mode() {
525     return top_scope_->is_classic_mode();
526   }
527
528   // Returns NULL if parsing failed.
529   FunctionLiteral* ParseProgram();
530
531   FunctionLiteral* ParseLazy();
532   FunctionLiteral* ParseLazy(Utf16CharacterStream* source);
533
534   Isolate* isolate() { return isolate_; }
535   Zone* zone() const { return zone_; }
536   CompilationInfo* info() const { return info_; }
537
538   // Called by ParseProgram after setting up the scanner.
539   FunctionLiteral* DoParseProgram(CompilationInfo* info,
540                                   Handle<String> source);
541
542   // Report syntax error
543   void ReportInvalidPreparseData(Handle<String> name, bool* ok);
544   void ReportMessage(const char* message, Vector<const char*> args);
545   void ReportMessage(const char* message, Vector<Handle<String> > args);
546   void ReportMessageAt(Scanner::Location location, const char* type) {
547     ReportMessageAt(location, type, Vector<const char*>::empty());
548   }
549   void ReportMessageAt(Scanner::Location loc,
550                        const char* message,
551                        Vector<const char*> args);
552   void ReportMessageAt(Scanner::Location loc,
553                        const char* message,
554                        Vector<Handle<String> > args);
555
556   void set_pre_parse_data(ScriptDataImpl *data) {
557     pre_parse_data_ = data;
558     symbol_cache_.Initialize(data ? data->symbol_count() : 0, zone());
559   }
560
561   bool inside_with() const { return top_scope_->inside_with(); }
562   Scanner& scanner()  { return scanner_; }
563   Mode mode() const { return mode_; }
564   ScriptDataImpl* pre_parse_data() const { return pre_parse_data_; }
565   bool is_extended_mode() {
566     ASSERT(top_scope_ != NULL);
567     return top_scope_->is_extended_mode();
568   }
569   Scope* DeclarationScope(VariableMode mode) {
570     return IsLexicalVariableMode(mode)
571         ? top_scope_ : top_scope_->DeclarationScope();
572   }
573
574   // Check if the given string is 'eval' or 'arguments'.
575   bool IsEvalOrArguments(Handle<String> string);
576
577   // All ParseXXX functions take as the last argument an *ok parameter
578   // which is set to false if parsing failed; it is unchanged otherwise.
579   // By making the 'exception handling' explicit, we are forced to check
580   // for failure at the call sites.
581   void* ParseSourceElements(ZoneList<Statement*>* processor, int end_token,
582                             bool is_eval, bool is_global, bool* ok);
583   Statement* ParseModuleElement(ZoneStringList* labels, bool* ok);
584   Statement* ParseModuleDeclaration(ZoneStringList* names, bool* ok);
585   Module* ParseModule(bool* ok);
586   Module* ParseModuleLiteral(bool* ok);
587   Module* ParseModulePath(bool* ok);
588   Module* ParseModuleVariable(bool* ok);
589   Module* ParseModuleUrl(bool* ok);
590   Module* ParseModuleSpecifier(bool* ok);
591   Block* ParseImportDeclaration(bool* ok);
592   Statement* ParseExportDeclaration(bool* ok);
593   Statement* ParseBlockElement(ZoneStringList* labels, bool* ok);
594   Statement* ParseStatement(ZoneStringList* labels, bool* ok);
595   Statement* ParseFunctionDeclaration(ZoneStringList* names, bool* ok);
596   Statement* ParseNativeDeclaration(bool* ok);
597   Block* ParseBlock(ZoneStringList* labels, bool* ok);
598   Block* ParseVariableStatement(VariableDeclarationContext var_context,
599                                 ZoneStringList* names,
600                                 bool* ok);
601   Block* ParseVariableDeclarations(VariableDeclarationContext var_context,
602                                    VariableDeclarationProperties* decl_props,
603                                    ZoneStringList* names,
604                                    Handle<String>* out,
605                                    bool* ok);
606   Statement* ParseExpressionOrLabelledStatement(ZoneStringList* labels,
607                                                 bool* ok);
608   IfStatement* ParseIfStatement(ZoneStringList* labels, bool* ok);
609   Statement* ParseContinueStatement(bool* ok);
610   Statement* ParseBreakStatement(ZoneStringList* labels, bool* ok);
611   Statement* ParseReturnStatement(bool* ok);
612   Statement* ParseWithStatement(ZoneStringList* labels, bool* ok);
613   CaseClause* ParseCaseClause(bool* default_seen_ptr, bool* ok);
614   SwitchStatement* ParseSwitchStatement(ZoneStringList* labels, bool* ok);
615   DoWhileStatement* ParseDoWhileStatement(ZoneStringList* labels, bool* ok);
616   WhileStatement* ParseWhileStatement(ZoneStringList* labels, bool* ok);
617   Statement* ParseForStatement(ZoneStringList* labels, bool* ok);
618   Statement* ParseThrowStatement(bool* ok);
619   Expression* MakeCatchContext(Handle<String> id, VariableProxy* value);
620   TryStatement* ParseTryStatement(bool* ok);
621   DebuggerStatement* ParseDebuggerStatement(bool* ok);
622
623   // Support for hamony block scoped bindings.
624   Block* ParseScopedBlock(ZoneStringList* labels, bool* ok);
625
626   Expression* ParseExpression(bool accept_IN, bool* ok);
627   Expression* ParseAssignmentExpression(bool accept_IN, bool* ok);
628   Expression* ParseYieldExpression(bool* ok);
629   Expression* ParseConditionalExpression(bool accept_IN, bool* ok);
630   Expression* ParseBinaryExpression(int prec, bool accept_IN, bool* ok);
631   Expression* ParseUnaryExpression(bool* ok);
632   Expression* ParsePostfixExpression(bool* ok);
633   Expression* ParseLeftHandSideExpression(bool* ok);
634   Expression* ParseNewExpression(bool* ok);
635   Expression* ParseMemberExpression(bool* ok);
636   Expression* ParseNewPrefix(PositionStack* stack, bool* ok);
637   Expression* ParseMemberWithNewPrefixesExpression(PositionStack* stack,
638                                                    bool* ok);
639   Expression* ParsePrimaryExpression(bool* ok);
640   Expression* ParseArrayLiteral(bool* ok);
641   Expression* ParseObjectLiteral(bool* ok);
642   Expression* ParseRegExpLiteral(bool seen_equal, bool* ok);
643
644   // Initialize the components of a for-in / for-of statement.
645   void InitializeForEachStatement(ForEachStatement* stmt,
646                                   Expression* each,
647                                   Expression* subject,
648                                   Statement* body);
649
650   ZoneList<Expression*>* ParseArguments(bool* ok);
651   FunctionLiteral* ParseFunctionLiteral(
652       Handle<String> name,
653       Scanner::Location function_name_location,
654       bool name_is_strict_reserved,
655       bool is_generator,
656       int function_token_position,
657       FunctionLiteral::FunctionType type,
658       bool* ok);
659
660   // Magical syntax support.
661   Expression* ParseV8Intrinsic(bool* ok);
662
663   bool is_generator() const { return current_function_state_->is_generator(); }
664
665   bool CheckInOrOf(bool accept_OF, ForEachStatement::VisitMode* visit_mode);
666
667   Handle<String> LiteralString(PretenureFlag tenured) {
668     if (scanner().is_literal_ascii()) {
669       return isolate_->factory()->NewStringFromAscii(
670           scanner().literal_ascii_string(), tenured);
671     } else {
672       return isolate_->factory()->NewStringFromTwoByte(
673             scanner().literal_utf16_string(), tenured);
674     }
675   }
676
677   Handle<String> NextLiteralString(PretenureFlag tenured) {
678     if (scanner().is_next_literal_ascii()) {
679       return isolate_->factory()->NewStringFromAscii(
680           scanner().next_literal_ascii_string(), tenured);
681     } else {
682       return isolate_->factory()->NewStringFromTwoByte(
683           scanner().next_literal_utf16_string(), tenured);
684     }
685   }
686
687   Handle<String> GetSymbol();
688
689   // Get odd-ball literals.
690   Literal* GetLiteralUndefined(int position);
691   Literal* GetLiteralTheHole(int position);
692
693   Handle<String> ParseIdentifier(AllowEvalOrArgumentsAsIdentifier, bool* ok);
694   Handle<String> ParseIdentifierOrStrictReservedWord(
695       bool* is_strict_reserved, bool* ok);
696   Handle<String> ParseIdentifierName(bool* ok);
697   Handle<String> ParseIdentifierNameOrGetOrSet(bool* is_get,
698                                                bool* is_set,
699                                                bool* ok);
700
701   // Determine if the expression is a variable proxy and mark it as being used
702   // in an assignment or with a increment/decrement operator. This is currently
703   // used on for the statically checking assignments to harmony const bindings.
704   void MarkAsLValue(Expression* expression);
705
706   // Strict mode validation of LValue expressions
707   void CheckStrictModeLValue(Expression* expression,
708                              bool* ok);
709
710   // For harmony block scoping mode: Check if the scope has conflicting var/let
711   // declarations from different scopes. It covers for example
712   //
713   // function f() { { { var x; } let x; } }
714   // function g() { { var x; let x; } }
715   //
716   // The var declarations are hoisted to the function scope, but originate from
717   // a scope where the name has also been let bound or the var declaration is
718   // hoisted over such a scope.
719   void CheckConflictingVarDeclarations(Scope* scope, bool* ok);
720
721   // Parser support
722   VariableProxy* NewUnresolved(Handle<String> name,
723                                VariableMode mode,
724                                Interface* interface);
725   void Declare(Declaration* declaration, bool resolve, bool* ok);
726
727   bool TargetStackContainsLabel(Handle<String> label);
728   BreakableStatement* LookupBreakTarget(Handle<String> label, bool* ok);
729   IterationStatement* LookupContinueTarget(Handle<String> label, bool* ok);
730
731   void RegisterTargetUse(Label* target, Target* stop);
732
733   // Factory methods.
734
735   Scope* NewScope(Scope* parent, ScopeType type);
736
737   Handle<String> LookupSymbol(int symbol_id);
738
739   Handle<String> LookupCachedSymbol(int symbol_id);
740
741   // Generate AST node that throw a ReferenceError with the given type.
742   Expression* NewThrowReferenceError(Handle<String> type);
743
744   // Generate AST node that throw a SyntaxError with the given
745   // type. The first argument may be null (in the handle sense) in
746   // which case no arguments are passed to the constructor.
747   Expression* NewThrowSyntaxError(Handle<String> type, Handle<Object> first);
748
749   // Generate AST node that throw a TypeError with the given
750   // type. Both arguments must be non-null (in the handle sense).
751   Expression* NewThrowTypeError(Handle<String> type,
752                                 Handle<Object> first,
753                                 Handle<Object> second);
754
755   // Generic AST generator for throwing errors from compiled code.
756   Expression* NewThrowError(Handle<String> constructor,
757                             Handle<String> type,
758                             Vector< Handle<Object> > arguments);
759
760   PreParser::PreParseResult LazyParseFunctionLiteral(
761        SingletonLogger* logger);
762
763   AstNodeFactory<AstConstructionVisitor>* factory() {
764     return current_function_state_->factory();
765   }
766
767   Isolate* isolate_;
768   ZoneList<Handle<String> > symbol_cache_;
769
770   Handle<Script> script_;
771   Scanner scanner_;
772   PreParser* reusable_preparser_;
773   Scope* top_scope_;
774   Scope* original_scope_;  // for ES5 function declarations in sloppy eval
775   FunctionState* current_function_state_;
776   Target* target_stack_;  // for break, continue statements
777   v8::Extension* extension_;
778   ScriptDataImpl* pre_parse_data_;
779   FuncNameInferrer* fni_;
780
781   Mode mode_;
782   // If true, the next (and immediately following) function literal is
783   // preceded by a parenthesis.
784   // Heuristically that means that the function will be called immediately,
785   // so never lazily compile it.
786   bool parenthesized_function_;
787
788   Zone* zone_;
789   CompilationInfo* info_;
790   friend class BlockState;
791   friend class FunctionState;
792 };
793
794
795 // Support for handling complex values (array and object literals) that
796 // can be fully handled at compile time.
797 class CompileTimeValue: public AllStatic {
798  public:
799   enum LiteralType {
800     OBJECT_LITERAL_FAST_ELEMENTS,
801     OBJECT_LITERAL_SLOW_ELEMENTS,
802     ARRAY_LITERAL
803   };
804
805   static bool IsCompileTimeValue(Expression* expression);
806
807   // Get the value as a compile time value.
808   static Handle<FixedArray> GetValue(Isolate* isolate, Expression* expression);
809
810   // Get the type of a compile time value returned by GetValue().
811   static LiteralType GetLiteralType(Handle<FixedArray> value);
812
813   // Get the elements array of a compile time value returned by GetValue().
814   static Handle<FixedArray> GetElements(Handle<FixedArray> value);
815
816  private:
817   static const int kLiteralTypeSlot = 0;
818   static const int kElementsSlot = 1;
819
820   DISALLOW_IMPLICIT_CONSTRUCTORS(CompileTimeValue);
821 };
822
823 } }  // namespace v8::internal
824
825 #endif  // V8_PARSER_H_