You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2018/09/08 22:13:12 UTC

groovy git commit: GROOVY-8778: Cast short-hand breaks for empty map (closes #792)

Repository: groovy
Updated Branches:
  refs/heads/master 6a6e1f0ff -> 522fd4996


GROOVY-8778: Cast short-hand breaks for empty map (closes #792)


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

Branch: refs/heads/master
Commit: 522fd4996b6314caa05d16590f3896bf750eb83e
Parents: 6a6e1f0
Author: Paul King <pa...@asert.com.au>
Authored: Sat Sep 8 10:22:37 2018 +1000
Committer: Paul King <pa...@asert.com.au>
Committed: Sun Sep 9 08:12:16 2018 +1000

----------------------------------------------------------------------
 src/antlr/GroovyParser.g4                          |  2 +-
 src/test/groovy/GroovyTruthTest.groovy             |  4 +++-
 .../apache/groovy/parser/antlr4/AstBuilder.java    | 17 ++++++++---------
 3 files changed, 12 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/522fd499/src/antlr/GroovyParser.g4
----------------------------------------------------------------------
diff --git a/src/antlr/GroovyParser.g4 b/src/antlr/GroovyParser.g4
index f2a3e2f..72d6fab 100644
--- a/src/antlr/GroovyParser.g4
+++ b/src/antlr/GroovyParser.g4
@@ -1013,7 +1013,7 @@ indexPropertyArgs
     ;
 
 namedPropertyArgs
-    :   LBRACK mapEntryList RBRACK
+    :   LBRACK (mapEntryList | COLON) RBRACK
     ;
 
 primary

http://git-wip-us.apache.org/repos/asf/groovy/blob/522fd499/src/test/groovy/GroovyTruthTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/GroovyTruthTest.groovy b/src/test/groovy/GroovyTruthTest.groovy
index 8034e59..e602125 100644
--- a/src/test/groovy/GroovyTruthTest.groovy
+++ b/src/test/groovy/GroovyTruthTest.groovy
@@ -43,8 +43,10 @@ class GroovyTruthTest extends GroovyTestCase {
         testTrue([1])
         testFalse([].toArray())
 
-        testFalse [:]
+        testFalse Collections.EMPTY_MAP
+        testFalse([:])
         testTrue([bla: 'some value'])
+
         testTrue 1234
         testFalse 0
         testTrue 0.3f

http://git-wip-us.apache.org/repos/asf/groovy/blob/522fd499/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 ce8ee31..16bda16 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
@@ -2332,19 +2332,18 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
             return configureAST(
                     new BinaryExpression(baseExpr, createGroovyToken(tuple.getFirst()), tuple.getSecond(), isSafeChain || asBoolean(ctx.indexPropertyArgs().QUESTION())),
                     ctx);
-        } else if (asBoolean(ctx.namedPropertyArgs())) { // this is a special way to new instance, e.g. Person[name: 'Daniel.Sun', location: 'Shanghai']
+        } else if (asBoolean(ctx.namedPropertyArgs())) { // this is a special way to signify a cast, e.g. Person[name: 'Daniel.Sun', location: 'Shanghai']
             List<MapEntryExpression> mapEntryExpressionList =
                     this.visitNamedPropertyArgs(ctx.namedPropertyArgs());
 
             Expression right;
-            if (mapEntryExpressionList.size() == 1) {
-                MapEntryExpression mapEntryExpression = mapEntryExpressionList.get(0);
-
-                if (mapEntryExpression.getKeyExpression() instanceof SpreadMapExpression) {
-                    right = mapEntryExpression.getKeyExpression();
-                } else {
-                    right = mapEntryExpression;
-                }
+            if (mapEntryExpressionList.size() == 0) {
+                // expecting list of MapEntryExpressions later so use SpreadMap to smuggle empty MapExpression to later stages
+                right = configureAST(
+                        new SpreadMapExpression(configureAST(new MapExpression(), ctx.namedPropertyArgs())),
+                        ctx.namedPropertyArgs());
+            } else if (mapEntryExpressionList.size() == 1 && mapEntryExpressionList.get(0).getKeyExpression() instanceof SpreadMapExpression) {
+                right = mapEntryExpressionList.get(0).getKeyExpression();
             } else {
                 ListExpression listExpression =
                         configureAST(