Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(136)

Side by Side Diff: src/preparser.cc

Issue 671913002: harmony-scoping: Allow 'const' iteration variables in strict mode. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Remove assertion (checked downstream) Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/preparser.h ('k') | test/cctest/test-parsing.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 // require initializers for multiple consts.
467 (is_strict_const && peek() == Token::COMMA)) {
464 Expect(Token::ASSIGN, CHECK_OK); 468 Expect(Token::ASSIGN, CHECK_OK);
465 ParseAssignmentExpression(var_context != kForStatement, CHECK_OK); 469 ParseAssignmentExpression(var_context != kForStatement, CHECK_OK);
466 if (decl_props != NULL) *decl_props = kHasInitializers; 470 if (decl_props != NULL) *decl_props = kHasInitializers;
467 } 471 }
468 } while (peek() == Token::COMMA); 472 } while (peek() == Token::COMMA);
469 473
470 if (num_decl != NULL) *num_decl = nvars; 474 if (num_decl != NULL) *num_decl = nvars;
471 return Statement::Default(); 475 return Statement::Default();
472 } 476 }
473 477
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 675
672 PreParser::Statement PreParser::ParseForStatement(bool* ok) { 676 PreParser::Statement PreParser::ParseForStatement(bool* ok) {
673 // ForStatement :: 677 // ForStatement ::
674 // 'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement 678 // 'for' '(' Expression? ';' Expression? ';' Expression? ')' Statement
675 679
676 Expect(Token::FOR, CHECK_OK); 680 Expect(Token::FOR, CHECK_OK);
677 Expect(Token::LPAREN, CHECK_OK); 681 Expect(Token::LPAREN, CHECK_OK);
678 if (peek() != Token::SEMICOLON) { 682 if (peek() != Token::SEMICOLON) {
679 if (peek() == Token::VAR || peek() == Token::CONST || 683 if (peek() == Token::VAR || peek() == Token::CONST ||
680 (peek() == Token::LET && strict_mode() == STRICT)) { 684 (peek() == Token::LET && strict_mode() == STRICT)) {
681 bool is_let = peek() == Token::LET; 685 bool is_lexical = 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 = decl_count == 1 && !(is_lexical && has_initializers);
688 bool accept_OF = !has_initializers; 693 bool accept_OF = !has_initializers;
689 if (accept_IN && CheckInOrOf(accept_OF)) { 694 if (accept_IN && CheckInOrOf(accept_OF)) {
690 ParseExpression(true, CHECK_OK); 695 ParseExpression(true, CHECK_OK);
691 Expect(Token::RPAREN, CHECK_OK); 696 Expect(Token::RPAREN, CHECK_OK);
692 697
693 ParseStatement(CHECK_OK); 698 ParseStatement(CHECK_OK);
694 return Statement::Default(); 699 return Statement::Default();
695 } 700 }
696 } else { 701 } else {
697 Expression lhs = ParseExpression(false, CHECK_OK); 702 Expression lhs = ParseExpression(false, CHECK_OK);
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after
934 ParseIdentifier(kAllowEvalOrArguments, CHECK_OK); 939 ParseIdentifier(kAllowEvalOrArguments, CHECK_OK);
935 ParseArguments(ok); 940 ParseArguments(ok);
936 941
937 return Expression::Default(); 942 return Expression::Default();
938 } 943 }
939 944
940 #undef CHECK_OK 945 #undef CHECK_OK
941 946
942 947
943 } } // v8::internal 948 } } // v8::internal
OLDNEW
« no previous file with comments | « src/preparser.h ('k') | test/cctest/test-parsing.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698