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 "include/v8stdint.h"
9 #include "src/allocation.h"
10 #include "src/base/logging.h"
11 #include "src/conversions-inl.h"
12 #include "src/conversions.h"
13 #include "src/globals.h"
14 #include "src/hashmap.h"
16 #include "src/preparse-data.h"
17 #include "src/preparse-data-format.h"
18 #include "src/preparser.h"
19 #include "src/unicode.h"
20 #include "src/utils.h"
22 #if V8_LIBC_MSVCRT && (_MSC_VER < 1800)
25 // Usually defined in math.h, but not in MSVC until VS2013+.
27 int isfinite(double value);
35 class PreParserTraits::Checkpoint
36 : public ParserBase<PreParserTraits>::CheckpointBase {
38 explicit Checkpoint(ParserBase<PreParserTraits>* parser)
39 : ParserBase<PreParserTraits>::CheckpointBase(parser) {}
42 void PreParserTraits::ReportMessageAt(Scanner::Location location,
45 bool is_reference_error) {
46 ReportMessageAt(location.beg_pos,
54 void PreParserTraits::ReportMessageAt(int start_pos,
58 bool is_reference_error) {
59 pre_parser_->log_->LogMessage(start_pos, end_pos, message, arg,
64 PreParserIdentifier PreParserTraits::GetSymbol(Scanner* scanner) {
65 if (scanner->current_token() == Token::FUTURE_RESERVED_WORD) {
66 return PreParserIdentifier::FutureReserved();
67 } else if (scanner->current_token() ==
68 Token::FUTURE_STRICT_RESERVED_WORD) {
69 return PreParserIdentifier::FutureStrictReserved();
70 } else if (scanner->current_token() == Token::LET) {
71 return PreParserIdentifier::Let();
72 } else if (scanner->current_token() == Token::YIELD) {
73 return PreParserIdentifier::Yield();
75 if (scanner->UnescapedLiteralMatches("eval", 4)) {
76 return PreParserIdentifier::Eval();
78 if (scanner->UnescapedLiteralMatches("arguments", 9)) {
79 return PreParserIdentifier::Arguments();
81 if (scanner->UnescapedLiteralMatches("prototype", 9)) {
82 return PreParserIdentifier::Prototype();
84 if (scanner->UnescapedLiteralMatches("constructor", 11)) {
85 return PreParserIdentifier::Constructor();
87 return PreParserIdentifier::Default();
91 PreParserIdentifier PreParserTraits::GetNumberAsSymbol(Scanner* scanner) {
92 return PreParserIdentifier::Default();
96 PreParserExpression PreParserTraits::ExpressionFromString(
97 int pos, Scanner* scanner, PreParserFactory* factory) {
98 if (scanner->UnescapedLiteralMatches("use strict", 10)) {
99 return PreParserExpression::UseStrictStringLiteral();
101 return PreParserExpression::StringLiteral();
105 PreParserExpression PreParserTraits::ParseV8Intrinsic(bool* ok) {
106 return pre_parser_->ParseV8Intrinsic(ok);
110 PreParserExpression PreParserTraits::ParseFunctionLiteral(
111 PreParserIdentifier name, Scanner::Location function_name_location,
112 bool name_is_strict_reserved, FunctionKind kind,
113 int function_token_position, FunctionLiteral::FunctionType type,
114 FunctionLiteral::ArityRestriction arity_restriction, bool* ok) {
115 return pre_parser_->ParseFunctionLiteral(
116 name, function_name_location, name_is_strict_reserved, kind,
117 function_token_position, type, arity_restriction, ok);
121 PreParser::PreParseResult PreParser::PreParseLazyFunction(
122 StrictMode strict_mode, bool is_generator, ParserRecorder* log) {
124 // Lazy functions always have trivial outer scopes (no with/catch scopes).
125 PreParserScope top_scope(scope_, GLOBAL_SCOPE);
126 FunctionState top_state(&function_state_, &scope_, &top_scope, NULL,
127 this->ast_value_factory());
128 scope_->SetStrictMode(strict_mode);
129 PreParserScope function_scope(scope_, FUNCTION_SCOPE);
130 FunctionState function_state(&function_state_, &scope_, &function_scope, NULL,
131 this->ast_value_factory());
132 function_state.set_is_generator(is_generator);
133 DCHECK_EQ(Token::LBRACE, scanner()->current_token());
135 int start_position = peek_position();
136 ParseLazyFunctionLiteralBody(&ok);
137 if (stack_overflow()) return kPreParseStackOverflow;
139 ReportUnexpectedToken(scanner()->current_token());
141 DCHECK_EQ(Token::RBRACE, scanner()->peek());
142 if (scope_->strict_mode() == STRICT) {
143 int end_pos = scanner()->location().end_pos;
144 CheckOctalLiteral(start_position, end_pos, &ok);
147 return kPreParseSuccess;
151 // Preparsing checks a JavaScript program and emits preparse-data that helps
152 // a later parsing to be faster.
153 // See preparser-data.h for the data.
155 // The PreParser checks that the syntax follows the grammar for JavaScript,
156 // and collects some information about the program along the way.
157 // The grammar check is only performed in order to understand the program
158 // sufficiently to deduce some information about it, that can be used
159 // to speed up later parsing. Finding errors is not the goal of pre-parsing,
160 // rather it is to speed up properly written and correct programs.
161 // That means that contextual checks (like a label being declared where
162 // it is used) are generally omitted.
165 #define CHECK_OK ok); \
166 if (!*ok) return kUnknownSourceElements; \
168 #define DUMMY ) // to make indentation work
172 PreParser::Statement PreParser::ParseSourceElement(bool* ok) {
173 // (Ecma 262 5th Edition, clause 14):
176 // FunctionDeclaration
178 // In harmony mode we allow additionally the following productions
182 // GeneratorDeclaration
185 case Token::FUNCTION:
186 return ParseFunctionDeclaration(ok);
188 return ParseClassDeclaration(ok);
190 return ParseVariableStatement(kSourceElement, ok);
192 DCHECK(allow_harmony_scoping());
193 if (strict_mode() == STRICT) {
194 return ParseVariableStatement(kSourceElement, ok);
198 return ParseStatement(ok);
203 PreParser::SourceElements PreParser::ParseSourceElements(int end_token,
206 // (Statement)* <end_token>
208 bool directive_prologue = true;
209 while (peek() != end_token) {
210 if (directive_prologue && peek() != Token::STRING) {
211 directive_prologue = false;
213 Statement statement = ParseSourceElement(CHECK_OK);
214 if (directive_prologue) {
215 if (statement.IsUseStrictLiteral()) {
216 scope_->SetStrictMode(STRICT);
217 } else if (!statement.IsStringLiteral()) {
218 directive_prologue = false;
222 return kUnknownSourceElements;
227 #define CHECK_OK ok); \
228 if (!*ok) return Statement::Default(); \
230 #define DUMMY ) // to make indentation work
234 PreParser::Statement PreParser::ParseStatement(bool* ok) {
239 // ExpressionStatement
241 // IterationStatement
252 // Note: Since labels can only be used by 'break' and 'continue'
253 // statements, which themselves are only valid within blocks,
254 // iterations or 'switch' statements (i.e., BreakableStatements),
255 // labels can be simply ignored in all other cases; except for
256 // trivial labeled break statements 'label: break label' which is
257 // parsed into an empty statement.
259 // Keep the source position of the statement
262 return ParseBlock(ok);
264 case Token::SEMICOLON:
266 return Statement::Default();
269 return ParseIfStatement(ok);
272 return ParseDoWhileStatement(ok);
275 return ParseWhileStatement(ok);
278 return ParseForStatement(ok);
280 case Token::CONTINUE:
281 return ParseContinueStatement(ok);
284 return ParseBreakStatement(ok);
287 return ParseReturnStatement(ok);
290 return ParseWithStatement(ok);
293 return ParseSwitchStatement(ok);
296 return ParseThrowStatement(ok);
299 return ParseTryStatement(ok);
301 case Token::FUNCTION: {
302 Scanner::Location start_location = scanner()->peek_location();
303 Statement statement = ParseFunctionDeclaration(CHECK_OK);
304 Scanner::Location end_location = scanner()->location();
305 if (strict_mode() == STRICT) {
306 PreParserTraits::ReportMessageAt(start_location.beg_pos,
307 end_location.end_pos,
310 return Statement::Default();
317 return ParseClassDeclaration(CHECK_OK);
319 case Token::DEBUGGER:
320 return ParseDebuggerStatement(ok);
324 return ParseVariableStatement(kStatement, ok);
327 DCHECK(allow_harmony_scoping());
328 if (strict_mode() == STRICT) {
329 return ParseVariableStatement(kStatement, ok);
333 return ParseExpressionOrLabelledStatement(ok);
338 PreParser::Statement PreParser::ParseFunctionDeclaration(bool* ok) {
339 // FunctionDeclaration ::
340 // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}'
341 // GeneratorDeclaration ::
342 // 'function' '*' Identifier '(' FormalParameterListopt ')'
343 // '{' FunctionBody '}'
344 Expect(Token::FUNCTION, CHECK_OK);
345 int pos = position();
346 bool is_generator = Check(Token::MUL);
347 bool is_strict_reserved = false;
348 Identifier name = ParseIdentifierOrStrictReservedWord(
349 &is_strict_reserved, CHECK_OK);
350 ParseFunctionLiteral(name, scanner()->location(), is_strict_reserved,
351 is_generator ? FunctionKind::kGeneratorFunction
352 : FunctionKind::kNormalFunction,
353 pos, FunctionLiteral::DECLARATION,
354 FunctionLiteral::NORMAL_ARITY, CHECK_OK);
355 return Statement::FunctionDeclaration();
359 PreParser::Statement PreParser::ParseClassDeclaration(bool* ok) {
360 Expect(Token::CLASS, CHECK_OK);
361 int pos = position();
362 bool is_strict_reserved = false;
364 ParseIdentifierOrStrictReservedWord(&is_strict_reserved, CHECK_OK);
365 ParseClassLiteral(name, scanner()->location(), is_strict_reserved, pos,
367 return Statement::Default();
371 PreParser::Statement PreParser::ParseBlock(bool* ok) {
373 // '{' Statement* '}'
375 // Note that a Block does not introduce a new execution scope!
376 // (ECMA-262, 3rd, 12.2)
378 Expect(Token::LBRACE, CHECK_OK);
379 while (peek() != Token::RBRACE) {
380 if (allow_harmony_scoping() && strict_mode() == STRICT) {
381 ParseSourceElement(CHECK_OK);
383 ParseStatement(CHECK_OK);
386 Expect(Token::RBRACE, ok);
387 return Statement::Default();
391 PreParser::Statement PreParser::ParseVariableStatement(
392 VariableDeclarationContext var_context,
394 // VariableStatement ::
395 // VariableDeclarations ';'
397 Statement result = ParseVariableDeclarations(var_context,
401 ExpectSemicolon(CHECK_OK);
406 // If the variable declaration declares exactly one non-const
407 // variable, then *var is set to that variable. In all other cases,
408 // *var is untouched; in particular, it is the caller's responsibility
409 // to initialize it properly. This mechanism is also used for the parsing
410 // of 'for-in' loops.
411 PreParser::Statement PreParser::ParseVariableDeclarations(
412 VariableDeclarationContext var_context,
413 VariableDeclarationProperties* decl_props,
416 // VariableDeclarations ::
417 // ('var' | 'const') (Identifier ('=' AssignmentExpression)?)+[',']
419 // The ES6 Draft Rev3 specifies the following grammar for const declarations
421 // ConstDeclaration ::
422 // const ConstBinding (',' ConstBinding)* ';'
424 // Identifier '=' AssignmentExpression
428 // BindingPattern '=' AssignmentExpression
429 bool require_initializer = false;
430 if (peek() == Token::VAR) {
432 } else if (peek() == Token::CONST) {
433 // TODO(ES6): The ES6 Draft Rev4 section 12.2.2 reads:
435 // ConstDeclaration : const ConstBinding (',' ConstBinding)* ';'
437 // * It is a Syntax Error if the code that matches this production is not
438 // contained in extended code.
440 // However disallowing const in sloppy mode will break compatibility with
441 // existing pages. Therefore we keep allowing const with the old
442 // non-harmony semantics in sloppy mode.
443 Consume(Token::CONST);
444 if (strict_mode() == STRICT) {
445 if (allow_harmony_scoping()) {
446 if (var_context != kSourceElement && var_context != kForStatement) {
447 ReportMessageAt(scanner()->peek_location(), "unprotected_const");
449 return Statement::Default();
451 require_initializer = true;
453 Scanner::Location location = scanner()->peek_location();
454 ReportMessageAt(location, "strict_const");
456 return Statement::Default();
459 } else if (peek() == Token::LET && strict_mode() == STRICT) {
461 if (var_context != kSourceElement && var_context != kForStatement) {
462 ReportMessageAt(scanner()->peek_location(), "unprotected_let");
464 return Statement::Default();
468 return Statement::Default();
471 // The scope of a var/const declared variable anywhere inside a function
472 // is the entire function (ECMA-262, 3rd, 10.1.3, and 12.2). The scope
473 // of a let declared variable is the scope of the immediately enclosing
475 int nvars = 0; // the number of variables declared
477 // Parse variable name.
478 if (nvars > 0) Consume(Token::COMMA);
479 ParseIdentifier(kDontAllowEvalOrArguments, CHECK_OK);
481 if (peek() == Token::ASSIGN || require_initializer) {
482 Expect(Token::ASSIGN, CHECK_OK);
483 ParseAssignmentExpression(var_context != kForStatement, CHECK_OK);
484 if (decl_props != NULL) *decl_props = kHasInitializers;
486 } while (peek() == Token::COMMA);
488 if (num_decl != NULL) *num_decl = nvars;
489 return Statement::Default();
493 PreParser::Statement PreParser::ParseExpressionOrLabelledStatement(bool* ok) {
494 // ExpressionStatement | LabelledStatement ::
496 // Identifier ':' Statement
498 bool starts_with_identifier = peek_any_identifier();
499 Expression expr = ParseExpression(true, CHECK_OK);
500 // Even if the expression starts with an identifier, it is not necessarily an
501 // identifier. For example, "foo + bar" starts with an identifier but is not
503 if (starts_with_identifier && expr.IsIdentifier() && peek() == Token::COLON) {
504 // Expression is a single identifier, and not, e.g., a parenthesized
506 DCHECK(!expr.AsIdentifier().IsFutureReserved());
507 DCHECK(strict_mode() == SLOPPY ||
508 (!expr.AsIdentifier().IsFutureStrictReserved() &&
509 !expr.AsIdentifier().IsYield()));
510 Consume(Token::COLON);
511 return ParseStatement(ok);
512 // Preparsing is disabled for extensions (because the extension details
513 // aren't passed to lazily compiled functions), so we don't
514 // accept "native function" in the preparser.
516 // Parsed expression statement.
517 ExpectSemicolon(CHECK_OK);
518 return Statement::ExpressionStatement(expr);
522 PreParser::Statement PreParser::ParseIfStatement(bool* ok) {
524 // 'if' '(' Expression ')' Statement ('else' Statement)?
526 Expect(Token::IF, CHECK_OK);
527 Expect(Token::LPAREN, CHECK_OK);
528 ParseExpression(true, CHECK_OK);
529 Expect(Token::RPAREN, CHECK_OK);
530 ParseStatement(CHECK_OK);
531 if (peek() == Token::ELSE) {
533 ParseStatement(CHECK_OK);
535 return Statement::Default();
539 PreParser::Statement PreParser::ParseContinueStatement(bool* ok) {
540 // ContinueStatement ::
541 // 'continue' [no line terminator] Identifier? ';'
543 Expect(Token::CONTINUE, CHECK_OK);
544 Token::Value tok = peek();
545 if (!scanner()->HasAnyLineTerminatorBeforeNext() &&
546 tok != Token::SEMICOLON &&
547 tok != Token::RBRACE &&
549 // ECMA allows "eval" or "arguments" as labels even in strict mode.
550 ParseIdentifier(kAllowEvalOrArguments, CHECK_OK);
552 ExpectSemicolon(CHECK_OK);
553 return Statement::Default();
557 PreParser::Statement PreParser::ParseBreakStatement(bool* ok) {
559 // 'break' [no line terminator] Identifier? ';'
561 Expect(Token::BREAK, CHECK_OK);
562 Token::Value tok = peek();
563 if (!scanner()->HasAnyLineTerminatorBeforeNext() &&
564 tok != Token::SEMICOLON &&
565 tok != Token::RBRACE &&
567 // ECMA allows "eval" or "arguments" as labels even in strict mode.
568 ParseIdentifier(kAllowEvalOrArguments, CHECK_OK);
570 ExpectSemicolon(CHECK_OK);
571 return Statement::Default();
575 PreParser::Statement PreParser::ParseReturnStatement(bool* ok) {
576 // ReturnStatement ::
577 // 'return' [no line terminator] Expression? ';'
579 // Consume the return token. It is necessary to do before
580 // reporting any errors on it, because of the way errors are
581 // reported (underlining).
582 Expect(Token::RETURN, CHECK_OK);
584 // An ECMAScript program is considered syntactically incorrect if it
585 // contains a return statement that is not within the body of a
586 // function. See ECMA-262, section 12.9, page 67.
587 // This is not handled during preparsing.
589 Token::Value tok = peek();
590 if (!scanner()->HasAnyLineTerminatorBeforeNext() &&
591 tok != Token::SEMICOLON &&
592 tok != Token::RBRACE &&
594 ParseExpression(true, CHECK_OK);
596 ExpectSemicolon(CHECK_OK);
597 return Statement::Default();
601 PreParser::Statement PreParser::ParseWithStatement(bool* ok) {
603 // 'with' '(' Expression ')' Statement
604 Expect(Token::WITH, CHECK_OK);
605 if (strict_mode() == STRICT) {
606 ReportMessageAt(scanner()->location(), "strict_mode_with");
608 return Statement::Default();
610 Expect(Token::LPAREN, CHECK_OK);
611 ParseExpression(true, CHECK_OK);
612 Expect(Token::RPAREN, CHECK_OK);
614 PreParserScope with_scope(scope_, WITH_SCOPE);
615 BlockState block_state(&scope_, &with_scope);
616 ParseStatement(CHECK_OK);
617 return Statement::Default();
621 PreParser::Statement PreParser::ParseSwitchStatement(bool* ok) {
622 // SwitchStatement ::
623 // 'switch' '(' Expression ')' '{' CaseClause* '}'
625 Expect(Token::SWITCH, CHECK_OK);
626 Expect(Token::LPAREN, CHECK_OK);
627 ParseExpression(true, CHECK_OK);
628 Expect(Token::RPAREN, CHECK_OK);
630 Expect(Token::LBRACE, CHECK_OK);
631 Token::Value token = peek();
632 while (token != Token::RBRACE) {
633 if (token == Token::CASE) {
634 Expect(Token::CASE, CHECK_OK);
635 ParseExpression(true, CHECK_OK);
637 Expect(Token::DEFAULT, CHECK_OK);
639 Expect(Token::COLON, CHECK_OK);
641 while (token != Token::CASE &&
642 token != Token::DEFAULT &&
643 token != Token::RBRACE) {
644 ParseStatement(CHECK_OK);
648 Expect(Token::RBRACE, ok);
649 return Statement::Default();
653 PreParser::Statement PreParser::ParseDoWhileStatement(bool* ok) {
655 // 'do' Statement 'while' '(' Expression ')' ';'
657 Expect(Token::DO, CHECK_OK);
658 ParseStatement(CHECK_OK);
659 Expect(Token::WHILE, CHECK_OK);
660 Expect(Token::LPAREN, CHECK_OK);
661 ParseExpression(true, CHECK_OK);
662 Expect(Token::RPAREN, ok);
663 if (peek() == Token::SEMICOLON) Consume(Token::SEMICOLON);
664 return Statement::Default();
668 PreParser::Statement PreParser::ParseWhileStatement(bool* ok) {
670 // 'while' '(' Expression ')' Statement
672 Expect(Token::WHILE, CHECK_OK);
673 Expect(Token::LPAREN, CHECK_OK);
674 ParseExpression(true, CHECK_OK);
675 Expect(Token::RPAREN, CHECK_OK);
677 return Statement::Default();
681 bool PreParser::CheckInOrOf(bool accept_OF) {
682 if (Check(Token::IN) ||
683 (accept_OF && CheckContextualKeyword(CStrVector("of")))) {
690 PreParser::Statement PreParser::ParseForStatement(bool* ok) {
692 // 'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement
694 Expect(Token::FOR, CHECK_OK);
695 Expect(Token::LPAREN, CHECK_OK);
696 if (peek() != Token::SEMICOLON) {
697 if (peek() == Token::VAR || peek() == Token::CONST ||
698 (peek() == Token::LET && strict_mode() == STRICT)) {
699 bool is_let = peek() == Token::LET;
701 VariableDeclarationProperties decl_props = kHasNoInitializers;
702 ParseVariableDeclarations(
703 kForStatement, &decl_props, &decl_count, CHECK_OK);
704 bool has_initializers = decl_props == kHasInitializers;
705 bool accept_IN = decl_count == 1 && !(is_let && has_initializers);
706 bool accept_OF = !has_initializers;
707 if (accept_IN && CheckInOrOf(accept_OF)) {
708 ParseExpression(true, CHECK_OK);
709 Expect(Token::RPAREN, CHECK_OK);
711 ParseStatement(CHECK_OK);
712 return Statement::Default();
715 Expression lhs = ParseExpression(false, CHECK_OK);
716 if (CheckInOrOf(lhs.IsIdentifier())) {
717 ParseExpression(true, CHECK_OK);
718 Expect(Token::RPAREN, CHECK_OK);
720 ParseStatement(CHECK_OK);
721 return Statement::Default();
726 // Parsed initializer at this point.
727 Expect(Token::SEMICOLON, CHECK_OK);
729 if (peek() != Token::SEMICOLON) {
730 ParseExpression(true, CHECK_OK);
732 Expect(Token::SEMICOLON, CHECK_OK);
734 if (peek() != Token::RPAREN) {
735 ParseExpression(true, CHECK_OK);
737 Expect(Token::RPAREN, CHECK_OK);
740 return Statement::Default();
744 PreParser::Statement PreParser::ParseThrowStatement(bool* ok) {
746 // 'throw' [no line terminator] Expression ';'
748 Expect(Token::THROW, CHECK_OK);
749 if (scanner()->HasAnyLineTerminatorBeforeNext()) {
750 ReportMessageAt(scanner()->location(), "newline_after_throw");
752 return Statement::Default();
754 ParseExpression(true, CHECK_OK);
756 return Statement::Default();
760 PreParser::Statement PreParser::ParseTryStatement(bool* ok) {
763 // 'try' Block Finally
764 // 'try' Block Catch Finally
767 // 'catch' '(' Identifier ')' Block
772 Expect(Token::TRY, CHECK_OK);
774 ParseBlock(CHECK_OK);
776 Token::Value tok = peek();
777 if (tok != Token::CATCH && tok != Token::FINALLY) {
778 ReportMessageAt(scanner()->location(), "no_catch_or_finally");
780 return Statement::Default();
782 if (tok == Token::CATCH) {
783 Consume(Token::CATCH);
784 Expect(Token::LPAREN, CHECK_OK);
785 ParseIdentifier(kDontAllowEvalOrArguments, CHECK_OK);
786 Expect(Token::RPAREN, CHECK_OK);
788 PreParserScope with_scope(scope_, WITH_SCOPE);
789 BlockState block_state(&scope_, &with_scope);
790 ParseBlock(CHECK_OK);
794 if (tok == Token::FINALLY) {
795 Consume(Token::FINALLY);
796 ParseBlock(CHECK_OK);
798 return Statement::Default();
802 PreParser::Statement PreParser::ParseDebuggerStatement(bool* ok) {
803 // In ECMA-262 'debugger' is defined as a reserved keyword. In some browser
804 // contexts this is used as a statement which invokes the debugger as if a
805 // break point is present.
806 // DebuggerStatement ::
809 Expect(Token::DEBUGGER, CHECK_OK);
811 return Statement::Default();
816 #define CHECK_OK ok); \
817 if (!*ok) return Expression::Default(); \
819 #define DUMMY ) // to make indentation work
823 PreParser::Expression PreParser::ParseFunctionLiteral(
824 Identifier function_name, Scanner::Location function_name_location,
825 bool name_is_strict_reserved, FunctionKind kind, int function_token_pos,
826 FunctionLiteral::FunctionType function_type,
827 FunctionLiteral::ArityRestriction arity_restriction, bool* ok) {
829 // '(' FormalParameterList? ')' '{' FunctionBody '}'
831 // Parse function body.
832 ScopeType outer_scope_type = scope_->type();
833 PreParserScope function_scope(scope_, FUNCTION_SCOPE);
834 FunctionState function_state(&function_state_, &scope_, &function_scope, NULL,
835 this->ast_value_factory());
836 function_state.set_is_generator(IsGeneratorFunction(kind));
837 // FormalParameterList ::
838 // '(' (Identifier)*[','] ')'
839 Expect(Token::LPAREN, CHECK_OK);
840 int start_position = position();
841 DuplicateFinder duplicate_finder(scanner()->unicode_cache());
842 // We don't yet know if the function will be strict, so we cannot yet produce
843 // errors for parameter names or duplicates. However, we remember the
844 // locations of these errors if they occur and produce the errors later.
845 Scanner::Location eval_args_error_loc = Scanner::Location::invalid();
846 Scanner::Location dupe_error_loc = Scanner::Location::invalid();
847 Scanner::Location reserved_error_loc = Scanner::Location::invalid();
849 bool done = arity_restriction == FunctionLiteral::GETTER_ARITY ||
850 (peek() == Token::RPAREN &&
851 arity_restriction != FunctionLiteral::SETTER_ARITY);
853 bool is_strict_reserved = false;
854 Identifier param_name =
855 ParseIdentifierOrStrictReservedWord(&is_strict_reserved, CHECK_OK);
856 if (!eval_args_error_loc.IsValid() && param_name.IsEvalOrArguments()) {
857 eval_args_error_loc = scanner()->location();
859 if (!reserved_error_loc.IsValid() && is_strict_reserved) {
860 reserved_error_loc = scanner()->location();
863 int prev_value = scanner()->FindSymbol(&duplicate_finder, 1);
865 if (!dupe_error_loc.IsValid() && prev_value != 0) {
866 dupe_error_loc = scanner()->location();
869 if (arity_restriction == FunctionLiteral::SETTER_ARITY) break;
870 done = (peek() == Token::RPAREN);
871 if (!done) Expect(Token::COMMA, CHECK_OK);
873 Expect(Token::RPAREN, CHECK_OK);
875 // See Parser::ParseFunctionLiteral for more information about lazy parsing
876 // and lazy compilation.
877 bool is_lazily_parsed = (outer_scope_type == GLOBAL_SCOPE && allow_lazy() &&
878 !parenthesized_function_);
879 parenthesized_function_ = false;
881 Expect(Token::LBRACE, CHECK_OK);
882 if (is_lazily_parsed) {
883 ParseLazyFunctionLiteralBody(CHECK_OK);
885 ParseSourceElements(Token::RBRACE, ok);
887 Expect(Token::RBRACE, CHECK_OK);
889 // Validate strict mode. We can do this only after parsing the function,
890 // since the function can declare itself strict.
891 // Concise methods use StrictFormalParameters.
892 if (strict_mode() == STRICT || IsConciseMethod(kind)) {
893 if (function_name.IsEvalOrArguments()) {
894 ReportMessageAt(function_name_location, "strict_eval_arguments");
896 return Expression::Default();
898 if (name_is_strict_reserved) {
899 ReportMessageAt(function_name_location, "unexpected_strict_reserved");
901 return Expression::Default();
903 if (eval_args_error_loc.IsValid()) {
904 ReportMessageAt(eval_args_error_loc, "strict_eval_arguments");
906 return Expression::Default();
908 if (dupe_error_loc.IsValid()) {
909 ReportMessageAt(dupe_error_loc, "strict_param_dupe");
911 return Expression::Default();
913 if (reserved_error_loc.IsValid()) {
914 ReportMessageAt(reserved_error_loc, "unexpected_strict_reserved");
916 return Expression::Default();
919 int end_position = scanner()->location().end_pos;
920 CheckOctalLiteral(start_position, end_position, CHECK_OK);
923 return Expression::Default();
927 void PreParser::ParseLazyFunctionLiteralBody(bool* ok) {
928 int body_start = position();
929 ParseSourceElements(Token::RBRACE, ok);
932 // Position right after terminal '}'.
933 DCHECK_EQ(Token::RBRACE, scanner()->peek());
934 int body_end = scanner()->peek_location().end_pos;
935 log_->LogFunction(body_start, body_end,
936 function_state_->materialized_literal_count(),
937 function_state_->expected_property_count(),
942 PreParser::Expression PreParser::ParseV8Intrinsic(bool* ok) {
944 // '%' Identifier Arguments
945 Expect(Token::MOD, CHECK_OK);
946 if (!allow_natives_syntax()) {
948 return Expression::Default();
950 // Allow "eval" or "arguments" for backward compatibility.
951 ParseIdentifier(kAllowEvalOrArguments, CHECK_OK);
954 return Expression::Default();