Repository: groovy Updated Branches: refs/heads/GROOVY_2_6_X a7354c298 -> 0282fbfeb
Forbidden using command chain expression in array initializer to avoid ambiguities (cherry picked from commit 2f6894a8f4746aac66542605d818a74f3dd2b7de) Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/0282fbfe Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/0282fbfe Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/0282fbfe Branch: refs/heads/GROOVY_2_6_X Commit: 0282fbfeb9b53897814209074daf759ca7ad0f6d Parents: a7354c2 Author: Daniel Sun <[email protected]> Authored: Sat Aug 18 15:42:39 2018 +0800 Committer: Daniel Sun <[email protected]> Committed: Sat Aug 18 15:46:08 2018 +0800 ---------------------------------------------------------------------- .../apache/groovy/parser/antlr4/AstBuilder.java | 16 +++++++++++++++- .../groovy/parser/antlr4/SyntaxErrorTest.groovy | 4 ++++ .../resources/fail/CommandExpression_01x.groovy | 20 ++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/0282fbfe/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 01e5825..86f8862 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 @@ -2097,13 +2097,20 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov return list; } + private int visitingArrayInitializerCnt = 0; + @Override public List<Expression> visitArrayInitializer(ArrayInitializerContext ctx) { if (!asBoolean(ctx)) { return Collections.emptyList(); } - return this.visitVariableInitializers(ctx.variableInitializers()); + try { + visitingArrayInitializerCnt++; + return this.visitVariableInitializers(ctx.variableInitializers()); + } finally { + visitingArrayInitializerCnt--; + } } @Override @@ -2142,6 +2149,13 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov @Override public ExpressionStatement visitCommandExprAlt(CommandExprAltContext ctx) { + if (visitingArrayInitializerCnt > 0) { + // To avoid ambiguities, command chain expression should not be used in array initializer + // the old parser does not support either, so no breaking changes + // SEE http://groovy.329449.n5.nabble.com/parrot-Command-expressions-in-array-initializer-tt5752273.html + throw createParsingFailedException("Command chain expression can not be used in array initializer", ctx); + } + return configureAST(new ExpressionStatement(this.visitCommandExpression(ctx.commandExpression())), ctx); } http://git-wip-us.apache.org/repos/asf/groovy/blob/0282fbfe/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 d18abfb..af8bd61 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 @@ -49,6 +49,10 @@ class SyntaxErrorTest extends GroovyTestCase { TestUtils.shouldFail('fail/Expression_09.groovy'); } + void "test groovy core - CommandExpression"() { + TestUtils.doRunAndShouldFail('fail/CommandExpression_01x.groovy'); + } + void "test groovy core - Switch"() { TestUtils.shouldFail('fail/Switch_01.groovy'); } http://git-wip-us.apache.org/repos/asf/groovy/blob/0282fbfe/subprojects/parser-antlr4/src/test/resources/fail/CommandExpression_01x.groovy ---------------------------------------------------------------------- diff --git a/subprojects/parser-antlr4/src/test/resources/fail/CommandExpression_01x.groovy b/subprojects/parser-antlr4/src/test/resources/fail/CommandExpression_01x.groovy new file mode 100644 index 0000000..c97b45a --- /dev/null +++ b/subprojects/parser-antlr4/src/test/resources/fail/CommandExpression_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. + */ + +new int[] { a b, c, d } \ No newline at end of file
