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 402788d31d GROOVY-11055: Support lambda expression as named value
402788d31d is described below

commit 402788d31d74f6f6edd38e51636fbf1624dc307b
Author: Daniel Sun <sun...@apache.org>
AuthorDate: Sat Jun 15 09:48:03 2024 +0900

    GROOVY-11055: Support lambda expression as named value
---
 src/antlr/GroovyParser.g4                          | 17 ++++++++-----
 .../apache/groovy/parser/antlr4/AstBuilder.java    | 23 +++++++++++++++--
 src/test-resources/bugs/GROOVY-11055.groovy        | 29 ++++++++++++++++++++++
 .../groovy/parser/antlr4/GroovyParserTest.groovy   |  4 +++
 4 files changed, 65 insertions(+), 8 deletions(-)

diff --git a/src/antlr/GroovyParser.g4 b/src/antlr/GroovyParser.g4
index f61d713adb..1b9cec7fd5 100644
--- a/src/antlr/GroovyParser.g4
+++ b/src/antlr/GroovyParser.g4
@@ -743,6 +743,11 @@ expressionListElement[boolean canSpread]
     :   MUL? expression
     ;
 
+enhancedExpression
+    :   expression
+    |   standardLambdaExpression
+    ;
+
 enhancedStatementExpression
     :   statementExpression
     |   standardLambdaExpression
@@ -1096,20 +1101,20 @@ options { baseContext = mapEntryList; }
     ;
 
 mapEntry
-    :   mapEntryLabel COLON nls expression
-    |   MUL COLON nls expression
+    :   mapEntryLabel COLON nls enhancedExpression
+    |   MUL COLON nls enhancedExpression
     ;
 
 namedPropertyArg
 options { baseContext = mapEntry; }
-    :   namedPropertyArgLabel COLON nls expression
-    |   MUL COLON nls expression
+    :   namedPropertyArgLabel COLON nls enhancedExpression
+    |   MUL COLON nls enhancedExpression
     ;
 
 namedArg
 options { baseContext = mapEntry; }
-    :   namedArgLabel COLON nls expression
-    |   MUL COLON nls expression
+    :   namedArgLabel COLON nls enhancedExpression
+    |   MUL COLON nls enhancedExpression
     ;
 
 mapEntryLabel
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 273df2a37e..be763c47bd 100644
--- a/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -20,7 +20,11 @@ package org.apache.groovy.parser.antlr4;
 
 import groovy.lang.Tuple2;
 import groovy.lang.Tuple3;
-import groovy.transform.*;
+import groovy.transform.CompileStatic;
+import groovy.transform.NonSealed;
+import groovy.transform.Sealed;
+import groovy.transform.Trait;
+import groovy.transform.TupleConstructor;
 import org.antlr.v4.runtime.ANTLRErrorListener;
 import org.antlr.v4.runtime.CharStream;
 import org.antlr.v4.runtime.CharStreams;
@@ -2405,6 +2409,21 @@ public class AstBuilder extends 
GroovyParserBaseVisitor<Object> {
         return 
this.visitEnhancedStatementExpression(ctx.enhancedStatementExpression());
     }
 
+    @Override
+    public Expression visitEnhancedExpression(final EnhancedExpressionContext 
ctx) {
+        Expression expression;
+
+        if (asBoolean(ctx.expression())) {
+            expression = (Expression) this.visit(ctx.expression());
+        } else if (asBoolean(ctx.standardLambdaExpression())) {
+            expression = 
this.visitStandardLambdaExpression(ctx.standardLambdaExpression());
+        } else {
+            throw createParsingFailedException("Unsupported enhanced 
expression: " + ctx.getText(), ctx);
+        }
+
+        return configureAST(expression, ctx);
+    }
+
     @Override
     public Expression visitEnhancedStatementExpression(final 
EnhancedStatementExpressionContext ctx) {
         Expression expression;
@@ -3462,7 +3481,7 @@ public class AstBuilder extends 
GroovyParserBaseVisitor<Object> {
     @Override
     public MapEntryExpression visitMapEntry(final MapEntryContext ctx) {
         Expression keyExpr;
-        Expression valueExpr = (Expression) this.visit(ctx.expression());
+        Expression valueExpr = 
this.visitEnhancedExpression(ctx.enhancedExpression());
 
         if (asBoolean(ctx.MUL())) {
             keyExpr = configureAST(new SpreadMapExpression(valueExpr), ctx);
diff --git a/src/test-resources/bugs/GROOVY-11055.groovy 
b/src/test-resources/bugs/GROOVY-11055.groovy
new file mode 100644
index 0000000000..e036609f6f
--- /dev/null
+++ b/src/test-resources/bugs/GROOVY-11055.groovy
@@ -0,0 +1,29 @@
+/*
+ *  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.
+ */
+
+def m(Map paramMap) {
+    def name = paramMap.name
+    def hello = paramMap.hello
+    assert 'Hello, Daniel' == hello(name)
+    def hi = paramMap.hi
+    if (hi) assert 'Hi, Daniel' == hi(name)
+}
+
+m(hello: n -> 'Hello, ' + n, name: 'Daniel')
+m(hello: n -> 'Hello, ' + n, hi: n -> 'Hi, ' + n, name: 'Daniel')
diff --git a/src/test/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy 
b/src/test/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
index 9099bac62f..8f03498eb8 100644
--- a/src/test/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
+++ b/src/test/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
@@ -533,4 +533,8 @@ final class GroovyParserTest extends GroovyTestCase {
     void "test groovy core - GROOVY-10659"() {
         doTest('bugs/BUG-GROOVY-10659.groovy');
     }
+
+    void "test groovy core - GROOVY-11055"() {
+        doRunAndTestAntlr4('bugs/GROOVY-11055.groovy')
+    }
 }

Reply via email to