1 // 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.
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"
14 #include "src/preparse-data.h"
15 #include "src/preparse-data-format.h"
16 #include "src/preparser.h"
17 #include "src/unicode.h"
18 #include "src/utils.h"
23 void PreParserTraits::ReportMessageAt(Scanner::Location location,
24 const char* message, const char* arg,
25 ParseErrorType error_type) {
26 ReportMessageAt(location.beg_pos, location.end_pos, message, arg, error_type);
30 void PreParserTraits::ReportMessageAt(int start_pos, int end_pos,
31 const char* message, const char* arg,
32 ParseErrorType error_type) {
33 pre_parser_->log_->LogMessage(start_pos, end_pos, message, arg, error_type);
37 PreParserIdentifier PreParserTraits::GetSymbol(Scanner* scanner) {
38 if (scanner->current_token() == Token::FUTURE_RESERVED_WORD) {
39 return PreParserIdentifier::FutureReserved();
40 } else if (scanner->current_token() ==
41 Token::FUTURE_STRICT_RESERVED_WORD) {
42 return PreParserIdentifier::FutureStrictReserved();
43 } else if (scanner->current_token() == Token::LET) {
44 return PreParserIdentifier::Let();
45 } else if (scanner->current_token() == Token::STATIC) {
46 return PreParserIdentifier::Static();
47 } else if (scanner->current_token() == Token::YIELD) {
48 return PreParserIdentifier::Yield();
50 if (scanner->UnescapedLiteralMatches("eval", 4)) {
51 return PreParserIdentifier::Eval();
53 if (scanner->UnescapedLiteralMatches("arguments", 9)) {
54 return PreParserIdentifier::Arguments();
56 if (scanner->LiteralMatches("prototype", 9)) {
57 return PreParserIdentifier::Prototype();
59 if (scanner->LiteralMatches("constructor", 11)) {
60 return PreParserIdentifier::Constructor();
62 return PreParserIdentifier::Default();
66 PreParserIdentifier PreParserTraits::GetNumberAsSymbol(Scanner* scanner) {
67 return PreParserIdentifier::Default();
71 PreParserExpression PreParserTraits::ExpressionFromString(
72 int pos, Scanner* scanner, PreParserFactory* factory) {
73 if (scanner->UnescapedLiteralMatches("use strict", 10)) {
74 return PreParserExpression::UseStrictStringLiteral();
75 } else if (scanner->UnescapedLiteralMatches("use strong", 10)) {
76 return PreParserExpression::UseStrongStringLiteral();
78 return PreParserExpression::StringLiteral();
82 PreParserExpression PreParserTraits::ParseV8Intrinsic(bool* ok) {
83 return pre_parser_->ParseV8Intrinsic(ok);
87 PreParserExpression PreParserTraits::ParseFunctionLiteral(
88 PreParserIdentifier name, Scanner::Location function_name_location,
89 bool name_is_strict_reserved, FunctionKind kind,
90 int function_token_position, FunctionLiteral::FunctionType type,
91 FunctionLiteral::ArityRestriction arity_restriction, bool* ok) {
92 return pre_parser_->ParseFunctionLiteral(
93 name, function_name_location, name_is_strict_reserved, kind,
94 function_token_position, type, arity_restriction, ok);
98 PreParser::PreParseResult PreParser::PreParseLazyFunction(
99 LanguageMode language_mode, FunctionKind kind, ParserRecorder* log) {
101 // Lazy functions always have trivial outer scopes (no with/catch scopes).
102 Scope* top_scope = NewScope(scope_, SCRIPT_SCOPE);
103 PreParserFactory top_factory(NULL);
104 FunctionState top_state(&function_state_, &scope_, top_scope, kNormalFunction,
106 scope_->SetLanguageMode(language_mode);
107 Scope* function_scope = NewScope(scope_, FUNCTION_SCOPE);
108 PreParserFactory function_factory(NULL);
109 FunctionState function_state(&function_state_, &scope_, function_scope, kind,
111 DCHECK_EQ(Token::LBRACE, scanner()->current_token());
113 int start_position = peek_position();
114 ParseLazyFunctionLiteralBody(&ok);
115 if (stack_overflow()) return kPreParseStackOverflow;
117 ReportUnexpectedToken(scanner()->current_token());
119 DCHECK_EQ(Token::RBRACE, scanner()->peek());
120 if (is_strict(scope_->language_mode())) {
121 int end_pos = scanner()->location().end_pos;
122 CheckStrictOctalLiteral(start_position, end_pos, &ok);
125 return kPreParseSuccess;
129 PreParserExpression PreParserTraits::ParseClassLiteral(
130 PreParserIdentifier name, Scanner::Location class_name_location,
131 bool name_is_strict_reserved, int pos, bool* ok) {
132 return pre_parser_->ParseClassLiteral(name, class_name_location,
133 name_is_strict_reserved, pos, ok);
137 // Preparsing checks a JavaScript program and emits preparse-data that helps
138 // a later parsing to be faster.
139 // See preparser-data.h for the data.
141 // The PreParser checks that the syntax follows the grammar for JavaScript,
142 // and collects some information about the program along the way.
143 // The grammar check is only performed in order to understand the program
144 // sufficiently to deduce some information about it, that can be used
145 // to speed up later parsing. Finding errors is not the goal of pre-parsing,
146 // rather it is to speed up properly written and correct programs.
147 // That means that contextual checks (like a label being declared where
148 // it is used) are generally omitted.
151 PreParser::Statement PreParser::ParseStatementListItem(bool* ok) {
152 // ECMA 262 6th Edition
153 // StatementListItem[Yield, Return] :
154 // Statement[?Yield, ?Return]
155 // Declaration[?Yield]
157 // Declaration[Yield] :
158 // HoistableDeclaration[?Yield]
159 // ClassDeclaration[?Yield]
160 // LexicalDeclaration[In, ?Yield]
162 // HoistableDeclaration[Yield, Default] :
163 // FunctionDeclaration[?Yield, ?Default]
164 // GeneratorDeclaration[?Yield, ?Default]
166 // LexicalDeclaration[In, Yield] :
167 // LetOrConst BindingList[?In, ?Yield] ;
170 case Token::FUNCTION:
171 return ParseFunctionDeclaration(ok);
173 return ParseClassDeclaration(ok);
175 return ParseVariableStatement(kStatementListItem, ok);
177 if (is_strict(language_mode())) {
178 return ParseVariableStatement(kStatementListItem, ok);
182 return ParseStatement(ok);
187 void PreParser::ParseStatementList(int end_token, bool* ok) {
189 // (Statement)* <end_token>
191 bool directive_prologue = true;
192 while (peek() != end_token) {
193 if (directive_prologue && peek() != Token::STRING) {
194 directive_prologue = false;
196 Token::Value token = peek();
197 Scanner::Location old_super_loc = function_state_->super_call_location();
198 Statement statement = ParseStatementListItem(ok);
200 Scanner::Location super_loc = function_state_->super_call_location();
201 if (is_strong(language_mode()) &&
202 i::IsConstructor(function_state_->kind()) &&
203 !old_super_loc.IsValid() && super_loc.IsValid() &&
204 token != Token::SUPER) {
205 ReportMessageAt(super_loc, "strong_super_call_nested");
209 if (directive_prologue) {
210 if (statement.IsUseStrictLiteral()) {
211 scope_->SetLanguageMode(
212 static_cast<LanguageMode>(scope_->language_mode() | STRICT_BIT));
213 } else if (statement.IsUseStrongLiteral() && allow_strong_mode()) {
214 scope_->SetLanguageMode(static_cast<LanguageMode>(
215 scope_->language_mode() | STRICT_BIT | STRONG_BIT));
216 } else if (!statement.IsStringLiteral()) {
217 directive_prologue = false;
224 #define CHECK_OK ok); \
225 if (!*ok) return Statement::Default(); \
227 #define DUMMY ) // to make indentation work
231 PreParser::Statement PreParser::ParseStatement(bool* ok) {
235 return ParseSubStatement(ok);
239 PreParser::Statement PreParser::ParseSubStatement(bool* ok) {
244 // ExpressionStatement
246 // IterationStatement
257 // Note: Since labels can only be used by 'break' and 'continue'
258 // statements, which themselves are only valid within blocks,
259 // iterations or 'switch' statements (i.e., BreakableStatements),
260 // labels can be simply ignored in all other cases; except for
261 // trivial labeled break statements 'label: break label' which is
262 // parsed into an empty statement.
264 // Keep the source position of the statement
267 return ParseBlock(ok);
269 case Token::SEMICOLON:
270 if (is_strong(language_mode())) {
271 PreParserTraits::ReportMessageAt(scanner()->peek_location(),
274 return Statement::Default();
277 return Statement::Default();
280 return ParseIfStatement(ok);
283 return ParseDoWhileStatement(ok);
286 return ParseWhileStatement(ok);
289 return ParseForStatement(ok);
291 case Token::CONTINUE:
292 return ParseContinueStatement(ok);
295 return ParseBreakStatement(ok);
298 return ParseReturnStatement(ok);
301 return ParseWithStatement(ok);
304 return ParseSwitchStatement(ok);
307 return ParseThrowStatement(ok);
310 return ParseTryStatement(ok);
312 case Token::FUNCTION: {
313 Scanner::Location start_location = scanner()->peek_location();
314 Statement statement = ParseFunctionDeclaration(CHECK_OK);
315 Scanner::Location end_location = scanner()->location();
316 if (is_strict(language_mode())) {
317 PreParserTraits::ReportMessageAt(start_location.beg_pos,
318 end_location.end_pos,
321 return Statement::Default();
327 case Token::DEBUGGER:
328 return ParseDebuggerStatement(ok);
331 return ParseVariableStatement(kStatement, ok);
334 // In ES6 CONST is not allowed as a Statement, only as a
335 // LexicalDeclaration, however we continue to allow it in sloppy mode for
336 // backwards compatibility.
337 if (is_sloppy(language_mode())) {
338 return ParseVariableStatement(kStatement, ok);
343 return ParseExpressionOrLabelledStatement(ok);
348 PreParser::Statement PreParser::ParseFunctionDeclaration(bool* ok) {
349 // FunctionDeclaration ::
350 // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}'
351 // GeneratorDeclaration ::
352 // 'function' '*' Identifier '(' FormalParameterListopt ')'
353 // '{' FunctionBody '}'
354 Expect(Token::FUNCTION, CHECK_OK);
355 int pos = position();
356 bool is_generator = Check(Token::MUL);
357 bool is_strict_reserved = false;
358 Identifier name = ParseIdentifierOrStrictReservedWord(
359 &is_strict_reserved, CHECK_OK);
360 ParseFunctionLiteral(name, scanner()->location(), is_strict_reserved,
361 is_generator ? FunctionKind::kGeneratorFunction
362 : FunctionKind::kNormalFunction,
363 pos, FunctionLiteral::DECLARATION,
364 FunctionLiteral::NORMAL_ARITY, CHECK_OK);
365 return Statement::FunctionDeclaration();
369 PreParser::Statement PreParser::ParseClassDeclaration(bool* ok) {
370 Expect(Token::CLASS, CHECK_OK);
371 if (!allow_harmony_sloppy() && is_sloppy(language_mode())) {
372 ReportMessage("sloppy_lexical");
374 return Statement::Default();
377 int pos = position();
378 bool is_strict_reserved = false;
380 ParseIdentifierOrStrictReservedWord(&is_strict_reserved, CHECK_OK);
381 ParseClassLiteral(name, scanner()->location(), is_strict_reserved, pos,
383 return Statement::Default();
387 PreParser::Statement PreParser::ParseBlock(bool* ok) {
389 // '{' Statement* '}'
391 // Note that a Block does not introduce a new execution scope!
392 // (ECMA-262, 3rd, 12.2)
394 Expect(Token::LBRACE, CHECK_OK);
395 while (peek() != Token::RBRACE) {
396 if (is_strict(language_mode())) {
397 ParseStatementListItem(CHECK_OK);
399 ParseStatement(CHECK_OK);
402 Expect(Token::RBRACE, ok);
403 return Statement::Default();
407 PreParser::Statement PreParser::ParseVariableStatement(
408 VariableDeclarationContext var_context,
410 // VariableStatement ::
411 // VariableDeclarations ';'
413 Statement result = ParseVariableDeclarations(var_context,
417 ExpectSemicolon(CHECK_OK);
422 // If the variable declaration declares exactly one non-const
423 // variable, then *var is set to that variable. In all other cases,
424 // *var is untouched; in particular, it is the caller's responsibility
425 // to initialize it properly. This mechanism is also used for the parsing
426 // of 'for-in' loops.
427 PreParser::Statement PreParser::ParseVariableDeclarations(
428 VariableDeclarationContext var_context,
429 VariableDeclarationProperties* decl_props,
432 // VariableDeclarations ::
433 // ('var' | 'const') (Identifier ('=' AssignmentExpression)?)+[',']
435 // The ES6 Draft Rev3 specifies the following grammar for const declarations
437 // ConstDeclaration ::
438 // const ConstBinding (',' ConstBinding)* ';'
440 // Identifier '=' AssignmentExpression
444 // BindingPattern '=' AssignmentExpression
445 bool require_initializer = false;
446 bool is_strict_const = false;
447 if (peek() == Token::VAR) {
448 if (is_strong(language_mode())) {
449 Scanner::Location location = scanner()->peek_location();
450 ReportMessageAt(location, "strong_var");
452 return Statement::Default();
455 } else if (peek() == Token::CONST) {
456 // TODO(ES6): The ES6 Draft Rev4 section 12.2.2 reads:
458 // ConstDeclaration : const ConstBinding (',' ConstBinding)* ';'
460 // * It is a Syntax Error if the code that matches this production is not
461 // contained in extended code.
463 // However disallowing const in sloppy mode will break compatibility with
464 // existing pages. Therefore we keep allowing const with the old
465 // non-harmony semantics in sloppy mode.
466 Consume(Token::CONST);
467 if (is_strict(language_mode())) {
468 DCHECK(var_context != kStatement);
469 is_strict_const = true;
470 require_initializer = var_context != kForStatement;
472 } else if (peek() == Token::LET && is_strict(language_mode())) {
474 DCHECK(var_context != kStatement);
477 return Statement::Default();
480 // The scope of a var/const declared variable anywhere inside a function
481 // is the entire function (ECMA-262, 3rd, 10.1.3, and 12.2). The scope
482 // of a let declared variable is the scope of the immediately enclosing
484 int nvars = 0; // the number of variables declared
486 // Parse variable name.
487 if (nvars > 0) Consume(Token::COMMA);
488 ParseIdentifier(kDontAllowEvalOrArguments, CHECK_OK);
490 if (peek() == Token::ASSIGN || require_initializer ||
491 // require initializers for multiple consts.
492 (is_strict_const && peek() == Token::COMMA)) {
493 Expect(Token::ASSIGN, CHECK_OK);
494 ParseAssignmentExpression(var_context != kForStatement, CHECK_OK);
495 if (decl_props != NULL) *decl_props = kHasInitializers;
497 } while (peek() == Token::COMMA);
499 if (num_decl != NULL) *num_decl = nvars;
500 return Statement::Default();
504 PreParser::Statement PreParser::ParseExpressionOrLabelledStatement(bool* ok) {
505 // ExpressionStatement | LabelledStatement ::
507 // Identifier ':' Statement
510 case Token::FUNCTION:
512 UNREACHABLE(); // Always handled by the callers.
514 ReportUnexpectedToken(Next());
516 return Statement::Default();
518 // TODO(arv): Handle `let [`
519 // https://code.google.com/p/v8/issues/detail?id=3847
525 bool starts_with_identifier = peek_any_identifier();
526 Expression expr = ParseExpression(true, CHECK_OK);
527 // Even if the expression starts with an identifier, it is not necessarily an
528 // identifier. For example, "foo + bar" starts with an identifier but is not
530 if (starts_with_identifier && expr.IsIdentifier() && peek() == Token::COLON) {
531 // Expression is a single identifier, and not, e.g., a parenthesized
533 DCHECK(!expr.AsIdentifier().IsFutureReserved());
534 DCHECK(is_sloppy(language_mode()) ||
535 !IsFutureStrictReserved(expr.AsIdentifier()));
536 Consume(Token::COLON);
537 return ParseStatement(ok);
538 // Preparsing is disabled for extensions (because the extension details
539 // aren't passed to lazily compiled functions), so we don't
540 // accept "native function" in the preparser.
542 // Parsed expression statement.
543 // Detect attempts at 'let' declarations in sloppy mode.
544 if (peek() == Token::IDENTIFIER && is_sloppy(language_mode()) &&
545 expr.IsIdentifier() && expr.AsIdentifier().IsLet()) {
546 ReportMessage("sloppy_lexical", NULL);
548 return Statement::Default();
550 ExpectSemicolon(CHECK_OK);
551 return Statement::ExpressionStatement(expr);
555 PreParser::Statement PreParser::ParseIfStatement(bool* ok) {
557 // 'if' '(' Expression ')' Statement ('else' Statement)?
559 Expect(Token::IF, CHECK_OK);
560 Expect(Token::LPAREN, CHECK_OK);
561 ParseExpression(true, CHECK_OK);
562 Expect(Token::RPAREN, CHECK_OK);
563 ParseSubStatement(CHECK_OK);
564 if (peek() == Token::ELSE) {
566 ParseSubStatement(CHECK_OK);
568 return Statement::Default();
572 PreParser::Statement PreParser::ParseContinueStatement(bool* ok) {
573 // ContinueStatement ::
574 // 'continue' [no line terminator] Identifier? ';'
576 Expect(Token::CONTINUE, CHECK_OK);
577 Token::Value tok = peek();
578 if (!scanner()->HasAnyLineTerminatorBeforeNext() &&
579 tok != Token::SEMICOLON &&
580 tok != Token::RBRACE &&
582 // ECMA allows "eval" or "arguments" as labels even in strict mode.
583 ParseIdentifier(kAllowEvalOrArguments, CHECK_OK);
585 ExpectSemicolon(CHECK_OK);
586 return Statement::Default();
590 PreParser::Statement PreParser::ParseBreakStatement(bool* ok) {
592 // 'break' [no line terminator] Identifier? ';'
594 Expect(Token::BREAK, CHECK_OK);
595 Token::Value tok = peek();
596 if (!scanner()->HasAnyLineTerminatorBeforeNext() &&
597 tok != Token::SEMICOLON &&
598 tok != Token::RBRACE &&
600 // ECMA allows "eval" or "arguments" as labels even in strict mode.
601 ParseIdentifier(kAllowEvalOrArguments, CHECK_OK);
603 ExpectSemicolon(CHECK_OK);
604 return Statement::Default();
608 PreParser::Statement PreParser::ParseReturnStatement(bool* ok) {
609 // ReturnStatement ::
610 // 'return' [no line terminator] Expression? ';'
612 // Consume the return token. It is necessary to do before
613 // reporting any errors on it, because of the way errors are
614 // reported (underlining).
615 Expect(Token::RETURN, CHECK_OK);
616 function_state_->set_return_location(scanner()->location());
618 // An ECMAScript program is considered syntactically incorrect if it
619 // contains a return statement that is not within the body of a
620 // function. See ECMA-262, section 12.9, page 67.
621 // This is not handled during preparsing.
623 Token::Value tok = peek();
624 if (!scanner()->HasAnyLineTerminatorBeforeNext() &&
625 tok != Token::SEMICOLON &&
626 tok != Token::RBRACE &&
628 if (is_strong(language_mode()) &&
629 i::IsConstructor(function_state_->kind())) {
630 int pos = peek_position();
631 ReportMessageAt(Scanner::Location(pos, pos + 1),
632 "strong_constructor_return_value");
634 return Statement::Default();
636 ParseExpression(true, CHECK_OK);
638 ExpectSemicolon(CHECK_OK);
639 return Statement::Default();
643 PreParser::Statement PreParser::ParseWithStatement(bool* ok) {
645 // 'with' '(' Expression ')' Statement
646 Expect(Token::WITH, CHECK_OK);
647 if (is_strict(language_mode())) {
648 ReportMessageAt(scanner()->location(), "strict_mode_with");
650 return Statement::Default();
652 Expect(Token::LPAREN, CHECK_OK);
653 ParseExpression(true, CHECK_OK);
654 Expect(Token::RPAREN, CHECK_OK);
656 Scope* with_scope = NewScope(scope_, WITH_SCOPE);
657 BlockState block_state(&scope_, with_scope);
658 ParseSubStatement(CHECK_OK);
659 return Statement::Default();
663 PreParser::Statement PreParser::ParseSwitchStatement(bool* ok) {
664 // SwitchStatement ::
665 // 'switch' '(' Expression ')' '{' CaseClause* '}'
667 Expect(Token::SWITCH, CHECK_OK);
668 Expect(Token::LPAREN, CHECK_OK);
669 ParseExpression(true, CHECK_OK);
670 Expect(Token::RPAREN, CHECK_OK);
672 Expect(Token::LBRACE, CHECK_OK);
673 Token::Value token = peek();
674 while (token != Token::RBRACE) {
675 if (token == Token::CASE) {
676 Expect(Token::CASE, CHECK_OK);
677 ParseExpression(true, CHECK_OK);
679 Expect(Token::DEFAULT, CHECK_OK);
681 Expect(Token::COLON, CHECK_OK);
683 while (token != Token::CASE &&
684 token != Token::DEFAULT &&
685 token != Token::RBRACE) {
686 ParseStatementListItem(CHECK_OK);
690 Expect(Token::RBRACE, ok);
691 return Statement::Default();
695 PreParser::Statement PreParser::ParseDoWhileStatement(bool* ok) {
697 // 'do' Statement 'while' '(' Expression ')' ';'
699 Expect(Token::DO, CHECK_OK);
700 ParseSubStatement(CHECK_OK);
701 Expect(Token::WHILE, CHECK_OK);
702 Expect(Token::LPAREN, CHECK_OK);
703 ParseExpression(true, CHECK_OK);
704 Expect(Token::RPAREN, ok);
705 if (peek() == Token::SEMICOLON) Consume(Token::SEMICOLON);
706 return Statement::Default();
710 PreParser::Statement PreParser::ParseWhileStatement(bool* ok) {
712 // 'while' '(' Expression ')' Statement
714 Expect(Token::WHILE, CHECK_OK);
715 Expect(Token::LPAREN, CHECK_OK);
716 ParseExpression(true, CHECK_OK);
717 Expect(Token::RPAREN, CHECK_OK);
718 ParseSubStatement(ok);
719 return Statement::Default();
723 PreParser::Statement PreParser::ParseForStatement(bool* ok) {
725 // 'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement
727 Expect(Token::FOR, CHECK_OK);
728 Expect(Token::LPAREN, CHECK_OK);
729 bool is_let_identifier_expression = false;
730 if (peek() != Token::SEMICOLON) {
731 ForEachStatement::VisitMode visit_mode;
732 if (peek() == Token::VAR || peek() == Token::CONST ||
733 (peek() == Token::LET && is_strict(language_mode()))) {
734 bool is_lexical = peek() == Token::LET ||
735 (peek() == Token::CONST && is_strict(language_mode()));
737 VariableDeclarationProperties decl_props = kHasNoInitializers;
738 ParseVariableDeclarations(
739 kForStatement, &decl_props, &decl_count, CHECK_OK);
740 bool has_initializers = decl_props == kHasInitializers;
741 bool accept_IN = decl_count == 1 && !(is_lexical && has_initializers);
742 bool accept_OF = !has_initializers;
743 if (accept_IN && CheckInOrOf(accept_OF, &visit_mode, ok)) {
744 if (!*ok) return Statement::Default();
745 ParseExpression(true, CHECK_OK);
746 Expect(Token::RPAREN, CHECK_OK);
747 ParseSubStatement(CHECK_OK);
748 return Statement::Default();
751 Expression lhs = ParseExpression(false, CHECK_OK);
752 is_let_identifier_expression =
753 lhs.IsIdentifier() && lhs.AsIdentifier().IsLet();
754 if (CheckInOrOf(lhs.IsIdentifier(), &visit_mode, ok)) {
755 if (!*ok) return Statement::Default();
756 ParseExpression(true, CHECK_OK);
757 Expect(Token::RPAREN, CHECK_OK);
758 ParseSubStatement(CHECK_OK);
759 return Statement::Default();
764 // Parsed initializer at this point.
765 // Detect attempts at 'let' declarations in sloppy mode.
766 if (peek() == Token::IDENTIFIER && is_sloppy(language_mode()) &&
767 is_let_identifier_expression) {
768 ReportMessage("sloppy_lexical", NULL);
770 return Statement::Default();
772 Expect(Token::SEMICOLON, CHECK_OK);
774 if (peek() != Token::SEMICOLON) {
775 ParseExpression(true, CHECK_OK);
777 Expect(Token::SEMICOLON, CHECK_OK);
779 if (peek() != Token::RPAREN) {
780 ParseExpression(true, CHECK_OK);
782 Expect(Token::RPAREN, CHECK_OK);
784 ParseSubStatement(ok);
785 return Statement::Default();
789 PreParser::Statement PreParser::ParseThrowStatement(bool* ok) {
791 // 'throw' [no line terminator] Expression ';'
793 Expect(Token::THROW, CHECK_OK);
794 if (scanner()->HasAnyLineTerminatorBeforeNext()) {
795 ReportMessageAt(scanner()->location(), "newline_after_throw");
797 return Statement::Default();
799 ParseExpression(true, CHECK_OK);
801 return Statement::Default();
805 PreParser::Statement PreParser::ParseTryStatement(bool* ok) {
808 // 'try' Block Finally
809 // 'try' Block Catch Finally
812 // 'catch' '(' Identifier ')' Block
817 Expect(Token::TRY, CHECK_OK);
819 ParseBlock(CHECK_OK);
821 Token::Value tok = peek();
822 if (tok != Token::CATCH && tok != Token::FINALLY) {
823 ReportMessageAt(scanner()->location(), "no_catch_or_finally");
825 return Statement::Default();
827 if (tok == Token::CATCH) {
828 Consume(Token::CATCH);
829 Expect(Token::LPAREN, CHECK_OK);
830 ParseIdentifier(kDontAllowEvalOrArguments, CHECK_OK);
831 Expect(Token::RPAREN, CHECK_OK);
833 Scope* with_scope = NewScope(scope_, WITH_SCOPE);
834 BlockState block_state(&scope_, with_scope);
835 ParseBlock(CHECK_OK);
839 if (tok == Token::FINALLY) {
840 Consume(Token::FINALLY);
841 ParseBlock(CHECK_OK);
843 return Statement::Default();
847 PreParser::Statement PreParser::ParseDebuggerStatement(bool* ok) {
848 // In ECMA-262 'debugger' is defined as a reserved keyword. In some browser
849 // contexts this is used as a statement which invokes the debugger as if a
850 // break point is present.
851 // DebuggerStatement ::
854 Expect(Token::DEBUGGER, CHECK_OK);
856 return Statement::Default();
861 #define CHECK_OK ok); \
862 if (!*ok) return Expression::Default(); \
864 #define DUMMY ) // to make indentation work
868 PreParser::Expression PreParser::ParseFunctionLiteral(
869 Identifier function_name, Scanner::Location function_name_location,
870 bool name_is_strict_reserved, FunctionKind kind, int function_token_pos,
871 FunctionLiteral::FunctionType function_type,
872 FunctionLiteral::ArityRestriction arity_restriction, bool* ok) {
874 // '(' FormalParameterList? ')' '{' FunctionBody '}'
876 // Parse function body.
877 bool outer_is_script_scope = scope_->is_script_scope();
878 Scope* function_scope = NewScope(scope_, FUNCTION_SCOPE);
879 PreParserFactory factory(NULL);
880 FunctionState function_state(&function_state_, &scope_, function_scope, kind,
882 // FormalParameterList ::
883 // '(' (Identifier)*[','] ')'
884 Expect(Token::LPAREN, CHECK_OK);
885 int start_position = position();
886 DuplicateFinder duplicate_finder(scanner()->unicode_cache());
887 // We don't yet know if the function will be strict, so we cannot yet produce
888 // errors for parameter names or duplicates. However, we remember the
889 // locations of these errors if they occur and produce the errors later.
890 Scanner::Location eval_args_error_loc = Scanner::Location::invalid();
891 Scanner::Location dupe_error_loc = Scanner::Location::invalid();
892 Scanner::Location reserved_error_loc = Scanner::Location::invalid();
894 bool is_rest = false;
895 bool done = arity_restriction == FunctionLiteral::GETTER_ARITY ||
896 (peek() == Token::RPAREN &&
897 arity_restriction != FunctionLiteral::SETTER_ARITY);
899 bool is_strict_reserved = false;
900 is_rest = peek() == Token::ELLIPSIS && allow_harmony_rest_params();
902 Consume(Token::ELLIPSIS);
905 Identifier param_name =
906 ParseIdentifierOrStrictReservedWord(&is_strict_reserved, CHECK_OK);
907 if (!eval_args_error_loc.IsValid() && param_name.IsEvalOrArguments()) {
908 eval_args_error_loc = scanner()->location();
910 if (!reserved_error_loc.IsValid() && is_strict_reserved) {
911 reserved_error_loc = scanner()->location();
914 int prev_value = scanner()->FindSymbol(&duplicate_finder, 1);
916 if (!dupe_error_loc.IsValid() && prev_value != 0) {
917 dupe_error_loc = scanner()->location();
920 if (arity_restriction == FunctionLiteral::SETTER_ARITY) break;
921 done = (peek() == Token::RPAREN);
924 ReportMessageAt(scanner()->peek_location(), "param_after_rest");
926 return Expression::Default();
928 Expect(Token::COMMA, CHECK_OK);
931 Expect(Token::RPAREN, CHECK_OK);
933 // See Parser::ParseFunctionLiteral for more information about lazy parsing
934 // and lazy compilation.
935 bool is_lazily_parsed =
936 (outer_is_script_scope && allow_lazy() && !parenthesized_function_);
937 parenthesized_function_ = false;
939 Expect(Token::LBRACE, CHECK_OK);
940 if (is_lazily_parsed) {
941 ParseLazyFunctionLiteralBody(CHECK_OK);
943 ParseStatementList(Token::RBRACE, CHECK_OK);
945 Expect(Token::RBRACE, CHECK_OK);
947 // Validate name and parameter names. We can do this only after parsing the
948 // function, since the function can declare itself strict.
949 CheckFunctionName(language_mode(), kind, function_name,
950 name_is_strict_reserved, function_name_location, CHECK_OK);
951 const bool use_strict_params = is_rest || IsConciseMethod(kind);
952 CheckFunctionParameterNames(language_mode(), use_strict_params,
953 eval_args_error_loc, dupe_error_loc,
954 reserved_error_loc, CHECK_OK);
956 if (is_strict(language_mode())) {
957 int end_position = scanner()->location().end_pos;
958 CheckStrictOctalLiteral(start_position, end_position, CHECK_OK);
961 if (is_strong(language_mode()) && IsSubclassConstructor(kind)) {
962 if (!function_state.super_call_location().IsValid()) {
963 ReportMessageAt(function_name_location, "strong_super_call_missing",
966 return Expression::Default();
970 return Expression::Default();
974 void PreParser::ParseLazyFunctionLiteralBody(bool* ok) {
975 int body_start = position();
976 ParseStatementList(Token::RBRACE, ok);
979 // Position right after terminal '}'.
980 DCHECK_EQ(Token::RBRACE, scanner()->peek());
981 int body_end = scanner()->peek_location().end_pos;
982 log_->LogFunction(body_start, body_end,
983 function_state_->materialized_literal_count(),
984 function_state_->expected_property_count(), language_mode(),
985 scope_->uses_super_property());
989 PreParserExpression PreParser::ParseClassLiteral(
990 PreParserIdentifier name, Scanner::Location class_name_location,
991 bool name_is_strict_reserved, int pos, bool* ok) {
992 // All parts of a ClassDeclaration and ClassExpression are strict code.
993 if (name_is_strict_reserved) {
994 ReportMessageAt(class_name_location, "unexpected_strict_reserved");
996 return EmptyExpression();
998 if (IsEvalOrArguments(name)) {
999 ReportMessageAt(class_name_location, "strict_eval_arguments");
1001 return EmptyExpression();
1004 Scope* scope = NewScope(scope_, BLOCK_SCOPE);
1005 BlockState block_state(&scope_, scope);
1006 scope_->SetLanguageMode(
1007 static_cast<LanguageMode>(scope_->language_mode() | STRICT_BIT));
1008 // TODO(marja): Make PreParser use scope names too.
1009 // scope_->SetScopeName(name);
1011 bool has_extends = Check(Token::EXTENDS);
1013 ParseLeftHandSideExpression(CHECK_OK);
1016 ClassLiteralChecker checker(this);
1017 bool has_seen_constructor = false;
1019 Expect(Token::LBRACE, CHECK_OK);
1020 while (peek() != Token::RBRACE) {
1021 if (Check(Token::SEMICOLON)) continue;
1022 const bool in_class = true;
1023 const bool is_static = false;
1024 bool is_computed_name = false; // Classes do not care about computed
1025 // property names here.
1026 ParsePropertyDefinition(&checker, in_class, has_extends, is_static,
1027 &is_computed_name, &has_seen_constructor, CHECK_OK);
1030 Expect(Token::RBRACE, CHECK_OK);
1032 return Expression::Default();
1036 PreParser::Expression PreParser::ParseV8Intrinsic(bool* ok) {
1038 // '%' Identifier Arguments
1039 Expect(Token::MOD, CHECK_OK);
1040 if (!allow_natives()) {
1042 return Expression::Default();
1044 // Allow "eval" or "arguments" for backward compatibility.
1045 ParseIdentifier(kAllowEvalOrArguments, CHECK_OK);
1048 return Expression::Default();