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