groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [2/2] groovy git commit: Minor refactoring: Remove duplicated code in `AstBuilder`
Date Sat, 24 Nov 2018 14:24:35 GMT
Minor refactoring: Remove duplicated code in `AstBuilder`


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

Branch: refs/heads/master
Commit: 885d8f7ece4c80bf08254431ac24ad14349140f5
Parents: 6bb02b1
Author: Daniel Sun <sunlan@apache.org>
Authored: Sat Nov 24 22:24:25 2018 +0800
Committer: Daniel Sun <sunlan@apache.org>
Committed: Sat Nov 24 22:24:25 2018 +0800

----------------------------------------------------------------------
 .../apache/groovy/parser/antlr4/AstBuilder.java | 26 +++++++++++---------
 1 file changed, 14 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/885d8f7e/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 13a862e..5671afc 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
@@ -305,6 +305,7 @@ import static org.apache.groovy.parser.antlr4.GroovyLangParser.SUB;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.ShiftExprAltContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.StandardLambdaExpressionContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.StandardLambdaParametersContext;
+import static org.apache.groovy.parser.antlr4.GroovyLangParser.StatementContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.StatementsContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.StringLiteralAltContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.StringLiteralContext;
@@ -743,21 +744,24 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object>
implements Groov
 
     @Override
     public WhileStatement visitWhileStmtAlt(WhileStmtAltContext ctx) {
-        Expression conditionExpression = this.visitExpressionInPar(ctx.expressionInPar());
-        BooleanExpression booleanExpression =
-                configureAST(
-                        new BooleanExpression(conditionExpression), conditionExpression);
-
-        Statement loopBlock = this.unpackStatement((Statement) this.visit(ctx.statement()));
+        Tuple2<BooleanExpression, Statement> conditionAndBlock = createLoopConditionExpressionAndBlock(ctx.expressionInPar(),
ctx.statement());
 
         return configureAST(
-                new WhileStatement(booleanExpression, asBoolean(loopBlock) ? loopBlock :
EmptyStatement.INSTANCE),
+                new WhileStatement(conditionAndBlock.getV1(), asBoolean(conditionAndBlock.getV2())
? conditionAndBlock.getV2() : EmptyStatement.INSTANCE),
                 ctx);
     }
 
     @Override
     public DoWhileStatement visitDoWhileStmtAlt(DoWhileStmtAltContext ctx) {
-        Expression conditionExpression = this.visitExpressionInPar(ctx.expressionInPar());
+        Tuple2<BooleanExpression, Statement> conditionAndBlock = createLoopConditionExpressionAndBlock(ctx.expressionInPar(),
ctx.statement());
+
+        return configureAST(
+                new DoWhileStatement(conditionAndBlock.getV1(), asBoolean(conditionAndBlock.getV2())
? conditionAndBlock.getV2() : EmptyStatement.INSTANCE),
+                ctx);
+    }
+
+    private Tuple2<BooleanExpression, Statement> createLoopConditionExpressionAndBlock(ExpressionInParContext
eipc, StatementContext sc) {
+        Expression conditionExpression = this.visitExpressionInPar(eipc);
 
         BooleanExpression booleanExpression =
                 configureAST(
@@ -765,11 +769,9 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object>
implements Groov
                         conditionExpression
                 );
 
-        Statement loopBlock = this.unpackStatement((Statement) this.visit(ctx.statement()));
+        Statement loopBlock = this.unpackStatement((Statement) this.visit(sc));
 
-        return configureAST(
-                new DoWhileStatement(booleanExpression, asBoolean(loopBlock) ? loopBlock
: EmptyStatement.INSTANCE),
-                ctx);
+        return tuple(booleanExpression, loopBlock);
     }
 
     @Override


Mime
View raw message