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