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 2021/07/24 05:56:42 UTC

[groovy] branch master updated: Minor tweak: extract variable for switch-expression to deconstruct

This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 9bf3f55  Minor tweak: extract variable for switch-expression to deconstruct
9bf3f55 is described below

commit 9bf3f556f71e0dc261e3196cc7652714c1bbe9ff
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Jul 24 13:56:25 2021 +0800

    Minor tweak: extract variable for switch-expression to deconstruct
---
 src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java b/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
index 2f53780..621bc0c 100644
--- a/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -351,6 +351,8 @@ import static org.codehaus.groovy.ast.ClassHelper.isPrimitiveVoid;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.assignX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.callX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.closureX;
+import static org.codehaus.groovy.ast.tools.GeneralUtils.declS;
+import static org.codehaus.groovy.ast.tools.GeneralUtils.localVarX;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.returnS;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.stmt;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.varX;
@@ -1180,9 +1182,11 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
                 throw createParsingFailedException("default case should appear at last", defaultStatementList.get(0));
             }
 
+            String variableName = "__$$sev" + switchExpressionVariableSeq++;
+            Statement declarationStatement = declS(localVarX(variableName), this.visitExpressionInPar(ctx.expressionInPar()));
             SwitchStatement switchStatement = configureAST(
                     new SwitchStatement(
-                            this.visitExpressionInPar(ctx.expressionInPar()),
+                            varX(variableName),
                             caseStatementList,
                             defaultStatementListSize == 0 ? EmptyStatement.INSTANCE : defaultStatementList.get(0)
                     ),
@@ -1190,7 +1194,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
 
             MethodCallExpression callClosure = callX(
                     configureAST(
-                            closureX(null, createBlockStatement(switchStatement)),
+                            closureX(null, createBlockStatement(declarationStatement, switchStatement)),
                             ctx
                     ), "call");
             callClosure.setImplicitThis(false);
@@ -1200,6 +1204,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
             switchExpressionRuleContextStack.pop();
         }
     }
+    private int switchExpressionVariableSeq;
 
     @Override
     @SuppressWarnings("unchecked")