The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / v8 / src / parser.cc @ f230a1cf

History | View | Annotate | Download (195 KB)

1
// Copyright 2012 the V8 project authors. All rights reserved.
2
// Redistribution and use in source and binary forms, with or without
3
// modification, are permitted provided that the following conditions are
4
// met:
5
//
6
//     * Redistributions of source code must retain the above copyright
7
//       notice, this list of conditions and the following disclaimer.
8
//     * Redistributions in binary form must reproduce the above
9
//       copyright notice, this list of conditions and the following
10
//       disclaimer in the documentation and/or other materials provided
11
//       with the distribution.
12
//     * Neither the name of Google Inc. nor the names of its
13
//       contributors may be used to endorse or promote products derived
14
//       from this software without specific prior written permission.
15
//
16
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27

    
28
#include "v8.h"
29

    
30
#include "api.h"
31
#include "ast.h"
32
#include "bootstrapper.h"
33
#include "char-predicates-inl.h"
34
#include "codegen.h"
35
#include "compiler.h"
36
#include "func-name-inferrer.h"
37
#include "messages.h"
38
#include "parser.h"
39
#include "platform.h"
40
#include "preparser.h"
41
#include "runtime.h"
42
#include "scanner-character-streams.h"
43
#include "scopeinfo.h"
44
#include "string-stream.h"
45

    
46
namespace v8 {
47
namespace internal {
48

    
49
// PositionStack is used for on-stack allocation of token positions for
50
// new expressions. Please look at ParseNewExpression.
51

    
52
class PositionStack  {
53
 public:
54
  explicit PositionStack(bool* ok) : top_(NULL), ok_(ok) {}
55
  ~PositionStack() {
56
    ASSERT(!*ok_ || is_empty());
57
    USE(ok_);
58
  }
59

    
60
  class Element  {
61
   public:
62
    Element(PositionStack* stack, int value) {
63
      previous_ = stack->top();
64
      value_ = value;
65
      stack->set_top(this);
66
    }
67

    
68
   private:
69
    Element* previous() { return previous_; }
70
    int value() { return value_; }
71
    friend class PositionStack;
72
    Element* previous_;
73
    int value_;
74
  };
75

    
76
  bool is_empty() { return top_ == NULL; }
77
  int pop() {
78
    ASSERT(!is_empty());
79
    int result = top_->value();
80
    top_ = top_->previous();
81
    return result;
82
  }
83

    
84
 private:
85
  Element* top() { return top_; }
86
  void set_top(Element* value) { top_ = value; }
87
  Element* top_;
88
  bool* ok_;
89
};
90

    
91

    
92
RegExpBuilder::RegExpBuilder(Zone* zone)
93
    : zone_(zone),
94
      pending_empty_(false),
95
      characters_(NULL),
96
      terms_(),
97
      alternatives_()
98
#ifdef DEBUG
99
    , last_added_(ADD_NONE)
100
#endif
101
  {}
102

    
103

    
104
void RegExpBuilder::FlushCharacters() {
105
  pending_empty_ = false;
106
  if (characters_ != NULL) {
107
    RegExpTree* atom = new(zone()) RegExpAtom(characters_->ToConstVector());
108
    characters_ = NULL;
109
    text_.Add(atom, zone());
110
    LAST(ADD_ATOM);
111
  }
112
}
113

    
114

    
115
void RegExpBuilder::FlushText() {
116
  FlushCharacters();
117
  int num_text = text_.length();
118
  if (num_text == 0) {
119
    return;
120
  } else if (num_text == 1) {
121
    terms_.Add(text_.last(), zone());
122
  } else {
123
    RegExpText* text = new(zone()) RegExpText(zone());
124
    for (int i = 0; i < num_text; i++)
125
      text_.Get(i)->AppendToText(text, zone());
126
    terms_.Add(text, zone());
127
  }
128
  text_.Clear();
129
}
130

    
131

    
132
void RegExpBuilder::AddCharacter(uc16 c) {
133
  pending_empty_ = false;
134
  if (characters_ == NULL) {
135
    characters_ = new(zone()) ZoneList<uc16>(4, zone());
136
  }
137
  characters_->Add(c, zone());
138
  LAST(ADD_CHAR);
139
}
140

    
141

    
142
void RegExpBuilder::AddEmpty() {
143
  pending_empty_ = true;
144
}
145

    
146

    
147
void RegExpBuilder::AddAtom(RegExpTree* term) {
148
  if (term->IsEmpty()) {
149
    AddEmpty();
150
    return;
151
  }
152
  if (term->IsTextElement()) {
153
    FlushCharacters();
154
    text_.Add(term, zone());
155
  } else {
156
    FlushText();
157
    terms_.Add(term, zone());
158
  }
159
  LAST(ADD_ATOM);
160
}
161

    
162

    
163
void RegExpBuilder::AddAssertion(RegExpTree* assert) {
164
  FlushText();
165
  terms_.Add(assert, zone());
166
  LAST(ADD_ASSERT);
167
}
168

    
169

    
170
void RegExpBuilder::NewAlternative() {
171
  FlushTerms();
172
}
173

    
174

    
175
void RegExpBuilder::FlushTerms() {
176
  FlushText();
177
  int num_terms = terms_.length();
178
  RegExpTree* alternative;
179
  if (num_terms == 0) {
180
    alternative = RegExpEmpty::GetInstance();
181
  } else if (num_terms == 1) {
182
    alternative = terms_.last();
183
  } else {
184
    alternative = new(zone()) RegExpAlternative(terms_.GetList(zone()));
185
  }
186
  alternatives_.Add(alternative, zone());
187
  terms_.Clear();
188
  LAST(ADD_NONE);
189
}
190

    
191

    
192
RegExpTree* RegExpBuilder::ToRegExp() {
193
  FlushTerms();
194
  int num_alternatives = alternatives_.length();
195
  if (num_alternatives == 0) {
196
    return RegExpEmpty::GetInstance();
197
  }
198
  if (num_alternatives == 1) {
199
    return alternatives_.last();
200
  }
201
  return new(zone()) RegExpDisjunction(alternatives_.GetList(zone()));
202
}
203

    
204

    
205
void RegExpBuilder::AddQuantifierToAtom(
206
    int min, int max, RegExpQuantifier::QuantifierType quantifier_type) {
207
  if (pending_empty_) {
208
    pending_empty_ = false;
209
    return;
210
  }
211
  RegExpTree* atom;
212
  if (characters_ != NULL) {
213
    ASSERT(last_added_ == ADD_CHAR);
214
    // Last atom was character.
215
    Vector<const uc16> char_vector = characters_->ToConstVector();
216
    int num_chars = char_vector.length();
217
    if (num_chars > 1) {
218
      Vector<const uc16> prefix = char_vector.SubVector(0, num_chars - 1);
219
      text_.Add(new(zone()) RegExpAtom(prefix), zone());
220
      char_vector = char_vector.SubVector(num_chars - 1, num_chars);
221
    }
222
    characters_ = NULL;
223
    atom = new(zone()) RegExpAtom(char_vector);
224
    FlushText();
225
  } else if (text_.length() > 0) {
226
    ASSERT(last_added_ == ADD_ATOM);
227
    atom = text_.RemoveLast();
228
    FlushText();
229
  } else if (terms_.length() > 0) {
230
    ASSERT(last_added_ == ADD_ATOM);
231
    atom = terms_.RemoveLast();
232
    if (atom->max_match() == 0) {
233
      // Guaranteed to only match an empty string.
234
      LAST(ADD_TERM);
235
      if (min == 0) {
236
        return;
237
      }
238
      terms_.Add(atom, zone());
239
      return;
240
    }
241
  } else {
242
    // Only call immediately after adding an atom or character!
243
    UNREACHABLE();
244
    return;
245
  }
246
  terms_.Add(
247
      new(zone()) RegExpQuantifier(min, max, quantifier_type, atom), zone());
248
  LAST(ADD_TERM);
249
}
250

    
251

    
252
Handle<String> Parser::LookupSymbol(int symbol_id) {
253
  // Length of symbol cache is the number of identified symbols.
254
  // If we are larger than that, or negative, it's not a cached symbol.
255
  // This might also happen if there is no preparser symbol data, even
256
  // if there is some preparser data.
257
  if (static_cast<unsigned>(symbol_id)
258
      >= static_cast<unsigned>(symbol_cache_.length())) {
259
    if (scanner().is_literal_ascii()) {
260
      return isolate()->factory()->InternalizeOneByteString(
261
          Vector<const uint8_t>::cast(scanner().literal_ascii_string()));
262
    } else {
263
      return isolate()->factory()->InternalizeTwoByteString(
264
          scanner().literal_utf16_string());
265
    }
266
  }
267
  return LookupCachedSymbol(symbol_id);
268
}
269

    
270

    
271
Handle<String> Parser::LookupCachedSymbol(int symbol_id) {
272
  // Make sure the cache is large enough to hold the symbol identifier.
273
  if (symbol_cache_.length() <= symbol_id) {
274
    // Increase length to index + 1.
275
    symbol_cache_.AddBlock(Handle<String>::null(),
276
                           symbol_id + 1 - symbol_cache_.length(), zone());
277
  }
278
  Handle<String> result = symbol_cache_.at(symbol_id);
279
  if (result.is_null()) {
280
    if (scanner().is_literal_ascii()) {
281
      result = isolate()->factory()->InternalizeOneByteString(
282
          Vector<const uint8_t>::cast(scanner().literal_ascii_string()));
283
    } else {
284
      result = isolate()->factory()->InternalizeTwoByteString(
285
          scanner().literal_utf16_string());
286
    }
287
    symbol_cache_.at(symbol_id) = result;
288
    return result;
289
  }
290
  isolate()->counters()->total_preparse_symbols_skipped()->Increment();
291
  return result;
292
}
293

    
294

    
295
FunctionEntry ScriptDataImpl::GetFunctionEntry(int start) {
296
  // The current pre-data entry must be a FunctionEntry with the given
297
  // start position.
298
  if ((function_index_ + FunctionEntry::kSize <= store_.length())
299
      && (static_cast<int>(store_[function_index_]) == start)) {
300
    int index = function_index_;
301
    function_index_ += FunctionEntry::kSize;
302
    return FunctionEntry(store_.SubVector(index,
303
                                          index + FunctionEntry::kSize));
304
  }
305
  return FunctionEntry();
306
}
307

    
308

    
309
int ScriptDataImpl::GetSymbolIdentifier() {
310
  return ReadNumber(&symbol_data_);
311
}
312

    
313

    
314
bool ScriptDataImpl::SanityCheck() {
315
  // Check that the header data is valid and doesn't specify
316
  // point to positions outside the store.
317
  if (store_.length() < PreparseDataConstants::kHeaderSize) return false;
318
  if (magic() != PreparseDataConstants::kMagicNumber) return false;
319
  if (version() != PreparseDataConstants::kCurrentVersion) return false;
320
  if (has_error()) {
321
    // Extra sane sanity check for error message encoding.
322
    if (store_.length() <= PreparseDataConstants::kHeaderSize
323
                         + PreparseDataConstants::kMessageTextPos) {
324
      return false;
325
    }
326
    if (Read(PreparseDataConstants::kMessageStartPos) >
327
        Read(PreparseDataConstants::kMessageEndPos)) {
328
      return false;
329
    }
330
    unsigned arg_count = Read(PreparseDataConstants::kMessageArgCountPos);
331
    int pos = PreparseDataConstants::kMessageTextPos;
332
    for (unsigned int i = 0; i <= arg_count; i++) {
333
      if (store_.length() <= PreparseDataConstants::kHeaderSize + pos) {
334
        return false;
335
      }
336
      int length = static_cast<int>(Read(pos));
337
      if (length < 0) return false;
338
      pos += 1 + length;
339
    }
340
    if (store_.length() < PreparseDataConstants::kHeaderSize + pos) {
341
      return false;
342
    }
343
    return true;
344
  }
345
  // Check that the space allocated for function entries is sane.
346
  int functions_size =
347
      static_cast<int>(store_[PreparseDataConstants::kFunctionsSizeOffset]);
348
  if (functions_size < 0) return false;
349
  if (functions_size % FunctionEntry::kSize != 0) return false;
350
  // Check that the count of symbols is non-negative.
351
  int symbol_count =
352
      static_cast<int>(store_[PreparseDataConstants::kSymbolCountOffset]);
353
  if (symbol_count < 0) return false;
354
  // Check that the total size has room for header and function entries.
355
  int minimum_size =
356
      PreparseDataConstants::kHeaderSize + functions_size;
357
  if (store_.length() < minimum_size) return false;
358
  return true;
359
}
360

    
361

    
362

    
363
const char* ScriptDataImpl::ReadString(unsigned* start, int* chars) {
364
  int length = start[0];
365
  char* result = NewArray<char>(length + 1);
366
  for (int i = 0; i < length; i++) {
367
    result[i] = start[i + 1];
368
  }
369
  result[length] = '\0';
370
  if (chars != NULL) *chars = length;
371
  return result;
372
}
373

    
374

    
375
Scanner::Location ScriptDataImpl::MessageLocation() {
376
  int beg_pos = Read(PreparseDataConstants::kMessageStartPos);
377
  int end_pos = Read(PreparseDataConstants::kMessageEndPos);
378
  return Scanner::Location(beg_pos, end_pos);
379
}
380

    
381

    
382
const char* ScriptDataImpl::BuildMessage() {
383
  unsigned* start = ReadAddress(PreparseDataConstants::kMessageTextPos);
384
  return ReadString(start, NULL);
385
}
386

    
387

    
388
Vector<const char*> ScriptDataImpl::BuildArgs() {
389
  int arg_count = Read(PreparseDataConstants::kMessageArgCountPos);
390
  const char** array = NewArray<const char*>(arg_count);
391
  // Position after text found by skipping past length field and
392
  // length field content words.
393
  int pos = PreparseDataConstants::kMessageTextPos + 1
394
      + Read(PreparseDataConstants::kMessageTextPos);
395
  for (int i = 0; i < arg_count; i++) {
396
    int count = 0;
397
    array[i] = ReadString(ReadAddress(pos), &count);
398
    pos += count + 1;
399
  }
400
  return Vector<const char*>(array, arg_count);
401
}
402

    
403

    
404
unsigned ScriptDataImpl::Read(int position) {
405
  return store_[PreparseDataConstants::kHeaderSize + position];
406
}
407

    
408

    
409
unsigned* ScriptDataImpl::ReadAddress(int position) {
410
  return &store_[PreparseDataConstants::kHeaderSize + position];
411
}
412

    
413

    
414
Scope* Parser::NewScope(Scope* parent, ScopeType scope_type) {
415
  Scope* result = new(zone()) Scope(parent, scope_type, zone());
416
  result->Initialize();
417
  return result;
418
}
419

    
420

    
421
// ----------------------------------------------------------------------------
422
// Target is a support class to facilitate manipulation of the
423
// Parser's target_stack_ (the stack of potential 'break' and
424
// 'continue' statement targets). Upon construction, a new target is
425
// added; it is removed upon destruction.
426

    
427
class Target BASE_EMBEDDED {
428
 public:
429
  Target(Target** variable, AstNode* node)
430
      : variable_(variable), node_(node), previous_(*variable) {
431
    *variable = this;
432
  }
433

    
434
  ~Target() {
435
    *variable_ = previous_;
436
  }
437

    
438
  Target* previous() { return previous_; }
439
  AstNode* node() { return node_; }
440

    
441
 private:
442
  Target** variable_;
443
  AstNode* node_;
444
  Target* previous_;
445
};
446

    
447

    
448
class TargetScope BASE_EMBEDDED {
449
 public:
450
  explicit TargetScope(Target** variable)
451
      : variable_(variable), previous_(*variable) {
452
    *variable = NULL;
453
  }
454

    
455
  ~TargetScope() {
456
    *variable_ = previous_;
457
  }
458

    
459
 private:
460
  Target** variable_;
461
  Target* previous_;
462
};
463

    
464

    
465
// ----------------------------------------------------------------------------
466
// FunctionState and BlockState together implement the parser's scope stack.
467
// The parser's current scope is in top_scope_.  The BlockState and
468
// FunctionState constructors push on the scope stack and the destructors
469
// pop.  They are also used to hold the parser's per-function and per-block
470
// state.
471

    
472
class Parser::BlockState BASE_EMBEDDED {
473
 public:
474
  BlockState(Parser* parser, Scope* scope)
475
      : parser_(parser),
476
        outer_scope_(parser->top_scope_) {
477
    parser->top_scope_ = scope;
478
  }
479

    
480
  ~BlockState() { parser_->top_scope_ = outer_scope_; }
481

    
482
 private:
483
  Parser* parser_;
484
  Scope* outer_scope_;
485
};
486

    
487

    
488
Parser::FunctionState::FunctionState(Parser* parser,
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

    
506
Parser::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

    
538
Parser::Parser(CompilationInfo* info)
539
    : ParserBase(&scanner_, info->isolate()->stack_guard()->real_climit()),
540
      isolate_(info->isolate()),
541
      symbol_cache_(0, info->zone()),
542
      script_(info->script()),
543
      scanner_(isolate_->unicode_cache()),
544
      reusable_preparser_(NULL),
545
      top_scope_(NULL),
546
      original_scope_(NULL),
547
      current_function_state_(NULL),
548
      target_stack_(NULL),
549
      extension_(info->extension()),
550
      pre_parse_data_(NULL),
551
      fni_(NULL),
552
      parenthesized_function_(false),
553
      zone_(info->zone()),
554
      info_(info) {
555
  ASSERT(!script_.is_null());
556
  isolate_->set_ast_node_id(0);
557
  set_allow_harmony_scoping(!info->is_native() && FLAG_harmony_scoping);
558
  set_allow_modules(!info->is_native() && FLAG_harmony_modules);
559
  set_allow_natives_syntax(FLAG_allow_natives_syntax || info->is_native());
560
  set_allow_lazy(false);  // Must be explicitly enabled.
561
  set_allow_generators(FLAG_harmony_generators);
562
  set_allow_for_of(FLAG_harmony_iteration);
563
  set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals);
564
}
565

    
566

    
567
FunctionLiteral* Parser::ParseProgram() {
568
  // TODO(bmeurer): We temporarily need to pass allow_nesting = true here,
569
  // see comment for HistogramTimerScope class.
570
  HistogramTimerScope timer_scope(isolate()->counters()->parse(), true);
571
  Handle<String> source(String::cast(script_->source()));
572
  isolate()->counters()->total_parse_size()->Increment(source->length());
573
  ElapsedTimer timer;
574
  if (FLAG_trace_parse) {
575
    timer.Start();
576
  }
577
  fni_ = new(zone()) FuncNameInferrer(isolate(), zone());
578

    
579
  // Initialize parser state.
580
  source->TryFlatten();
581
  FunctionLiteral* result;
582
  if (source->IsExternalTwoByteString()) {
583
    // Notice that the stream is destroyed at the end of the branch block.
584
    // The last line of the blocks can't be moved outside, even though they're
585
    // identical calls.
586
    ExternalTwoByteStringUtf16CharacterStream stream(
587
        Handle<ExternalTwoByteString>::cast(source), 0, source->length());
588
    scanner_.Initialize(&stream);
589
    result = DoParseProgram(info(), source);
590
  } else {
591
    GenericStringUtf16CharacterStream stream(source, 0, source->length());
592
    scanner_.Initialize(&stream);
593
    result = DoParseProgram(info(), source);
594
  }
595

    
596
  if (FLAG_trace_parse && result != NULL) {
597
    double ms = timer.Elapsed().InMillisecondsF();
598
    if (info()->is_eval()) {
599
      PrintF("[parsing eval");
600
    } else if (info()->script()->name()->IsString()) {
601
      String* name = String::cast(info()->script()->name());
602
      SmartArrayPointer<char> name_chars = name->ToCString();
603
      PrintF("[parsing script: %s", *name_chars);
604
    } else {
605
      PrintF("[parsing script");
606
    }
607
    PrintF(" - took %0.3f ms]\n", ms);
608
  }
609
  return result;
610
}
611

    
612

    
613
FunctionLiteral* Parser::DoParseProgram(CompilationInfo* info,
614
                                        Handle<String> source) {
615
  ASSERT(top_scope_ == NULL);
616
  ASSERT(target_stack_ == NULL);
617
  if (pre_parse_data_ != NULL) pre_parse_data_->Initialize();
618

    
619
  Handle<String> no_name = isolate()->factory()->empty_string();
620

    
621
  FunctionLiteral* result = NULL;
622
  { Scope* scope = NewScope(top_scope_, GLOBAL_SCOPE);
623
    info->SetGlobalScope(scope);
624
    if (!info->context().is_null()) {
625
      scope = Scope::DeserializeScopeChain(*info->context(), scope, zone());
626
    }
627
    original_scope_ = scope;
628
    if (info->is_eval()) {
629
      if (!scope->is_global_scope() || info->language_mode() != CLASSIC_MODE) {
630
        scope = NewScope(scope, EVAL_SCOPE);
631
      }
632
    } else if (info->is_global()) {
633
      scope = NewScope(scope, GLOBAL_SCOPE);
634
    }
635
    scope->set_start_position(0);
636
    scope->set_end_position(source->length());
637

    
638
    // Compute the parsing mode.
639
    Mode mode = (FLAG_lazy && allow_lazy()) ? PARSE_LAZILY : PARSE_EAGERLY;
640
    if (allow_natives_syntax() ||
641
        extension_ != NULL ||
642
        scope->is_eval_scope()) {
643
      mode = PARSE_EAGERLY;
644
    }
645
    ParsingModeScope parsing_mode(this, mode);
646

    
647
    // Enters 'scope'.
648
    FunctionState function_state(this, scope, isolate());
649

    
650
    top_scope_->SetLanguageMode(info->language_mode());
651
    ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(16, zone());
652
    bool ok = true;
653
    int beg_pos = scanner().location().beg_pos;
654
    ParseSourceElements(body, Token::EOS, info->is_eval(), true, &ok);
655
    if (ok && !top_scope_->is_classic_mode()) {
656
      CheckOctalLiteral(beg_pos, scanner().location().end_pos, &ok);
657
    }
658

    
659
    if (ok && is_extended_mode()) {
660
      CheckConflictingVarDeclarations(top_scope_, &ok);
661
    }
662

    
663
    if (ok && info->parse_restriction() == ONLY_SINGLE_FUNCTION_LITERAL) {
664
      if (body->length() != 1 ||
665
          !body->at(0)->IsExpressionStatement() ||
666
          !body->at(0)->AsExpressionStatement()->
667
              expression()->IsFunctionLiteral()) {
668
        ReportMessage("single_function_literal", Vector<const char*>::empty());
669
        ok = false;
670
      }
671
    }
672

    
673
    if (ok) {
674
      result = factory()->NewFunctionLiteral(
675
          no_name,
676
          top_scope_,
677
          body,
678
          function_state.materialized_literal_count(),
679
          function_state.expected_property_count(),
680
          function_state.handler_count(),
681
          0,
682
          FunctionLiteral::kNoDuplicateParameters,
683
          FunctionLiteral::ANONYMOUS_EXPRESSION,
684
          FunctionLiteral::kGlobalOrEval,
685
          FunctionLiteral::kNotParenthesized,
686
          FunctionLiteral::kNotGenerator,
687
          0);
688
      result->set_ast_properties(factory()->visitor()->ast_properties());
689
      result->set_dont_optimize_reason(
690
          factory()->visitor()->dont_optimize_reason());
691
    } else if (stack_overflow()) {
692
      isolate()->StackOverflow();
693
    }
694
  }
695

    
696
  // Make sure the target stack is empty.
697
  ASSERT(target_stack_ == NULL);
698

    
699
  return result;
700
}
701

    
702

    
703
FunctionLiteral* Parser::ParseLazy() {
704
  HistogramTimerScope timer_scope(isolate()->counters()->parse_lazy());
705
  Handle<String> source(String::cast(script_->source()));
706
  isolate()->counters()->total_parse_size()->Increment(source->length());
707
  ElapsedTimer timer;
708
  if (FLAG_trace_parse) {
709
    timer.Start();
710
  }
711
  Handle<SharedFunctionInfo> shared_info = info()->shared_info();
712

    
713
  // Initialize parser state.
714
  source->TryFlatten();
715
  FunctionLiteral* result;
716
  if (source->IsExternalTwoByteString()) {
717
    ExternalTwoByteStringUtf16CharacterStream stream(
718
        Handle<ExternalTwoByteString>::cast(source),
719
        shared_info->start_position(),
720
        shared_info->end_position());
721
    result = ParseLazy(&stream);
722
  } else {
723
    GenericStringUtf16CharacterStream stream(source,
724
                                             shared_info->start_position(),
725
                                             shared_info->end_position());
726
    result = ParseLazy(&stream);
727
  }
728

    
729
  if (FLAG_trace_parse && result != NULL) {
730
    double ms = timer.Elapsed().InMillisecondsF();
731
    SmartArrayPointer<char> name_chars = result->debug_name()->ToCString();
732
    PrintF("[parsing function: %s - took %0.3f ms]\n", *name_chars, ms);
733
  }
734
  return result;
735
}
736

    
737

    
738
FunctionLiteral* Parser::ParseLazy(Utf16CharacterStream* source) {
739
  Handle<SharedFunctionInfo> shared_info = info()->shared_info();
740
  scanner_.Initialize(source);
741
  ASSERT(top_scope_ == NULL);
742
  ASSERT(target_stack_ == NULL);
743

    
744
  Handle<String> name(String::cast(shared_info->name()));
745
  fni_ = new(zone()) FuncNameInferrer(isolate(), zone());
746
  fni_->PushEnclosingName(name);
747

    
748
  ParsingModeScope parsing_mode(this, PARSE_EAGERLY);
749

    
750
  // Place holder for the result.
751
  FunctionLiteral* result = NULL;
752

    
753
  {
754
    // Parse the function literal.
755
    Scope* scope = NewScope(top_scope_, GLOBAL_SCOPE);
756
    info()->SetGlobalScope(scope);
757
    if (!info()->closure().is_null()) {
758
      scope = Scope::DeserializeScopeChain(info()->closure()->context(), scope,
759
                                           zone());
760
    }
761
    original_scope_ = scope;
762
    FunctionState function_state(this, scope, isolate());
763
    ASSERT(scope->language_mode() != STRICT_MODE || !info()->is_classic_mode());
764
    ASSERT(scope->language_mode() != EXTENDED_MODE ||
765
           info()->is_extended_mode());
766
    ASSERT(info()->language_mode() == shared_info->language_mode());
767
    scope->SetLanguageMode(shared_info->language_mode());
768
    FunctionLiteral::FunctionType function_type = shared_info->is_expression()
769
        ? (shared_info->is_anonymous()
770
              ? FunctionLiteral::ANONYMOUS_EXPRESSION
771
              : FunctionLiteral::NAMED_EXPRESSION)
772
        : FunctionLiteral::DECLARATION;
773
    bool ok = true;
774
    result = ParseFunctionLiteral(name,
775
                                  false,  // Strict mode name already checked.
776
                                  shared_info->is_generator(),
777
                                  RelocInfo::kNoPosition,
778
                                  function_type,
779
                                  &ok);
780
    // Make sure the results agree.
781
    ASSERT(ok == (result != NULL));
782
  }
783

    
784
  // Make sure the target stack is empty.
785
  ASSERT(target_stack_ == NULL);
786

    
787
  if (result == NULL) {
788
    if (stack_overflow()) isolate()->StackOverflow();
789
  } else {
790
    Handle<String> inferred_name(shared_info->inferred_name());
791
    result->set_inferred_name(inferred_name);
792
  }
793
  return result;
794
}
795

    
796

    
797
Handle<String> Parser::GetSymbol() {
798
  int symbol_id = -1;
799
  if (pre_parse_data() != NULL) {
800
    symbol_id = pre_parse_data()->GetSymbolIdentifier();
801
  }
802
  return LookupSymbol(symbol_id);
803
}
804

    
805

    
806
void Parser::ReportMessage(const char* message, Vector<const char*> args) {
807
  Scanner::Location source_location = scanner().location();
808
  ReportMessageAt(source_location, message, args);
809
}
810

    
811

    
812
void Parser::ReportMessage(const char* message, Vector<Handle<String> > args) {
813
  Scanner::Location source_location = scanner().location();
814
  ReportMessageAt(source_location, message, args);
815
}
816

    
817

    
818
void Parser::ReportMessageAt(Scanner::Location source_location,
819
                             const char* message,
820
                             Vector<const char*> args) {
821
  MessageLocation location(script_,
822
                           source_location.beg_pos,
823
                           source_location.end_pos);
824
  Factory* factory = isolate()->factory();
825
  Handle<FixedArray> elements = factory->NewFixedArray(args.length());
826
  for (int i = 0; i < args.length(); i++) {
827
    Handle<String> arg_string = factory->NewStringFromUtf8(CStrVector(args[i]));
828
    elements->set(i, *arg_string);
829
  }
830
  Handle<JSArray> array = factory->NewJSArrayWithElements(elements);
831
  Handle<Object> result = factory->NewSyntaxError(message, array);
832
  isolate()->Throw(*result, &location);
833
}
834

    
835

    
836
void Parser::ReportMessageAt(Scanner::Location source_location,
837
                             const char* message,
838
                             Vector<Handle<String> > args) {
839
  MessageLocation location(script_,
840
                           source_location.beg_pos,
841
                           source_location.end_pos);
842
  Factory* factory = isolate()->factory();
843
  Handle<FixedArray> elements = factory->NewFixedArray(args.length());
844
  for (int i = 0; i < args.length(); i++) {
845
    elements->set(i, *args[i]);
846
  }
847
  Handle<JSArray> array = factory->NewJSArrayWithElements(elements);
848
  Handle<Object> result = factory->NewSyntaxError(message, array);
849
  isolate()->Throw(*result, &location);
850
}
851

    
852

    
853
void* Parser::ParseSourceElements(ZoneList<Statement*>* processor,
854
                                  int end_token,
855
                                  bool is_eval,
856
                                  bool is_global,
857
                                  bool* ok) {
858
  // SourceElements ::
859
  //   (ModuleElement)* <end_token>
860

    
861
  // Allocate a target stack to use for this set of source
862
  // elements. This way, all scripts and functions get their own
863
  // target stack thus avoiding illegal breaks and continues across
864
  // functions.
865
  TargetScope scope(&this->target_stack_);
866

    
867
  ASSERT(processor != NULL);
868
  bool directive_prologue = true;     // Parsing directive prologue.
869

    
870
  while (peek() != end_token) {
871
    if (directive_prologue && peek() != Token::STRING) {
872
      directive_prologue = false;
873
    }
874

    
875
    Scanner::Location token_loc = scanner().peek_location();
876
    Statement* stat;
877
    if (is_global && !is_eval) {
878
      stat = ParseModuleElement(NULL, CHECK_OK);
879
    } else {
880
      stat = ParseBlockElement(NULL, CHECK_OK);
881
    }
882
    if (stat == NULL || stat->IsEmpty()) {
883
      directive_prologue = false;   // End of directive prologue.
884
      continue;
885
    }
886

    
887
    if (directive_prologue) {
888
      // A shot at a directive.
889
      ExpressionStatement* e_stat;
890
      Literal* literal;
891
      // Still processing directive prologue?
892
      if ((e_stat = stat->AsExpressionStatement()) != NULL &&
893
          (literal = e_stat->expression()->AsLiteral()) != NULL &&
894
          literal->value()->IsString()) {
895
        Handle<String> directive = Handle<String>::cast(literal->value());
896

    
897
        // Check "use strict" directive (ES5 14.1).
898
        if (top_scope_->is_classic_mode() &&
899
            directive->Equals(isolate()->heap()->use_strict_string()) &&
900
            token_loc.end_pos - token_loc.beg_pos ==
901
              isolate()->heap()->use_strict_string()->length() + 2) {
902
          // TODO(mstarzinger): Global strict eval calls, need their own scope
903
          // as specified in ES5 10.4.2(3). The correct fix would be to always
904
          // add this scope in DoParseProgram(), but that requires adaptations
905
          // all over the code base, so we go with a quick-fix for now.
906
          // In the same manner, we have to patch the parsing mode.
907
          if (is_eval && !top_scope_->is_eval_scope()) {
908
            ASSERT(top_scope_->is_global_scope());
909
            Scope* scope = NewScope(top_scope_, EVAL_SCOPE);
910
            scope->set_start_position(top_scope_->start_position());
911
            scope->set_end_position(top_scope_->end_position());
912
            top_scope_ = scope;
913
            mode_ = PARSE_EAGERLY;
914
          }
915
          // TODO(ES6): Fix entering extended mode, once it is specified.
916
          top_scope_->SetLanguageMode(allow_harmony_scoping()
917
                                      ? EXTENDED_MODE : STRICT_MODE);
918
          // "use strict" is the only directive for now.
919
          directive_prologue = false;
920
        }
921
      } else {
922
        // End of the directive prologue.
923
        directive_prologue = false;
924
      }
925
    }
926

    
927
    processor->Add(stat, zone());
928
  }
929

    
930
  return 0;
931
}
932

    
933

    
934
Statement* Parser::ParseModuleElement(ZoneStringList* labels,
935
                                      bool* ok) {
936
  // (Ecma 262 5th Edition, clause 14):
937
  // SourceElement:
938
  //    Statement
939
  //    FunctionDeclaration
940
  //
941
  // In harmony mode we allow additionally the following productions
942
  // ModuleElement:
943
  //    LetDeclaration
944
  //    ConstDeclaration
945
  //    ModuleDeclaration
946
  //    ImportDeclaration
947
  //    ExportDeclaration
948
  //    GeneratorDeclaration
949

    
950
  switch (peek()) {
951
    case Token::FUNCTION:
952
      return ParseFunctionDeclaration(NULL, ok);
953
    case Token::LET:
954
    case Token::CONST:
955
      return ParseVariableStatement(kModuleElement, NULL, ok);
956
    case Token::IMPORT:
957
      return ParseImportDeclaration(ok);
958
    case Token::EXPORT:
959
      return ParseExportDeclaration(ok);
960
    default: {
961
      Statement* stmt = ParseStatement(labels, CHECK_OK);
962
      // Handle 'module' as a context-sensitive keyword.
963
      if (FLAG_harmony_modules &&
964
          peek() == Token::IDENTIFIER &&
965
          !scanner().HasAnyLineTerminatorBeforeNext() &&
966
          stmt != NULL) {
967
        ExpressionStatement* estmt = stmt->AsExpressionStatement();
968
        if (estmt != NULL &&
969
            estmt->expression()->AsVariableProxy() != NULL &&
970
            estmt->expression()->AsVariableProxy()->name()->Equals(
971
                isolate()->heap()->module_string()) &&
972
            !scanner().literal_contains_escapes()) {
973
          return ParseModuleDeclaration(NULL, ok);
974
        }
975
      }
976
      return stmt;
977
    }
978
  }
979
}
980

    
981

    
982
Statement* Parser::ParseModuleDeclaration(ZoneStringList* names, bool* ok) {
983
  // ModuleDeclaration:
984
  //    'module' Identifier Module
985

    
986
  int pos = peek_position();
987
  Handle<String> name = ParseIdentifier(CHECK_OK);
988

    
989
#ifdef DEBUG
990
  if (FLAG_print_interface_details)
991
    PrintF("# Module %s...\n", name->ToAsciiArray());
992
#endif
993

    
994
  Module* module = ParseModule(CHECK_OK);
995
  VariableProxy* proxy = NewUnresolved(name, MODULE, module->interface());
996
  Declaration* declaration =
997
      factory()->NewModuleDeclaration(proxy, module, top_scope_, pos);
998
  Declare(declaration, true, CHECK_OK);
999

    
1000
#ifdef DEBUG
1001
  if (FLAG_print_interface_details)
1002
    PrintF("# Module %s.\n", name->ToAsciiArray());
1003

    
1004
  if (FLAG_print_interfaces) {
1005
    PrintF("module %s : ", name->ToAsciiArray());
1006
    module->interface()->Print();
1007
  }
1008
#endif
1009

    
1010
  if (names) names->Add(name, zone());
1011
  if (module->body() == NULL)
1012
    return factory()->NewEmptyStatement(pos);
1013
  else
1014
    return factory()->NewModuleStatement(proxy, module->body(), pos);
1015
}
1016

    
1017

    
1018
Module* Parser::ParseModule(bool* ok) {
1019
  // Module:
1020
  //    '{' ModuleElement '}'
1021
  //    '=' ModulePath ';'
1022
  //    'at' String ';'
1023

    
1024
  switch (peek()) {
1025
    case Token::LBRACE:
1026
      return ParseModuleLiteral(ok);
1027

    
1028
    case Token::ASSIGN: {
1029
      Expect(Token::ASSIGN, CHECK_OK);
1030
      Module* result = ParseModulePath(CHECK_OK);
1031
      ExpectSemicolon(CHECK_OK);
1032
      return result;
1033
    }
1034

    
1035
    default: {
1036
      ExpectContextualKeyword(CStrVector("at"), CHECK_OK);
1037
      Module* result = ParseModuleUrl(CHECK_OK);
1038
      ExpectSemicolon(CHECK_OK);
1039
      return result;
1040
    }
1041
  }
1042
}
1043

    
1044

    
1045
Module* Parser::ParseModuleLiteral(bool* ok) {
1046
  // Module:
1047
  //    '{' ModuleElement '}'
1048

    
1049
  int pos = peek_position();
1050
  // Construct block expecting 16 statements.
1051
  Block* body = factory()->NewBlock(NULL, 16, false, RelocInfo::kNoPosition);
1052
#ifdef DEBUG
1053
  if (FLAG_print_interface_details) PrintF("# Literal ");
1054
#endif
1055
  Scope* scope = NewScope(top_scope_, MODULE_SCOPE);
1056

    
1057
  Expect(Token::LBRACE, CHECK_OK);
1058
  scope->set_start_position(scanner().location().beg_pos);
1059
  scope->SetLanguageMode(EXTENDED_MODE);
1060

    
1061
  {
1062
    BlockState block_state(this, scope);
1063
    TargetCollector collector(zone());
1064
    Target target(&this->target_stack_, &collector);
1065
    Target target_body(&this->target_stack_, body);
1066

    
1067
    while (peek() != Token::RBRACE) {
1068
      Statement* stat = ParseModuleElement(NULL, CHECK_OK);
1069
      if (stat && !stat->IsEmpty()) {
1070
        body->AddStatement(stat, zone());
1071
      }
1072
    }
1073
  }
1074

    
1075
  Expect(Token::RBRACE, CHECK_OK);
1076
  scope->set_end_position(scanner().location().end_pos);
1077
  body->set_scope(scope);
1078

    
1079
  // Check that all exports are bound.
1080
  Interface* interface = scope->interface();
1081
  for (Interface::Iterator it = interface->iterator();
1082
       !it.done(); it.Advance()) {
1083
    if (scope->LocalLookup(it.name()) == NULL) {
1084
      Handle<String> name(it.name());
1085
      ReportMessage("module_export_undefined",
1086
                    Vector<Handle<String> >(&name, 1));
1087
      *ok = false;
1088
      return NULL;
1089
    }
1090
  }
1091

    
1092
  interface->MakeModule(ok);
1093
  ASSERT(*ok);
1094
  interface->Freeze(ok);
1095
  ASSERT(*ok);
1096
  return factory()->NewModuleLiteral(body, interface, pos);
1097
}
1098

    
1099

    
1100
Module* Parser::ParseModulePath(bool* ok) {
1101
  // ModulePath:
1102
  //    Identifier
1103
  //    ModulePath '.' Identifier
1104

    
1105
  int pos = peek_position();
1106
  Module* result = ParseModuleVariable(CHECK_OK);
1107
  while (Check(Token::PERIOD)) {
1108
    Handle<String> name = ParseIdentifierName(CHECK_OK);
1109
#ifdef DEBUG
1110
    if (FLAG_print_interface_details)
1111
      PrintF("# Path .%s ", name->ToAsciiArray());
1112
#endif
1113
    Module* member = factory()->NewModulePath(result, name, pos);
1114
    result->interface()->Add(name, member->interface(), zone(), ok);
1115
    if (!*ok) {
1116
#ifdef DEBUG
1117
      if (FLAG_print_interfaces) {
1118
        PrintF("PATH TYPE ERROR at '%s'\n", name->ToAsciiArray());
1119
        PrintF("result: ");
1120
        result->interface()->Print();
1121
        PrintF("member: ");
1122
        member->interface()->Print();
1123
      }
1124
#endif
1125
      ReportMessage("invalid_module_path", Vector<Handle<String> >(&name, 1));
1126
      return NULL;
1127
    }
1128
    result = member;
1129
  }
1130

    
1131
  return result;
1132
}
1133

    
1134

    
1135
Module* Parser::ParseModuleVariable(bool* ok) {
1136
  // ModulePath:
1137
  //    Identifier
1138

    
1139
  int pos = peek_position();
1140
  Handle<String> name = ParseIdentifier(CHECK_OK);
1141
#ifdef DEBUG
1142
  if (FLAG_print_interface_details)
1143
    PrintF("# Module variable %s ", name->ToAsciiArray());
1144
#endif
1145
  VariableProxy* proxy = top_scope_->NewUnresolved(
1146
      factory(), name, Interface::NewModule(zone()),
1147
      scanner().location().beg_pos);
1148

    
1149
  return factory()->NewModuleVariable(proxy, pos);
1150
}
1151

    
1152

    
1153
Module* Parser::ParseModuleUrl(bool* ok) {
1154
  // Module:
1155
  //    String
1156

    
1157
  int pos = peek_position();
1158
  Expect(Token::STRING, CHECK_OK);
1159
  Handle<String> symbol = GetSymbol();
1160

    
1161
  // TODO(ES6): Request JS resource from environment...
1162

    
1163
#ifdef DEBUG
1164
  if (FLAG_print_interface_details) PrintF("# Url ");
1165
#endif
1166

    
1167
  // Create an empty literal as long as the feature isn't finished.
1168
  USE(symbol);
1169
  Scope* scope = NewScope(top_scope_, MODULE_SCOPE);
1170
  Block* body = factory()->NewBlock(NULL, 1, false, RelocInfo::kNoPosition);
1171
  body->set_scope(scope);
1172
  Interface* interface = scope->interface();
1173
  Module* result = factory()->NewModuleLiteral(body, interface, pos);
1174
  interface->Freeze(ok);
1175
  ASSERT(*ok);
1176
  interface->Unify(scope->interface(), zone(), ok);
1177
  ASSERT(*ok);
1178
  return result;
1179
}
1180

    
1181

    
1182
Module* Parser::ParseModuleSpecifier(bool* ok) {
1183
  // ModuleSpecifier:
1184
  //    String
1185
  //    ModulePath
1186

    
1187
  if (peek() == Token::STRING) {
1188
    return ParseModuleUrl(ok);
1189
  } else {
1190
    return ParseModulePath(ok);
1191
  }
1192
}
1193

    
1194

    
1195
Block* Parser::ParseImportDeclaration(bool* ok) {
1196
  // ImportDeclaration:
1197
  //    'import' IdentifierName (',' IdentifierName)* 'from' ModuleSpecifier ';'
1198
  //
1199
  // TODO(ES6): implement destructuring ImportSpecifiers
1200

    
1201
  int pos = peek_position();
1202
  Expect(Token::IMPORT, CHECK_OK);
1203
  ZoneStringList names(1, zone());
1204

    
1205
  Handle<String> name = ParseIdentifierName(CHECK_OK);
1206
  names.Add(name, zone());
1207
  while (peek() == Token::COMMA) {
1208
    Consume(Token::COMMA);
1209
    name = ParseIdentifierName(CHECK_OK);
1210
    names.Add(name, zone());
1211
  }
1212

    
1213
  ExpectContextualKeyword(CStrVector("from"), CHECK_OK);
1214
  Module* module = ParseModuleSpecifier(CHECK_OK);
1215
  ExpectSemicolon(CHECK_OK);
1216

    
1217
  // Generate a separate declaration for each identifier.
1218
  // TODO(ES6): once we implement destructuring, make that one declaration.
1219
  Block* block = factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition);
1220
  for (int i = 0; i < names.length(); ++i) {
1221
#ifdef DEBUG
1222
    if (FLAG_print_interface_details)
1223
      PrintF("# Import %s ", names[i]->ToAsciiArray());
1224
#endif
1225
    Interface* interface = Interface::NewUnknown(zone());
1226
    module->interface()->Add(names[i], interface, zone(), ok);
1227
    if (!*ok) {
1228
#ifdef DEBUG
1229
      if (FLAG_print_interfaces) {
1230
        PrintF("IMPORT TYPE ERROR at '%s'\n", names[i]->ToAsciiArray());
1231
        PrintF("module: ");
1232
        module->interface()->Print();
1233
      }
1234
#endif
1235
      ReportMessage("invalid_module_path", Vector<Handle<String> >(&name, 1));
1236
      return NULL;
1237
    }
1238
    VariableProxy* proxy = NewUnresolved(names[i], LET, interface);
1239
    Declaration* declaration =
1240
        factory()->NewImportDeclaration(proxy, module, top_scope_, pos);
1241
    Declare(declaration, true, CHECK_OK);
1242
  }
1243

    
1244
  return block;
1245
}
1246

    
1247

    
1248
Statement* Parser::ParseExportDeclaration(bool* ok) {
1249
  // ExportDeclaration:
1250
  //    'export' Identifier (',' Identifier)* ';'
1251
  //    'export' VariableDeclaration
1252
  //    'export' FunctionDeclaration
1253
  //    'export' GeneratorDeclaration
1254
  //    'export' ModuleDeclaration
1255
  //
1256
  // TODO(ES6): implement structuring ExportSpecifiers
1257

    
1258
  Expect(Token::EXPORT, CHECK_OK);
1259

    
1260
  Statement* result = NULL;
1261
  ZoneStringList names(1, zone());
1262
  switch (peek()) {
1263
    case Token::IDENTIFIER: {
1264
      int pos = position();
1265
      Handle<String> name = ParseIdentifier(CHECK_OK);
1266
      // Handle 'module' as a context-sensitive keyword.
1267
      if (!name->IsOneByteEqualTo(STATIC_ASCII_VECTOR("module"))) {
1268
        names.Add(name, zone());
1269
        while (peek() == Token::COMMA) {
1270
          Consume(Token::COMMA);
1271
          name = ParseIdentifier(CHECK_OK);
1272
          names.Add(name, zone());
1273
        }
1274
        ExpectSemicolon(CHECK_OK);
1275
        result = factory()->NewEmptyStatement(pos);
1276
      } else {
1277
        result = ParseModuleDeclaration(&names, CHECK_OK);
1278
      }
1279
      break;
1280
    }
1281

    
1282
    case Token::FUNCTION:
1283
      result = ParseFunctionDeclaration(&names, CHECK_OK);
1284
      break;
1285

    
1286
    case Token::VAR:
1287
    case Token::LET:
1288
    case Token::CONST:
1289
      result = ParseVariableStatement(kModuleElement, &names, CHECK_OK);
1290
      break;
1291

    
1292
    default:
1293
      *ok = false;
1294
      ReportUnexpectedToken(scanner().current_token());
1295
      return NULL;
1296
  }
1297

    
1298
  // Extract declared names into export declarations and interface.
1299
  Interface* interface = top_scope_->interface();
1300
  for (int i = 0; i < names.length(); ++i) {
1301
#ifdef DEBUG
1302
    if (FLAG_print_interface_details)
1303
      PrintF("# Export %s ", names[i]->ToAsciiArray());
1304
#endif
1305
    Interface* inner = Interface::NewUnknown(zone());
1306
    interface->Add(names[i], inner, zone(), CHECK_OK);
1307
    if (!*ok)
1308
      return NULL;
1309
    VariableProxy* proxy = NewUnresolved(names[i], LET, inner);
1310
    USE(proxy);
1311
    // TODO(rossberg): Rethink whether we actually need to store export
1312
    // declarations (for compilation?).
1313
    // ExportDeclaration* declaration =
1314
    //     factory()->NewExportDeclaration(proxy, top_scope_, position);
1315
    // top_scope_->AddDeclaration(declaration);
1316
  }
1317

    
1318
  ASSERT(result != NULL);
1319
  return result;
1320
}
1321

    
1322

    
1323
Statement* Parser::ParseBlockElement(ZoneStringList* labels,
1324
                                     bool* ok) {
1325
  // (Ecma 262 5th Edition, clause 14):
1326
  // SourceElement:
1327
  //    Statement
1328
  //    FunctionDeclaration
1329
  //
1330
  // In harmony mode we allow additionally the following productions
1331
  // BlockElement (aka SourceElement):
1332
  //    LetDeclaration
1333
  //    ConstDeclaration
1334
  //    GeneratorDeclaration
1335

    
1336
  switch (peek()) {
1337
    case Token::FUNCTION:
1338
      return ParseFunctionDeclaration(NULL, ok);
1339
    case Token::LET:
1340
    case Token::CONST:
1341
      return ParseVariableStatement(kModuleElement, NULL, ok);
1342
    default:
1343
      return ParseStatement(labels, ok);
1344
  }
1345
}
1346

    
1347

    
1348
Statement* Parser::ParseStatement(ZoneStringList* labels, bool* ok) {
1349
  // Statement ::
1350
  //   Block
1351
  //   VariableStatement
1352
  //   EmptyStatement
1353
  //   ExpressionStatement
1354
  //   IfStatement
1355
  //   IterationStatement
1356
  //   ContinueStatement
1357
  //   BreakStatement
1358
  //   ReturnStatement
1359
  //   WithStatement
1360
  //   LabelledStatement
1361
  //   SwitchStatement
1362
  //   ThrowStatement
1363
  //   TryStatement
1364
  //   DebuggerStatement
1365

    
1366
  // Note: Since labels can only be used by 'break' and 'continue'
1367
  // statements, which themselves are only valid within blocks,
1368
  // iterations or 'switch' statements (i.e., BreakableStatements),
1369
  // labels can be simply ignored in all other cases; except for
1370
  // trivial labeled break statements 'label: break label' which is
1371
  // parsed into an empty statement.
1372
  switch (peek()) {
1373
    case Token::LBRACE:
1374
      return ParseBlock(labels, ok);
1375

    
1376
    case Token::CONST:  // fall through
1377
    case Token::LET:
1378
    case Token::VAR:
1379
      return ParseVariableStatement(kStatement, NULL, ok);
1380

    
1381
    case Token::SEMICOLON:
1382
      Next();
1383
      return factory()->NewEmptyStatement(RelocInfo::kNoPosition);
1384

    
1385
    case Token::IF:
1386
      return ParseIfStatement(labels, ok);
1387

    
1388
    case Token::DO:
1389
      return ParseDoWhileStatement(labels, ok);
1390

    
1391
    case Token::WHILE:
1392
      return ParseWhileStatement(labels, ok);
1393

    
1394
    case Token::FOR:
1395
      return ParseForStatement(labels, ok);
1396

    
1397
    case Token::CONTINUE:
1398
      return ParseContinueStatement(ok);
1399

    
1400
    case Token::BREAK:
1401
      return ParseBreakStatement(labels, ok);
1402

    
1403
    case Token::RETURN:
1404
      return ParseReturnStatement(ok);
1405

    
1406
    case Token::WITH:
1407
      return ParseWithStatement(labels, ok);
1408

    
1409
    case Token::SWITCH:
1410
      return ParseSwitchStatement(labels, ok);
1411

    
1412
    case Token::THROW:
1413
      return ParseThrowStatement(ok);
1414

    
1415
    case Token::TRY: {
1416
      // NOTE: It is somewhat complicated to have labels on
1417
      // try-statements. When breaking out of a try-finally statement,
1418
      // one must take great care not to treat it as a
1419
      // fall-through. It is much easier just to wrap the entire
1420
      // try-statement in a statement block and put the labels there
1421
      Block* result =
1422
          factory()->NewBlock(labels, 1, false, RelocInfo::kNoPosition);
1423
      Target target(&this->target_stack_, result);
1424
      TryStatement* statement = ParseTryStatement(CHECK_OK);
1425
      if (result) result->AddStatement(statement, zone());
1426
      return result;
1427
    }
1428

    
1429
    case Token::FUNCTION: {
1430
      // FunctionDeclaration is only allowed in the context of SourceElements
1431
      // (Ecma 262 5th Edition, clause 14):
1432
      // SourceElement:
1433
      //    Statement
1434
      //    FunctionDeclaration
1435
      // Common language extension is to allow function declaration in place
1436
      // of any statement. This language extension is disabled in strict mode.
1437
      //
1438
      // In Harmony mode, this case also handles the extension:
1439
      // Statement:
1440
      //    GeneratorDeclaration
1441
      if (!top_scope_->is_classic_mode()) {
1442
        ReportMessageAt(scanner().peek_location(), "strict_function",
1443
                        Vector<const char*>::empty());
1444
        *ok = false;
1445
        return NULL;
1446
      }
1447
      return ParseFunctionDeclaration(NULL, ok);
1448
    }
1449

    
1450
    case Token::DEBUGGER:
1451
      return ParseDebuggerStatement(ok);
1452

    
1453
    default:
1454
      return ParseExpressionOrLabelledStatement(labels, ok);
1455
  }
1456
}
1457

    
1458

    
1459
VariableProxy* Parser::NewUnresolved(
1460
    Handle<String> name, VariableMode mode, Interface* interface) {
1461
  // If we are inside a function, a declaration of a var/const variable is a
1462
  // truly local variable, and the scope of the variable is always the function
1463
  // scope.
1464
  // Let/const variables in harmony mode are always added to the immediately
1465
  // enclosing scope.
1466
  return DeclarationScope(mode)->NewUnresolved(
1467
      factory(), name, interface, position());
1468
}
1469

    
1470

    
1471
void Parser::Declare(Declaration* declaration, bool resolve, bool* ok) {
1472
  VariableProxy* proxy = declaration->proxy();
1473
  Handle<String> name = proxy->name();
1474
  VariableMode mode = declaration->mode();
1475
  Scope* declaration_scope = DeclarationScope(mode);
1476
  Variable* var = NULL;
1477

    
1478
  // If a suitable scope exists, then we can statically declare this
1479
  // variable and also set its mode. In any case, a Declaration node
1480
  // will be added to the scope so that the declaration can be added
1481
  // to the corresponding activation frame at runtime if necessary.
1482
  // For instance declarations inside an eval scope need to be added
1483
  // to the calling function context.
1484
  // Similarly, strict mode eval scope does not leak variable declarations to
1485
  // the caller's scope so we declare all locals, too.
1486
  if (declaration_scope->is_function_scope() ||
1487
      declaration_scope->is_strict_or_extended_eval_scope() ||
1488
      declaration_scope->is_block_scope() ||
1489
      declaration_scope->is_module_scope() ||
1490
      declaration_scope->is_global_scope()) {
1491
    // Declare the variable in the declaration scope.
1492
    // For the global scope, we have to check for collisions with earlier
1493
    // (i.e., enclosing) global scopes, to maintain the illusion of a single
1494
    // global scope.
1495
    var = declaration_scope->is_global_scope()
1496
        ? declaration_scope->Lookup(name)
1497
        : declaration_scope->LocalLookup(name);
1498
    if (var == NULL) {
1499
      // Declare the name.
1500
      var = declaration_scope->DeclareLocal(
1501
          name, mode, declaration->initialization(), proxy->interface());
1502
    } else if ((mode != VAR || var->mode() != VAR) &&
1503
               (!declaration_scope->is_global_scope() ||
1504
                IsLexicalVariableMode(mode) ||
1505
                IsLexicalVariableMode(var->mode()))) {
1506
      // The name was declared in this scope before; check for conflicting
1507
      // re-declarations. We have a conflict if either of the declarations is
1508
      // not a var (in the global scope, we also have to ignore legacy const for
1509
      // compatibility). There is similar code in runtime.cc in the Declare
1510
      // functions. The function CheckNonConflictingScope checks for conflicting
1511
      // var and let bindings from different scopes whereas this is a check for
1512
      // conflicting declarations within the same scope. This check also covers
1513
      // the special case
1514
      //
1515
      // function () { let x; { var x; } }
1516
      //
1517
      // because the var declaration is hoisted to the function scope where 'x'
1518
      // is already bound.
1519
      ASSERT(IsDeclaredVariableMode(var->mode()));
1520
      if (is_extended_mode()) {
1521
        // In harmony mode we treat re-declarations as early errors. See
1522
        // ES5 16 for a definition of early errors.
1523
        SmartArrayPointer<char> c_string = name->ToCString(DISALLOW_NULLS);
1524
        const char* elms[2] = { "Variable", *c_string };
1525
        Vector<const char*> args(elms, 2);
1526
        ReportMessage("redeclaration", args);
1527
        *ok = false;
1528
        return;
1529
      }
1530
      Handle<String> message_string =
1531
          isolate()->factory()->NewStringFromUtf8(CStrVector("Variable"),
1532
                                                  TENURED);
1533
      Expression* expression =
1534
          NewThrowTypeError(isolate()->factory()->redeclaration_string(),
1535
                            message_string, name);
1536
      declaration_scope->SetIllegalRedeclaration(expression);
1537
    }
1538
  }
1539

    
1540
  // We add a declaration node for every declaration. The compiler
1541
  // will only generate code if necessary. In particular, declarations
1542
  // for inner local variables that do not represent functions won't
1543
  // result in any generated code.
1544
  //
1545
  // Note that we always add an unresolved proxy even if it's not
1546
  // used, simply because we don't know in this method (w/o extra
1547
  // parameters) if the proxy is needed or not. The proxy will be
1548
  // bound during variable resolution time unless it was pre-bound
1549
  // below.
1550
  //
1551
  // WARNING: This will lead to multiple declaration nodes for the
1552
  // same variable if it is declared several times. This is not a
1553
  // semantic issue as long as we keep the source order, but it may be
1554
  // a performance issue since it may lead to repeated
1555
  // Runtime::DeclareContextSlot() calls.
1556
  declaration_scope->AddDeclaration(declaration);
1557

    
1558
  if (mode == CONST && declaration_scope->is_global_scope()) {
1559
    // For global const variables we bind the proxy to a variable.
1560
    ASSERT(resolve);  // should be set by all callers
1561
    Variable::Kind kind = Variable::NORMAL;
1562
    var = new(zone()) Variable(
1563
        declaration_scope, name, mode, true, kind,
1564
        kNeedsInitialization, proxy->interface());
1565
  } else if (declaration_scope->is_eval_scope() &&
1566
             declaration_scope->is_classic_mode()) {
1567
    // For variable declarations in a non-strict eval scope the proxy is bound
1568
    // to a lookup variable to force a dynamic declaration using the
1569
    // DeclareContextSlot runtime function.
1570
    Variable::Kind kind = Variable::NORMAL;
1571
    var = new(zone()) Variable(
1572
        declaration_scope, name, mode, true, kind,
1573
        declaration->initialization(), proxy->interface());
1574
    var->AllocateTo(Variable::LOOKUP, -1);
1575
    resolve = true;
1576
  }
1577

    
1578
  // If requested and we have a local variable, bind the proxy to the variable
1579
  // at parse-time. This is used for functions (and consts) declared inside
1580
  // statements: the corresponding function (or const) variable must be in the
1581
  // function scope and not a statement-local scope, e.g. as provided with a
1582
  // 'with' statement:
1583
  //
1584
  //   with (obj) {
1585
  //     function f() {}
1586
  //   }
1587
  //
1588
  // which is translated into:
1589
  //
1590
  //   with (obj) {
1591
  //     // in this case this is not: 'var f; f = function () {};'
1592
  //     var f = function () {};
1593
  //   }
1594
  //
1595
  // Note that if 'f' is accessed from inside the 'with' statement, it
1596
  // will be allocated in the context (because we must be able to look
1597
  // it up dynamically) but it will also be accessed statically, i.e.,
1598
  // with a context slot index and a context chain length for this
1599
  // initialization code. Thus, inside the 'with' statement, we need
1600
  // both access to the static and the dynamic context chain; the
1601
  // runtime needs to provide both.
1602
  if (resolve && var != NULL) {
1603
    proxy->BindTo(var);
1604

    
1605
    if (FLAG_harmony_modules) {
1606
      bool ok;
1607
#ifdef DEBUG
1608
      if (FLAG_print_interface_details)
1609
        PrintF("# Declare %s\n", var->name()->ToAsciiArray());
1610
#endif
1611
      proxy->interface()->Unify(var->interface(), zone(), &ok);
1612
      if (!ok) {
1613
#ifdef DEBUG
1614
        if (FLAG_print_interfaces) {
1615
          PrintF("DECLARE TYPE ERROR\n");
1616
          PrintF("proxy: ");
1617
          proxy->interface()->Print();
1618
          PrintF("var: ");
1619
          var->interface()->Print();
1620
        }
1621
#endif
1622
        ReportMessage("module_type_error", Vector<Handle<String> >(&name, 1));
1623
      }
1624
    }
1625
  }
1626
}
1627

    
1628

    
1629
// Language extension which is only enabled for source files loaded
1630
// through the API's extension mechanism.  A native function
1631
// declaration is resolved by looking up the function through a
1632
// callback provided by the extension.
1633
Statement* Parser::ParseNativeDeclaration(bool* ok) {
1634
  int pos = peek_position();
1635
  Expect(Token::FUNCTION, CHECK_OK);
1636
  Handle<String> name = ParseIdentifier(CHECK_OK);
1637
  Expect(Token::LPAREN, CHECK_OK);
1638
  bool done = (peek() == Token::RPAREN);
1639
  while (!done) {
1640
    ParseIdentifier(CHECK_OK);
1641
    done = (peek() == Token::RPAREN);
1642
    if (!done) {
1643
      Expect(Token::COMMA, CHECK_OK);
1644
    }
1645
  }
1646
  Expect(Token::RPAREN, CHECK_OK);
1647
  Expect(Token::SEMICOLON, CHECK_OK);
1648

    
1649
  // Make sure that the function containing the native declaration
1650
  // isn't lazily compiled. The extension structures are only
1651
  // accessible while parsing the first time not when reparsing
1652
  // because of lazy compilation.
1653
  DeclarationScope(VAR)->ForceEagerCompilation();
1654

    
1655
  // TODO(1240846): It's weird that native function declarations are
1656
  // introduced dynamically when we meet their declarations, whereas
1657
  // other functions are set up when entering the surrounding scope.
1658
  VariableProxy* proxy = NewUnresolved(name, VAR, Interface::NewValue());
1659
  Declaration* declaration =
1660
      factory()->NewVariableDeclaration(proxy, VAR, top_scope_, pos);
1661
  Declare(declaration, true, CHECK_OK);
1662
  NativeFunctionLiteral* lit = factory()->NewNativeFunctionLiteral(
1663
      name, extension_, RelocInfo::kNoPosition);
1664
  return factory()->NewExpressionStatement(
1665
      factory()->NewAssignment(
1666
          Token::INIT_VAR, proxy, lit, RelocInfo::kNoPosition),
1667
      pos);
1668
}
1669

    
1670

    
1671
Statement* Parser::ParseFunctionDeclaration(ZoneStringList* names, bool* ok) {
1672
  // FunctionDeclaration ::
1673
  //   'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}'
1674
  // GeneratorDeclaration ::
1675
  //   'function' '*' Identifier '(' FormalParameterListopt ')'
1676
  //      '{' FunctionBody '}'
1677
  Expect(Token::FUNCTION, CHECK_OK);
1678
  int pos = position();
1679
  bool is_generator = allow_generators() && Check(Token::MUL);
1680
  bool is_strict_reserved = false;
1681
  Handle<String> name = ParseIdentifierOrStrictReservedWord(
1682
      &is_strict_reserved, CHECK_OK);
1683
  FunctionLiteral* fun = ParseFunctionLiteral(name,
1684
                                              is_strict_reserved,
1685
                                              is_generator,
1686
                                              pos,
1687
                                              FunctionLiteral::DECLARATION,
1688
                                              CHECK_OK);
1689
  // Even if we're not at the top-level of the global or a function
1690
  // scope, we treat it as such and introduce the function with its
1691
  // initial value upon entering the corresponding scope.
1692
  // In extended mode, a function behaves as a lexical binding, except in the
1693
  // global scope.
1694
  VariableMode mode =
1695
      is_extended_mode() && !top_scope_->is_global_scope() ? LET : VAR;
1696
  VariableProxy* proxy = NewUnresolved(name, mode, Interface::NewValue());
1697
  Declaration* declaration =
1698
      factory()->NewFunctionDeclaration(proxy, mode, fun, top_scope_, pos);
1699
  Declare(declaration, true, CHECK_OK);
1700
  if (names) names->Add(name, zone());
1701
  return factory()->NewEmptyStatement(RelocInfo::kNoPosition);
1702
}
1703

    
1704

    
1705
Block* Parser::ParseBlock(ZoneStringList* labels, bool* ok) {
1706
  if (top_scope_->is_extended_mode()) return ParseScopedBlock(labels, ok);
1707

    
1708
  // Block ::
1709
  //   '{' Statement* '}'
1710

    
1711
  // Note that a Block does not introduce a new execution scope!
1712
  // (ECMA-262, 3rd, 12.2)
1713
  //
1714
  // Construct block expecting 16 statements.
1715
  Block* result =
1716
      factory()->NewBlock(labels, 16, false, RelocInfo::kNoPosition);
1717
  Target target(&this->target_stack_, result);
1718
  Expect(Token::LBRACE, CHECK_OK);
1719
  while (peek() != Token::RBRACE) {
1720
    Statement* stat = ParseStatement(NULL, CHECK_OK);
1721
    if (stat && !stat->IsEmpty()) {
1722
      result->AddStatement(stat, zone());
1723
    }
1724
  }
1725
  Expect(Token::RBRACE, CHECK_OK);
1726
  return result;
1727
}
1728

    
1729

    
1730
Block* Parser::ParseScopedBlock(ZoneStringList* labels, bool* ok) {
1731
  // The harmony mode uses block elements instead of statements.
1732
  //
1733
  // Block ::
1734
  //   '{' BlockElement* '}'
1735

    
1736
  // Construct block expecting 16 statements.
1737
  Block* body =
1738
      factory()->NewBlock(labels, 16, false, RelocInfo::kNoPosition);
1739
  Scope* block_scope = NewScope(top_scope_, BLOCK_SCOPE);
1740

    
1741
  // Parse the statements and collect escaping labels.
1742
  Expect(Token::LBRACE, CHECK_OK);
1743
  block_scope->set_start_position(scanner().location().beg_pos);
1744
  { BlockState block_state(this, block_scope);
1745
    TargetCollector collector(zone());
1746
    Target target(&this->target_stack_, &collector);
1747
    Target target_body(&this->target_stack_, body);
1748

    
1749
    while (peek() != Token::RBRACE) {
1750
      Statement* stat = ParseBlockElement(NULL, CHECK_OK);
1751
      if (stat && !stat->IsEmpty()) {
1752
        body->AddStatement(stat, zone());
1753
      }
1754
    }
1755
  }
1756
  Expect(Token::RBRACE, CHECK_OK);
1757
  block_scope->set_end_position(scanner().location().end_pos);
1758
  block_scope = block_scope->FinalizeBlockScope();
1759
  body->set_scope(block_scope);
1760
  return body;
1761
}
1762

    
1763

    
1764
Block* Parser::ParseVariableStatement(VariableDeclarationContext var_context,
1765
                                      ZoneStringList* names,
1766
                                      bool* ok) {
1767
  // VariableStatement ::
1768
  //   VariableDeclarations ';'
1769

    
1770
  Handle<String> ignore;
1771
  Block* result =
1772
      ParseVariableDeclarations(var_context, NULL, names, &ignore, CHECK_OK);
1773
  ExpectSemicolon(CHECK_OK);
1774
  return result;
1775
}
1776

    
1777

    
1778
bool Parser::IsEvalOrArguments(Handle<String> string) {
1779
  return string.is_identical_to(isolate()->factory()->eval_string()) ||
1780
      string.is_identical_to(isolate()->factory()->arguments_string());
1781
}
1782

    
1783

    
1784
// If the variable declaration declares exactly one non-const
1785
// variable, then *out is set to that variable. In all other cases,
1786
// *out is untouched; in particular, it is the caller's responsibility
1787
// to initialize it properly. This mechanism is used for the parsing
1788
// of 'for-in' loops.
1789
Block* Parser::ParseVariableDeclarations(
1790
    VariableDeclarationContext var_context,
1791
    VariableDeclarationProperties* decl_props,
1792
    ZoneStringList* names,
1793
    Handle<String>* out,
1794
    bool* ok) {
1795
  // VariableDeclarations ::
1796
  //   ('var' | 'const' | 'let') (Identifier ('=' AssignmentExpression)?)+[',']
1797
  //
1798
  // The ES6 Draft Rev3 specifies the following grammar for const declarations
1799
  //
1800
  // ConstDeclaration ::
1801
  //   const ConstBinding (',' ConstBinding)* ';'
1802
  // ConstBinding ::
1803
  //   Identifier '=' AssignmentExpression
1804
  //
1805
  // TODO(ES6):
1806
  // ConstBinding ::
1807
  //   BindingPattern '=' AssignmentExpression
1808

    
1809
  int pos = peek_position();
1810
  VariableMode mode = VAR;
1811
  // True if the binding needs initialization. 'let' and 'const' declared
1812
  // bindings are created uninitialized by their declaration nodes and
1813
  // need initialization. 'var' declared bindings are always initialized
1814
  // immediately by their declaration nodes.
1815
  bool needs_init = false;
1816
  bool is_const = false;
1817
  Token::Value init_op = Token::INIT_VAR;
1818
  if (peek() == Token::VAR) {
1819
    Consume(Token::VAR);
1820
  } else if (peek() == Token::CONST) {
1821
    // TODO(ES6): The ES6 Draft Rev4 section 12.2.2 reads:
1822
    //
1823
    // ConstDeclaration : const ConstBinding (',' ConstBinding)* ';'
1824
    //
1825
    // * It is a Syntax Error if the code that matches this production is not
1826
    //   contained in extended code.
1827
    //
1828
    // However disallowing const in classic mode will break compatibility with
1829
    // existing pages. Therefore we keep allowing const with the old
1830
    // non-harmony semantics in classic mode.
1831
    Consume(Token::CONST);
1832
    switch (top_scope_->language_mode()) {
1833
      case CLASSIC_MODE:
1834
        mode = CONST;
1835
        init_op = Token::INIT_CONST;
1836
        break;
1837
      case STRICT_MODE:
1838
        ReportMessage("strict_const", Vector<const char*>::empty());
1839
        *ok = false;
1840
        return NULL;
1841
      case EXTENDED_MODE:
1842
        if (var_context == kStatement) {
1843
          // In extended mode 'const' declarations are only allowed in source
1844
          // element positions.
1845
          ReportMessage("unprotected_const", Vector<const char*>::empty());
1846
          *ok = false;
1847
          return NULL;
1848
        }
1849
        mode = CONST_HARMONY;
1850
        init_op = Token::INIT_CONST_HARMONY;
1851
    }
1852
    is_const = true;
1853
    needs_init = true;
1854
  } else if (peek() == Token::LET) {
1855
    // ES6 Draft Rev4 section 12.2.1:
1856
    //
1857
    // LetDeclaration : let LetBindingList ;
1858
    //
1859
    // * It is a Syntax Error if the code that matches this production is not
1860
    //   contained in extended code.
1861
    if (!is_extended_mode()) {
1862
      ReportMessage("illegal_let", Vector<const char*>::empty());
1863
      *ok = false;
1864
      return NULL;
1865
    }
1866
    Consume(Token::LET);
1867
    if (var_context == kStatement) {
1868
      // Let declarations are only allowed in source element positions.
1869
      ReportMessage("unprotected_let", Vector<const char*>::empty());
1870
      *ok = false;
1871
      return NULL;
1872
    }
1873
    mode = LET;
1874
    needs_init = true;
1875
    init_op = Token::INIT_LET;
1876
  } else {
1877
    UNREACHABLE();  // by current callers
1878
  }
1879

    
1880
  Scope* declaration_scope = DeclarationScope(mode);
1881

    
1882
  // The scope of a var/const declared variable anywhere inside a function
1883
  // is the entire function (ECMA-262, 3rd, 10.1.3, and 12.2). Thus we can
1884
  // transform a source-level var/const declaration into a (Function)
1885
  // Scope declaration, and rewrite the source-level initialization into an
1886
  // assignment statement. We use a block to collect multiple assignments.
1887
  //
1888
  // We mark the block as initializer block because we don't want the
1889
  // rewriter to add a '.result' assignment to such a block (to get compliant
1890
  // behavior for code such as print(eval('var x = 7')), and for cosmetic
1891
  // reasons when pretty-printing. Also, unless an assignment (initialization)
1892
  // is inside an initializer block, it is ignored.
1893
  //
1894
  // Create new block with one expected declaration.
1895
  Block* block = factory()->NewBlock(NULL, 1, true, pos);
1896
  int nvars = 0;  // the number of variables declared
1897
  Handle<String> name;
1898
  do {
1899
    if (fni_ != NULL) fni_->Enter();
1900

    
1901
    // Parse variable name.
1902
    if (nvars > 0) Consume(Token::COMMA);
1903
    name = ParseIdentifier(CHECK_OK);
1904
    if (fni_ != NULL) fni_->PushVariableName(name);
1905

    
1906
    // Strict mode variables may not be named eval or arguments
1907
    if (!declaration_scope->is_classic_mode() && IsEvalOrArguments(name)) {
1908
      ReportMessage("strict_var_name", Vector<const char*>::empty());
1909
      *ok = false;
1910
      return NULL;
1911
    }
1912

    
1913
    // Declare variable.
1914
    // Note that we *always* must treat the initial value via a separate init
1915
    // assignment for variables and constants because the value must be assigned
1916
    // when the variable is encountered in the source. But the variable/constant
1917
    // is declared (and set to 'undefined') upon entering the function within
1918
    // which the variable or constant is declared. Only function variables have
1919
    // an initial value in the declaration (because they are initialized upon
1920
    // entering the function).
1921
    //
1922
    // If we have a const declaration, in an inner scope, the proxy is always
1923
    // bound to the declared variable (independent of possibly surrounding with
1924
    // statements).
1925
    // For let/const declarations in harmony mode, we can also immediately
1926
    // pre-resolve the proxy because it resides in the same scope as the
1927
    // declaration.
1928
    Interface* interface =
1929
        is_const ? Interface::NewConst() : Interface::NewValue();
1930
    VariableProxy* proxy = NewUnresolved(name, mode, interface);
1931
    Declaration* declaration =
1932
        factory()->NewVariableDeclaration(proxy, mode, top_scope_, pos);
1933
    Declare(declaration, mode != VAR, CHECK_OK);
1934
    nvars++;
1935
    if (declaration_scope->num_var_or_const() > kMaxNumFunctionLocals) {
1936
      ReportMessageAt(scanner().location(), "too_many_variables",
1937
                      Vector<const char*>::empty());
1938
      *ok = false;
1939
      return NULL;
1940
    }
1941
    if (names) names->Add(name, zone());
1942

    
1943
    // Parse initialization expression if present and/or needed. A
1944
    // declaration of the form:
1945
    //
1946
    //    var v = x;
1947
    //
1948
    // is syntactic sugar for:
1949
    //
1950
    //    var v; v = x;
1951
    //
1952
    // In particular, we need to re-lookup 'v' (in top_scope_, not
1953
    // declaration_scope) as it may be a different 'v' than the 'v' in the
1954
    // declaration (e.g., if we are inside a 'with' statement or 'catch'
1955
    // block).
1956
    //
1957
    // However, note that const declarations are different! A const
1958
    // declaration of the form:
1959
    //
1960
    //   const c = x;
1961
    //
1962
    // is *not* syntactic sugar for:
1963
    //
1964
    //   const c; c = x;
1965
    //
1966
    // The "variable" c initialized to x is the same as the declared
1967
    // one - there is no re-lookup (see the last parameter of the
1968
    // Declare() call above).
1969

    
1970
    Scope* initialization_scope = is_const ? declaration_scope : top_scope_;
1971
    Expression* value = NULL;
1972
    int pos = -1;
1973
    // Harmony consts have non-optional initializers.
1974
    if (peek() == Token::ASSIGN || mode == CONST_HARMONY) {
1975
      Expect(Token::ASSIGN, CHECK_OK);
1976
      pos = position();
1977
      value = ParseAssignmentExpression(var_context != kForStatement, CHECK_OK);
1978
      // Don't infer if it is "a = function(){...}();"-like expression.
1979
      if (fni_ != NULL &&
1980
          value->AsCall() == NULL &&
1981
          value->AsCallNew() == NULL) {
1982
        fni_->Infer();
1983
      } else {
1984
        fni_->RemoveLastFunction();
1985
      }
1986
      if (decl_props != NULL) *decl_props = kHasInitializers;
1987
    }
1988

    
1989
    // Record the end position of the initializer.
1990
    if (proxy->var() != NULL) {
1991
      proxy->var()->set_initializer_position(position());
1992
    }
1993

    
1994
    // Make sure that 'const x' and 'let x' initialize 'x' to undefined.
1995
    if (value == NULL && needs_init) {
1996
      value = GetLiteralUndefined(position());
1997
    }
1998

    
1999
    // Global variable declarations must be compiled in a specific
2000
    // way. When the script containing the global variable declaration
2001
    // is entered, the global variable must be declared, so that if it
2002
    // doesn't exist (on the global object itself, see ES5 errata) it
2003
    // gets created with an initial undefined value. This is handled
2004
    // by the declarations part of the function representing the
2005
    // top-level global code; see Runtime::DeclareGlobalVariable. If
2006
    // it already exists (in the object or in a prototype), it is
2007
    // *not* touched until the variable declaration statement is
2008
    // executed.
2009
    //
2010
    // Executing the variable declaration statement will always
2011
    // guarantee to give the global object a "local" variable; a
2012
    // variable defined in the global object and not in any
2013
    // prototype. This way, global variable declarations can shadow
2014
    // properties in the prototype chain, but only after the variable
2015
    // declaration statement has been executed. This is important in
2016
    // browsers where the global object (window) has lots of
2017
    // properties defined in prototype objects.
2018
    if (initialization_scope->is_global_scope() &&
2019
        !IsLexicalVariableMode(mode)) {
2020
      // Compute the arguments for the runtime call.
2021
      ZoneList<Expression*>* arguments =
2022
          new(zone()) ZoneList<Expression*>(3, zone());
2023
      // We have at least 1 parameter.
2024
      arguments->Add(factory()->NewLiteral(name, pos), zone());
2025
      CallRuntime* initialize;
2026

    
2027
      if (is_const) {
2028
        arguments->Add(value, zone());
2029
        value = NULL;  // zap the value to avoid the unnecessary assignment
2030

    
2031
        // Construct the call to Runtime_InitializeConstGlobal
2032
        // and add it to the initialization statement block.
2033
        // Note that the function does different things depending on
2034
        // the number of arguments (1 or 2).
2035
        initialize = factory()->NewCallRuntime(
2036
            isolate()->factory()->InitializeConstGlobal_string(),
2037
            Runtime::FunctionForId(Runtime::kInitializeConstGlobal),
2038
            arguments, pos);
2039
      } else {
2040
        // Add strict mode.
2041
        // We may want to pass singleton to avoid Literal allocations.
2042
        LanguageMode language_mode = initialization_scope->language_mode();
2043
        arguments->Add(factory()->NewNumberLiteral(language_mode, pos), zone());
2044

    
2045
        // Be careful not to assign a value to the global variable if
2046
        // we're in a with. The initialization value should not
2047
        // necessarily be stored in the global object in that case,
2048
        // which is why we need to generate a separate assignment node.
2049
        if (value != NULL && !inside_with()) {
2050
          arguments->Add(value, zone());
2051
          value = NULL;  // zap the value to avoid the unnecessary assignment
2052
        }
2053

    
2054
        // Construct the call to Runtime_InitializeVarGlobal
2055
        // and add it to the initialization statement block.
2056
        // Note that the function does different things depending on
2057
        // the number of arguments (2 or 3).
2058
        initialize = factory()->NewCallRuntime(
2059
            isolate()->factory()->InitializeVarGlobal_string(),
2060
            Runtime::FunctionForId(Runtime::kInitializeVarGlobal),
2061
            arguments, pos);
2062
      }
2063

    
2064
      block->AddStatement(
2065
          factory()->NewExpressionStatement(initialize, RelocInfo::kNoPosition),
2066
          zone());
2067
    } else if (needs_init) {
2068
      // Constant initializations always assign to the declared constant which
2069
      // is always at the function scope level. This is only relevant for
2070
      // dynamically looked-up variables and constants (the start context for
2071
      // constant lookups is always the function context, while it is the top
2072
      // context for var declared variables). Sigh...
2073
      // For 'let' and 'const' declared variables in harmony mode the
2074
      // initialization also always assigns to the declared variable.
2075
      ASSERT(proxy != NULL);
2076
      ASSERT(proxy->var() != NULL);
2077
      ASSERT(value != NULL);
2078
      Assignment* assignment =
2079
          factory()->NewAssignment(init_op, proxy, value, pos);
2080
      block->AddStatement(
2081
          factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition),
2082
          zone());
2083
      value = NULL;
2084
    }
2085

    
2086
    // Add an assignment node to the initialization statement block if we still
2087
    // have a pending initialization value.
2088
    if (value != NULL) {
2089
      ASSERT(mode == VAR);
2090
      // 'var' initializations are simply assignments (with all the consequences
2091
      // if they are inside a 'with' statement - they may change a 'with' object
2092
      // property).
2093
      VariableProxy* proxy =
2094
          initialization_scope->NewUnresolved(factory(), name, interface);
2095
      Assignment* assignment =
2096
          factory()->NewAssignment(init_op, proxy, value, pos);
2097
      block->AddStatement(
2098
          factory()->NewExpressionStatement(assignment, RelocInfo::kNoPosition),
2099
          zone());
2100
    }
2101

    
2102
    if (fni_ != NULL) fni_->Leave();
2103
  } while (peek() == Token::COMMA);
2104

    
2105
  // If there was a single non-const declaration, return it in the output
2106
  // parameter for possible use by for/in.
2107
  if (nvars == 1 && !is_const) {
2108
    *out = name;
2109
  }
2110

    
2111
  return block;
2112
}
2113

    
2114

    
2115
static bool ContainsLabel(ZoneStringList* labels, Handle<String> label) {
2116
  ASSERT(!label.is_null());
2117
  if (labels != NULL)
2118
    for (int i = labels->length(); i-- > 0; )
2119
      if (labels->at(i).is_identical_to(label))
2120
        return true;
2121

    
2122
  return false;
2123
}
2124

    
2125

    
2126
Statement* Parser::ParseExpressionOrLabelledStatement(ZoneStringList* labels,
2127
                                                      bool* ok) {
2128
  // ExpressionStatement | LabelledStatement ::
2129
  //   Expression ';'
2130
  //   Identifier ':' Statement
2131
  int pos = peek_position();
2132
  bool starts_with_idenfifier = peek_any_identifier();
2133
  Expression* expr = ParseExpression(true, CHECK_OK);
2134
  if (peek() == Token::COLON && starts_with_idenfifier && expr != NULL &&
2135
      expr->AsVariableProxy() != NULL &&
2136
      !expr->AsVariableProxy()->is_this()) {
2137
    // Expression is a single identifier, and not, e.g., a parenthesized
2138
    // identifier.
2139
    VariableProxy* var = expr->AsVariableProxy();
2140
    Handle<String> label = var->name();
2141
    // TODO(1240780): We don't check for redeclaration of labels
2142
    // during preparsing since keeping track of the set of active
2143
    // labels requires nontrivial changes to the way scopes are
2144
    // structured.  However, these are probably changes we want to
2145
    // make later anyway so we should go back and fix this then.
2146
    if (ContainsLabel(labels, label) || TargetStackContainsLabel(label)) {
2147
      SmartArrayPointer<char> c_string = label->ToCString(DISALLOW_NULLS);
2148
      const char* elms[2] = { "Label", *c_string };
2149
      Vector<const char*> args(elms, 2);
2150
      ReportMessage("redeclaration", args);
2151
      *ok = false;
2152
      return NULL;
2153
    }
2154
    if (labels == NULL) {
2155
      labels = new(zone()) ZoneStringList(4, zone());
2156
    }
2157
    labels->Add(label, zone());
2158
    // Remove the "ghost" variable that turned out to be a label
2159
    // from the top scope. This way, we don't try to resolve it
2160
    // during the scope processing.
2161
    top_scope_->RemoveUnresolved(var);
2162
    Expect(Token::COLON, CHECK_OK);
2163
    return ParseStatement(labels, ok);
2164
  }
2165

    
2166
  // If we have an extension, we allow a native function declaration.
2167
  // A native function declaration starts with "native function" with
2168
  // no line-terminator between the two words.
2169
  if (extension_ != NULL &&
2170
      peek() == Token::FUNCTION &&
2171
      !scanner().HasAnyLineTerminatorBeforeNext() &&
2172
      expr != NULL &&
2173
      expr->AsVariableProxy() != NULL &&
2174
      expr->AsVariableProxy()->name()->Equals(
2175
          isolate()->heap()->native_string()) &&
2176
      !scanner().literal_contains_escapes()) {
2177
    return ParseNativeDeclaration(ok);
2178
  }
2179

    
2180
  // Parsed expression statement, or the context-sensitive 'module' keyword.
2181
  // Only expect semicolon in the former case.
2182
  if (!FLAG_harmony_modules ||
2183
      peek() != Token::IDENTIFIER ||
2184
      scanner().HasAnyLineTerminatorBeforeNext() ||
2185
      expr->AsVariableProxy() == NULL ||
2186
      !expr->AsVariableProxy()->name()->Equals(
2187
          isolate()->heap()->module_string()) ||
2188
      scanner().literal_contains_escapes()) {
2189
    ExpectSemicolon(CHECK_OK);
2190
  }
2191
  return factory()->NewExpressionStatement(expr, pos);
2192
}
2193

    
2194

    
2195
IfStatement* Parser::ParseIfStatement(ZoneStringList* labels, bool* ok) {
2196
  // IfStatement ::
2197
  //   'if' '(' Expression ')' Statement ('else' Statement)?
2198

    
2199
  int pos = peek_position();
2200
  Expect(Token::IF, CHECK_OK);
2201
  Expect(Token::LPAREN, CHECK_OK);
2202
  Expression* condition = ParseExpression(true, CHECK_OK);
2203
  Expect(Token::RPAREN, CHECK_OK);
2204
  Statement* then_statement = ParseStatement(labels, CHECK_OK);
2205
  Statement* else_statement = NULL;
2206
  if (peek() == Token::ELSE) {
2207
    Next();
2208
    else_statement = ParseStatement(labels, CHECK_OK);
2209
  } else {
2210
    else_statement = factory()->NewEmptyStatement(RelocInfo::kNoPosition);
2211
  }
2212
  return factory()->NewIfStatement(
2213
      condition, then_statement, else_statement, pos);
2214
}
2215

    
2216

    
2217
Statement* Parser::ParseContinueStatement(bool* ok) {
2218
  // ContinueStatement ::
2219
  //   'continue' Identifier? ';'
2220

    
2221
  int pos = peek_position();
2222
  Expect(Token::CONTINUE, CHECK_OK);
2223
  Handle<String> label = Handle<String>::null();
2224
  Token::Value tok = peek();
2225
  if (!scanner().HasAnyLineTerminatorBeforeNext() &&
2226
      tok != Token::SEMICOLON && tok != Token::RBRACE && tok != Token::EOS) {
2227
    label = ParseIdentifier(CHECK_OK);
2228
  }
2229
  IterationStatement* target = NULL;
2230
  target = LookupContinueTarget(label, CHECK_OK);
2231
  if (target == NULL) {
2232
    // Illegal continue statement.
2233
    const char* message = "illegal_continue";
2234
    Vector<Handle<String> > args;
2235
    if (!label.is_null()) {
2236
      message = "unknown_label";
2237
      args = Vector<Handle<String> >(&label, 1);
2238
    }
2239
    ReportMessageAt(scanner().location(), message, args);
2240
    *ok = false;
2241
    return NULL;
2242
  }
2243
  ExpectSemicolon(CHECK_OK);
2244
  return factory()->NewContinueStatement(target, pos);
2245
}
2246

    
2247

    
2248
Statement* Parser::ParseBreakStatement(ZoneStringList* labels, bool* ok) {
2249
  // BreakStatement ::
2250
  //   'break' Identifier? ';'
2251

    
2252
  int pos = peek_position();
2253
  Expect(Token::BREAK, CHECK_OK);
2254
  Handle<String> label;
2255
  Token::Value tok = peek();
2256
  if (!scanner().HasAnyLineTerminatorBeforeNext() &&
2257
      tok != Token::SEMICOLON && tok != Token::RBRACE && tok != Token::EOS) {
2258
    label = ParseIdentifier(CHECK_OK);
2259
  }
2260
  // Parse labeled break statements that target themselves into
2261
  // empty statements, e.g. 'l1: l2: l3: break l2;'
2262
  if (!label.is_null() && ContainsLabel(labels, label)) {
2263
    ExpectSemicolon(CHECK_OK);
2264
    return factory()->NewEmptyStatement(pos);
2265
  }
2266
  BreakableStatement* target = NULL;
2267
  target = LookupBreakTarget(label, CHECK_OK);
2268
  if (target == NULL) {
2269
    // Illegal break statement.
2270
    const char* message = "illegal_break";
2271
    Vector<Handle<String> > args;
2272
    if (!label.is_null()) {
2273
      message = "unknown_label";
2274
      args = Vector<Handle<String> >(&label, 1);
2275
    }
2276
    ReportMessageAt(scanner().location(), message, args);
2277
    *ok = false;
2278
    return NULL;
2279
  }
2280
  ExpectSemicolon(CHECK_OK);
2281
  return factory()->NewBreakStatement(target, pos);
2282
}
2283

    
2284

    
2285
Statement* Parser::ParseReturnStatement(bool* ok) {
2286
  // ReturnStatement ::
2287
  //   'return' Expression? ';'
2288

    
2289
  // Consume the return token. It is necessary to do that before
2290
  // reporting any errors on it, because of the way errors are
2291
  // reported (underlining).
2292
  Expect(Token::RETURN, CHECK_OK);
2293
  int pos = position();
2294

    
2295
  Token::Value tok = peek();
2296
  Statement* result;
2297
  Expression* return_value;
2298
  if (scanner().HasAnyLineTerminatorBeforeNext() ||
2299
      tok == Token::SEMICOLON ||
2300
      tok == Token::RBRACE ||
2301
      tok == Token::EOS) {
2302
    return_value = GetLiteralUndefined(position());
2303
  } else {
2304
    return_value = ParseExpression(true, CHECK_OK);
2305
  }
2306
  ExpectSemicolon(CHECK_OK);
2307
  if (is_generator()) {
2308
    Expression* generator = factory()->NewVariableProxy(
2309
        current_function_state_->generator_object_variable());
2310
    Expression* yield = factory()->NewYield(
2311
        generator, return_value, Yield::FINAL, pos);
2312
    result = factory()->NewExpressionStatement(yield, pos);
2313
  } else {
2314
    result = factory()->NewReturnStatement(return_value, pos);
2315
  }
2316

    
2317
  // An ECMAScript program is considered syntactically incorrect if it
2318
  // contains a return statement that is not within the body of a
2319
  // function. See ECMA-262, section 12.9, page 67.
2320
  //
2321
  // To be consistent with KJS we report the syntax error at runtime.
2322
  Scope* declaration_scope = top_scope_->DeclarationScope();
2323
  if (declaration_scope->is_global_scope() ||
2324
      declaration_scope->is_eval_scope()) {
2325
    Handle<String> message = isolate()->factory()->illegal_return_string();
2326
    Expression* throw_error =
2327
        NewThrowSyntaxError(message, Handle<Object>::null());
2328
    return factory()->NewExpressionStatement(throw_error, pos);
2329
  }
2330
  return result;
2331
}
2332

    
2333

    
2334
Statement* Parser::ParseWithStatement(ZoneStringList* labels, bool* ok) {
2335
  // WithStatement ::
2336
  //   'with' '(' Expression ')' Statement
2337

    
2338
  Expect(Token::WITH, CHECK_OK);
2339
  int pos = position();
2340

    
2341
  if (!top_scope_->is_classic_mode()) {
2342
    ReportMessage("strict_mode_with", Vector<const char*>::empty());
2343
    *ok = false;
2344
    return NULL;
2345
  }
2346

    
2347
  Expect(Token::LPAREN, CHECK_OK);
2348
  Expression* expr = ParseExpression(true, CHECK_OK);
2349
  Expect(Token::RPAREN, CHECK_OK);
2350

    
2351
  top_scope_->DeclarationScope()->RecordWithStatement();
2352
  Scope* with_scope = NewScope(top_scope_, WITH_SCOPE);
2353
  Statement* stmt;
2354
  { BlockState block_state(this, with_scope);
2355
    with_scope->set_start_position(scanner().peek_location().beg_pos);
2356
    stmt = ParseStatement(labels, CHECK_OK);
2357
    with_scope->set_end_position(scanner().location().end_pos);
2358
  }
2359
  return factory()->NewWithStatement(with_scope, expr, stmt, pos);
2360
}
2361

    
2362

    
2363
CaseClause* Parser::ParseCaseClause(bool* default_seen_ptr, bool* ok) {
2364
  // CaseClause ::
2365
  //   'case' Expression ':' Statement*
2366
  //   'default' ':' Statement*
2367

    
2368
  Expression* label = NULL;  // NULL expression indicates default case
2369
  if (peek() == Token::CASE) {
2370
    Expect(Token::CASE, CHECK_OK);
2371
    label = ParseExpression(true, CHECK_OK);
2372
  } else {
2373
    Expect(Token::DEFAULT, CHECK_OK);
2374
    if (*default_seen_ptr) {
2375
      ReportMessage("multiple_defaults_in_switch",
2376
                    Vector<const char*>::empty());
2377
      *ok = false;
2378
      return NULL;
2379
    }
2380
    *default_seen_ptr = true;
2381
  }
2382
  Expect(Token::COLON, CHECK_OK);
2383
  int pos = position();
2384
  ZoneList<Statement*>* statements =
2385
      new(zone()) ZoneList<Statement*>(5, zone());
2386
  while (peek() != Token::CASE &&
2387
         peek() != Token::DEFAULT &&
2388
         peek() != Token::RBRACE) {
2389
    Statement* stat = ParseStatement(NULL, CHECK_OK);
2390
    statements->Add(stat, zone());
2391
  }
2392

    
2393
  return factory()->NewCaseClause(label, statements, pos);
2394
}
2395

    
2396

    
2397
SwitchStatement* Parser::ParseSwitchStatement(ZoneStringList* labels,
2398
                                              bool* ok) {
2399
  // SwitchStatement ::
2400
  //   'switch' '(' Expression ')' '{' CaseClause* '}'
2401

    
2402
  SwitchStatement* statement =
2403
      factory()->NewSwitchStatement(labels, peek_position());
2404
  Target target(&this->target_stack_, statement);
2405

    
2406
  Expect(Token::SWITCH, CHECK_OK);
2407
  Expect(Token::LPAREN, CHECK_OK);
2408
  Expression* tag = ParseExpression(true, CHECK_OK);
2409
  Expect(Token::RPAREN, CHECK_OK);
2410

    
2411
  bool default_seen = false;
2412
  ZoneList<CaseClause*>* cases = new(zone()) ZoneList<CaseClause*>(4, zone());
2413
  Expect(Token::LBRACE, CHECK_OK);
2414
  while (peek() != Token::RBRACE) {
2415
    CaseClause* clause = ParseCaseClause(&default_seen, CHECK_OK);
2416
    cases->Add(clause, zone());
2417
  }
2418
  Expect(Token::RBRACE, CHECK_OK);
2419

    
2420
  if (statement) statement->Initialize(tag, cases);
2421
  return statement;
2422
}
2423

    
2424

    
2425
Statement* Parser::ParseThrowStatement(bool* ok) {
2426
  // ThrowStatement ::
2427
  //   'throw' Expression ';'
2428

    
2429
  Expect(Token::THROW, CHECK_OK);
2430
  int pos = position();
2431
  if (scanner().HasAnyLineTerminatorBeforeNext()) {
2432
    ReportMessage("newline_after_throw", Vector<const char*>::empty());
2433
    *ok = false;
2434
    return NULL;
2435
  }
2436
  Expression* exception = ParseExpression(true, CHECK_OK);
2437
  ExpectSemicolon(CHECK_OK);
2438

    
2439
  return factory()->NewExpressionStatement(
2440
      factory()->NewThrow(exception, pos), pos);
2441
}
2442

    
2443

    
2444
TryStatement* Parser::ParseTryStatement(bool* ok) {
2445
  // TryStatement ::
2446
  //   'try' Block Catch
2447
  //   'try' Block Finally
2448
  //   'try' Block Catch Finally
2449
  //
2450
  // Catch ::
2451
  //   'catch' '(' Identifier ')' Block
2452
  //
2453
  // Finally ::
2454
  //   'finally' Block
2455

    
2456
  Expect(Token::TRY, CHECK_OK);
2457
  int pos = position();
2458

    
2459
  TargetCollector try_collector(zone());
2460
  Block* try_block;
2461

    
2462
  { Target target(&this->target_stack_, &try_collector);
2463
    try_block = ParseBlock(NULL, CHECK_OK);
2464
  }
2465

    
2466
  Token::Value tok = peek();
2467
  if (tok != Token::CATCH && tok != Token::FINALLY) {
2468
    ReportMessage("no_catch_or_finally", Vector<const char*>::empty());
2469
    *ok = false;
2470
    return NULL;
2471
  }
2472

    
2473
  // If we can break out from the catch block and there is a finally block,
2474
  // then we will need to collect escaping targets from the catch
2475
  // block. Since we don't know yet if there will be a finally block, we
2476
  // always collect the targets.
2477
  TargetCollector catch_collector(zone());
2478
  Scope* catch_scope = NULL;
2479
  Variable* catch_variable = NULL;
2480
  Block* catch_block = NULL;
2481
  Handle<String> name;
2482
  if (tok == Token::CATCH) {
2483
    Consume(Token::CATCH);
2484

    
2485
    Expect(Token::LPAREN, CHECK_OK);
2486
    catch_scope = NewScope(top_scope_, CATCH_SCOPE);
2487
    catch_scope->set_start_position(scanner().location().beg_pos);
2488
    name = ParseIdentifier(CHECK_OK);
2489

    
2490
    if (!top_scope_->is_classic_mode() && IsEvalOrArguments(name)) {
2491
      ReportMessage("strict_catch_variable", Vector<const char*>::empty());
2492
      *ok = false;
2493
      return NULL;
2494
    }
2495

    
2496
    Expect(Token::RPAREN, CHECK_OK);
2497

    
2498
    if (peek() == Token::LBRACE) {
2499
      Target target(&this->target_stack_, &catch_collector);
2500
      VariableMode mode = is_extended_mode() ? LET : VAR;
2501
      catch_variable =
2502
          catch_scope->DeclareLocal(name, mode, kCreatedInitialized);
2503

    
2504
      BlockState block_state(this, catch_scope);
2505
      catch_block = ParseBlock(NULL, CHECK_OK);
2506
    } else {
2507
      Expect(Token::LBRACE, CHECK_OK);
2508
    }
2509
    catch_scope->set_end_position(scanner().location().end_pos);
2510
    tok = peek();
2511
  }
2512

    
2513
  Block* finally_block = NULL;
2514
  if (tok == Token::FINALLY || catch_block == NULL) {
2515
    Consume(Token::FINALLY);
2516
    finally_block = ParseBlock(NULL, CHECK_OK);
2517
  }
2518

    
2519
  // Simplify the AST nodes by converting:
2520
  //   'try B0 catch B1 finally B2'
2521
  // to:
2522
  //   'try { try B0 catch B1 } finally B2'
2523

    
2524
  if (catch_block != NULL && finally_block != NULL) {
2525
    // If we have both, create an inner try/catch.
2526
    ASSERT(catch_scope != NULL && catch_variable != NULL);
2527
    int index = current_function_state_->NextHandlerIndex();
2528
    TryCatchStatement* statement = factory()->NewTryCatchStatement(
2529
        index, try_block, catch_scope, catch_variable, catch_block,
2530
        RelocInfo::kNoPosition);
2531
    statement->set_escaping_targets(try_collector.targets());
2532
    try_block = factory()->NewBlock(NULL, 1, false, RelocInfo::kNoPosition);
2533
    try_block->AddStatement(statement, zone());
2534
    catch_block = NULL;  // Clear to indicate it's been handled.
2535
  }
2536

    
2537
  TryStatement* result = NULL;
2538
  if (catch_block != NULL) {
2539
    ASSERT(finally_block == NULL);
2540
    ASSERT(catch_scope != NULL && catch_variable != NULL);
2541
    int index = current_function_state_->NextHandlerIndex();
2542
    result = factory()->NewTryCatchStatement(
2543
        index, try_block, catch_scope, catch_variable, catch_block, pos);
2544
  } else {
2545
    ASSERT(finally_block != NULL);
2546
    int index = current_function_state_->NextHandlerIndex();
2547
    result = factory()->NewTryFinallyStatement(
2548
        index, try_block, finally_block, pos);
2549
    // Combine the jump targets of the try block and the possible catch block.
2550
    try_collector.targets()->AddAll(*catch_collector.targets(), zone());
2551
  }
2552

    
2553
  result->set_escaping_targets(try_collector.targets());
2554
  return result;
2555
}
2556

    
2557

    
2558
DoWhileStatement* Parser::ParseDoWhileStatement(ZoneStringList* labels,
2559
                                                bool* ok) {
2560
  // DoStatement ::
2561
  //   'do' Statement 'while' '(' Expression ')' ';'
2562

    
2563
  DoWhileStatement* loop =
2564
      factory()->NewDoWhileStatement(labels, peek_position());
2565
  Target target(&this->target_stack_, loop);
2566

    
2567
  Expect(Token::DO, CHECK_OK);
2568
  Statement* body = ParseStatement(NULL, CHECK_OK);
2569
  Expect(Token::WHILE, CHECK_OK);
2570
  Expect(Token::LPAREN, CHECK_OK);
2571

    
2572
  Expression* cond = ParseExpression(true, CHECK_OK);
2573
  Expect(Token::RPAREN, CHECK_OK);
2574

    
2575
  // Allow do-statements to be terminated with and without
2576
  // semi-colons. This allows code such as 'do;while(0)return' to
2577
  // parse, which would not be the case if we had used the
2578
  // ExpectSemicolon() functionality here.
2579
  if (peek() == Token::SEMICOLON) Consume(Token::SEMICOLON);
2580

    
2581
  if (loop != NULL) loop->Initialize(cond, body);
2582
  return loop;
2583
}
2584

    
2585

    
2586
WhileStatement* Parser::ParseWhileStatement(ZoneStringList* labels, bool* ok) {
2587
  // WhileStatement ::
2588
  //   'while' '(' Expression ')' Statement
2589

    
2590
  WhileStatement* loop = factory()->NewWhileStatement(labels, peek_position());
2591
  Target target(&this->target_stack_, loop);
2592

    
2593
  Expect(Token::WHILE, CHECK_OK);
2594
  Expect(Token::LPAREN, CHECK_OK);
2595
  Expression* cond = ParseExpression(true, CHECK_OK);
2596
  Expect(Token::RPAREN, CHECK_OK);
2597
  Statement* body = ParseStatement(NULL, CHECK_OK);
2598

    
2599
  if (loop != NULL) loop->Initialize(cond, body);
2600
  return loop;
2601
}
2602

    
2603

    
2604
bool Parser::CheckInOrOf(bool accept_OF,
2605
                         ForEachStatement::VisitMode* visit_mode) {
2606
  if (Check(Token::IN)) {
2607
    *visit_mode = ForEachStatement::ENUMERATE;
2608
    return true;
2609
  } else if (allow_for_of() && accept_OF &&
2610
             CheckContextualKeyword(CStrVector("of"))) {
2611
    *visit_mode = ForEachStatement::ITERATE;
2612
    return true;
2613
  }
2614
  return false;
2615
}
2616

    
2617

    
2618
void Parser::InitializeForEachStatement(ForEachStatement* stmt,
2619
                                        Expression* each,
2620
                                        Expression* subject,
2621
                                        Statement* body) {
2622
  ForOfStatement* for_of = stmt->AsForOfStatement();
2623

    
2624
  if (for_of != NULL) {
2625
    Factory* heap_factory = isolate()->factory();
2626
    Handle<String> iterator_str = heap_factory->InternalizeOneByteString(
2627
        STATIC_ASCII_VECTOR(".iterator"));
2628
    Handle<String> result_str = heap_factory->InternalizeOneByteString(
2629
        STATIC_ASCII_VECTOR(".result"));
2630
    Variable* iterator =
2631
        top_scope_->DeclarationScope()->NewTemporary(iterator_str);
2632
    Variable* result = top_scope_->DeclarationScope()->NewTemporary(result_str);
2633

    
2634
    Expression* assign_iterator;
2635
    Expression* next_result;
2636
    Expression* result_done;
2637
    Expression* assign_each;
2638

    
2639
    // var iterator = iterable;
2640
    {
2641
      Expression* iterator_proxy = factory()->NewVariableProxy(iterator);
2642
      assign_iterator = factory()->NewAssignment(
2643
          Token::ASSIGN, iterator_proxy, subject, RelocInfo::kNoPosition);
2644
    }
2645

    
2646
    // var result = iterator.next();
2647
    {
2648
      Expression* iterator_proxy = factory()->NewVariableProxy(iterator);
2649
      Expression* next_literal = factory()->NewLiteral(
2650
          heap_factory->next_string(), RelocInfo::kNoPosition);
2651
      Expression* next_property = factory()->NewProperty(
2652
          iterator_proxy, next_literal, RelocInfo::kNoPosition);
2653
      ZoneList<Expression*>* next_arguments =
2654
          new(zone()) ZoneList<Expression*>(0, zone());
2655
      Expression* next_call = factory()->NewCall(
2656
          next_property, next_arguments, RelocInfo::kNoPosition);
2657
      Expression* result_proxy = factory()->NewVariableProxy(result);
2658
      next_result = factory()->NewAssignment(
2659
          Token::ASSIGN, result_proxy, next_call, RelocInfo::kNoPosition);
2660
    }
2661

    
2662
    // result.done
2663
    {
2664
      Expression* done_literal = factory()->NewLiteral(
2665
          heap_factory->done_string(), RelocInfo::kNoPosition);
2666
      Expression* result_proxy = factory()->NewVariableProxy(result);
2667
      result_done = factory()->NewProperty(
2668
          result_proxy, done_literal, RelocInfo::kNoPosition);
2669
    }
2670

    
2671
    // each = result.value
2672
    {
2673
      Expression* value_literal = factory()->NewLiteral(
2674
          heap_factory->value_string(), RelocInfo::kNoPosition);
2675
      Expression* result_proxy = factory()->NewVariableProxy(result);
2676
      Expression* result_value = factory()->NewProperty(
2677
          result_proxy, value_literal, RelocInfo::kNoPosition);
2678
      assign_each = factory()->NewAssignment(
2679
          Token::ASSIGN, each, result_value, RelocInfo::kNoPosition);
2680
    }
2681

    
2682
    for_of->Initialize(each, subject, body,
2683
                       assign_iterator, next_result, result_done, assign_each);
2684
  } else {
2685
    stmt->Initialize(each, subject, body);
2686
  }
2687
}
2688

    
2689

    
2690
Statement* Parser::ParseForStatement(ZoneStringList* labels, bool* ok) {
2691
  // ForStatement ::
2692
  //   'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement
2693

    
2694
  int pos = peek_position();
2695
  Statement* init = NULL;
2696

    
2697
  // Create an in-between scope for let-bound iteration variables.
2698
  Scope* saved_scope = top_scope_;
2699
  Scope* for_scope = NewScope(top_scope_, BLOCK_SCOPE);
2700
  top_scope_ = for_scope;
2701

    
2702
  Expect(Token::FOR, CHECK_OK);
2703
  Expect(Token::LPAREN, CHECK_OK);
2704
  for_scope->set_start_position(scanner().location().beg_pos);
2705
  if (peek() != Token::SEMICOLON) {
2706
    if (peek() == Token::VAR || peek() == Token::CONST) {
2707
      bool is_const = peek() == Token::CONST;
2708
      Handle<String> name;
2709
      VariableDeclarationProperties decl_props = kHasNoInitializers;
2710
      Block* variable_statement =
2711
          ParseVariableDeclarations(kForStatement, &decl_props, NULL, &name,
2712
                                    CHECK_OK);
2713
      bool accept_OF = decl_props == kHasNoInitializers;
2714
      ForEachStatement::VisitMode mode;
2715

    
2716
      if (!name.is_null() && CheckInOrOf(accept_OF, &mode)) {
2717
        Interface* interface =
2718
            is_const ? Interface::NewConst() : Interface::NewValue();
2719
        ForEachStatement* loop =
2720
            factory()->NewForEachStatement(mode, labels, pos);
2721
        Target target(&this->target_stack_, loop);
2722

    
2723
        Expression* enumerable = ParseExpression(true, CHECK_OK);
2724
        Expect(Token::RPAREN, CHECK_OK);
2725

    
2726
        VariableProxy* each =
2727
            top_scope_->NewUnresolved(factory(), name, interface);
2728
        Statement* body = ParseStatement(NULL, CHECK_OK);
2729
        InitializeForEachStatement(loop, each, enumerable, body);
2730
        Block* result =
2731
            factory()->NewBlock(NULL, 2, false, RelocInfo::kNoPosition);
2732
        result->AddStatement(variable_statement, zone());
2733
        result->AddStatement(loop, zone());
2734
        top_scope_ = saved_scope;
2735
        for_scope->set_end_position(scanner().location().end_pos);
2736
        for_scope = for_scope->FinalizeBlockScope();
2737
        ASSERT(for_scope == NULL);
2738
        // Parsed for-in loop w/ variable/const declaration.
2739
        return result;
2740
      } else {
2741
        init = variable_statement;
2742
      }
2743
    } else if (peek() == Token::LET) {
2744
      Handle<String> name;
2745
      VariableDeclarationProperties decl_props = kHasNoInitializers;
2746
      Block* variable_statement =
2747
         ParseVariableDeclarations(kForStatement, &decl_props, NULL, &name,
2748
                                   CHECK_OK);
2749
      bool accept_IN = !name.is_null() && decl_props != kHasInitializers;
2750
      bool accept_OF = decl_props == kHasNoInitializers;
2751
      ForEachStatement::VisitMode mode;
2752

    
2753
      if (accept_IN && CheckInOrOf(accept_OF, &mode)) {
2754
        // Rewrite a for-in statement of the form
2755
        //
2756
        //   for (let x in e) b
2757
        //
2758
        // into
2759
        //
2760
        //   <let x' be a temporary variable>
2761
        //   for (x' in e) {
2762
        //     let x;
2763
        //     x = x';
2764
        //     b;
2765
        //   }
2766

    
2767
        // TODO(keuchel): Move the temporary variable to the block scope, after
2768
        // implementing stack allocated block scoped variables.
2769
        Factory* heap_factory = isolate()->factory();
2770
        Handle<String> tempstr =
2771
            heap_factory->NewConsString(heap_factory->dot_for_string(), name);
2772
        Handle<String> tempname = heap_factory->InternalizeString(tempstr);
2773
        Variable* temp = top_scope_->DeclarationScope()->NewTemporary(tempname);
2774
        VariableProxy* temp_proxy = factory()->NewVariableProxy(temp);
2775
        ForEachStatement* loop =
2776
            factory()->NewForEachStatement(mode, labels, pos);
2777
        Target target(&this->target_stack_, loop);
2778

    
2779
        // The expression does not see the loop variable.
2780
        top_scope_ = saved_scope;
2781
        Expression* enumerable = ParseExpression(true, CHECK_OK);
2782
        top_scope_ = for_scope;
2783
        Expect(Token::RPAREN, CHECK_OK);
2784

    
2785
        VariableProxy* each =
2786
            top_scope_->NewUnresolved(factory(), name, Interface::NewValue());
2787
        Statement* body = ParseStatement(NULL, CHECK_OK);
2788
        Block* body_block =
2789
            factory()->NewBlock(NULL, 3, false, RelocInfo::kNoPosition);
2790
        Assignment* assignment = factory()->NewAssignment(
2791
            Token::ASSIGN, each, temp_proxy, RelocInfo::kNoPosition);
2792
        Statement* assignment_statement = factory()->NewExpressionStatement(
2793
            assignment, RelocInfo::kNoPosition);
2794
        body_block->AddStatement(variable_statement, zone());
2795
        body_block->AddStatement(assignment_statement, zone());
2796
        body_block->AddStatement(body, zone());
2797
        InitializeForEachStatement(loop, temp_proxy, enumerable, body_block);
2798
        top_scope_ = saved_scope;
2799
        for_scope->set_end_position(scanner().location().end_pos);
2800
        for_scope = for_scope->FinalizeBlockScope();
2801
        body_block->set_scope(for_scope);
2802
        // Parsed for-in loop w/ let declaration.
2803
        return loop;
2804

    
2805
      } else {
2806
        init = variable_statement;
2807
      }
2808
    } else {
2809
      Expression* expression = ParseExpression(false, CHECK_OK);
2810
      ForEachStatement::VisitMode mode;
2811
      bool accept_OF = expression->AsVariableProxy();
2812

    
2813
      if (CheckInOrOf(accept_OF, &mode)) {
2814
        // Signal a reference error if the expression is an invalid
2815
        // left-hand side expression.  We could report this as a syntax
2816
        // error here but for compatibility with JSC we choose to report
2817
        // the error at runtime.
2818
        if (expression == NULL || !expression->IsValidLeftHandSide()) {
2819
          Handle<String> message =
2820
              isolate()->factory()->invalid_lhs_in_for_in_string();
2821
          expression = NewThrowReferenceError(message);
2822
        }
2823
        ForEachStatement* loop =
2824
            factory()->NewForEachStatement(mode, labels, pos);
2825
        Target target(&this->target_stack_, loop);
2826

    
2827
        Expression* enumerable = ParseExpression(true, CHECK_OK);
2828
        Expect(Token::RPAREN, CHECK_OK);
2829

    
2830
        Statement* body = ParseStatement(NULL, CHECK_OK);
2831
        InitializeForEachStatement(loop, expression, enumerable, body);
2832
        top_scope_ = saved_scope;
2833
        for_scope->set_end_position(scanner().location().end_pos);
2834
        for_scope = for_scope->FinalizeBlockScope();
2835
        ASSERT(for_scope == NULL);
2836
        // Parsed for-in loop.
2837
        return loop;
2838

    
2839
      } else {
2840
        init = factory()->NewExpressionStatement(
2841
            expression, RelocInfo::kNoPosition);
2842
      }
2843
    }
2844
  }
2845

    
2846
  // Standard 'for' loop
2847
  ForStatement* loop = factory()->NewForStatement(labels, pos);
2848
  Target target(&this->target_stack_, loop);
2849

    
2850
  // Parsed initializer at this point.
2851
  Expect(Token::SEMICOLON, CHECK_OK);
2852

    
2853
  Expression* cond = NULL;
2854
  if (peek() != Token::SEMICOLON) {
2855
    cond = ParseExpression(true, CHECK_OK);
2856
  }
2857
  Expect(Token::SEMICOLON, CHECK_OK);
2858

    
2859
  Statement* next = NULL;
2860
  if (peek() != Token::RPAREN) {
2861
    Expression* exp = ParseExpression(true, CHECK_OK);
2862
    next = factory()->NewExpressionStatement(exp, RelocInfo::kNoPosition);
2863
  }
2864
  Expect(Token::RPAREN, CHECK_OK);
2865

    
2866
  Statement* body = ParseStatement(NULL, CHECK_OK);
2867
  top_scope_ = saved_scope;
2868
  for_scope->set_end_position(scanner().location().end_pos);
2869
  for_scope = for_scope->FinalizeBlockScope();
2870
  if (for_scope != NULL) {
2871
    // Rewrite a for statement of the form
2872
    //
2873
    //   for (let x = i; c; n) b
2874
    //
2875
    // into
2876
    //
2877
    //   {
2878
    //     let x = i;
2879
    //     for (; c; n) b
2880
    //   }
2881
    ASSERT(init != NULL);
2882
    Block* result = factory()->NewBlock(NULL, 2, false, RelocInfo::kNoPosition);
2883
    result->AddStatement(init, zone());
2884
    result->AddStatement(loop, zone());
2885
    result->set_scope(for_scope);
2886
    loop->Initialize(NULL, cond, next, body);
2887
    return result;
2888
  } else {
2889
    loop->Initialize(init, cond, next, body);
2890
    return loop;
2891
  }
2892
}
2893

    
2894

    
2895
// Precedence = 1
2896
Expression* Parser::ParseExpression(bool accept_IN, bool* ok) {
2897
  // Expression ::
2898
  //   AssignmentExpression
2899
  //   Expression ',' AssignmentExpression
2900

    
2901
  Expression* result = ParseAssignmentExpression(accept_IN, CHECK_OK);
2902
  while (peek() == Token::COMMA) {
2903
    Expect(Token::COMMA, CHECK_OK);
2904
    int pos = position();
2905
    Expression* right = ParseAssignmentExpression(accept_IN, CHECK_OK);
2906
    result = factory()->NewBinaryOperation(Token::COMMA, result, right, pos);
2907
  }
2908
  return result;
2909
}
2910

    
2911

    
2912
// Precedence = 2
2913
Expression* Parser::ParseAssignmentExpression(bool accept_IN, bool* ok) {
2914
  // AssignmentExpression ::
2915
  //   ConditionalExpression
2916
  //   YieldExpression
2917
  //   LeftHandSideExpression AssignmentOperator AssignmentExpression
2918

    
2919
  if (peek() == Token::YIELD && is_generator()) {
2920
    return ParseYieldExpression(ok);
2921
  }
2922

    
2923
  if (fni_ != NULL) fni_->Enter();
2924
  Expression* expression = ParseConditionalExpression(accept_IN, CHECK_OK);
2925

    
2926
  if (!Token::IsAssignmentOp(peek())) {
2927
    if (fni_ != NULL) fni_->Leave();
2928
    // Parsed conditional expression only (no assignment).
2929
    return expression;
2930
  }
2931

    
2932
  // Signal a reference error if the expression is an invalid left-hand
2933
  // side expression.  We could report this as a syntax error here but
2934
  // for compatibility with JSC we choose to report the error at
2935
  // runtime.
2936
  // TODO(ES5): Should change parsing for spec conformance.
2937
  if (expression == NULL || !expression->IsValidLeftHandSide()) {
2938
    Handle<String> message =
2939
        isolate()->factory()->invalid_lhs_in_assignment_string();
2940
    expression = NewThrowReferenceError(message);
2941
  }
2942

    
2943
  if (!top_scope_->is_classic_mode()) {
2944
    // Assignment to eval or arguments is disallowed in strict mode.
2945
    CheckStrictModeLValue(expression, "strict_lhs_assignment", CHECK_OK);
2946
  }
2947
  MarkAsLValue(expression);
2948

    
2949
  Token::Value op = Next();  // Get assignment operator.
2950
  int pos = position();
2951
  Expression* right = ParseAssignmentExpression(accept_IN, CHECK_OK);
2952

    
2953
  // TODO(1231235): We try to estimate the set of properties set by
2954
  // constructors. We define a new property whenever there is an
2955
  // assignment to a property of 'this'. We should probably only add
2956
  // properties if we haven't seen them before. Otherwise we'll
2957
  // probably overestimate the number of properties.
2958
  Property* property = expression ? expression->AsProperty() : NULL;
2959
  if (op == Token::ASSIGN &&
2960
      property != NULL &&
2961
      property->obj()->AsVariableProxy() != NULL &&
2962
      property->obj()->AsVariableProxy()->is_this()) {
2963
    current_function_state_->AddProperty();
2964
  }
2965

    
2966
  // If we assign a function literal to a property we pretenure the
2967
  // literal so it can be added as a constant function property.
2968
  if (property != NULL && right->AsFunctionLiteral() != NULL) {
2969
    right->AsFunctionLiteral()->set_pretenure();
2970
  }
2971

    
2972
  if (fni_ != NULL) {
2973
    // Check if the right hand side is a call to avoid inferring a
2974
    // name if we're dealing with "a = function(){...}();"-like
2975
    // expression.
2976
    if ((op == Token::INIT_VAR
2977
         || op == Token::INIT_CONST
2978
         || op == Token::ASSIGN)
2979
        && (right->AsCall() == NULL && right->AsCallNew() == NULL)) {
2980
      fni_->Infer();
2981
    } else {
2982
      fni_->RemoveLastFunction();
2983
    }
2984
    fni_->Leave();
2985
  }
2986

    
2987
  return factory()->NewAssignment(op, expression, right, pos);
2988
}
2989

    
2990

    
2991
Expression* Parser::ParseYieldExpression(bool* ok) {
2992
  // YieldExpression ::
2993
  //   'yield' '*'? AssignmentExpression
2994
  int pos = peek_position();
2995
  Expect(Token::YIELD, CHECK_OK);
2996
  Yield::Kind kind =
2997
      Check(Token::MUL) ? Yield::DELEGATING : Yield::SUSPEND;
2998
  Expression* generator_object = factory()->NewVariableProxy(
2999
      current_function_state_->generator_object_variable());
3000
  Expression* expression = ParseAssignmentExpression(false, CHECK_OK);
3001
  Yield* yield = factory()->NewYield(generator_object, expression, kind, pos);
3002
  if (kind == Yield::DELEGATING) {
3003
    yield->set_index(current_function_state_->NextHandlerIndex());
3004
  }
3005
  return yield;
3006
}
3007

    
3008

    
3009
// Precedence = 3
3010
Expression* Parser::ParseConditionalExpression(bool accept_IN, bool* ok) {
3011
  // ConditionalExpression ::
3012
  //   LogicalOrExpression
3013
  //   LogicalOrExpression '?' AssignmentExpression ':' AssignmentExpression
3014

    
3015
  int pos = peek_position();
3016
  // We start using the binary expression parser for prec >= 4 only!
3017
  Expression* expression = ParseBinaryExpression(4, accept_IN, CHECK_OK);
3018
  if (peek() != Token::CONDITIONAL) return expression;
3019
  Consume(Token::CONDITIONAL);
3020
  // In parsing the first assignment expression in conditional
3021
  // expressions we always accept the 'in' keyword; see ECMA-262,
3022
  // section 11.12, page 58.
3023
  Expression* left = ParseAssignmentExpression(true, CHECK_OK);
3024
  Expect(Token::COLON, CHECK_OK);
3025
  Expression* right = ParseAssignmentExpression(accept_IN, CHECK_OK);
3026
  return factory()->NewConditional(expression, left, right, pos);
3027
}
3028

    
3029

    
3030
int ParserBase::Precedence(Token::Value tok, bool accept_IN) {
3031
  if (tok == Token::IN && !accept_IN)
3032
    return 0;  // 0 precedence will terminate binary expression parsing
3033

    
3034
  return Token::Precedence(tok);
3035
}
3036

    
3037

    
3038
// Precedence >= 4
3039
Expression* Parser::ParseBinaryExpression(int prec, bool accept_IN, bool* ok) {
3040
  ASSERT(prec >= 4);
3041
  Expression* x = ParseUnaryExpression(CHECK_OK);
3042
  for (int prec1 = Precedence(peek(), accept_IN); prec1 >= prec; prec1--) {
3043
    // prec1 >= 4
3044
    while (Precedence(peek(), accept_IN) == prec1) {
3045
      Token::Value op = Next();
3046
      int pos = position();
3047
      Expression* y = ParseBinaryExpression(prec1 + 1, accept_IN, CHECK_OK);
3048

    
3049
      // Compute some expressions involving only number literals.
3050
      if (x && x->AsLiteral() && x->AsLiteral()->value()->IsNumber() &&
3051
          y && y->AsLiteral() && y->AsLiteral()->value()->IsNumber()) {
3052
        double x_val = x->AsLiteral()->value()->Number();
3053
        double y_val = y->AsLiteral()->value()->Number();
3054

    
3055
        switch (op) {
3056
          case Token::ADD:
3057
            x = factory()->NewNumberLiteral(x_val + y_val, pos);
3058
            continue;
3059
          case Token::SUB:
3060
            x = factory()->NewNumberLiteral(x_val - y_val, pos);
3061
            continue;
3062
          case Token::MUL:
3063
            x = factory()->NewNumberLiteral(x_val * y_val, pos);
3064
            continue;
3065
          case Token::DIV:
3066
            x = factory()->NewNumberLiteral(x_val / y_val, pos);
3067
            continue;
3068
          case Token::BIT_OR: {
3069
            int value = DoubleToInt32(x_val) | DoubleToInt32(y_val);
3070
            x = factory()->NewNumberLiteral(value, pos);
3071
            continue;
3072
          }
3073
          case Token::BIT_AND: {
3074
            int value = DoubleToInt32(x_val) & DoubleToInt32(y_val);
3075
            x = factory()->NewNumberLiteral(value, pos);
3076
            continue;
3077
          }
3078
          case Token::BIT_XOR: {
3079
            int value = DoubleToInt32(x_val) ^ DoubleToInt32(y_val);
3080
            x = factory()->NewNumberLiteral(value, pos);
3081
            continue;
3082
          }
3083
          case Token::SHL: {
3084
            int value = DoubleToInt32(x_val) << (DoubleToInt32(y_val) & 0x1f);
3085
            x = factory()->NewNumberLiteral(value, pos);
3086
            continue;
3087
          }
3088
          case Token::SHR: {
3089
            uint32_t shift = DoubleToInt32(y_val) & 0x1f;
3090
            uint32_t value = DoubleToUint32(x_val) >> shift;
3091
            x = factory()->NewNumberLiteral(value, pos);
3092
            continue;
3093
          }
3094
          case Token::SAR: {
3095
            uint32_t shift = DoubleToInt32(y_val) & 0x1f;
3096
            int value = ArithmeticShiftRight(DoubleToInt32(x_val), shift);
3097
            x = factory()->NewNumberLiteral(value, pos);
3098
            continue;
3099
          }
3100
          default:
3101
            break;
3102
        }
3103
      }
3104

    
3105
      // For now we distinguish between comparisons and other binary
3106
      // operations.  (We could combine the two and get rid of this
3107
      // code and AST node eventually.)
3108
      if (Token::IsCompareOp(op)) {
3109
        // We have a comparison.
3110
        Token::Value cmp = op;
3111
        switch (op) {
3112
          case Token::NE: cmp = Token::EQ; break;
3113
          case Token::NE_STRICT: cmp = Token::EQ_STRICT; break;
3114
          default: break;
3115
        }
3116
        x = factory()->NewCompareOperation(cmp, x, y, pos);
3117
        if (cmp != op) {
3118
          // The comparison was negated - add a NOT.
3119
          x = factory()->NewUnaryOperation(Token::NOT, x, pos);
3120
        }
3121

    
3122
      } else {
3123
        // We have a "normal" binary operation.
3124
        x = factory()->NewBinaryOperation(op, x, y, pos);
3125
      }
3126
    }
3127
  }
3128
  return x;
3129
}
3130

    
3131

    
3132
Expression* Parser::ParseUnaryExpression(bool* ok) {
3133
  // UnaryExpression ::
3134
  //   PostfixExpression
3135
  //   'delete' UnaryExpression
3136
  //   'void' UnaryExpression
3137
  //   'typeof' UnaryExpression
3138
  //   '++' UnaryExpression
3139
  //   '--' UnaryExpression
3140
  //   '+' UnaryExpression
3141
  //   '-' UnaryExpression
3142
  //   '~' UnaryExpression
3143
  //   '!' UnaryExpression
3144

    
3145
  Token::Value op = peek();
3146
  if (Token::IsUnaryOp(op)) {
3147
    op = Next();
3148
    int pos = position();
3149
    Expression* expression = ParseUnaryExpression(CHECK_OK);
3150

    
3151
    if (expression != NULL && (expression->AsLiteral() != NULL)) {
3152
      Handle<Object> literal = expression->AsLiteral()->value();
3153
      if (op == Token::NOT) {
3154
        // Convert the literal to a boolean condition and negate it.
3155
        bool condition = literal->BooleanValue();
3156
        Handle<Object> result = isolate()->factory()->ToBoolean(!condition);
3157
        return factory()->NewLiteral(result, pos);
3158
      } else if (literal->IsNumber()) {
3159
        // Compute some expressions involving only number literals.
3160
        double value = literal->Number();
3161
        switch (op) {
3162
          case Token::ADD:
3163
            return expression;
3164
          case Token::SUB:
3165
            return factory()->NewNumberLiteral(-value, pos);
3166
          case Token::BIT_NOT:
3167
            return factory()->NewNumberLiteral(~DoubleToInt32(value), pos);
3168
          default:
3169
            break;
3170
        }
3171
      }
3172
    }
3173

    
3174
    // "delete identifier" is a syntax error in strict mode.
3175
    if (op == Token::DELETE && !top_scope_->is_classic_mode()) {
3176
      VariableProxy* operand = expression->AsVariableProxy();
3177
      if (operand != NULL && !operand->is_this()) {
3178
        ReportMessage("strict_delete", Vector<const char*>::empty());
3179
        *ok = false;
3180
        return NULL;
3181
      }
3182
    }
3183

    
3184
    // Desugar '+foo' into 'foo*1', this enables the collection of type feedback
3185
    // without any special stub and the multiplication is removed later in
3186
    // Crankshaft's canonicalization pass.
3187
    if (op == Token::ADD) {
3188
      return factory()->NewBinaryOperation(Token::MUL,
3189
                                           expression,
3190
                                           factory()->NewNumberLiteral(1, pos),
3191
                                           pos);
3192
    }
3193
    // The same idea for '-foo' => 'foo*(-1)'.
3194
    if (op == Token::SUB) {
3195
      return factory()->NewBinaryOperation(Token::MUL,
3196
                                           expression,
3197
                                           factory()->NewNumberLiteral(-1, pos),
3198
                                           pos);
3199
    }
3200
    // ...and one more time for '~foo' => 'foo^(~0)'.
3201
    if (op == Token::BIT_NOT) {
3202
      return factory()->NewBinaryOperation(Token::BIT_XOR,
3203
                                           expression,
3204
                                           factory()->NewNumberLiteral(~0, pos),
3205
                                           pos);
3206
    }
3207

    
3208
    return factory()->NewUnaryOperation(op, expression, pos);
3209

    
3210
  } else if (Token::IsCountOp(op)) {
3211
    op = Next();
3212
    Expression* expression = ParseUnaryExpression(CHECK_OK);
3213
    // Signal a reference error if the expression is an invalid
3214
    // left-hand side expression.  We could report this as a syntax
3215
    // error here but for compatibility with JSC we choose to report the
3216
    // error at runtime.
3217
    if (expression == NULL || !expression->IsValidLeftHandSide()) {
3218
      Handle<String> message =
3219
          isolate()->factory()->invalid_lhs_in_prefix_op_string();
3220
      expression = NewThrowReferenceError(message);
3221
    }
3222

    
3223
    if (!top_scope_->is_classic_mode()) {
3224
      // Prefix expression operand in strict mode may not be eval or arguments.
3225
      CheckStrictModeLValue(expression, "strict_lhs_prefix", CHECK_OK);
3226
    }
3227
    MarkAsLValue(expression);
3228

    
3229
    return factory()->NewCountOperation(op,
3230
                                        true /* prefix */,
3231
                                        expression,
3232
                                        position());
3233

    
3234
  } else {
3235
    return ParsePostfixExpression(ok);
3236
  }
3237
}
3238

    
3239

    
3240
Expression* Parser::ParsePostfixExpression(bool* ok) {
3241
  // PostfixExpression ::
3242
  //   LeftHandSideExpression ('++' | '--')?
3243

    
3244
  Expression* expression = ParseLeftHandSideExpression(CHECK_OK);
3245
  if (!scanner().HasAnyLineTerminatorBeforeNext() &&
3246
      Token::IsCountOp(peek())) {
3247
    // Signal a reference error if the expression is an invalid
3248
    // left-hand side expression.  We could report this as a syntax
3249
    // error here but for compatibility with JSC we choose to report the
3250
    // error at runtime.
3251
    if (expression == NULL || !expression->IsValidLeftHandSide()) {
3252
      Handle<String> message =
3253
          isolate()->factory()->invalid_lhs_in_postfix_op_string();
3254
      expression = NewThrowReferenceError(message);
3255
    }
3256

    
3257
    if (!top_scope_->is_classic_mode()) {
3258
      // Postfix expression operand in strict mode may not be eval or arguments.
3259
      CheckStrictModeLValue(expression, "strict_lhs_prefix", CHECK_OK);
3260
    }
3261
    MarkAsLValue(expression);
3262

    
3263
    Token::Value next = Next();
3264
    expression =
3265
        factory()->NewCountOperation(next,
3266
                                     false /* postfix */,
3267
                                     expression,
3268
                                     position());
3269
  }
3270
  return expression;
3271
}
3272

    
3273

    
3274
Expression* Parser::ParseLeftHandSideExpression(bool* ok) {
3275
  // LeftHandSideExpression ::
3276
  //   (NewExpression | MemberExpression) ...
3277

    
3278
  Expression* result;
3279
  if (peek() == Token::NEW) {
3280
    result = ParseNewExpression(CHECK_OK);
3281
  } else {
3282
    result = ParseMemberExpression(CHECK_OK);
3283
  }
3284

    
3285
  while (true) {
3286
    switch (peek()) {
3287
      case Token::LBRACK: {
3288
        Consume(Token::LBRACK);
3289
        int pos = position();
3290
        Expression* index = ParseExpression(true, CHECK_OK);
3291
        result = factory()->NewProperty(result, index, pos);
3292
        Expect(Token::RBRACK, CHECK_OK);
3293
        break;
3294
      }
3295

    
3296
      case Token::LPAREN: {
3297
        int pos;
3298
        if (scanner().current_token() == Token::IDENTIFIER) {
3299
          // For call of an identifier we want to report position of
3300
          // the identifier as position of the call in the stack trace.
3301
          pos = position();
3302
        } else {
3303
          // For other kinds of calls we record position of the parenthesis as
3304
          // position of the call.  Note that this is extremely important for
3305
          // expressions of the form function(){...}() for which call position
3306
          // should not point to the closing brace otherwise it will intersect
3307
          // with positions recorded for function literal and confuse debugger.
3308
          pos = peek_position();
3309
          // Also the trailing parenthesis are a hint that the function will
3310
          // be called immediately. If we happen to have parsed a preceding
3311
          // function literal eagerly, we can also compile it eagerly.
3312
          if (result->IsFunctionLiteral() && mode() == PARSE_EAGERLY) {
3313
            result->AsFunctionLiteral()->set_parenthesized();
3314
          }
3315
        }
3316
        ZoneList<Expression*>* args = ParseArguments(CHECK_OK);
3317

    
3318
        // Keep track of eval() calls since they disable all local variable
3319
        // optimizations.
3320
        // The calls that need special treatment are the
3321
        // direct eval calls. These calls are all of the form eval(...), with
3322
        // no explicit receiver.
3323
        // These calls are marked as potentially direct eval calls. Whether
3324
        // they are actually direct calls to eval is determined at run time.
3325
        VariableProxy* callee = result->AsVariableProxy();
3326
        if (callee != NULL &&
3327
            callee->IsVariable(isolate()->factory()->eval_string())) {
3328
          top_scope_->DeclarationScope()->RecordEvalCall();
3329
        }
3330
        result = factory()->NewCall(result, args, pos);
3331
        if (fni_ != NULL) fni_->RemoveLastFunction();
3332
        break;
3333
      }
3334

    
3335
      case Token::PERIOD: {
3336
        Consume(Token::PERIOD);
3337
        int pos = position();
3338
        Handle<String> name = ParseIdentifierName(CHECK_OK);
3339
        result = factory()->NewProperty(
3340
            result, factory()->NewLiteral(name, pos), pos);
3341
        if (fni_ != NULL) fni_->PushLiteralName(name);
3342
        break;
3343
      }
3344

    
3345
      default:
3346
        return result;
3347
    }
3348
  }
3349
}
3350

    
3351

    
3352
Expression* Parser::ParseNewPrefix(PositionStack* stack, bool* ok) {
3353
  // NewExpression ::
3354
  //   ('new')+ MemberExpression
3355

    
3356
  // The grammar for new expressions is pretty warped. The keyword
3357
  // 'new' can either be a part of the new expression (where it isn't
3358
  // followed by an argument list) or a part of the member expression,
3359
  // where it must be followed by an argument list. To accommodate
3360
  // this, we parse the 'new' keywords greedily and keep track of how
3361
  // many we have parsed. This information is then passed on to the
3362
  // member expression parser, which is only allowed to match argument
3363
  // lists as long as it has 'new' prefixes left
3364
  Expect(Token::NEW, CHECK_OK);
3365
  PositionStack::Element pos(stack, position());
3366

    
3367
  Expression* result;
3368
  if (peek() == Token::NEW) {
3369
    result = ParseNewPrefix(stack, CHECK_OK);
3370
  } else {
3371
    result = ParseMemberWithNewPrefixesExpression(stack, CHECK_OK);
3372
  }
3373

    
3374
  if (!stack->is_empty()) {
3375
    int last = stack->pop();
3376
    result = factory()->NewCallNew(
3377
        result, new(zone()) ZoneList<Expression*>(0, zone()), last);
3378
  }
3379
  return result;
3380
}
3381

    
3382

    
3383
Expression* Parser::ParseNewExpression(bool* ok) {
3384
  PositionStack stack(ok);
3385
  return ParseNewPrefix(&stack, ok);
3386
}
3387

    
3388

    
3389
Expression* Parser::ParseMemberExpression(bool* ok) {
3390
  return ParseMemberWithNewPrefixesExpression(NULL, ok);
3391
}
3392

    
3393

    
3394
Expression* Parser::ParseMemberWithNewPrefixesExpression(PositionStack* stack,
3395
                                                         bool* ok) {
3396
  // MemberExpression ::
3397
  //   (PrimaryExpression | FunctionLiteral)
3398
  //     ('[' Expression ']' | '.' Identifier | Arguments)*
3399

    
3400
  // Parse the initial primary or function expression.
3401
  Expression* result = NULL;
3402
  if (peek() == Token::FUNCTION) {
3403
    Expect(Token::FUNCTION, CHECK_OK);
3404
    int function_token_position = position();
3405
    bool is_generator = allow_generators() && Check(Token::MUL);
3406
    Handle<String> name;
3407
    bool is_strict_reserved_name = false;
3408
    if (peek_any_identifier()) {
3409
      name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved_name,
3410
                                                 CHECK_OK);
3411
    }
3412
    FunctionLiteral::FunctionType function_type = name.is_null()
3413
        ? FunctionLiteral::ANONYMOUS_EXPRESSION
3414
        : FunctionLiteral::NAMED_EXPRESSION;
3415
    result = ParseFunctionLiteral(name,
3416
                                  is_strict_reserved_name,
3417
                                  is_generator,
3418
                                  function_token_position,
3419
                                  function_type,
3420
                                  CHECK_OK);
3421
  } else {
3422
    result = ParsePrimaryExpression(CHECK_OK);
3423
  }
3424

    
3425
  while (true) {
3426
    switch (peek()) {
3427
      case Token::LBRACK: {
3428
        Consume(Token::LBRACK);
3429
        int pos = position();
3430
        Expression* index = ParseExpression(true, CHECK_OK);
3431
        result = factory()->NewProperty(result, index, pos);
3432
        if (fni_ != NULL) {
3433
          if (index->IsPropertyName()) {
3434
            fni_->PushLiteralName(index->AsLiteral()->AsPropertyName());
3435
          } else {
3436
            fni_->PushLiteralName(
3437
                isolate()->factory()->anonymous_function_string());
3438
          }
3439
        }
3440
        Expect(Token::RBRACK, CHECK_OK);
3441
        break;
3442
      }
3443
      case Token::PERIOD: {
3444
        Consume(Token::PERIOD);
3445
        int pos = position();
3446
        Handle<String> name = ParseIdentifierName(CHECK_OK);
3447
        result = factory()->NewProperty(
3448
            result, factory()->NewLiteral(name, pos), pos);
3449
        if (fni_ != NULL) fni_->PushLiteralName(name);
3450
        break;
3451
      }
3452
      case Token::LPAREN: {
3453
        if ((stack == NULL) || stack->is_empty()) return result;
3454
        // Consume one of the new prefixes (already parsed).
3455
        ZoneList<Expression*>* args = ParseArguments(CHECK_OK);
3456
        int pos = stack->pop();
3457
        result = factory()->NewCallNew(result, args, pos);
3458
        break;
3459
      }
3460
      default:
3461
        return result;
3462
    }
3463
  }
3464
}
3465

    
3466

    
3467
DebuggerStatement* Parser::ParseDebuggerStatement(bool* ok) {
3468
  // In ECMA-262 'debugger' is defined as a reserved keyword. In some browser
3469
  // contexts this is used as a statement which invokes the debugger as i a
3470
  // break point is present.
3471
  // DebuggerStatement ::
3472
  //   'debugger' ';'
3473

    
3474
  int pos = peek_position();
3475
  Expect(Token::DEBUGGER, CHECK_OK);
3476
  ExpectSemicolon(CHECK_OK);
3477
  return factory()->NewDebuggerStatement(pos);
3478
}
3479

    
3480

    
3481
void Parser::ReportUnexpectedToken(Token::Value token) {
3482
  // We don't report stack overflows here, to avoid increasing the
3483
  // stack depth even further.  Instead we report it after parsing is
3484
  // over, in ParseProgram/ParseJson.
3485
  if (token == Token::ILLEGAL && stack_overflow()) return;
3486
  // Four of the tokens are treated specially
3487
  switch (token) {
3488
    case Token::EOS:
3489
      return ReportMessage("unexpected_eos", Vector<const char*>::empty());
3490
    case Token::NUMBER:
3491
      return ReportMessage("unexpected_token_number",
3492
                           Vector<const char*>::empty());
3493
    case Token::STRING:
3494
      return ReportMessage("unexpected_token_string",
3495
                           Vector<const char*>::empty());
3496
    case Token::IDENTIFIER:
3497
      return ReportMessage("unexpected_token_identifier",
3498
                           Vector<const char*>::empty());
3499
    case Token::FUTURE_RESERVED_WORD:
3500
      return ReportMessage("unexpected_reserved",
3501
                           Vector<const char*>::empty());
3502
    case Token::YIELD:
3503
    case Token::FUTURE_STRICT_RESERVED_WORD:
3504
      return ReportMessage(top_scope_->is_classic_mode() ?
3505
                               "unexpected_token_identifier" :
3506
                               "unexpected_strict_reserved",
3507
                           Vector<const char*>::empty());
3508
    default:
3509
      const char* name = Token::String(token);
3510
      ASSERT(name != NULL);
3511
      ReportMessage("unexpected_token", Vector<const char*>(&name, 1));
3512
  }
3513
}
3514

    
3515

    
3516
void Parser::ReportInvalidPreparseData(Handle<String> name, bool* ok) {
3517
  SmartArrayPointer<char> name_string = name->ToCString(DISALLOW_NULLS);
3518
  const char* element[1] = { *name_string };
3519
  ReportMessage("invalid_preparser_data",
3520
                Vector<const char*>(element, 1));
3521
  *ok = false;
3522
}
3523

    
3524

    
3525
Expression* Parser::ParsePrimaryExpression(bool* ok) {
3526
  // PrimaryExpression ::
3527
  //   'this'
3528
  //   'null'
3529
  //   'true'
3530
  //   'false'
3531
  //   Identifier
3532
  //   Number
3533
  //   String
3534
  //   ArrayLiteral
3535
  //   ObjectLiteral
3536
  //   RegExpLiteral
3537
  //   '(' Expression ')'
3538

    
3539
  int pos = peek_position();
3540
  Expression* result = NULL;
3541
  switch (peek()) {
3542
    case Token::THIS: {
3543
      Consume(Token::THIS);
3544
      result = factory()->NewVariableProxy(top_scope_->receiver());
3545
      break;
3546
    }
3547

    
3548
    case Token::NULL_LITERAL:
3549
      Consume(Token::NULL_LITERAL);
3550
      result = factory()->NewLiteral(isolate()->factory()->null_value(), pos);
3551
      break;
3552

    
3553
    case Token::TRUE_LITERAL:
3554
      Consume(Token::TRUE_LITERAL);
3555
      result = factory()->NewLiteral(isolate()->factory()->true_value(), pos);
3556
      break;
3557

    
3558
    case Token::FALSE_LITERAL:
3559
      Consume(Token::FALSE_LITERAL);
3560
      result = factory()->NewLiteral(isolate()->factory()->false_value(), pos);
3561
      break;
3562

    
3563
    case Token::IDENTIFIER:
3564
    case Token::YIELD:
3565
    case Token::FUTURE_STRICT_RESERVED_WORD: {
3566
      Handle<String> name = ParseIdentifier(CHECK_OK);
3567
      if (fni_ != NULL) fni_->PushVariableName(name);
3568
      // The name may refer to a module instance object, so its type is unknown.
3569
#ifdef DEBUG
3570
      if (FLAG_print_interface_details)
3571
        PrintF("# Variable %s ", name->ToAsciiArray());
3572
#endif
3573
      Interface* interface = Interface::NewUnknown(zone());
3574
      result = top_scope_->NewUnresolved(factory(), name, interface, pos);
3575
      break;
3576
    }
3577

    
3578
    case Token::NUMBER: {
3579
      Consume(Token::NUMBER);
3580
      ASSERT(scanner().is_literal_ascii());
3581
      double value = StringToDouble(isolate()->unicode_cache(),
3582
                                    scanner().literal_ascii_string(),
3583
                                    ALLOW_HEX | ALLOW_OCTAL |
3584
                                        ALLOW_IMPLICIT_OCTAL | ALLOW_BINARY);
3585
      result = factory()->NewNumberLiteral(value, pos);
3586
      break;
3587
    }
3588

    
3589
    case Token::STRING: {
3590
      Consume(Token::STRING);
3591
      Handle<String> symbol = GetSymbol();
3592
      result = factory()->NewLiteral(symbol, pos);
3593
      if (fni_ != NULL) fni_->PushLiteralName(symbol);
3594
      break;
3595
    }
3596

    
3597
    case Token::ASSIGN_DIV:
3598
      result = ParseRegExpLiteral(true, CHECK_OK);
3599
      break;
3600

    
3601
    case Token::DIV:
3602
      result = ParseRegExpLiteral(false, CHECK_OK);
3603
      break;
3604

    
3605
    case Token::LBRACK:
3606
      result = ParseArrayLiteral(CHECK_OK);
3607
      break;
3608

    
3609
    case Token::LBRACE:
3610
      result = ParseObjectLiteral(CHECK_OK);
3611
      break;
3612

    
3613
    case Token::LPAREN:
3614
      Consume(Token::LPAREN);
3615
      // Heuristically try to detect immediately called functions before
3616
      // seeing the call parentheses.
3617
      parenthesized_function_ = (peek() == Token::FUNCTION);
3618
      result = ParseExpression(true, CHECK_OK);
3619
      Expect(Token::RPAREN, CHECK_OK);
3620
      break;
3621

    
3622
    case Token::MOD:
3623
      if (allow_natives_syntax() || extension_ != NULL) {
3624
        result = ParseV8Intrinsic(CHECK_OK);
3625
        break;
3626
      }
3627
      // If we're not allowing special syntax we fall-through to the
3628
      // default case.
3629

    
3630
    default: {
3631
      Token::Value tok = Next();
3632
      ReportUnexpectedToken(tok);
3633
      *ok = false;
3634
      return NULL;
3635
    }
3636
  }
3637

    
3638
  return result;
3639
}
3640

    
3641

    
3642
Expression* Parser::ParseArrayLiteral(bool* ok) {
3643
  // ArrayLiteral ::
3644
  //   '[' Expression? (',' Expression?)* ']'
3645

    
3646
  int pos = peek_position();
3647
  ZoneList<Expression*>* values = new(zone()) ZoneList<Expression*>(4, zone());
3648
  Expect(Token::LBRACK, CHECK_OK);
3649
  while (peek() != Token::RBRACK) {
3650
    Expression* elem;
3651
    if (peek() == Token::COMMA) {
3652
      elem = GetLiteralTheHole(peek_position());
3653
    } else {
3654
      elem = ParseAssignmentExpression(true, CHECK_OK);
3655
    }
3656
    values->Add(elem, zone());
3657
    if (peek() != Token::RBRACK) {
3658
      Expect(Token::COMMA, CHECK_OK);
3659
    }
3660
  }
3661
  Expect(Token::RBRACK, CHECK_OK);
3662

    
3663
  // Update the scope information before the pre-parsing bailout.
3664
  int literal_index = current_function_state_->NextMaterializedLiteralIndex();
3665

    
3666
  // Allocate a fixed array to hold all the object literals.
3667
  Handle<JSArray> array =
3668
      isolate()->factory()->NewJSArray(0, FAST_HOLEY_SMI_ELEMENTS);
3669
  isolate()->factory()->SetElementsCapacityAndLength(
3670
      array, values->length(), values->length());
3671

    
3672
  // Fill in the literals.
3673
  Heap* heap = isolate()->heap();
3674
  bool is_simple = true;
3675
  int depth = 1;
3676
  bool is_holey = false;
3677
  for (int i = 0, n = values->length(); i < n; i++) {
3678
    MaterializedLiteral* m_literal = values->at(i)->AsMaterializedLiteral();
3679
    if (m_literal != NULL && m_literal->depth() + 1 > depth) {
3680
      depth = m_literal->depth() + 1;
3681
    }
3682
    Handle<Object> boilerplate_value = GetBoilerplateValue(values->at(i));
3683
    if (boilerplate_value->IsTheHole()) {
3684
      is_holey = true;
3685
    } else if (boilerplate_value->IsUninitialized()) {
3686
      is_simple = false;
3687
      JSObject::SetOwnElement(
3688
          array, i, handle(Smi::FromInt(0), isolate()), kNonStrictMode);
3689
    } else {
3690
      JSObject::SetOwnElement(array, i, boilerplate_value, kNonStrictMode);
3691
    }
3692
  }
3693

    
3694
  Handle<FixedArrayBase> element_values(array->elements());
3695

    
3696
  // Simple and shallow arrays can be lazily copied, we transform the
3697
  // elements array to a copy-on-write array.
3698
  if (is_simple && depth == 1 && values->length() > 0 &&
3699
      array->HasFastSmiOrObjectElements()) {
3700
    element_values->set_map(heap->fixed_cow_array_map());
3701
  }
3702

    
3703
  // Remember both the literal's constant values as well as the ElementsKind
3704
  // in a 2-element FixedArray.
3705
  Handle<FixedArray> literals = isolate()->factory()->NewFixedArray(2, TENURED);
3706

    
3707
  ElementsKind kind = array->GetElementsKind();
3708
  kind = is_holey ? GetHoleyElementsKind(kind) : GetPackedElementsKind(kind);
3709

    
3710
  literals->set(0, Smi::FromInt(kind));
3711
  literals->set(1, *element_values);
3712

    
3713
  return factory()->NewArrayLiteral(
3714
      literals, values, literal_index, is_simple, depth, pos);
3715
}
3716

    
3717

    
3718
bool Parser::IsBoilerplateProperty(ObjectLiteral::Property* property) {
3719
  return property != NULL &&
3720
         property->kind() != ObjectLiteral::Property::PROTOTYPE;
3721
}
3722

    
3723

    
3724
bool CompileTimeValue::IsCompileTimeValue(Expression* expression) {
3725
  if (expression->AsLiteral() != NULL) return true;
3726
  MaterializedLiteral* lit = expression->AsMaterializedLiteral();
3727
  return lit != NULL && lit->is_simple();
3728
}
3729

    
3730

    
3731
Handle<FixedArray> CompileTimeValue::GetValue(Isolate* isolate,
3732
                                              Expression* expression) {
3733
  Factory* factory = isolate->factory();
3734
  ASSERT(IsCompileTimeValue(expression));
3735
  Handle<FixedArray> result = factory->NewFixedArray(2, TENURED);
3736
  ObjectLiteral* object_literal = expression->AsObjectLiteral();
3737
  if (object_literal != NULL) {
3738
    ASSERT(object_literal->is_simple());
3739
    if (object_literal->fast_elements()) {
3740
      result->set(kLiteralTypeSlot, Smi::FromInt(OBJECT_LITERAL_FAST_ELEMENTS));
3741
    } else {
3742
      result->set(kLiteralTypeSlot, Smi::FromInt(OBJECT_LITERAL_SLOW_ELEMENTS));
3743
    }
3744
    result->set(kElementsSlot, *object_literal->constant_properties());
3745
  } else {
3746
    ArrayLiteral* array_literal = expression->AsArrayLiteral();
3747
    ASSERT(array_literal != NULL && array_literal->is_simple());
3748
    result->set(kLiteralTypeSlot, Smi::FromInt(ARRAY_LITERAL));
3749
    result->set(kElementsSlot, *array_literal->constant_elements());
3750
  }
3751
  return result;
3752
}
3753

    
3754

    
3755
CompileTimeValue::LiteralType CompileTimeValue::GetLiteralType(
3756
    Handle<FixedArray> value) {
3757
  Smi* literal_type = Smi::cast(value->get(kLiteralTypeSlot));
3758
  return static_cast<LiteralType>(literal_type->value());
3759
}
3760

    
3761

    
3762
Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) {
3763
  return Handle<FixedArray>(FixedArray::cast(value->get(kElementsSlot)));
3764
}
3765

    
3766

    
3767
Handle<Object> Parser::GetBoilerplateValue(Expression* expression) {
3768
  if (expression->AsLiteral() != NULL) {
3769
    return expression->AsLiteral()->value();
3770
  }
3771
  if (CompileTimeValue::IsCompileTimeValue(expression)) {
3772
    return CompileTimeValue::GetValue(isolate(), expression);
3773
  }
3774
  return isolate()->factory()->uninitialized_value();
3775
}
3776

    
3777

    
3778
void Parser::BuildObjectLiteralConstantProperties(
3779
    ZoneList<ObjectLiteral::Property*>* properties,
3780
    Handle<FixedArray> constant_properties,
3781
    bool* is_simple,
3782
    bool* fast_elements,
3783
    int* depth,
3784
    bool* may_store_doubles) {
3785
  int position = 0;
3786
  // Accumulate the value in local variables and store it at the end.
3787
  bool is_simple_acc = true;
3788
  int depth_acc = 1;
3789
  uint32_t max_element_index = 0;
3790
  uint32_t elements = 0;
3791
  for (int i = 0; i < properties->length(); i++) {
3792
    ObjectLiteral::Property* property = properties->at(i);
3793
    if (!IsBoilerplateProperty(property)) {
3794
      is_simple_acc = false;
3795
      continue;
3796
    }
3797
    MaterializedLiteral* m_literal = property->value()->AsMaterializedLiteral();
3798
    if (m_literal != NULL && m_literal->depth() >= depth_acc) {
3799
      depth_acc = m_literal->depth() + 1;
3800
    }
3801

    
3802
    // Add CONSTANT and COMPUTED properties to boilerplate. Use undefined
3803
    // value for COMPUTED properties, the real value is filled in at
3804
    // runtime. The enumeration order is maintained.
3805
    Handle<Object> key = property->key()->value();
3806
    Handle<Object> value = GetBoilerplateValue(property->value());
3807

    
3808
    // Ensure objects that may, at any point in time, contain fields with double
3809
    // representation are always treated as nested objects. This is true for
3810
    // computed fields (value is undefined), and smi and double literals
3811
    // (value->IsNumber()).
3812
    // TODO(verwaest): Remove once we can store them inline.
3813
    if (FLAG_track_double_fields &&
3814
        (value->IsNumber() || value->IsUninitialized())) {
3815
      *may_store_doubles = true;
3816
    }
3817

    
3818
    is_simple_acc = is_simple_acc && !value->IsUninitialized();
3819

    
3820
    // Keep track of the number of elements in the object literal and
3821
    // the largest element index.  If the largest element index is
3822
    // much larger than the number of elements, creating an object
3823
    // literal with fast elements will be a waste of space.
3824
    uint32_t element_index = 0;
3825
    if (key->IsString()
3826
        && Handle<String>::cast(key)->AsArrayIndex(&element_index)
3827
        && element_index > max_element_index) {
3828
      max_element_index = element_index;
3829
      elements++;
3830
    } else if (key->IsSmi()) {
3831
      int key_value = Smi::cast(*key)->value();
3832
      if (key_value > 0
3833
          && static_cast<uint32_t>(key_value) > max_element_index) {
3834
        max_element_index = key_value;
3835
      }
3836
      elements++;
3837
    }
3838

    
3839
    // Add name, value pair to the fixed array.
3840
    constant_properties->set(position++, *key);
3841
    constant_properties->set(position++, *value);
3842
  }
3843
  *fast_elements =
3844
      (max_element_index <= 32) || ((2 * elements) >= max_element_index);
3845
  *is_simple = is_simple_acc;
3846
  *depth = depth_acc;
3847
}
3848

    
3849

    
3850
Expression* Parser::ParseObjectLiteral(bool* ok) {
3851
  // ObjectLiteral ::
3852
  //   '{' (
3853
  //       ((IdentifierName | String | Number) ':' AssignmentExpression)
3854
  //     | (('get' | 'set') (IdentifierName | String | Number) FunctionLiteral)
3855
  //    )*[','] '}'
3856

    
3857
  int pos = peek_position();
3858
  ZoneList<ObjectLiteral::Property*>* properties =
3859
      new(zone()) ZoneList<ObjectLiteral::Property*>(4, zone());
3860
  int number_of_boilerplate_properties = 0;
3861
  bool has_function = false;
3862

    
3863
  ObjectLiteralChecker checker(this, top_scope_->language_mode());
3864

    
3865
  Expect(Token::LBRACE, CHECK_OK);
3866

    
3867
  while (peek() != Token::RBRACE) {
3868
    if (fni_ != NULL) fni_->Enter();
3869

    
3870
    Literal* key = NULL;
3871
    Token::Value next = peek();
3872
    int next_pos = peek_position();
3873

    
3874
    switch (next) {
3875
      case Token::FUTURE_RESERVED_WORD:
3876
      case Token::FUTURE_STRICT_RESERVED_WORD:
3877
      case Token::IDENTIFIER: {
3878
        bool is_getter = false;
3879
        bool is_setter = false;
3880
        Handle<String> id =
3881
            ParseIdentifierNameOrGetOrSet(&is_getter, &is_setter, CHECK_OK);
3882
        if (fni_ != NULL) fni_->PushLiteralName(id);
3883

    
3884
        if ((is_getter || is_setter) && peek() != Token::COLON) {
3885
          // Special handling of getter and setter syntax:
3886
          // { ... , get foo() { ... }, ... , set foo(v) { ... v ... } , ... }
3887
          // We have already read the "get" or "set" keyword.
3888
          Token::Value next = Next();
3889
          bool is_keyword = Token::IsKeyword(next);
3890
          if (next != i::Token::IDENTIFIER &&
3891
              next != i::Token::FUTURE_RESERVED_WORD &&
3892
              next != i::Token::FUTURE_STRICT_RESERVED_WORD &&
3893
              next != i::Token::NUMBER &&
3894
              next != i::Token::STRING &&
3895
              !is_keyword) {
3896
            // Unexpected token.
3897
            ReportUnexpectedToken(next);
3898
            *ok = false;
3899
            return NULL;
3900
          }
3901
          // Validate the property.
3902
          PropertyKind type = is_getter ? kGetterProperty : kSetterProperty;
3903
          checker.CheckProperty(next, type, CHECK_OK);
3904
          Handle<String> name = is_keyword
3905
              ? isolate_->factory()->InternalizeUtf8String(Token::String(next))
3906
              : GetSymbol();
3907
          FunctionLiteral* value =
3908
              ParseFunctionLiteral(name,
3909
                                   false,   // reserved words are allowed here
3910
                                   false,   // not a generator
3911
                                   RelocInfo::kNoPosition,
3912
                                   FunctionLiteral::ANONYMOUS_EXPRESSION,
3913
                                   CHECK_OK);
3914
          // Allow any number of parameters for compatibilty with JSC.
3915
          // Specification only allows zero parameters for get and one for set.
3916
          ObjectLiteral::Property* property =
3917
              factory()->NewObjectLiteralProperty(is_getter, value, next_pos);
3918
          if (IsBoilerplateProperty(property)) {
3919
            number_of_boilerplate_properties++;
3920
          }
3921
          properties->Add(property, zone());
3922
          if (peek() != Token::RBRACE) Expect(Token::COMMA, CHECK_OK);
3923

    
3924
          if (fni_ != NULL) {
3925
            fni_->Infer();
3926
            fni_->Leave();
3927
          }
3928
          continue;  // restart the while
3929
        }
3930
        // Failed to parse as get/set property, so it's just a property
3931
        // called "get" or "set".
3932
        key = factory()->NewLiteral(id, next_pos);
3933
        break;
3934
      }
3935
      case Token::STRING: {
3936
        Consume(Token::STRING);
3937
        Handle<String> string = GetSymbol();
3938
        if (fni_ != NULL) fni_->PushLiteralName(string);
3939
        uint32_t index;
3940
        if (!string.is_null() && string->AsArrayIndex(&index)) {
3941
          key = factory()->NewNumberLiteral(index, next_pos);
3942
          break;
3943
        }
3944
        key = factory()->NewLiteral(string, next_pos);
3945
        break;
3946
      }
3947
      case Token::NUMBER: {
3948
        Consume(Token::NUMBER);
3949
        ASSERT(scanner().is_literal_ascii());
3950
        double value = StringToDouble(isolate()->unicode_cache(),
3951
                                      scanner().literal_ascii_string(),
3952
                                      ALLOW_HEX | ALLOW_OCTAL |
3953
                                          ALLOW_IMPLICIT_OCTAL | ALLOW_BINARY);
3954
        key = factory()->NewNumberLiteral(value, next_pos);
3955
        break;
3956
      }
3957
      default:
3958
        if (Token::IsKeyword(next)) {
3959
          Consume(next);
3960
          Handle<String> string = GetSymbol();
3961
          key = factory()->NewLiteral(string, next_pos);
3962
        } else {
3963
          // Unexpected token.
3964
          Token::Value next = Next();
3965
          ReportUnexpectedToken(next);
3966
          *ok = false;
3967
          return NULL;
3968
        }
3969
    }
3970

    
3971
    // Validate the property
3972
    checker.CheckProperty(next, kValueProperty, CHECK_OK);
3973

    
3974
    Expect(Token::COLON, CHECK_OK);
3975
    Expression* value = ParseAssignmentExpression(true, CHECK_OK);
3976

    
3977
    ObjectLiteral::Property* property =
3978
        new(zone()) ObjectLiteral::Property(key, value, isolate());
3979

    
3980
    // Mark top-level object literals that contain function literals and
3981
    // pretenure the literal so it can be added as a constant function
3982
    // property.
3983
    if (top_scope_->DeclarationScope()->is_global_scope() &&
3984
        value->AsFunctionLiteral() != NULL) {
3985
      has_function = true;
3986
      value->AsFunctionLiteral()->set_pretenure();
3987
    }
3988

    
3989
    // Count CONSTANT or COMPUTED properties to maintain the enumeration order.
3990
    if (IsBoilerplateProperty(property)) number_of_boilerplate_properties++;
3991
    properties->Add(property, zone());
3992

    
3993
    // TODO(1240767): Consider allowing trailing comma.
3994
    if (peek() != Token::RBRACE) Expect(Token::COMMA, CHECK_OK);
3995

    
3996
    if (fni_ != NULL) {
3997
      fni_->Infer();
3998
      fni_->Leave();
3999
    }
4000
  }
4001
  Expect(Token::RBRACE, CHECK_OK);
4002

    
4003
  // Computation of literal_index must happen before pre parse bailout.
4004
  int literal_index = current_function_state_->NextMaterializedLiteralIndex();
4005

    
4006
  Handle<FixedArray> constant_properties = isolate()->factory()->NewFixedArray(
4007
      number_of_boilerplate_properties * 2, TENURED);
4008

    
4009
  bool is_simple = true;
4010
  bool fast_elements = true;
4011
  int depth = 1;
4012
  bool may_store_doubles = false;
4013
  BuildObjectLiteralConstantProperties(properties,
4014
                                       constant_properties,
4015
                                       &is_simple,
4016
                                       &fast_elements,
4017
                                       &depth,
4018
                                       &may_store_doubles);
4019
  return factory()->NewObjectLiteral(constant_properties,
4020
                                     properties,
4021
                                     literal_index,
4022
                                     is_simple,
4023
                                     fast_elements,
4024
                                     depth,
4025
                                     may_store_doubles,
4026
                                     has_function,
4027
                                     pos);
4028
}
4029

    
4030

    
4031
Expression* Parser::ParseRegExpLiteral(bool seen_equal, bool* ok) {
4032
  int pos = peek_position();
4033
  if (!scanner().ScanRegExpPattern(seen_equal)) {
4034
    Next();
4035
    ReportMessage("unterminated_regexp", Vector<const char*>::empty());
4036
    *ok = false;
4037
    return NULL;
4038
  }
4039

    
4040
  int literal_index = current_function_state_->NextMaterializedLiteralIndex();
4041

    
4042
  Handle<String> js_pattern = NextLiteralString(TENURED);
4043
  scanner().ScanRegExpFlags();
4044
  Handle<String> js_flags = NextLiteralString(TENURED);
4045
  Next();
4046

    
4047
  return factory()->NewRegExpLiteral(js_pattern, js_flags, literal_index, pos);
4048
}
4049

    
4050

    
4051
ZoneList<Expression*>* Parser::ParseArguments(bool* ok) {
4052
  // Arguments ::
4053
  //   '(' (AssignmentExpression)*[','] ')'
4054

    
4055
  ZoneList<Expression*>* result = new(zone()) ZoneList<Expression*>(4, zone());
4056
  Expect(Token::LPAREN, CHECK_OK);
4057
  bool done = (peek() == Token::RPAREN);
4058
  while (!done) {
4059
    Expression* argument = ParseAssignmentExpression(true, CHECK_OK);
4060
    result->Add(argument, zone());
4061
    if (result->length() > Code::kMaxArguments) {
4062
      ReportMessageAt(scanner().location(), "too_many_arguments",
4063
                      Vector<const char*>::empty());
4064
      *ok = false;
4065
      return NULL;
4066
    }
4067
    done = (peek() == Token::RPAREN);
4068
    if (!done) Expect(Token::COMMA, CHECK_OK);
4069
  }
4070
  Expect(Token::RPAREN, CHECK_OK);
4071
  return result;
4072
}
4073

    
4074

    
4075
class SingletonLogger : public ParserRecorder {
4076
 public:
4077
  SingletonLogger() : has_error_(false), start_(-1), end_(-1) { }
4078
  virtual ~SingletonLogger() { }
4079

    
4080
  void Reset() { has_error_ = false; }
4081

    
4082
  virtual void LogFunction(int start,
4083
                           int end,
4084
                           int literals,
4085
                           int properties,
4086
                           LanguageMode mode) {
4087
    ASSERT(!has_error_);
4088
    start_ = start;
4089
    end_ = end;
4090
    literals_ = literals;
4091
    properties_ = properties;
4092
    mode_ = mode;
4093
  };
4094

    
4095
  // Logs a symbol creation of a literal or identifier.
4096
  virtual void LogAsciiSymbol(int start, Vector<const char> literal) { }
4097
  virtual void LogUtf16Symbol(int start, Vector<const uc16> literal) { }
4098

    
4099
  // Logs an error message and marks the log as containing an error.
4100
  // Further logging will be ignored, and ExtractData will return a vector
4101
  // representing the error only.
4102
  virtual void LogMessage(int start,
4103
                          int end,
4104
                          const char* message,
4105
                          const char* argument_opt) {
4106
    if (has_error_) return;
4107
    has_error_ = true;
4108
    start_ = start;
4109
    end_ = end;
4110
    message_ = message;
4111
    argument_opt_ = argument_opt;
4112
  }
4113

    
4114
  virtual int function_position() { return 0; }
4115

    
4116
  virtual int symbol_position() { return 0; }
4117

    
4118
  virtual int symbol_ids() { return -1; }
4119

    
4120
  virtual Vector<unsigned> ExtractData() {
4121
    UNREACHABLE();
4122
    return Vector<unsigned>();
4123
  }
4124

    
4125
  virtual void PauseRecording() { }
4126

    
4127
  virtual void ResumeRecording() { }
4128

    
4129
  bool has_error() { return has_error_; }
4130

    
4131
  int start() { return start_; }
4132
  int end() { return end_; }
4133
  int literals() {
4134
    ASSERT(!has_error_);
4135
    return literals_;
4136
  }
4137
  int properties() {
4138
    ASSERT(!has_error_);
4139
    return properties_;
4140
  }
4141
  LanguageMode language_mode() {
4142
    ASSERT(!has_error_);
4143
    return mode_;
4144
  }
4145
  const char* message() {
4146
    ASSERT(has_error_);
4147
    return message_;
4148
  }
4149
  const char* argument_opt() {
4150
    ASSERT(has_error_);
4151
    return argument_opt_;
4152
  }
4153

    
4154
 private:
4155
  bool has_error_;
4156
  int start_;
4157
  int end_;
4158
  // For function entries.
4159
  int literals_;
4160
  int properties_;
4161
  LanguageMode mode_;
4162
  // For error messages.
4163
  const char* message_;
4164
  const char* argument_opt_;
4165
};
4166

    
4167

    
4168
FunctionLiteral* Parser::ParseFunctionLiteral(
4169
    Handle<String> function_name,
4170
    bool name_is_strict_reserved,
4171
    bool is_generator,
4172
    int function_token_pos,
4173
    FunctionLiteral::FunctionType function_type,
4174
    bool* ok) {
4175
  // Function ::
4176
  //   '(' FormalParameterList? ')' '{' FunctionBody '}'
4177

    
4178
  int pos = function_token_pos == RelocInfo::kNoPosition
4179
      ? peek_position() : function_token_pos;
4180

    
4181
  // Anonymous functions were passed either the empty symbol or a null
4182
  // handle as the function name.  Remember if we were passed a non-empty
4183
  // handle to decide whether to invoke function name inference.
4184
  bool should_infer_name = function_name.is_null();
4185

    
4186
  // We want a non-null handle as the function name.
4187
  if (should_infer_name) {
4188
    function_name = isolate()->factory()->empty_string();
4189
  }
4190

    
4191
  int num_parameters = 0;
4192
  // Function declarations are function scoped in normal mode, so they are
4193
  // hoisted. In harmony block scoping mode they are block scoped, so they
4194
  // are not hoisted.
4195
  //
4196
  // One tricky case are function declarations in a local sloppy-mode eval:
4197
  // their declaration is hoisted, but they still see the local scope. E.g.,
4198
  //
4199
  // function() {
4200
  //   var x = 0
4201
  //   try { throw 1 } catch (x) { eval("function g() { return x }") }
4202
  //   return g()
4203
  // }
4204
  //
4205
  // needs to return 1. To distinguish such cases, we need to detect
4206
  // (1) whether a function stems from a sloppy eval, and
4207
  // (2) whether it actually hoists across the eval.
4208
  // Unfortunately, we do not represent sloppy eval scopes, so we do not have
4209
  // either information available directly, especially not when lazily compiling
4210
  // a function like 'g'. We hence rely on the following invariants:
4211
  // - (1) is the case iff the innermost scope of the deserialized scope chain
4212
  //   under which we compile is _not_ a declaration scope. This holds because
4213
  //   in all normal cases, function declarations are fully hoisted to a
4214
  //   declaration scope and compiled relative to that.
4215
  // - (2) is the case iff the current declaration scope is still the original
4216
  //   one relative to the deserialized scope chain. Otherwise we must be
4217
  //   compiling a function in an inner declaration scope in the eval, e.g. a
4218
  //   nested function, and hoisting works normally relative to that.
4219
  Scope* declaration_scope = top_scope_->DeclarationScope();
4220
  Scope* original_declaration_scope = original_scope_->DeclarationScope();
4221
  Scope* scope =
4222
      function_type == FunctionLiteral::DECLARATION && !is_extended_mode() &&
4223
      (original_scope_ == original_declaration_scope ||
4224
       declaration_scope != original_declaration_scope)
4225
          ? NewScope(declaration_scope, FUNCTION_SCOPE)
4226
          : NewScope(top_scope_, FUNCTION_SCOPE);
4227
  ZoneList<Statement*>* body = NULL;
4228
  int materialized_literal_count = -1;
4229
  int expected_property_count = -1;
4230
  int handler_count = 0;
4231
  FunctionLiteral::ParameterFlag duplicate_parameters =
4232
      FunctionLiteral::kNoDuplicateParameters;
4233
  FunctionLiteral::IsParenthesizedFlag parenthesized = parenthesized_function_
4234
      ? FunctionLiteral::kIsParenthesized
4235
      : FunctionLiteral::kNotParenthesized;
4236
  FunctionLiteral::IsGeneratorFlag generator = is_generator
4237
      ? FunctionLiteral::kIsGenerator
4238
      : FunctionLiteral::kNotGenerator;
4239
  AstProperties ast_properties;
4240
  BailoutReason dont_optimize_reason = kNoReason;
4241
  // Parse function body.
4242
  { FunctionState function_state(this, scope, isolate());
4243
    top_scope_->SetScopeName(function_name);
4244

    
4245
    if (is_generator) {
4246
      // For generators, allocating variables in contexts is currently a win
4247
      // because it minimizes the work needed to suspend and resume an
4248
      // activation.
4249
      top_scope_->ForceContextAllocation();
4250

    
4251
      // Calling a generator returns a generator object.  That object is stored
4252
      // in a temporary variable, a definition that is used by "yield"
4253
      // expressions.  Presence of a variable for the generator object in the
4254
      // FunctionState indicates that this function is a generator.
4255
      Handle<String> tempname = isolate()->factory()->InternalizeOneByteString(
4256
          STATIC_ASCII_VECTOR(".generator_object"));
4257
      Variable* temp = top_scope_->DeclarationScope()->NewTemporary(tempname);
4258
      function_state.set_generator_object_variable(temp);
4259
    }
4260

    
4261
    //  FormalParameterList ::
4262
    //    '(' (Identifier)*[','] ')'
4263
    Expect(Token::LPAREN, CHECK_OK);
4264
    scope->set_start_position(scanner().location().beg_pos);
4265
    Scanner::Location name_loc = Scanner::Location::invalid();
4266
    Scanner::Location dupe_loc = Scanner::Location::invalid();
4267
    Scanner::Location reserved_loc = Scanner::Location::invalid();
4268

    
4269
    bool done = (peek() == Token::RPAREN);
4270
    while (!done) {
4271
      bool is_strict_reserved = false;
4272
      Handle<String> param_name =
4273
          ParseIdentifierOrStrictReservedWord(&is_strict_reserved,
4274
                                              CHECK_OK);
4275

    
4276
      // Store locations for possible future error reports.
4277
      if (!name_loc.IsValid() && IsEvalOrArguments(param_name)) {
4278
        name_loc = scanner().location();
4279
      }
4280
      if (!dupe_loc.IsValid() && top_scope_->IsDeclared(param_name)) {
4281
        duplicate_parameters = FunctionLiteral::kHasDuplicateParameters;
4282
        dupe_loc = scanner().location();
4283
      }
4284
      if (!reserved_loc.IsValid() && is_strict_reserved) {
4285
        reserved_loc = scanner().location();
4286
      }
4287

    
4288
      top_scope_->DeclareParameter(param_name, VAR);
4289
      num_parameters++;
4290
      if (num_parameters > Code::kMaxArguments) {
4291
        ReportMessageAt(scanner().location(), "too_many_parameters",
4292
                        Vector<const char*>::empty());
4293
        *ok = false;
4294
        return NULL;
4295
      }
4296
      done = (peek() == Token::RPAREN);
4297
      if (!done) Expect(Token::COMMA, CHECK_OK);
4298
    }
4299
    Expect(Token::RPAREN, CHECK_OK);
4300

    
4301
    Expect(Token::LBRACE, CHECK_OK);
4302

    
4303
    // If we have a named function expression, we add a local variable
4304
    // declaration to the body of the function with the name of the
4305
    // function and let it refer to the function itself (closure).
4306
    // NOTE: We create a proxy and resolve it here so that in the
4307
    // future we can change the AST to only refer to VariableProxies
4308
    // instead of Variables and Proxis as is the case now.
4309
    Variable* fvar = NULL;
4310
    Token::Value fvar_init_op = Token::INIT_CONST;
4311
    if (function_type == FunctionLiteral::NAMED_EXPRESSION) {
4312
      if (is_extended_mode()) fvar_init_op = Token::INIT_CONST_HARMONY;
4313
      VariableMode fvar_mode = is_extended_mode() ? CONST_HARMONY : CONST;
4314
      fvar = new(zone()) Variable(top_scope_,
4315
         function_name, fvar_mode, true /* is valid LHS */,
4316
         Variable::NORMAL, kCreatedInitialized, Interface::NewConst());
4317
      VariableProxy* proxy = factory()->NewVariableProxy(fvar);
4318
      VariableDeclaration* fvar_declaration = factory()->NewVariableDeclaration(
4319
          proxy, fvar_mode, top_scope_, RelocInfo::kNoPosition);
4320
      top_scope_->DeclareFunctionVar(fvar_declaration);
4321
    }
4322

    
4323
    // Determine whether the function will be lazily compiled.
4324
    // The heuristics are:
4325
    // - It must not have been prohibited by the caller to Parse (some callers
4326
    //   need a full AST).
4327
    // - The outer scope must allow lazy compilation of inner functions.
4328
    // - The function mustn't be a function expression with an open parenthesis
4329
    //   before; we consider that a hint that the function will be called
4330
    //   immediately, and it would be a waste of time to make it lazily
4331
    //   compiled.
4332
    // These are all things we can know at this point, without looking at the
4333
    // function itself.
4334
    bool is_lazily_compiled = (mode() == PARSE_LAZILY &&
4335
                               top_scope_->AllowsLazyCompilation() &&
4336
                               !parenthesized_function_);
4337
    parenthesized_function_ = false;  // The bit was set for this function only.
4338

    
4339
    if (is_lazily_compiled) {
4340
      int function_block_pos = position();
4341
      FunctionEntry entry;
4342
      if (pre_parse_data_ != NULL) {
4343
        // If we have pre_parse_data_, we use it to skip parsing the function
4344
        // body.  The preparser data contains the information we need to
4345
        // construct the lazy function.
4346
        entry = pre_parse_data()->GetFunctionEntry(function_block_pos);
4347
        if (entry.is_valid()) {
4348
          if (entry.end_pos() <= function_block_pos) {
4349
            // End position greater than end of stream is safe, and hard
4350
            // to check.
4351
            ReportInvalidPreparseData(function_name, CHECK_OK);
4352
          }
4353
          scanner().SeekForward(entry.end_pos() - 1);
4354

    
4355
          scope->set_end_position(entry.end_pos());
4356
          Expect(Token::RBRACE, CHECK_OK);
4357
          isolate()->counters()->total_preparse_skipped()->Increment(
4358
              scope->end_position() - function_block_pos);
4359
          materialized_literal_count = entry.literal_count();
4360
          expected_property_count = entry.property_count();
4361
          top_scope_->SetLanguageMode(entry.language_mode());
4362
        } else {
4363
          is_lazily_compiled = false;
4364
        }
4365
      } else {
4366
        // With no preparser data, we partially parse the function, without
4367
        // building an AST. This gathers the data needed to build a lazy
4368
        // function.
4369
        SingletonLogger logger;
4370
        PreParser::PreParseResult result = LazyParseFunctionLiteral(&logger);
4371
        if (result == PreParser::kPreParseStackOverflow) {
4372
          // Propagate stack overflow.
4373
          set_stack_overflow();
4374
          *ok = false;
4375
          return NULL;
4376
        }
4377
        if (logger.has_error()) {
4378
          const char* arg = logger.argument_opt();
4379
          Vector<const char*> args;
4380
          if (arg != NULL) {
4381
            args = Vector<const char*>(&arg, 1);
4382
          }
4383
          ReportMessageAt(Scanner::Location(logger.start(), logger.end()),
4384
                          logger.message(), args);
4385
          *ok = false;
4386
          return NULL;
4387
        }
4388
        scope->set_end_position(logger.end());
4389
        Expect(Token::RBRACE, CHECK_OK);
4390
        isolate()->counters()->total_preparse_skipped()->Increment(
4391
            scope->end_position() - function_block_pos);
4392
        materialized_literal_count = logger.literals();
4393
        expected_property_count = logger.properties();
4394
        top_scope_->SetLanguageMode(logger.language_mode());
4395
      }
4396
    }
4397

    
4398
    if (!is_lazily_compiled) {
4399
      ParsingModeScope parsing_mode(this, PARSE_EAGERLY);
4400
      body = new(zone()) ZoneList<Statement*>(8, zone());
4401
      if (fvar != NULL) {
4402
        VariableProxy* fproxy = top_scope_->NewUnresolved(
4403
            factory(), function_name, Interface::NewConst());
4404
        fproxy->BindTo(fvar);
4405
        body->Add(factory()->NewExpressionStatement(
4406
            factory()->NewAssignment(fvar_init_op,
4407
                                     fproxy,
4408
                                     factory()->NewThisFunction(pos),
4409
                                     RelocInfo::kNoPosition),
4410
            RelocInfo::kNoPosition), zone());
4411
      }
4412

    
4413
      // For generators, allocate and yield an iterator on function entry.
4414
      if (is_generator) {
4415
        ZoneList<Expression*>* arguments =
4416
            new(zone()) ZoneList<Expression*>(0, zone());
4417
        CallRuntime* allocation = factory()->NewCallRuntime(
4418
            isolate()->factory()->empty_string(),
4419
            Runtime::FunctionForId(Runtime::kCreateJSGeneratorObject),
4420
            arguments, pos);
4421
        VariableProxy* init_proxy = factory()->NewVariableProxy(
4422
            current_function_state_->generator_object_variable());
4423
        Assignment* assignment = factory()->NewAssignment(
4424
            Token::INIT_VAR, init_proxy, allocation, RelocInfo::kNoPosition);
4425
        VariableProxy* get_proxy = factory()->NewVariableProxy(
4426
            current_function_state_->generator_object_variable());
4427
        Yield* yield = factory()->NewYield(
4428
            get_proxy, assignment, Yield::INITIAL, RelocInfo::kNoPosition);
4429
        body->Add(factory()->NewExpressionStatement(
4430
            yield, RelocInfo::kNoPosition), zone());
4431
      }
4432

    
4433
      ParseSourceElements(body, Token::RBRACE, false, false, CHECK_OK);
4434

    
4435
      if (is_generator) {
4436
        VariableProxy* get_proxy = factory()->NewVariableProxy(
4437
            current_function_state_->generator_object_variable());
4438
        Expression *undefined = factory()->NewLiteral(
4439
            isolate()->factory()->undefined_value(), RelocInfo::kNoPosition);
4440
        Yield* yield = factory()->NewYield(
4441
            get_proxy, undefined, Yield::FINAL, RelocInfo::kNoPosition);
4442
        body->Add(factory()->NewExpressionStatement(
4443
            yield, RelocInfo::kNoPosition), zone());
4444
      }
4445

    
4446
      materialized_literal_count = function_state.materialized_literal_count();
4447
      expected_property_count = function_state.expected_property_count();
4448
      handler_count = function_state.handler_count();
4449

    
4450
      Expect(Token::RBRACE, CHECK_OK);
4451
      scope->set_end_position(scanner().location().end_pos);
4452
    }
4453

    
4454
    // Validate strict mode.
4455
    if (!top_scope_->is_classic_mode()) {
4456
      if (IsEvalOrArguments(function_name)) {
4457
        int start_pos = scope->start_position();
4458
        int position = function_token_pos != RelocInfo::kNoPosition
4459
            ? function_token_pos : (start_pos > 0 ? start_pos - 1 : start_pos);
4460
        Scanner::Location location = Scanner::Location(position, start_pos);
4461
        ReportMessageAt(location,
4462
                        "strict_function_name", Vector<const char*>::empty());
4463
        *ok = false;
4464
        return NULL;
4465
      }
4466
      if (name_loc.IsValid()) {
4467
        ReportMessageAt(name_loc, "strict_param_name",
4468
                        Vector<const char*>::empty());
4469
        *ok = false;
4470
        return NULL;
4471
      }
4472
      if (dupe_loc.IsValid()) {
4473
        ReportMessageAt(dupe_loc, "strict_param_dupe",
4474
                        Vector<const char*>::empty());
4475
        *ok = false;
4476
        return NULL;
4477
      }
4478
      if (name_is_strict_reserved) {
4479
        int start_pos = scope->start_position();
4480
        int position = function_token_pos != RelocInfo::kNoPosition
4481
            ? function_token_pos : (start_pos > 0 ? start_pos - 1 : start_pos);
4482
        Scanner::Location location = Scanner::Location(position, start_pos);
4483
        ReportMessageAt(location, "strict_reserved_word",
4484
                        Vector<const char*>::empty());
4485
        *ok = false;
4486
        return NULL;
4487
      }
4488
      if (reserved_loc.IsValid()) {
4489
        ReportMessageAt(reserved_loc, "strict_reserved_word",
4490
                        Vector<const char*>::empty());
4491
        *ok = false;
4492
        return NULL;
4493
      }
4494
      CheckOctalLiteral(scope->start_position(),
4495
                        scope->end_position(),
4496
                        CHECK_OK);
4497
    }
4498
    ast_properties = *factory()->visitor()->ast_properties();
4499
    dont_optimize_reason = factory()->visitor()->dont_optimize_reason();
4500
  }
4501

    
4502
  if (is_extended_mode()) {
4503
    CheckConflictingVarDeclarations(scope, CHECK_OK);
4504
  }
4505

    
4506
  FunctionLiteral* function_literal =
4507
      factory()->NewFunctionLiteral(function_name,
4508
                                    scope,
4509
                                    body,
4510
                                    materialized_literal_count,
4511
                                    expected_property_count,
4512
                                    handler_count,
4513
                                    num_parameters,
4514
                                    duplicate_parameters,
4515
                                    function_type,
4516
                                    FunctionLiteral::kIsFunction,
4517
                                    parenthesized,
4518
                                    generator,
4519
                                    pos);
4520
  function_literal->set_function_token_position(function_token_pos);
4521
  function_literal->set_ast_properties(&ast_properties);
4522
  function_literal->set_dont_optimize_reason(dont_optimize_reason);
4523

    
4524
  if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal);
4525
  return function_literal;
4526
}
4527

    
4528

    
4529
PreParser::PreParseResult Parser::LazyParseFunctionLiteral(
4530
    SingletonLogger* logger) {
4531
  HistogramTimerScope preparse_scope(isolate()->counters()->pre_parse());
4532
  ASSERT_EQ(Token::LBRACE, scanner().current_token());
4533

    
4534
  if (reusable_preparser_ == NULL) {
4535
    intptr_t stack_limit = isolate()->stack_guard()->real_climit();
4536
    reusable_preparser_ = new PreParser(&scanner_, NULL, stack_limit);
4537
    reusable_preparser_->set_allow_harmony_scoping(allow_harmony_scoping());
4538
    reusable_preparser_->set_allow_modules(allow_modules());
4539
    reusable_preparser_->set_allow_natives_syntax(allow_natives_syntax());
4540
    reusable_preparser_->set_allow_lazy(true);
4541
    reusable_preparser_->set_allow_generators(allow_generators());
4542
    reusable_preparser_->set_allow_for_of(allow_for_of());
4543
    reusable_preparser_->set_allow_harmony_numeric_literals(
4544
        allow_harmony_numeric_literals());
4545
  }
4546
  PreParser::PreParseResult result =
4547
      reusable_preparser_->PreParseLazyFunction(top_scope_->language_mode(),
4548
                                                is_generator(),
4549
                                                logger);
4550
  return result;
4551
}
4552

    
4553

    
4554
Expression* Parser::ParseV8Intrinsic(bool* ok) {
4555
  // CallRuntime ::
4556
  //   '%' Identifier Arguments
4557

    
4558
  int pos = peek_position();
4559
  Expect(Token::MOD, CHECK_OK);
4560
  Handle<String> name = ParseIdentifier(CHECK_OK);
4561
  ZoneList<Expression*>* args = ParseArguments(CHECK_OK);
4562

    
4563
  if (extension_ != NULL) {
4564
    // The extension structures are only accessible while parsing the
4565
    // very first time not when reparsing because of lazy compilation.
4566
    top_scope_->DeclarationScope()->ForceEagerCompilation();
4567
  }
4568

    
4569
  const Runtime::Function* function = Runtime::FunctionForName(name);
4570

    
4571
  // Check for built-in IS_VAR macro.
4572
  if (function != NULL &&
4573
      function->intrinsic_type == Runtime::RUNTIME &&
4574
      function->function_id == Runtime::kIS_VAR) {
4575
    // %IS_VAR(x) evaluates to x if x is a variable,
4576
    // leads to a parse error otherwise.  Could be implemented as an
4577
    // inline function %_IS_VAR(x) to eliminate this special case.
4578
    if (args->length() == 1 && args->at(0)->AsVariableProxy() != NULL) {
4579
      return args->at(0);
4580
    } else {
4581
      ReportMessage("not_isvar", Vector<const char*>::empty());
4582
      *ok = false;
4583
      return NULL;
4584
    }
4585
  }
4586

    
4587
  // Check that the expected number of arguments are being passed.
4588
  if (function != NULL &&
4589
      function->nargs != -1 &&
4590
      function->nargs != args->length()) {
4591
    ReportMessage("illegal_access", Vector<const char*>::empty());
4592
    *ok = false;
4593
    return NULL;
4594
  }
4595

    
4596
  // Check that the function is defined if it's an inline runtime call.
4597
  if (function == NULL && name->Get(0) == '_') {
4598
    ReportMessage("not_defined", Vector<Handle<String> >(&name, 1));
4599
    *ok = false;
4600
    return NULL;
4601
  }
4602

    
4603
  // We have a valid intrinsics call or a call to a builtin.
4604
  return factory()->NewCallRuntime(name, function, args, pos);
4605
}
4606

    
4607

    
4608
bool ParserBase::peek_any_identifier() {
4609
  Token::Value next = peek();
4610
  return next == Token::IDENTIFIER ||
4611
         next == Token::FUTURE_RESERVED_WORD ||
4612
         next == Token::FUTURE_STRICT_RESERVED_WORD ||
4613
         next == Token::YIELD;
4614
}
4615

    
4616

    
4617
bool ParserBase::CheckContextualKeyword(Vector<const char> keyword) {
4618
  if (peek() == Token::IDENTIFIER &&
4619
      scanner()->is_next_contextual_keyword(keyword)) {
4620
    Consume(Token::IDENTIFIER);
4621
    return true;
4622
  }
4623
  return false;
4624
}
4625

    
4626

    
4627
void ParserBase::ExpectSemicolon(bool* ok) {
4628
  // Check for automatic semicolon insertion according to
4629
  // the rules given in ECMA-262, section 7.9, page 21.
4630
  Token::Value tok = peek();
4631
  if (tok == Token::SEMICOLON) {
4632
    Next();
4633
    return;
4634
  }
4635
  if (scanner()->HasAnyLineTerminatorBeforeNext() ||
4636
      tok == Token::RBRACE ||
4637
      tok == Token::EOS) {
4638
    return;
4639
  }
4640
  Expect(Token::SEMICOLON, ok);
4641
}
4642

    
4643

    
4644
void ParserBase::ExpectContextualKeyword(Vector<const char> keyword, bool* ok) {
4645
  Expect(Token::IDENTIFIER, ok);
4646
  if (!*ok) return;
4647
  if (!scanner()->is_literal_contextual_keyword(keyword)) {
4648
    ReportUnexpectedToken(scanner()->current_token());
4649
    *ok = false;
4650
  }
4651
}
4652

    
4653

    
4654
Literal* Parser::GetLiteralUndefined(int position) {
4655
  return factory()->NewLiteral(
4656
      isolate()->factory()->undefined_value(), position);
4657
}
4658

    
4659

    
4660
Literal* Parser::GetLiteralTheHole(int position) {
4661
  return factory()->NewLiteral(
4662
      isolate()->factory()->the_hole_value(), RelocInfo::kNoPosition);
4663
}
4664

    
4665

    
4666
// Parses an identifier that is valid for the current scope, in particular it
4667
// fails on strict mode future reserved keywords in a strict scope.
4668
Handle<String> Parser::ParseIdentifier(bool* ok) {
4669
  Token::Value next = Next();
4670
  if (next == Token::IDENTIFIER ||
4671
      (top_scope_->is_classic_mode() &&
4672
       (next == Token::FUTURE_STRICT_RESERVED_WORD ||
4673
        (next == Token::YIELD && !is_generator())))) {
4674
    return GetSymbol();
4675
  } else {
4676
    ReportUnexpectedToken(next);
4677
    *ok = false;
4678
    return Handle<String>();
4679
  }
4680
}
4681

    
4682

    
4683
// Parses and identifier or a strict mode future reserved word, and indicate
4684
// whether it is strict mode future reserved.
4685
Handle<String> Parser::ParseIdentifierOrStrictReservedWord(
4686
    bool* is_strict_reserved, bool* ok) {
4687
  Token::Value next = Next();
4688
  if (next == Token::IDENTIFIER) {
4689
    *is_strict_reserved = false;
4690
  } else if (next == Token::FUTURE_STRICT_RESERVED_WORD ||
4691
             (next == Token::YIELD && !is_generator())) {
4692
    *is_strict_reserved = true;
4693
  } else {
4694
    ReportUnexpectedToken(next);
4695
    *ok = false;
4696
    return Handle<String>();
4697
  }
4698
  return GetSymbol();
4699
}
4700

    
4701

    
4702
Handle<String> Parser::ParseIdentifierName(bool* ok) {
4703
  Token::Value next = Next();
4704
  if (next != Token::IDENTIFIER &&
4705
      next != Token::FUTURE_RESERVED_WORD &&
4706
      next != Token::FUTURE_STRICT_RESERVED_WORD &&
4707
      !Token::IsKeyword(next)) {
4708
    ReportUnexpectedToken(next);
4709
    *ok = false;
4710
    return Handle<String>();
4711
  }
4712
  return GetSymbol();
4713
}
4714

    
4715

    
4716
void Parser::MarkAsLValue(Expression* expression) {
4717
  VariableProxy* proxy = expression != NULL
4718
      ? expression->AsVariableProxy()
4719
      : NULL;
4720

    
4721
  if (proxy != NULL) proxy->MarkAsLValue();
4722
}
4723

    
4724

    
4725
// Checks LHS expression for assignment and prefix/postfix increment/decrement
4726
// in strict mode.
4727
void Parser::CheckStrictModeLValue(Expression* expression,
4728
                                   const char* error,
4729
                                   bool* ok) {
4730
  ASSERT(!top_scope_->is_classic_mode());
4731
  VariableProxy* lhs = expression != NULL
4732
      ? expression->AsVariableProxy()
4733
      : NULL;
4734

    
4735
  if (lhs != NULL && !lhs->is_this() && IsEvalOrArguments(lhs->name())) {
4736
    ReportMessage(error, Vector<const char*>::empty());
4737
    *ok = false;
4738
  }
4739
}
4740

    
4741

    
4742
// Checks whether an octal literal was last seen between beg_pos and end_pos.
4743
// If so, reports an error. Only called for strict mode.
4744
void ParserBase::CheckOctalLiteral(int beg_pos, int end_pos, bool* ok) {
4745
  Scanner::Location octal = scanner()->octal_position();
4746
  if (octal.IsValid() && beg_pos <= octal.beg_pos && octal.end_pos <= end_pos) {
4747
    ReportMessageAt(octal, "strict_octal_literal");
4748
    scanner()->clear_octal_position();
4749
    *ok = false;
4750
  }
4751
}
4752

    
4753

    
4754
void Parser::CheckConflictingVarDeclarations(Scope* scope, bool* ok) {
4755
  Declaration* decl = scope->CheckConflictingVarDeclarations();
4756
  if (decl != NULL) {
4757
    // In harmony mode we treat conflicting variable bindinds as early
4758
    // errors. See ES5 16 for a definition of early errors.
4759
    Handle<String> name = decl->proxy()->name();
4760
    SmartArrayPointer<char> c_string = name->ToCString(DISALLOW_NULLS);
4761
    const char* elms[2] = { "Variable", *c_string };
4762
    Vector<const char*> args(elms, 2);
4763
    int position = decl->proxy()->position();
4764
    Scanner::Location location = position == RelocInfo::kNoPosition
4765
        ? Scanner::Location::invalid()
4766
        : Scanner::Location(position, position + 1);
4767
    ReportMessageAt(location, "redeclaration", args);
4768
    *ok = false;
4769
  }
4770
}
4771

    
4772

    
4773
// This function reads an identifier name and determines whether or not it
4774
// is 'get' or 'set'.
4775
Handle<String> Parser::ParseIdentifierNameOrGetOrSet(bool* is_get,
4776
                                                     bool* is_set,
4777
                                                     bool* ok) {
4778
  Handle<String> result = ParseIdentifierName(ok);
4779
  if (!*ok) return Handle<String>();
4780
  if (scanner().is_literal_ascii() && scanner().literal_length() == 3) {
4781
    const char* token = scanner().literal_ascii_string().start();
4782
    *is_get = strncmp(token, "get", 3) == 0;
4783
    *is_set = !*is_get && strncmp(token, "set", 3) == 0;
4784
  }
4785
  return result;
4786
}
4787

    
4788

    
4789
// ----------------------------------------------------------------------------
4790
// Parser support
4791

    
4792

    
4793
bool Parser::TargetStackContainsLabel(Handle<String> label) {
4794
  for (Target* t = target_stack_; t != NULL; t = t->previous()) {
4795
    BreakableStatement* stat = t->node()->AsBreakableStatement();
4796
    if (stat != NULL && ContainsLabel(stat->labels(), label))
4797
      return true;
4798
  }
4799
  return false;
4800
}
4801

    
4802

    
4803
BreakableStatement* Parser::LookupBreakTarget(Handle<String> label, bool* ok) {
4804
  bool anonymous = label.is_null();
4805
  for (Target* t = target_stack_; t != NULL; t = t->previous()) {
4806
    BreakableStatement* stat = t->node()->AsBreakableStatement();
4807
    if (stat == NULL) continue;
4808
    if ((anonymous && stat->is_target_for_anonymous()) ||
4809
        (!anonymous && ContainsLabel(stat->labels(), label))) {
4810
      RegisterTargetUse(stat->break_target(), t->previous());
4811
      return stat;
4812
    }
4813
  }
4814
  return NULL;
4815
}
4816

    
4817

    
4818
IterationStatement* Parser::LookupContinueTarget(Handle<String> label,
4819
                                                 bool* ok) {
4820
  bool anonymous = label.is_null();
4821
  for (Target* t = target_stack_; t != NULL; t = t->previous()) {
4822
    IterationStatement* stat = t->node()->AsIterationStatement();
4823
    if (stat == NULL) continue;
4824

    
4825
    ASSERT(stat->is_target_for_anonymous());
4826
    if (anonymous || ContainsLabel(stat->labels(), label)) {
4827
      RegisterTargetUse(stat->continue_target(), t->previous());
4828
      return stat;
4829
    }
4830
  }
4831
  return NULL;
4832
}
4833

    
4834

    
4835
void Parser::RegisterTargetUse(Label* target, Target* stop) {
4836
  // Register that a break target found at the given stop in the
4837
  // target stack has been used from the top of the target stack. Add
4838
  // the break target to any TargetCollectors passed on the stack.
4839
  for (Target* t = target_stack_; t != stop; t = t->previous()) {
4840
    TargetCollector* collector = t->node()->AsTargetCollector();
4841
    if (collector != NULL) collector->AddTarget(target, zone());
4842
  }
4843
}
4844

    
4845

    
4846
Expression* Parser::NewThrowReferenceError(Handle<String> message) {
4847
  return NewThrowError(isolate()->factory()->MakeReferenceError_string(),
4848
                       message, HandleVector<Object>(NULL, 0));
4849
}
4850

    
4851

    
4852
Expression* Parser::NewThrowSyntaxError(Handle<String> message,
4853
                                        Handle<Object> first) {
4854
  int argc = first.is_null() ? 0 : 1;
4855
  Vector< Handle<Object> > arguments = HandleVector<Object>(&first, argc);
4856
  return NewThrowError(
4857
      isolate()->factory()->MakeSyntaxError_string(), message, arguments);
4858
}
4859

    
4860

    
4861
Expression* Parser::NewThrowTypeError(Handle<String> message,
4862
                                      Handle<Object> first,
4863
                                      Handle<Object> second) {
4864
  ASSERT(!first.is_null() && !second.is_null());
4865
  Handle<Object> elements[] = { first, second };
4866
  Vector< Handle<Object> > arguments =
4867
      HandleVector<Object>(elements, ARRAY_SIZE(elements));
4868
  return NewThrowError(
4869
      isolate()->factory()->MakeTypeError_string(), message, arguments);
4870
}
4871

    
4872

    
4873
Expression* Parser::NewThrowError(Handle<String> constructor,
4874
                                  Handle<String> message,
4875
                                  Vector< Handle<Object> > arguments) {
4876
  int argc = arguments.length();
4877
  Handle<FixedArray> elements = isolate()->factory()->NewFixedArray(argc,
4878
                                                                    TENURED);
4879
  for (int i = 0; i < argc; i++) {
4880
    Handle<Object> element = arguments[i];
4881
    if (!element.is_null()) {
4882
      elements->set(i, *element);
4883
    }
4884
  }
4885
  Handle<JSArray> array = isolate()->factory()->NewJSArrayWithElements(
4886
      elements, FAST_ELEMENTS, TENURED);
4887

    
4888
  int pos = position();
4889
  ZoneList<Expression*>* args = new(zone()) ZoneList<Expression*>(2, zone());
4890
  args->Add(factory()->NewLiteral(message, pos), zone());
4891
  args->Add(factory()->NewLiteral(array, pos), zone());
4892
  CallRuntime* call_constructor =
4893
      factory()->NewCallRuntime(constructor, NULL, args, pos);
4894
  return factory()->NewThrow(call_constructor, pos);
4895
}
4896

    
4897

    
4898
// ----------------------------------------------------------------------------
4899
// Regular expressions
4900

    
4901

    
4902
RegExpParser::RegExpParser(FlatStringReader* in,
4903
                           Handle<String>* error,
4904
                           bool multiline,
4905
                           Zone* zone)
4906
    : isolate_(zone->isolate()),
4907
      zone_(zone),
4908
      error_(error),
4909
      captures_(NULL),
4910
      in_(in),
4911
      current_(kEndMarker),
4912
      next_pos_(0),
4913
      capture_count_(0),
4914
      has_more_(true),
4915
      multiline_(multiline),
4916
      simple_(false),
4917
      contains_anchor_(false),
4918
      is_scanned_for_captures_(false),
4919
      failed_(false) {
4920
  Advance();
4921
}
4922

    
4923

    
4924
uc32 RegExpParser::Next() {
4925
  if (has_next()) {
4926
    return in()->Get(next_pos_);
4927
  } else {
4928
    return kEndMarker;
4929
  }
4930
}
4931

    
4932

    
4933
void RegExpParser::Advance() {
4934
  if (next_pos_ < in()->length()) {
4935
    StackLimitCheck check(isolate());
4936
    if (check.HasOverflowed()) {
4937
      ReportError(CStrVector(Isolate::kStackOverflowMessage));
4938
    } else if (zone()->excess_allocation()) {
4939
      ReportError(CStrVector("Regular expression too large"));
4940
    } else {
4941
      current_ = in()->Get(next_pos_);
4942
      next_pos_++;
4943
    }
4944
  } else {
4945
    current_ = kEndMarker;
4946
    has_more_ = false;
4947
  }
4948
}
4949

    
4950

    
4951
void RegExpParser::Reset(int pos) {
4952
  next_pos_ = pos;
4953
  has_more_ = (pos < in()->length());
4954
  Advance();
4955
}
4956

    
4957

    
4958
void RegExpParser::Advance(int dist) {
4959
  next_pos_ += dist - 1;
4960
  Advance();
4961
}
4962

    
4963

    
4964
bool RegExpParser::simple() {
4965
  return simple_;
4966
}
4967

    
4968

    
4969
RegExpTree* RegExpParser::ReportError(Vector<const char> message) {
4970
  failed_ = true;
4971
  *error_ = isolate()->factory()->NewStringFromAscii(message, NOT_TENURED);
4972
  // Zip to the end to make sure the no more input is read.
4973
  current_ = kEndMarker;
4974
  next_pos_ = in()->length();
4975
  return NULL;
4976
}
4977

    
4978

    
4979
// Pattern ::
4980
//   Disjunction
4981
RegExpTree* RegExpParser::ParsePattern() {
4982
  RegExpTree* result = ParseDisjunction(CHECK_FAILED);
4983
  ASSERT(!has_more());
4984
  // If the result of parsing is a literal string atom, and it has the
4985
  // same length as the input, then the atom is identical to the input.
4986
  if (result->IsAtom() && result->AsAtom()->length() == in()->length()) {
4987
    simple_ = true;
4988
  }
4989
  return result;
4990
}
4991

    
4992

    
4993
// Disjunction ::
4994
//   Alternative
4995
//   Alternative | Disjunction
4996
// Alternative ::
4997
//   [empty]
4998
//   Term Alternative
4999
// Term ::
5000
//   Assertion
5001
//   Atom
5002
//   Atom Quantifier
5003
RegExpTree* RegExpParser::ParseDisjunction() {
5004
  // Used to store current state while parsing subexpressions.
5005
  RegExpParserState initial_state(NULL, INITIAL, 0, zone());
5006
  RegExpParserState* stored_state = &initial_state;
5007
  // Cache the builder in a local variable for quick access.
5008
  RegExpBuilder* builder = initial_state.builder();
5009
  while (true) {
5010
    switch (current()) {
5011
    case kEndMarker:
5012
      if (stored_state->IsSubexpression()) {
5013
        // Inside a parenthesized group when hitting end of input.
5014
        ReportError(CStrVector("Unterminated group") CHECK_FAILED);
5015
      }
5016
      ASSERT_EQ(INITIAL, stored_state->group_type());
5017
      // Parsing completed successfully.
5018
      return builder->ToRegExp();
5019
    case ')': {
5020
      if (!stored_state->IsSubexpression()) {
5021
        ReportError(CStrVector("Unmatched ')'") CHECK_FAILED);
5022
      }
5023
      ASSERT_NE(INITIAL, stored_state->group_type());
5024

    
5025
      Advance();
5026
      // End disjunction parsing and convert builder content to new single
5027
      // regexp atom.
5028
      RegExpTree* body = builder->ToRegExp();
5029

    
5030
      int end_capture_index = captures_started();
5031

    
5032
      int capture_index = stored_state->capture_index();
5033
      SubexpressionType group_type = stored_state->group_type();
5034

    
5035
      // Restore previous state.
5036
      stored_state = stored_state->previous_state();
5037
      builder = stored_state->builder();
5038

    
5039
      // Build result of subexpression.
5040
      if (group_type == CAPTURE) {
5041
        RegExpCapture* capture = new(zone()) RegExpCapture(body, capture_index);
5042
        captures_->at(capture_index - 1) = capture;
5043
        body = capture;
5044
      } else if (group_type != GROUPING) {
5045
        ASSERT(group_type == POSITIVE_LOOKAHEAD ||
5046
               group_type == NEGATIVE_LOOKAHEAD);
5047
        bool is_positive = (group_type == POSITIVE_LOOKAHEAD);
5048
        body = new(zone()) RegExpLookahead(body,
5049
                                   is_positive,
5050
                                   end_capture_index - capture_index,
5051
                                   capture_index);
5052
      }
5053
      builder->AddAtom(body);
5054
      // For compatability with JSC and ES3, we allow quantifiers after
5055
      // lookaheads, and break in all cases.
5056
      break;
5057
    }
5058
    case '|': {
5059
      Advance();
5060
      builder->NewAlternative();
5061
      continue;
5062
    }
5063
    case '*':
5064
    case '+':
5065
    case '?':
5066
      return ReportError(CStrVector("Nothing to repeat"));
5067
    case '^': {
5068
      Advance();
5069
      if (multiline_) {
5070
        builder->AddAssertion(
5071
            new(zone()) RegExpAssertion(RegExpAssertion::START_OF_LINE));
5072
      } else {
5073
        builder->AddAssertion(
5074
            new(zone()) RegExpAssertion(RegExpAssertion::START_OF_INPUT));
5075
        set_contains_anchor();
5076
      }
5077
      continue;
5078
    }
5079
    case '$': {
5080
      Advance();
5081
      RegExpAssertion::AssertionType assertion_type =
5082
          multiline_ ? RegExpAssertion::END_OF_LINE :
5083
                       RegExpAssertion::END_OF_INPUT;
5084
      builder->AddAssertion(new(zone()) RegExpAssertion(assertion_type));
5085
      continue;
5086
    }
5087
    case '.': {
5088
      Advance();
5089
      // everything except \x0a, \x0d, \u2028 and \u2029
5090
      ZoneList<CharacterRange>* ranges =
5091
          new(zone()) ZoneList<CharacterRange>(2, zone());
5092
      CharacterRange::AddClassEscape('.', ranges, zone());
5093
      RegExpTree* atom = new(zone()) RegExpCharacterClass(ranges, false);
5094
      builder->AddAtom(atom);
5095
      break;
5096
    }
5097
    case '(': {
5098
      SubexpressionType subexpr_type = CAPTURE;
5099
      Advance();
5100
      if (current() == '?') {
5101
        switch (Next()) {
5102
          case ':':
5103
            subexpr_type = GROUPING;
5104
            break;
5105
          case '=':
5106
            subexpr_type = POSITIVE_LOOKAHEAD;
5107
            break;
5108
          case '!':
5109
            subexpr_type = NEGATIVE_LOOKAHEAD;
5110
            break;
5111
          default:
5112
            ReportError(CStrVector("Invalid group") CHECK_FAILED);
5113
            break;
5114
        }
5115
        Advance(2);
5116
      } else {
5117
        if (captures_ == NULL) {
5118
          captures_ = new(zone()) ZoneList<RegExpCapture*>(2, zone());
5119
        }
5120
        if (captures_started() >= kMaxCaptures) {
5121
          ReportError(CStrVector("Too many captures") CHECK_FAILED);
5122
        }
5123
        captures_->Add(NULL, zone());
5124
      }
5125
      // Store current state and begin new disjunction parsing.
5126
      stored_state = new(zone()) RegExpParserState(stored_state, subexpr_type,
5127
                                                   captures_started(), zone());
5128
      builder = stored_state->builder();
5129
      continue;
5130
    }
5131
    case '[': {
5132
      RegExpTree* atom = ParseCharacterClass(CHECK_FAILED);
5133
      builder->AddAtom(atom);
5134
      break;
5135
    }
5136
    // Atom ::
5137
    //   \ AtomEscape
5138
    case '\\':
5139
      switch (Next()) {
5140
      case kEndMarker:
5141
        return ReportError(CStrVector("\\ at end of pattern"));
5142
      case 'b':
5143
        Advance(2);
5144
        builder->AddAssertion(
5145
            new(zone()) RegExpAssertion(RegExpAssertion::BOUNDARY));
5146
        continue;
5147
      case 'B':
5148
        Advance(2);
5149
        builder->AddAssertion(
5150
            new(zone()) RegExpAssertion(RegExpAssertion::NON_BOUNDARY));
5151
        continue;
5152
      // AtomEscape ::
5153
      //   CharacterClassEscape
5154
      //
5155
      // CharacterClassEscape :: one of
5156
      //   d D s S w W
5157
      case 'd': case 'D': case 's': case 'S': case 'w': case 'W': {
5158
        uc32 c = Next();
5159
        Advance(2);
5160
        ZoneList<CharacterRange>* ranges =
5161
            new(zone()) ZoneList<CharacterRange>(2, zone());
5162
        CharacterRange::AddClassEscape(c, ranges, zone());
5163
        RegExpTree* atom = new(zone()) RegExpCharacterClass(ranges, false);
5164
        builder->AddAtom(atom);
5165
        break;
5166
      }
5167
      case '1': case '2': case '3': case '4': case '5': case '6':
5168
      case '7': case '8': case '9': {
5169
        int index = 0;
5170
        if (ParseBackReferenceIndex(&index)) {
5171
          RegExpCapture* capture = NULL;
5172
          if (captures_ != NULL && index <= captures_->length()) {
5173
            capture = captures_->at(index - 1);
5174
          }
5175
          if (capture == NULL) {
5176
            builder->AddEmpty();
5177
            break;
5178
          }
5179
          RegExpTree* atom = new(zone()) RegExpBackReference(capture);
5180
          builder->AddAtom(atom);
5181
          break;
5182
        }
5183
        uc32 first_digit = Next();
5184
        if (first_digit == '8' || first_digit == '9') {
5185
          // Treat as identity escape
5186
          builder->AddCharacter(first_digit);
5187
          Advance(2);
5188
          break;
5189
        }
5190
      }
5191
      // FALLTHROUGH
5192
      case '0': {
5193
        Advance();
5194
        uc32 octal = ParseOctalLiteral();
5195
        builder->AddCharacter(octal);
5196
        break;
5197
      }
5198
      // ControlEscape :: one of
5199
      //   f n r t v
5200
      case 'f':
5201
        Advance(2);
5202
        builder->AddCharacter('\f');
5203
        break;
5204
      case 'n':
5205
        Advance(2);
5206
        builder->AddCharacter('\n');
5207
        break;
5208
      case 'r':
5209
        Advance(2);
5210
        builder->AddCharacter('\r');
5211
        break;
5212
      case 't':
5213
        Advance(2);
5214
        builder->AddCharacter('\t');
5215
        break;
5216
      case 'v':
5217
        Advance(2);
5218
        builder->AddCharacter('\v');
5219
        break;
5220
      case 'c': {
5221
        Advance();
5222
        uc32 controlLetter = Next();
5223
        // Special case if it is an ASCII letter.
5224
        // Convert lower case letters to uppercase.
5225
        uc32 letter = controlLetter & ~('a' ^ 'A');
5226
        if (letter < 'A' || 'Z' < letter) {
5227
          // controlLetter is not in range 'A'-'Z' or 'a'-'z'.
5228
          // This is outside the specification. We match JSC in
5229
          // reading the backslash as a literal character instead
5230
          // of as starting an escape.
5231
          builder->AddCharacter('\\');
5232
        } else {
5233
          Advance(2);
5234
          builder->AddCharacter(controlLetter & 0x1f);
5235
        }
5236
        break;
5237
      }
5238
      case 'x': {
5239
        Advance(2);
5240
        uc32 value;
5241
        if (ParseHexEscape(2, &value)) {
5242
          builder->AddCharacter(value);
5243
        } else {
5244
          builder->AddCharacter('x');
5245
        }
5246
        break;
5247
      }
5248
      case 'u': {
5249
        Advance(2);
5250
        uc32 value;
5251
        if (ParseHexEscape(4, &value)) {
5252
          builder->AddCharacter(value);
5253
        } else {
5254
          builder->AddCharacter('u');
5255
        }
5256
        break;
5257
      }
5258
      default:
5259
        // Identity escape.
5260
        builder->AddCharacter(Next());
5261
        Advance(2);
5262
        break;
5263
      }
5264
      break;
5265
    case '{': {
5266
      int dummy;
5267
      if (ParseIntervalQuantifier(&dummy, &dummy)) {
5268
        ReportError(CStrVector("Nothing to repeat") CHECK_FAILED);
5269
      }
5270
      // fallthrough
5271
    }
5272
    default:
5273
      builder->AddCharacter(current());
5274
      Advance();
5275
      break;
5276
    }  // end switch(current())
5277

    
5278
    int min;
5279
    int max;
5280
    switch (current()) {
5281
    // QuantifierPrefix ::
5282
    //   *
5283
    //   +
5284
    //   ?
5285
    //   {
5286
    case '*':
5287
      min = 0;
5288
      max = RegExpTree::kInfinity;
5289
      Advance();
5290
      break;
5291
    case '+':
5292
      min = 1;
5293
      max = RegExpTree::kInfinity;
5294
      Advance();
5295
      break;
5296
    case '?':
5297
      min = 0;
5298
      max = 1;
5299
      Advance();
5300
      break;
5301
    case '{':
5302
      if (ParseIntervalQuantifier(&min, &max)) {
5303
        if (max < min) {
5304
          ReportError(CStrVector("numbers out of order in {} quantifier.")
5305
                      CHECK_FAILED);
5306
        }
5307
        break;
5308
      } else {
5309
        continue;
5310
      }
5311
    default:
5312
      continue;
5313
    }
5314
    RegExpQuantifier::QuantifierType quantifier_type = RegExpQuantifier::GREEDY;
5315
    if (current() == '?') {
5316
      quantifier_type = RegExpQuantifier::NON_GREEDY;
5317
      Advance();
5318
    } else if (FLAG_regexp_possessive_quantifier && current() == '+') {
5319
      // FLAG_regexp_possessive_quantifier is a debug-only flag.
5320
      quantifier_type = RegExpQuantifier::POSSESSIVE;
5321
      Advance();
5322
    }
5323
    builder->AddQuantifierToAtom(min, max, quantifier_type);
5324
  }
5325
}
5326

    
5327

    
5328
#ifdef DEBUG
5329
// Currently only used in an ASSERT.
5330
static bool IsSpecialClassEscape(uc32 c) {
5331
  switch (c) {
5332
    case 'd': case 'D':
5333
    case 's': case 'S':
5334
    case 'w': case 'W':
5335
      return true;
5336
    default:
5337
      return false;
5338
  }
5339
}
5340
#endif
5341

    
5342

    
5343
// In order to know whether an escape is a backreference or not we have to scan
5344
// the entire regexp and find the number of capturing parentheses.  However we
5345
// don't want to scan the regexp twice unless it is necessary.  This mini-parser
5346
// is called when needed.  It can see the difference between capturing and
5347
// noncapturing parentheses and can skip character classes and backslash-escaped
5348
// characters.
5349
void RegExpParser::ScanForCaptures() {
5350
  // Start with captures started previous to current position
5351
  int capture_count = captures_started();
5352
  // Add count of captures after this position.
5353
  int n;
5354
  while ((n = current()) != kEndMarker) {
5355
    Advance();
5356
    switch (n) {
5357
      case '\\':
5358
        Advance();
5359
        break;
5360
      case '[': {
5361
        int c;
5362
        while ((c = current()) != kEndMarker) {
5363
          Advance();
5364
          if (c == '\\') {
5365
            Advance();
5366
          } else {
5367
            if (c == ']') break;
5368
          }
5369
        }
5370
        break;
5371
      }
5372
      case '(':
5373
        if (current() != '?') capture_count++;
5374
        break;
5375
    }
5376
  }
5377
  capture_count_ = capture_count;
5378
  is_scanned_for_captures_ = true;
5379
}
5380

    
5381

    
5382
bool RegExpParser::ParseBackReferenceIndex(int* index_out) {
5383
  ASSERT_EQ('\\', current());
5384
  ASSERT('1' <= Next() && Next() <= '9');
5385
  // Try to parse a decimal literal that is no greater than the total number
5386
  // of left capturing parentheses in the input.
5387
  int start = position();
5388
  int value = Next() - '0';
5389
  Advance(2);
5390
  while (true) {
5391
    uc32 c = current();
5392
    if (IsDecimalDigit(c)) {
5393
      value = 10 * value + (c - '0');
5394
      if (value > kMaxCaptures) {
5395
        Reset(start);
5396
        return false;
5397
      }
5398
      Advance();
5399
    } else {
5400
      break;
5401
    }
5402
  }
5403
  if (value > captures_started()) {
5404
    if (!is_scanned_for_captures_) {
5405
      int saved_position = position();
5406
      ScanForCaptures();
5407
      Reset(saved_position);
5408
    }
5409
    if (value > capture_count_) {
5410
      Reset(start);
5411
      return false;
5412
    }
5413
  }
5414
  *index_out = value;
5415
  return true;
5416
}
5417

    
5418

    
5419
// QuantifierPrefix ::
5420
//   { DecimalDigits }
5421
//   { DecimalDigits , }
5422
//   { DecimalDigits , DecimalDigits }
5423
//
5424
// Returns true if parsing succeeds, and set the min_out and max_out
5425
// values. Values are truncated to RegExpTree::kInfinity if they overflow.
5426
bool RegExpParser::ParseIntervalQuantifier(int* min_out, int* max_out) {
5427
  ASSERT_EQ(current(), '{');
5428
  int start = position();
5429
  Advance();
5430
  int min = 0;
5431
  if (!IsDecimalDigit(current())) {
5432
    Reset(start);
5433
    return false;
5434
  }
5435
  while (IsDecimalDigit(current())) {
5436
    int next = current() - '0';
5437
    if (min > (RegExpTree::kInfinity - next) / 10) {
5438
      // Overflow. Skip past remaining decimal digits and return -1.
5439
      do {
5440
        Advance();
5441
      } while (IsDecimalDigit(current()));
5442
      min = RegExpTree::kInfinity;
5443
      break;
5444
    }
5445
    min = 10 * min + next;
5446
    Advance();
5447
  }
5448
  int max = 0;
5449
  if (current() == '}') {
5450
    max = min;
5451
    Advance();
5452
  } else if (current() == ',') {
5453
    Advance();
5454
    if (current() == '}') {
5455
      max = RegExpTree::kInfinity;
5456
      Advance();
5457
    } else {
5458
      while (IsDecimalDigit(current())) {
5459
        int next = current() - '0';
5460
        if (max > (RegExpTree::kInfinity - next) / 10) {
5461
          do {
5462
            Advance();
5463
          } while (IsDecimalDigit(current()));
5464
          max = RegExpTree::kInfinity;
5465
          break;
5466
        }
5467
        max = 10 * max + next;
5468
        Advance();
5469
      }
5470
      if (current() != '}') {
5471
        Reset(start);
5472
        return false;
5473
      }
5474
      Advance();
5475
    }
5476
  } else {
5477
    Reset(start);
5478
    return false;
5479
  }
5480
  *min_out = min;
5481
  *max_out = max;
5482
  return true;
5483
}
5484

    
5485

    
5486
uc32 RegExpParser::ParseOctalLiteral() {
5487
  ASSERT('0' <= current() && current() <= '7');
5488
  // For compatibility with some other browsers (not all), we parse
5489
  // up to three octal digits with a value below 256.
5490
  uc32 value = current() - '0';
5491
  Advance();
5492
  if ('0' <= current() && current() <= '7') {
5493
    value = value * 8 + current() - '0';
5494
    Advance();
5495
    if (value < 32 && '0' <= current() && current() <= '7') {
5496
      value = value * 8 + current() - '0';
5497
      Advance();
5498
    }
5499
  }
5500
  return value;
5501
}
5502

    
5503

    
5504
bool RegExpParser::ParseHexEscape(int length, uc32 *value) {
5505
  int start = position();
5506
  uc32 val = 0;
5507
  bool done = false;
5508
  for (int i = 0; !done; i++) {
5509
    uc32 c = current();
5510
    int d = HexValue(c);
5511
    if (d < 0) {
5512
      Reset(start);
5513
      return false;
5514
    }
5515
    val = val * 16 + d;
5516
    Advance();
5517
    if (i == length - 1) {
5518
      done = true;
5519
    }
5520
  }
5521
  *value = val;
5522
  return true;
5523
}
5524

    
5525

    
5526
uc32 RegExpParser::ParseClassCharacterEscape() {
5527
  ASSERT(current() == '\\');
5528
  ASSERT(has_next() && !IsSpecialClassEscape(Next()));
5529
  Advance();
5530
  switch (current()) {
5531
    case 'b':
5532
      Advance();
5533
      return '\b';
5534
    // ControlEscape :: one of
5535
    //   f n r t v
5536
    case 'f':
5537
      Advance();
5538
      return '\f';
5539
    case 'n':
5540
      Advance();
5541
      return '\n';
5542
    case 'r':
5543
      Advance();
5544
      return '\r';
5545
    case 't':
5546
      Advance();
5547
      return '\t';
5548
    case 'v':
5549
      Advance();
5550
      return '\v';
5551
    case 'c': {
5552
      uc32 controlLetter = Next();
5553
      uc32 letter = controlLetter & ~('A' ^ 'a');
5554
      // For compatibility with JSC, inside a character class
5555
      // we also accept digits and underscore as control characters.
5556
      if ((controlLetter >= '0' && controlLetter <= '9') ||
5557
          controlLetter == '_' ||
5558
          (letter >= 'A' && letter <= 'Z')) {
5559
        Advance(2);
5560
        // Control letters mapped to ASCII control characters in the range
5561
        // 0x00-0x1f.
5562
        return controlLetter & 0x1f;
5563
      }
5564
      // We match JSC in reading the backslash as a literal
5565
      // character instead of as starting an escape.
5566
      return '\\';
5567
    }
5568
    case '0': case '1': case '2': case '3': case '4': case '5':
5569
    case '6': case '7':
5570
      // For compatibility, we interpret a decimal escape that isn't
5571
      // a back reference (and therefore either \0 or not valid according
5572
      // to the specification) as a 1..3 digit octal character code.
5573
      return ParseOctalLiteral();
5574
    case 'x': {
5575
      Advance();
5576
      uc32 value;
5577
      if (ParseHexEscape(2, &value)) {
5578
        return value;
5579
      }
5580
      // If \x is not followed by a two-digit hexadecimal, treat it
5581
      // as an identity escape.
5582
      return 'x';
5583
    }
5584
    case 'u': {
5585
      Advance();
5586
      uc32 value;
5587
      if (ParseHexEscape(4, &value)) {
5588
        return value;
5589
      }
5590
      // If \u is not followed by a four-digit hexadecimal, treat it
5591
      // as an identity escape.
5592
      return 'u';
5593
    }
5594
    default: {
5595
      // Extended identity escape. We accept any character that hasn't
5596
      // been matched by a more specific case, not just the subset required
5597
      // by the ECMAScript specification.
5598
      uc32 result = current();
5599
      Advance();
5600
      return result;
5601
    }
5602
  }
5603
  return 0;
5604
}
5605

    
5606

    
5607
CharacterRange RegExpParser::ParseClassAtom(uc16* char_class) {
5608
  ASSERT_EQ(0, *char_class);
5609
  uc32 first = current();
5610
  if (first == '\\') {
5611
    switch (Next()) {
5612
      case 'w': case 'W': case 'd': case 'D': case 's': case 'S': {
5613
        *char_class = Next();
5614
        Advance(2);
5615
        return CharacterRange::Singleton(0);  // Return dummy value.
5616
      }
5617
      case kEndMarker:
5618
        return ReportError(CStrVector("\\ at end of pattern"));
5619
      default:
5620
        uc32 c = ParseClassCharacterEscape(CHECK_FAILED);
5621
        return CharacterRange::Singleton(c);
5622
    }
5623
  } else {
5624
    Advance();
5625
    return CharacterRange::Singleton(first);
5626
  }
5627
}
5628

    
5629

    
5630
static const uc16 kNoCharClass = 0;
5631

    
5632
// Adds range or pre-defined character class to character ranges.
5633
// If char_class is not kInvalidClass, it's interpreted as a class
5634
// escape (i.e., 's' means whitespace, from '\s').
5635
static inline void AddRangeOrEscape(ZoneList<CharacterRange>* ranges,
5636
                                    uc16 char_class,
5637
                                    CharacterRange range,
5638
                                    Zone* zone) {
5639
  if (char_class != kNoCharClass) {
5640
    CharacterRange::AddClassEscape(char_class, ranges, zone);
5641
  } else {
5642
    ranges->Add(range, zone);
5643
  }
5644
}
5645

    
5646

    
5647
RegExpTree* RegExpParser::ParseCharacterClass() {
5648
  static const char* kUnterminated = "Unterminated character class";
5649
  static const char* kRangeOutOfOrder = "Range out of order in character class";
5650

    
5651
  ASSERT_EQ(current(), '[');
5652
  Advance();
5653
  bool is_negated = false;
5654
  if (current() == '^') {
5655
    is_negated = true;
5656
    Advance();
5657
  }
5658
  ZoneList<CharacterRange>* ranges =
5659
      new(zone()) ZoneList<CharacterRange>(2, zone());
5660
  while (has_more() && current() != ']') {
5661
    uc16 char_class = kNoCharClass;
5662
    CharacterRange first = ParseClassAtom(&char_class CHECK_FAILED);
5663
    if (current() == '-') {
5664
      Advance();
5665
      if (current() == kEndMarker) {
5666
        // If we reach the end we break out of the loop and let the
5667
        // following code report an error.
5668
        break;
5669
      } else if (current() == ']') {
5670
        AddRangeOrEscape(ranges, char_class, first, zone());
5671
        ranges->Add(CharacterRange::Singleton('-'), zone());
5672
        break;
5673
      }
5674
      uc16 char_class_2 = kNoCharClass;
5675
      CharacterRange next = ParseClassAtom(&char_class_2 CHECK_FAILED);
5676
      if (char_class != kNoCharClass || char_class_2 != kNoCharClass) {
5677
        // Either end is an escaped character class. Treat the '-' verbatim.
5678
        AddRangeOrEscape(ranges, char_class, first, zone());
5679
        ranges->Add(CharacterRange::Singleton('-'), zone());
5680
        AddRangeOrEscape(ranges, char_class_2, next, zone());
5681
        continue;
5682
      }
5683
      if (first.from() > next.to()) {
5684
        return ReportError(CStrVector(kRangeOutOfOrder) CHECK_FAILED);
5685
      }
5686
      ranges->Add(CharacterRange::Range(first.from(), next.to()), zone());
5687
    } else {
5688
      AddRangeOrEscape(ranges, char_class, first, zone());
5689
    }
5690
  }
5691
  if (!has_more()) {
5692
    return ReportError(CStrVector(kUnterminated) CHECK_FAILED);
5693
  }
5694
  Advance();
5695
  if (ranges->length() == 0) {
5696
    ranges->Add(CharacterRange::Everything(), zone());
5697
    is_negated = !is_negated;
5698
  }
5699
  return new(zone()) RegExpCharacterClass(ranges, is_negated);
5700
}
5701

    
5702

    
5703
// ----------------------------------------------------------------------------
5704
// The Parser interface.
5705

    
5706
ParserMessage::~ParserMessage() {
5707
  for (int i = 0; i < args().length(); i++)
5708
    DeleteArray(args()[i]);
5709
  DeleteArray(args().start());
5710
}
5711

    
5712

    
5713
ScriptDataImpl::~ScriptDataImpl() {
5714
  if (owns_store_) store_.Dispose();
5715
}
5716

    
5717

    
5718
int ScriptDataImpl::Length() {
5719
  return store_.length() * sizeof(unsigned);
5720
}
5721

    
5722

    
5723
const char* ScriptDataImpl::Data() {
5724
  return reinterpret_cast<const char*>(store_.start());
5725
}
5726

    
5727

    
5728
bool ScriptDataImpl::HasError() {
5729
  return has_error();
5730
}
5731

    
5732

    
5733
void ScriptDataImpl::Initialize() {
5734
  // Prepares state for use.
5735
  if (store_.length() >= PreparseDataConstants::kHeaderSize) {
5736
    function_index_ = PreparseDataConstants::kHeaderSize;
5737
    int symbol_data_offset = PreparseDataConstants::kHeaderSize
5738
        + store_[PreparseDataConstants::kFunctionsSizeOffset];
5739
    if (store_.length() > symbol_data_offset) {
5740
      symbol_data_ = reinterpret_cast<byte*>(&store_[symbol_data_offset]);
5741
    } else {
5742
      // Partial preparse causes no symbol information.
5743
      symbol_data_ = reinterpret_cast<byte*>(&store_[0] + store_.length());
5744
    }
5745
    symbol_data_end_ = reinterpret_cast<byte*>(&store_[0] + store_.length());
5746
  }
5747
}
5748

    
5749

    
5750
int ScriptDataImpl::ReadNumber(byte** source) {
5751
  // Reads a number from symbol_data_ in base 128. The most significant
5752
  // bit marks that there are more digits.
5753
  // If the first byte is 0x80 (kNumberTerminator), it would normally
5754
  // represent a leading zero. Since that is useless, and therefore won't
5755
  // appear as the first digit of any actual value, it is used to
5756
  // mark the end of the input stream.
5757
  byte* data = *source;
5758
  if (data >= symbol_data_end_) return -1;
5759
  byte input = *data;
5760
  if (input == PreparseDataConstants::kNumberTerminator) {
5761
    // End of stream marker.
5762
    return -1;
5763
  }
5764
  int result = input & 0x7f;
5765
  data++;
5766
  while ((input & 0x80u) != 0) {
5767
    if (data >= symbol_data_end_) return -1;
5768
    input = *data;
5769
    result = (result << 7) | (input & 0x7f);
5770
    data++;
5771
  }
5772
  *source = data;
5773
  return result;
5774
}
5775

    
5776

    
5777
// Create a Scanner for the preparser to use as input, and preparse the source.
5778
ScriptDataImpl* PreParserApi::PreParse(Isolate* isolate,
5779
                                       Utf16CharacterStream* source) {
5780
  CompleteParserRecorder recorder;
5781
  HistogramTimerScope timer(isolate->counters()->pre_parse());
5782
  Scanner scanner(isolate->unicode_cache());
5783
  intptr_t stack_limit = isolate->stack_guard()->real_climit();
5784
  PreParser preparser(&scanner, &recorder, stack_limit);
5785
  preparser.set_allow_lazy(true);
5786
  preparser.set_allow_generators(FLAG_harmony_generators);
5787
  preparser.set_allow_for_of(FLAG_harmony_iteration);
5788
  preparser.set_allow_harmony_scoping(FLAG_harmony_scoping);
5789
  preparser.set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals);
5790
  scanner.Initialize(source);
5791
  PreParser::PreParseResult result = preparser.PreParseProgram();
5792
  if (result == PreParser::kPreParseStackOverflow) {
5793
    isolate->StackOverflow();
5794
    return NULL;
5795
  }
5796

    
5797
  // Extract the accumulated data from the recorder as a single
5798
  // contiguous vector that we are responsible for disposing.
5799
  Vector<unsigned> store = recorder.ExtractData();
5800
  return new ScriptDataImpl(store);
5801
}
5802

    
5803

    
5804
bool RegExpParser::ParseRegExp(FlatStringReader* input,
5805
                               bool multiline,
5806
                               RegExpCompileData* result,
5807
                               Zone* zone) {
5808
  ASSERT(result != NULL);
5809
  RegExpParser parser(input, &result->error, multiline, zone);
5810
  RegExpTree* tree = parser.ParsePattern();
5811
  if (parser.failed()) {
5812
    ASSERT(tree == NULL);
5813
    ASSERT(!result->error.is_null());
5814
  } else {
5815
    ASSERT(tree != NULL);
5816
    ASSERT(result->error.is_null());
5817
    result->tree = tree;
5818
    int capture_count = parser.captures_started();
5819
    result->simple = tree->IsAtom() && parser.simple() && capture_count == 0;
5820
    result->contains_anchor = parser.contains_anchor();
5821
    result->capture_count = capture_count;
5822
  }
5823
  return !parser.failed();
5824
}
5825

    
5826

    
5827
bool Parser::Parse() {
5828
  ASSERT(info()->function() == NULL);
5829
  FunctionLiteral* result = NULL;
5830
  if (info()->is_lazy()) {
5831
    ASSERT(!info()->is_eval());
5832
    if (info()->shared_info()->is_function()) {
5833
      result = ParseLazy();
5834
    } else {
5835
      result = ParseProgram();
5836
    }
5837
  } else {
5838
    ScriptDataImpl* pre_parse_data = info()->pre_parse_data();
5839
    set_pre_parse_data(pre_parse_data);
5840
    if (pre_parse_data != NULL && pre_parse_data->has_error()) {
5841
      Scanner::Location loc = pre_parse_data->MessageLocation();
5842
      const char* message = pre_parse_data->BuildMessage();
5843
      Vector<const char*> args = pre_parse_data->BuildArgs();
5844
      ReportMessageAt(loc, message, args);
5845
      DeleteArray(message);
5846
      for (int i = 0; i < args.length(); i++) {
5847
        DeleteArray(args[i]);
5848
      }
5849
      DeleteArray(args.start());
5850
      ASSERT(info()->isolate()->has_pending_exception());
5851
    } else {
5852
      result = ParseProgram();
5853
    }
5854
  }
5855
  info()->SetFunction(result);
5856
  return (result != NULL);
5857
}
5858

    
5859
} }  // namespace v8::internal