From 89a11db9f553737c653e7c1996d0e5c368496608 Mon Sep 17 00:00:00 2001 From: Paul King Date: Sat, 8 Sep 2018 10:22:37 +1000 Subject: [PATCH] GROOVY-8778: Cast short-hand breaks for empty map --- 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(-) diff --git a/src/antlr/GroovyParser.g4 b/src/antlr/GroovyParser.g4 index f2a3e2fd6ed..72d6fab6e5a 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 diff --git a/src/test/groovy/GroovyTruthTest.groovy b/src/test/groovy/GroovyTruthTest.groovy index 8034e59fb91..e602125a43e 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 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 ce8ee318b76..16bda16ce62 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 Expression visitPathElement(PathElementContext ctx) { 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 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(