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 2017/08/06 07:14:31 UTC
groovy git commit: Validate var-arg parameter
Repository: groovy
Updated Branches:
refs/heads/master 891c35f28 -> c04b2a09d
Validate var-arg parameter
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/c04b2a09
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/c04b2a09
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/c04b2a09
Branch: refs/heads/master
Commit: c04b2a09d9460d17a417e4fd2919efea94201d43
Parents: 891c35f
Author: sunlan <su...@apache.org>
Authored: Sun Aug 6 15:13:20 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Sun Aug 6 15:14:04 2017 +0800
----------------------------------------------------------------------
src/main/antlr/GroovyParser.g4 | 9 ++-----
.../apache/groovy/parser/antlr4/AstBuilder.java | 27 +++++++++++---------
.../groovy/parser/antlr4/SyntaxErrorTest.groovy | 3 +++
.../resources/fail/VarArgParameter_01x.groovy | 22 ++++++++++++++++
4 files changed, 42 insertions(+), 19 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/c04b2a09/src/main/antlr/GroovyParser.g4
----------------------------------------------------------------------
diff --git a/src/main/antlr/GroovyParser.g4 b/src/main/antlr/GroovyParser.g4
index 90099d4..7519e04 100644
--- a/src/main/antlr/GroovyParser.g4
+++ b/src/main/antlr/GroovyParser.g4
@@ -410,8 +410,7 @@ formalParameters
;
formalParameterList
- : (formalParameter | thisFormalParameter) (COMMA nls formalParameter)* (COMMA nls lastFormalParameter)?
- | lastFormalParameter
+ : (formalParameter | thisFormalParameter) (COMMA nls formalParameter)*
;
thisFormalParameter
@@ -419,11 +418,7 @@ thisFormalParameter
;
formalParameter
- : variableModifiersOpt type? variableDeclaratorId (nls ASSIGN nls expression)?
- ;
-
-lastFormalParameter
- : variableModifiersOpt type? ELLIPSIS variableDeclaratorId (nls ASSIGN nls expression)?
+ : variableModifiersOpt type? ELLIPSIS? variableDeclaratorId (nls ASSIGN nls expression)?
;
methodBody
http://git-wip-us.apache.org/repos/asf/groovy/blob/c04b2a09/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 b65505a..d3773f6 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
@@ -3349,22 +3349,30 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
parameterList.add(this.visitThisFormalParameter(ctx.thisFormalParameter()));
}
- if (asBoolean(ctx.formalParameter())) {
+ List<? extends FormalParameterContext> formalParameterList = ctx.formalParameter();
+ if (asBoolean(formalParameterList)) {
+ validateVarArgParameter(formalParameterList);
+
parameterList.addAll(
- ctx.formalParameter().stream()
+ formalParameterList.stream()
.map(this::visitFormalParameter)
.collect(Collectors.toList()));
}
- if (asBoolean(ctx.lastFormalParameter())) {
- parameterList.add(this.visitLastFormalParameter(ctx.lastFormalParameter()));
- }
-
validateParameterList(parameterList);
return parameterList.toArray(new Parameter[0]);
}
+ private void validateVarArgParameter(List<? extends FormalParameterContext> formalParameterList) {
+ for (int i = 0, n = formalParameterList.size(); i < n - 1; i++) {
+ FormalParameterContext formalParameterContext = formalParameterList.get(i);
+ if (asBoolean(formalParameterContext.ELLIPSIS())) {
+ throw createParsingFailedException("The var-arg parameter strs must be the last parameter", formalParameterContext);
+ }
+ }
+ }
+
private void validateParameterList(List<Parameter> parameterList) {
for (int n = parameterList.size(), i = n - 1; i >= 0; i--) {
Parameter parameter = parameterList.get(i);
@@ -3383,7 +3391,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
@Override
public Parameter visitFormalParameter(FormalParameterContext ctx) {
- return this.processFormalParameter(ctx, ctx.variableModifiersOpt(), ctx.type(), null, ctx.variableDeclaratorId(), ctx.expression());
+ return this.processFormalParameter(ctx, ctx.variableModifiersOpt(), ctx.type(), ctx.ELLIPSIS(), ctx.variableDeclaratorId(), ctx.expression());
}
@Override
@@ -3392,11 +3400,6 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
}
@Override
- public Parameter visitLastFormalParameter(LastFormalParameterContext ctx) {
- return this.processFormalParameter(ctx, ctx.variableModifiersOpt(), ctx.type(), ctx.ELLIPSIS(), ctx.variableDeclaratorId(), ctx.expression());
- }
-
- @Override
public List<ModifierNode> visitClassOrInterfaceModifiersOpt(ClassOrInterfaceModifiersOptContext ctx) {
if (asBoolean(ctx.classOrInterfaceModifiers())) {
return this.visitClassOrInterfaceModifiers(ctx.classOrInterfaceModifiers());
http://git-wip-us.apache.org/repos/asf/groovy/blob/c04b2a09/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 0905717..b031654 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
@@ -193,6 +193,9 @@ class SyntaxErrorTest extends GroovyTestCase {
TestUtils.doRunAndShouldFail('fail/ThreadSafe_01x.groovy');
}
+ void "test groovy core - VarArgParameter"() {
+ TestUtils.doRunAndShouldFail('fail/VarArgParameter_01x.groovy');
+ }
/**************************************/
static unzipScriptAndShouldFail(String entryName, List ignoreClazzList, Map<String, String> replacementsMap=[:], boolean toCheckNewParserOnly = false) {
http://git-wip-us.apache.org/repos/asf/groovy/blob/c04b2a09/subprojects/parser-antlr4/src/test/resources/fail/VarArgParameter_01x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/resources/fail/VarArgParameter_01x.groovy b/subprojects/parser-antlr4/src/test/resources/fail/VarArgParameter_01x.groovy
new file mode 100644
index 0000000..ceba8ce
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/fail/VarArgParameter_01x.groovy
@@ -0,0 +1,22 @@
+/*
+ * 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 foo(String... strs, int i) { println i }
+
+foo("me", "you", 42)