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 2018/05/25 10:40:07 UTC

groovy git commit: GROOVY-8606: Provide friendly error message for invalid lambda body

Repository: groovy
Updated Branches:
  refs/heads/master 22d872a3c -> 1bd642488


GROOVY-8606: Provide friendly error message for invalid lambda body


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

Branch: refs/heads/master
Commit: 1bd642488104ca8204bd45c1c1cf4d304d7d564f
Parents: 22d872a
Author: sunlan <su...@apache.org>
Authored: Fri May 25 18:39:40 2018 +0800
Committer: sunlan <su...@apache.org>
Committed: Fri May 25 18:39:40 2018 +0800

----------------------------------------------------------------------
 src/antlr/GroovyParser.g4                       |  1 +
 .../apache/groovy/parser/antlr4/AstBuilder.java |  8 ++++++--
 .../groovy/parser/antlr4/SyntaxErrorTest.groovy |  4 ++++
 .../src/test/resources/fail/Lambda_01x.groovy   | 20 ++++++++++++++++++++
 4 files changed, 31 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/1bd64248/src/antlr/GroovyParser.g4
----------------------------------------------------------------------
diff --git a/src/antlr/GroovyParser.g4 b/src/antlr/GroovyParser.g4
index 77867da..dbc946c 100644
--- a/src/antlr/GroovyParser.g4
+++ b/src/antlr/GroovyParser.g4
@@ -501,6 +501,7 @@ standardLambdaParameters
 lambdaBody
 	:	block
 	|	statementExpression
+	|   statement // !!! Error Alternative !!!
 	;
 
 

http://git-wip-us.apache.org/repos/asf/groovy/blob/1bd64248/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 031680f..22e35ca 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
@@ -3720,12 +3720,16 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
 
     @Override
     public Statement visitLambdaBody(LambdaBodyContext ctx) {
+        if (asBoolean(ctx.block())) {
+            return configureAST(this.visitBlock(ctx.block()), ctx);
+        }
+
         if (asBoolean(ctx.statementExpression())) {
             return configureAST((ExpressionStatement) this.visit(ctx.statementExpression()), ctx);
         }
 
-        if (asBoolean(ctx.block())) {
-            return configureAST(this.visitBlock(ctx.block()), ctx);
+        if (asBoolean(ctx.statement())) {
+            throw createParsingFailedException("Statements should be wrapped in a block, e.g. () -> { some statements here }", ctx);
         }
 
         throw createParsingFailedException("Unsupported lambda body: " + ctx.getText(), ctx);

http://git-wip-us.apache.org/repos/asf/groovy/blob/1bd64248/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
index 868269f..1669b32 100644
--- a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
+++ b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
@@ -231,6 +231,10 @@ class SyntaxErrorTest extends GroovyTestCase {
         TestUtils.doRunAndShouldFail('fail/Import_02x.groovy');
     }
 
+    void "test groovy core - Lambda"() {
+        TestUtils.doRunAndShouldFail('fail/Lambda_01x.groovy');
+    }
+
     /**************************************/
     static unzipScriptAndShouldFail(String entryName, List ignoreClazzList, Map<String, String> replacementsMap=[:], boolean toCheckNewParserOnly = false) {
         ignoreClazzList.addAll(TestUtils.COMMON_IGNORE_CLASS_LIST)

http://git-wip-us.apache.org/repos/asf/groovy/blob/1bd64248/subprojects/parser-antlr4/src/test/resources/fail/Lambda_01x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/resources/fail/Lambda_01x.groovy b/subprojects/parser-antlr4/src/test/resources/fail/Lambda_01x.groovy
new file mode 100644
index 0000000..edc0ab1
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/fail/Lambda_01x.groovy
@@ -0,0 +1,20 @@
+/*
+ *  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 a = () -> assert 1 == 1