d525252001bcb14748446510ef0689f96c7fefaa
[platform/framework/web/crosswalk.git] / src / v8 / src / parser.cc
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 #include "v8.h"
29
30 #include "api.h"
31 #include "ast.h"
32 #include "bootstrapper.h"
33 #include "char-predicates-inl.h"
34 #include "codegen.h"
35 #include "compiler.h"
36 #include "func-name-inferrer.h"
37 #include "messages.h"
38 #include "parser.h"
39 #include "platform.h"
40 #include "preparser.h"
41 #include "runtime.h"
42 #include "scanner-character-streams.h"
43 #include "scopeinfo.h"
44 #include "string-stream.h"
45
46 namespace v8 {
47 namespace internal {
48
49 // PositionStack is used for on-stack allocation of token positions for
50 // new expressions. Please look at ParseNewExpression.
51
52 class PositionStack  {
53  public:
54   explicit PositionStack(bool* ok) : top_(NULL), ok_(ok) {}
55   ~PositionStack() {
56     ASSERT(!*ok_ || is_empty());
57     USE(ok_);
58   }
59
60   class Element  {
61    public:
62     Element(PositionStack* stack, int value) {
63       previous_ = stack->top();
64       value_ = value;
65       stack->set_top(this);
66     }
67
68    private:
69     Element* previous() { return previous_; }
70     int value() { return value_; }
71     friend class PositionStack;
72     Element* previous_;
73     int value_;
74   };
75
76   bool is_empty() { return top_ == NULL; }
77   int pop() {
78     ASSERT(!is_empty());
79     int result = top_->value();
80     top_ = top_->previous();
81     return result;
82   }
83
84  private:
85   Element* top() { return top_; }
86   void set_top(Element* value) { top_ = value; }
87   Element* top_;
88   bool* ok_;
89 };
90
91
92 RegExpBuilder::RegExpBuilder(Zone* zone)
93     : zone_(zone),
94       pending_empty_(false),
95       characters_(NULL),
96       terms_(),
97       alternatives_()
98 #ifdef DEBUG
99     , last_added_(ADD_NONE)
100 #endif
101   {}
102
103
104 void RegExpBuilder::FlushCharacters() {
105   pending_empty_ = false;
106   if (characters_ != NULL) {
107     RegExpTree* atom = new(zone()) RegExpAtom(characters_->ToConstVector());
108     characters_ = NULL;
109     text_.Add(atom, zone());
110     LAST(ADD_ATOM);
111   }
112 }
113
114
115 void RegExpBuilder::FlushText() {
116   FlushCharacters();
117   int num_text = text_.length();
118   if (num_text == 0) {
119     return;
120   } else if (num_text == 1) {
121     terms_.Add(text_.last(), zone());
122   } else {
123     RegExpText* text = new(zone()) RegExpText(zone());
124     for (int i = 0; i < num_text; i++)
125       text_.Get(i)->AppendToText(text, zone());
126     terms_.Add(text, zone());
127   }
128   text_.Clear();
129 }
130
131
132 void RegExpBuilder::AddCharacter(uc16 c) {
133   pending_empty_ = false;
134   if (characters_ == NULL) {
135     characters_ = new(zone()) ZoneList<uc16>(4, zone());
136   }
137   characters_->Add(c, zone());
138   LAST(ADD_CHAR);
139 }
140
141
142 void RegExpBuilder::AddEmpty() {
143   pending_empty_ = true;
144 }
145
146
147 void RegExpBuilder::AddAtom(RegExpTree* term) {
148   if (term->IsEmpty()) {
149     AddEmpty();
150     return;
151   }
152   if (term->IsTextElement()) {
153     FlushCharacters();
154     text_.Add(term, zone());
155   } else {
156     FlushText();
157     terms_.Add(term, zone());
158   }
159   LAST(ADD_ATOM);
160 }
161
162
163 void RegExpBuilder::AddAssertion(RegExpTree* assert) {
164   FlushText();
165   terms_.Add(assert, zone());
166   LAST(ADD_ASSERT);
167 }
168
169
170 void RegExpBuilder::NewAlternative() {
171   FlushTerms();
172 }
173
174
175 void RegExpBuilder::FlushTerms() {
176   FlushText();
177   int num_terms = terms_.length();
178   RegExpTree* alternative;
179   if (num_terms == 0) {
180     alternative = RegExpEmpty::GetInstance();
181   } else if (num_terms == 1) {
182     alternative = terms_.last();
183   } else {
184     alternative = new(zone()) RegExpAlternative(terms_.GetList(zone()));
185   }
186   alternatives_.Add(alternative, zone());
187   terms_.Clear();
188   LAST(ADD_NONE);
189 }
190
191
192 RegExpTree* RegExpBuilder::ToRegExp() {
193   FlushTerms();
194   int num_alternatives = alternatives_.length();
195   if (num_alternatives == 0) {
196     return RegExpEmpty::GetInstance();
197   }
198   if (num_alternatives == 1) {
199     return alternatives_.last();
200   }
201   return new(zone()) RegExpDisjunction(alternatives_.GetList(zone()));
202 }
203
204
205 void RegExpBuilder::AddQuantifierToAtom(
206     int min, int max, RegExpQuantifier::QuantifierType quantifier_type) {
207   if (pending_empty_) {
208     pending_empty_ = false;
209     return;
210   }
211   RegExpTree* atom;
212   if (characters_ != NULL) {
213     ASSERT(last_added_ == ADD_CHAR);
214     // Last atom was character.
215     Vector<const uc16> char_vector = characters_->ToConstVector();
216     int num_chars = char_vector.length();
217     if (num_chars > 1) {
218       Vector<const uc16> prefix = char_vector.SubVector(0, num_chars - 1);
219       text_.Add(new(zone()) RegExpAtom(prefix), zone());
220       char_vector = char_vector.SubVector(num_chars - 1, num_chars);
221     }
222     characters_ = NULL;
223     atom = new(zone()) RegExpAtom(char_vector);
224     FlushText();
225   } else if (text_.length() > 0) {
226     ASSERT(last_added_ == ADD_ATOM);
227     atom = text_.RemoveLast();
228     FlushText();
229   } else if (terms_.length() > 0) {
230     ASSERT(last_added_ == ADD_ATOM);
231     atom = terms_.RemoveLast();
232     if (atom->max_match() == 0) {
233       // Guaranteed to only match an empty string.
234       LAST(ADD_TERM);
235       if (min == 0) {
236         return;
237       }
238       terms_.Add(atom, zone());
239       return;
240     }
241   } else {
242     // Only call immediately after adding an atom or character!
243     UNREACHABLE();
244     return;
245   }
246   terms_.Add(
247       new(zone()) RegExpQuantifier(min, max, quantifier_type, atom), zone());
248   LAST(ADD_TERM);
249 }
250
251
252 Handle<String> Parser::LookupSymbol(int symbol_id) {
253   // Length of symbol cache is the number of identified symbols.
254   // If we are larger than that, or negative, it's not a cached symbol.
255   // This might also happen if there is no preparser symbol data, even
256   // if there is some preparser data.
257   if (static_cast<unsigned>(symbol_id)
258       >= static_cast<unsigned>(symbol_cache_.length())) {
259     if (scanner().is_literal_ascii()) {
260       return isolate()->factory()->InternalizeOneByteString(
261           Vector<const uint8_t>::cast(scanner().literal_ascii_string()));
262     } else {
263       return isolate()->factory()->InternalizeTwoByteString(
264           scanner().literal_utf16_string());
265     }
266   }
267   return LookupCachedSymbol(symbol_id);
268 }
269
270
271 Handle<String> Parser::LookupCachedSymbol(int symbol_id) {
272   // Make sure the cache is large enough to hold the symbol identifier.
273   if (symbol_cache_.length() <= symbol_id) {
274     // Increase length to index + 1.
275     symbol_cache_.AddBlock(Handle<String>::null(),
276                            symbol_id + 1 - symbol_cache_.length(), zone());
277   }
278   Handle<String> result = symbol_cache_.at(symbol_id);
279   if (result.is_null()) {
280     if (scanner().is_literal_ascii()) {
281       result = isolate()->factory()->InternalizeOneByteString(
282           Vector<const uint8_t>::cast(scanner().literal_ascii_string()));
283     } else {
284       result = isolate()->factory()->InternalizeTwoByteString(
285           scanner().literal_utf16_string());
286     }
287     symbol_cache_.at(symbol_id) = result;
288     return result;
289   }
290   isolate()->counters()->total_preparse_symbols_skipped()->Increment();
291   return result;
292 }
293
294
295 FunctionEntry ScriptDataImpl::GetFunctionEntry(int start) {
296   // The current pre-data entry must be a FunctionEntry with the given
297   // start position.
298   if ((function_index_ + FunctionEntry::kSize <= store_.length())
299       && (static_cast<int>(store_[function_index_]) == start)) {
300     int index = function_index_;
301     function_index_ += FunctionEntry::kSize;
302     return FunctionEntry(store_.SubVector(index,
303                                           index + FunctionEntry::kSize));
304   }
305   return FunctionEntry();
306 }
307
308
309 int ScriptDataImpl::GetSymbolIdentifier() {
310   return ReadNumber(&symbol_data_);
311 }
312
313
314 bool ScriptDataImpl::SanityCheck() {
315   // Check that the header data is valid and doesn't specify
316   // point to positions outside the store.
317   if (store_.length() < PreparseDataConstants::kHeaderSize) return false;
318   if (magic() != PreparseDataConstants::kMagicNumber) return false;
319   if (version() != PreparseDataConstants::kCurrentVersion) return false;
320   if (has_error()) {
321     // Extra sane sanity check for error message encoding.
322     if (store_.length() <= PreparseDataConstants::kHeaderSize
323                          + PreparseDataConstants::kMessageTextPos) {
324       return false;
325     }
326     if (Read(PreparseDataConstants::kMessageStartPos) >
327         Read(PreparseDataConstants::kMessageEndPos)) {
328       return false;
329     }
330     unsigned arg_count = Read(PreparseDataConstants::kMessageArgCountPos);
331     int pos = PreparseDataConstants::kMessageTextPos;
332     for (unsigned int i = 0; i <= arg_count; i++) {
333       if (store_.length() <= PreparseDataConstants::kHeaderSize + pos) {
334         return false;
335       }
336       int length = static_cast<int>(Read(pos));
337       if (length < 0) return false;
338       pos += 1 + length;
339     }
340     if (store_.length() < PreparseDataConstants::kHeaderSize + pos) {
341       return false;
342     }
343     return true;
344   }
345   // Check that the space allocated for function entries is sane.
346   int functions_size =
347       static_cast<int>(store_[PreparseDataConstants::kFunctionsSizeOffset]);
348   if (functions_size < 0) return false;
349   if (functions_size % FunctionEntry::kSize != 0) return false;
350   // Check that the count of symbols is non-negative.
351   int symbol_count =
352       static_cast<int>(store_[PreparseDataConstants::kSymbolCountOffset]);
353   if (symbol_count < 0) return false;
354   // Check that the total size has room for header and function entries.
355   int minimum_size =
356       PreparseDataConstants::kHeaderSize + functions_size;
357   if (store_.length() < minimum_size) return false;
358   return true;
359 }
360
361
362
363 const char* ScriptDataImpl::ReadString(unsigned* start, int* chars) {
364   int length = start[0];
365   char* result = NewArray<char>(length + 1);
366   for (int i = 0; i < length; i++) {
367     result[i] = start[i + 1];
368   }
369   result[length] = '\0';
370   if (chars != NULL) *chars = length;
371   return result;
372 }
373
374
375 Scanner::Location ScriptDataImpl::MessageLocation() {
376   int beg_pos = Read(PreparseDataConstants::kMessageStartPos);
377   int end_pos = Read(PreparseDataConstants::kMessageEndPos);
378   return Scanner::Location(beg_pos, end_pos);
379 }
380
381
382 const char* ScriptDataImpl::BuildMessage() {
383   unsigned* start = ReadAddress(PreparseDataConstants::kMessageTextPos);
384   return ReadString(start, NULL);
385 }
386
387
388 Vector<const char*> ScriptDataImpl::BuildArgs() {
389   int arg_count = Read(PreparseDataConstants::kMessageArgCountPos);
390   const char** array = NewArray<const char*>(arg_count);
391   // Position after text found by skipping past length field and
392   // length field content words.
393   int pos = PreparseDataConstants::kMessageTextPos + 1
394       + Read(PreparseDataConstants::kMessageTextPos);
395   for (int i = 0; i < arg_count; i++) {
396     int count = 0;
397     array[i] = ReadString(ReadAddress(pos), &count);
398     pos += count + 1;
399   }
400   return Vector<const char*>(array, arg_count);
401 }
402
403
404 unsigned ScriptDataImpl::Read(int position) {
405   return store_[PreparseDataConstants::kHeaderSize + position];
406 }
407
408
409 unsigned* ScriptDataImpl::ReadAddress(int position) {
410   return &store_[PreparseDataConstants::kHeaderSize + position];
411 }
412
413
414 Scope* Parser::NewScope(Scope* parent, ScopeType scope_type) {
415   Scope* result = new(zone()) Scope(parent, scope_type, zone());
416   result->Initialize();
417   return result;
418 }
419
420
421 // ----------------------------------------------------------------------------
422 // Target is a support class to facilitate manipulation of the
423 // Parser's target_stack_ (the stack of potential 'break' and
424 // 'continue' statement targets). Upon construction, a new target is
425 // added; it is removed upon destruction.
426
427 class Target BASE_EMBEDDED {
428  public:
429   Target(Target** variable, AstNode* node)
430       : variable_(variable), node_(node), previous_(*variable) {
431     *variable = this;
432   }
433
434   ~Target() {
435     *variable_ = previous_;
436   }
437
438   Target* previous() { return previous_; }
439   AstNode* node() { return node_; }
440
441  private:
442   Target** variable_;
443   AstNode* node_;
444   Target* previous_;
445 };
446
447
448 class TargetScope BASE_EMBEDDED {
449  public:
450   explicit TargetScope(Target** variable)
451       : variable_(variable), previous_(*variable) {
452     *variable = NULL;
453   }
454
455   ~TargetScope() {
456     *variable_ = previous_;
457   }
458
459  private:
460   Target** variable_;
461   Target* previous_;
462 };
463
464
465 // ----------------------------------------------------------------------------
466 // FunctionState and BlockState together implement the parser's scope stack.
467 // The parser's current scope is in top_scope_.  The BlockState and
468 // FunctionState constructors push on the scope stack and the destructors
469 // pop.  They are also used to hold the parser's per-function and per-block
470 // state.
471
472 class Parser::BlockState BASE_EMBEDDED {
473  public:
474   BlockState(Parser* parser, Scope* scope)
475       : parser_(parser),
476         outer_scope_(parser->top_scope_) {
477     parser->top_scope_ = scope;
478   }
479
480   ~BlockState() { parser_->top_scope_ = outer_scope_; }
481
482  private:
483   Parser* parser_;
484   Scope* outer_scope_;
485 };
486
487
488 Parser::FunctionState::FunctionState(Parser* parser, Scope* scope)
489     : next_materialized_literal_index_(JSFunction::kLiteralsPrefixSize),
490       next_handler_index_(0),
491       expected_property_count_(0),
492       generator_object_variable_(NULL),
493       parser_(parser),
494       outer_function_state_(parser->current_function_state_),
495       outer_scope_(parser->top_scope_),
496       saved_ast_node_id_(parser->zone()->isolate()->ast_node_id()),
497       factory_(parser->zone()) {
498   parser->top_scope_ = scope;
499   parser->current_function_state_ = this;
500   parser->zone()->isolate()->set_ast_node_id(BailoutId::FirstUsable().ToInt());
501 }
502
503
504 Parser::FunctionState::~FunctionState() {
505   parser_->top_scope_ = outer_scope_;
506   parser_->current_function_state_ = outer_function_state_;
507   if (outer_function_state_ != NULL) {
508     parser_->isolate()->set_ast_node_id(saved_ast_node_id_);
509   }
510 }
511
512
513 // ----------------------------------------------------------------------------
514 // The CHECK_OK macro is a convenient macro to enforce error
515 // handling for functions that may fail (by returning !*ok).
516 //
517 // CAUTION: This macro appends extra statements after a call,
518 // thus it must never be used where only a single statement
519 // is correct (e.g. an if statement branch w/o braces)!
520
521 #define CHECK_OK  ok);   \
522   if (!*ok) return NULL; \
523   ((void)0
524 #define DUMMY )  // to make indentation work
525 #undef DUMMY
526
527 #define CHECK_FAILED  /**/);   \
528   if (failed_) return NULL; \
529   ((void)0
530 #define DUMMY )  // to make indentation work
531 #undef DUMMY
532
533 // ----------------------------------------------------------------------------
534 // Implementation of Parser
535
536 Parser::Parser(CompilationInfo* info)
537     : ParserBase(&scanner_, info->isolate()->stack_guard()->real_climit()),
538       isolate_(info->isolate()),
539       symbol_cache_(0, info->zone()),
540       script_(info->script()),
541       scanner_(isolate_->unicode_cache()),
542       reusable_preparser_(NULL),
543       top_scope_(NULL),
544       original_scope_(NULL),
545       current_function_state_(NULL),
546       target_stack_(NULL),
547       extension_(info->extension()),
548       pre_parse_data_(NULL),
549       fni_(NULL),
550       parenthesized_function_(false),
551       zone_(info->zone()),
552       info_(info) {
553   ASSERT(!script_.is_null());
554   isolate_->set_ast_node_id(0);
555   set_allow_harmony_scoping(!info->is_native() && FLAG_harmony_scoping);
556   set_allow_modules(!info->is_native() && FLAG_harmony_modules);
557   set_allow_natives_syntax(FLAG_allow_natives_syntax || info->is_native());
558   set_allow_lazy(false);  // Must be explicitly enabled.
559   set_allow_generators(FLAG_harmony_generators);
560   set_allow_for_of(FLAG_harmony_iteration);
561   set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals);
562 }
563
564
565 FunctionLiteral* Parser::ParseProgram() {
566   // TODO(bmeurer): We temporarily need to pass allow_nesting = true here,
567   // see comment for HistogramTimerScope class.
568   HistogramTimerScope timer_scope(isolate()->counters()->parse(), true);
569   Handle<String> source(String::cast(script_->source()));
570   isolate()->counters()->total_parse_size()->Increment(source->length());
571   ElapsedTimer timer;
572   if (FLAG_trace_parse) {
573     timer.Start();
574   }
575   fni_ = new(zone()) FuncNameInferrer(isolate(), zone());
576
577   // Initialize parser state.
578   source->TryFlatten();
579   FunctionLiteral* result;
580   if (source->IsExternalTwoByteString()) {
581     // Notice that the stream is destroyed at the end of the branch block.
582     // The last line of the blocks can't be moved outside, even though they're
583     // identical calls.
584     ExternalTwoByteStringUtf16CharacterStream stream(
585         Handle<ExternalTwoByteString>::cast(source), 0, source->length());
586     scanner_.Initialize(&stream);
587     result = DoParseProgram(info(), source);
588   } else {
589     GenericStringUtf16CharacterStream stream(source, 0, source->length());
590     scanner_.Initialize(&stream);
591     result = DoParseProgram(info(), source);
592   }
593
594   if (FLAG_trace_parse && result != NULL) {
595     double ms = timer.Elapsed().InMillisecondsF();
596     if (info()->is_eval()) {
597       PrintF("[parsing eval");
598     } else if (info()->script()->name()->IsString()) {
599       String* name = String::cast(info()->script()->name());
600       SmartArrayPointer<char> name_chars = name->ToCString();
601       PrintF("[parsing script: %s", name_chars.get());
602     } else {
603       PrintF("[parsing script");
604     }
605     PrintF(" - took %0.3f ms]\n", ms);
606   }
607   return result;
608 }
609
610
611 FunctionLiteral* Parser::DoParseProgram(CompilationInfo* info,
612                                         Handle<String> source) {
613   ASSERT(top_scope_ == NULL);
614   ASSERT(target_stack_ == NULL);
615   if (pre_parse_data_ != NULL) pre_parse_data_->Initialize();
616
617   Handle<String> no_name = isolate()->factory()->empty_string();
618
619   FunctionLiteral* result = NULL;
620   { Scope* scope = NewScope(top_scope_, GLOBAL_SCOPE);
621     info->SetGlobalScope(scope);
622     if (!info->context().is_null()) {
623       scope = Scope::DeserializeScopeChain(*info->context(), scope, zone());
624     }
625     original_scope_ = scope;
626     if (info->is_eval()) {
627       if (!scope->is_global_scope() || info->language_mode() != CLASSIC_MODE) {
628         scope = NewScope(scope, EVAL_SCOPE);
629       }
630     } else if (info->is_global()) {
631       scope = NewScope(scope, GLOBAL_SCOPE);
632     }
633     scope->set_start_position(0);
634     scope->set_end_position(source->length());
635
636     // Compute the parsing mode.
637     Mode mode = (FLAG_lazy && allow_lazy()) ? PARSE_LAZILY : PARSE_EAGERLY;
638     if (allow_natives_syntax() ||
639         extension_ != NULL ||
640         scope->is_eval_scope()) {
641       mode = PARSE_EAGERLY;
642     }
643     ParsingModeScope parsing_mode(this, mode);
644
645     // Enters 'scope'.
646     FunctionState function_state(this, scope);
647
648     top_scope_->SetLanguageMode(info->language_mode());
649     ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(16, zone());
650     bool ok = true;
651     int beg_pos = scanner().location().beg_pos;
652     ParseSourceElements(body, Token::EOS, info->is_eval(), true, &ok);
653     if (ok && !top_scope_->is_classic_mode()) {
654       CheckOctalLiteral(beg_pos, scanner().location().end_pos, &ok);
655     }
656
657     if (ok && is_extended_mode()) {
658       CheckConflictingVarDeclarations(top_scope_, &ok);
659     }
660
661     if (ok && info->parse_restriction() == ONLY_SINGLE_FUNCTION_LITERAL) {
662       if (body->length() != 1 ||
663           !body->at(0)->IsExpressionStatement() ||
664           !body->at(0)->AsExpressionStatement()->
665               expression()->IsFunctionLiteral()) {
666         ReportMessage("single_function_literal", Vector<const char*>::empty());
667         ok = false;
668       }
669     }
670
671     if (ok) {
672       result = factory()->NewFunctionLiteral(
673           no_name,
674           top_scope_,
675           body,
676           function_state.materialized_literal_count(),
677           function_state.expected_property_count(),
678           function_state.handler_count(),
679           0,
680           FunctionLiteral::kNoDuplicateParameters,
681           FunctionLiteral::ANONYMOUS_EXPRESSION,
682           FunctionLiteral::kGlobalOrEval,
683           FunctionLiteral::kNotParenthesized,
684           FunctionLiteral::kNotGenerator,
685           0);
686       result->set_ast_properties(factory()->visitor()->ast_properties());
687       result->set_dont_optimize_reason(
688           factory()->visitor()->dont_optimize_reason());
689     } else if (stack_overflow()) {
690       isolate()->StackOverflow();
691     }
692   }
693
694   // Make sure the target stack is empty.
695   ASSERT(target_stack_ == NULL);
696
697   return result;
698 }
699
700
701 FunctionLiteral* Parser::ParseLazy() {
702   HistogramTimerScope timer_scope(isolate()->counters()->parse_lazy());
703   Handle<String> source(String::cast(script_->source()));
704   isolate()->counters()->total_parse_size()->Increment(source->length());
705   ElapsedTimer timer;
706   if (FLAG_trace_parse) {
707     timer.Start();
708   }
709   Handle<SharedFunctionInfo> shared_info = info()->shared_info();
710
711   // Initialize parser state.
712   source->TryFlatten();
713   FunctionLiteral* result;
714   if (source->IsExternalTwoByteString()) {
715     ExternalTwoByteStringUtf16CharacterStream stream(
716         Handle<ExternalTwoByteString>::cast(source),
717         shared_info->start_position(),
718         shared_info->end_position());
719     result = ParseLazy(&stream);
720   } else {
721     GenericStringUtf16CharacterStream stream(source,
722                                              shared_info->start_position(),
723                                              shared_info->end_position());
724     result = ParseLazy(&stream);
725   }
726
727   if (FLAG_trace_parse && result != NULL) {
728     double ms = timer.Elapsed().InMillisecondsF();
729     SmartArrayPointer<char> name_chars = result->debug_name()->ToCString();
730     PrintF("[parsing function: %s - took %0.3f ms]\n", name_chars.get(), ms);
731   }
732   return result;
733 }
734
735
736 FunctionLiteral* Parser::ParseLazy(Utf16CharacterStream* source) {
737   Handle<SharedFunctionInfo> shared_info = info()->shared_info();
738   scanner_.Initialize(source);
739   ASSERT(top_scope_ == NULL);
740   ASSERT(target_stack_ == NULL);
741
742   Handle<String> name(String::cast(shared_info->name()));
743   fni_ = new(zone()) FuncNameInferrer(isolate(), zone());
744   fni_->PushEnclosingName(name);
745
746   ParsingModeScope parsing_mode(this, PARSE_EAGERLY);
747
748   // Place holder for the result.
749   FunctionLiteral* result = NULL;
750
751   {
752     // Parse the function literal.
753     Scope* scope = NewScope(top_scope_, GLOBAL_SCOPE);
754     info()->SetGlobalScope(scope);
755     if (!info()->closure().is_null()) {
756       scope = Scope::DeserializeScopeChain(info()->closure()->context(), scope,
757                                            zone());
758     }
759     original_scope_ = scope;
760     FunctionState function_state(this, scope);
761     ASSERT(scope->language_mode() != STRICT_MODE || !info()->is_classic_mode());
762     ASSERT(scope->language_mode() != EXTENDED_MODE ||
763            info()->is_extended_mode());
764     ASSERT(info()->language_mode() == shared_info->language_mode());
765     scope->SetLanguageMode(shared_info->language_mode());
766     FunctionLiteral::FunctionType function_type = shared_info->is_expression()
767         ? (shared_info->is_anonymous()
768               ? FunctionLiteral::ANONYMOUS_EXPRESSION
769               : FunctionLiteral::NAMED_EXPRESSION)
770         : FunctionLiteral::DECLARATION;
771     bool ok = true;
772     result = ParseFunctionLiteral(name,
773                                   false,  // Strict mode name already checked.
774                                   shared_info->is_generator(),
775                                   RelocInfo::kNoPosition,
776                                   function_type,
777                                   &ok);
778     // Make sure the results agree.
779     ASSERT(ok == (result != NULL));
780   }
781
782   // Make sure the target stack is empty.
783   ASSERT(target_stack_ == NULL);
784
785   if (result == NULL) {
786     if (stack_overflow()) isolate()->StackOverflow();
787   } else {
788     Handle<String> inferred_name(shared_info->inferred_name());
789     result->set_inferred_name(inferred_name);
790   }
791   return result;
792 }
793
794
795 Handle<String> Parser::GetSymbol() {
796   int symbol_id = -1;
797   if (pre_parse_data() != NULL) {
798     symbol_id = pre_parse_data()->GetSymbolIdentifier();
799   }
800   return LookupSymbol(symbol_id);
801 }
802
803
804 void Parser::ReportMessage(const char* message, Vector<const char*> args) {
805   Scanner::Location source_location = scanner().location();
806   ReportMessageAt(source_location, message, args);
807 }
808
809
810 void Parser::ReportMessage(const char* message, Vector<Handle<String> > args) {
811   Scanner::Location source_location = scanner().location();
812   ReportMessageAt(source_location, message, args);
813 }
814
815
816 void Parser::ReportMessageAt(Scanner::Location source_location,
817                              const char* message,
818                              Vector<const char*> args) {
819   MessageLocation location(script_,
820                            source_location.beg_pos,
821                            source_location.end_pos);
822   Factory* factory = isolate()->factory();
823   Handle<FixedArray> elements = factory->NewFixedArray(args.length());
824   for (int i = 0; i < args.length(); i++) {
825     Handle<String> arg_string = factory->NewStringFromUtf8(CStrVector(args[i]));
826     elements->set(i, *arg_string);
827   }
828   Handle<JSArray> array = factory->NewJSArrayWithElements(elements);
829   Handle<Object> result = factory->NewSyntaxError(message, array);
830   isolate()->Throw(*result, &location);
831 }
832
833
834 void Parser::ReportMessageAt(Scanner::Location source_location,
835                              const char* message,
836                              Vector<Handle<String> > args) {
837   MessageLocation location(script_,
838                            source_location.beg_pos,
839                            source_location.end_pos);
840   Factory* factory = isolate()->factory();
841   Handle<FixedArray> elements = factory->NewFixedArray(args.length());
842   for (int i = 0; i < args.length(); i++) {
843     elements->set(i, *args[i]);
844   }
845   Handle<JSArray> array = factory->NewJSArrayWithElements(elements);
846   Handle<Object> result = factory->NewSyntaxError(message, array);
847   isolate()->Throw(*result, &location);
848 }
849
850
851 void* Parser::ParseSourceElements(ZoneList<Statement*>* processor,
852                                   int end_token,
853                                   bool is_eval,
854                                   bool is_global,
855                                   bool* ok) {
856   // SourceElements ::
857   //   (ModuleElement)* <end_token>
858
859   // Allocate a target stack to use for this set of source
860   // elements. This way, all scripts and functions get their own
861   // target stack thus avoiding illegal breaks and continues across
862   // functions.
863   TargetScope scope(&this->target_stack_);
864
865   ASSERT(processor != NULL);
866   bool directive_prologue = true;     // Parsing directive prologue.
867
868   while (peek() != end_token) {
869     if (directive_prologue && peek() != Token::STRING) {
870       directive_prologue = false;
871     }
872
873     Scanner::Location token_loc = scanner().peek_location();
874     Statement* stat;
875     if (is_global && !is_eval) {
876       stat = ParseModuleElement(NULL, CHECK_OK);
877     } else {
878       stat = ParseBlockElement(NULL, CHECK_OK);
879     }
880     if (stat == NULL || stat->IsEmpty()) {
881       directive_prologue = false;   // End of directive prologue.
882       continue;
883     }
884
885     if (directive_prologue) {
886       // A shot at a directive.
887       ExpressionStatement* e_stat;
888       Literal* literal;
889       // Still processing directive prologue?
890       if ((e_stat = stat->AsExpressionStatement()) != NULL &&
891           (literal = e_stat->expression()->AsLiteral()) != NULL &&
892           literal->value()->IsString()) {
893         Handle<String> directive = Handle<String>::cast(literal->value());
894
895         // Check "use strict" directive (ES5 14.1).
896         if (top_scope_->is_classic_mode() &&
897             directive->Equals(isolate()->heap()->use_strict_string()) &&
898             token_loc.end_pos - token_loc.beg_pos ==
899               isolate()->heap()->use_strict_string()->length() + 2) {
900           // TODO(mstarzinger): Global strict eval calls, need their own scope
901           // as specified in ES5 10.4.2(3). The correct fix would be to always
902           // add this scope in DoParseProgram(), but that requires adaptations
903           // all over the code base, so we go with a quick-fix for now.
904           // In the same manner, we have to patch the parsing mode.
905           if (is_eval && !top_scope_->is_eval_scope()) {
906             ASSERT(top_scope_->is_global_scope());
907             Scope* scope = NewScope(top_scope_, EVAL_SCOPE);
908             scope->set_start_position(top_scope_->start_position());
909             scope->set_end_position(top_scope_->end_position());
910             top_scope_ = scope;
911             mode_ = PARSE_EAGERLY;
912           }
913           // TODO(ES6): Fix entering extended mode, once it is specified.
914           top_scope_->SetLanguageMode(allow_harmony_scoping()
915                                       ? EXTENDED_MODE : STRICT_MODE);
916           // "use strict" is the only directive for now.
917           directive_prologue = false;
918         }
919       } else {
920         // End of the directive prologue.
921         directive_prologue = false;
922       }
923     }
924
925     processor->Add(stat, zone());
926   }
927
928   return 0;
929 }
930
931
932 Statement* Parser::ParseModuleElement(ZoneStringList* labels,
933                                       bool* ok) {
934   // (Ecma 262 5th Edition, clause 14):
935   // SourceElement:
936   //    Statement
937   //    FunctionDeclaration
938   //
939   // In harmony mode we allow additionally the following productions
940   // ModuleElement:
941   //    LetDeclaration
942   //    ConstDeclaration
943   //    ModuleDeclaration
944   //    ImportDeclaration
945   //    ExportDeclaration
946   //    GeneratorDeclaration
947
948   switch (peek()) {
949     case Token::FUNCTION:
950       return ParseFunctionDeclaration(NULL, ok);
951     case Token::LET:
952     case Token::CONST:
953       return ParseVariableStatement(kModuleElement, NULL, ok);
954     case Token::IMPORT:
955       return ParseImportDeclaration(ok);
956     case Token::EXPORT:
957       return ParseExportDeclaration(ok);
958     default: {
959       Statement* stmt = ParseStatement(labels, CHECK_OK);
960       // Handle 'module' as a context-sensitive keyword.
961       if (FLAG_harmony_modules &&
962           peek() == Token::IDENTIFIER &&
963           !scanner().HasAnyLineTerminatorBeforeNext() &&
964           stmt != NULL) {
965         ExpressionStatement* estmt = stmt->AsExpressionStatement();
966         if (estmt != NULL &&
967             estmt->expression()->AsVariableProxy() != NULL &&
968             estmt->expression()->AsVariableProxy()->name()->Equals(
969                 isolate()->heap()->module_string()) &&
970             !scanner().literal_contains_escapes()) {
971           return ParseModuleDeclaration(NULL, ok);
972         }
973       }
974       return stmt;
975     }
976   }
977 }
978
979
980 Statement* Parser::ParseModuleDeclaration(ZoneStringList* names, bool* ok) {
981   // ModuleDeclaration:
982   //    'module' Identifier Module
983
984   int pos = peek_position();
985   Handle<String> name = ParseIdentifier(CHECK_OK);
986
987 #ifdef DEBUG
988   if (FLAG_print_interface_details)
989     PrintF("# Module %s...\n", name->ToAsciiArray());
990 #endif
991
992   Module* module = ParseModule(CHECK_OK);
993   VariableProxy* proxy = NewUnresolved(name, MODULE, module->interface());
994   Declaration* declaration =
995       factory()->NewModuleDeclaration(proxy, module, top_scope_, pos);
996   Declare(declaration, true, CHECK_OK);
997
998 #ifdef DEBUG
999   if (FLAG_print_interface_details)
1000     PrintF("# Module %s.\n", name->ToAsciiArray());
1001
1002   if (FLAG_print_interfaces) {
1003     PrintF("module %s : ", name->ToAsciiArray());
1004     module->interface()->Print();
1005   }
1006 #endif
1007
1008   if (names) names->Add(name, zone());
1009   if (module->body() == NULL)
1010     return factory()->NewEmptyStatement(pos);
1011   else
1012     return factory()->NewModuleStatement(proxy, module->body(), pos);
1013 }
1014
1015
1016 Module* Parser::ParseModule(bool* ok) {
1017   // Module:
1018   //    '{' ModuleElement '}'
1019   //    '=' ModulePath ';'
1020   //    'at' String ';'
1021
1022   switch (peek()) {
1023     case Token::LBRACE:
1024       return ParseModuleLiteral(ok);
1025
1026     case Token::ASSIGN: {
1027       Expect(Token::ASSIGN, CHECK_OK);
1028       Module* result = ParseModulePath(CHECK_OK);
1029       ExpectSemicolon(CHECK_OK);
1030       return result;
1031     }
1032
1033     default: {
1034       ExpectContextualKeyword(CStrVector("at"), CHECK_OK);
1035       Module* result = ParseModuleUrl(CHECK_OK);
1036       ExpectSemicolon(CHECK_OK);
1037       return result;
1038     }
1039   }
1040 }
1041
1042
1043 Module* Parser::ParseModuleLiteral(bool* ok) {
1044   // Module:
1045   //    '{' ModuleElement '}'
1046
1047   int pos = peek_position();
1048   // Construct block expecting 16 statements.
1049   Block* body = factory()->NewBlock(NULL, 16, false, RelocInfo::kNoPosition);
1050 #ifdef DEBUG
1051   if (FLAG_print_interface_details) PrintF("# Literal ");
1052 #endif
1053   Scope* scope = NewScope(top_scope_, MODULE_SCOPE);
1054
1055   Expect(Token::LBRACE, CHECK_OK);
1056   scope->set_start_position(scanner().location().beg_pos);
1057   scope->SetLanguageMode(EXTENDED_MODE);
1058
1059   {
1060     BlockState block_state(this, scope);
1061     TargetCollector collector(zone());
1062     Target target(&this->target_stack_, &collector);
1063     Target target_body(&this->target_stack_, body);
1064
1065     while (peek() != Token::RBRACE) {
1066       Statement* stat = ParseModuleElement(NULL, CHECK_OK);
1067       if (stat && !stat->IsEmpty()) {
1068         body->AddStatement(stat, zone());
1069       }
1070     }
1071   }
1072
1073   Expect(Token::RBRACE, CHECK_OK);
1074   scope->set_end_position(scanner().location().end_pos);
1075   body->set_scope(scope);
1076
1077   // Check that all exports are bound.
1078   Interface* interface = scope->interface();
1079   for (Interface::Iterator it = interface->iterator();
1080        !it.done(); it.Advance()) {
1081     if (scope->LocalLookup(it.name()) == NULL) {
1082       Handle<String> name(it.name());
1083       ReportMessage("module_export_undefined",
1084                     Vector<Handle<String> >(&name, 1));
1085       *ok = false;
1086       return NULL;
1087     }
1088   }
1089
1090   interface->MakeModule(ok);
1091   ASSERT(*ok);
1092   interface->Freeze(ok);
1093   ASSERT(*ok);
1094   return factory()->NewModuleLiteral(body, interface, pos);
1095 }
1096
1097
1098 Module* Parser::ParseModulePath(bool* ok) {
1099   // ModulePath:
1100   //    Identifier
1101   //    ModulePath '.' Identifier
1102
1103   int pos = peek_position();
1104   Module* result = ParseModuleVariable(CHECK_OK);
1105   while (Check(Token::PERIOD)) {
1106     Handle<String> name = ParseIdentifierName(CHECK_OK);
1107 #ifdef DEBUG
1108     if (FLAG_print_interface_details)
1109       PrintF("# Path .%s ", name->ToAsciiArray());
1110 #endif
1111     Module* member = factory()->NewModulePath(result, name, pos);
1112     result->interface()->Add(name, member->interface(), zone(), ok);
1113     if (!*ok) {
1114 #ifdef DEBUG
1115       if (FLAG_print_interfaces) {
1116         PrintF("PATH TYPE ERROR at '%s'\n", name->ToAsciiArray());
1117         PrintF("result: ");
1118         result->interface()->Print();
1119         PrintF("member: ");
1120         member->interface()->Print();
1121       }
1122 #endif
1123       ReportMessage("invalid_module_path", Vector<Handle<String> >(&name, 1));
1124       return NULL;
1125     }
1126     result = member;
1127   }
1128
1129   return result;
1130 }
1131
1132
1133 Module* Parser::ParseModuleVariable(bool* ok) {
1134   // ModulePath:
1135   //    Identifier
1136
1137   int pos = peek_position();
1138   Handle<String> name = ParseIdentifier(CHECK_OK);
1139 #ifdef DEBUG
1140   if (FLAG_print_interface_details)
1141     PrintF("# Module variable %s ", name->ToAsciiArray());
1142 #endif
1143   VariableProxy* proxy = top_scope_->NewUnresolved(
1144       factory(), name, Interface::NewModule(zone()),
1145       scanner().location().beg_pos);
1146
1147   return factory()->NewModuleVariable(proxy, pos);
1148 }
1149
1150
1151 Module* Parser::ParseModuleUrl(bool* ok) {
1152   // Module:
1153   //    String
1154
1155   int pos = peek_position();
1156   Expect(Token::STRING, CHECK_OK);
1157   Handle<String> symbol = GetSymbol();
1158
1159   // TODO(ES6): Request JS resource from environment...
1160
1161 #ifdef DEBUG
1162   if (FLAG_print_interface_details) PrintF("# Url ");
1163 #endif
1164
1165   // Create an empty literal as long as the feature isn't finished.
1166   USE(symbol);
1167   Scope* scope = NewScope(top_scope_, MODULE_SCOPE);
1168   Block* body = factory()->NewBlock(NULL, 1, false, RelocInfo::kNoPosition);
1169   body->set_scope(scope);
1170   Interface* interface = scope->interface();
1171   Module* result = factory()->NewModuleLiteral(body, interface, pos);
1172   interface->Freeze(ok);
1173   ASSERT(*ok);
1174   interface->Unify(scope->interface(), zone(), ok);
1175   ASSERT(*ok);
1176   return result;
1177 }
1178
1179
1180 Module* Parser::ParseModuleSpecifier(bool* ok) {
1181   // ModuleSpecifier:
1182   //    String
1183   //    ModulePath
1184
1185   if (peek() == Token::STRING) {
1186     return ParseModuleUrl(ok);
1187   } else {
1188     return ParseModulePath(ok);
1189   }
1190 }
1191
1192
1193 Block* Parser::ParseImportDeclaration(bool* ok) {
1194   // ImportDeclaration:
1195   //    'import' IdentifierName (',' IdentifierName)* 'from' ModuleSpecifier ';'
1196   //
1197   // TODO(ES6): implement destructuring ImportSpecifiers
1198
1199   int pos = peek_position();
1200   Expect(Token::IMPORT, CHECK_OK);
1201   ZoneStringList names(1, zone());
1202
1203   Handle<String> name = ParseIdentifierName(CHECK_OK);
1204   names.Add(name, zone());
1205   while (peek() == Token::COMMA) {
1206     Consume(Token::COMMA);
1207     name = ParseIdentifierName(CHECK_OK);
1208     names.Add(name, zone());
1209   }
1210
1211   ExpectContextualKeyword(CStrVector("from"), CHECK_OK);
1212   Module* module = ParseModuleSpecifier(CHECK_OK);
1213   ExpectSemicolon(CHECK_OK);
1214
1215   // Generate a separate declaration for each identifier.
1216   // TODO(ES6): once we implement destructuring, make that one declaration.
1217   Block* block = factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition);
1218   for (int i = 0; i < names.length(); ++i) {
1219 #ifdef DEBUG
1220     if (FLAG_print_interface_details)
1221       PrintF("# Import %s ", names[i]->ToAsciiArray());
1222 #endif
1223     Interface* interface = Interface::NewUnknown(zone());
1224     module->interface()->Add(names[i], interface, zone(), ok);
1225     if (!*ok) {
1226 #ifdef DEBUG
1227       if (FLAG_print_interfaces) {
1228         PrintF("IMPORT TYPE ERROR at '%s'\n", names[i]->ToAsciiArray());
1229         PrintF("module: ");
1230         module->interface()->Print();
1231       }
1232 #endif
1233       ReportMessage("invalid_module_path", Vector<Handle<String> >(&name, 1));
1234       return NULL;
1235     }
1236     VariableProxy* proxy = NewUnresolved(names[i], LET, interface);
1237     Declaration* declaration =
1238         factory()->NewImportDeclaration(proxy, module, top_scope_, pos);
1239     Declare(declaration, true, CHECK_OK);
1240   }
1241
1242   return block;
1243 }
1244
1245
1246 Statement* Parser::ParseExportDeclaration(bool* ok) {
1247   // ExportDeclaration:
1248   //    'export' Identifier (',' Identifier)* ';'
1249   //    'export' VariableDeclaration
1250   //    'export' FunctionDeclaration
1251   //    'export' GeneratorDeclaration
1252   //    'export' ModuleDeclaration
1253   //
1254   // TODO(ES6): implement structuring ExportSpecifiers
1255
1256   Expect(Token::EXPORT, CHECK_OK);
1257
1258   Statement* result = NULL;
1259   ZoneStringList names(1, zone());
1260   switch (peek()) {
1261     case Token::IDENTIFIER: {
1262       int pos = position();
1263       Handle<String> name = ParseIdentifier(CHECK_OK);
1264       // Handle 'module' as a context-sensitive keyword.
1265       if (!name->IsOneByteEqualTo(STATIC_ASCII_VECTOR("module"))) {
1266         names.Add(name, zone());
1267         while (peek() == Token::COMMA) {
1268           Consume(Token::COMMA);
1269           name = ParseIdentifier(CHECK_OK);
1270           names.Add(name, zone());
1271         }
1272         ExpectSemicolon(CHECK_OK);
1273         result = factory()->NewEmptyStatement(pos);
1274       } else {
1275         result = ParseModuleDeclaration(&names, CHECK_OK);
1276       }
1277       break;
1278     }
1279
1280     case Token::FUNCTION:
1281       result = ParseFunctionDeclaration(&names, CHECK_OK);
1282       break;
1283
1284     case Token::VAR:
1285     case Token::LET:
1286     case Token::CONST:
1287       result = ParseVariableStatement(kModuleElement, &names, CHECK_OK);
1288       break;
1289
1290     default:
1291       *ok = false;
1292       ReportUnexpectedToken(scanner().current_token());
1293       return NULL;
1294   }
1295
1296   // Extract declared names into export declarations and interface.
1297   Interface* interface = top_scope_->interface();
1298   for (int i = 0; i < names.length(); ++i) {
1299 #ifdef DEBUG
1300     if (FLAG_print_interface_details)
1301       PrintF("# Export %s ", names[i]->ToAsciiArray());
1302 #endif
1303     Interface* inner = Interface::NewUnknown(zone());
1304     interface->Add(names[i], inner, zone(), CHECK_OK);
1305     if (!*ok)
1306       return NULL;
1307     VariableProxy* proxy = NewUnresolved(names[i], LET, inner);
1308     USE(proxy);
1309     // TODO(rossberg): Rethink whether we actually need to store export
1310     // declarations (for compilation?).
1311     // ExportDeclaration* declaration =
1312     //     factory()->NewExportDeclaration(proxy, top_scope_, position);
1313     // top_scope_->AddDeclaration(declaration);
1314   }
1315
1316   ASSERT(result != NULL);
1317   return result;
1318 }
1319
1320
1321 Statement* Parser::ParseBlockElement(ZoneStringList* labels,
1322                                      bool* ok) {
1323   // (Ecma 262 5th Edition, clause 14):
1324   // SourceElement:
1325   //    Statement
1326   //    FunctionDeclaration
1327   //
1328   // In harmony mode we allow additionally the following productions
1329   // BlockElement (aka SourceElement):
1330   //    LetDeclaration
1331   //    ConstDeclaration
1332   //    GeneratorDeclaration
1333
1334   switch (peek()) {
1335     case Token::FUNCTION:
1336       return ParseFunctionDeclaration(NULL, ok);
1337     case Token::LET:
1338     case Token::CONST:
1339       return ParseVariableStatement(kModuleElement, NULL, ok);
1340     default:
1341       return ParseStatement(labels, ok);
1342   }
1343 }
1344
1345
1346 Statement* Parser::ParseStatement(ZoneStringList* labels, bool* ok) {
1347   // Statement ::
1348   //   Block
1349   //   VariableStatement
1350   //   EmptyStatement
1351   //   ExpressionStatement
1352   //   IfStatement
1353   //   IterationStatement
1354   //   ContinueStatement
1355   //   BreakStatement
1356   //   ReturnStatement
1357   //   WithStatement
1358   //   LabelledStatement
1359   //   SwitchStatement
1360   //   ThrowStatement
1361   //   TryStatement
1362   //   DebuggerStatement
1363
1364   // Note: Since labels can only be used by 'break' and 'continue'
1365   // statements, which themselves are only valid within blocks,
1366   // iterations or 'switch' statements (i.e., BreakableStatements),
1367   // labels can be simply ignored in all other cases; except for
1368   // trivial labeled break statements 'label: break label' which is
1369   // parsed into an empty statement.
1370   switch (peek()) {
1371     case Token::LBRACE:
1372       return ParseBlock(labels, ok);
1373
1374     case Token::CONST:  // fall through
1375     case Token::LET:
1376     case Token::VAR:
1377       return ParseVariableStatement(kStatement, NULL, ok);
1378
1379     case Token::SEMICOLON:
1380       Next();
1381       return factory()->NewEmptyStatement(RelocInfo::kNoPosition);
1382
1383     case Token::IF:
1384       return ParseIfStatement(labels, ok);
1385
1386     case Token::DO:
1387       return ParseDoWhileStatement(labels, ok);
1388
1389     case Token::WHILE:
1390       return ParseWhileStatement(labels, ok);
1391
1392     case Token::FOR:
1393       return ParseForStatement(labels, ok);
1394
1395     case Token::CONTINUE:
1396       return ParseContinueStatement(ok);
1397
1398     case Token::BREAK:
1399       return ParseBreakStatement(labels, ok);
1400
1401     case Token::RETURN:
1402       return ParseReturnStatement(ok);
1403
1404     case Token::WITH:
1405       return ParseWithStatement(labels, ok);
1406
1407     case Token::SWITCH:
1408       return ParseSwitchStatement(labels, ok);
1409
1410     case Token::THROW:
1411       return ParseThrowStatement(ok);
1412
1413     case Token::TRY: {
1414       // NOTE: It is somewhat complicated to have labels on
1415       // try-statements. When breaking out of a try-finally statement,
1416       // one must take great care not to treat it as a
1417       // fall-through. It is much easier just to wrap the entire
1418       // try-statement in a statement block and put the labels there
1419       Block* result =
1420           factory()->NewBlock(labels, 1, false, RelocInfo::kNoPosition);
1421       Target target(&this->target_stack_, result);
1422       TryStatement* statement = ParseTryStatement(CHECK_OK);
1423       if (result) result->AddStatement(statement, zone());
1424       return result;
1425     }
1426
1427     case Token::FUNCTION: {
1428       // FunctionDeclaration is only allowed in the context of SourceElements
1429       // (Ecma 262 5th Edition, clause 14):
1430       // SourceElement:
1431       //    Statement
1432       //    FunctionDeclaration
1433       // Common language extension is to allow function declaration in place
1434       // of any statement. This language extension is disabled in strict mode.
1435       //
1436       // In Harmony mode, this case also handles the extension:
1437       // Statement:
1438       //    GeneratorDeclaration
1439       if (!top_scope_->is_classic_mode()) {
1440         ReportMessageAt(scanner().peek_location(), "strict_function",
1441                         Vector<const char*>::empty());
1442         *ok = false;
1443         return NULL;
1444       }
1445       return ParseFunctionDeclaration(NULL, ok);
1446     }
1447
1448     case Token::DEBUGGER:
1449       return ParseDebuggerStatement(ok);
1450
1451     default:
1452       return ParseExpressionOrLabelledStatement(labels, ok);
1453   }
1454 }
1455
1456
1457 VariableProxy* Parser::NewUnresolved(
1458     Handle<String> name, VariableMode mode, Interface* interface) {
1459   // If we are inside a function, a declaration of a var/const variable is a
1460   // truly local variable, and the scope of the variable is always the function
1461   // scope.
1462   // Let/const variables in harmony mode are always added to the immediately
1463   // enclosing scope.
1464   return DeclarationScope(mode)->NewUnresolved(
1465       factory(), name, interface, position());
1466 }
1467
1468
1469 void Parser::Declare(Declaration* declaration, bool resolve, bool* ok) {
1470   VariableProxy* proxy = declaration->proxy();
1471   Handle<String> name = proxy->name();
1472   VariableMode mode = declaration->mode();
1473   Scope* declaration_scope = DeclarationScope(mode);
1474   Variable* var = NULL;
1475
1476   // If a suitable scope exists, then we can statically declare this
1477   // variable and also set its mode. In any case, a Declaration node
1478   // will be added to the scope so that the declaration can be added
1479   // to the corresponding activation frame at runtime if necessary.
1480   // For instance declarations inside an eval scope need to be added
1481   // to the calling function context.
1482   // Similarly, strict mode eval scope does not leak variable declarations to
1483   // the caller's scope so we declare all locals, too.
1484   if (declaration_scope->is_function_scope() ||
1485       declaration_scope->is_strict_or_extended_eval_scope() ||
1486       declaration_scope->is_block_scope() ||
1487       declaration_scope->is_module_scope() ||
1488       declaration_scope->is_global_scope()) {
1489     // Declare the variable in the declaration scope.
1490     // For the global scope, we have to check for collisions with earlier
1491     // (i.e., enclosing) global scopes, to maintain the illusion of a single
1492     // global scope.
1493     var = declaration_scope->is_global_scope()
1494         ? declaration_scope->Lookup(name)
1495         : declaration_scope->LocalLookup(name);
1496     if (var == NULL) {
1497       // Declare the name.
1498       var = declaration_scope->DeclareLocal(
1499           name, mode, declaration->initialization(), proxy->interface());
1500     } else if ((mode != VAR || var->mode() != VAR) &&
1501                (!declaration_scope->is_global_scope() ||
1502                 IsLexicalVariableMode(mode) ||
1503                 IsLexicalVariableMode(var->mode()))) {
1504       // The name was declared in this scope before; check for conflicting
1505       // re-declarations. We have a conflict if either of the declarations is
1506       // not a var (in the global scope, we also have to ignore legacy const for
1507       // compatibility). There is similar code in runtime.cc in the Declare
1508       // functions. The function CheckNonConflictingScope checks for conflicting
1509       // var and let bindings from different scopes whereas this is a check for
1510       // conflicting declarations within the same scope. This check also covers
1511       // the special case
1512       //
1513       // function () { let x; { var x; } }
1514       //
1515       // because the var declaration is hoisted to the function scope where 'x'
1516       // is already bound.
1517       ASSERT(IsDeclaredVariableMode(var->mode()));
1518       if (is_extended_mode()) {
1519         // In harmony mode we treat re-declarations as early errors. See
1520         // ES5 16 for a definition of early errors.
1521         SmartArrayPointer<char> c_string = name->ToCString(DISALLOW_NULLS);
1522         const char* elms[2] = { "Variable", c_string.get() };
1523         Vector<const char*> args(elms, 2);
1524         ReportMessage("redeclaration", args);
1525         *ok = false;
1526         return;
1527       }
1528       Handle<String> message_string =
1529           isolate()->factory()->NewStringFromUtf8(CStrVector("Variable"),
1530                                                   TENURED);
1531       Expression* expression =
1532           NewThrowTypeError(isolate()->factory()->redeclaration_string(),
1533                             message_string, name);
1534       declaration_scope->SetIllegalRedeclaration(expression);
1535     }
1536   }
1537
1538   // We add a declaration node for every declaration. The compiler
1539   // will only generate code if necessary. In particular, declarations
1540   // for inner local variables that do not represent functions won't
1541   // result in any generated code.
1542   //
1543   // Note that we always add an unresolved proxy even if it's not
1544   // used, simply because we don't know in this method (w/o extra
1545   // parameters) if the proxy is needed or not. The proxy will be
1546   // bound during variable resolution time unless it was pre-bound
1547   // below.
1548   //
1549   // WARNING: This will lead to multiple declaration nodes for the
1550   // same variable if it is declared several times. This is not a
1551   // semantic issue as long as we keep the source order, but it may be
1552   // a performance issue since it may lead to repeated
1553   // Runtime::DeclareContextSlot() calls.
1554   declaration_scope->AddDeclaration(declaration);
1555
1556   if (mode == CONST && declaration_scope->is_global_scope()) {
1557     // For global const variables we bind the proxy to a variable.
1558     ASSERT(resolve);  // should be set by all callers
1559     Variable::Kind kind = Variable::NORMAL;
1560     var = new(zone()) Variable(
1561         declaration_scope, name, mode, true, kind,
1562         kNeedsInitialization, proxy->interface());
1563   } else if (declaration_scope->is_eval_scope() &&
1564              declaration_scope->is_classic_mode()) {
1565     // For variable declarations in a non-strict eval scope the proxy is bound
1566     // to a lookup variable to force a dynamic declaration using the
1567     // DeclareContextSlot runtime function.
1568     Variable::Kind kind = Variable::NORMAL;
1569     var = new(zone()) Variable(
1570         declaration_scope, name, mode, true, kind,
1571         declaration->initialization(), proxy->interface());
1572     var->AllocateTo(Variable::LOOKUP, -1);
1573     resolve = true;
1574   }
1575
1576   // If requested and we have a local variable, bind the proxy to the variable
1577   // at parse-time. This is used for functions (and consts) declared inside
1578   // statements: the corresponding function (or const) variable must be in the
1579   // function scope and not a statement-local scope, e.g. as provided with a
1580   // 'with' statement:
1581   //
1582   //   with (obj) {
1583   //     function f() {}
1584   //   }
1585   //
1586   // which is translated into:
1587   //
1588   //   with (obj) {
1589   //     // in this case this is not: 'var f; f = function () {};'
1590   //     var f = function () {};
1591   //   }
1592   //
1593   // Note that if 'f' is accessed from inside the 'with' statement, it
1594   // will be allocated in the context (because we must be able to look
1595   // it up dynamically) but it will also be accessed statically, i.e.,
1596   // with a context slot index and a context chain length for this
1597   // initialization code. Thus, inside the 'with' statement, we need
1598   // both access to the static and the dynamic context chain; the
1599   // runtime needs to provide both.
1600   if (resolve && var != NULL) {
1601     proxy->BindTo(var);
1602
1603     if (FLAG_harmony_modules) {
1604       bool ok;
1605 #ifdef DEBUG
1606       if (FLAG_print_interface_details)
1607         PrintF("# Declare %s\n", var->name()->ToAsciiArray());
1608 #endif
1609       proxy->interface()->Unify(var->interface(), zone(), &ok);
1610       if (!ok) {
1611 #ifdef DEBUG
1612         if (FLAG_print_interfaces) {
1613           PrintF("DECLARE TYPE ERROR\n");
1614           PrintF("proxy: ");
1615           proxy->interface()->Print();
1616           PrintF("var: ");
1617           var->interface()->Print();
1618         }
1619 #endif
1620         ReportMessage("module_type_error", Vector<Handle<String> >(&name, 1));
1621       }
1622     }
1623   }
1624 }
1625
1626
1627 // Language extension which is only enabled for source files loaded
1628 // through the API's extension mechanism.  A native function
1629 // declaration is resolved by looking up the function through a
1630 // callback provided by the extension.
1631 Statement* Parser::ParseNativeDeclaration(bool* ok) {
1632   int pos = peek_position();
1633   Expect(Token::FUNCTION, CHECK_OK);
1634   Handle<String> name = ParseIdentifier(CHECK_OK);
1635   Expect(Token::LPAREN, CHECK_OK);
1636   bool done = (peek() == Token::RPAREN);
1637   while (!done) {
1638     ParseIdentifier(CHECK_OK);
1639     done = (peek() == Token::RPAREN);
1640     if (!done) {
1641       Expect(Token::COMMA, CHECK_OK);
1642     }
1643   }
1644   Expect(Token::RPAREN, CHECK_OK);
1645   Expect(Token::SEMICOLON, CHECK_OK);
1646
1647   // Make sure that the function containing the native declaration
1648   // isn't lazily compiled. The extension structures are only
1649   // accessible while parsing the first time not when reparsing
1650   // because of lazy compilation.
1651   DeclarationScope(VAR)->ForceEagerCompilation();
1652
1653   // TODO(1240846): It's weird that native function declarations are
1654   // introduced dynamically when we meet their declarations, whereas
1655   // other functions are set up when entering the surrounding scope.
1656   VariableProxy* proxy = NewUnresolved(name, VAR, Interface::NewValue());
1657   Declaration* declaration =
1658       factory()->NewVariableDeclaration(proxy, VAR, top_scope_, pos);
1659   Declare(declaration, true, CHECK_OK);
1660   NativeFunctionLiteral* lit = factory()->NewNativeFunctionLiteral(
1661       name, extension_, RelocInfo::kNoPosition);
1662   return factory()->NewExpressionStatement(
1663       factory()->NewAssignment(
1664           Token::INIT_VAR, proxy, lit, RelocInfo::kNoPosition),
1665       pos);
1666 }
1667
1668
1669 Statement* Parser::ParseFunctionDeclaration(ZoneStringList* names, bool* ok) {
1670   // FunctionDeclaration ::
1671   //   'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}'
1672   // GeneratorDeclaration ::
1673   //   'function' '*' Identifier '(' FormalParameterListopt ')'
1674   //      '{' FunctionBody '}'
1675   Expect(Token::FUNCTION, CHECK_OK);
1676   int pos = position();
1677   bool is_generator = allow_generators() && Check(Token::MUL);
1678   bool is_strict_reserved = false;
1679   Handle<String> name = ParseIdentifierOrStrictReservedWord(
1680       &is_strict_reserved, CHECK_OK);
1681   FunctionLiteral* fun = ParseFunctionLiteral(name,
1682                                               is_strict_reserved,
1683                                               is_generator,
1684                                               pos,
1685                                               FunctionLiteral::DECLARATION,
1686                                               CHECK_OK);
1687   // Even if we're not at the top-level of the global or a function
1688   // scope, we treat it as such and introduce the function with its
1689   // initial value upon entering the corresponding scope.
1690   // In extended mode, a function behaves as a lexical binding, except in the
1691   // global scope.
1692   VariableMode mode =
1693       is_extended_mode() && !top_scope_->is_global_scope() ? LET : VAR;
1694   VariableProxy* proxy = NewUnresolved(name, mode, Interface::NewValue());
1695   Declaration* declaration =
1696       factory()->NewFunctionDeclaration(proxy, mode, fun, top_scope_, pos);
1697   Declare(declaration, true, CHECK_OK);
1698   if (names) names->Add(name, zone());
1699   return factory()->NewEmptyStatement(RelocInfo::kNoPosition);
1700 }
1701
1702
1703 Block* Parser::ParseBlock(ZoneStringList* labels, bool* ok) {
1704   if (top_scope_->is_extended_mode()) return ParseScopedBlock(labels, ok);
1705
1706   // Block ::
1707   //   '{' Statement* '}'
1708
1709   // Note that a Block does not introduce a new execution scope!
1710   // (ECMA-262, 3rd, 12.2)
1711   //
1712   // Construct block expecting 16 statements.
1713   Block* result =
1714       factory()->NewBlock(labels, 16, false, RelocInfo::kNoPosition);
1715   Target target(&this->target_stack_, result);
1716   Expect(Token::LBRACE, CHECK_OK);
1717   while (peek() != Token::RBRACE) {
1718     Statement* stat = ParseStatement(NULL, CHECK_OK);
1719     if (stat && !stat->IsEmpty()) {
1720       result->AddStatement(stat, zone());
1721     }
1722   }
1723   Expect(Token::RBRACE, CHECK_OK);
1724   return result;
1725 }
1726
1727
1728 Block* Parser::ParseScopedBlock(ZoneStringList* labels, bool* ok) {
1729   // The harmony mode uses block elements instead of statements.
1730   //
1731   // Block ::
1732   //   '{' BlockElement* '}'
1733
1734   // Construct block expecting 16 statements.
1735   Block* body =
1736       factory()->NewBlock(labels, 16, false, RelocInfo::kNoPosition);
1737   Scope* block_scope = NewScope(top_scope_, BLOCK_SCOPE);
1738
1739   // Parse the statements and collect escaping labels.
1740   Expect(Token::LBRACE, CHECK_OK);
1741   block_scope->set_start_position(scanner().location().beg_pos);
1742   { BlockState block_state(this, block_scope);
1743     TargetCollector collector(zone());
1744     Target target(&this->target_stack_, &collector);
1745     Target target_body(&this->target_stack_, body);
1746
1747     while (peek() != Token::RBRACE) {
1748       Statement* stat = ParseBlockElement(NULL, CHECK_OK);
1749       if (stat && !stat->IsEmpty()) {
1750         body->AddStatement(stat, zone());
1751       }
1752     }
1753   }
1754   Expect(Token::RBRACE, CHECK_OK);
1755   block_scope->set_end_position(scanner().location().end_pos);
1756   block_scope = block_scope->FinalizeBlockScope();
1757   body->set_scope(block_scope);
1758   return body;
1759 }
1760
1761
1762 Block* Parser::ParseVariableStatement(VariableDeclarationContext var_context,
1763                                       ZoneStringList* names,
1764                                       bool* ok) {
1765   // VariableStatement ::
1766   //   VariableDeclarations ';'
1767
1768   Handle<String> ignore;
1769   Block* result =
1770       ParseVariableDeclarations(var_context, NULL, names, &ignore, CHECK_OK);
1771   ExpectSemicolon(CHECK_OK);
1772   return result;
1773 }
1774
1775
1776 bool Parser::IsEvalOrArguments(Handle<String> string) {
1777   return string.is_identical_to(isolate()->factory()->eval_string()) ||
1778       string.is_identical_to(isolate()->factory()->arguments_string());
1779 }
1780
1781
1782 // If the variable declaration declares exactly one non-const
1783 // variable, then *out is set to that variable. In all other cases,
1784 // *out is untouched; in particular, it is the caller's responsibility
1785 // to initialize it properly. This mechanism is used for the parsing
1786 // of 'for-in' loops.
1787 Block* Parser::ParseVariableDeclarations(
1788     VariableDeclarationContext var_context,
1789     VariableDeclarationProperties* decl_props,
1790     ZoneStringList* names,
1791     Handle<String>* out,
1792     bool* ok) {
1793   // VariableDeclarations ::
1794   //   ('var' | 'const' | 'let') (Identifier ('=' AssignmentExpression)?)+[',']
1795   //
1796   // The ES6 Draft Rev3 specifies the following grammar for const declarations
1797   //
1798   // ConstDeclaration ::
1799   //   const ConstBinding (',' ConstBinding)* ';'
1800   // ConstBinding ::
1801   //   Identifier '=' AssignmentExpression
1802   //
1803   // TODO(ES6):
1804   // ConstBinding ::
1805   //   BindingPattern '=' AssignmentExpression
1806
1807   int pos = peek_position();
1808   VariableMode mode = VAR;
1809   // True if the binding needs initialization. 'let' and 'const' declared
1810   // bindings are created uninitialized by their declaration nodes and
1811   // need initialization. 'var' declared bindings are always initialized
1812   // immediately by their declaration nodes.
1813   bool needs_init = false;
1814   bool is_const = false;
1815   Token::Value init_op = Token::INIT_VAR;
1816   if (peek() == Token::VAR) {
1817     Consume(Token::VAR);
1818   } else if (peek() == Token::CONST) {
1819     // TODO(ES6): The ES6 Draft Rev4 section 12.2.2 reads:
1820     //
1821     // ConstDeclaration : const ConstBinding (',' ConstBinding)* ';'
1822     //
1823     // * It is a Syntax Error if the code that matches this production is not
1824     //   contained in extended code.
1825     //
1826     // However disallowing const in classic mode will break compatibility with
1827     // existing pages. Therefore we keep allowing const with the old
1828     // non-harmony semantics in classic mode.
1829     Consume(Token::CONST);
1830     switch (top_scope_->language_mode()) {
1831       case CLASSIC_MODE:
1832         mode = CONST;
1833         init_op = Token::INIT_CONST;
1834         break;
1835       case STRICT_MODE:
1836         ReportMessage("strict_const", Vector<const char*>::empty());
1837         *ok = false;
1838         return NULL;
1839       case EXTENDED_MODE:
1840         if (var_context == kStatement) {
1841           // In extended mode 'const' declarations are only allowed in source
1842           // element positions.
1843           ReportMessage("unprotected_const", Vector<const char*>::empty());
1844           *ok = false;
1845           return NULL;
1846         }
1847         mode = CONST_HARMONY;
1848         init_op = Token::INIT_CONST_HARMONY;
1849     }
1850     is_const = true;
1851     needs_init = true;
1852   } else if (peek() == Token::LET) {
1853     // ES6 Draft Rev4 section 12.2.1:
1854     //
1855     // LetDeclaration : let LetBindingList ;
1856     //
1857     // * It is a Syntax Error if the code that matches this production is not
1858     //   contained in extended code.
1859     if (!is_extended_mode()) {
1860       ReportMessage("illegal_let", Vector<const char*>::empty());
1861       *ok = false;
1862       return NULL;
1863     }
1864     Consume(Token::LET);
1865     if (var_context == kStatement) {
1866       // Let declarations are only allowed in source element positions.
1867       ReportMessage("unprotected_let", Vector<const char*>::empty());
1868       *ok = false;
1869       return NULL;
1870     }
1871     mode = LET;
1872     needs_init = true;
1873     init_op = Token::INIT_LET;
1874   } else {
1875     UNREACHABLE();  // by current callers
1876   }
1877
1878   Scope* declaration_scope = DeclarationScope(mode);
1879
1880   // The scope of a var/const declared variable anywhere inside a function
1881   // is the entire function (ECMA-262, 3rd, 10.1.3, and 12.2). Thus we can
1882   // transform a source-level var/const declaration into a (Function)
1883   // Scope declaration, and rewrite the source-level initialization into an
1884   // assignment statement. We use a block to collect multiple assignments.
1885   //
1886   // We mark the block as initializer block because we don't want the
1887   // rewriter to add a '.result' assignment to such a block (to get compliant
1888   // behavior for code such as print(eval('var x = 7')), and for cosmetic
1889   // reasons when pretty-printing. Also, unless an assignment (initialization)
1890   // is inside an initializer block, it is ignored.
1891   //
1892   // Create new block with one expected declaration.
1893   Block* block = factory()->NewBlock(NULL, 1, true, pos);
1894   int nvars = 0;  // the number of variables declared
1895   Handle<String> name;
1896   do {
1897     if (fni_ != NULL) fni_->Enter();
1898
1899     // Parse variable name.
1900     if (nvars > 0) Consume(Token::COMMA);
1901     name = ParseIdentifier(CHECK_OK);
1902     if (fni_ != NULL) fni_->PushVariableName(name);
1903
1904     // Strict mode variables may not be named eval or arguments
1905     if (!declaration_scope->is_classic_mode() && IsEvalOrArguments(name)) {
1906       ReportMessage("strict_var_name", Vector<const char*>::empty());
1907       *ok = false;
1908       return NULL;
1909     }
1910
1911     // Declare variable.
1912     // Note that we *always* must treat the initial value via a separate init
1913     // assignment for variables and constants because the value must be assigned
1914     // when the variable is encountered in the source. But the variable/constant
1915     // is declared (and set to 'undefined') upon entering the function within
1916     // which the variable or constant is declared. Only function variables have
1917     // an initial value in the declaration (because they are initialized upon
1918     // entering the function).
1919     //
1920     // If we have a const declaration, in an inner scope, the proxy is always
1921     // bound to the declared variable (independent of possibly surrounding with
1922     // statements).
1923     // For let/const declarations in harmony mode, we can also immediately
1924     // pre-resolve the proxy because it resides in the same scope as the
1925     // declaration.
1926     Interface* interface =
1927         is_const ? Interface::NewConst() : Interface::NewValue();
1928     VariableProxy* proxy = NewUnresolved(name, mode, interface);
1929     Declaration* declaration =
1930         factory()->NewVariableDeclaration(proxy, mode, top_scope_, pos);
1931     Declare(declaration, mode != VAR, CHECK_OK);
1932     nvars++;
1933     if (declaration_scope->num_var_or_const() > kMaxNumFunctionLocals) {
1934       ReportMessageAt(scanner().location(), "too_many_variables",
1935                       Vector<const char*>::empty());
1936       *ok = false;
1937       return NULL;
1938     }
1939     if (names) names->Add(name, zone());
1940
1941     // Parse initialization expression if present and/or needed. A
1942     // declaration of the form:
1943     //
1944     //    var v = x;
1945     //
1946     // is syntactic sugar for:
1947     //
1948     //    var v; v = x;
1949     //
1950     // In particular, we need to re-lookup 'v' (in top_scope_, not
1951     // declaration_scope) as it may be a different 'v' than the 'v' in the
1952     // declaration (e.g., if we are inside a 'with' statement or 'catch'
1953     // block).
1954     //
1955     // However, note that const declarations are different! A const
1956     // declaration of the form:
1957     //
1958     //   const c = x;
1959     //
1960     // is *not* syntactic sugar for:
1961     //
1962     //   const c; c = x;
1963     //
1964     // The "variable" c initialized to x is the same as the declared
1965     // one - there is no re-lookup (see the last parameter of the
1966     // Declare() call above).
1967
1968     Scope* initialization_scope = is_const ? declaration_scope : top_scope_;
1969     Expression* value = NULL;
1970     int pos = -1;
1971     // Harmony consts have non-optional initializers.
1972     if (peek() == Token::ASSIGN || mode == CONST_HARMONY) {
1973       Expect(Token::ASSIGN, CHECK_OK);
1974       pos = position();
1975       value = ParseAssignmentExpression(var_context != kForStatement, CHECK_OK);
1976       // Don't infer if it is "a = function(){...}();"-like expression.
1977       if (fni_ != NULL &&
1978           value->AsCall() == NULL &&
1979           value->AsCallNew() == NULL) {
1980         fni_->Infer();
1981       } else {
1982         fni_->RemoveLastFunction();
1983       }
1984       if (decl_props != NULL) *decl_props = kHasInitializers;
1985     }
1986
1987     // Record the end position of the initializer.
1988     if (proxy->var() != NULL) {
1989       proxy->var()->set_initializer_position(position());
1990     }
1991
1992     // Make sure that 'const x' and 'let x' initialize 'x' to undefined.
1993     if (value == NULL && needs_init) {
1994       value = GetLiteralUndefined(position());
1995     }
1996
1997     // Global variable declarations must be compiled in a specific
1998     // way. When the script containing the global variable declaration
1999     // is entered, the global variable must be declared, so that if it
2000     // doesn't exist (on the global object itself, see ES5 errata) it
2001     // gets created with an initial undefined value. This is handled
2002     // by the declarations part of the function representing the
2003     // top-level global code; see Runtime::DeclareGlobalVariable. If
2004     // it already exists (in the object or in a prototype), it is
2005     // *not* touched until the variable declaration statement is
2006     // executed.
2007     //
2008     // Executing the variable declaration statement will always
2009     // guarantee to give the global object a "local" variable; a
2010     // variable defined in the global object and not in any
2011     // prototype. This way, global variable declarations can shadow
2012     // properties in the prototype chain, but only after the variable
2013     // declaration statement has been executed. This is important in
2014     // browsers where the global object (window) has lots of
2015     // properties defined in prototype objects.
2016     if (initialization_scope->is_global_scope() &&
2017         !IsLexicalVariableMode(mode)) {
2018       // Compute the arguments for the runtime call.
2019       ZoneList<Expression*>* arguments =
2020           new(zone()) ZoneList<Expression*>(3, zone());
2021       // We have at least 1 parameter.
2022       arguments->Add(factory()->NewLiteral(name, pos), zone());
2023       CallRuntime* initialize;
2024
2025       if (is_const) {
2026         arguments->Add(value, zone());
2027         value = NULL;  // zap the value to avoid the unnecessary assignment
2028
2029         // Construct the call to Runtime_InitializeConstGlobal
2030         // and add it to the initialization statement block.
2031         // Note that the function does different things depending on
2032         // the number of arguments (1 or 2).
2033         initialize = factory()->NewCallRuntime(
2034             isolate()->factory()->InitializeConstGlobal_string(),
2035             Runtime::FunctionForId(Runtime::kInitializeConstGlobal),
2036             arguments, pos);
2037       } else {
2038         // Add strict mode.
2039         // We may want to pass singleton to avoid Literal allocations.
2040         LanguageMode language_mode = initialization_scope->language_mode();
2041         arguments->Add(factory()->NewNumberLiteral(language_mode, pos), zone());
2042
2043         // Be careful not to assign a value to the global variable if
2044         // we're in a with. The initialization value should not
2045         // necessarily be stored in the global object in that case,
2046         // which is why we need to generate a separate assignment node.
2047         if (value != NULL && !inside_with()) {
2048           arguments->Add(value, zone());
2049           value = NULL;  // zap the value to avoid the unnecessary assignment
2050         }
2051
2052         // Construct the call to Runtime_InitializeVarGlobal
2053         // and add it to the initialization statement block.
2054         // Note that the function does different things depending on
2055         // the number of arguments (2 or 3).
2056         initialize = factory()->NewCallRuntime(
2057             isolate()->factory()->InitializeVarGlobal_string(),
2058             Runtime::FunctionForId(Runtime::kInitializeVarGlobal),
2059             arguments, pos);
2060       }
2061
2062       block->AddStatement(
2063           factory()->NewExpressionStatement(initialize, RelocInfo::kNoPosition),
2064           zone());
2065     } else if (needs_init) {
2066       // Constant initializations always assign to the declared constant which
2067       // is always at the function scope level. This is only relevant for
2068       // dynamically looked-up variables and constants (the start context for
2069       // constant lookups is always the function context, while it is the top
2070       // context for var declared variables). Sigh...
2071       // For 'let' and 'const' declared variables in harmony mode the
2072       // initialization also always assigns to the declared variable.
2073       ASSERT(proxy != NULL);
2074       ASSERT(proxy->var() != NULL);
2075       ASSERT(value != NULL);
2076       Assignment* assignment =
2077           factory()->NewAssignment(init_op, proxy, value, pos);
2078       block->AddStatement(
2079           factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition),
2080           zone());
2081       value = NULL;
2082     }
2083
2084     // Add an assignment node to the initialization statement block if we still
2085     // have a pending initialization value.
2086     if (value != NULL) {
2087       ASSERT(mode == VAR);
2088       // 'var' initializations are simply assignments (with all the consequences
2089       // if they are inside a 'with' statement - they may change a 'with' object
2090       // property).
2091       VariableProxy* proxy =
2092           initialization_scope->NewUnresolved(factory(), name, interface);
2093       Assignment* assignment =
2094           factory()->NewAssignment(init_op, proxy, value, pos);
2095       block->AddStatement(
2096           factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition),
2097           zone());
2098     }
2099
2100     if (fni_ != NULL) fni_->Leave();
2101   } while (peek() == Token::COMMA);
2102
2103   // If there was a single non-const declaration, return it in the output
2104   // parameter for possible use by for/in.
2105   if (nvars == 1 && !is_const) {
2106     *out = name;
2107   }
2108
2109   return block;
2110 }
2111
2112
2113 static bool ContainsLabel(ZoneStringList* labels, Handle<String> label) {
2114   ASSERT(!label.is_null());
2115   if (labels != NULL)
2116     for (int i = labels->length(); i-- > 0; )
2117       if (labels->at(i).is_identical_to(label))
2118         return true;
2119
2120   return false;
2121 }
2122
2123
2124 Statement* Parser::ParseExpressionOrLabelledStatement(ZoneStringList* labels,
2125                                                       bool* ok) {
2126   // ExpressionStatement | LabelledStatement ::
2127   //   Expression ';'
2128   //   Identifier ':' Statement
2129   int pos = peek_position();
2130   bool starts_with_idenfifier = peek_any_identifier();
2131   Expression* expr = ParseExpression(true, CHECK_OK);
2132   if (peek() == Token::COLON && starts_with_idenfifier && expr != NULL &&
2133       expr->AsVariableProxy() != NULL &&
2134       !expr->AsVariableProxy()->is_this()) {
2135     // Expression is a single identifier, and not, e.g., a parenthesized
2136     // identifier.
2137     VariableProxy* var = expr->AsVariableProxy();
2138     Handle<String> label = var->name();
2139     // TODO(1240780): We don't check for redeclaration of labels
2140     // during preparsing since keeping track of the set of active
2141     // labels requires nontrivial changes to the way scopes are
2142     // structured.  However, these are probably changes we want to
2143     // make later anyway so we should go back and fix this then.
2144     if (ContainsLabel(labels, label) || TargetStackContainsLabel(label)) {
2145       SmartArrayPointer<char> c_string = label->ToCString(DISALLOW_NULLS);
2146       const char* elms[2] = { "Label", c_string.get() };
2147       Vector<const char*> args(elms, 2);
2148       ReportMessage("redeclaration", args);
2149       *ok = false;
2150       return NULL;
2151     }
2152     if (labels == NULL) {
2153       labels = new(zone()) ZoneStringList(4, zone());
2154     }
2155     labels->Add(label, zone());
2156     // Remove the "ghost" variable that turned out to be a label
2157     // from the top scope. This way, we don't try to resolve it
2158     // during the scope processing.
2159     top_scope_->RemoveUnresolved(var);
2160     Expect(Token::COLON, CHECK_OK);
2161     return ParseStatement(labels, ok);
2162   }
2163
2164   // If we have an extension, we allow a native function declaration.
2165   // A native function declaration starts with "native function" with
2166   // no line-terminator between the two words.
2167   if (extension_ != NULL &&
2168       peek() == Token::FUNCTION &&
2169       !scanner().HasAnyLineTerminatorBeforeNext() &&
2170       expr != NULL &&
2171       expr->AsVariableProxy() != NULL &&
2172       expr->AsVariableProxy()->name()->Equals(
2173           isolate()->heap()->native_string()) &&
2174       !scanner().literal_contains_escapes()) {
2175     return ParseNativeDeclaration(ok);
2176   }
2177
2178   // Parsed expression statement, or the context-sensitive 'module' keyword.
2179   // Only expect semicolon in the former case.
2180   if (!FLAG_harmony_modules ||
2181       peek() != Token::IDENTIFIER ||
2182       scanner().HasAnyLineTerminatorBeforeNext() ||
2183       expr->AsVariableProxy() == NULL ||
2184       !expr->AsVariableProxy()->name()->Equals(
2185           isolate()->heap()->module_string()) ||
2186       scanner().literal_contains_escapes()) {
2187     ExpectSemicolon(CHECK_OK);
2188   }
2189   return factory()->NewExpressionStatement(expr, pos);
2190 }
2191
2192
2193 IfStatement* Parser::ParseIfStatement(ZoneStringList* labels, bool* ok) {
2194   // IfStatement ::
2195   //   'if' '(' Expression ')' Statement ('else' Statement)?
2196
2197   int pos = peek_position();
2198   Expect(Token::IF, CHECK_OK);
2199   Expect(Token::LPAREN, CHECK_OK);
2200   Expression* condition = ParseExpression(true, CHECK_OK);
2201   Expect(Token::RPAREN, CHECK_OK);
2202   Statement* then_statement = ParseStatement(labels, CHECK_OK);
2203   Statement* else_statement = NULL;
2204   if (peek() == Token::ELSE) {
2205     Next();
2206     else_statement = ParseStatement(labels, CHECK_OK);
2207   } else {
2208     else_statement = factory()->NewEmptyStatement(RelocInfo::kNoPosition);
2209   }
2210   return factory()->NewIfStatement(
2211       condition, then_statement, else_statement, pos);
2212 }
2213
2214
2215 Statement* Parser::ParseContinueStatement(bool* ok) {
2216   // ContinueStatement ::
2217   //   'continue' Identifier? ';'
2218
2219   int pos = peek_position();
2220   Expect(Token::CONTINUE, CHECK_OK);
2221   Handle<String> label = Handle<String>::null();
2222   Token::Value tok = peek();
2223   if (!scanner().HasAnyLineTerminatorBeforeNext() &&
2224       tok != Token::SEMICOLON && tok != Token::RBRACE && tok != Token::EOS) {
2225     label = ParseIdentifier(CHECK_OK);
2226   }
2227   IterationStatement* target = NULL;
2228   target = LookupContinueTarget(label, CHECK_OK);
2229   if (target == NULL) {
2230     // Illegal continue statement.
2231     const char* message = "illegal_continue";
2232     Vector<Handle<String> > args;
2233     if (!label.is_null()) {
2234       message = "unknown_label";
2235       args = Vector<Handle<String> >(&label, 1);
2236     }
2237     ReportMessageAt(scanner().location(), message, args);
2238     *ok = false;
2239     return NULL;
2240   }
2241   ExpectSemicolon(CHECK_OK);
2242   return factory()->NewContinueStatement(target, pos);
2243 }
2244
2245
2246 Statement* Parser::ParseBreakStatement(ZoneStringList* labels, bool* ok) {
2247   // BreakStatement ::
2248   //   'break' Identifier? ';'
2249
2250   int pos = peek_position();
2251   Expect(Token::BREAK, CHECK_OK);
2252   Handle<String> label;
2253   Token::Value tok = peek();
2254   if (!scanner().HasAnyLineTerminatorBeforeNext() &&
2255       tok != Token::SEMICOLON && tok != Token::RBRACE && tok != Token::EOS) {
2256     label = ParseIdentifier(CHECK_OK);
2257   }
2258   // Parse labeled break statements that target themselves into
2259   // empty statements, e.g. 'l1: l2: l3: break l2;'
2260   if (!label.is_null() && ContainsLabel(labels, label)) {
2261     ExpectSemicolon(CHECK_OK);
2262     return factory()->NewEmptyStatement(pos);
2263   }
2264   BreakableStatement* target = NULL;
2265   target = LookupBreakTarget(label, CHECK_OK);
2266   if (target == NULL) {
2267     // Illegal break statement.
2268     const char* message = "illegal_break";
2269     Vector<Handle<String> > args;
2270     if (!label.is_null()) {
2271       message = "unknown_label";
2272       args = Vector<Handle<String> >(&label, 1);
2273     }
2274     ReportMessageAt(scanner().location(), message, args);
2275     *ok = false;
2276     return NULL;
2277   }
2278   ExpectSemicolon(CHECK_OK);
2279   return factory()->NewBreakStatement(target, pos);
2280 }
2281
2282
2283 Statement* Parser::ParseReturnStatement(bool* ok) {
2284   // ReturnStatement ::
2285   //   'return' Expression? ';'
2286
2287   // Consume the return token. It is necessary to do that before
2288   // reporting any errors on it, because of the way errors are
2289   // reported (underlining).
2290   Expect(Token::RETURN, CHECK_OK);
2291   int pos = position();
2292
2293   Token::Value tok = peek();
2294   Statement* result;
2295   Expression* return_value;
2296   if (scanner().HasAnyLineTerminatorBeforeNext() ||
2297       tok == Token::SEMICOLON ||
2298       tok == Token::RBRACE ||
2299       tok == Token::EOS) {
2300     return_value = GetLiteralUndefined(position());
2301   } else {
2302     return_value = ParseExpression(true, CHECK_OK);
2303   }
2304   ExpectSemicolon(CHECK_OK);
2305   if (is_generator()) {
2306     Expression* generator = factory()->NewVariableProxy(
2307         current_function_state_->generator_object_variable());
2308     Expression* yield = factory()->NewYield(
2309         generator, return_value, Yield::FINAL, pos);
2310     result = factory()->NewExpressionStatement(yield, pos);
2311   } else {
2312     result = factory()->NewReturnStatement(return_value, pos);
2313   }
2314
2315   // An ECMAScript program is considered syntactically incorrect if it
2316   // contains a return statement that is not within the body of a
2317   // function. See ECMA-262, section 12.9, page 67.
2318   //
2319   // To be consistent with KJS we report the syntax error at runtime.
2320   Scope* declaration_scope = top_scope_->DeclarationScope();
2321   if (declaration_scope->is_global_scope() ||
2322       declaration_scope->is_eval_scope()) {
2323     Handle<String> message = isolate()->factory()->illegal_return_string();
2324     Expression* throw_error =
2325         NewThrowSyntaxError(message, Handle<Object>::null());
2326     return factory()->NewExpressionStatement(throw_error, pos);
2327   }
2328   return result;
2329 }
2330
2331
2332 Statement* Parser::ParseWithStatement(ZoneStringList* labels, bool* ok) {
2333   // WithStatement ::
2334   //   'with' '(' Expression ')' Statement
2335
2336   Expect(Token::WITH, CHECK_OK);
2337   int pos = position();
2338
2339   if (!top_scope_->is_classic_mode()) {
2340     ReportMessage("strict_mode_with", Vector<const char*>::empty());
2341     *ok = false;
2342     return NULL;
2343   }
2344
2345   Expect(Token::LPAREN, CHECK_OK);
2346   Expression* expr = ParseExpression(true, CHECK_OK);
2347   Expect(Token::RPAREN, CHECK_OK);
2348
2349   top_scope_->DeclarationScope()->RecordWithStatement();
2350   Scope* with_scope = NewScope(top_scope_, WITH_SCOPE);
2351   Statement* stmt;
2352   { BlockState block_state(this, with_scope);
2353     with_scope->set_start_position(scanner().peek_location().beg_pos);
2354     stmt = ParseStatement(labels, CHECK_OK);
2355     with_scope->set_end_position(scanner().location().end_pos);
2356   }
2357   return factory()->NewWithStatement(with_scope, expr, stmt, pos);
2358 }
2359
2360
2361 CaseClause* Parser::ParseCaseClause(bool* default_seen_ptr, bool* ok) {
2362   // CaseClause ::
2363   //   'case' Expression ':' Statement*
2364   //   'default' ':' Statement*
2365
2366   Expression* label = NULL;  // NULL expression indicates default case
2367   if (peek() == Token::CASE) {
2368     Expect(Token::CASE, CHECK_OK);
2369     label = ParseExpression(true, CHECK_OK);
2370   } else {
2371     Expect(Token::DEFAULT, CHECK_OK);
2372     if (*default_seen_ptr) {
2373       ReportMessage("multiple_defaults_in_switch",
2374                     Vector<const char*>::empty());
2375       *ok = false;
2376       return NULL;
2377     }
2378     *default_seen_ptr = true;
2379   }
2380   Expect(Token::COLON, CHECK_OK);
2381   int pos = position();
2382   ZoneList<Statement*>* statements =
2383       new(zone()) ZoneList<Statement*>(5, zone());
2384   while (peek() != Token::CASE &&
2385          peek() != Token::DEFAULT &&
2386          peek() != Token::RBRACE) {
2387     Statement* stat = ParseStatement(NULL, CHECK_OK);
2388     statements->Add(stat, zone());
2389   }
2390
2391   return factory()->NewCaseClause(label, statements, pos);
2392 }
2393
2394
2395 SwitchStatement* Parser::ParseSwitchStatement(ZoneStringList* labels,
2396                                               bool* ok) {
2397   // SwitchStatement ::
2398   //   'switch' '(' Expression ')' '{' CaseClause* '}'
2399
2400   SwitchStatement* statement =
2401       factory()->NewSwitchStatement(labels, peek_position());
2402   Target target(&this->target_stack_, statement);
2403
2404   Expect(Token::SWITCH, CHECK_OK);
2405   Expect(Token::LPAREN, CHECK_OK);
2406   Expression* tag = ParseExpression(true, CHECK_OK);
2407   Expect(Token::RPAREN, CHECK_OK);
2408
2409   bool default_seen = false;
2410   ZoneList<CaseClause*>* cases = new(zone()) ZoneList<CaseClause*>(4, zone());
2411   Expect(Token::LBRACE, CHECK_OK);
2412   while (peek() != Token::RBRACE) {
2413     CaseClause* clause = ParseCaseClause(&default_seen, CHECK_OK);
2414     cases->Add(clause, zone());
2415   }
2416   Expect(Token::RBRACE, CHECK_OK);
2417
2418   if (statement) statement->Initialize(tag, cases);
2419   return statement;
2420 }
2421
2422
2423 Statement* Parser::ParseThrowStatement(bool* ok) {
2424   // ThrowStatement ::
2425   //   'throw' Expression ';'
2426
2427   Expect(Token::THROW, CHECK_OK);
2428   int pos = position();
2429   if (scanner().HasAnyLineTerminatorBeforeNext()) {
2430     ReportMessage("newline_after_throw", Vector<const char*>::empty());
2431     *ok = false;
2432     return NULL;
2433   }
2434   Expression* exception = ParseExpression(true, CHECK_OK);
2435   ExpectSemicolon(CHECK_OK);
2436
2437   return factory()->NewExpressionStatement(
2438       factory()->NewThrow(exception, pos), pos);
2439 }
2440
2441
2442 TryStatement* Parser::ParseTryStatement(bool* ok) {
2443   // TryStatement ::
2444   //   'try' Block Catch
2445   //   'try' Block Finally
2446   //   'try' Block Catch Finally
2447   //
2448   // Catch ::
2449   //   'catch' '(' Identifier ')' Block
2450   //
2451   // Finally ::
2452   //   'finally' Block
2453
2454   Expect(Token::TRY, CHECK_OK);
2455   int pos = position();
2456
2457   TargetCollector try_collector(zone());
2458   Block* try_block;
2459
2460   { Target target(&this->target_stack_, &try_collector);
2461     try_block = ParseBlock(NULL, CHECK_OK);
2462   }
2463
2464   Token::Value tok = peek();
2465   if (tok != Token::CATCH && tok != Token::FINALLY) {
2466     ReportMessage("no_catch_or_finally", Vector<const char*>::empty());
2467     *ok = false;
2468     return NULL;
2469   }
2470
2471   // If we can break out from the catch block and there is a finally block,
2472   // then we will need to collect escaping targets from the catch
2473   // block. Since we don't know yet if there will be a finally block, we
2474   // always collect the targets.
2475   TargetCollector catch_collector(zone());
2476   Scope* catch_scope = NULL;
2477   Variable* catch_variable = NULL;
2478   Block* catch_block = NULL;
2479   Handle<String> name;
2480   if (tok == Token::CATCH) {
2481     Consume(Token::CATCH);
2482
2483     Expect(Token::LPAREN, CHECK_OK);
2484     catch_scope = NewScope(top_scope_, CATCH_SCOPE);
2485     catch_scope->set_start_position(scanner().location().beg_pos);
2486     name = ParseIdentifier(CHECK_OK);
2487
2488     if (!top_scope_->is_classic_mode() && IsEvalOrArguments(name)) {
2489       ReportMessage("strict_catch_variable", Vector<const char*>::empty());
2490       *ok = false;
2491       return NULL;
2492     }
2493
2494     Expect(Token::RPAREN, CHECK_OK);
2495
2496     if (peek() == Token::LBRACE) {
2497       Target target(&this->target_stack_, &catch_collector);
2498       VariableMode mode = is_extended_mode() ? LET : VAR;
2499       catch_variable =
2500           catch_scope->DeclareLocal(name, mode, kCreatedInitialized);
2501
2502       BlockState block_state(this, catch_scope);
2503       catch_block = ParseBlock(NULL, CHECK_OK);
2504     } else {
2505       Expect(Token::LBRACE, CHECK_OK);
2506     }
2507     catch_scope->set_end_position(scanner().location().end_pos);
2508     tok = peek();
2509   }
2510
2511   Block* finally_block = NULL;
2512   if (tok == Token::FINALLY || catch_block == NULL) {
2513     Consume(Token::FINALLY);
2514     finally_block = ParseBlock(NULL, CHECK_OK);
2515   }
2516
2517   // Simplify the AST nodes by converting:
2518   //   'try B0 catch B1 finally B2'
2519   // to:
2520   //   'try { try B0 catch B1 } finally B2'
2521
2522   if (catch_block != NULL && finally_block != NULL) {
2523     // If we have both, create an inner try/catch.
2524     ASSERT(catch_scope != NULL && catch_variable != NULL);
2525     int index = current_function_state_->NextHandlerIndex();
2526     TryCatchStatement* statement = factory()->NewTryCatchStatement(
2527         index, try_block, catch_scope, catch_variable, catch_block,
2528         RelocInfo::kNoPosition);
2529     statement->set_escaping_targets(try_collector.targets());
2530     try_block = factory()->NewBlock(NULL, 1, false, RelocInfo::kNoPosition);
2531     try_block->AddStatement(statement, zone());
2532     catch_block = NULL;  // Clear to indicate it's been handled.
2533   }
2534
2535   TryStatement* result = NULL;
2536   if (catch_block != NULL) {
2537     ASSERT(finally_block == NULL);
2538     ASSERT(catch_scope != NULL && catch_variable != NULL);
2539     int index = current_function_state_->NextHandlerIndex();
2540     result = factory()->NewTryCatchStatement(
2541         index, try_block, catch_scope, catch_variable, catch_block, pos);
2542   } else {
2543     ASSERT(finally_block != NULL);
2544     int index = current_function_state_->NextHandlerIndex();
2545     result = factory()->NewTryFinallyStatement(
2546         index, try_block, finally_block, pos);
2547     // Combine the jump targets of the try block and the possible catch block.
2548     try_collector.targets()->AddAll(*catch_collector.targets(), zone());
2549   }
2550
2551   result->set_escaping_targets(try_collector.targets());
2552   return result;
2553 }
2554
2555
2556 DoWhileStatement* Parser::ParseDoWhileStatement(ZoneStringList* labels,
2557                                                 bool* ok) {
2558   // DoStatement ::
2559   //   'do' Statement 'while' '(' Expression ')' ';'
2560
2561   DoWhileStatement* loop =
2562       factory()->NewDoWhileStatement(labels, peek_position());
2563   Target target(&this->target_stack_, loop);
2564
2565   Expect(Token::DO, CHECK_OK);
2566   Statement* body = ParseStatement(NULL, CHECK_OK);
2567   Expect(Token::WHILE, CHECK_OK);
2568   Expect(Token::LPAREN, CHECK_OK);
2569
2570   Expression* cond = ParseExpression(true, CHECK_OK);
2571   Expect(Token::RPAREN, CHECK_OK);
2572
2573   // Allow do-statements to be terminated with and without
2574   // semi-colons. This allows code such as 'do;while(0)return' to
2575   // parse, which would not be the case if we had used the
2576   // ExpectSemicolon() functionality here.
2577   if (peek() == Token::SEMICOLON) Consume(Token::SEMICOLON);
2578
2579   if (loop != NULL) loop->Initialize(cond, body);
2580   return loop;
2581 }
2582
2583
2584 WhileStatement* Parser::ParseWhileStatement(ZoneStringList* labels, bool* ok) {
2585   // WhileStatement ::
2586   //   'while' '(' Expression ')' Statement
2587
2588   WhileStatement* loop = factory()->NewWhileStatement(labels, peek_position());
2589   Target target(&this->target_stack_, loop);
2590
2591   Expect(Token::WHILE, CHECK_OK);
2592   Expect(Token::LPAREN, CHECK_OK);
2593   Expression* cond = ParseExpression(true, CHECK_OK);
2594   Expect(Token::RPAREN, CHECK_OK);
2595   Statement* body = ParseStatement(NULL, CHECK_OK);
2596
2597   if (loop != NULL) loop->Initialize(cond, body);
2598   return loop;
2599 }
2600
2601
2602 bool Parser::CheckInOrOf(bool accept_OF,
2603                          ForEachStatement::VisitMode* visit_mode) {
2604   if (Check(Token::IN)) {
2605     *visit_mode = ForEachStatement::ENUMERATE;
2606     return true;
2607   } else if (allow_for_of() && accept_OF &&
2608              CheckContextualKeyword(CStrVector("of"))) {
2609     *visit_mode = ForEachStatement::ITERATE;
2610     return true;
2611   }
2612   return false;
2613 }
2614
2615
2616 void Parser::InitializeForEachStatement(ForEachStatement* stmt,
2617                                         Expression* each,
2618                                         Expression* subject,
2619                                         Statement* body) {
2620   ForOfStatement* for_of = stmt->AsForOfStatement();
2621
2622   if (for_of != NULL) {
2623     Factory* heap_factory = isolate()->factory();
2624     Variable* iterator = top_scope_->DeclarationScope()->NewTemporary(
2625         heap_factory->dot_iterator_string());
2626     Variable* result = top_scope_->DeclarationScope()->NewTemporary(
2627         heap_factory->dot_result_string());
2628
2629     Expression* assign_iterator;
2630     Expression* next_result;
2631     Expression* result_done;
2632     Expression* assign_each;
2633
2634     // var iterator = iterable;
2635     {
2636       Expression* iterator_proxy = factory()->NewVariableProxy(iterator);
2637       assign_iterator = factory()->NewAssignment(
2638           Token::ASSIGN, iterator_proxy, subject, RelocInfo::kNoPosition);
2639     }
2640
2641     // var result = iterator.next();
2642     {
2643       Expression* iterator_proxy = factory()->NewVariableProxy(iterator);
2644       Expression* next_literal = factory()->NewLiteral(
2645           heap_factory->next_string(), RelocInfo::kNoPosition);
2646       Expression* next_property = factory()->NewProperty(
2647           iterator_proxy, next_literal, RelocInfo::kNoPosition);
2648       ZoneList<Expression*>* next_arguments =
2649           new(zone()) ZoneList<Expression*>(0, zone());
2650       Expression* next_call = factory()->NewCall(
2651           next_property, next_arguments, RelocInfo::kNoPosition);
2652       Expression* result_proxy = factory()->NewVariableProxy(result);
2653       next_result = factory()->NewAssignment(
2654           Token::ASSIGN, result_proxy, next_call, RelocInfo::kNoPosition);
2655     }
2656
2657     // result.done
2658     {
2659       Expression* done_literal = factory()->NewLiteral(
2660           heap_factory->done_string(), RelocInfo::kNoPosition);
2661       Expression* result_proxy = factory()->NewVariableProxy(result);
2662       result_done = factory()->NewProperty(
2663           result_proxy, done_literal, RelocInfo::kNoPosition);
2664     }
2665
2666     // each = result.value
2667     {
2668       Expression* value_literal = factory()->NewLiteral(
2669           heap_factory->value_string(), RelocInfo::kNoPosition);
2670       Expression* result_proxy = factory()->NewVariableProxy(result);
2671       Expression* result_value = factory()->NewProperty(
2672           result_proxy, value_literal, RelocInfo::kNoPosition);
2673       assign_each = factory()->NewAssignment(
2674           Token::ASSIGN, each, result_value, RelocInfo::kNoPosition);
2675     }
2676
2677     for_of->Initialize(each, subject, body,
2678                        assign_iterator, next_result, result_done, assign_each);
2679   } else {
2680     stmt->Initialize(each, subject, body);
2681   }
2682 }
2683
2684
2685 Statement* Parser::ParseForStatement(ZoneStringList* labels, bool* ok) {
2686   // ForStatement ::
2687   //   'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement
2688
2689   int pos = peek_position();
2690   Statement* init = NULL;
2691
2692   // Create an in-between scope for let-bound iteration variables.
2693   Scope* saved_scope = top_scope_;
2694   Scope* for_scope = NewScope(top_scope_, BLOCK_SCOPE);
2695   top_scope_ = for_scope;
2696
2697   Expect(Token::FOR, CHECK_OK);
2698   Expect(Token::LPAREN, CHECK_OK);
2699   for_scope->set_start_position(scanner().location().beg_pos);
2700   if (peek() != Token::SEMICOLON) {
2701     if (peek() == Token::VAR || peek() == Token::CONST) {
2702       bool is_const = peek() == Token::CONST;
2703       Handle<String> name;
2704       VariableDeclarationProperties decl_props = kHasNoInitializers;
2705       Block* variable_statement =
2706           ParseVariableDeclarations(kForStatement, &decl_props, NULL, &name,
2707                                     CHECK_OK);
2708       bool accept_OF = decl_props == kHasNoInitializers;
2709       ForEachStatement::VisitMode mode;
2710
2711       if (!name.is_null() && CheckInOrOf(accept_OF, &mode)) {
2712         Interface* interface =
2713             is_const ? Interface::NewConst() : Interface::NewValue();
2714         ForEachStatement* loop =
2715             factory()->NewForEachStatement(mode, labels, pos);
2716         Target target(&this->target_stack_, loop);
2717
2718         Expression* enumerable = ParseExpression(true, CHECK_OK);
2719         Expect(Token::RPAREN, CHECK_OK);
2720
2721         VariableProxy* each =
2722             top_scope_->NewUnresolved(factory(), name, interface);
2723         Statement* body = ParseStatement(NULL, CHECK_OK);
2724         InitializeForEachStatement(loop, each, enumerable, body);
2725         Block* result =
2726             factory()->NewBlock(NULL, 2, false, RelocInfo::kNoPosition);
2727         result->AddStatement(variable_statement, zone());
2728         result->AddStatement(loop, zone());
2729         top_scope_ = saved_scope;
2730         for_scope->set_end_position(scanner().location().end_pos);
2731         for_scope = for_scope->FinalizeBlockScope();
2732         ASSERT(for_scope == NULL);
2733         // Parsed for-in loop w/ variable/const declaration.
2734         return result;
2735       } else {
2736         init = variable_statement;
2737       }
2738     } else if (peek() == Token::LET) {
2739       Handle<String> name;
2740       VariableDeclarationProperties decl_props = kHasNoInitializers;
2741       Block* variable_statement =
2742          ParseVariableDeclarations(kForStatement, &decl_props, NULL, &name,
2743                                    CHECK_OK);
2744       bool accept_IN = !name.is_null() && decl_props != kHasInitializers;
2745       bool accept_OF = decl_props == kHasNoInitializers;
2746       ForEachStatement::VisitMode mode;
2747
2748       if (accept_IN && CheckInOrOf(accept_OF, &mode)) {
2749         // Rewrite a for-in statement of the form
2750         //
2751         //   for (let x in e) b
2752         //
2753         // into
2754         //
2755         //   <let x' be a temporary variable>
2756         //   for (x' in e) {
2757         //     let x;
2758         //     x = x';
2759         //     b;
2760         //   }
2761
2762         // TODO(keuchel): Move the temporary variable to the block scope, after
2763         // implementing stack allocated block scoped variables.
2764         Factory* heap_factory = isolate()->factory();
2765         Handle<String> tempstr =
2766             heap_factory->NewConsString(heap_factory->dot_for_string(), name);
2767         Handle<String> tempname = heap_factory->InternalizeString(tempstr);
2768         Variable* temp = top_scope_->DeclarationScope()->NewTemporary(tempname);
2769         VariableProxy* temp_proxy = factory()->NewVariableProxy(temp);
2770         ForEachStatement* loop =
2771             factory()->NewForEachStatement(mode, labels, pos);
2772         Target target(&this->target_stack_, loop);
2773
2774         // The expression does not see the loop variable.
2775         top_scope_ = saved_scope;
2776         Expression* enumerable = ParseExpression(true, CHECK_OK);
2777         top_scope_ = for_scope;
2778         Expect(Token::RPAREN, CHECK_OK);
2779
2780         VariableProxy* each =
2781             top_scope_->NewUnresolved(factory(), name, Interface::NewValue());
2782         Statement* body = ParseStatement(NULL, CHECK_OK);
2783         Block* body_block =
2784             factory()->NewBlock(NULL, 3, false, RelocInfo::kNoPosition);
2785         Assignment* assignment = factory()->NewAssignment(
2786             Token::ASSIGN, each, temp_proxy, RelocInfo::kNoPosition);
2787         Statement* assignment_statement = factory()->NewExpressionStatement(
2788             assignment, RelocInfo::kNoPosition);
2789         body_block->AddStatement(variable_statement, zone());
2790         body_block->AddStatement(assignment_statement, zone());
2791         body_block->AddStatement(body, zone());
2792         InitializeForEachStatement(loop, temp_proxy, enumerable, body_block);
2793         top_scope_ = saved_scope;
2794         for_scope->set_end_position(scanner().location().end_pos);
2795         for_scope = for_scope->FinalizeBlockScope();
2796         body_block->set_scope(for_scope);
2797         // Parsed for-in loop w/ let declaration.
2798         return loop;
2799
2800       } else {
2801         init = variable_statement;
2802       }
2803     } else {
2804       Expression* expression = ParseExpression(false, CHECK_OK);
2805       ForEachStatement::VisitMode mode;
2806       bool accept_OF = expression->AsVariableProxy();
2807
2808       if (CheckInOrOf(accept_OF, &mode)) {
2809         // Signal a reference error if the expression is an invalid
2810         // left-hand side expression.  We could report this as a syntax
2811         // error here but for compatibility with JSC we choose to report
2812         // the error at runtime.
2813         if (expression == NULL || !expression->IsValidLeftHandSide()) {
2814           Handle<String> message =
2815               isolate()->factory()->invalid_lhs_in_for_in_string();
2816           expression = NewThrowReferenceError(message);
2817         }
2818         ForEachStatement* loop =
2819             factory()->NewForEachStatement(mode, labels, pos);
2820         Target target(&this->target_stack_, loop);
2821
2822         Expression* enumerable = ParseExpression(true, CHECK_OK);
2823         Expect(Token::RPAREN, CHECK_OK);
2824
2825         Statement* body = ParseStatement(NULL, CHECK_OK);
2826         InitializeForEachStatement(loop, expression, enumerable, body);
2827         top_scope_ = saved_scope;
2828         for_scope->set_end_position(scanner().location().end_pos);
2829         for_scope = for_scope->FinalizeBlockScope();
2830         ASSERT(for_scope == NULL);
2831         // Parsed for-in loop.
2832         return loop;
2833
2834       } else {
2835         init = factory()->NewExpressionStatement(
2836             expression, RelocInfo::kNoPosition);
2837       }
2838     }
2839   }
2840
2841   // Standard 'for' loop
2842   ForStatement* loop = factory()->NewForStatement(labels, pos);
2843   Target target(&this->target_stack_, loop);
2844
2845   // Parsed initializer at this point.
2846   Expect(Token::SEMICOLON, CHECK_OK);
2847
2848   Expression* cond = NULL;
2849   if (peek() != Token::SEMICOLON) {
2850     cond = ParseExpression(true, CHECK_OK);
2851   }
2852   Expect(Token::SEMICOLON, CHECK_OK);
2853
2854   Statement* next = NULL;
2855   if (peek() != Token::RPAREN) {
2856     Expression* exp = ParseExpression(true, CHECK_OK);
2857     next = factory()->NewExpressionStatement(exp, RelocInfo::kNoPosition);
2858   }
2859   Expect(Token::RPAREN, CHECK_OK);
2860
2861   Statement* body = ParseStatement(NULL, CHECK_OK);
2862   top_scope_ = saved_scope;
2863   for_scope->set_end_position(scanner().location().end_pos);
2864   for_scope = for_scope->FinalizeBlockScope();
2865   if (for_scope != NULL) {
2866     // Rewrite a for statement of the form
2867     //
2868     //   for (let x = i; c; n) b
2869     //
2870     // into
2871     //
2872     //   {
2873     //     let x = i;
2874     //     for (; c; n) b
2875     //   }
2876     ASSERT(init != NULL);
2877     Block* result = factory()->NewBlock(NULL, 2, false, RelocInfo::kNoPosition);
2878     result->AddStatement(init, zone());
2879     result->AddStatement(loop, zone());
2880     result->set_scope(for_scope);
2881     loop->Initialize(NULL, cond, next, body);
2882     return result;
2883   } else {
2884     loop->Initialize(init, cond, next, body);
2885     return loop;
2886   }
2887 }
2888
2889
2890 // Precedence = 1
2891 Expression* Parser::ParseExpression(bool accept_IN, bool* ok) {
2892   // Expression ::
2893   //   AssignmentExpression
2894   //   Expression ',' AssignmentExpression
2895
2896   Expression* result = ParseAssignmentExpression(accept_IN, CHECK_OK);
2897   while (peek() == Token::COMMA) {
2898     Expect(Token::COMMA, CHECK_OK);
2899     int pos = position();
2900     Expression* right = ParseAssignmentExpression(accept_IN, CHECK_OK);
2901     result = factory()->NewBinaryOperation(Token::COMMA, result, right, pos);
2902   }
2903   return result;
2904 }
2905
2906
2907 // Precedence = 2
2908 Expression* Parser::ParseAssignmentExpression(bool accept_IN, bool* ok) {
2909   // AssignmentExpression ::
2910   //   ConditionalExpression
2911   //   YieldExpression
2912   //   LeftHandSideExpression AssignmentOperator AssignmentExpression
2913
2914   if (peek() == Token::YIELD && is_generator()) {
2915     return ParseYieldExpression(ok);
2916   }
2917
2918   if (fni_ != NULL) fni_->Enter();
2919   Expression* expression = ParseConditionalExpression(accept_IN, CHECK_OK);
2920
2921   if (!Token::IsAssignmentOp(peek())) {
2922     if (fni_ != NULL) fni_->Leave();
2923     // Parsed conditional expression only (no assignment).
2924     return expression;
2925   }
2926
2927   // Signal a reference error if the expression is an invalid left-hand
2928   // side expression.  We could report this as a syntax error here but
2929   // for compatibility with JSC we choose to report the error at
2930   // runtime.
2931   // TODO(ES5): Should change parsing for spec conformance.
2932   if (expression == NULL || !expression->IsValidLeftHandSide()) {
2933     Handle<String> message =
2934         isolate()->factory()->invalid_lhs_in_assignment_string();
2935     expression = NewThrowReferenceError(message);
2936   }
2937
2938   if (!top_scope_->is_classic_mode()) {
2939     // Assignment to eval or arguments is disallowed in strict mode.
2940     CheckStrictModeLValue(expression, "strict_lhs_assignment", CHECK_OK);
2941   }
2942   MarkAsLValue(expression);
2943
2944   Token::Value op = Next();  // Get assignment operator.
2945   int pos = position();
2946   Expression* right = ParseAssignmentExpression(accept_IN, CHECK_OK);
2947
2948   // TODO(1231235): We try to estimate the set of properties set by
2949   // constructors. We define a new property whenever there is an
2950   // assignment to a property of 'this'. We should probably only add
2951   // properties if we haven't seen them before. Otherwise we'll
2952   // probably overestimate the number of properties.
2953   Property* property = expression ? expression->AsProperty() : NULL;
2954   if (op == Token::ASSIGN &&
2955       property != NULL &&
2956       property->obj()->AsVariableProxy() != NULL &&
2957       property->obj()->AsVariableProxy()->is_this()) {
2958     current_function_state_->AddProperty();
2959   }
2960
2961   // If we assign a function literal to a property we pretenure the
2962   // literal so it can be added as a constant function property.
2963   if (property != NULL && right->AsFunctionLiteral() != NULL) {
2964     right->AsFunctionLiteral()->set_pretenure();
2965   }
2966
2967   if (fni_ != NULL) {
2968     // Check if the right hand side is a call to avoid inferring a
2969     // name if we're dealing with "a = function(){...}();"-like
2970     // expression.
2971     if ((op == Token::INIT_VAR
2972          || op == Token::INIT_CONST
2973          || op == Token::ASSIGN)
2974         && (right->AsCall() == NULL && right->AsCallNew() == NULL)) {
2975       fni_->Infer();
2976     } else {
2977       fni_->RemoveLastFunction();
2978     }
2979     fni_->Leave();
2980   }
2981
2982   return factory()->NewAssignment(op, expression, right, pos);
2983 }
2984
2985
2986 Expression* Parser::ParseYieldExpression(bool* ok) {
2987   // YieldExpression ::
2988   //   'yield' '*'? AssignmentExpression
2989   int pos = peek_position();
2990   Expect(Token::YIELD, CHECK_OK);
2991   Yield::Kind kind =
2992       Check(Token::MUL) ? Yield::DELEGATING : Yield::SUSPEND;
2993   Expression* generator_object = factory()->NewVariableProxy(
2994       current_function_state_->generator_object_variable());
2995   Expression* expression = ParseAssignmentExpression(false, CHECK_OK);
2996   Yield* yield = factory()->NewYield(generator_object, expression, kind, pos);
2997   if (kind == Yield::DELEGATING) {
2998     yield->set_index(current_function_state_->NextHandlerIndex());
2999   }
3000   return yield;
3001 }
3002
3003
3004 // Precedence = 3
3005 Expression* Parser::ParseConditionalExpression(bool accept_IN, bool* ok) {
3006   // ConditionalExpression ::
3007   //   LogicalOrExpression
3008   //   LogicalOrExpression '?' AssignmentExpression ':' AssignmentExpression
3009
3010   int pos = peek_position();
3011   // We start using the binary expression parser for prec >= 4 only!
3012   Expression* expression = ParseBinaryExpression(4, accept_IN, CHECK_OK);
3013   if (peek() != Token::CONDITIONAL) return expression;
3014   Consume(Token::CONDITIONAL);
3015   // In parsing the first assignment expression in conditional
3016   // expressions we always accept the 'in' keyword; see ECMA-262,
3017   // section 11.12, page 58.
3018   Expression* left = ParseAssignmentExpression(true, CHECK_OK);
3019   Expect(Token::COLON, CHECK_OK);
3020   Expression* right = ParseAssignmentExpression(accept_IN, CHECK_OK);
3021   return factory()->NewConditional(expression, left, right, pos);
3022 }
3023
3024
3025 int ParserBase::Precedence(Token::Value tok, bool accept_IN) {
3026   if (tok == Token::IN && !accept_IN)
3027     return 0;  // 0 precedence will terminate binary expression parsing
3028
3029   return Token::Precedence(tok);
3030 }
3031
3032
3033 // Precedence >= 4
3034 Expression* Parser::ParseBinaryExpression(int prec, bool accept_IN, bool* ok) {
3035   ASSERT(prec >= 4);
3036   Expression* x = ParseUnaryExpression(CHECK_OK);
3037   for (int prec1 = Precedence(peek(), accept_IN); prec1 >= prec; prec1--) {
3038     // prec1 >= 4
3039     while (Precedence(peek(), accept_IN) == prec1) {
3040       Token::Value op = Next();
3041       int pos = position();
3042       Expression* y = ParseBinaryExpression(prec1 + 1, accept_IN, CHECK_OK);
3043
3044       // Compute some expressions involving only number literals.
3045       if (x && x->AsLiteral() && x->AsLiteral()->value()->IsNumber() &&
3046           y && y->AsLiteral() && y->AsLiteral()->value()->IsNumber()) {
3047         double x_val = x->AsLiteral()->value()->Number();
3048         double y_val = y->AsLiteral()->value()->Number();
3049
3050         switch (op) {
3051           case Token::ADD:
3052             x = factory()->NewNumberLiteral(x_val + y_val, pos);
3053             continue;
3054           case Token::SUB:
3055             x = factory()->NewNumberLiteral(x_val - y_val, pos);
3056             continue;
3057           case Token::MUL:
3058             x = factory()->NewNumberLiteral(x_val * y_val, pos);
3059             continue;
3060           case Token::DIV:
3061             x = factory()->NewNumberLiteral(x_val / y_val, pos);
3062             continue;
3063           case Token::BIT_OR: {
3064             int value = DoubleToInt32(x_val) | DoubleToInt32(y_val);
3065             x = factory()->NewNumberLiteral(value, pos);
3066             continue;
3067           }
3068           case Token::BIT_AND: {
3069             int value = DoubleToInt32(x_val) & DoubleToInt32(y_val);
3070             x = factory()->NewNumberLiteral(value, pos);
3071             continue;
3072           }
3073           case Token::BIT_XOR: {
3074             int value = DoubleToInt32(x_val) ^ DoubleToInt32(y_val);
3075             x = factory()->NewNumberLiteral(value, pos);
3076             continue;
3077           }
3078           case Token::SHL: {
3079             int value = DoubleToInt32(x_val) << (DoubleToInt32(y_val) & 0x1f);
3080             x = factory()->NewNumberLiteral(value, pos);
3081             continue;
3082           }
3083           case Token::SHR: {
3084             uint32_t shift = DoubleToInt32(y_val) & 0x1f;
3085             uint32_t value = DoubleToUint32(x_val) >> shift;
3086             x = factory()->NewNumberLiteral(value, pos);
3087             continue;
3088           }
3089           case Token::SAR: {
3090             uint32_t shift = DoubleToInt32(y_val) & 0x1f;
3091             int value = ArithmeticShiftRight(DoubleToInt32(x_val), shift);
3092             x = factory()->NewNumberLiteral(value, pos);
3093             continue;
3094           }
3095           default:
3096             break;
3097         }
3098       }
3099
3100       // For now we distinguish between comparisons and other binary
3101       // operations.  (We could combine the two and get rid of this
3102       // code and AST node eventually.)
3103       if (Token::IsCompareOp(op)) {
3104         // We have a comparison.
3105         Token::Value cmp = op;
3106         switch (op) {
3107           case Token::NE: cmp = Token::EQ; break;
3108           case Token::NE_STRICT: cmp = Token::EQ_STRICT; break;
3109           default: break;
3110         }
3111         x = factory()->NewCompareOperation(cmp, x, y, pos);
3112         if (cmp != op) {
3113           // The comparison was negated - add a NOT.
3114           x = factory()->NewUnaryOperation(Token::NOT, x, pos);
3115         }
3116
3117       } else {
3118         // We have a "normal" binary operation.
3119         x = factory()->NewBinaryOperation(op, x, y, pos);
3120       }
3121     }
3122   }
3123   return x;
3124 }
3125
3126
3127 Expression* Parser::ParseUnaryExpression(bool* ok) {
3128   // UnaryExpression ::
3129   //   PostfixExpression
3130   //   'delete' UnaryExpression
3131   //   'void' UnaryExpression
3132   //   'typeof' UnaryExpression
3133   //   '++' UnaryExpression
3134   //   '--' UnaryExpression
3135   //   '+' UnaryExpression
3136   //   '-' UnaryExpression
3137   //   '~' UnaryExpression
3138   //   '!' UnaryExpression
3139
3140   Token::Value op = peek();
3141   if (Token::IsUnaryOp(op)) {
3142     op = Next();
3143     int pos = position();
3144     Expression* expression = ParseUnaryExpression(CHECK_OK);
3145
3146     if (expression != NULL && (expression->AsLiteral() != NULL)) {
3147       Handle<Object> literal = expression->AsLiteral()->value();
3148       if (op == Token::NOT) {
3149         // Convert the literal to a boolean condition and negate it.
3150         bool condition = literal->BooleanValue();
3151         Handle<Object> result = isolate()->factory()->ToBoolean(!condition);
3152         return factory()->NewLiteral(result, pos);
3153       } else if (literal->IsNumber()) {
3154         // Compute some expressions involving only number literals.
3155         double value = literal->Number();
3156         switch (op) {
3157           case Token::ADD:
3158             return expression;
3159           case Token::SUB:
3160             return factory()->NewNumberLiteral(-value, pos);
3161           case Token::BIT_NOT:
3162             return factory()->NewNumberLiteral(~DoubleToInt32(value), pos);
3163           default:
3164             break;
3165         }
3166       }
3167     }
3168
3169     // "delete identifier" is a syntax error in strict mode.
3170     if (op == Token::DELETE && !top_scope_->is_classic_mode()) {
3171       VariableProxy* operand = expression->AsVariableProxy();
3172       if (operand != NULL && !operand->is_this()) {
3173         ReportMessage("strict_delete", Vector<const char*>::empty());
3174         *ok = false;
3175         return NULL;
3176       }
3177     }
3178
3179     // Desugar '+foo' into 'foo*1', this enables the collection of type feedback
3180     // without any special stub and the multiplication is removed later in
3181     // Crankshaft's canonicalization pass.
3182     if (op == Token::ADD) {
3183       return factory()->NewBinaryOperation(Token::MUL,
3184                                            expression,
3185                                            factory()->NewNumberLiteral(1, pos),
3186                                            pos);
3187     }
3188     // The same idea for '-foo' => 'foo*(-1)'.
3189     if (op == Token::SUB) {
3190       return factory()->NewBinaryOperation(Token::MUL,
3191                                            expression,
3192                                            factory()->NewNumberLiteral(-1, pos),
3193                                            pos);
3194     }
3195     // ...and one more time for '~foo' => 'foo^(~0)'.
3196     if (op == Token::BIT_NOT) {
3197       return factory()->NewBinaryOperation(Token::BIT_XOR,
3198                                            expression,
3199                                            factory()->NewNumberLiteral(~0, pos),
3200                                            pos);
3201     }
3202
3203     return factory()->NewUnaryOperation(op, expression, pos);
3204
3205   } else if (Token::IsCountOp(op)) {
3206     op = Next();
3207     Expression* expression = ParseUnaryExpression(CHECK_OK);
3208     // Signal a reference error if the expression is an invalid
3209     // left-hand side expression.  We could report this as a syntax
3210     // error here but for compatibility with JSC we choose to report the
3211     // error at runtime.
3212     if (expression == NULL || !expression->IsValidLeftHandSide()) {
3213       Handle<String> message =
3214           isolate()->factory()->invalid_lhs_in_prefix_op_string();
3215       expression = NewThrowReferenceError(message);
3216     }
3217
3218     if (!top_scope_->is_classic_mode()) {
3219       // Prefix expression operand in strict mode may not be eval or arguments.
3220       CheckStrictModeLValue(expression, "strict_lhs_prefix", CHECK_OK);
3221     }
3222     MarkAsLValue(expression);
3223
3224     return factory()->NewCountOperation(op,
3225                                         true /* prefix */,
3226                                         expression,
3227                                         position());
3228
3229   } else {
3230     return ParsePostfixExpression(ok);
3231   }
3232 }
3233
3234
3235 Expression* Parser::ParsePostfixExpression(bool* ok) {
3236   // PostfixExpression ::
3237   //   LeftHandSideExpression ('++' | '--')?
3238
3239   Expression* expression = ParseLeftHandSideExpression(CHECK_OK);
3240   if (!scanner().HasAnyLineTerminatorBeforeNext() &&
3241       Token::IsCountOp(peek())) {
3242     // Signal a reference error if the expression is an invalid
3243     // left-hand side expression.  We could report this as a syntax
3244     // error here but for compatibility with JSC we choose to report the
3245     // error at runtime.
3246     if (expression == NULL || !expression->IsValidLeftHandSide()) {
3247       Handle<String> message =
3248           isolate()->factory()->invalid_lhs_in_postfix_op_string();
3249       expression = NewThrowReferenceError(message);
3250     }
3251
3252     if (!top_scope_->is_classic_mode()) {
3253       // Postfix expression operand in strict mode may not be eval or arguments.
3254       CheckStrictModeLValue(expression, "strict_lhs_prefix", CHECK_OK);
3255     }
3256     MarkAsLValue(expression);
3257
3258     Token::Value next = Next();
3259     expression =
3260         factory()->NewCountOperation(next,
3261                                      false /* postfix */,
3262                                      expression,
3263                                      position());
3264   }
3265   return expression;
3266 }
3267
3268
3269 Expression* Parser::ParseLeftHandSideExpression(bool* ok) {
3270   // LeftHandSideExpression ::
3271   //   (NewExpression | MemberExpression) ...
3272
3273   Expression* result;
3274   if (peek() == Token::NEW) {
3275     result = ParseNewExpression(CHECK_OK);
3276   } else {
3277     result = ParseMemberExpression(CHECK_OK);
3278   }
3279
3280   while (true) {
3281     switch (peek()) {
3282       case Token::LBRACK: {
3283         Consume(Token::LBRACK);
3284         int pos = position();
3285         Expression* index = ParseExpression(true, CHECK_OK);
3286         result = factory()->NewProperty(result, index, pos);
3287         Expect(Token::RBRACK, CHECK_OK);
3288         break;
3289       }
3290
3291       case Token::LPAREN: {
3292         int pos;
3293         if (scanner().current_token() == Token::IDENTIFIER) {
3294           // For call of an identifier we want to report position of
3295           // the identifier as position of the call in the stack trace.
3296           pos = position();
3297         } else {
3298           // For other kinds of calls we record position of the parenthesis as
3299           // position of the call.  Note that this is extremely important for
3300           // expressions of the form function(){...}() for which call position
3301           // should not point to the closing brace otherwise it will intersect
3302           // with positions recorded for function literal and confuse debugger.
3303           pos = peek_position();
3304           // Also the trailing parenthesis are a hint that the function will
3305           // be called immediately. If we happen to have parsed a preceding
3306           // function literal eagerly, we can also compile it eagerly.
3307           if (result->IsFunctionLiteral() && mode() == PARSE_EAGERLY) {
3308             result->AsFunctionLiteral()->set_parenthesized();
3309           }
3310         }
3311         ZoneList<Expression*>* args = ParseArguments(CHECK_OK);
3312
3313         // Keep track of eval() calls since they disable all local variable
3314         // optimizations.
3315         // The calls that need special treatment are the
3316         // direct eval calls. These calls are all of the form eval(...), with
3317         // no explicit receiver.
3318         // These calls are marked as potentially direct eval calls. Whether
3319         // they are actually direct calls to eval is determined at run time.
3320         VariableProxy* callee = result->AsVariableProxy();
3321         if (callee != NULL &&
3322             callee->IsVariable(isolate()->factory()->eval_string())) {
3323           top_scope_->DeclarationScope()->RecordEvalCall();
3324         }
3325         result = factory()->NewCall(result, args, pos);
3326         if (fni_ != NULL) fni_->RemoveLastFunction();
3327         break;
3328       }
3329
3330       case Token::PERIOD: {
3331         Consume(Token::PERIOD);
3332         int pos = position();
3333         Handle<String> name = ParseIdentifierName(CHECK_OK);
3334         result = factory()->NewProperty(
3335             result, factory()->NewLiteral(name, pos), pos);
3336         if (fni_ != NULL) fni_->PushLiteralName(name);
3337         break;
3338       }
3339
3340       default:
3341         return result;
3342     }
3343   }
3344 }
3345
3346
3347 Expression* Parser::ParseNewPrefix(PositionStack* stack, bool* ok) {
3348   // NewExpression ::
3349   //   ('new')+ MemberExpression
3350
3351   // The grammar for new expressions is pretty warped. The keyword
3352   // 'new' can either be a part of the new expression (where it isn't
3353   // followed by an argument list) or a part of the member expression,
3354   // where it must be followed by an argument list. To accommodate
3355   // this, we parse the 'new' keywords greedily and keep track of how
3356   // many we have parsed. This information is then passed on to the
3357   // member expression parser, which is only allowed to match argument
3358   // lists as long as it has 'new' prefixes left
3359   Expect(Token::NEW, CHECK_OK);
3360   PositionStack::Element pos(stack, position());
3361
3362   Expression* result;
3363   if (peek() == Token::NEW) {
3364     result = ParseNewPrefix(stack, CHECK_OK);
3365   } else {
3366     result = ParseMemberWithNewPrefixesExpression(stack, CHECK_OK);
3367   }
3368
3369   if (!stack->is_empty()) {
3370     int last = stack->pop();
3371     result = factory()->NewCallNew(
3372         result, new(zone()) ZoneList<Expression*>(0, zone()), last);
3373   }
3374   return result;
3375 }
3376
3377
3378 Expression* Parser::ParseNewExpression(bool* ok) {
3379   PositionStack stack(ok);
3380   return ParseNewPrefix(&stack, ok);
3381 }
3382
3383
3384 Expression* Parser::ParseMemberExpression(bool* ok) {
3385   return ParseMemberWithNewPrefixesExpression(NULL, ok);
3386 }
3387
3388
3389 Expression* Parser::ParseMemberWithNewPrefixesExpression(PositionStack* stack,
3390                                                          bool* ok) {
3391   // MemberExpression ::
3392   //   (PrimaryExpression | FunctionLiteral)
3393   //     ('[' Expression ']' | '.' Identifier | Arguments)*
3394
3395   // Parse the initial primary or function expression.
3396   Expression* result = NULL;
3397   if (peek() == Token::FUNCTION) {
3398     Expect(Token::FUNCTION, CHECK_OK);
3399     int function_token_position = position();
3400     bool is_generator = allow_generators() && Check(Token::MUL);
3401     Handle<String> name;
3402     bool is_strict_reserved_name = false;
3403     if (peek_any_identifier()) {
3404       name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved_name,
3405                                                  CHECK_OK);
3406     }
3407     FunctionLiteral::FunctionType function_type = name.is_null()
3408         ? FunctionLiteral::ANONYMOUS_EXPRESSION
3409         : FunctionLiteral::NAMED_EXPRESSION;
3410     result = ParseFunctionLiteral(name,
3411                                   is_strict_reserved_name,
3412                                   is_generator,
3413                                   function_token_position,
3414                                   function_type,
3415                                   CHECK_OK);
3416   } else {
3417     result = ParsePrimaryExpression(CHECK_OK);
3418   }
3419
3420   while (true) {
3421     switch (peek()) {
3422       case Token::LBRACK: {
3423         Consume(Token::LBRACK);
3424         int pos = position();
3425         Expression* index = ParseExpression(true, CHECK_OK);
3426         result = factory()->NewProperty(result, index, pos);
3427         if (fni_ != NULL) {
3428           if (index->IsPropertyName()) {
3429             fni_->PushLiteralName(index->AsLiteral()->AsPropertyName());
3430           } else {
3431             fni_->PushLiteralName(
3432                 isolate()->factory()->anonymous_function_string());
3433           }
3434         }
3435         Expect(Token::RBRACK, CHECK_OK);
3436         break;
3437       }
3438       case Token::PERIOD: {
3439         Consume(Token::PERIOD);
3440         int pos = position();
3441         Handle<String> name = ParseIdentifierName(CHECK_OK);
3442         result = factory()->NewProperty(
3443             result, factory()->NewLiteral(name, pos), pos);
3444         if (fni_ != NULL) fni_->PushLiteralName(name);
3445         break;
3446       }
3447       case Token::LPAREN: {
3448         if ((stack == NULL) || stack->is_empty()) return result;
3449         // Consume one of the new prefixes (already parsed).
3450         ZoneList<Expression*>* args = ParseArguments(CHECK_OK);
3451         int pos = stack->pop();
3452         result = factory()->NewCallNew(result, args, pos);
3453         break;
3454       }
3455       default:
3456         return result;
3457     }
3458   }
3459 }
3460
3461
3462 DebuggerStatement* Parser::ParseDebuggerStatement(bool* ok) {
3463   // In ECMA-262 'debugger' is defined as a reserved keyword. In some browser
3464   // contexts this is used as a statement which invokes the debugger as i a
3465   // break point is present.
3466   // DebuggerStatement ::
3467   //   'debugger' ';'
3468
3469   int pos = peek_position();
3470   Expect(Token::DEBUGGER, CHECK_OK);
3471   ExpectSemicolon(CHECK_OK);
3472   return factory()->NewDebuggerStatement(pos);
3473 }
3474
3475
3476 void Parser::ReportUnexpectedToken(Token::Value token) {
3477   // We don't report stack overflows here, to avoid increasing the
3478   // stack depth even further.  Instead we report it after parsing is
3479   // over, in ParseProgram/ParseJson.
3480   if (token == Token::ILLEGAL && stack_overflow()) return;
3481   // Four of the tokens are treated specially
3482   switch (token) {
3483     case Token::EOS:
3484       return ReportMessage("unexpected_eos", Vector<const char*>::empty());
3485     case Token::NUMBER:
3486       return ReportMessage("unexpected_token_number",
3487                            Vector<const char*>::empty());
3488     case Token::STRING:
3489       return ReportMessage("unexpected_token_string",
3490                            Vector<const char*>::empty());
3491     case Token::IDENTIFIER:
3492       return ReportMessage("unexpected_token_identifier",
3493                            Vector<const char*>::empty());
3494     case Token::FUTURE_RESERVED_WORD:
3495       return ReportMessage("unexpected_reserved",
3496                            Vector<const char*>::empty());
3497     case Token::YIELD:
3498     case Token::FUTURE_STRICT_RESERVED_WORD:
3499       return ReportMessage(top_scope_->is_classic_mode() ?
3500                                "unexpected_token_identifier" :
3501                                "unexpected_strict_reserved",
3502                            Vector<const char*>::empty());
3503     default:
3504       const char* name = Token::String(token);
3505       ASSERT(name != NULL);
3506       ReportMessage("unexpected_token", Vector<const char*>(&name, 1));
3507   }
3508 }
3509
3510
3511 void Parser::ReportInvalidPreparseData(Handle<String> name, bool* ok) {
3512   SmartArrayPointer<char> name_string = name->ToCString(DISALLOW_NULLS);
3513   const char* element[1] = { name_string.get() };
3514   ReportMessage("invalid_preparser_data",
3515                 Vector<const char*>(element, 1));
3516   *ok = false;
3517 }
3518
3519
3520 Expression* Parser::ParsePrimaryExpression(bool* ok) {
3521   // PrimaryExpression ::
3522   //   'this'
3523   //   'null'
3524   //   'true'
3525   //   'false'
3526   //   Identifier
3527   //   Number
3528   //   String
3529   //   ArrayLiteral
3530   //   ObjectLiteral
3531   //   RegExpLiteral
3532   //   '(' Expression ')'
3533
3534   int pos = peek_position();
3535   Expression* result = NULL;
3536   switch (peek()) {
3537     case Token::THIS: {
3538       Consume(Token::THIS);
3539       result = factory()->NewVariableProxy(top_scope_->receiver());
3540       break;
3541     }
3542
3543     case Token::NULL_LITERAL:
3544       Consume(Token::NULL_LITERAL);
3545       result = factory()->NewLiteral(isolate()->factory()->null_value(), pos);
3546       break;
3547
3548     case Token::TRUE_LITERAL:
3549       Consume(Token::TRUE_LITERAL);
3550       result = factory()->NewLiteral(isolate()->factory()->true_value(), pos);
3551       break;
3552
3553     case Token::FALSE_LITERAL:
3554       Consume(Token::FALSE_LITERAL);
3555       result = factory()->NewLiteral(isolate()->factory()->false_value(), pos);
3556       break;
3557
3558     case Token::IDENTIFIER:
3559     case Token::YIELD:
3560     case Token::FUTURE_STRICT_RESERVED_WORD: {
3561       Handle<String> name = ParseIdentifier(CHECK_OK);
3562       if (fni_ != NULL) fni_->PushVariableName(name);
3563       // The name may refer to a module instance object, so its type is unknown.
3564 #ifdef DEBUG
3565       if (FLAG_print_interface_details)
3566         PrintF("# Variable %s ", name->ToAsciiArray());
3567 #endif
3568       Interface* interface = Interface::NewUnknown(zone());
3569       result = top_scope_->NewUnresolved(factory(), name, interface, pos);
3570       break;
3571     }
3572
3573     case Token::NUMBER: {
3574       Consume(Token::NUMBER);
3575       ASSERT(scanner().is_literal_ascii());
3576       double value = StringToDouble(isolate()->unicode_cache(),
3577                                     scanner().literal_ascii_string(),
3578                                     ALLOW_HEX | ALLOW_OCTAL |
3579                                         ALLOW_IMPLICIT_OCTAL | ALLOW_BINARY);
3580       result = factory()->NewNumberLiteral(value, pos);
3581       break;
3582     }
3583
3584     case Token::STRING: {
3585       Consume(Token::STRING);
3586       Handle<String> symbol = GetSymbol();
3587       result = factory()->NewLiteral(symbol, pos);
3588       if (fni_ != NULL) fni_->PushLiteralName(symbol);
3589       break;
3590     }
3591
3592     case Token::ASSIGN_DIV:
3593       result = ParseRegExpLiteral(true, CHECK_OK);
3594       break;
3595
3596     case Token::DIV:
3597       result = ParseRegExpLiteral(false, CHECK_OK);
3598       break;
3599
3600     case Token::LBRACK:
3601       result = ParseArrayLiteral(CHECK_OK);
3602       break;
3603
3604     case Token::LBRACE:
3605       result = ParseObjectLiteral(CHECK_OK);
3606       break;
3607
3608     case Token::LPAREN:
3609       Consume(Token::LPAREN);
3610       // Heuristically try to detect immediately called functions before
3611       // seeing the call parentheses.
3612       parenthesized_function_ = (peek() == Token::FUNCTION);
3613       result = ParseExpression(true, CHECK_OK);
3614       Expect(Token::RPAREN, CHECK_OK);
3615       break;
3616
3617     case Token::MOD:
3618       if (allow_natives_syntax() || extension_ != NULL) {
3619         result = ParseV8Intrinsic(CHECK_OK);
3620         break;
3621       }
3622       // If we're not allowing special syntax we fall-through to the
3623       // default case.
3624
3625     default: {
3626       Token::Value tok = Next();
3627       ReportUnexpectedToken(tok);
3628       *ok = false;
3629       return NULL;
3630     }
3631   }
3632
3633   return result;
3634 }
3635
3636
3637 Expression* Parser::ParseArrayLiteral(bool* ok) {
3638   // ArrayLiteral ::
3639   //   '[' Expression? (',' Expression?)* ']'
3640
3641   int pos = peek_position();
3642   ZoneList<Expression*>* values = new(zone()) ZoneList<Expression*>(4, zone());
3643   Expect(Token::LBRACK, CHECK_OK);
3644   while (peek() != Token::RBRACK) {
3645     Expression* elem;
3646     if (peek() == Token::COMMA) {
3647       elem = GetLiteralTheHole(peek_position());
3648     } else {
3649       elem = ParseAssignmentExpression(true, CHECK_OK);
3650     }
3651     values->Add(elem, zone());
3652     if (peek() != Token::RBRACK) {
3653       Expect(Token::COMMA, CHECK_OK);
3654     }
3655   }
3656   Expect(Token::RBRACK, CHECK_OK);
3657
3658   // Update the scope information before the pre-parsing bailout.
3659   int literal_index = current_function_state_->NextMaterializedLiteralIndex();
3660
3661   return factory()->NewArrayLiteral(values, literal_index, pos);
3662 }
3663
3664
3665 bool CompileTimeValue::IsCompileTimeValue(Expression* expression) {
3666   if (expression->AsLiteral() != NULL) return true;
3667   MaterializedLiteral* lit = expression->AsMaterializedLiteral();
3668   return lit != NULL && lit->is_simple();
3669 }
3670
3671
3672 Handle<FixedArray> CompileTimeValue::GetValue(Isolate* isolate,
3673                                               Expression* expression) {
3674   Factory* factory = isolate->factory();
3675   ASSERT(IsCompileTimeValue(expression));
3676   Handle<FixedArray> result = factory->NewFixedArray(2, TENURED);
3677   ObjectLiteral* object_literal = expression->AsObjectLiteral();
3678   if (object_literal != NULL) {
3679     ASSERT(object_literal->is_simple());
3680     if (object_literal->fast_elements()) {
3681       result->set(kLiteralTypeSlot, Smi::FromInt(OBJECT_LITERAL_FAST_ELEMENTS));
3682     } else {
3683       result->set(kLiteralTypeSlot, Smi::FromInt(OBJECT_LITERAL_SLOW_ELEMENTS));
3684     }
3685     result->set(kElementsSlot, *object_literal->constant_properties());
3686   } else {
3687     ArrayLiteral* array_literal = expression->AsArrayLiteral();
3688     ASSERT(array_literal != NULL && array_literal->is_simple());
3689     result->set(kLiteralTypeSlot, Smi::FromInt(ARRAY_LITERAL));
3690     result->set(kElementsSlot, *array_literal->constant_elements());
3691   }
3692   return result;
3693 }
3694
3695
3696 CompileTimeValue::LiteralType CompileTimeValue::GetLiteralType(
3697     Handle<FixedArray> value) {
3698   Smi* literal_type = Smi::cast(value->get(kLiteralTypeSlot));
3699   return static_cast<LiteralType>(literal_type->value());
3700 }
3701
3702
3703 Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) {
3704   return Handle<FixedArray>(FixedArray::cast(value->get(kElementsSlot)));
3705 }
3706
3707
3708 Expression* Parser::ParseObjectLiteral(bool* ok) {
3709   // ObjectLiteral ::
3710   //   '{' (
3711   //       ((IdentifierName | String | Number) ':' AssignmentExpression)
3712   //     | (('get' | 'set') (IdentifierName | String | Number) FunctionLiteral)
3713   //    )*[','] '}'
3714
3715   int pos = peek_position();
3716   ZoneList<ObjectLiteral::Property*>* properties =
3717       new(zone()) ZoneList<ObjectLiteral::Property*>(4, zone());
3718   int number_of_boilerplate_properties = 0;
3719   bool has_function = false;
3720
3721   ObjectLiteralChecker checker(this, top_scope_->language_mode());
3722
3723   Expect(Token::LBRACE, CHECK_OK);
3724
3725   while (peek() != Token::RBRACE) {
3726     if (fni_ != NULL) fni_->Enter();
3727
3728     Literal* key = NULL;
3729     Token::Value next = peek();
3730     int next_pos = peek_position();
3731
3732     switch (next) {
3733       case Token::FUTURE_RESERVED_WORD:
3734       case Token::FUTURE_STRICT_RESERVED_WORD:
3735       case Token::IDENTIFIER: {
3736         bool is_getter = false;
3737         bool is_setter = false;
3738         Handle<String> id =
3739             ParseIdentifierNameOrGetOrSet(&is_getter, &is_setter, CHECK_OK);
3740         if (fni_ != NULL) fni_->PushLiteralName(id);
3741
3742         if ((is_getter || is_setter) && peek() != Token::COLON) {
3743           // Special handling of getter and setter syntax:
3744           // { ... , get foo() { ... }, ... , set foo(v) { ... v ... } , ... }
3745           // We have already read the "get" or "set" keyword.
3746           Token::Value next = Next();
3747           bool is_keyword = Token::IsKeyword(next);
3748           if (next != i::Token::IDENTIFIER &&
3749               next != i::Token::FUTURE_RESERVED_WORD &&
3750               next != i::Token::FUTURE_STRICT_RESERVED_WORD &&
3751               next != i::Token::NUMBER &&
3752               next != i::Token::STRING &&
3753               !is_keyword) {
3754             // Unexpected token.
3755             ReportUnexpectedToken(next);
3756             *ok = false;
3757             return NULL;
3758           }
3759           // Validate the property.
3760           PropertyKind type = is_getter ? kGetterProperty : kSetterProperty;
3761           checker.CheckProperty(next, type, CHECK_OK);
3762           Handle<String> name = is_keyword
3763               ? isolate_->factory()->InternalizeUtf8String(Token::String(next))
3764               : GetSymbol();
3765           FunctionLiteral* value =
3766               ParseFunctionLiteral(name,
3767                                    false,   // reserved words are allowed here
3768                                    false,   // not a generator
3769                                    RelocInfo::kNoPosition,
3770                                    FunctionLiteral::ANONYMOUS_EXPRESSION,
3771                                    CHECK_OK);
3772           // Allow any number of parameters for compatibilty with JSC.
3773           // Specification only allows zero parameters for get and one for set.
3774           ObjectLiteral::Property* property =
3775               factory()->NewObjectLiteralProperty(is_getter, value, next_pos);
3776           if (ObjectLiteral::IsBoilerplateProperty(property)) {
3777             number_of_boilerplate_properties++;
3778           }
3779           properties->Add(property, zone());
3780           if (peek() != Token::RBRACE) Expect(Token::COMMA, CHECK_OK);
3781
3782           if (fni_ != NULL) {
3783             fni_->Infer();
3784             fni_->Leave();
3785           }
3786           continue;  // restart the while
3787         }
3788         // Failed to parse as get/set property, so it's just a property
3789         // called "get" or "set".
3790         key = factory()->NewLiteral(id, next_pos);
3791         break;
3792       }
3793       case Token::STRING: {
3794         Consume(Token::STRING);
3795         Handle<String> string = GetSymbol();
3796         if (fni_ != NULL) fni_->PushLiteralName(string);
3797         uint32_t index;
3798         if (!string.is_null() && string->AsArrayIndex(&index)) {
3799           key = factory()->NewNumberLiteral(index, next_pos);
3800           break;
3801         }
3802         key = factory()->NewLiteral(string, next_pos);
3803         break;
3804       }
3805       case Token::NUMBER: {
3806         Consume(Token::NUMBER);
3807         ASSERT(scanner().is_literal_ascii());
3808         double value = StringToDouble(isolate()->unicode_cache(),
3809                                       scanner().literal_ascii_string(),
3810                                       ALLOW_HEX | ALLOW_OCTAL |
3811                                           ALLOW_IMPLICIT_OCTAL | ALLOW_BINARY);
3812         key = factory()->NewNumberLiteral(value, next_pos);
3813         break;
3814       }
3815       default:
3816         if (Token::IsKeyword(next)) {
3817           Consume(next);
3818           Handle<String> string = GetSymbol();
3819           key = factory()->NewLiteral(string, next_pos);
3820         } else {
3821           // Unexpected token.
3822           Token::Value next = Next();
3823           ReportUnexpectedToken(next);
3824           *ok = false;
3825           return NULL;
3826         }
3827     }
3828
3829     // Validate the property
3830     checker.CheckProperty(next, kValueProperty, CHECK_OK);
3831
3832     Expect(Token::COLON, CHECK_OK);
3833     Expression* value = ParseAssignmentExpression(true, CHECK_OK);
3834
3835     ObjectLiteral::Property* property =
3836         factory()->NewObjectLiteralProperty(key, value);
3837
3838     // Mark top-level object literals that contain function literals and
3839     // pretenure the literal so it can be added as a constant function
3840     // property.
3841     if (top_scope_->DeclarationScope()->is_global_scope() &&
3842         value->AsFunctionLiteral() != NULL) {
3843       has_function = true;
3844       value->AsFunctionLiteral()->set_pretenure();
3845     }
3846
3847     // Count CONSTANT or COMPUTED properties to maintain the enumeration order.
3848     if (ObjectLiteral::IsBoilerplateProperty(property)) {
3849       number_of_boilerplate_properties++;
3850     }
3851     properties->Add(property, zone());
3852
3853     // TODO(1240767): Consider allowing trailing comma.
3854     if (peek() != Token::RBRACE) Expect(Token::COMMA, CHECK_OK);
3855
3856     if (fni_ != NULL) {
3857       fni_->Infer();
3858       fni_->Leave();
3859     }
3860   }
3861   Expect(Token::RBRACE, CHECK_OK);
3862
3863   // Computation of literal_index must happen before pre parse bailout.
3864   int literal_index = current_function_state_->NextMaterializedLiteralIndex();
3865
3866   return factory()->NewObjectLiteral(properties,
3867                                      literal_index,
3868                                      number_of_boilerplate_properties,
3869                                      has_function,
3870                                      pos);
3871 }
3872
3873
3874 Expression* Parser::ParseRegExpLiteral(bool seen_equal, bool* ok) {
3875   int pos = peek_position();
3876   if (!scanner().ScanRegExpPattern(seen_equal)) {
3877     Next();
3878     ReportMessage("unterminated_regexp", Vector<const char*>::empty());
3879     *ok = false;
3880     return NULL;
3881   }
3882
3883   int literal_index = current_function_state_->NextMaterializedLiteralIndex();
3884
3885   Handle<String> js_pattern = NextLiteralString(TENURED);
3886   scanner().ScanRegExpFlags();
3887   Handle<String> js_flags = NextLiteralString(TENURED);
3888   Next();
3889
3890   return factory()->NewRegExpLiteral(js_pattern, js_flags, literal_index, pos);
3891 }
3892
3893
3894 ZoneList<Expression*>* Parser::ParseArguments(bool* ok) {
3895   // Arguments ::
3896   //   '(' (AssignmentExpression)*[','] ')'
3897
3898   ZoneList<Expression*>* result = new(zone()) ZoneList<Expression*>(4, zone());
3899   Expect(Token::LPAREN, CHECK_OK);
3900   bool done = (peek() == Token::RPAREN);
3901   while (!done) {
3902     Expression* argument = ParseAssignmentExpression(true, CHECK_OK);
3903     result->Add(argument, zone());
3904     if (result->length() > Code::kMaxArguments) {
3905       ReportMessageAt(scanner().location(), "too_many_arguments",
3906                       Vector<const char*>::empty());
3907       *ok = false;
3908       return NULL;
3909     }
3910     done = (peek() == Token::RPAREN);
3911     if (!done) Expect(Token::COMMA, CHECK_OK);
3912   }
3913   Expect(Token::RPAREN, CHECK_OK);
3914   return result;
3915 }
3916
3917
3918 class SingletonLogger : public ParserRecorder {
3919  public:
3920   SingletonLogger() : has_error_(false), start_(-1), end_(-1) { }
3921   virtual ~SingletonLogger() { }
3922
3923   void Reset() { has_error_ = false; }
3924
3925   virtual void LogFunction(int start,
3926                            int end,
3927                            int literals,
3928                            int properties,
3929                            LanguageMode mode) {
3930     ASSERT(!has_error_);
3931     start_ = start;
3932     end_ = end;
3933     literals_ = literals;
3934     properties_ = properties;
3935     mode_ = mode;
3936   };
3937
3938   // Logs a symbol creation of a literal or identifier.
3939   virtual void LogAsciiSymbol(int start, Vector<const char> literal) { }
3940   virtual void LogUtf16Symbol(int start, Vector<const uc16> literal) { }
3941
3942   // Logs an error message and marks the log as containing an error.
3943   // Further logging will be ignored, and ExtractData will return a vector
3944   // representing the error only.
3945   virtual void LogMessage(int start,
3946                           int end,
3947                           const char* message,
3948                           const char* argument_opt) {
3949     if (has_error_) return;
3950     has_error_ = true;
3951     start_ = start;
3952     end_ = end;
3953     message_ = message;
3954     argument_opt_ = argument_opt;
3955   }
3956
3957   virtual int function_position() { return 0; }
3958
3959   virtual int symbol_position() { return 0; }
3960
3961   virtual int symbol_ids() { return -1; }
3962
3963   virtual Vector<unsigned> ExtractData() {
3964     UNREACHABLE();
3965     return Vector<unsigned>();
3966   }
3967
3968   virtual void PauseRecording() { }
3969
3970   virtual void ResumeRecording() { }
3971
3972   bool has_error() { return has_error_; }
3973
3974   int start() { return start_; }
3975   int end() { return end_; }
3976   int literals() {
3977     ASSERT(!has_error_);
3978     return literals_;
3979   }
3980   int properties() {
3981     ASSERT(!has_error_);
3982     return properties_;
3983   }
3984   LanguageMode language_mode() {
3985     ASSERT(!has_error_);
3986     return mode_;
3987   }
3988   const char* message() {
3989     ASSERT(has_error_);
3990     return message_;
3991   }
3992   const char* argument_opt() {
3993     ASSERT(has_error_);
3994     return argument_opt_;
3995   }
3996
3997  private:
3998   bool has_error_;
3999   int start_;
4000   int end_;
4001   // For function entries.
4002   int literals_;
4003   int properties_;
4004   LanguageMode mode_;
4005   // For error messages.
4006   const char* message_;
4007   const char* argument_opt_;
4008 };
4009
4010
4011 FunctionLiteral* Parser::ParseFunctionLiteral(
4012     Handle<String> function_name,
4013     bool name_is_strict_reserved,
4014     bool is_generator,
4015     int function_token_pos,
4016     FunctionLiteral::FunctionType function_type,
4017     bool* ok) {
4018   // Function ::
4019   //   '(' FormalParameterList? ')' '{' FunctionBody '}'
4020
4021   int pos = function_token_pos == RelocInfo::kNoPosition
4022       ? peek_position() : function_token_pos;
4023
4024   // Anonymous functions were passed either the empty symbol or a null
4025   // handle as the function name.  Remember if we were passed a non-empty
4026   // handle to decide whether to invoke function name inference.
4027   bool should_infer_name = function_name.is_null();
4028
4029   // We want a non-null handle as the function name.
4030   if (should_infer_name) {
4031     function_name = isolate()->factory()->empty_string();
4032   }
4033
4034   int num_parameters = 0;
4035   // Function declarations are function scoped in normal mode, so they are
4036   // hoisted. In harmony block scoping mode they are block scoped, so they
4037   // are not hoisted.
4038   //
4039   // One tricky case are function declarations in a local sloppy-mode eval:
4040   // their declaration is hoisted, but they still see the local scope. E.g.,
4041   //
4042   // function() {
4043   //   var x = 0
4044   //   try { throw 1 } catch (x) { eval("function g() { return x }") }
4045   //   return g()
4046   // }
4047   //
4048   // needs to return 1. To distinguish such cases, we need to detect
4049   // (1) whether a function stems from a sloppy eval, and
4050   // (2) whether it actually hoists across the eval.
4051   // Unfortunately, we do not represent sloppy eval scopes, so we do not have
4052   // either information available directly, especially not when lazily compiling
4053   // a function like 'g'. We hence rely on the following invariants:
4054   // - (1) is the case iff the innermost scope of the deserialized scope chain
4055   //   under which we compile is _not_ a declaration scope. This holds because
4056   //   in all normal cases, function declarations are fully hoisted to a
4057   //   declaration scope and compiled relative to that.
4058   // - (2) is the case iff the current declaration scope is still the original
4059   //   one relative to the deserialized scope chain. Otherwise we must be
4060   //   compiling a function in an inner declaration scope in the eval, e.g. a
4061   //   nested function, and hoisting works normally relative to that.
4062   Scope* declaration_scope = top_scope_->DeclarationScope();
4063   Scope* original_declaration_scope = original_scope_->DeclarationScope();
4064   Scope* scope =
4065       function_type == FunctionLiteral::DECLARATION && !is_extended_mode() &&
4066       (original_scope_ == original_declaration_scope ||
4067        declaration_scope != original_declaration_scope)
4068           ? NewScope(declaration_scope, FUNCTION_SCOPE)
4069           : NewScope(top_scope_, FUNCTION_SCOPE);
4070   ZoneList<Statement*>* body = NULL;
4071   int materialized_literal_count = -1;
4072   int expected_property_count = -1;
4073   int handler_count = 0;
4074   FunctionLiteral::ParameterFlag duplicate_parameters =
4075       FunctionLiteral::kNoDuplicateParameters;
4076   FunctionLiteral::IsParenthesizedFlag parenthesized = parenthesized_function_
4077       ? FunctionLiteral::kIsParenthesized
4078       : FunctionLiteral::kNotParenthesized;
4079   FunctionLiteral::IsGeneratorFlag generator = is_generator
4080       ? FunctionLiteral::kIsGenerator
4081       : FunctionLiteral::kNotGenerator;
4082   AstProperties ast_properties;
4083   BailoutReason dont_optimize_reason = kNoReason;
4084   // Parse function body.
4085   { FunctionState function_state(this, scope);
4086     top_scope_->SetScopeName(function_name);
4087
4088     if (is_generator) {
4089       // For generators, allocating variables in contexts is currently a win
4090       // because it minimizes the work needed to suspend and resume an
4091       // activation.
4092       top_scope_->ForceContextAllocation();
4093
4094       // Calling a generator returns a generator object.  That object is stored
4095       // in a temporary variable, a definition that is used by "yield"
4096       // expressions.  Presence of a variable for the generator object in the
4097       // FunctionState indicates that this function is a generator.
4098       Variable* temp = top_scope_->DeclarationScope()->NewTemporary(
4099           isolate()->factory()->dot_generator_object_string());
4100       function_state.set_generator_object_variable(temp);
4101     }
4102
4103     //  FormalParameterList ::
4104     //    '(' (Identifier)*[','] ')'
4105     Expect(Token::LPAREN, CHECK_OK);
4106     scope->set_start_position(scanner().location().beg_pos);
4107     Scanner::Location name_loc = Scanner::Location::invalid();
4108     Scanner::Location dupe_loc = Scanner::Location::invalid();
4109     Scanner::Location reserved_loc = Scanner::Location::invalid();
4110
4111     bool done = (peek() == Token::RPAREN);
4112     while (!done) {
4113       bool is_strict_reserved = false;
4114       Handle<String> param_name =
4115           ParseIdentifierOrStrictReservedWord(&is_strict_reserved, CHECK_OK);
4116
4117       // Store locations for possible future error reports.
4118       if (!name_loc.IsValid() && IsEvalOrArguments(param_name)) {
4119         name_loc = scanner().location();
4120       }
4121       if (!dupe_loc.IsValid() && top_scope_->IsDeclared(param_name)) {
4122         duplicate_parameters = FunctionLiteral::kHasDuplicateParameters;
4123         dupe_loc = scanner().location();
4124       }
4125       if (!reserved_loc.IsValid() && is_strict_reserved) {
4126         reserved_loc = scanner().location();
4127       }
4128
4129       top_scope_->DeclareParameter(param_name, VAR);
4130       num_parameters++;
4131       if (num_parameters > Code::kMaxArguments) {
4132         ReportMessageAt(scanner().location(), "too_many_parameters",
4133                         Vector<const char*>::empty());
4134         *ok = false;
4135         return NULL;
4136       }
4137       done = (peek() == Token::RPAREN);
4138       if (!done) Expect(Token::COMMA, CHECK_OK);
4139     }
4140     Expect(Token::RPAREN, CHECK_OK);
4141
4142     Expect(Token::LBRACE, CHECK_OK);
4143
4144     // If we have a named function expression, we add a local variable
4145     // declaration to the body of the function with the name of the
4146     // function and let it refer to the function itself (closure).
4147     // NOTE: We create a proxy and resolve it here so that in the
4148     // future we can change the AST to only refer to VariableProxies
4149     // instead of Variables and Proxis as is the case now.
4150     Variable* fvar = NULL;
4151     Token::Value fvar_init_op = Token::INIT_CONST;
4152     if (function_type == FunctionLiteral::NAMED_EXPRESSION) {
4153       if (is_extended_mode()) fvar_init_op = Token::INIT_CONST_HARMONY;
4154       VariableMode fvar_mode = is_extended_mode() ? CONST_HARMONY : CONST;
4155       fvar = new(zone()) Variable(top_scope_,
4156          function_name, fvar_mode, true /* is valid LHS */,
4157          Variable::NORMAL, kCreatedInitialized, Interface::NewConst());
4158       VariableProxy* proxy = factory()->NewVariableProxy(fvar);
4159       VariableDeclaration* fvar_declaration = factory()->NewVariableDeclaration(
4160           proxy, fvar_mode, top_scope_, RelocInfo::kNoPosition);
4161       top_scope_->DeclareFunctionVar(fvar_declaration);
4162     }
4163
4164     // Determine whether the function will be lazily compiled.
4165     // The heuristics are:
4166     // - It must not have been prohibited by the caller to Parse (some callers
4167     //   need a full AST).
4168     // - The outer scope must allow lazy compilation of inner functions.
4169     // - The function mustn't be a function expression with an open parenthesis
4170     //   before; we consider that a hint that the function will be called
4171     //   immediately, and it would be a waste of time to make it lazily
4172     //   compiled.
4173     // These are all things we can know at this point, without looking at the
4174     // function itself.
4175     bool is_lazily_compiled = (mode() == PARSE_LAZILY &&
4176                                top_scope_->AllowsLazyCompilation() &&
4177                                !parenthesized_function_);
4178     parenthesized_function_ = false;  // The bit was set for this function only.
4179
4180     if (is_lazily_compiled) {
4181       int function_block_pos = position();
4182       FunctionEntry entry;
4183       if (pre_parse_data_ != NULL) {
4184         // If we have pre_parse_data_, we use it to skip parsing the function
4185         // body.  The preparser data contains the information we need to
4186         // construct the lazy function.
4187         entry = pre_parse_data()->GetFunctionEntry(function_block_pos);
4188         if (entry.is_valid()) {
4189           if (entry.end_pos() <= function_block_pos) {
4190             // End position greater than end of stream is safe, and hard
4191             // to check.
4192             ReportInvalidPreparseData(function_name, CHECK_OK);
4193           }
4194           scanner().SeekForward(entry.end_pos() - 1);
4195
4196           scope->set_end_position(entry.end_pos());
4197           Expect(Token::RBRACE, CHECK_OK);
4198           isolate()->counters()->total_preparse_skipped()->Increment(
4199               scope->end_position() - function_block_pos);
4200           materialized_literal_count = entry.literal_count();
4201           expected_property_count = entry.property_count();
4202           top_scope_->SetLanguageMode(entry.language_mode());
4203         } else {
4204           is_lazily_compiled = false;
4205         }
4206       } else {
4207         // With no preparser data, we partially parse the function, without
4208         // building an AST. This gathers the data needed to build a lazy
4209         // function.
4210         SingletonLogger logger;
4211         PreParser::PreParseResult result = LazyParseFunctionLiteral(&logger);
4212         if (result == PreParser::kPreParseStackOverflow) {
4213           // Propagate stack overflow.
4214           set_stack_overflow();
4215           *ok = false;
4216           return NULL;
4217         }
4218         if (logger.has_error()) {
4219           const char* arg = logger.argument_opt();
4220           Vector<const char*> args;
4221           if (arg != NULL) {
4222             args = Vector<const char*>(&arg, 1);
4223           }
4224           ReportMessageAt(Scanner::Location(logger.start(), logger.end()),
4225                           logger.message(), args);
4226           *ok = false;
4227           return NULL;
4228         }
4229         scope->set_end_position(logger.end());
4230         Expect(Token::RBRACE, CHECK_OK);
4231         isolate()->counters()->total_preparse_skipped()->Increment(
4232             scope->end_position() - function_block_pos);
4233         materialized_literal_count = logger.literals();
4234         expected_property_count = logger.properties();
4235         top_scope_->SetLanguageMode(logger.language_mode());
4236       }
4237     }
4238
4239     if (!is_lazily_compiled) {
4240       ParsingModeScope parsing_mode(this, PARSE_EAGERLY);
4241       body = new(zone()) ZoneList<Statement*>(8, zone());
4242       if (fvar != NULL) {
4243         VariableProxy* fproxy = top_scope_->NewUnresolved(
4244             factory(), function_name, Interface::NewConst());
4245         fproxy->BindTo(fvar);
4246         body->Add(factory()->NewExpressionStatement(
4247             factory()->NewAssignment(fvar_init_op,
4248                                      fproxy,
4249                                      factory()->NewThisFunction(pos),
4250                                      RelocInfo::kNoPosition),
4251             RelocInfo::kNoPosition), zone());
4252       }
4253
4254       // For generators, allocate and yield an iterator on function entry.
4255       if (is_generator) {
4256         ZoneList<Expression*>* arguments =
4257             new(zone()) ZoneList<Expression*>(0, zone());
4258         CallRuntime* allocation = factory()->NewCallRuntime(
4259             isolate()->factory()->empty_string(),
4260             Runtime::FunctionForId(Runtime::kCreateJSGeneratorObject),
4261             arguments, pos);
4262         VariableProxy* init_proxy = factory()->NewVariableProxy(
4263             current_function_state_->generator_object_variable());
4264         Assignment* assignment = factory()->NewAssignment(
4265             Token::INIT_VAR, init_proxy, allocation, RelocInfo::kNoPosition);
4266         VariableProxy* get_proxy = factory()->NewVariableProxy(
4267             current_function_state_->generator_object_variable());
4268         Yield* yield = factory()->NewYield(
4269             get_proxy, assignment, Yield::INITIAL, RelocInfo::kNoPosition);
4270         body->Add(factory()->NewExpressionStatement(
4271             yield, RelocInfo::kNoPosition), zone());
4272       }
4273
4274       ParseSourceElements(body, Token::RBRACE, false, false, CHECK_OK);
4275
4276       if (is_generator) {
4277         VariableProxy* get_proxy = factory()->NewVariableProxy(
4278             current_function_state_->generator_object_variable());
4279         Expression *undefined = factory()->NewLiteral(
4280             isolate()->factory()->undefined_value(), RelocInfo::kNoPosition);
4281         Yield* yield = factory()->NewYield(
4282             get_proxy, undefined, Yield::FINAL, RelocInfo::kNoPosition);
4283         body->Add(factory()->NewExpressionStatement(
4284             yield, RelocInfo::kNoPosition), zone());
4285       }
4286
4287       materialized_literal_count = function_state.materialized_literal_count();
4288       expected_property_count = function_state.expected_property_count();
4289       handler_count = function_state.handler_count();
4290
4291       Expect(Token::RBRACE, CHECK_OK);
4292       scope->set_end_position(scanner().location().end_pos);
4293     }
4294
4295     // Validate strict mode.
4296     if (!top_scope_->is_classic_mode()) {
4297       if (IsEvalOrArguments(function_name)) {
4298         int start_pos = scope->start_position();
4299         int position = function_token_pos != RelocInfo::kNoPosition
4300             ? function_token_pos : (start_pos > 0 ? start_pos - 1 : start_pos);
4301         Scanner::Location location = Scanner::Location(position, start_pos);
4302         ReportMessageAt(location,
4303                         "strict_function_name", Vector<const char*>::empty());
4304         *ok = false;
4305         return NULL;
4306       }
4307       if (name_loc.IsValid()) {
4308         ReportMessageAt(name_loc, "strict_param_name",
4309                         Vector<const char*>::empty());
4310         *ok = false;
4311         return NULL;
4312       }
4313       if (dupe_loc.IsValid()) {
4314         ReportMessageAt(dupe_loc, "strict_param_dupe",
4315                         Vector<const char*>::empty());
4316         *ok = false;
4317         return NULL;
4318       }
4319       if (name_is_strict_reserved) {
4320         int start_pos = scope->start_position();
4321         int position = function_token_pos != RelocInfo::kNoPosition
4322             ? function_token_pos : (start_pos > 0 ? start_pos - 1 : start_pos);
4323         Scanner::Location location = Scanner::Location(position, start_pos);
4324         ReportMessageAt(location, "strict_reserved_word",
4325                         Vector<const char*>::empty());
4326         *ok = false;
4327         return NULL;
4328       }
4329       if (reserved_loc.IsValid()) {
4330         ReportMessageAt(reserved_loc, "strict_reserved_word",
4331                         Vector<const char*>::empty());
4332         *ok = false;
4333         return NULL;
4334       }
4335       CheckOctalLiteral(scope->start_position(),
4336                         scope->end_position(),
4337                         CHECK_OK);
4338     }
4339     ast_properties = *factory()->visitor()->ast_properties();
4340     dont_optimize_reason = factory()->visitor()->dont_optimize_reason();
4341   }
4342
4343   if (is_extended_mode()) {
4344     CheckConflictingVarDeclarations(scope, CHECK_OK);
4345   }
4346
4347   FunctionLiteral* function_literal =
4348       factory()->NewFunctionLiteral(function_name,
4349                                     scope,
4350                                     body,
4351                                     materialized_literal_count,
4352                                     expected_property_count,
4353                                     handler_count,
4354                                     num_parameters,
4355                                     duplicate_parameters,
4356                                     function_type,
4357                                     FunctionLiteral::kIsFunction,
4358                                     parenthesized,
4359                                     generator,
4360                                     pos);
4361   function_literal->set_function_token_position(function_token_pos);
4362   function_literal->set_ast_properties(&ast_properties);
4363   function_literal->set_dont_optimize_reason(dont_optimize_reason);
4364
4365   if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal);
4366   return function_literal;
4367 }
4368
4369
4370 PreParser::PreParseResult Parser::LazyParseFunctionLiteral(
4371     SingletonLogger* logger) {
4372   HistogramTimerScope preparse_scope(isolate()->counters()->pre_parse());
4373   ASSERT_EQ(Token::LBRACE, scanner().current_token());
4374
4375   if (reusable_preparser_ == NULL) {
4376     intptr_t stack_limit = isolate()->stack_guard()->real_climit();
4377     reusable_preparser_ = new PreParser(&scanner_, NULL, stack_limit);
4378     reusable_preparser_->set_allow_harmony_scoping(allow_harmony_scoping());
4379     reusable_preparser_->set_allow_modules(allow_modules());
4380     reusable_preparser_->set_allow_natives_syntax(allow_natives_syntax());
4381     reusable_preparser_->set_allow_lazy(true);
4382     reusable_preparser_->set_allow_generators(allow_generators());
4383     reusable_preparser_->set_allow_for_of(allow_for_of());
4384     reusable_preparser_->set_allow_harmony_numeric_literals(
4385         allow_harmony_numeric_literals());
4386   }
4387   PreParser::PreParseResult result =
4388       reusable_preparser_->PreParseLazyFunction(top_scope_->language_mode(),
4389                                                 is_generator(),
4390                                                 logger);
4391   return result;
4392 }
4393
4394
4395 Expression* Parser::ParseV8Intrinsic(bool* ok) {
4396   // CallRuntime ::
4397   //   '%' Identifier Arguments
4398
4399   int pos = peek_position();
4400   Expect(Token::MOD, CHECK_OK);
4401   Handle<String> name = ParseIdentifier(CHECK_OK);
4402   ZoneList<Expression*>* args = ParseArguments(CHECK_OK);
4403
4404   if (extension_ != NULL) {
4405     // The extension structures are only accessible while parsing the
4406     // very first time not when reparsing because of lazy compilation.
4407     top_scope_->DeclarationScope()->ForceEagerCompilation();
4408   }
4409
4410   const Runtime::Function* function = Runtime::FunctionForName(name);
4411
4412   // Check for built-in IS_VAR macro.
4413   if (function != NULL &&
4414       function->intrinsic_type == Runtime::RUNTIME &&
4415       function->function_id == Runtime::kIS_VAR) {
4416     // %IS_VAR(x) evaluates to x if x is a variable,
4417     // leads to a parse error otherwise.  Could be implemented as an
4418     // inline function %_IS_VAR(x) to eliminate this special case.
4419     if (args->length() == 1 && args->at(0)->AsVariableProxy() != NULL) {
4420       return args->at(0);
4421     } else {
4422       ReportMessage("not_isvar", Vector<const char*>::empty());
4423       *ok = false;
4424       return NULL;
4425     }
4426   }
4427
4428   // Check that the expected number of arguments are being passed.
4429   if (function != NULL &&
4430       function->nargs != -1 &&
4431       function->nargs != args->length()) {
4432     ReportMessage("illegal_access", Vector<const char*>::empty());
4433     *ok = false;
4434     return NULL;
4435   }
4436
4437   // Check that the function is defined if it's an inline runtime call.
4438   if (function == NULL && name->Get(0) == '_') {
4439     ReportMessage("not_defined", Vector<Handle<String> >(&name, 1));
4440     *ok = false;
4441     return NULL;
4442   }
4443
4444   // We have a valid intrinsics call or a call to a builtin.
4445   return factory()->NewCallRuntime(name, function, args, pos);
4446 }
4447
4448
4449 bool ParserBase::peek_any_identifier() {
4450   Token::Value next = peek();
4451   return next == Token::IDENTIFIER ||
4452          next == Token::FUTURE_RESERVED_WORD ||
4453          next == Token::FUTURE_STRICT_RESERVED_WORD ||
4454          next == Token::YIELD;
4455 }
4456
4457
4458 bool ParserBase::CheckContextualKeyword(Vector<const char> keyword) {
4459   if (peek() == Token::IDENTIFIER &&
4460       scanner()->is_next_contextual_keyword(keyword)) {
4461     Consume(Token::IDENTIFIER);
4462     return true;
4463   }
4464   return false;
4465 }
4466
4467
4468 void ParserBase::ExpectSemicolon(bool* ok) {
4469   // Check for automatic semicolon insertion according to
4470   // the rules given in ECMA-262, section 7.9, page 21.
4471   Token::Value tok = peek();
4472   if (tok == Token::SEMICOLON) {
4473     Next();
4474     return;
4475   }
4476   if (scanner()->HasAnyLineTerminatorBeforeNext() ||
4477       tok == Token::RBRACE ||
4478       tok == Token::EOS) {
4479     return;
4480   }
4481   Expect(Token::SEMICOLON, ok);
4482 }
4483
4484
4485 void ParserBase::ExpectContextualKeyword(Vector<const char> keyword, bool* ok) {
4486   Expect(Token::IDENTIFIER, ok);
4487   if (!*ok) return;
4488   if (!scanner()->is_literal_contextual_keyword(keyword)) {
4489     ReportUnexpectedToken(scanner()->current_token());
4490     *ok = false;
4491   }
4492 }
4493
4494
4495 Literal* Parser::GetLiteralUndefined(int position) {
4496   return factory()->NewLiteral(
4497       isolate()->factory()->undefined_value(), position);
4498 }
4499
4500
4501 Literal* Parser::GetLiteralTheHole(int position) {
4502   return factory()->NewLiteral(
4503       isolate()->factory()->the_hole_value(), RelocInfo::kNoPosition);
4504 }
4505
4506
4507 // Parses an identifier that is valid for the current scope, in particular it
4508 // fails on strict mode future reserved keywords in a strict scope.
4509 Handle<String> Parser::ParseIdentifier(bool* ok) {
4510   Token::Value next = Next();
4511   if (next == Token::IDENTIFIER ||
4512       (top_scope_->is_classic_mode() &&
4513        (next == Token::FUTURE_STRICT_RESERVED_WORD ||
4514         (next == Token::YIELD && !is_generator())))) {
4515     return GetSymbol();
4516   } else {
4517     ReportUnexpectedToken(next);
4518     *ok = false;
4519     return Handle<String>();
4520   }
4521 }
4522
4523
4524 // Parses and identifier or a strict mode future reserved word, and indicate
4525 // whether it is strict mode future reserved.
4526 Handle<String> Parser::ParseIdentifierOrStrictReservedWord(
4527     bool* is_strict_reserved, bool* ok) {
4528   Token::Value next = Next();
4529   if (next == Token::IDENTIFIER) {
4530     *is_strict_reserved = false;
4531   } else if (next == Token::FUTURE_STRICT_RESERVED_WORD ||
4532              (next == Token::YIELD && !is_generator())) {
4533     *is_strict_reserved = true;
4534   } else {
4535     ReportUnexpectedToken(next);
4536     *ok = false;
4537     return Handle<String>();
4538   }
4539   return GetSymbol();
4540 }
4541
4542
4543 Handle<String> Parser::ParseIdentifierName(bool* ok) {
4544   Token::Value next = Next();
4545   if (next != Token::IDENTIFIER &&
4546       next != Token::FUTURE_RESERVED_WORD &&
4547       next != Token::FUTURE_STRICT_RESERVED_WORD &&
4548       !Token::IsKeyword(next)) {
4549     ReportUnexpectedToken(next);
4550     *ok = false;
4551     return Handle<String>();
4552   }
4553   return GetSymbol();
4554 }
4555
4556
4557 void Parser::MarkAsLValue(Expression* expression) {
4558   VariableProxy* proxy = expression != NULL
4559       ? expression->AsVariableProxy()
4560       : NULL;
4561
4562   if (proxy != NULL) proxy->MarkAsLValue();
4563 }
4564
4565
4566 // Checks LHS expression for assignment and prefix/postfix increment/decrement
4567 // in strict mode.
4568 void Parser::CheckStrictModeLValue(Expression* expression,
4569                                    const char* error,
4570                                    bool* ok) {
4571   ASSERT(!top_scope_->is_classic_mode());
4572   VariableProxy* lhs = expression != NULL
4573       ? expression->AsVariableProxy()
4574       : NULL;
4575
4576   if (lhs != NULL && !lhs->is_this() && IsEvalOrArguments(lhs->name())) {
4577     ReportMessage(error, Vector<const char*>::empty());
4578     *ok = false;
4579   }
4580 }
4581
4582
4583 // Checks whether an octal literal was last seen between beg_pos and end_pos.
4584 // If so, reports an error. Only called for strict mode.
4585 void ParserBase::CheckOctalLiteral(int beg_pos, int end_pos, bool* ok) {
4586   Scanner::Location octal = scanner()->octal_position();
4587   if (octal.IsValid() && beg_pos <= octal.beg_pos && octal.end_pos <= end_pos) {
4588     ReportMessageAt(octal, "strict_octal_literal");
4589     scanner()->clear_octal_position();
4590     *ok = false;
4591   }
4592 }
4593
4594
4595 void Parser::CheckConflictingVarDeclarations(Scope* scope, bool* ok) {
4596   Declaration* decl = scope->CheckConflictingVarDeclarations();
4597   if (decl != NULL) {
4598     // In harmony mode we treat conflicting variable bindinds as early
4599     // errors. See ES5 16 for a definition of early errors.
4600     Handle<String> name = decl->proxy()->name();
4601     SmartArrayPointer<char> c_string = name->ToCString(DISALLOW_NULLS);
4602     const char* elms[2] = { "Variable", c_string.get() };
4603     Vector<const char*> args(elms, 2);
4604     int position = decl->proxy()->position();
4605     Scanner::Location location = position == RelocInfo::kNoPosition
4606         ? Scanner::Location::invalid()
4607         : Scanner::Location(position, position + 1);
4608     ReportMessageAt(location, "redeclaration", args);
4609     *ok = false;
4610   }
4611 }
4612
4613
4614 // This function reads an identifier name and determines whether or not it
4615 // is 'get' or 'set'.
4616 Handle<String> Parser::ParseIdentifierNameOrGetOrSet(bool* is_get,
4617                                                      bool* is_set,
4618                                                      bool* ok) {
4619   Handle<String> result = ParseIdentifierName(ok);
4620   if (!*ok) return Handle<String>();
4621   if (scanner().is_literal_ascii() && scanner().literal_length() == 3) {
4622     const char* token = scanner().literal_ascii_string().start();
4623     *is_get = strncmp(token, "get", 3) == 0;
4624     *is_set = !*is_get && strncmp(token, "set", 3) == 0;
4625   }
4626   return result;
4627 }
4628
4629
4630 // ----------------------------------------------------------------------------
4631 // Parser support
4632
4633
4634 bool Parser::TargetStackContainsLabel(Handle<String> label) {
4635   for (Target* t = target_stack_; t != NULL; t = t->previous()) {
4636     BreakableStatement* stat = t->node()->AsBreakableStatement();
4637     if (stat != NULL && ContainsLabel(stat->labels(), label))
4638       return true;
4639   }
4640   return false;
4641 }
4642
4643
4644 BreakableStatement* Parser::LookupBreakTarget(Handle<String> label, bool* ok) {
4645   bool anonymous = label.is_null();
4646   for (Target* t = target_stack_; t != NULL; t = t->previous()) {
4647     BreakableStatement* stat = t->node()->AsBreakableStatement();
4648     if (stat == NULL) continue;
4649     if ((anonymous && stat->is_target_for_anonymous()) ||
4650         (!anonymous && ContainsLabel(stat->labels(), label))) {
4651       RegisterTargetUse(stat->break_target(), t->previous());
4652       return stat;
4653     }
4654   }
4655   return NULL;
4656 }
4657
4658
4659 IterationStatement* Parser::LookupContinueTarget(Handle<String> label,
4660                                                  bool* ok) {
4661   bool anonymous = label.is_null();
4662   for (Target* t = target_stack_; t != NULL; t = t->previous()) {
4663     IterationStatement* stat = t->node()->AsIterationStatement();
4664     if (stat == NULL) continue;
4665
4666     ASSERT(stat->is_target_for_anonymous());
4667     if (anonymous || ContainsLabel(stat->labels(), label)) {
4668       RegisterTargetUse(stat->continue_target(), t->previous());
4669       return stat;
4670     }
4671   }
4672   return NULL;
4673 }
4674
4675
4676 void Parser::RegisterTargetUse(Label* target, Target* stop) {
4677   // Register that a break target found at the given stop in the
4678   // target stack has been used from the top of the target stack. Add
4679   // the break target to any TargetCollectors passed on the stack.
4680   for (Target* t = target_stack_; t != stop; t = t->previous()) {
4681     TargetCollector* collector = t->node()->AsTargetCollector();
4682     if (collector != NULL) collector->AddTarget(target, zone());
4683   }
4684 }
4685
4686
4687 Expression* Parser::NewThrowReferenceError(Handle<String> message) {
4688   return NewThrowError(isolate()->factory()->MakeReferenceError_string(),
4689                        message, HandleVector<Object>(NULL, 0));
4690 }
4691
4692
4693 Expression* Parser::NewThrowSyntaxError(Handle<String> message,
4694                                         Handle<Object> first) {
4695   int argc = first.is_null() ? 0 : 1;
4696   Vector< Handle<Object> > arguments = HandleVector<Object>(&first, argc);
4697   return NewThrowError(
4698       isolate()->factory()->MakeSyntaxError_string(), message, arguments);
4699 }
4700
4701
4702 Expression* Parser::NewThrowTypeError(Handle<String> message,
4703                                       Handle<Object> first,
4704                                       Handle<Object> second) {
4705   ASSERT(!first.is_null() && !second.is_null());
4706   Handle<Object> elements[] = { first, second };
4707   Vector< Handle<Object> > arguments =
4708       HandleVector<Object>(elements, ARRAY_SIZE(elements));
4709   return NewThrowError(
4710       isolate()->factory()->MakeTypeError_string(), message, arguments);
4711 }
4712
4713
4714 Expression* Parser::NewThrowError(Handle<String> constructor,
4715                                   Handle<String> message,
4716                                   Vector< Handle<Object> > arguments) {
4717   int argc = arguments.length();
4718   Handle<FixedArray> elements = isolate()->factory()->NewFixedArray(argc,
4719                                                                     TENURED);
4720   for (int i = 0; i < argc; i++) {
4721     Handle<Object> element = arguments[i];
4722     if (!element.is_null()) {
4723       elements->set(i, *element);
4724     }
4725   }
4726   Handle<JSArray> array = isolate()->factory()->NewJSArrayWithElements(
4727       elements, FAST_ELEMENTS, TENURED);
4728
4729   int pos = position();
4730   ZoneList<Expression*>* args = new(zone()) ZoneList<Expression*>(2, zone());
4731   args->Add(factory()->NewLiteral(message, pos), zone());
4732   args->Add(factory()->NewLiteral(array, pos), zone());
4733   CallRuntime* call_constructor =
4734       factory()->NewCallRuntime(constructor, NULL, args, pos);
4735   return factory()->NewThrow(call_constructor, pos);
4736 }
4737
4738
4739 // ----------------------------------------------------------------------------
4740 // Regular expressions
4741
4742
4743 RegExpParser::RegExpParser(FlatStringReader* in,
4744                            Handle<String>* error,
4745                            bool multiline,
4746                            Zone* zone)
4747     : isolate_(zone->isolate()),
4748       zone_(zone),
4749       error_(error),
4750       captures_(NULL),
4751       in_(in),
4752       current_(kEndMarker),
4753       next_pos_(0),
4754       capture_count_(0),
4755       has_more_(true),
4756       multiline_(multiline),
4757       simple_(false),
4758       contains_anchor_(false),
4759       is_scanned_for_captures_(false),
4760       failed_(false) {
4761   Advance();
4762 }
4763
4764
4765 uc32 RegExpParser::Next() {
4766   if (has_next()) {
4767     return in()->Get(next_pos_);
4768   } else {
4769     return kEndMarker;
4770   }
4771 }
4772
4773
4774 void RegExpParser::Advance() {
4775   if (next_pos_ < in()->length()) {
4776     StackLimitCheck check(isolate());
4777     if (check.HasOverflowed()) {
4778       ReportError(CStrVector(Isolate::kStackOverflowMessage));
4779     } else if (zone()->excess_allocation()) {
4780       ReportError(CStrVector("Regular expression too large"));
4781     } else {
4782       current_ = in()->Get(next_pos_);
4783       next_pos_++;
4784     }
4785   } else {
4786     current_ = kEndMarker;
4787     has_more_ = false;
4788   }
4789 }
4790
4791
4792 void RegExpParser::Reset(int pos) {
4793   next_pos_ = pos;
4794   has_more_ = (pos < in()->length());
4795   Advance();
4796 }
4797
4798
4799 void RegExpParser::Advance(int dist) {
4800   next_pos_ += dist - 1;
4801   Advance();
4802 }
4803
4804
4805 bool RegExpParser::simple() {
4806   return simple_;
4807 }
4808
4809
4810 RegExpTree* RegExpParser::ReportError(Vector<const char> message) {
4811   failed_ = true;
4812   *error_ = isolate()->factory()->NewStringFromAscii(message, NOT_TENURED);
4813   // Zip to the end to make sure the no more input is read.
4814   current_ = kEndMarker;
4815   next_pos_ = in()->length();
4816   return NULL;
4817 }
4818
4819
4820 // Pattern ::
4821 //   Disjunction
4822 RegExpTree* RegExpParser::ParsePattern() {
4823   RegExpTree* result = ParseDisjunction(CHECK_FAILED);
4824   ASSERT(!has_more());
4825   // If the result of parsing is a literal string atom, and it has the
4826   // same length as the input, then the atom is identical to the input.
4827   if (result->IsAtom() && result->AsAtom()->length() == in()->length()) {
4828     simple_ = true;
4829   }
4830   return result;
4831 }
4832
4833
4834 // Disjunction ::
4835 //   Alternative
4836 //   Alternative | Disjunction
4837 // Alternative ::
4838 //   [empty]
4839 //   Term Alternative
4840 // Term ::
4841 //   Assertion
4842 //   Atom
4843 //   Atom Quantifier
4844 RegExpTree* RegExpParser::ParseDisjunction() {
4845   // Used to store current state while parsing subexpressions.
4846   RegExpParserState initial_state(NULL, INITIAL, 0, zone());
4847   RegExpParserState* stored_state = &initial_state;
4848   // Cache the builder in a local variable for quick access.
4849   RegExpBuilder* builder = initial_state.builder();
4850   while (true) {
4851     switch (current()) {
4852     case kEndMarker:
4853       if (stored_state->IsSubexpression()) {
4854         // Inside a parenthesized group when hitting end of input.
4855         ReportError(CStrVector("Unterminated group") CHECK_FAILED);
4856       }
4857       ASSERT_EQ(INITIAL, stored_state->group_type());
4858       // Parsing completed successfully.
4859       return builder->ToRegExp();
4860     case ')': {
4861       if (!stored_state->IsSubexpression()) {
4862         ReportError(CStrVector("Unmatched ')'") CHECK_FAILED);
4863       }
4864       ASSERT_NE(INITIAL, stored_state->group_type());
4865
4866       Advance();
4867       // End disjunction parsing and convert builder content to new single
4868       // regexp atom.
4869       RegExpTree* body = builder->ToRegExp();
4870
4871       int end_capture_index = captures_started();
4872
4873       int capture_index = stored_state->capture_index();
4874       SubexpressionType group_type = stored_state->group_type();
4875
4876       // Restore previous state.
4877       stored_state = stored_state->previous_state();
4878       builder = stored_state->builder();
4879
4880       // Build result of subexpression.
4881       if (group_type == CAPTURE) {
4882         RegExpCapture* capture = new(zone()) RegExpCapture(body, capture_index);
4883         captures_->at(capture_index - 1) = capture;
4884         body = capture;
4885       } else if (group_type != GROUPING) {
4886         ASSERT(group_type == POSITIVE_LOOKAHEAD ||
4887                group_type == NEGATIVE_LOOKAHEAD);
4888         bool is_positive = (group_type == POSITIVE_LOOKAHEAD);
4889         body = new(zone()) RegExpLookahead(body,
4890                                    is_positive,
4891                                    end_capture_index - capture_index,
4892                                    capture_index);
4893       }
4894       builder->AddAtom(body);
4895       // For compatability with JSC and ES3, we allow quantifiers after
4896       // lookaheads, and break in all cases.
4897       break;
4898     }
4899     case '|': {
4900       Advance();
4901       builder->NewAlternative();
4902       continue;
4903     }
4904     case '*':
4905     case '+':
4906     case '?':
4907       return ReportError(CStrVector("Nothing to repeat"));
4908     case '^': {
4909       Advance();
4910       if (multiline_) {
4911         builder->AddAssertion(
4912             new(zone()) RegExpAssertion(RegExpAssertion::START_OF_LINE));
4913       } else {
4914         builder->AddAssertion(
4915             new(zone()) RegExpAssertion(RegExpAssertion::START_OF_INPUT));
4916         set_contains_anchor();
4917       }
4918       continue;
4919     }
4920     case '$': {
4921       Advance();
4922       RegExpAssertion::AssertionType assertion_type =
4923           multiline_ ? RegExpAssertion::END_OF_LINE :
4924                        RegExpAssertion::END_OF_INPUT;
4925       builder->AddAssertion(new(zone()) RegExpAssertion(assertion_type));
4926       continue;
4927     }
4928     case '.': {
4929       Advance();
4930       // everything except \x0a, \x0d, \u2028 and \u2029
4931       ZoneList<CharacterRange>* ranges =
4932           new(zone()) ZoneList<CharacterRange>(2, zone());
4933       CharacterRange::AddClassEscape('.', ranges, zone());
4934       RegExpTree* atom = new(zone()) RegExpCharacterClass(ranges, false);
4935       builder->AddAtom(atom);
4936       break;
4937     }
4938     case '(': {
4939       SubexpressionType subexpr_type = CAPTURE;
4940       Advance();
4941       if (current() == '?') {
4942         switch (Next()) {
4943           case ':':
4944             subexpr_type = GROUPING;
4945             break;
4946           case '=':
4947             subexpr_type = POSITIVE_LOOKAHEAD;
4948             break;
4949           case '!':
4950             subexpr_type = NEGATIVE_LOOKAHEAD;
4951             break;
4952           default:
4953             ReportError(CStrVector("Invalid group") CHECK_FAILED);
4954             break;
4955         }
4956         Advance(2);
4957       } else {
4958         if (captures_ == NULL) {
4959           captures_ = new(zone()) ZoneList<RegExpCapture*>(2, zone());
4960         }
4961         if (captures_started() >= kMaxCaptures) {
4962           ReportError(CStrVector("Too many captures") CHECK_FAILED);
4963         }
4964         captures_->Add(NULL, zone());
4965       }
4966       // Store current state and begin new disjunction parsing.
4967       stored_state = new(zone()) RegExpParserState(stored_state, subexpr_type,
4968                                                    captures_started(), zone());
4969       builder = stored_state->builder();
4970       continue;
4971     }
4972     case '[': {
4973       RegExpTree* atom = ParseCharacterClass(CHECK_FAILED);
4974       builder->AddAtom(atom);
4975       break;
4976     }
4977     // Atom ::
4978     //   \ AtomEscape
4979     case '\\':
4980       switch (Next()) {
4981       case kEndMarker:
4982         return ReportError(CStrVector("\\ at end of pattern"));
4983       case 'b':
4984         Advance(2);
4985         builder->AddAssertion(
4986             new(zone()) RegExpAssertion(RegExpAssertion::BOUNDARY));
4987         continue;
4988       case 'B':
4989         Advance(2);
4990         builder->AddAssertion(
4991             new(zone()) RegExpAssertion(RegExpAssertion::NON_BOUNDARY));
4992         continue;
4993       // AtomEscape ::
4994       //   CharacterClassEscape
4995       //
4996       // CharacterClassEscape :: one of
4997       //   d D s S w W
4998       case 'd': case 'D': case 's': case 'S': case 'w': case 'W': {
4999         uc32 c = Next();
5000         Advance(2);
5001         ZoneList<CharacterRange>* ranges =
5002             new(zone()) ZoneList<CharacterRange>(2, zone());
5003         CharacterRange::AddClassEscape(c, ranges, zone());
5004         RegExpTree* atom = new(zone()) RegExpCharacterClass(ranges, false);
5005         builder->AddAtom(atom);
5006         break;
5007       }
5008       case '1': case '2': case '3': case '4': case '5': case '6':
5009       case '7': case '8': case '9': {
5010         int index = 0;
5011         if (ParseBackReferenceIndex(&index)) {
5012           RegExpCapture* capture = NULL;
5013           if (captures_ != NULL && index <= captures_->length()) {
5014             capture = captures_->at(index - 1);
5015           }
5016           if (capture == NULL) {
5017             builder->AddEmpty();
5018             break;
5019           }
5020           RegExpTree* atom = new(zone()) RegExpBackReference(capture);
5021           builder->AddAtom(atom);
5022           break;
5023         }
5024         uc32 first_digit = Next();
5025         if (first_digit == '8' || first_digit == '9') {
5026           // Treat as identity escape
5027           builder->AddCharacter(first_digit);
5028           Advance(2);
5029           break;
5030         }
5031       }
5032       // FALLTHROUGH
5033       case '0': {
5034         Advance();
5035         uc32 octal = ParseOctalLiteral();
5036         builder->AddCharacter(octal);
5037         break;
5038       }
5039       // ControlEscape :: one of
5040       //   f n r t v
5041       case 'f':
5042         Advance(2);
5043         builder->AddCharacter('\f');
5044         break;
5045       case 'n':
5046         Advance(2);
5047         builder->AddCharacter('\n');
5048         break;
5049       case 'r':
5050         Advance(2);
5051         builder->AddCharacter('\r');
5052         break;
5053       case 't':
5054         Advance(2);
5055         builder->AddCharacter('\t');
5056         break;
5057       case 'v':
5058         Advance(2);
5059         builder->AddCharacter('\v');
5060         break;
5061       case 'c': {
5062         Advance();
5063         uc32 controlLetter = Next();
5064         // Special case if it is an ASCII letter.
5065         // Convert lower case letters to uppercase.
5066         uc32 letter = controlLetter & ~('a' ^ 'A');
5067         if (letter < 'A' || 'Z' < letter) {
5068           // controlLetter is not in range 'A'-'Z' or 'a'-'z'.
5069           // This is outside the specification. We match JSC in
5070           // reading the backslash as a literal character instead
5071           // of as starting an escape.
5072           builder->AddCharacter('\\');
5073         } else {
5074           Advance(2);
5075           builder->AddCharacter(controlLetter & 0x1f);
5076         }
5077         break;
5078       }
5079       case 'x': {
5080         Advance(2);
5081         uc32 value;
5082         if (ParseHexEscape(2, &value)) {
5083           builder->AddCharacter(value);
5084         } else {
5085           builder->AddCharacter('x');
5086         }
5087         break;
5088       }
5089       case 'u': {
5090         Advance(2);
5091         uc32 value;
5092         if (ParseHexEscape(4, &value)) {
5093           builder->AddCharacter(value);
5094         } else {
5095           builder->AddCharacter('u');
5096         }
5097         break;
5098       }
5099       default:
5100         // Identity escape.
5101         builder->AddCharacter(Next());
5102         Advance(2);
5103         break;
5104       }
5105       break;
5106     case '{': {
5107       int dummy;
5108       if (ParseIntervalQuantifier(&dummy, &dummy)) {
5109         ReportError(CStrVector("Nothing to repeat") CHECK_FAILED);
5110       }
5111       // fallthrough
5112     }
5113     default:
5114       builder->AddCharacter(current());
5115       Advance();
5116       break;
5117     }  // end switch(current())
5118
5119     int min;
5120     int max;
5121     switch (current()) {
5122     // QuantifierPrefix ::
5123     //   *
5124     //   +
5125     //   ?
5126     //   {
5127     case '*':
5128       min = 0;
5129       max = RegExpTree::kInfinity;
5130       Advance();
5131       break;
5132     case '+':
5133       min = 1;
5134       max = RegExpTree::kInfinity;
5135       Advance();
5136       break;
5137     case '?':
5138       min = 0;
5139       max = 1;
5140       Advance();
5141       break;
5142     case '{':
5143       if (ParseIntervalQuantifier(&min, &max)) {
5144         if (max < min) {
5145           ReportError(CStrVector("numbers out of order in {} quantifier.")
5146                       CHECK_FAILED);
5147         }
5148         break;
5149       } else {
5150         continue;
5151       }
5152     default:
5153       continue;
5154     }
5155     RegExpQuantifier::QuantifierType quantifier_type = RegExpQuantifier::GREEDY;
5156     if (current() == '?') {
5157       quantifier_type = RegExpQuantifier::NON_GREEDY;
5158       Advance();
5159     } else if (FLAG_regexp_possessive_quantifier && current() == '+') {
5160       // FLAG_regexp_possessive_quantifier is a debug-only flag.
5161       quantifier_type = RegExpQuantifier::POSSESSIVE;
5162       Advance();
5163     }
5164     builder->AddQuantifierToAtom(min, max, quantifier_type);
5165   }
5166 }
5167
5168
5169 #ifdef DEBUG
5170 // Currently only used in an ASSERT.
5171 static bool IsSpecialClassEscape(uc32 c) {
5172   switch (c) {
5173     case 'd': case 'D':
5174     case 's': case 'S':
5175     case 'w': case 'W':
5176       return true;
5177     default:
5178       return false;
5179   }
5180 }
5181 #endif
5182
5183
5184 // In order to know whether an escape is a backreference or not we have to scan
5185 // the entire regexp and find the number of capturing parentheses.  However we
5186 // don't want to scan the regexp twice unless it is necessary.  This mini-parser
5187 // is called when needed.  It can see the difference between capturing and
5188 // noncapturing parentheses and can skip character classes and backslash-escaped
5189 // characters.
5190 void RegExpParser::ScanForCaptures() {
5191   // Start with captures started previous to current position
5192   int capture_count = captures_started();
5193   // Add count of captures after this position.
5194   int n;
5195   while ((n = current()) != kEndMarker) {
5196     Advance();
5197     switch (n) {
5198       case '\\':
5199         Advance();
5200         break;
5201       case '[': {
5202         int c;
5203         while ((c = current()) != kEndMarker) {
5204           Advance();
5205           if (c == '\\') {
5206             Advance();
5207           } else {
5208             if (c == ']') break;
5209           }
5210         }
5211         break;
5212       }
5213       case '(':
5214         if (current() != '?') capture_count++;
5215         break;
5216     }
5217   }
5218   capture_count_ = capture_count;
5219   is_scanned_for_captures_ = true;
5220 }
5221
5222
5223 bool RegExpParser::ParseBackReferenceIndex(int* index_out) {
5224   ASSERT_EQ('\\', current());
5225   ASSERT('1' <= Next() && Next() <= '9');
5226   // Try to parse a decimal literal that is no greater than the total number
5227   // of left capturing parentheses in the input.
5228   int start = position();
5229   int value = Next() - '0';
5230   Advance(2);
5231   while (true) {
5232     uc32 c = current();
5233     if (IsDecimalDigit(c)) {
5234       value = 10 * value + (c - '0');
5235       if (value > kMaxCaptures) {
5236         Reset(start);
5237         return false;
5238       }
5239       Advance();
5240     } else {
5241       break;
5242     }
5243   }
5244   if (value > captures_started()) {
5245     if (!is_scanned_for_captures_) {
5246       int saved_position = position();
5247       ScanForCaptures();
5248       Reset(saved_position);
5249     }
5250     if (value > capture_count_) {
5251       Reset(start);
5252       return false;
5253     }
5254   }
5255   *index_out = value;
5256   return true;
5257 }
5258
5259
5260 // QuantifierPrefix ::
5261 //   { DecimalDigits }
5262 //   { DecimalDigits , }
5263 //   { DecimalDigits , DecimalDigits }
5264 //
5265 // Returns true if parsing succeeds, and set the min_out and max_out
5266 // values. Values are truncated to RegExpTree::kInfinity if they overflow.
5267 bool RegExpParser::ParseIntervalQuantifier(int* min_out, int* max_out) {
5268   ASSERT_EQ(current(), '{');
5269   int start = position();
5270   Advance();
5271   int min = 0;
5272   if (!IsDecimalDigit(current())) {
5273     Reset(start);
5274     return false;
5275   }
5276   while (IsDecimalDigit(current())) {
5277     int next = current() - '0';
5278     if (min > (RegExpTree::kInfinity - next) / 10) {
5279       // Overflow. Skip past remaining decimal digits and return -1.
5280       do {
5281         Advance();
5282       } while (IsDecimalDigit(current()));
5283       min = RegExpTree::kInfinity;
5284       break;
5285     }
5286     min = 10 * min + next;
5287     Advance();
5288   }
5289   int max = 0;
5290   if (current() == '}') {
5291     max = min;
5292     Advance();
5293   } else if (current() == ',') {
5294     Advance();
5295     if (current() == '}') {
5296       max = RegExpTree::kInfinity;
5297       Advance();
5298     } else {
5299       while (IsDecimalDigit(current())) {
5300         int next = current() - '0';
5301         if (max > (RegExpTree::kInfinity - next) / 10) {
5302           do {
5303             Advance();
5304           } while (IsDecimalDigit(current()));
5305           max = RegExpTree::kInfinity;
5306           break;
5307         }
5308         max = 10 * max + next;
5309         Advance();
5310       }
5311       if (current() != '}') {
5312         Reset(start);
5313         return false;
5314       }
5315       Advance();
5316     }
5317   } else {
5318     Reset(start);
5319     return false;
5320   }
5321   *min_out = min;
5322   *max_out = max;
5323   return true;
5324 }
5325
5326
5327 uc32 RegExpParser::ParseOctalLiteral() {
5328   ASSERT('0' <= current() && current() <= '7');
5329   // For compatibility with some other browsers (not all), we parse
5330   // up to three octal digits with a value below 256.
5331   uc32 value = current() - '0';
5332   Advance();
5333   if ('0' <= current() && current() <= '7') {
5334     value = value * 8 + current() - '0';
5335     Advance();
5336     if (value < 32 && '0' <= current() && current() <= '7') {
5337       value = value * 8 + current() - '0';
5338       Advance();
5339     }
5340   }
5341   return value;
5342 }
5343
5344
5345 bool RegExpParser::ParseHexEscape(int length, uc32 *value) {
5346   int start = position();
5347   uc32 val = 0;
5348   bool done = false;
5349   for (int i = 0; !done; i++) {
5350     uc32 c = current();
5351     int d = HexValue(c);
5352     if (d < 0) {
5353       Reset(start);
5354       return false;
5355     }
5356     val = val * 16 + d;
5357     Advance();
5358     if (i == length - 1) {
5359       done = true;
5360     }
5361   }
5362   *value = val;
5363   return true;
5364 }
5365
5366
5367 uc32 RegExpParser::ParseClassCharacterEscape() {
5368   ASSERT(current() == '\\');
5369   ASSERT(has_next() && !IsSpecialClassEscape(Next()));
5370   Advance();
5371   switch (current()) {
5372     case 'b':
5373       Advance();
5374       return '\b';
5375     // ControlEscape :: one of
5376     //   f n r t v
5377     case 'f':
5378       Advance();
5379       return '\f';
5380     case 'n':
5381       Advance();
5382       return '\n';
5383     case 'r':
5384       Advance();
5385       return '\r';
5386     case 't':
5387       Advance();
5388       return '\t';
5389     case 'v':
5390       Advance();
5391       return '\v';
5392     case 'c': {
5393       uc32 controlLetter = Next();
5394       uc32 letter = controlLetter & ~('A' ^ 'a');
5395       // For compatibility with JSC, inside a character class
5396       // we also accept digits and underscore as control characters.
5397       if ((controlLetter >= '0' && controlLetter <= '9') ||
5398           controlLetter == '_' ||
5399           (letter >= 'A' && letter <= 'Z')) {
5400         Advance(2);
5401         // Control letters mapped to ASCII control characters in the range
5402         // 0x00-0x1f.
5403         return controlLetter & 0x1f;
5404       }
5405       // We match JSC in reading the backslash as a literal
5406       // character instead of as starting an escape.
5407       return '\\';
5408     }
5409     case '0': case '1': case '2': case '3': case '4': case '5':
5410     case '6': case '7':
5411       // For compatibility, we interpret a decimal escape that isn't
5412       // a back reference (and therefore either \0 or not valid according
5413       // to the specification) as a 1..3 digit octal character code.
5414       return ParseOctalLiteral();
5415     case 'x': {
5416       Advance();
5417       uc32 value;
5418       if (ParseHexEscape(2, &value)) {
5419         return value;
5420       }
5421       // If \x is not followed by a two-digit hexadecimal, treat it
5422       // as an identity escape.
5423       return 'x';
5424     }
5425     case 'u': {
5426       Advance();
5427       uc32 value;
5428       if (ParseHexEscape(4, &value)) {
5429         return value;
5430       }
5431       // If \u is not followed by a four-digit hexadecimal, treat it
5432       // as an identity escape.
5433       return 'u';
5434     }
5435     default: {
5436       // Extended identity escape. We accept any character that hasn't
5437       // been matched by a more specific case, not just the subset required
5438       // by the ECMAScript specification.
5439       uc32 result = current();
5440       Advance();
5441       return result;
5442     }
5443   }
5444   return 0;
5445 }
5446
5447
5448 CharacterRange RegExpParser::ParseClassAtom(uc16* char_class) {
5449   ASSERT_EQ(0, *char_class);
5450   uc32 first = current();
5451   if (first == '\\') {
5452     switch (Next()) {
5453       case 'w': case 'W': case 'd': case 'D': case 's': case 'S': {
5454         *char_class = Next();
5455         Advance(2);
5456         return CharacterRange::Singleton(0);  // Return dummy value.
5457       }
5458       case kEndMarker:
5459         return ReportError(CStrVector("\\ at end of pattern"));
5460       default:
5461         uc32 c = ParseClassCharacterEscape(CHECK_FAILED);
5462         return CharacterRange::Singleton(c);
5463     }
5464   } else {
5465     Advance();
5466     return CharacterRange::Singleton(first);
5467   }
5468 }
5469
5470
5471 static const uc16 kNoCharClass = 0;
5472
5473 // Adds range or pre-defined character class to character ranges.
5474 // If char_class is not kInvalidClass, it's interpreted as a class
5475 // escape (i.e., 's' means whitespace, from '\s').
5476 static inline void AddRangeOrEscape(ZoneList<CharacterRange>* ranges,
5477                                     uc16 char_class,
5478                                     CharacterRange range,
5479                                     Zone* zone) {
5480   if (char_class != kNoCharClass) {
5481     CharacterRange::AddClassEscape(char_class, ranges, zone);
5482   } else {
5483     ranges->Add(range, zone);
5484   }
5485 }
5486
5487
5488 RegExpTree* RegExpParser::ParseCharacterClass() {
5489   static const char* kUnterminated = "Unterminated character class";
5490   static const char* kRangeOutOfOrder = "Range out of order in character class";
5491
5492   ASSERT_EQ(current(), '[');
5493   Advance();
5494   bool is_negated = false;
5495   if (current() == '^') {
5496     is_negated = true;
5497     Advance();
5498   }
5499   ZoneList<CharacterRange>* ranges =
5500       new(zone()) ZoneList<CharacterRange>(2, zone());
5501   while (has_more() && current() != ']') {
5502     uc16 char_class = kNoCharClass;
5503     CharacterRange first = ParseClassAtom(&char_class CHECK_FAILED);
5504     if (current() == '-') {
5505       Advance();
5506       if (current() == kEndMarker) {
5507         // If we reach the end we break out of the loop and let the
5508         // following code report an error.
5509         break;
5510       } else if (current() == ']') {
5511         AddRangeOrEscape(ranges, char_class, first, zone());
5512         ranges->Add(CharacterRange::Singleton('-'), zone());
5513         break;
5514       }
5515       uc16 char_class_2 = kNoCharClass;
5516       CharacterRange next = ParseClassAtom(&char_class_2 CHECK_FAILED);
5517       if (char_class != kNoCharClass || char_class_2 != kNoCharClass) {
5518         // Either end is an escaped character class. Treat the '-' verbatim.
5519         AddRangeOrEscape(ranges, char_class, first, zone());
5520         ranges->Add(CharacterRange::Singleton('-'), zone());
5521         AddRangeOrEscape(ranges, char_class_2, next, zone());
5522         continue;
5523       }
5524       if (first.from() > next.to()) {
5525         return ReportError(CStrVector(kRangeOutOfOrder) CHECK_FAILED);
5526       }
5527       ranges->Add(CharacterRange::Range(first.from(), next.to()), zone());
5528     } else {
5529       AddRangeOrEscape(ranges, char_class, first, zone());
5530     }
5531   }
5532   if (!has_more()) {
5533     return ReportError(CStrVector(kUnterminated) CHECK_FAILED);
5534   }
5535   Advance();
5536   if (ranges->length() == 0) {
5537     ranges->Add(CharacterRange::Everything(), zone());
5538     is_negated = !is_negated;
5539   }
5540   return new(zone()) RegExpCharacterClass(ranges, is_negated);
5541 }
5542
5543
5544 // ----------------------------------------------------------------------------
5545 // The Parser interface.
5546
5547 ScriptDataImpl::~ScriptDataImpl() {
5548   if (owns_store_) store_.Dispose();
5549 }
5550
5551
5552 int ScriptDataImpl::Length() {
5553   return store_.length() * sizeof(unsigned);
5554 }
5555
5556
5557 const char* ScriptDataImpl::Data() {
5558   return reinterpret_cast<const char*>(store_.start());
5559 }
5560
5561
5562 bool ScriptDataImpl::HasError() {
5563   return has_error();
5564 }
5565
5566
5567 void ScriptDataImpl::Initialize() {
5568   // Prepares state for use.
5569   if (store_.length() >= PreparseDataConstants::kHeaderSize) {
5570     function_index_ = PreparseDataConstants::kHeaderSize;
5571     int symbol_data_offset = PreparseDataConstants::kHeaderSize
5572         + store_[PreparseDataConstants::kFunctionsSizeOffset];
5573     if (store_.length() > symbol_data_offset) {
5574       symbol_data_ = reinterpret_cast<byte*>(&store_[symbol_data_offset]);
5575     } else {
5576       // Partial preparse causes no symbol information.
5577       symbol_data_ = reinterpret_cast<byte*>(&store_[0] + store_.length());
5578     }
5579     symbol_data_end_ = reinterpret_cast<byte*>(&store_[0] + store_.length());
5580   }
5581 }
5582
5583
5584 int ScriptDataImpl::ReadNumber(byte** source) {
5585   // Reads a number from symbol_data_ in base 128. The most significant
5586   // bit marks that there are more digits.
5587   // If the first byte is 0x80 (kNumberTerminator), it would normally
5588   // represent a leading zero. Since that is useless, and therefore won't
5589   // appear as the first digit of any actual value, it is used to
5590   // mark the end of the input stream.
5591   byte* data = *source;
5592   if (data >= symbol_data_end_) return -1;
5593   byte input = *data;
5594   if (input == PreparseDataConstants::kNumberTerminator) {
5595     // End of stream marker.
5596     return -1;
5597   }
5598   int result = input & 0x7f;
5599   data++;
5600   while ((input & 0x80u) != 0) {
5601     if (data >= symbol_data_end_) return -1;
5602     input = *data;
5603     result = (result << 7) | (input & 0x7f);
5604     data++;
5605   }
5606   *source = data;
5607   return result;
5608 }
5609
5610
5611 // Create a Scanner for the preparser to use as input, and preparse the source.
5612 ScriptDataImpl* PreParserApi::PreParse(Isolate* isolate,
5613                                        Utf16CharacterStream* source) {
5614   CompleteParserRecorder recorder;
5615   HistogramTimerScope timer(isolate->counters()->pre_parse());
5616   Scanner scanner(isolate->unicode_cache());
5617   intptr_t stack_limit = isolate->stack_guard()->real_climit();
5618   PreParser preparser(&scanner, &recorder, stack_limit);
5619   preparser.set_allow_lazy(true);
5620   preparser.set_allow_generators(FLAG_harmony_generators);
5621   preparser.set_allow_for_of(FLAG_harmony_iteration);
5622   preparser.set_allow_harmony_scoping(FLAG_harmony_scoping);
5623   preparser.set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals);
5624   scanner.Initialize(source);
5625   PreParser::PreParseResult result = preparser.PreParseProgram();
5626   if (result == PreParser::kPreParseStackOverflow) {
5627     isolate->StackOverflow();
5628     return NULL;
5629   }
5630
5631   // Extract the accumulated data from the recorder as a single
5632   // contiguous vector that we are responsible for disposing.
5633   Vector<unsigned> store = recorder.ExtractData();
5634   return new ScriptDataImpl(store);
5635 }
5636
5637
5638 bool RegExpParser::ParseRegExp(FlatStringReader* input,
5639                                bool multiline,
5640                                RegExpCompileData* result,
5641                                Zone* zone) {
5642   ASSERT(result != NULL);
5643   RegExpParser parser(input, &result->error, multiline, zone);
5644   RegExpTree* tree = parser.ParsePattern();
5645   if (parser.failed()) {
5646     ASSERT(tree == NULL);
5647     ASSERT(!result->error.is_null());
5648   } else {
5649     ASSERT(tree != NULL);
5650     ASSERT(result->error.is_null());
5651     result->tree = tree;
5652     int capture_count = parser.captures_started();
5653     result->simple = tree->IsAtom() && parser.simple() && capture_count == 0;
5654     result->contains_anchor = parser.contains_anchor();
5655     result->capture_count = capture_count;
5656   }
5657   return !parser.failed();
5658 }
5659
5660
5661 bool Parser::Parse() {
5662   ASSERT(info()->function() == NULL);
5663   FunctionLiteral* result = NULL;
5664   if (info()->is_lazy()) {
5665     ASSERT(!info()->is_eval());
5666     if (info()->shared_info()->is_function()) {
5667       result = ParseLazy();
5668     } else {
5669       result = ParseProgram();
5670     }
5671   } else {
5672     ScriptDataImpl* pre_parse_data = info()->pre_parse_data();
5673     set_pre_parse_data(pre_parse_data);
5674     if (pre_parse_data != NULL && pre_parse_data->has_error()) {
5675       Scanner::Location loc = pre_parse_data->MessageLocation();
5676       const char* message = pre_parse_data->BuildMessage();
5677       Vector<const char*> args = pre_parse_data->BuildArgs();
5678       ReportMessageAt(loc, message, args);
5679       DeleteArray(message);
5680       for (int i = 0; i < args.length(); i++) {
5681         DeleteArray(args[i]);
5682       }
5683       DeleteArray(args.start());
5684       ASSERT(info()->isolate()->has_pending_exception());
5685     } else {
5686       result = ParseProgram();
5687     }
5688   }
5689   info()->SetFunction(result);
5690   return (result != NULL);
5691 }
5692
5693 } }  // namespace v8::internal