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 2017/09/30 07:29:38 UTC
groovy git commit: Minor refactoring: try-catch-finally
Repository: groovy
Updated Branches:
refs/heads/master 8cdb04566 -> d1c9de369
Minor refactoring: try-catch-finally
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/d1c9de36
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/d1c9de36
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/d1c9de36
Branch: refs/heads/master
Commit: d1c9de36962ce314bc33584f3b0812060667a08e
Parents: 8cdb045
Author: sunlan <su...@apache.org>
Authored: Sat Sep 30 15:29:33 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Sat Sep 30 15:29:33 2017 +0800
----------------------------------------------------------------------
src/antlr/GroovyParser.g4 | 15 +++------------
.../org/apache/groovy/parser/antlr4/AstBuilder.java | 8 ++++++++
2 files changed, 11 insertions(+), 12 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/d1c9de36/src/antlr/GroovyParser.g4
----------------------------------------------------------------------
diff --git a/src/antlr/GroovyParser.g4 b/src/antlr/GroovyParser.g4
index f745130..36c4bde 100644
--- a/src/antlr/GroovyParser.g4
+++ b/src/antlr/GroovyParser.g4
@@ -654,18 +654,9 @@ locals[ boolean isInsideLoop, boolean isInsideSwitch ]
;
tryCatchStatement
-locals[boolean resourcesExists = false]
- : TRY (resources { $resourcesExists = true; })? nls
- block
- (
- (nls catchClause)+
- (nls finallyBlock)?
- |
- nls finallyBlock
- |
- // catch and finally clauses required unless it's a try-with-resources block
- { require($resourcesExists, "either a catch or finally clause or both is required for a try-catch-finally statement"); }
- )
+ : TRY resources? nls block
+ (nls catchClause)*
+ (nls finallyBlock)?
;
assertStatement
http://git-wip-us.apache.org/repos/asf/groovy/blob/d1c9de36/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
index 931a8ae..b1a9fa6 100644
--- a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -538,6 +538,14 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
@Override
public Statement visitTryCatchStatement(TryCatchStatementContext ctx) {
+ boolean resourcesExists = asBoolean(ctx.resources());
+ boolean catchExists = asBoolean(ctx.catchClause());
+ boolean finallyExists = asBoolean(ctx.finallyBlock());
+
+ if (!(resourcesExists || catchExists || finallyExists)) {
+ throw createParsingFailedException("Either a catch or finally clause or both is required for a try-catch-finally statement", ctx);
+ }
+
TryCatchStatement tryCatchStatement =
new TryCatchStatement((Statement) this.visit(ctx.block()),
this.visitFinallyBlock(ctx.finallyBlock()));