blob: 3173cc0f900255aa1d764736d8c6f70d51da0ddb [file] [log] [blame]
Ben Murdoch257744e2011-11-30 15:57:28 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include <cmath>
Steve Block44f0eee2011-05-26 01:26:41 +01006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "include/v8stdint.h"
Ben Murdoch589d6972011-11-30 16:04:58 +00008
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#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"
15#include "src/list.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"
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000021
Ben Murdochb8a8cc12014-11-26 15:28:44 +000022#if V8_LIBC_MSVCRT && (_MSC_VER < 1800)
23namespace std {
Ben Murdoch589d6972011-11-30 16:04:58 +000024
Ben Murdochb8a8cc12014-11-26 15:28:44 +000025// Usually defined in math.h, but not in MSVC until VS2013+.
Ben Murdoch589d6972011-11-30 16:04:58 +000026// Abstracted to work
27int isfinite(double value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028
29} // namespace std
Ben Murdoch589d6972011-11-30 16:04:58 +000030#endif
31
Ben Murdochb8a8cc12014-11-26 15:28:44 +000032namespace v8 {
33namespace internal {
34
35class PreParserTraits::Checkpoint
36 : public ParserBase<PreParserTraits>::CheckpointBase {
37 public:
38 explicit Checkpoint(ParserBase<PreParserTraits>* parser)
39 : ParserBase<PreParserTraits>::CheckpointBase(parser) {}
40};
41
42void PreParserTraits::ReportMessageAt(Scanner::Location location,
43 const char* message,
44 const char* arg,
45 bool is_reference_error) {
46 ReportMessageAt(location.beg_pos,
47 location.end_pos,
48 message,
49 arg,
50 is_reference_error);
51}
52
53
54void PreParserTraits::ReportMessageAt(int start_pos,
55 int end_pos,
56 const char* message,
57 const char* arg,
58 bool is_reference_error) {
59 pre_parser_->log_->LogMessage(start_pos, end_pos, message, arg,
60 is_reference_error);
61}
62
63
64PreParserIdentifier 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();
74 }
75 if (scanner->UnescapedLiteralMatches("eval", 4)) {
76 return PreParserIdentifier::Eval();
77 }
78 if (scanner->UnescapedLiteralMatches("arguments", 9)) {
79 return PreParserIdentifier::Arguments();
80 }
81 if (scanner->UnescapedLiteralMatches("prototype", 9)) {
82 return PreParserIdentifier::Prototype();
83 }
84 if (scanner->UnescapedLiteralMatches("constructor", 11)) {
85 return PreParserIdentifier::Constructor();
86 }
87 return PreParserIdentifier::Default();
88}
89
90
91PreParserIdentifier PreParserTraits::GetNumberAsSymbol(Scanner* scanner) {
92 return PreParserIdentifier::Default();
93}
94
95
96PreParserExpression PreParserTraits::ExpressionFromString(
97 int pos, Scanner* scanner, PreParserFactory* factory) {
98 if (scanner->UnescapedLiteralMatches("use strict", 10)) {
99 return PreParserExpression::UseStrictStringLiteral();
100 }
101 return PreParserExpression::StringLiteral();
102}
103
104
105PreParserExpression PreParserTraits::ParseV8Intrinsic(bool* ok) {
106 return pre_parser_->ParseV8Intrinsic(ok);
107}
108
109
110PreParserExpression 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);
118}
119
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800120
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100121PreParser::PreParseResult PreParser::PreParseLazyFunction(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000122 StrictMode strict_mode, bool is_generator, ParserRecorder* log) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100123 log_ = log;
124 // Lazy functions always have trivial outer scopes (no with/catch scopes).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000125 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());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100134 bool ok = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000135 int start_position = peek_position();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100136 ParseLazyFunctionLiteralBody(&ok);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000137 if (stack_overflow()) return kPreParseStackOverflow;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100138 if (!ok) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000139 ReportUnexpectedToken(scanner()->current_token());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100140 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000141 DCHECK_EQ(Token::RBRACE, scanner()->peek());
142 if (scope_->strict_mode() == STRICT) {
143 int end_pos = scanner()->location().end_pos;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100144 CheckOctalLiteral(start_position, end_pos, &ok);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100145 }
146 }
147 return kPreParseSuccess;
148}
149
150
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800151// 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.
154
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.
163
Ben Murdoch257744e2011-11-30 15:57:28 +0000164
165#define CHECK_OK ok); \
166 if (!*ok) return kUnknownSourceElements; \
167 ((void)0
168#define DUMMY ) // to make indentation work
169#undef DUMMY
170
171
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000172PreParser::Statement PreParser::ParseSourceElement(bool* ok) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100173 // (Ecma 262 5th Edition, clause 14):
174 // SourceElement:
175 // Statement
176 // FunctionDeclaration
177 //
178 // In harmony mode we allow additionally the following productions
179 // SourceElement:
180 // LetDeclaration
181 // ConstDeclaration
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000182 // GeneratorDeclaration
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100183
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000184 switch (peek()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000185 case Token::FUNCTION:
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100186 return ParseFunctionDeclaration(ok);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000187 case Token::CLASS:
188 return ParseClassDeclaration(ok);
189 case Token::CONST:
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000190 return ParseVariableStatement(kSourceElement, ok);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000191 case Token::LET:
192 DCHECK(allow_harmony_scoping());
193 if (strict_mode() == STRICT) {
194 return ParseVariableStatement(kSourceElement, ok);
195 }
196 // Fall through.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000197 default:
198 return ParseStatement(ok);
199 }
200}
201
202
Ben Murdochb0fe1622011-05-05 13:52:32 +0100203PreParser::SourceElements PreParser::ParseSourceElements(int end_token,
204 bool* ok) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800205 // SourceElements ::
206 // (Statement)* <end_token>
207
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000208 bool directive_prologue = true;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800209 while (peek() != end_token) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000210 if (directive_prologue && peek() != Token::STRING) {
211 directive_prologue = false;
212 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000213 Statement statement = ParseSourceElement(CHECK_OK);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000214 if (directive_prologue) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000215 if (statement.IsUseStrictLiteral()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000216 scope_->SetStrictMode(STRICT);
Ben Murdoch257744e2011-11-30 15:57:28 +0000217 } else if (!statement.IsStringLiteral()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000218 directive_prologue = false;
Ben Murdoch257744e2011-11-30 15:57:28 +0000219 }
220 }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800221 }
222 return kUnknownSourceElements;
223}
224
225
Ben Murdoch257744e2011-11-30 15:57:28 +0000226#undef CHECK_OK
227#define CHECK_OK ok); \
228 if (!*ok) return Statement::Default(); \
229 ((void)0
230#define DUMMY ) // to make indentation work
231#undef DUMMY
232
233
Ben Murdochb0fe1622011-05-05 13:52:32 +0100234PreParser::Statement PreParser::ParseStatement(bool* ok) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800235 // Statement ::
236 // Block
237 // VariableStatement
238 // EmptyStatement
239 // ExpressionStatement
240 // IfStatement
241 // IterationStatement
242 // ContinueStatement
243 // BreakStatement
244 // ReturnStatement
245 // WithStatement
246 // LabelledStatement
247 // SwitchStatement
248 // ThrowStatement
249 // TryStatement
250 // DebuggerStatement
251
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.
258
259 // Keep the source position of the statement
260 switch (peek()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000261 case Token::LBRACE:
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800262 return ParseBlock(ok);
263
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000264 case Token::SEMICOLON:
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800265 Next();
Ben Murdoch257744e2011-11-30 15:57:28 +0000266 return Statement::Default();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800267
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000268 case Token::IF:
Ben Murdoch257744e2011-11-30 15:57:28 +0000269 return ParseIfStatement(ok);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800270
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000271 case Token::DO:
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800272 return ParseDoWhileStatement(ok);
273
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000274 case Token::WHILE:
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800275 return ParseWhileStatement(ok);
276
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000277 case Token::FOR:
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800278 return ParseForStatement(ok);
279
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000280 case Token::CONTINUE:
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800281 return ParseContinueStatement(ok);
282
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000283 case Token::BREAK:
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800284 return ParseBreakStatement(ok);
285
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000286 case Token::RETURN:
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800287 return ParseReturnStatement(ok);
288
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000289 case Token::WITH:
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800290 return ParseWithStatement(ok);
291
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000292 case Token::SWITCH:
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800293 return ParseSwitchStatement(ok);
294
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000295 case Token::THROW:
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800296 return ParseThrowStatement(ok);
297
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000298 case Token::TRY:
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800299 return ParseTryStatement(ok);
300
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000301 case Token::FUNCTION: {
302 Scanner::Location start_location = scanner()->peek_location();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100303 Statement statement = ParseFunctionDeclaration(CHECK_OK);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000304 Scanner::Location end_location = scanner()->location();
305 if (strict_mode() == STRICT) {
306 PreParserTraits::ReportMessageAt(start_location.beg_pos,
307 end_location.end_pos,
308 "strict_function");
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100309 *ok = false;
310 return Statement::Default();
311 } else {
312 return statement;
313 }
314 }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800315
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000316 case Token::CLASS:
317 return ParseClassDeclaration(CHECK_OK);
318
319 case Token::DEBUGGER:
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800320 return ParseDebuggerStatement(ok);
321
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000322 case Token::VAR:
323 case Token::CONST:
324 return ParseVariableStatement(kStatement, ok);
325
326 case Token::LET:
327 DCHECK(allow_harmony_scoping());
328 if (strict_mode() == STRICT) {
329 return ParseVariableStatement(kStatement, ok);
330 }
331 // Fall through.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800332 default:
333 return ParseExpressionOrLabelledStatement(ok);
334 }
335}
336
337
Ben Murdochb0fe1622011-05-05 13:52:32 +0100338PreParser::Statement PreParser::ParseFunctionDeclaration(bool* ok) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800339 // FunctionDeclaration ::
340 // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}'
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000341 // 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);
Ben Murdoch257744e2011-11-30 15:57:28 +0000355 return Statement::FunctionDeclaration();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800356}
357
358
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000359PreParser::Statement PreParser::ParseClassDeclaration(bool* ok) {
360 Expect(Token::CLASS, CHECK_OK);
361 int pos = position();
362 bool is_strict_reserved = false;
363 Identifier name =
364 ParseIdentifierOrStrictReservedWord(&is_strict_reserved, CHECK_OK);
365 ParseClassLiteral(name, scanner()->location(), is_strict_reserved, pos,
366 CHECK_OK);
367 return Statement::Default();
368}
369
370
Ben Murdochb0fe1622011-05-05 13:52:32 +0100371PreParser::Statement PreParser::ParseBlock(bool* ok) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800372 // Block ::
373 // '{' Statement* '}'
374
375 // Note that a Block does not introduce a new execution scope!
376 // (ECMA-262, 3rd, 12.2)
377 //
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000378 Expect(Token::LBRACE, CHECK_OK);
379 while (peek() != Token::RBRACE) {
380 if (allow_harmony_scoping() && strict_mode() == STRICT) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100381 ParseSourceElement(CHECK_OK);
382 } else {
383 ParseStatement(CHECK_OK);
Ben Murdoch257744e2011-11-30 15:57:28 +0000384 }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800385 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000386 Expect(Token::RBRACE, ok);
Ben Murdoch257744e2011-11-30 15:57:28 +0000387 return Statement::Default();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800388}
389
390
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000391PreParser::Statement PreParser::ParseVariableStatement(
392 VariableDeclarationContext var_context,
393 bool* ok) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800394 // VariableStatement ::
395 // VariableDeclarations ';'
396
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000397 Statement result = ParseVariableDeclarations(var_context,
398 NULL,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100399 NULL,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000400 CHECK_OK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800401 ExpectSemicolon(CHECK_OK);
402 return result;
403}
404
405
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.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000411PreParser::Statement PreParser::ParseVariableDeclarations(
412 VariableDeclarationContext var_context,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100413 VariableDeclarationProperties* decl_props,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000414 int* num_decl,
415 bool* ok) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800416 // VariableDeclarations ::
417 // ('var' | 'const') (Identifier ('=' AssignmentExpression)?)+[',']
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100418 //
419 // The ES6 Draft Rev3 specifies the following grammar for const declarations
420 //
421 // ConstDeclaration ::
422 // const ConstBinding (',' ConstBinding)* ';'
423 // ConstBinding ::
424 // Identifier '=' AssignmentExpression
425 //
426 // TODO(ES6):
427 // ConstBinding ::
428 // BindingPattern '=' AssignmentExpression
429 bool require_initializer = false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000430 if (peek() == Token::VAR) {
431 Consume(Token::VAR);
432 } else if (peek() == Token::CONST) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100433 // TODO(ES6): The ES6 Draft Rev4 section 12.2.2 reads:
434 //
435 // ConstDeclaration : const ConstBinding (',' ConstBinding)* ';'
436 //
437 // * It is a Syntax Error if the code that matches this production is not
438 // contained in extended code.
439 //
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000440 // However disallowing const in sloppy mode will break compatibility with
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100441 // existing pages. Therefore we keep allowing const with the old
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000442 // 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");
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100448 *ok = false;
449 return Statement::Default();
450 }
451 require_initializer = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000452 } else {
453 Scanner::Location location = scanner()->peek_location();
454 ReportMessageAt(location, "strict_const");
455 *ok = false;
456 return Statement::Default();
457 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100458 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000459 } else if (peek() == Token::LET && strict_mode() == STRICT) {
460 Consume(Token::LET);
461 if (var_context != kSourceElement && var_context != kForStatement) {
462 ReportMessageAt(scanner()->peek_location(), "unprotected_let");
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000463 *ok = false;
464 return Statement::Default();
465 }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800466 } else {
467 *ok = false;
Ben Murdoch257744e2011-11-30 15:57:28 +0000468 return Statement::Default();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800469 }
470
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000471 // 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
474 // block.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800475 int nvars = 0; // the number of variables declared
476 do {
477 // Parse variable name.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000478 if (nvars > 0) Consume(Token::COMMA);
479 ParseIdentifier(kDontAllowEvalOrArguments, CHECK_OK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800480 nvars++;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000481 if (peek() == Token::ASSIGN || require_initializer) {
482 Expect(Token::ASSIGN, CHECK_OK);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000483 ParseAssignmentExpression(var_context != kForStatement, CHECK_OK);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100484 if (decl_props != NULL) *decl_props = kHasInitializers;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800485 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000486 } while (peek() == Token::COMMA);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800487
488 if (num_decl != NULL) *num_decl = nvars;
Ben Murdoch257744e2011-11-30 15:57:28 +0000489 return Statement::Default();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800490}
491
492
Ben Murdoch257744e2011-11-30 15:57:28 +0000493PreParser::Statement PreParser::ParseExpressionOrLabelledStatement(bool* ok) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800494 // ExpressionStatement | LabelledStatement ::
495 // Expression ';'
496 // Identifier ':' Statement
497
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000498 bool starts_with_identifier = peek_any_identifier();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800499 Expression expr = ParseExpression(true, CHECK_OK);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000500 // 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
502 // an identifier.
503 if (starts_with_identifier && expr.IsIdentifier() && peek() == Token::COLON) {
504 // Expression is a single identifier, and not, e.g., a parenthesized
505 // identifier.
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);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000512 // 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.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800515 }
516 // Parsed expression statement.
517 ExpectSemicolon(CHECK_OK);
Ben Murdoch257744e2011-11-30 15:57:28 +0000518 return Statement::ExpressionStatement(expr);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800519}
520
521
Ben Murdochb0fe1622011-05-05 13:52:32 +0100522PreParser::Statement PreParser::ParseIfStatement(bool* ok) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800523 // IfStatement ::
524 // 'if' '(' Expression ')' Statement ('else' Statement)?
525
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000526 Expect(Token::IF, CHECK_OK);
527 Expect(Token::LPAREN, CHECK_OK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800528 ParseExpression(true, CHECK_OK);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000529 Expect(Token::RPAREN, CHECK_OK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800530 ParseStatement(CHECK_OK);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000531 if (peek() == Token::ELSE) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800532 Next();
533 ParseStatement(CHECK_OK);
534 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000535 return Statement::Default();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800536}
537
538
Ben Murdochb0fe1622011-05-05 13:52:32 +0100539PreParser::Statement PreParser::ParseContinueStatement(bool* ok) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800540 // ContinueStatement ::
541 // 'continue' [no line terminator] Identifier? ';'
542
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000543 Expect(Token::CONTINUE, CHECK_OK);
544 Token::Value tok = peek();
545 if (!scanner()->HasAnyLineTerminatorBeforeNext() &&
546 tok != Token::SEMICOLON &&
547 tok != Token::RBRACE &&
548 tok != Token::EOS) {
549 // ECMA allows "eval" or "arguments" as labels even in strict mode.
550 ParseIdentifier(kAllowEvalOrArguments, CHECK_OK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800551 }
552 ExpectSemicolon(CHECK_OK);
Ben Murdoch257744e2011-11-30 15:57:28 +0000553 return Statement::Default();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800554}
555
556
Ben Murdochb0fe1622011-05-05 13:52:32 +0100557PreParser::Statement PreParser::ParseBreakStatement(bool* ok) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800558 // BreakStatement ::
559 // 'break' [no line terminator] Identifier? ';'
560
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000561 Expect(Token::BREAK, CHECK_OK);
562 Token::Value tok = peek();
563 if (!scanner()->HasAnyLineTerminatorBeforeNext() &&
564 tok != Token::SEMICOLON &&
565 tok != Token::RBRACE &&
566 tok != Token::EOS) {
567 // ECMA allows "eval" or "arguments" as labels even in strict mode.
568 ParseIdentifier(kAllowEvalOrArguments, CHECK_OK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800569 }
570 ExpectSemicolon(CHECK_OK);
Ben Murdoch257744e2011-11-30 15:57:28 +0000571 return Statement::Default();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800572}
573
574
Ben Murdochb0fe1622011-05-05 13:52:32 +0100575PreParser::Statement PreParser::ParseReturnStatement(bool* ok) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800576 // ReturnStatement ::
577 // 'return' [no line terminator] Expression? ';'
578
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000579 // Consume the return token. It is necessary to do before
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800580 // reporting any errors on it, because of the way errors are
581 // reported (underlining).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000582 Expect(Token::RETURN, CHECK_OK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800583
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.
588
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000589 Token::Value tok = peek();
590 if (!scanner()->HasAnyLineTerminatorBeforeNext() &&
591 tok != Token::SEMICOLON &&
592 tok != Token::RBRACE &&
593 tok != Token::EOS) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800594 ParseExpression(true, CHECK_OK);
595 }
596 ExpectSemicolon(CHECK_OK);
Ben Murdoch257744e2011-11-30 15:57:28 +0000597 return Statement::Default();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800598}
599
600
Ben Murdochb0fe1622011-05-05 13:52:32 +0100601PreParser::Statement PreParser::ParseWithStatement(bool* ok) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800602 // WithStatement ::
603 // 'with' '(' Expression ')' Statement
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000604 Expect(Token::WITH, CHECK_OK);
605 if (strict_mode() == STRICT) {
606 ReportMessageAt(scanner()->location(), "strict_mode_with");
Ben Murdoch257744e2011-11-30 15:57:28 +0000607 *ok = false;
608 return Statement::Default();
609 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000610 Expect(Token::LPAREN, CHECK_OK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800611 ParseExpression(true, CHECK_OK);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000612 Expect(Token::RPAREN, CHECK_OK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800613
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000614 PreParserScope with_scope(scope_, WITH_SCOPE);
615 BlockState block_state(&scope_, &with_scope);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800616 ParseStatement(CHECK_OK);
Ben Murdoch257744e2011-11-30 15:57:28 +0000617 return Statement::Default();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800618}
619
620
Ben Murdochb0fe1622011-05-05 13:52:32 +0100621PreParser::Statement PreParser::ParseSwitchStatement(bool* ok) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800622 // SwitchStatement ::
623 // 'switch' '(' Expression ')' '{' CaseClause* '}'
624
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000625 Expect(Token::SWITCH, CHECK_OK);
626 Expect(Token::LPAREN, CHECK_OK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800627 ParseExpression(true, CHECK_OK);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000628 Expect(Token::RPAREN, CHECK_OK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800629
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000630 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);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800635 ParseExpression(true, CHECK_OK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800636 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000637 Expect(Token::DEFAULT, CHECK_OK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800638 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000639 Expect(Token::COLON, CHECK_OK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800640 token = peek();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000641 while (token != Token::CASE &&
642 token != Token::DEFAULT &&
643 token != Token::RBRACE) {
644 ParseStatement(CHECK_OK);
645 token = peek();
646 }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800647 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000648 Expect(Token::RBRACE, ok);
Ben Murdoch257744e2011-11-30 15:57:28 +0000649 return Statement::Default();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800650}
651
652
Ben Murdochb0fe1622011-05-05 13:52:32 +0100653PreParser::Statement PreParser::ParseDoWhileStatement(bool* ok) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800654 // DoStatement ::
655 // 'do' Statement 'while' '(' Expression ')' ';'
656
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000657 Expect(Token::DO, CHECK_OK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800658 ParseStatement(CHECK_OK);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000659 Expect(Token::WHILE, CHECK_OK);
660 Expect(Token::LPAREN, CHECK_OK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800661 ParseExpression(true, CHECK_OK);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000662 Expect(Token::RPAREN, ok);
663 if (peek() == Token::SEMICOLON) Consume(Token::SEMICOLON);
Ben Murdoch257744e2011-11-30 15:57:28 +0000664 return Statement::Default();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800665}
666
667
Ben Murdochb0fe1622011-05-05 13:52:32 +0100668PreParser::Statement PreParser::ParseWhileStatement(bool* ok) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800669 // WhileStatement ::
670 // 'while' '(' Expression ')' Statement
671
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000672 Expect(Token::WHILE, CHECK_OK);
673 Expect(Token::LPAREN, CHECK_OK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800674 ParseExpression(true, CHECK_OK);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000675 Expect(Token::RPAREN, CHECK_OK);
Ben Murdoch257744e2011-11-30 15:57:28 +0000676 ParseStatement(ok);
677 return Statement::Default();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800678}
679
680
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000681bool PreParser::CheckInOrOf(bool accept_OF) {
682 if (Check(Token::IN) ||
683 (accept_OF && CheckContextualKeyword(CStrVector("of")))) {
684 return true;
685 }
686 return false;
687}
688
689
Ben Murdochb0fe1622011-05-05 13:52:32 +0100690PreParser::Statement PreParser::ParseForStatement(bool* ok) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800691 // ForStatement ::
692 // 'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement
693
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000694 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;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800700 int decl_count;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100701 VariableDeclarationProperties decl_props = kHasNoInitializers;
702 ParseVariableDeclarations(
703 kForStatement, &decl_props, &decl_count, CHECK_OK);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000704 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)) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800708 ParseExpression(true, CHECK_OK);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000709 Expect(Token::RPAREN, CHECK_OK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800710
711 ParseStatement(CHECK_OK);
Ben Murdoch257744e2011-11-30 15:57:28 +0000712 return Statement::Default();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800713 }
714 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000715 Expression lhs = ParseExpression(false, CHECK_OK);
716 if (CheckInOrOf(lhs.IsIdentifier())) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800717 ParseExpression(true, CHECK_OK);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000718 Expect(Token::RPAREN, CHECK_OK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800719
720 ParseStatement(CHECK_OK);
Ben Murdoch257744e2011-11-30 15:57:28 +0000721 return Statement::Default();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800722 }
723 }
724 }
725
726 // Parsed initializer at this point.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000727 Expect(Token::SEMICOLON, CHECK_OK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800728
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000729 if (peek() != Token::SEMICOLON) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800730 ParseExpression(true, CHECK_OK);
731 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000732 Expect(Token::SEMICOLON, CHECK_OK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800733
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000734 if (peek() != Token::RPAREN) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800735 ParseExpression(true, CHECK_OK);
736 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000737 Expect(Token::RPAREN, CHECK_OK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800738
Ben Murdoch257744e2011-11-30 15:57:28 +0000739 ParseStatement(ok);
740 return Statement::Default();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800741}
742
743
Ben Murdochb0fe1622011-05-05 13:52:32 +0100744PreParser::Statement PreParser::ParseThrowStatement(bool* ok) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800745 // ThrowStatement ::
746 // 'throw' [no line terminator] Expression ';'
747
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000748 Expect(Token::THROW, CHECK_OK);
749 if (scanner()->HasAnyLineTerminatorBeforeNext()) {
750 ReportMessageAt(scanner()->location(), "newline_after_throw");
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800751 *ok = false;
Ben Murdoch257744e2011-11-30 15:57:28 +0000752 return Statement::Default();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800753 }
754 ParseExpression(true, CHECK_OK);
Ben Murdoch257744e2011-11-30 15:57:28 +0000755 ExpectSemicolon(ok);
756 return Statement::Default();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800757}
758
759
Ben Murdochb0fe1622011-05-05 13:52:32 +0100760PreParser::Statement PreParser::ParseTryStatement(bool* ok) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800761 // TryStatement ::
762 // 'try' Block Catch
763 // 'try' Block Finally
764 // 'try' Block Catch Finally
765 //
766 // Catch ::
767 // 'catch' '(' Identifier ')' Block
768 //
769 // Finally ::
770 // 'finally' Block
771
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000772 Expect(Token::TRY, CHECK_OK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800773
774 ParseBlock(CHECK_OK);
775
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000776 Token::Value tok = peek();
777 if (tok != Token::CATCH && tok != Token::FINALLY) {
778 ReportMessageAt(scanner()->location(), "no_catch_or_finally");
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800779 *ok = false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000780 return Statement::Default();
781 }
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);
787 {
788 PreParserScope with_scope(scope_, WITH_SCOPE);
789 BlockState block_state(&scope_, &with_scope);
790 ParseBlock(CHECK_OK);
791 }
792 tok = peek();
793 }
794 if (tok == Token::FINALLY) {
795 Consume(Token::FINALLY);
796 ParseBlock(CHECK_OK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800797 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000798 return Statement::Default();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800799}
800
801
Ben Murdochb0fe1622011-05-05 13:52:32 +0100802PreParser::Statement PreParser::ParseDebuggerStatement(bool* ok) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800803 // 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 ::
807 // 'debugger' ';'
808
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000809 Expect(Token::DEBUGGER, CHECK_OK);
Ben Murdoch257744e2011-11-30 15:57:28 +0000810 ExpectSemicolon(ok);
811 return Statement::Default();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800812}
813
814
Ben Murdoch257744e2011-11-30 15:57:28 +0000815#undef CHECK_OK
816#define CHECK_OK ok); \
817 if (!*ok) return Expression::Default(); \
818 ((void)0
819#define DUMMY ) // to make indentation work
820#undef DUMMY
821
822
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000823PreParser::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) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800828 // Function ::
829 // '(' FormalParameterList? ')' '{' FunctionBody '}'
830
831 // Parse function body.
832 ScopeType outer_scope_type = scope_->type();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000833 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));
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800837 // FormalParameterList ::
838 // '(' (Identifier)*[','] ')'
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000839 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();
848
849 bool done = arity_restriction == FunctionLiteral::GETTER_ARITY ||
850 (peek() == Token::RPAREN &&
851 arity_restriction != FunctionLiteral::SETTER_ARITY);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800852 while (!done) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000853 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();
Ben Murdoch257744e2011-11-30 15:57:28 +0000858 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000859 if (!reserved_error_loc.IsValid() && is_strict_reserved) {
860 reserved_error_loc = scanner()->location();
Ben Murdoch589d6972011-11-30 16:04:58 +0000861 }
862
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000863 int prev_value = scanner()->FindSymbol(&duplicate_finder, 1);
864
865 if (!dupe_error_loc.IsValid() && prev_value != 0) {
866 dupe_error_loc = scanner()->location();
Ben Murdoch589d6972011-11-30 16:04:58 +0000867 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000868
869 if (arity_restriction == FunctionLiteral::SETTER_ARITY) break;
870 done = (peek() == Token::RPAREN);
871 if (!done) Expect(Token::COMMA, CHECK_OK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800872 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000873 Expect(Token::RPAREN, CHECK_OK);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800874
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000875 // 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_);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100879 parenthesized_function_ = false;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800880
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000881 Expect(Token::LBRACE, CHECK_OK);
882 if (is_lazily_parsed) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100883 ParseLazyFunctionLiteralBody(CHECK_OK);
Ben Murdoch85b71792012-04-11 18:30:58 +0100884 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000885 ParseSourceElements(Token::RBRACE, ok);
Ben Murdoch85b71792012-04-11 18:30:58 +0100886 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000887 Expect(Token::RBRACE, CHECK_OK);
Ben Murdoch85b71792012-04-11 18:30:58 +0100888
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000889 // 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");
895 *ok = false;
896 return Expression::Default();
897 }
898 if (name_is_strict_reserved) {
899 ReportMessageAt(function_name_location, "unexpected_strict_reserved");
900 *ok = false;
901 return Expression::Default();
902 }
903 if (eval_args_error_loc.IsValid()) {
904 ReportMessageAt(eval_args_error_loc, "strict_eval_arguments");
905 *ok = false;
906 return Expression::Default();
907 }
908 if (dupe_error_loc.IsValid()) {
909 ReportMessageAt(dupe_error_loc, "strict_param_dupe");
910 *ok = false;
911 return Expression::Default();
912 }
913 if (reserved_error_loc.IsValid()) {
914 ReportMessageAt(reserved_error_loc, "unexpected_strict_reserved");
915 *ok = false;
916 return Expression::Default();
917 }
918
919 int end_position = scanner()->location().end_pos;
Ben Murdoch257744e2011-11-30 15:57:28 +0000920 CheckOctalLiteral(start_position, end_position, CHECK_OK);
Ben Murdoch257744e2011-11-30 15:57:28 +0000921 }
922
923 return Expression::Default();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800924}
925
926
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100927void PreParser::ParseLazyFunctionLiteralBody(bool* ok) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000928 int body_start = position();
929 ParseSourceElements(Token::RBRACE, ok);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100930 if (!*ok) return;
931
932 // Position right after terminal '}'.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000933 DCHECK_EQ(Token::RBRACE, scanner()->peek());
934 int body_end = scanner()->peek_location().end_pos;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100935 log_->LogFunction(body_start, body_end,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000936 function_state_->materialized_literal_count(),
937 function_state_->expected_property_count(),
938 strict_mode());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100939}
940
941
Ben Murdochb0fe1622011-05-05 13:52:32 +0100942PreParser::Expression PreParser::ParseV8Intrinsic(bool* ok) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800943 // CallRuntime ::
944 // '%' Identifier Arguments
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000945 Expect(Token::MOD, CHECK_OK);
946 if (!allow_natives_syntax()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100947 *ok = false;
948 return Expression::Default();
949 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000950 // Allow "eval" or "arguments" for backward compatibility.
951 ParseIdentifier(kAllowEvalOrArguments, CHECK_OK);
Ben Murdoch257744e2011-11-30 15:57:28 +0000952 ParseArguments(ok);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800953
Ben Murdoch257744e2011-11-30 15:57:28 +0000954 return Expression::Default();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800955}
956
Ben Murdoch257744e2011-11-30 15:57:28 +0000957#undef CHECK_OK
958
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800959
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000960} } // v8::internal