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

Unified Diff: src/parser.cc

Issue 1315483004: [es6] Implement destructuring for `catch` (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/ast-value-factory.h ('k') | src/preparser.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/parser.cc
diff --git a/src/parser.cc b/src/parser.cc
index 9334a33195a1e21f474055863bc28dd47fa2cb93..691f9fb83f43e3905f24e7676d406711cc334201 100644
--- a/src/parser.cc
+++ b/src/parser.cc
@@ -3041,21 +3041,63 @@ TryStatement* Parser::ParseTryStatement(bool* ok) {
Scope* catch_scope = NULL;
Variable* catch_variable = NULL;
Block* catch_block = NULL;
- const AstRawString* name = NULL;
if (tok == Token::CATCH) {
Consume(Token::CATCH);
Expect(Token::LPAREN, CHECK_OK);
catch_scope = NewScope(scope_, CATCH_SCOPE);
catch_scope->set_start_position(scanner()->location().beg_pos);
- name = ParseIdentifier(kDontAllowRestrictedIdentifiers, CHECK_OK);
+
+ ExpressionClassifier pattern_classifier;
+ Token::Value next = peek();
+ Expression* pattern =
+ ParsePrimaryExpression(&pattern_classifier, CHECK_OK);
+ ValidateBindingPattern(&pattern_classifier, CHECK_OK);
+
+ if (!allow_harmony_destructuring() && !pattern->IsVariableProxy()) {
+ ReportUnexpectedToken(next);
+ *ok = false;
+ return NULL;
+ }
Expect(Token::RPAREN, CHECK_OK);
- catch_variable = catch_scope->DeclareLocal(name, VAR, kCreatedInitialized,
- Variable::NORMAL);
- BlockState block_state(&scope_, catch_scope);
- catch_block = ParseBlock(NULL, CHECK_OK);
+ if (pattern->IsVariableProxy()) {
+ const AstRawString* name = pattern->AsVariableProxy()->raw_name();
+ catch_variable = catch_scope->DeclareLocal(name, VAR, kCreatedInitialized,
+ Variable::NORMAL);
+ BlockState block_state(&scope_, catch_scope);
+ catch_block = ParseBlock(NULL, CHECK_OK);
+ } else {
+ Variable* temp = scope_->NewTemporary(
+ ast_value_factory()->dot_catch_string());
+ Scope* destruct_scope = NewScope(catch_scope, BLOCK_SCOPE);
+ Block* destruct_block =
+ factory()->NewBlock(nullptr, 1, false, RelocInfo::kNoPosition);
+
+ DeclarationDescriptor descriptor;
+ descriptor.declaration_kind = DeclarationDescriptor::NORMAL;
+ descriptor.parser = this;
+ descriptor.declaration_scope = destruct_scope->DeclarationScope();
+ descriptor.scope = destruct_scope;
+ descriptor.hoist_scope = nullptr;
+ descriptor.mode = LET;
+ descriptor.is_const = false;
+ descriptor.needs_init = true;
+ descriptor.declaration_pos = RelocInfo::kNoPosition;
+ descriptor.initialization_pos = RelocInfo::kNoPosition;
+ descriptor.init_op = Token::INIT_LET;
+ Expression* initial_value = factory()->NewVariableProxy(temp);
+
+ BlockState block_state(&scope_, destruct_scope);
+ DeclarationParsingResult::Declaration decl(
+ pattern, pattern->position(), initial_value);
+ PatternRewriter::DeclareAndInitializeVariables(
+ destruct_block, &descriptor, &decl, nullptr, CHECK_OK);
+
+ catch_block->AddStatement(destruct_block, zone());
+ catch_block = ParseBlock(NULL, CHECK_OK);
+ }
catch_scope->set_end_position(scanner()->location().end_pos);
tok = peek();
« no previous file with comments | « src/ast-value-factory.h ('k') | src/preparser.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698