diff options
author | Lars Knoll <lars.knoll@qt.io> | 2018-05-09 14:03:39 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2018-05-11 07:17:22 +0000 |
commit | e3b9726483cb7336aa54ae7d9455becbd148b2b6 (patch) | |
tree | ba056c898a9a694179104e1fd4f4f32d91a6dd86 /src/qml/parser | |
parent | 541b8220cffbbae4b798ae8282d04ca145caccd1 (diff) |
Improve for-in and for-of support
Create a Block scope per iteration as defined in the ES spec. So
closures created inside the loop will remember the iteration variable
at that loop iteration.
Add support for destructuring of the left hand side expression or
declaration.
Change-Id: Id06ef94e2a4b93646827da4f6ce922eb436e5a31
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/qml/parser')
-rw-r--r-- | src/qml/parser/qqmljs.g | 35 | ||||
-rw-r--r-- | src/qml/parser/qqmljsast_p.h | 4 |
2 files changed, 25 insertions, 14 deletions
diff --git a/src/qml/parser/qqmljs.g b/src/qml/parser/qqmljs.g index 5efc392efa..4b1dc437f4 100644 --- a/src/qml/parser/qqmljs.g +++ b/src/qml/parser/qqmljs.g @@ -3144,14 +3144,14 @@ IterationStatement: T_FOR T_LPAREN LeftHandSideExpression InOrOf Expression_In T /. case $rule_number: { // need to convert the LHS to an AssignmentPattern if it was an Array/ObjectLiteral -// if (AST::Pattern *p = sym(3).Expression->patternCast()) { -// AST::SourceLocation errorLoc; -// QString errorMsg; -// if (!p->convertLiteralToAssignmentPattern(pool, &errorLoc, &errorMsg)) { -// syntaxError(errorLoc, errorMsg); -// return false; -// } -// } + if (AST::Pattern *p = sym(3).Expression->patternCast()) { + AST::SourceLocation errorLoc; + QString errorMsg; + if (!p->convertLiteralToAssignmentPattern(pool, &errorLoc, &errorMsg)) { + syntaxError(errorLoc, errorMsg); + return false; + } + } AST::ForEachStatement *node = new (pool) AST::ForEachStatement(sym(3).Expression, sym(5).Expression, sym(7).Statement); node->forToken = loc(1); node->lparenToken = loc(2); @@ -3175,9 +3175,9 @@ IterationStatement: T_FOR T_LPAREN ForDeclaration InOrOf Expression_In T_RPAREN } break; ./ -ForDeclaration: LetOrConst ForBinding; +ForDeclaration: LetOrConst BindingIdentifier; /. case $rule_number: Q_FALLTHROUGH(); ./ -ForDeclaration: Var ForBinding; +ForDeclaration: Var BindingIdentifier; /. case $rule_number: { auto *node = new (pool) AST::PatternElement(stringRef(2), nullptr); @@ -3187,10 +3187,17 @@ ForDeclaration: Var ForBinding; } break; ./ -ForBinding: BindingIdentifier; - -ForBinding: BindingPattern; -/. case $rule_number: UNIMPLEMENTED; ./ +ForDeclaration: LetOrConst BindingPattern; +/. case $rule_number: Q_FALLTHROUGH(); ./ +ForDeclaration: Var BindingPattern; +/. + case $rule_number: { + auto *node = new (pool) AST::PatternElement(sym(2).Pattern, nullptr); + node->identifierToken = loc(2); + node->scope = sym(1).scope; + sym(1).Node = node; + } break; +./ ContinueStatement: T_CONTINUE T_AUTOMATIC_SEMICOLON; ContinueStatement: T_CONTINUE T_SEMICOLON; diff --git a/src/qml/parser/qqmljsast_p.h b/src/qml/parser/qqmljsast_p.h index 83fcb6559c..9ed7ba79e5 100644 --- a/src/qml/parser/qqmljsast_p.h +++ b/src/qml/parser/qqmljsast_p.h @@ -1725,6 +1725,10 @@ public: SourceLocation lastSourceLocation() const override { return statement->lastSourceLocation(); } + PatternElement *declaration() const { + return AST::cast<PatternElement *>(lhs); + } + // attributes Node *lhs; ExpressionNode *expression; |