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