Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_6_X d7db0606a -> 80181003a


Validate var-arg parameter

(cherry picked from commit c04b2a0)


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

Branch: refs/heads/GROOVY_2_6_X
Commit: 80181003a2a2d523cc98449da9cec6ab32b5b08a
Parents: d7db060
Author: sunlan <sun...@apache.org>
Authored: Sun Aug 6 15:13:20 2017 +0800
Committer: sunlan <sun...@apache.org>
Committed: Sun Aug 6 15:30:54 2017 +0800

----------------------------------------------------------------------
 src/main/antlr/GroovyParser.g4                  |  9 ++----
 .../apache/groovy/parser/antlr4/AstBuilder.java | 30 +++++++++++---------
 .../groovy/parser/antlr4/SyntaxErrorTest.groovy |  3 ++
 .../resources/fail/VarArgParameter_01x.groovy   | 22 ++++++++++++++
 4 files changed, 43 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/80181003/src/main/antlr/GroovyParser.g4
----------------------------------------------------------------------
diff --git a/src/main/antlr/GroovyParser.g4 b/src/main/antlr/GroovyParser.g4
index 461a973..c53a28b 100644
--- a/src/main/antlr/GroovyParser.g4
+++ b/src/main/antlr/GroovyParser.g4
@@ -443,8 +443,7 @@ formalParameters
     ;
 
 formalParameterList
-    :   (formalParameter | thisFormalParameter) (COMMA nls formalParameter)* 
(COMMA nls lastFormalParameter)?
-    |   lastFormalParameter
+    :   (formalParameter | thisFormalParameter) (COMMA nls formalParameter)*
     ;
 
 thisFormalParameter
@@ -452,11 +451,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/80181003/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 5dd8118..5c5f966 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
@@ -3399,18 +3399,16 @@ 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);
+
             List<Parameter> list = new ArrayList<>();
-            for (FormalParameterContext formalParameterContext : 
ctx.formalParameter()) {
+            for (FormalParameterContext formalParameterContext : 
formalParameterList) {
                 Parameter parameter = 
visitFormalParameter(formalParameterContext);
                 list.add(parameter);
             }
-            parameterList.addAll(
-                    list);
-        }
-
-        if (asBoolean(ctx.lastFormalParameter())) {
-            
parameterList.add(this.visitLastFormalParameter(ctx.lastFormalParameter()));
+            parameterList.addAll(list);
         }
 
         validateParameterList(parameterList);
@@ -3418,6 +3416,15 @@ public class AstBuilder extends 
GroovyParserBaseVisitor<Object> implements Groov
         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);
@@ -3436,7 +3443,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
@@ -3445,11 +3452,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/80181003/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/80181003/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)

Reply via email to