blob: d335c8bdcd29433337d1173c9e17b3a1e116a999 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2011 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <cmath>
6
7#include "src/allocation.h"
8#include "src/base/logging.h"
9#include "src/conversions-inl.h"
10#include "src/conversions.h"
11#include "src/globals.h"
12#include "src/hashmap.h"
13#include "src/list.h"
14#include "src/parsing/parser-base.h"
15#include "src/parsing/preparse-data.h"
16#include "src/parsing/preparse-data-format.h"
17#include "src/parsing/preparser.h"
18#include "src/unicode.h"
19#include "src/utils.h"
20
21namespace v8 {
22namespace internal {
23
24void PreParserTraits::ReportMessageAt(Scanner::Location location,
25 MessageTemplate::Template message,
26 const char* arg,
27 ParseErrorType error_type) {
28 ReportMessageAt(location.beg_pos, location.end_pos, message, arg, error_type);
29}
30
31
32void PreParserTraits::ReportMessageAt(int start_pos, int end_pos,
33 MessageTemplate::Template message,
34 const char* arg,
35 ParseErrorType error_type) {
36 pre_parser_->log_->LogMessage(start_pos, end_pos, message, arg, error_type);
37}
38
39
40PreParserIdentifier PreParserTraits::GetSymbol(Scanner* scanner) {
41 if (scanner->current_token() == Token::FUTURE_RESERVED_WORD) {
42 return PreParserIdentifier::FutureReserved();
43 } else if (scanner->current_token() ==
44 Token::FUTURE_STRICT_RESERVED_WORD) {
45 return PreParserIdentifier::FutureStrictReserved();
46 } else if (scanner->current_token() == Token::LET) {
47 return PreParserIdentifier::Let();
48 } else if (scanner->current_token() == Token::STATIC) {
49 return PreParserIdentifier::Static();
50 } else if (scanner->current_token() == Token::YIELD) {
51 return PreParserIdentifier::Yield();
52 }
53 if (scanner->UnescapedLiteralMatches("eval", 4)) {
54 return PreParserIdentifier::Eval();
55 }
56 if (scanner->UnescapedLiteralMatches("arguments", 9)) {
57 return PreParserIdentifier::Arguments();
58 }
59 if (scanner->UnescapedLiteralMatches("undefined", 9)) {
60 return PreParserIdentifier::Undefined();
61 }
62 if (scanner->LiteralMatches("prototype", 9)) {
63 return PreParserIdentifier::Prototype();
64 }
65 if (scanner->LiteralMatches("constructor", 11)) {
66 return PreParserIdentifier::Constructor();
67 }
68 return PreParserIdentifier::Default();
69}
70
71
72PreParserIdentifier PreParserTraits::GetNumberAsSymbol(Scanner* scanner) {
73 return PreParserIdentifier::Default();
74}
75
76
77PreParserExpression PreParserTraits::ExpressionFromString(
78 int pos, Scanner* scanner, PreParserFactory* factory) {
79 if (scanner->UnescapedLiteralMatches("use strict", 10)) {
80 return PreParserExpression::UseStrictStringLiteral();
81 } else if (scanner->UnescapedLiteralMatches("use strong", 10)) {
82 return PreParserExpression::UseStrongStringLiteral();
83 }
84 return PreParserExpression::StringLiteral();
85}
86
87
88PreParserExpression PreParserTraits::ParseV8Intrinsic(bool* ok) {
89 return pre_parser_->ParseV8Intrinsic(ok);
90}
91
92
93PreParserExpression PreParserTraits::ParseFunctionLiteral(
94 PreParserIdentifier name, Scanner::Location function_name_location,
95 FunctionNameValidity function_name_validity, FunctionKind kind,
96 int function_token_position, FunctionLiteral::FunctionType type,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000097 LanguageMode language_mode, bool* ok) {
98 return pre_parser_->ParseFunctionLiteral(
99 name, function_name_location, function_name_validity, kind,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100100 function_token_position, type, language_mode, ok);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000101}
102
103
104PreParser::PreParseResult PreParser::PreParseLazyFunction(
105 LanguageMode language_mode, FunctionKind kind, bool has_simple_parameters,
106 ParserRecorder* log, Scanner::BookmarkScope* bookmark) {
107 log_ = log;
108 // Lazy functions always have trivial outer scopes (no with/catch scopes).
109 Scope* top_scope = NewScope(scope_, SCRIPT_SCOPE);
110 PreParserFactory top_factory(NULL);
111 FunctionState top_state(&function_state_, &scope_, top_scope, kNormalFunction,
112 &top_factory);
113 scope_->SetLanguageMode(language_mode);
114 Scope* function_scope = NewScope(scope_, FUNCTION_SCOPE, kind);
115 if (!has_simple_parameters) function_scope->SetHasNonSimpleParameters();
116 PreParserFactory function_factory(NULL);
117 FunctionState function_state(&function_state_, &scope_, function_scope, kind,
118 &function_factory);
119 DCHECK_EQ(Token::LBRACE, scanner()->current_token());
120 bool ok = true;
121 int start_position = peek_position();
122 ParseLazyFunctionLiteralBody(&ok, bookmark);
123 if (bookmark && bookmark->HasBeenReset()) {
124 // Do nothing, as we've just aborted scanning this function.
125 } else if (stack_overflow()) {
126 return kPreParseStackOverflow;
127 } else if (!ok) {
128 ReportUnexpectedToken(scanner()->current_token());
129 } else {
130 DCHECK_EQ(Token::RBRACE, scanner()->peek());
131 if (is_strict(scope_->language_mode())) {
132 int end_pos = scanner()->location().end_pos;
133 CheckStrictOctalLiteral(start_position, end_pos, &ok);
134 if (!ok) return kPreParseSuccess;
135
136 if (is_strong(scope_->language_mode()) && IsSubclassConstructor(kind)) {
137 if (!function_state.super_location().IsValid()) {
138 ReportMessageAt(Scanner::Location(start_position, start_position + 1),
139 MessageTemplate::kStrongSuperCallMissing,
140 kReferenceError);
141 return kPreParseSuccess;
142 }
143 }
144 }
145 }
146 return kPreParseSuccess;
147}
148
149
150PreParserExpression PreParserTraits::ParseClassLiteral(
151 PreParserIdentifier name, Scanner::Location class_name_location,
152 bool name_is_strict_reserved, int pos, bool* ok) {
153 return pre_parser_->ParseClassLiteral(name, class_name_location,
154 name_is_strict_reserved, pos, ok);
155}
156
157
158// Preparsing checks a JavaScript program and emits preparse-data that helps
159// a later parsing to be faster.
160// See preparser-data.h for the data.
161
162// The PreParser checks that the syntax follows the grammar for JavaScript,
163// and collects some information about the program along the way.
164// The grammar check is only performed in order to understand the program
165// sufficiently to deduce some information about it, that can be used
166// to speed up later parsing. Finding errors is not the goal of pre-parsing,
167// rather it is to speed up properly written and correct programs.
168// That means that contextual checks (like a label being declared where
169// it is used) are generally omitted.
170
171
172PreParser::Statement PreParser::ParseStatementListItem(bool* ok) {
173 // ECMA 262 6th Edition
174 // StatementListItem[Yield, Return] :
175 // Statement[?Yield, ?Return]
176 // Declaration[?Yield]
177 //
178 // Declaration[Yield] :
179 // HoistableDeclaration[?Yield]
180 // ClassDeclaration[?Yield]
181 // LexicalDeclaration[In, ?Yield]
182 //
183 // HoistableDeclaration[Yield, Default] :
184 // FunctionDeclaration[?Yield, ?Default]
185 // GeneratorDeclaration[?Yield, ?Default]
186 //
187 // LexicalDeclaration[In, Yield] :
188 // LetOrConst BindingList[?In, ?Yield] ;
189
190 switch (peek()) {
191 case Token::FUNCTION:
192 return ParseFunctionDeclaration(ok);
193 case Token::CLASS:
194 return ParseClassDeclaration(ok);
195 case Token::CONST:
196 if (allow_const()) {
197 return ParseVariableStatement(kStatementListItem, ok);
198 }
199 break;
200 case Token::LET:
201 if (IsNextLetKeyword()) {
202 return ParseVariableStatement(kStatementListItem, ok);
203 }
204 break;
205 default:
206 break;
207 }
208 return ParseStatement(ok);
209}
210
211
212void PreParser::ParseStatementList(int end_token, bool* ok,
213 Scanner::BookmarkScope* bookmark) {
214 // SourceElements ::
215 // (Statement)* <end_token>
216
217 // Bookkeeping for trial parse if bookmark is set:
218 DCHECK_IMPLIES(bookmark, bookmark->HasBeenSet());
219 bool maybe_reset = bookmark != nullptr;
220 int count_statements = 0;
221
222 bool directive_prologue = true;
223 while (peek() != end_token) {
224 if (directive_prologue && peek() != Token::STRING) {
225 directive_prologue = false;
226 }
227 bool starts_with_identifier = peek() == Token::IDENTIFIER;
228 Scanner::Location token_loc = scanner()->peek_location();
229 Scanner::Location old_this_loc = function_state_->this_location();
230 Scanner::Location old_super_loc = function_state_->super_location();
231 Statement statement = ParseStatementListItem(ok);
232 if (!*ok) return;
233
234 if (is_strong(language_mode()) && scope_->is_function_scope() &&
235 IsClassConstructor(function_state_->kind())) {
236 Scanner::Location this_loc = function_state_->this_location();
237 Scanner::Location super_loc = function_state_->super_location();
238 if (this_loc.beg_pos != old_this_loc.beg_pos &&
239 this_loc.beg_pos != token_loc.beg_pos) {
240 ReportMessageAt(this_loc, MessageTemplate::kStrongConstructorThis);
241 *ok = false;
242 return;
243 }
244 if (super_loc.beg_pos != old_super_loc.beg_pos &&
245 super_loc.beg_pos != token_loc.beg_pos) {
246 ReportMessageAt(super_loc, MessageTemplate::kStrongConstructorSuper);
247 *ok = false;
248 return;
249 }
250 }
251
252 if (directive_prologue) {
253 bool use_strict_found = statement.IsUseStrictLiteral();
254 bool use_strong_found =
255 statement.IsUseStrongLiteral() && allow_strong_mode();
256
257 if (use_strict_found) {
258 scope_->SetLanguageMode(
259 static_cast<LanguageMode>(scope_->language_mode() | STRICT));
260 } else if (use_strong_found) {
261 scope_->SetLanguageMode(static_cast<LanguageMode>(
262 scope_->language_mode() | STRONG));
263 if (IsClassConstructor(function_state_->kind())) {
264 // "use strong" cannot occur in a class constructor body, to avoid
265 // unintuitive strong class object semantics.
266 PreParserTraits::ReportMessageAt(
267 token_loc, MessageTemplate::kStrongConstructorDirective);
268 *ok = false;
269 return;
270 }
271 } else if (!statement.IsStringLiteral()) {
272 directive_prologue = false;
273 }
274
275 if ((use_strict_found || use_strong_found) &&
276 !scope_->HasSimpleParameters()) {
277 // TC39 deemed "use strict" directives to be an error when occurring
278 // in the body of a function with non-simple parameter list, on
279 // 29/7/2015. https://goo.gl/ueA7Ln
280 //
281 // In V8, this also applies to "use strong " directives.
282 PreParserTraits::ReportMessageAt(
283 token_loc, MessageTemplate::kIllegalLanguageModeDirective,
284 use_strict_found ? "use strict" : "use strong");
285 *ok = false;
286 return;
287 }
288 }
289
290 // If we're allowed to reset to a bookmark, we will do so when we see a long
291 // and trivial function.
292 // Our current definition of 'long and trivial' is:
293 // - over 200 statements
294 // - all starting with an identifier (i.e., no if, for, while, etc.)
295 if (maybe_reset && (!starts_with_identifier ||
296 ++count_statements > kLazyParseTrialLimit)) {
297 if (count_statements > kLazyParseTrialLimit) {
298 bookmark->Reset();
299 return;
300 }
301 maybe_reset = false;
302 }
303 }
304}
305
306
307#define CHECK_OK ok); \
308 if (!*ok) return Statement::Default(); \
309 ((void)0
310#define DUMMY ) // to make indentation work
311#undef DUMMY
312
313
314PreParser::Statement PreParser::ParseStatement(bool* ok) {
315 // Statement ::
316 // EmptyStatement
317 // ...
318
319 if (peek() == Token::SEMICOLON) {
320 Next();
321 return Statement::Default();
322 }
323 return ParseSubStatement(ok);
324}
325
326
327PreParser::Statement PreParser::ParseSubStatement(bool* ok) {
328 // Statement ::
329 // Block
330 // VariableStatement
331 // EmptyStatement
332 // ExpressionStatement
333 // IfStatement
334 // IterationStatement
335 // ContinueStatement
336 // BreakStatement
337 // ReturnStatement
338 // WithStatement
339 // LabelledStatement
340 // SwitchStatement
341 // ThrowStatement
342 // TryStatement
343 // DebuggerStatement
344
345 // Note: Since labels can only be used by 'break' and 'continue'
346 // statements, which themselves are only valid within blocks,
347 // iterations or 'switch' statements (i.e., BreakableStatements),
348 // labels can be simply ignored in all other cases; except for
349 // trivial labeled break statements 'label: break label' which is
350 // parsed into an empty statement.
351
352 // Keep the source position of the statement
353 switch (peek()) {
354 case Token::LBRACE:
355 return ParseBlock(ok);
356
357 case Token::SEMICOLON:
358 if (is_strong(language_mode())) {
359 PreParserTraits::ReportMessageAt(scanner()->peek_location(),
360 MessageTemplate::kStrongEmpty);
361 *ok = false;
362 return Statement::Default();
363 }
364 Next();
365 return Statement::Default();
366
367 case Token::IF:
368 return ParseIfStatement(ok);
369
370 case Token::DO:
371 return ParseDoWhileStatement(ok);
372
373 case Token::WHILE:
374 return ParseWhileStatement(ok);
375
376 case Token::FOR:
377 return ParseForStatement(ok);
378
379 case Token::CONTINUE:
380 return ParseContinueStatement(ok);
381
382 case Token::BREAK:
383 return ParseBreakStatement(ok);
384
385 case Token::RETURN:
386 return ParseReturnStatement(ok);
387
388 case Token::WITH:
389 return ParseWithStatement(ok);
390
391 case Token::SWITCH:
392 return ParseSwitchStatement(ok);
393
394 case Token::THROW:
395 return ParseThrowStatement(ok);
396
397 case Token::TRY:
398 return ParseTryStatement(ok);
399
400 case Token::FUNCTION: {
401 Scanner::Location start_location = scanner()->peek_location();
402 Statement statement = ParseFunctionDeclaration(CHECK_OK);
403 Scanner::Location end_location = scanner()->location();
404 if (is_strict(language_mode())) {
405 PreParserTraits::ReportMessageAt(start_location.beg_pos,
406 end_location.end_pos,
407 MessageTemplate::kStrictFunction);
408 *ok = false;
409 return Statement::Default();
410 } else {
411 return statement;
412 }
413 }
414
415 case Token::DEBUGGER:
416 return ParseDebuggerStatement(ok);
417
418 case Token::VAR:
419 return ParseVariableStatement(kStatement, ok);
420
421 case Token::CONST:
422 // In ES6 CONST is not allowed as a Statement, only as a
423 // LexicalDeclaration, however we continue to allow it in sloppy mode for
424 // backwards compatibility.
425 if (is_sloppy(language_mode()) && allow_legacy_const()) {
426 return ParseVariableStatement(kStatement, ok);
427 }
428
429 // Fall through.
430 default:
431 return ParseExpressionOrLabelledStatement(ok);
432 }
433}
434
435
436PreParser::Statement PreParser::ParseFunctionDeclaration(bool* ok) {
437 // FunctionDeclaration ::
438 // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}'
439 // GeneratorDeclaration ::
440 // 'function' '*' Identifier '(' FormalParameterListopt ')'
441 // '{' FunctionBody '}'
442 Expect(Token::FUNCTION, CHECK_OK);
443 int pos = position();
444 bool is_generator = Check(Token::MUL);
445 bool is_strict_reserved = false;
446 Identifier name = ParseIdentifierOrStrictReservedWord(
447 &is_strict_reserved, CHECK_OK);
448 ParseFunctionLiteral(name, scanner()->location(),
449 is_strict_reserved ? kFunctionNameIsStrictReserved
450 : kFunctionNameValidityUnknown,
451 is_generator ? FunctionKind::kGeneratorFunction
452 : FunctionKind::kNormalFunction,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100453 pos, FunctionLiteral::kDeclaration, language_mode(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000454 CHECK_OK);
455 return Statement::FunctionDeclaration();
456}
457
458
459PreParser::Statement PreParser::ParseClassDeclaration(bool* ok) {
460 Expect(Token::CLASS, CHECK_OK);
461 if (!allow_harmony_sloppy() && is_sloppy(language_mode())) {
462 ReportMessage(MessageTemplate::kSloppyLexical);
463 *ok = false;
464 return Statement::Default();
465 }
466
467 int pos = position();
468 bool is_strict_reserved = false;
469 Identifier name =
470 ParseIdentifierOrStrictReservedWord(&is_strict_reserved, CHECK_OK);
471 ParseClassLiteral(name, scanner()->location(), is_strict_reserved, pos,
472 CHECK_OK);
473 return Statement::Default();
474}
475
476
477PreParser::Statement PreParser::ParseBlock(bool* ok) {
478 // Block ::
479 // '{' StatementList '}'
480
481 Expect(Token::LBRACE, CHECK_OK);
482 Statement final = Statement::Default();
483 while (peek() != Token::RBRACE) {
484 final = ParseStatementListItem(CHECK_OK);
485 }
486 Expect(Token::RBRACE, ok);
487 return final;
488}
489
490
491PreParser::Statement PreParser::ParseVariableStatement(
492 VariableDeclarationContext var_context,
493 bool* ok) {
494 // VariableStatement ::
495 // VariableDeclarations ';'
496
497 Statement result = ParseVariableDeclarations(
498 var_context, nullptr, nullptr, nullptr, nullptr, nullptr, CHECK_OK);
499 ExpectSemicolon(CHECK_OK);
500 return result;
501}
502
503
504// If the variable declaration declares exactly one non-const
505// variable, then *var is set to that variable. In all other cases,
506// *var is untouched; in particular, it is the caller's responsibility
507// to initialize it properly. This mechanism is also used for the parsing
508// of 'for-in' loops.
509PreParser::Statement PreParser::ParseVariableDeclarations(
510 VariableDeclarationContext var_context, int* num_decl, bool* is_lexical,
511 bool* is_binding_pattern, Scanner::Location* first_initializer_loc,
512 Scanner::Location* bindings_loc, bool* ok) {
513 // VariableDeclarations ::
514 // ('var' | 'const') (Identifier ('=' AssignmentExpression)?)+[',']
515 //
516 // The ES6 Draft Rev3 specifies the following grammar for const declarations
517 //
518 // ConstDeclaration ::
519 // const ConstBinding (',' ConstBinding)* ';'
520 // ConstBinding ::
521 // Identifier '=' AssignmentExpression
522 //
523 // TODO(ES6):
524 // ConstBinding ::
525 // BindingPattern '=' AssignmentExpression
526 bool require_initializer = false;
527 bool lexical = false;
528 bool is_pattern = false;
529 if (peek() == Token::VAR) {
530 if (is_strong(language_mode())) {
531 Scanner::Location location = scanner()->peek_location();
532 ReportMessageAt(location, MessageTemplate::kStrongVar);
533 *ok = false;
534 return Statement::Default();
535 }
536 Consume(Token::VAR);
537 } else if (peek() == Token::CONST && allow_const()) {
538 // TODO(ES6): The ES6 Draft Rev4 section 12.2.2 reads:
539 //
540 // ConstDeclaration : const ConstBinding (',' ConstBinding)* ';'
541 //
542 // * It is a Syntax Error if the code that matches this production is not
543 // contained in extended code.
544 //
545 // However disallowing const in sloppy mode will break compatibility with
546 // existing pages. Therefore we keep allowing const with the old
547 // non-harmony semantics in sloppy mode.
548 Consume(Token::CONST);
549 if (is_strict(language_mode()) ||
550 (allow_harmony_sloppy() && !allow_legacy_const())) {
551 DCHECK(var_context != kStatement);
552 require_initializer = true;
553 lexical = true;
554 }
555 } else if (peek() == Token::LET && allow_let()) {
556 Consume(Token::LET);
557 DCHECK(var_context != kStatement);
558 lexical = true;
559 } else {
560 *ok = false;
561 return Statement::Default();
562 }
563
564 // The scope of a var/const declared variable anywhere inside a function
565 // is the entire function (ECMA-262, 3rd, 10.1.3, and 12.2). The scope
566 // of a let declared variable is the scope of the immediately enclosing
567 // block.
568 int nvars = 0; // the number of variables declared
569 int bindings_start = peek_position();
570 do {
571 // Parse binding pattern.
572 if (nvars > 0) Consume(Token::COMMA);
573 int decl_pos = peek_position();
574 PreParserExpression pattern = PreParserExpression::Default();
575 {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100576 ExpressionClassifier pattern_classifier(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000577 Token::Value next = peek();
578 pattern = ParsePrimaryExpression(&pattern_classifier, CHECK_OK);
579
580 ValidateBindingPattern(&pattern_classifier, CHECK_OK);
581 if (lexical) {
582 ValidateLetPattern(&pattern_classifier, CHECK_OK);
583 }
584
585 if (!allow_harmony_destructuring_bind() && !pattern.IsIdentifier()) {
586 ReportUnexpectedToken(next);
587 *ok = false;
588 return Statement::Default();
589 }
590 }
591
Ben Murdoch097c5b22016-05-18 11:27:45 +0100592 is_pattern = pattern.IsObjectLiteral() || pattern.IsArrayLiteral();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000593
594 Scanner::Location variable_loc = scanner()->location();
595 nvars++;
596 if (Check(Token::ASSIGN)) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100597 ExpressionClassifier classifier(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000598 ParseAssignmentExpression(var_context != kForStatement, &classifier,
599 CHECK_OK);
600 ValidateExpression(&classifier, CHECK_OK);
601
602 variable_loc.end_pos = scanner()->location().end_pos;
603 if (first_initializer_loc && !first_initializer_loc->IsValid()) {
604 *first_initializer_loc = variable_loc;
605 }
606 } else if ((require_initializer || is_pattern) &&
Ben Murdoch097c5b22016-05-18 11:27:45 +0100607 (var_context != kForStatement || !PeekInOrOf())) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000608 PreParserTraits::ReportMessageAt(
609 Scanner::Location(decl_pos, scanner()->location().end_pos),
610 MessageTemplate::kDeclarationMissingInitializer,
611 is_pattern ? "destructuring" : "const");
612 *ok = false;
613 return Statement::Default();
614 }
615 } while (peek() == Token::COMMA);
616
617 if (bindings_loc) {
618 *bindings_loc =
619 Scanner::Location(bindings_start, scanner()->location().end_pos);
620 }
621
622 if (num_decl != nullptr) *num_decl = nvars;
623 if (is_lexical != nullptr) *is_lexical = lexical;
624 if (is_binding_pattern != nullptr) *is_binding_pattern = is_pattern;
625 return Statement::Default();
626}
627
628
629PreParser::Statement PreParser::ParseExpressionOrLabelledStatement(bool* ok) {
630 // ExpressionStatement | LabelledStatement ::
631 // Expression ';'
632 // Identifier ':' Statement
633
634 switch (peek()) {
635 case Token::FUNCTION:
636 case Token::LBRACE:
637 UNREACHABLE(); // Always handled by the callers.
638 case Token::CLASS:
639 ReportUnexpectedToken(Next());
640 *ok = false;
641 return Statement::Default();
642
643 case Token::THIS:
644 if (!FLAG_strong_this) break;
645 // Fall through.
646 case Token::SUPER:
647 if (is_strong(language_mode()) &&
648 IsClassConstructor(function_state_->kind())) {
649 bool is_this = peek() == Token::THIS;
650 Expression expr = Expression::Default();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100651 ExpressionClassifier classifier(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000652 if (is_this) {
653 expr = ParseStrongInitializationExpression(&classifier, CHECK_OK);
654 } else {
655 expr = ParseStrongSuperCallExpression(&classifier, CHECK_OK);
656 }
657 ValidateExpression(&classifier, CHECK_OK);
658 switch (peek()) {
659 case Token::SEMICOLON:
660 Consume(Token::SEMICOLON);
661 break;
662 case Token::RBRACE:
663 case Token::EOS:
664 break;
665 default:
666 if (!scanner()->HasAnyLineTerminatorBeforeNext()) {
667 ReportMessageAt(function_state_->this_location(),
668 is_this
669 ? MessageTemplate::kStrongConstructorThis
670 : MessageTemplate::kStrongConstructorSuper);
671 *ok = false;
672 return Statement::Default();
673 }
674 }
675 return Statement::ExpressionStatement(expr);
676 }
677 break;
678
679 // TODO(arv): Handle `let [`
680 // https://code.google.com/p/v8/issues/detail?id=3847
681
682 default:
683 break;
684 }
685
686 bool starts_with_identifier = peek_any_identifier();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100687 ExpressionClassifier classifier(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000688 Expression expr = ParseExpression(true, &classifier, CHECK_OK);
689 ValidateExpression(&classifier, CHECK_OK);
690
691 // Even if the expression starts with an identifier, it is not necessarily an
692 // identifier. For example, "foo + bar" starts with an identifier but is not
693 // an identifier.
694 if (starts_with_identifier && expr.IsIdentifier() && peek() == Token::COLON) {
695 // Expression is a single identifier, and not, e.g., a parenthesized
696 // identifier.
697 DCHECK(!expr.AsIdentifier().IsFutureReserved());
698 DCHECK(is_sloppy(language_mode()) ||
699 !IsFutureStrictReserved(expr.AsIdentifier()));
700 Consume(Token::COLON);
701 Statement statement = ParseStatement(ok);
702 return statement.IsJumpStatement() ? Statement::Default() : statement;
703 // Preparsing is disabled for extensions (because the extension details
704 // aren't passed to lazily compiled functions), so we don't
705 // accept "native function" in the preparser.
706 }
707 // Parsed expression statement.
708 // Detect attempts at 'let' declarations in sloppy mode.
709 if (!allow_harmony_sloppy_let() && peek() == Token::IDENTIFIER &&
710 is_sloppy(language_mode()) && expr.IsIdentifier() &&
711 expr.AsIdentifier().IsLet()) {
712 ReportMessage(MessageTemplate::kSloppyLexical, NULL);
713 *ok = false;
714 return Statement::Default();
715 }
716 ExpectSemicolon(CHECK_OK);
717 return Statement::ExpressionStatement(expr);
718}
719
720
721PreParser::Statement PreParser::ParseIfStatement(bool* ok) {
722 // IfStatement ::
723 // 'if' '(' Expression ')' Statement ('else' Statement)?
724
725 Expect(Token::IF, CHECK_OK);
726 Expect(Token::LPAREN, CHECK_OK);
727 ParseExpression(true, CHECK_OK);
728 Expect(Token::RPAREN, CHECK_OK);
729 Statement stat = ParseSubStatement(CHECK_OK);
730 if (peek() == Token::ELSE) {
731 Next();
732 Statement else_stat = ParseSubStatement(CHECK_OK);
733 stat = (stat.IsJumpStatement() && else_stat.IsJumpStatement()) ?
734 Statement::Jump() : Statement::Default();
735 } else {
736 stat = Statement::Default();
737 }
738 return stat;
739}
740
741
742PreParser::Statement PreParser::ParseContinueStatement(bool* ok) {
743 // ContinueStatement ::
744 // 'continue' [no line terminator] Identifier? ';'
745
746 Expect(Token::CONTINUE, CHECK_OK);
747 Token::Value tok = peek();
748 if (!scanner()->HasAnyLineTerminatorBeforeNext() &&
749 tok != Token::SEMICOLON &&
750 tok != Token::RBRACE &&
751 tok != Token::EOS) {
752 // ECMA allows "eval" or "arguments" as labels even in strict mode.
753 ParseIdentifier(kAllowRestrictedIdentifiers, CHECK_OK);
754 }
755 ExpectSemicolon(CHECK_OK);
756 return Statement::Jump();
757}
758
759
760PreParser::Statement PreParser::ParseBreakStatement(bool* ok) {
761 // BreakStatement ::
762 // 'break' [no line terminator] Identifier? ';'
763
764 Expect(Token::BREAK, CHECK_OK);
765 Token::Value tok = peek();
766 if (!scanner()->HasAnyLineTerminatorBeforeNext() &&
767 tok != Token::SEMICOLON &&
768 tok != Token::RBRACE &&
769 tok != Token::EOS) {
770 // ECMA allows "eval" or "arguments" as labels even in strict mode.
771 ParseIdentifier(kAllowRestrictedIdentifiers, CHECK_OK);
772 }
773 ExpectSemicolon(CHECK_OK);
774 return Statement::Jump();
775}
776
777
778PreParser::Statement PreParser::ParseReturnStatement(bool* ok) {
779 // ReturnStatement ::
780 // 'return' [no line terminator] Expression? ';'
781
782 // Consume the return token. It is necessary to do before
783 // reporting any errors on it, because of the way errors are
784 // reported (underlining).
785 Expect(Token::RETURN, CHECK_OK);
786 function_state_->set_return_location(scanner()->location());
787
788 // An ECMAScript program is considered syntactically incorrect if it
789 // contains a return statement that is not within the body of a
790 // function. See ECMA-262, section 12.9, page 67.
791 // This is not handled during preparsing.
792
793 Token::Value tok = peek();
794 if (!scanner()->HasAnyLineTerminatorBeforeNext() &&
795 tok != Token::SEMICOLON &&
796 tok != Token::RBRACE &&
797 tok != Token::EOS) {
798 if (is_strong(language_mode()) &&
799 IsClassConstructor(function_state_->kind())) {
800 int pos = peek_position();
801 ReportMessageAt(Scanner::Location(pos, pos + 1),
802 MessageTemplate::kStrongConstructorReturnValue);
803 *ok = false;
804 return Statement::Default();
805 }
806 ParseExpression(true, CHECK_OK);
807 }
808 ExpectSemicolon(CHECK_OK);
809 return Statement::Jump();
810}
811
812
813PreParser::Statement PreParser::ParseWithStatement(bool* ok) {
814 // WithStatement ::
815 // 'with' '(' Expression ')' Statement
816 Expect(Token::WITH, CHECK_OK);
817 if (is_strict(language_mode())) {
818 ReportMessageAt(scanner()->location(), MessageTemplate::kStrictWith);
819 *ok = false;
820 return Statement::Default();
821 }
822 Expect(Token::LPAREN, CHECK_OK);
823 ParseExpression(true, CHECK_OK);
824 Expect(Token::RPAREN, CHECK_OK);
825
826 Scope* with_scope = NewScope(scope_, WITH_SCOPE);
827 BlockState block_state(&scope_, with_scope);
828 ParseSubStatement(CHECK_OK);
829 return Statement::Default();
830}
831
832
833PreParser::Statement PreParser::ParseSwitchStatement(bool* ok) {
834 // SwitchStatement ::
835 // 'switch' '(' Expression ')' '{' CaseClause* '}'
836
837 Expect(Token::SWITCH, CHECK_OK);
838 Expect(Token::LPAREN, CHECK_OK);
839 ParseExpression(true, CHECK_OK);
840 Expect(Token::RPAREN, CHECK_OK);
841
842 Expect(Token::LBRACE, CHECK_OK);
843 Token::Value token = peek();
844 while (token != Token::RBRACE) {
845 if (token == Token::CASE) {
846 Expect(Token::CASE, CHECK_OK);
847 ParseExpression(true, CHECK_OK);
848 } else {
849 Expect(Token::DEFAULT, CHECK_OK);
850 }
851 Expect(Token::COLON, CHECK_OK);
852 token = peek();
853 Statement statement = Statement::Jump();
854 while (token != Token::CASE &&
855 token != Token::DEFAULT &&
856 token != Token::RBRACE) {
857 statement = ParseStatementListItem(CHECK_OK);
858 token = peek();
859 }
860 if (is_strong(language_mode()) && !statement.IsJumpStatement() &&
861 token != Token::RBRACE) {
862 ReportMessageAt(scanner()->location(),
863 MessageTemplate::kStrongSwitchFallthrough);
864 *ok = false;
865 return Statement::Default();
866 }
867 }
868 Expect(Token::RBRACE, ok);
869 return Statement::Default();
870}
871
872
873PreParser::Statement PreParser::ParseDoWhileStatement(bool* ok) {
874 // DoStatement ::
875 // 'do' Statement 'while' '(' Expression ')' ';'
876
877 Expect(Token::DO, CHECK_OK);
878 ParseSubStatement(CHECK_OK);
879 Expect(Token::WHILE, CHECK_OK);
880 Expect(Token::LPAREN, CHECK_OK);
881 ParseExpression(true, CHECK_OK);
882 Expect(Token::RPAREN, ok);
883 if (peek() == Token::SEMICOLON) Consume(Token::SEMICOLON);
884 return Statement::Default();
885}
886
887
888PreParser::Statement PreParser::ParseWhileStatement(bool* ok) {
889 // WhileStatement ::
890 // 'while' '(' Expression ')' Statement
891
892 Expect(Token::WHILE, CHECK_OK);
893 Expect(Token::LPAREN, CHECK_OK);
894 ParseExpression(true, CHECK_OK);
895 Expect(Token::RPAREN, CHECK_OK);
896 ParseSubStatement(ok);
897 return Statement::Default();
898}
899
900
901PreParser::Statement PreParser::ParseForStatement(bool* ok) {
902 // ForStatement ::
903 // 'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement
904
905 Expect(Token::FOR, CHECK_OK);
906 Expect(Token::LPAREN, CHECK_OK);
907 bool is_let_identifier_expression = false;
908 if (peek() != Token::SEMICOLON) {
909 ForEachStatement::VisitMode mode;
910 if (peek() == Token::VAR || (peek() == Token::CONST && allow_const()) ||
911 (peek() == Token::LET && IsNextLetKeyword())) {
912 int decl_count;
913 bool is_lexical;
914 bool is_binding_pattern;
915 Scanner::Location first_initializer_loc = Scanner::Location::invalid();
916 Scanner::Location bindings_loc = Scanner::Location::invalid();
917 ParseVariableDeclarations(kForStatement, &decl_count, &is_lexical,
918 &is_binding_pattern, &first_initializer_loc,
919 &bindings_loc, CHECK_OK);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100920 if (CheckInOrOf(&mode, ok)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000921 if (!*ok) return Statement::Default();
922 if (decl_count != 1) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000923 PreParserTraits::ReportMessageAt(
924 bindings_loc, MessageTemplate::kForInOfLoopMultiBindings,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100925 ForEachStatement::VisitModeString(mode));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000926 *ok = false;
927 return Statement::Default();
928 }
929 if (first_initializer_loc.IsValid() &&
930 (is_strict(language_mode()) || mode == ForEachStatement::ITERATE ||
931 is_lexical || is_binding_pattern)) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100932 PreParserTraits::ReportMessageAt(
933 first_initializer_loc, MessageTemplate::kForInOfLoopInitializer,
934 ForEachStatement::VisitModeString(mode));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000935 *ok = false;
936 return Statement::Default();
937 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100938
939 if (mode == ForEachStatement::ITERATE) {
940 ExpressionClassifier classifier(this);
941 ParseAssignmentExpression(true, &classifier, CHECK_OK);
942 RewriteNonPattern(&classifier, CHECK_OK);
943 } else {
944 ParseExpression(true, CHECK_OK);
945 }
946
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000947 Expect(Token::RPAREN, CHECK_OK);
948 ParseSubStatement(CHECK_OK);
949 return Statement::Default();
950 }
951 } else {
952 int lhs_beg_pos = peek_position();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100953 ExpressionClassifier classifier(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000954 Expression lhs = ParseExpression(false, &classifier, CHECK_OK);
955 int lhs_end_pos = scanner()->location().end_pos;
956 is_let_identifier_expression =
957 lhs.IsIdentifier() && lhs.AsIdentifier().IsLet();
958 bool is_for_each = CheckInOrOf(&mode, ok);
959 if (!*ok) return Statement::Default();
960 bool is_destructuring = is_for_each &&
961 allow_harmony_destructuring_assignment() &&
962 (lhs->IsArrayLiteral() || lhs->IsObjectLiteral());
963
964 if (is_destructuring) {
965 ValidateAssignmentPattern(&classifier, CHECK_OK);
966 } else {
967 ValidateExpression(&classifier, CHECK_OK);
968 }
969
970 if (is_for_each) {
971 if (!is_destructuring) {
972 lhs = CheckAndRewriteReferenceExpression(
973 lhs, lhs_beg_pos, lhs_end_pos, MessageTemplate::kInvalidLhsInFor,
974 kSyntaxError, CHECK_OK);
975 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100976
977 if (mode == ForEachStatement::ITERATE) {
978 ExpressionClassifier classifier(this);
979 ParseAssignmentExpression(true, &classifier, CHECK_OK);
980 RewriteNonPattern(&classifier, CHECK_OK);
981 } else {
982 ParseExpression(true, CHECK_OK);
983 }
984
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000985 Expect(Token::RPAREN, CHECK_OK);
986 ParseSubStatement(CHECK_OK);
987 return Statement::Default();
988 }
989 }
990 }
991
992 // Parsed initializer at this point.
993 // Detect attempts at 'let' declarations in sloppy mode.
994 if (!allow_harmony_sloppy_let() && peek() == Token::IDENTIFIER &&
995 is_sloppy(language_mode()) && is_let_identifier_expression) {
996 ReportMessage(MessageTemplate::kSloppyLexical, NULL);
997 *ok = false;
998 return Statement::Default();
999 }
1000 Expect(Token::SEMICOLON, CHECK_OK);
1001
1002 if (peek() != Token::SEMICOLON) {
1003 ParseExpression(true, CHECK_OK);
1004 }
1005 Expect(Token::SEMICOLON, CHECK_OK);
1006
1007 if (peek() != Token::RPAREN) {
1008 ParseExpression(true, CHECK_OK);
1009 }
1010 Expect(Token::RPAREN, CHECK_OK);
1011
1012 ParseSubStatement(ok);
1013 return Statement::Default();
1014}
1015
1016
1017PreParser::Statement PreParser::ParseThrowStatement(bool* ok) {
1018 // ThrowStatement ::
1019 // 'throw' [no line terminator] Expression ';'
1020
1021 Expect(Token::THROW, CHECK_OK);
1022 if (scanner()->HasAnyLineTerminatorBeforeNext()) {
1023 ReportMessageAt(scanner()->location(), MessageTemplate::kNewlineAfterThrow);
1024 *ok = false;
1025 return Statement::Default();
1026 }
1027 ParseExpression(true, CHECK_OK);
1028 ExpectSemicolon(ok);
1029 return Statement::Jump();
1030}
1031
1032
1033PreParser::Statement PreParser::ParseTryStatement(bool* ok) {
1034 // TryStatement ::
1035 // 'try' Block Catch
1036 // 'try' Block Finally
1037 // 'try' Block Catch Finally
1038 //
1039 // Catch ::
1040 // 'catch' '(' Identifier ')' Block
1041 //
1042 // Finally ::
1043 // 'finally' Block
1044
1045 Expect(Token::TRY, CHECK_OK);
1046
1047 ParseBlock(CHECK_OK);
1048
1049 Token::Value tok = peek();
1050 if (tok != Token::CATCH && tok != Token::FINALLY) {
1051 ReportMessageAt(scanner()->location(), MessageTemplate::kNoCatchOrFinally);
1052 *ok = false;
1053 return Statement::Default();
1054 }
1055 if (tok == Token::CATCH) {
1056 Consume(Token::CATCH);
1057 Expect(Token::LPAREN, CHECK_OK);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001058 ExpressionClassifier pattern_classifier(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001059 ParsePrimaryExpression(&pattern_classifier, CHECK_OK);
1060 ValidateBindingPattern(&pattern_classifier, CHECK_OK);
1061 Expect(Token::RPAREN, CHECK_OK);
1062 {
1063 // TODO(adamk): Make this CATCH_SCOPE
1064 Scope* with_scope = NewScope(scope_, WITH_SCOPE);
1065 BlockState block_state(&scope_, with_scope);
1066 ParseBlock(CHECK_OK);
1067 }
1068 tok = peek();
1069 }
1070 if (tok == Token::FINALLY) {
1071 Consume(Token::FINALLY);
1072 ParseBlock(CHECK_OK);
1073 }
1074 return Statement::Default();
1075}
1076
1077
1078PreParser::Statement PreParser::ParseDebuggerStatement(bool* ok) {
1079 // In ECMA-262 'debugger' is defined as a reserved keyword. In some browser
1080 // contexts this is used as a statement which invokes the debugger as if a
1081 // break point is present.
1082 // DebuggerStatement ::
1083 // 'debugger' ';'
1084
1085 Expect(Token::DEBUGGER, CHECK_OK);
1086 ExpectSemicolon(ok);
1087 return Statement::Default();
1088}
1089
1090
1091#undef CHECK_OK
1092#define CHECK_OK ok); \
1093 if (!*ok) return Expression::Default(); \
1094 ((void)0
1095#define DUMMY ) // to make indentation work
1096#undef DUMMY
1097
1098
1099PreParser::Expression PreParser::ParseFunctionLiteral(
1100 Identifier function_name, Scanner::Location function_name_location,
1101 FunctionNameValidity function_name_validity, FunctionKind kind,
1102 int function_token_pos, FunctionLiteral::FunctionType function_type,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001103 LanguageMode language_mode, bool* ok) {
1104 // Function ::
1105 // '(' FormalParameterList? ')' '{' FunctionBody '}'
1106
1107 // Parse function body.
1108 bool outer_is_script_scope = scope_->is_script_scope();
1109 Scope* function_scope = NewScope(scope_, FUNCTION_SCOPE, kind);
1110 function_scope->SetLanguageMode(language_mode);
1111 PreParserFactory factory(NULL);
1112 FunctionState function_state(&function_state_, &scope_, function_scope, kind,
1113 &factory);
1114 DuplicateFinder duplicate_finder(scanner()->unicode_cache());
Ben Murdoch097c5b22016-05-18 11:27:45 +01001115 ExpressionClassifier formals_classifier(this, &duplicate_finder);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001116
1117 Expect(Token::LPAREN, CHECK_OK);
1118 int start_position = scanner()->location().beg_pos;
1119 function_scope->set_start_position(start_position);
1120 PreParserFormalParameters formals(function_scope);
1121 ParseFormalParameterList(&formals, &formals_classifier, CHECK_OK);
1122 Expect(Token::RPAREN, CHECK_OK);
1123 int formals_end_position = scanner()->location().end_pos;
1124
Ben Murdoch097c5b22016-05-18 11:27:45 +01001125 CheckArityRestrictions(formals.arity, kind, formals.has_rest, start_position,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001126 formals_end_position, CHECK_OK);
1127
1128 // See Parser::ParseFunctionLiteral for more information about lazy parsing
1129 // and lazy compilation.
1130 bool is_lazily_parsed =
1131 (outer_is_script_scope && allow_lazy() && !parenthesized_function_);
1132 parenthesized_function_ = false;
1133
1134 Expect(Token::LBRACE, CHECK_OK);
1135 if (is_lazily_parsed) {
1136 ParseLazyFunctionLiteralBody(CHECK_OK);
1137 } else {
1138 ParseStatementList(Token::RBRACE, CHECK_OK);
1139 }
1140 Expect(Token::RBRACE, CHECK_OK);
1141
1142 // Parsing the body may change the language mode in our scope.
1143 language_mode = function_scope->language_mode();
1144
1145 // Validate name and parameter names. We can do this only after parsing the
1146 // function, since the function can declare itself strict.
1147 CheckFunctionName(language_mode, function_name, function_name_validity,
1148 function_name_location, CHECK_OK);
1149 const bool allow_duplicate_parameters =
1150 is_sloppy(language_mode) && formals.is_simple && !IsConciseMethod(kind);
1151 ValidateFormalParameters(&formals_classifier, language_mode,
1152 allow_duplicate_parameters, CHECK_OK);
1153
1154 if (is_strict(language_mode)) {
1155 int end_position = scanner()->location().end_pos;
1156 CheckStrictOctalLiteral(start_position, end_position, CHECK_OK);
1157 }
1158
1159 if (is_strong(language_mode) && IsSubclassConstructor(kind)) {
1160 if (!function_state.super_location().IsValid()) {
1161 ReportMessageAt(function_name_location,
1162 MessageTemplate::kStrongSuperCallMissing,
1163 kReferenceError);
1164 *ok = false;
1165 return Expression::Default();
1166 }
1167 }
1168
1169 return Expression::Default();
1170}
1171
1172
1173void PreParser::ParseLazyFunctionLiteralBody(bool* ok,
1174 Scanner::BookmarkScope* bookmark) {
1175 int body_start = position();
1176 ParseStatementList(Token::RBRACE, ok, bookmark);
1177 if (!*ok) return;
1178 if (bookmark && bookmark->HasBeenReset()) return;
1179
1180 // Position right after terminal '}'.
1181 DCHECK_EQ(Token::RBRACE, scanner()->peek());
1182 int body_end = scanner()->peek_location().end_pos;
1183 log_->LogFunction(body_start, body_end,
1184 function_state_->materialized_literal_count(),
1185 function_state_->expected_property_count(), language_mode(),
1186 scope_->uses_super_property(), scope_->calls_eval());
1187}
1188
1189
1190PreParserExpression PreParser::ParseClassLiteral(
1191 PreParserIdentifier name, Scanner::Location class_name_location,
1192 bool name_is_strict_reserved, int pos, bool* ok) {
1193 // All parts of a ClassDeclaration and ClassExpression are strict code.
1194 if (name_is_strict_reserved) {
1195 ReportMessageAt(class_name_location,
1196 MessageTemplate::kUnexpectedStrictReserved);
1197 *ok = false;
1198 return EmptyExpression();
1199 }
1200 if (IsEvalOrArguments(name)) {
1201 ReportMessageAt(class_name_location, MessageTemplate::kStrictEvalArguments);
1202 *ok = false;
1203 return EmptyExpression();
1204 }
1205 LanguageMode class_language_mode = language_mode();
1206 if (is_strong(class_language_mode) && IsUndefined(name)) {
1207 ReportMessageAt(class_name_location, MessageTemplate::kStrongUndefined);
1208 *ok = false;
1209 return EmptyExpression();
1210 }
1211
1212 Scope* scope = NewScope(scope_, BLOCK_SCOPE);
1213 BlockState block_state(&scope_, scope);
1214 scope_->SetLanguageMode(
1215 static_cast<LanguageMode>(class_language_mode | STRICT));
1216 // TODO(marja): Make PreParser use scope names too.
1217 // scope_->SetScopeName(name);
1218
1219 bool has_extends = Check(Token::EXTENDS);
1220 if (has_extends) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001221 ExpressionClassifier classifier(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001222 ParseLeftHandSideExpression(&classifier, CHECK_OK);
1223 ValidateExpression(&classifier, CHECK_OK);
1224 }
1225
1226 ClassLiteralChecker checker(this);
1227 bool has_seen_constructor = false;
1228
1229 Expect(Token::LBRACE, CHECK_OK);
1230 while (peek() != Token::RBRACE) {
1231 if (Check(Token::SEMICOLON)) continue;
1232 const bool in_class = true;
1233 const bool is_static = false;
1234 bool is_computed_name = false; // Classes do not care about computed
1235 // property names here.
1236 Identifier name;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001237 ExpressionClassifier classifier(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001238 ParsePropertyDefinition(&checker, in_class, has_extends, is_static,
1239 &is_computed_name, &has_seen_constructor,
1240 &classifier, &name, CHECK_OK);
1241 ValidateExpression(&classifier, CHECK_OK);
1242 }
1243
1244 Expect(Token::RBRACE, CHECK_OK);
1245
1246 return Expression::Default();
1247}
1248
1249
1250PreParser::Expression PreParser::ParseV8Intrinsic(bool* ok) {
1251 // CallRuntime ::
1252 // '%' Identifier Arguments
1253 Expect(Token::MOD, CHECK_OK);
1254 if (!allow_natives()) {
1255 *ok = false;
1256 return Expression::Default();
1257 }
1258 // Allow "eval" or "arguments" for backward compatibility.
1259 ParseIdentifier(kAllowRestrictedIdentifiers, CHECK_OK);
1260 Scanner::Location spread_pos;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001261 ExpressionClassifier classifier(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001262 ParseArguments(&spread_pos, &classifier, ok);
1263 ValidateExpression(&classifier, CHECK_OK);
1264
1265 DCHECK(!spread_pos.IsValid());
1266
1267 return Expression::Default();
1268}
1269
1270
1271PreParserExpression PreParser::ParseDoExpression(bool* ok) {
1272 // AssignmentExpression ::
1273 // do '{' StatementList '}'
1274 Expect(Token::DO, CHECK_OK);
1275 Expect(Token::LBRACE, CHECK_OK);
1276 Scope* block_scope = NewScope(scope_, BLOCK_SCOPE);
1277 {
1278 BlockState block_state(&scope_, block_scope);
1279 while (peek() != Token::RBRACE) {
1280 ParseStatementListItem(CHECK_OK);
1281 }
1282 Expect(Token::RBRACE, CHECK_OK);
1283 return PreParserExpression::Default();
1284 }
1285}
1286
1287#undef CHECK_OK
1288
1289
1290} // namespace internal
1291} // namespace v8