You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2016/11/26 08:53:28 UTC
groovy git commit: GROOVY-8006: [parrot] Allow dropping 'def' or type
in try with resources
Repository: groovy
Updated Branches:
refs/heads/parrot 2ca95086e -> 08ebc38cb
GROOVY-8006: [parrot] Allow dropping 'def' or type in try with resources
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/08ebc38c
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/08ebc38c
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/08ebc38c
Branch: refs/heads/parrot
Commit: 08ebc38cb8e7605679532b4735f0193f48dcf096
Parents: 2ca9508
Author: Daniel Sun <su...@apache.org>
Authored: Sat Nov 26 16:52:48 2016 +0800
Committer: Daniel Sun <su...@apache.org>
Committed: Sat Nov 26 16:52:48 2016 +0800
----------------------------------------------------------------------
.../apache/groovy/parser/antlr4/GroovyParser.g4 | 1 +
.../apache/groovy/parser/antlr4/AstBuilder.java | 35 +++++++++++++++++---
.../resources/core/TryWithResources_01x.groovy | 15 +++++++--
3 files changed, 45 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/08ebc38c/subprojects/groovy-antlr4-grammar/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4
----------------------------------------------------------------------
diff --git a/subprojects/groovy-antlr4-grammar/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4 b/subprojects/groovy-antlr4-grammar/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4
index 8bdb6b8..a3c6043 100644
--- a/subprojects/groovy-antlr4-grammar/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4
+++ b/subprojects/groovy-antlr4-grammar/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4
@@ -688,6 +688,7 @@ resourceList
resource
: localVariableDeclaration
+ | expression
;
http://git-wip-us.apache.org/repos/asf/groovy/blob/08ebc38c/subprojects/groovy-antlr4-grammar/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
----------------------------------------------------------------------
diff --git a/subprojects/groovy-antlr4-grammar/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java b/subprojects/groovy-antlr4-grammar/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
index f731a8d..23d4fa5 100644
--- a/subprojects/groovy-antlr4-grammar/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/subprojects/groovy-antlr4-grammar/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -444,13 +444,40 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
@Override
public ExpressionStatement visitResource(ResourceContext ctx) {
- List<ExpressionStatement> declarationStatements = this.visitLocalVariableDeclaration(ctx.localVariableDeclaration()).getDeclarationStatements();
+ if (asBoolean(ctx.localVariableDeclaration())) {
+ List<ExpressionStatement> declarationStatements = this.visitLocalVariableDeclaration(ctx.localVariableDeclaration()).getDeclarationStatements();
+
+ if (declarationStatements.size() > 1) {
+ throw createParsingFailedException("Multi resources can not be declared in one statement", ctx);
+ }
+
+ return declarationStatements.get(0);
+ } else if (asBoolean(ctx.expression())) {
+ Expression expression = (Expression) this.visit(ctx.expression());
+ if (!(expression instanceof BinaryExpression
+ && Types.ASSIGN == ((BinaryExpression) expression).getOperation().getType()
+ && ((BinaryExpression) expression).getLeftExpression() instanceof VariableExpression)) {
+
+ throw createParsingFailedException("Only variable declarations are allowed to declare resource", ctx);
+ }
+
+ BinaryExpression assignmentExpression = (BinaryExpression) expression;
- if (declarationStatements.size() > 1) {
- throw createParsingFailedException("Multi resources can not be declared in one statement", ctx);
+ return this.configureAST(
+ new ExpressionStatement(
+ this.configureAST(
+ new DeclarationExpression(
+ this.configureAST(
+ new VariableExpression(assignmentExpression.getLeftExpression().getText()),
+ assignmentExpression.getLeftExpression()
+ ),
+ assignmentExpression.getOperation(),
+ assignmentExpression.getRightExpression()
+ ), ctx)
+ ), ctx);
}
- return declarationStatements.get(0);
+ throw createParsingFailedException("Unsupported resource declaration: " + ctx.getText(), ctx);
}
/**
http://git-wip-us.apache.org/repos/asf/groovy/blob/08ebc38c/subprojects/groovy-antlr4-grammar/src/test/resources/core/TryWithResources_01x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-antlr4-grammar/src/test/resources/core/TryWithResources_01x.groovy b/subprojects/groovy-antlr4-grammar/src/test/resources/core/TryWithResources_01x.groovy
index 38f8da8..9b88740 100644
--- a/subprojects/groovy-antlr4-grammar/src/test/resources/core/TryWithResources_01x.groovy
+++ b/subprojects/groovy-antlr4-grammar/src/test/resources/core/TryWithResources_01x.groovy
@@ -247,9 +247,20 @@ try (
Resource r1 = new Resource(
1
)
- Resource r2 = new Resource(2)
- ) {
+ Resource r2 = new Resource(2)
+) {
a = 2;
}
assert Resource.closedResourceIds == [2, 1]
assert 2 == a
+
+// test case 17
+Resource.closedResourceIds = []
+a = 1;
+try (r1 = new Resource(1)
+ r2 = new Resource(2)) {
+ a = 2;
+}
+assert Resource.closedResourceIds == [2, 1]
+assert 2 == a
+