OLD | NEW |
---|---|
1 // Copyright 2011 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <cmath> | 5 #include <cmath> |
6 | 6 |
7 #include "src/allocation.h" | 7 #include "src/allocation.h" |
8 #include "src/base/logging.h" | 8 #include "src/base/logging.h" |
9 #include "src/conversions-inl.h" | 9 #include "src/conversions-inl.h" |
10 #include "src/conversions.h" | 10 #include "src/conversions.h" |
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
402 // | 402 // |
403 // ConstDeclaration :: | 403 // ConstDeclaration :: |
404 // const ConstBinding (',' ConstBinding)* ';' | 404 // const ConstBinding (',' ConstBinding)* ';' |
405 // ConstBinding :: | 405 // ConstBinding :: |
406 // Identifier '=' AssignmentExpression | 406 // Identifier '=' AssignmentExpression |
407 // | 407 // |
408 // TODO(ES6): | 408 // TODO(ES6): |
409 // ConstBinding :: | 409 // ConstBinding :: |
410 // BindingPattern '=' AssignmentExpression | 410 // BindingPattern '=' AssignmentExpression |
411 bool require_initializer = false; | 411 bool require_initializer = false; |
412 bool is_strict_const = false; | |
412 if (peek() == Token::VAR) { | 413 if (peek() == Token::VAR) { |
413 Consume(Token::VAR); | 414 Consume(Token::VAR); |
414 } else if (peek() == Token::CONST) { | 415 } else if (peek() == Token::CONST) { |
415 // TODO(ES6): The ES6 Draft Rev4 section 12.2.2 reads: | 416 // TODO(ES6): The ES6 Draft Rev4 section 12.2.2 reads: |
416 // | 417 // |
417 // ConstDeclaration : const ConstBinding (',' ConstBinding)* ';' | 418 // ConstDeclaration : const ConstBinding (',' ConstBinding)* ';' |
418 // | 419 // |
419 // * It is a Syntax Error if the code that matches this production is not | 420 // * It is a Syntax Error if the code that matches this production is not |
420 // contained in extended code. | 421 // contained in extended code. |
421 // | 422 // |
422 // However disallowing const in sloppy mode will break compatibility with | 423 // However disallowing const in sloppy mode will break compatibility with |
423 // existing pages. Therefore we keep allowing const with the old | 424 // existing pages. Therefore we keep allowing const with the old |
424 // non-harmony semantics in sloppy mode. | 425 // non-harmony semantics in sloppy mode. |
425 Consume(Token::CONST); | 426 Consume(Token::CONST); |
426 if (strict_mode() == STRICT) { | 427 if (strict_mode() == STRICT) { |
427 if (allow_harmony_scoping()) { | 428 if (allow_harmony_scoping()) { |
428 if (var_context != kSourceElement && var_context != kForStatement) { | 429 if (var_context != kSourceElement && var_context != kForStatement) { |
429 ReportMessageAt(scanner()->peek_location(), "unprotected_const"); | 430 ReportMessageAt(scanner()->peek_location(), "unprotected_const"); |
430 *ok = false; | 431 *ok = false; |
431 return Statement::Default(); | 432 return Statement::Default(); |
432 } | 433 } |
433 require_initializer = true; | 434 is_strict_const = true; |
435 require_initializer = var_context != kForStatement; | |
434 } else { | 436 } else { |
435 Scanner::Location location = scanner()->peek_location(); | 437 Scanner::Location location = scanner()->peek_location(); |
436 ReportMessageAt(location, "strict_const"); | 438 ReportMessageAt(location, "strict_const"); |
437 *ok = false; | 439 *ok = false; |
438 return Statement::Default(); | 440 return Statement::Default(); |
439 } | 441 } |
440 } | 442 } |
441 } else if (peek() == Token::LET && strict_mode() == STRICT) { | 443 } else if (peek() == Token::LET && strict_mode() == STRICT) { |
442 Consume(Token::LET); | 444 Consume(Token::LET); |
443 if (var_context != kSourceElement && var_context != kForStatement) { | 445 if (var_context != kSourceElement && var_context != kForStatement) { |
444 ReportMessageAt(scanner()->peek_location(), "unprotected_let"); | 446 ReportMessageAt(scanner()->peek_location(), "unprotected_let"); |
445 *ok = false; | 447 *ok = false; |
446 return Statement::Default(); | 448 return Statement::Default(); |
447 } | 449 } |
448 } else { | 450 } else { |
449 *ok = false; | 451 *ok = false; |
450 return Statement::Default(); | 452 return Statement::Default(); |
451 } | 453 } |
452 | 454 |
453 // The scope of a var/const declared variable anywhere inside a function | 455 // The scope of a var/const declared variable anywhere inside a function |
454 // is the entire function (ECMA-262, 3rd, 10.1.3, and 12.2). The scope | 456 // is the entire function (ECMA-262, 3rd, 10.1.3, and 12.2). The scope |
455 // of a let declared variable is the scope of the immediately enclosing | 457 // of a let declared variable is the scope of the immediately enclosing |
456 // block. | 458 // block. |
457 int nvars = 0; // the number of variables declared | 459 int nvars = 0; // the number of variables declared |
458 do { | 460 do { |
459 // Parse variable name. | 461 // Parse variable name. |
460 if (nvars > 0) Consume(Token::COMMA); | 462 if (nvars > 0) Consume(Token::COMMA); |
461 ParseIdentifier(kDontAllowEvalOrArguments, CHECK_OK); | 463 ParseIdentifier(kDontAllowEvalOrArguments, CHECK_OK); |
462 nvars++; | 464 nvars++; |
463 if (peek() == Token::ASSIGN || require_initializer) { | 465 if (peek() == Token::ASSIGN || require_initializer || |
466 (is_strict_const && peek() == Token::COMMA)) { | |
rossberg
2014/10/23 09:44:54
I don't understand this condition. If it is true,
Dmitry Lomov (no reviews)
2014/10/23 10:33:34
Yes, intentional. Comment added.
| |
464 Expect(Token::ASSIGN, CHECK_OK); | 467 Expect(Token::ASSIGN, CHECK_OK); |
465 ParseAssignmentExpression(var_context != kForStatement, CHECK_OK); | 468 ParseAssignmentExpression(var_context != kForStatement, CHECK_OK); |
466 if (decl_props != NULL) *decl_props = kHasInitializers; | 469 if (decl_props != NULL) *decl_props = kHasInitializers; |
467 } | 470 } |
468 } while (peek() == Token::COMMA); | 471 } while (peek() == Token::COMMA); |
469 | 472 |
470 if (num_decl != NULL) *num_decl = nvars; | 473 if (num_decl != NULL) *num_decl = nvars; |
471 return Statement::Default(); | 474 return Statement::Default(); |
472 } | 475 } |
473 | 476 |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
671 | 674 |
672 PreParser::Statement PreParser::ParseForStatement(bool* ok) { | 675 PreParser::Statement PreParser::ParseForStatement(bool* ok) { |
673 // ForStatement :: | 676 // ForStatement :: |
674 // 'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement | 677 // 'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement |
675 | 678 |
676 Expect(Token::FOR, CHECK_OK); | 679 Expect(Token::FOR, CHECK_OK); |
677 Expect(Token::LPAREN, CHECK_OK); | 680 Expect(Token::LPAREN, CHECK_OK); |
678 if (peek() != Token::SEMICOLON) { | 681 if (peek() != Token::SEMICOLON) { |
679 if (peek() == Token::VAR || peek() == Token::CONST || | 682 if (peek() == Token::VAR || peek() == Token::CONST || |
680 (peek() == Token::LET && strict_mode() == STRICT)) { | 683 (peek() == Token::LET && strict_mode() == STRICT)) { |
681 bool is_let = peek() == Token::LET; | 684 bool is_let_or_strict_const = |
rossberg
2014/10/23 09:44:54
Nit: how about naming this 'is_lexical'?
Dmitry Lomov (no reviews)
2014/10/23 10:33:34
Done.
| |
685 peek() == Token::LET || | |
686 (peek() == Token::CONST && strict_mode() == STRICT); | |
682 int decl_count; | 687 int decl_count; |
683 VariableDeclarationProperties decl_props = kHasNoInitializers; | 688 VariableDeclarationProperties decl_props = kHasNoInitializers; |
684 ParseVariableDeclarations( | 689 ParseVariableDeclarations( |
685 kForStatement, &decl_props, &decl_count, CHECK_OK); | 690 kForStatement, &decl_props, &decl_count, CHECK_OK); |
686 bool has_initializers = decl_props == kHasInitializers; | 691 bool has_initializers = decl_props == kHasInitializers; |
687 bool accept_IN = decl_count == 1 && !(is_let && has_initializers); | 692 bool accept_IN = |
693 decl_count == 1 && !(is_let_or_strict_const && has_initializers); | |
688 bool accept_OF = !has_initializers; | 694 bool accept_OF = !has_initializers; |
689 if (accept_IN && CheckInOrOf(accept_OF)) { | 695 if (accept_IN && CheckInOrOf(accept_OF)) { |
690 ParseExpression(true, CHECK_OK); | 696 ParseExpression(true, CHECK_OK); |
691 Expect(Token::RPAREN, CHECK_OK); | 697 Expect(Token::RPAREN, CHECK_OK); |
692 | 698 |
693 ParseStatement(CHECK_OK); | 699 ParseStatement(CHECK_OK); |
694 return Statement::Default(); | 700 return Statement::Default(); |
695 } | 701 } |
696 } else { | 702 } else { |
697 Expression lhs = ParseExpression(false, CHECK_OK); | 703 Expression lhs = ParseExpression(false, CHECK_OK); |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
934 ParseIdentifier(kAllowEvalOrArguments, CHECK_OK); | 940 ParseIdentifier(kAllowEvalOrArguments, CHECK_OK); |
935 ParseArguments(ok); | 941 ParseArguments(ok); |
936 | 942 |
937 return Expression::Default(); | 943 return Expression::Default(); |
938 } | 944 } |
939 | 945 |
940 #undef CHECK_OK | 946 #undef CHECK_OK |
941 | 947 |
942 | 948 |
943 } } // v8::internal | 949 } } // v8::internal |
OLD | NEW |