groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject groovy git commit: Treat conditional statement as expression
Date Thu, 05 Oct 2017 04:47:20 GMT
Repository: groovy
Updated Branches:
  refs/heads/master 1783be3ae -> 35ae8e484


Treat conditional statement as expression


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

Branch: refs/heads/master
Commit: 35ae8e484020f2d11b2dd9c7efa3740ee527fa70
Parents: 1783be3
Author: sunlan <sunlan@apache.org>
Authored: Thu Oct 5 12:47:10 2017 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Thu Oct 5 12:47:10 2017 +0800

----------------------------------------------------------------------
 src/antlr/GroovyParser.g4                       |  1 +
 .../apache/groovy/parser/antlr4/AstBuilder.java | 18 ++++
 .../parser/antlr4/GroovyParserTest.groovy       |  4 +
 .../ConditionalStatementAsExpression_01x.groovy | 91 ++++++++++++++++++++
 4 files changed, 114 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/35ae8e48/src/antlr/GroovyParser.g4
----------------------------------------------------------------------
diff --git a/src/antlr/GroovyParser.g4 b/src/antlr/GroovyParser.g4
index 23e7e7a..dd0bcfb 100644
--- a/src/antlr/GroovyParser.g4
+++ b/src/antlr/GroovyParser.g4
@@ -784,6 +784,7 @@ expressionListElement[boolean canSpread]
 enhancedStatementExpression
     :   statementExpression
     |   standardLambdaExpression
+    |   conditionalStatement        // treat conditional statement as expression
     ;
 
 /**

http://git-wip-us.apache.org/repos/asf/groovy/blob/35ae8e48/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 157a5be..c69cf3c 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
@@ -1979,6 +1979,24 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object>
implements Groov
             expression = ((ExpressionStatement) this.visit(ctx.statementExpression())).getExpression();
         } else if (asBoolean(ctx.standardLambdaExpression())) {
             expression = this.visitStandardLambdaExpression(ctx.standardLambdaExpression());
+        } else if (asBoolean(ctx.conditionalStatement())) {
+            expression =
+                    configureAST(
+                        createCallMethodCallExpression(
+                            configureAST(
+                                    new ClosureExpression(
+                                        Parameter.EMPTY_ARRAY,
+                                        configureAST(
+                                            this.createBlockStatement(this.visitConditionalStatement(ctx.conditionalStatement())),
+                                            ctx
+                                        )
+                                    ),
+                                    ctx
+                            ),
+                            new ArgumentListExpression()
+                        ),
+                        ctx
+                    );
         } else {
             throw createParsingFailedException("Unsupported enhanced statement expression:
" + ctx.getText(), ctx);
         }

http://git-wip-us.apache.org/repos/asf/groovy/blob/35ae8e48/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
index fd65344..abadf5f 100644
--- a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
+++ b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
@@ -373,6 +373,10 @@ class GroovyParserTest extends GroovyTestCase {
         doRunAndTest('core/SafeChainOperator.groovy');
     }
 
+    void "test groovy core - ConditionalStatementAsExpression"() {
+        doRunAndTest('core/ConditionalStatementAsExpression_01x.groovy');
+    }
+
     void "test groovy core - BUG"() {
         doRunAndTest('bugs/BUG-GROOVY-4757.groovy');
         doRunAndTest('bugs/BUG-GROOVY-5652.groovy');

http://git-wip-us.apache.org/repos/asf/groovy/blob/35ae8e48/subprojects/parser-antlr4/src/test/resources/core/ConditionalStatementAsExpression_01x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/resources/core/ConditionalStatementAsExpression_01x.groovy
b/subprojects/parser-antlr4/src/test/resources/core/ConditionalStatementAsExpression_01x.groovy
new file mode 100644
index 0000000..3bc5d0f
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/core/ConditionalStatementAsExpression_01x.groovy
@@ -0,0 +1,91 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+import groovy.transform.CompileStatic
+
+def testConditionalStatementAsExpression() {
+    def a = 1
+    def b = 2
+    def max = if (a > b) a else b
+    assert 2 == max
+
+    def c = 1
+    def result = switch (c) {
+        case 1:
+            2; break
+        default:
+            0
+    }
+    assert 2 == result
+
+    def d = 6
+    d = if (d > 0) 1 else if (d == 0) 0 else -1
+    assert 1 == d
+
+    def e = 7
+    e = switch (e) {
+        case 5:
+            6; break
+        case 6:
+            7; break
+        case 7:
+            8; break
+        default:
+            0
+    }
+    assert 8 == e
+}
+
+testConditionalStatementAsExpression()
+
+@CompileStatic
+def testCsConditionalStatementAsExpression() {
+    def a = 1
+    def b = 2
+    def max = if (a > b) a else b
+    assert 2 == max
+
+    def c = 1
+    def result = switch (c) {
+        case 1:
+            2; break
+        default:
+            0
+    }
+    assert 2 == result
+
+    def d = 6
+    d = if (d > 0) 1 else if (d == 0) 0 else -1
+    assert 1 == d
+
+    def e = 7
+    e = switch (e) {
+        case 5:
+            6; break
+        case 6:
+            7; break
+        case 7:
+            8; break
+        default:
+            0
+    }
+    assert 8 == e
+}
+
+testCsConditionalStatementAsExpression()


Mime
View raw message