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 516 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
527 // ConstDeclaration :: | 527 // ConstDeclaration :: |
528 // const ConstBinding (',' ConstBinding)* ';' | 528 // const ConstBinding (',' ConstBinding)* ';' |
529 // ConstBinding :: | 529 // ConstBinding :: |
530 // Identifier '=' AssignmentExpression | 530 // Identifier '=' AssignmentExpression |
531 // | 531 // |
532 // TODO(ES6): | 532 // TODO(ES6): |
533 // ConstBinding :: | 533 // ConstBinding :: |
534 // BindingPattern '=' AssignmentExpression | 534 // BindingPattern '=' AssignmentExpression |
535 bool require_initializer = false; | 535 bool require_initializer = false; |
536 bool is_strict_const = false; | 536 bool is_strict_const = false; |
| 537 bool lexical = false; |
537 if (peek() == Token::VAR) { | 538 if (peek() == Token::VAR) { |
538 if (is_strong(language_mode())) { | 539 if (is_strong(language_mode())) { |
539 Scanner::Location location = scanner()->peek_location(); | 540 Scanner::Location location = scanner()->peek_location(); |
540 ReportMessageAt(location, MessageTemplate::kStrongVar); | 541 ReportMessageAt(location, MessageTemplate::kStrongVar); |
541 *ok = false; | 542 *ok = false; |
542 return Statement::Default(); | 543 return Statement::Default(); |
543 } | 544 } |
544 Consume(Token::VAR); | 545 Consume(Token::VAR); |
545 } else if (peek() == Token::CONST && allow_const()) { | 546 } else if (peek() == Token::CONST && allow_const()) { |
546 // TODO(ES6): The ES6 Draft Rev4 section 12.2.2 reads: | 547 // TODO(ES6): The ES6 Draft Rev4 section 12.2.2 reads: |
547 // | 548 // |
548 // ConstDeclaration : const ConstBinding (',' ConstBinding)* ';' | 549 // ConstDeclaration : const ConstBinding (',' ConstBinding)* ';' |
549 // | 550 // |
550 // * It is a Syntax Error if the code that matches this production is not | 551 // * It is a Syntax Error if the code that matches this production is not |
551 // contained in extended code. | 552 // contained in extended code. |
552 // | 553 // |
553 // However disallowing const in sloppy mode will break compatibility with | 554 // However disallowing const in sloppy mode will break compatibility with |
554 // existing pages. Therefore we keep allowing const with the old | 555 // existing pages. Therefore we keep allowing const with the old |
555 // non-harmony semantics in sloppy mode. | 556 // non-harmony semantics in sloppy mode. |
556 Consume(Token::CONST); | 557 Consume(Token::CONST); |
557 if (is_strict(language_mode()) || | 558 if (is_strict(language_mode()) || |
558 (allow_harmony_sloppy() && !allow_legacy_const())) { | 559 (allow_harmony_sloppy() && !allow_legacy_const())) { |
559 DCHECK(var_context != kStatement); | 560 DCHECK(var_context != kStatement); |
560 is_strict_const = true; | 561 is_strict_const = true; |
561 require_initializer = var_context != kForStatement; | 562 require_initializer = var_context != kForStatement; |
| 563 lexical = true; |
562 } | 564 } |
563 } else if (peek() == Token::LET && allow_let()) { | 565 } else if (peek() == Token::LET && allow_let()) { |
564 Consume(Token::LET); | 566 Consume(Token::LET); |
565 DCHECK(var_context != kStatement); | 567 DCHECK(var_context != kStatement); |
| 568 lexical = true; |
566 } else { | 569 } else { |
567 *ok = false; | 570 *ok = false; |
568 return Statement::Default(); | 571 return Statement::Default(); |
569 } | 572 } |
570 | 573 |
571 // The scope of a var/const declared variable anywhere inside a function | 574 // The scope of a var/const declared variable anywhere inside a function |
572 // is the entire function (ECMA-262, 3rd, 10.1.3, and 12.2). The scope | 575 // is the entire function (ECMA-262, 3rd, 10.1.3, and 12.2). The scope |
573 // of a let declared variable is the scope of the immediately enclosing | 576 // of a let declared variable is the scope of the immediately enclosing |
574 // block. | 577 // block. |
575 int nvars = 0; // the number of variables declared | 578 int nvars = 0; // the number of variables declared |
576 int bindings_start = peek_position(); | 579 int bindings_start = peek_position(); |
577 do { | 580 do { |
578 // Parse binding pattern. | 581 // Parse binding pattern. |
579 if (nvars > 0) Consume(Token::COMMA); | 582 if (nvars > 0) Consume(Token::COMMA); |
580 { | 583 { |
581 ExpressionClassifier pattern_classifier; | 584 ExpressionClassifier pattern_classifier; |
582 Token::Value next = peek(); | 585 Token::Value next = peek(); |
583 PreParserExpression pattern = | 586 PreParserExpression pattern = |
584 ParsePrimaryExpression(&pattern_classifier, CHECK_OK); | 587 ParsePrimaryExpression(&pattern_classifier, CHECK_OK); |
585 ValidateBindingPattern(&pattern_classifier, CHECK_OK); | 588 ValidateBindingPattern(&pattern_classifier, CHECK_OK); |
| 589 if (lexical) { |
| 590 ValidateLetPattern(&pattern_classifier, CHECK_OK); |
| 591 } |
586 | 592 |
587 if (!allow_harmony_destructuring() && !pattern.IsIdentifier()) { | 593 if (!allow_harmony_destructuring() && !pattern.IsIdentifier()) { |
588 ReportUnexpectedToken(next); | 594 ReportUnexpectedToken(next); |
589 *ok = false; | 595 *ok = false; |
590 return Statement::Default(); | 596 return Statement::Default(); |
591 } | 597 } |
592 } | 598 } |
593 | 599 |
594 Scanner::Location variable_loc = scanner()->location(); | 600 Scanner::Location variable_loc = scanner()->location(); |
595 nvars++; | 601 nvars++; |
(...skipping 636 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1232 DCHECK(!spread_pos.IsValid()); | 1238 DCHECK(!spread_pos.IsValid()); |
1233 | 1239 |
1234 return Expression::Default(); | 1240 return Expression::Default(); |
1235 } | 1241 } |
1236 | 1242 |
1237 #undef CHECK_OK | 1243 #undef CHECK_OK |
1238 | 1244 |
1239 | 1245 |
1240 } // namespace internal | 1246 } // namespace internal |
1241 } // namespace v8 | 1247 } // namespace v8 |
OLD | NEW |