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/09/16 02:30:55 UTC

[groovy] 01/01: GROOVY-10236: Parser fails to parse a single-statement lambda when omitting parentheses for method call

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

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

commit 98bdfae8bc00a500901652ac229b9b1a0aa62c63
Author: Daniel Sun <su...@apache.org>
AuthorDate: Thu Sep 16 10:30:40 2021 +0800

    GROOVY-10236: Parser fails to parse a single-statement lambda when omitting parentheses for method call
---
 src/antlr/GroovyParser.g4                          |  2 +-
 .../apache/groovy/parser/antlr4/AstBuilder.java    |  4 +--
 src/test/groovy/bugs/Groovy10236.groovy            | 37 ++++++++++++++++++++++
 3 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/src/antlr/GroovyParser.g4 b/src/antlr/GroovyParser.g4
index 3f486f7..455f9e3 100644
--- a/src/antlr/GroovyParser.g4
+++ b/src/antlr/GroovyParser.g4
@@ -486,7 +486,7 @@ standardLambdaParameters
 
 lambdaBody
     :   block
-    |   expression
+    |   statementExpression
     ;
 
 // CLOSURE
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 9743794..052dcfd 100644
--- a/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -4010,8 +4010,8 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
 
     @Override
     public Statement visitLambdaBody(final LambdaBodyContext ctx) {
-        if (asBoolean(ctx.expression())) {
-            return configureAST(new ExpressionStatement((Expression) this.visit(ctx.expression())), ctx);
+        if (asBoolean(ctx.statementExpression())) {
+            return configureAST((ExpressionStatement) this.visit(ctx.statementExpression()), ctx);
         }
 
         if (asBoolean(ctx.block())) {
diff --git a/src/test/groovy/bugs/Groovy10236.groovy b/src/test/groovy/bugs/Groovy10236.groovy
new file mode 100644
index 0000000..2ffac11
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy10236.groovy
@@ -0,0 +1,37 @@
+/*
+ *  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.
+ */
+package bugs
+
+import groovy.transform.CompileStatic
+import org.junit.Test
+
+import static groovy.test.GroovyAssert.assertScript
+
+@CompileStatic
+final class Groovy10236 {
+
+    @Test
+    void testOmittingParenthesesInLambdaBody() {
+        assertScript '''
+            def same(obj) { obj }
+            
+            assert ['1', '2', '3'] == [1, 2, 3].stream().map(num -> same "$num").toList()
+        '''
+    }
+}