groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject groovy git commit: Minor refactoring
Date Thu, 25 May 2017 12:21:14 GMT
Repository: groovy
Updated Branches:
  refs/heads/master ffe788e12 -> ac6c20c84


Minor refactoring


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/ac6c20c8
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/ac6c20c8
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/ac6c20c8

Branch: refs/heads/master
Commit: ac6c20c840b75d3e0bf9349684db9f6286c88917
Parents: ffe788e
Author: sunlan <sunlan@apache.org>
Authored: Thu May 25 20:21:05 2017 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Thu May 25 20:21:05 2017 +0800

----------------------------------------------------------------------
 .../apache/groovy/parser/antlr4/GroovyParser.g4 | 14 ++++--
 .../apache/groovy/parser/antlr4/AstBuilder.java | 48 +++++++++-----------
 2 files changed, 30 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/ac6c20c8/subprojects/parser-antlr4/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4
b/subprojects/parser-antlr4/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4
index d4518b5..1d424d1 100644
--- a/subprojects/parser-antlr4/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4
+++ b/subprojects/parser-antlr4/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4
@@ -364,8 +364,7 @@ variableDeclaratorId
     ;
 
 variableInitializer
-    :   statementExpression
-    |   standardLambda
+    :   enhancedStatementExpression
     ;
 
 variableInitializers
@@ -679,7 +678,7 @@ locals[boolean resourcesExists = false]
         |
             nls finallyBlock
         |
-            // a catch or finally clause is required unless it's try-with-resources
+            // try-with-resources can have no catche and finally clauses
             { $resourcesExists }?<fail={"catch or finally clauses are required for try-catch
statement"}>
         )
     ;
@@ -792,7 +791,12 @@ castParExpression
     ;
 
 parExpression
-    :   LPAREN (statementExpression | standardLambda) rparen
+    :   LPAREN enhancedStatementExpression rparen
+    ;
+
+enhancedStatementExpression
+    :   statementExpression
+    |   standardLambda
     ;
 
 expressionList[boolean canSpread]
@@ -920,7 +924,7 @@ expression
                            |   POWER_ASSIGN
                            |   ELVIS_ASSIGN
                            ) nls
-                     (statementExpression | standardLambda)                             
   #assignmentExprAlt
+                     enhancedStatementExpression                                        
   #assignmentExprAlt
     ;
 
 commandExpression

http://git-wip-us.apache.org/repos/asf/groovy/blob/ac6c20c8/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 451cff8..a7b9e97 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
@@ -127,7 +127,6 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Deque;
-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.LinkedList;
@@ -1650,17 +1649,9 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object>
implements Groov
             return EmptyExpression.INSTANCE;
         }
 
-        if (asBoolean(ctx.statementExpression())) {
-            return this.configureAST(
-                    ((ExpressionStatement) this.visit(ctx.statementExpression())).getExpression(),
-                    ctx);
-        }
-
-        if (asBoolean(ctx.standardLambda())) {
-            return this.configureAST(this.visitStandardLambda(ctx.standardLambda()), ctx);
-        }
-
-        throw createParsingFailedException("Unsupported variable initializer: " + ctx.getText(),
ctx);
+        return this.configureAST(
+                this.visitEnhancedStatementExpression(ctx.enhancedStatementExpression()),
+                ctx);
     }
 
     @Override
@@ -1824,15 +1815,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object>
implements Groov
 
     @Override
     public Expression visitParExpression(ParExpressionContext ctx) {
-        Expression expression;
-
-        if (asBoolean(ctx.statementExpression())) {
-            expression = ((ExpressionStatement) this.visit(ctx.statementExpression())).getExpression();
-        } else if (asBoolean(ctx.standardLambda())) {
-            expression = this.visitStandardLambda(ctx.standardLambda());
-        } else {
-            throw createParsingFailedException("Unsupported parentheses expression: " + ctx.getText(),
ctx);
-        }
+        Expression expression = this.visitEnhancedStatementExpression(ctx.enhancedStatementExpression());
 
         expression.putNodeMetaData(IS_INSIDE_PARENTHESES, true);
 
@@ -1847,6 +1830,21 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object>
implements Groov
         return this.configureAST(expression, ctx);
     }
 
+    @Override public Expression visitEnhancedStatementExpression(EnhancedStatementExpressionContext
ctx) {
+        Expression expression;
+
+        if (asBoolean(ctx.statementExpression())) {
+            expression = ((ExpressionStatement) this.visit(ctx.statementExpression())).getExpression();
+        } else if (asBoolean(ctx.standardLambda())) {
+            expression = this.visitStandardLambda(ctx.standardLambda());
+        } else {
+            throw createParsingFailedException("Unsupported enhanced statement expression:
" + ctx.getText(), ctx);
+        }
+
+        return this.configureAST(expression, ctx);
+    }
+
+
     @Override
     public Expression visitPathExpression(PathExpressionContext ctx) {
         return this.configureAST(
@@ -2603,9 +2601,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object>
implements Groov
                     new BinaryExpression(
                             this.configureAST(new TupleExpression(leftExpr), ctx.left),
                             this.createGroovyToken(ctx.op),
-                            asBoolean(ctx.statementExpression())
-                                    ? ((ExpressionStatement) this.visit(ctx.statementExpression())).getExpression()
-                                    : this.visitStandardLambda(ctx.standardLambda())),
+                            this.visitEnhancedStatementExpression(ctx.enhancedStatementExpression())),
                     ctx);
         }
 
@@ -2632,9 +2628,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object>
implements Groov
                 new BinaryExpression(
                         leftExpr,
                         this.createGroovyToken(ctx.op),
-                        asBoolean(ctx.statementExpression())
-                                ? ((ExpressionStatement) this.visit(ctx.statementExpression())).getExpression()
-                                : this.visitStandardLambda(ctx.standardLambda())),
+                        this.visitEnhancedStatementExpression(ctx.enhancedStatementExpression())),
                 ctx);
     }
 


Mime
View raw message