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/07/15 07:06:12 UTC

groovy git commit: Validate the duplicated parameters

Repository: groovy
Updated Branches:
  refs/heads/master ef33ab744 -> df389dd6c


Validate the duplicated parameters


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

Branch: refs/heads/master
Commit: df389dd6cadfbc070f92527b8a8343373c18e278
Parents: ef33ab7
Author: sunlan <su...@apache.org>
Authored: Sat Jul 15 15:05:56 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Sat Jul 15 15:05:56 2017 +0800

----------------------------------------------------------------------
 .../apache/groovy/parser/antlr4/GroovyParser.g4 |  2 +-
 .../apache/groovy/parser/antlr4/AstBuilder.java | 18 +++++++++++++++++
 .../groovy/parser/antlr4/SyntaxErrorTest.groovy |  2 ++
 .../resources/fail/MethodDeclaration_02x.groovy | 21 ++++++++++++++++++++
 .../resources/fail/MethodDeclaration_03x.groovy | 21 ++++++++++++++++++++
 5 files changed, 63 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/df389dd6/subprojects/parser-antlr4/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4 b/subprojects/parser-antlr4/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4
index b7efd4f..c612df1 100644
--- a/subprojects/parser-antlr4/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4
+++ b/subprojects/parser-antlr4/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4
@@ -1061,7 +1061,7 @@ primary
     |   SUPER                                                                               #superPrmrAlt
     |   parExpression                                                                       #parenPrmrAlt
     |   closure                                                                             #closurePrmrAlt
-    |   lambdaExpression                                                                              #lambdaPrmrAlt
+    |   lambdaExpression                                                                    #lambdaPrmrAlt
     |   list                                                                                #listPrmrAlt
     |   map                                                                                 #mapPrmrAlt
     |   builtInType                                                                         #typePrmrAlt

http://git-wip-us.apache.org/repos/asf/groovy/blob/df389dd6/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 9daacbf..72dfa36 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
@@ -3324,9 +3324,27 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
             parameterList.add(this.visitLastFormalParameter(ctx.lastFormalParameter()));
         }
 
+        validateParameterList(parameterList);
+
         return parameterList.toArray(new Parameter[0]);
     }
 
+    private void validateParameterList(List<Parameter> parameterList) {
+        for (int n = parameterList.size(), i = n - 1; i >= 0; i--) {
+            Parameter parameter = parameterList.get(i);
+
+            for (Parameter otherParameter : parameterList) {
+                if (otherParameter == parameter) {
+                    continue;
+                }
+
+                if (otherParameter.getName().equals(parameter.getName())) {
+                    throw createParsingFailedException("Duplicated parameter '" + parameter.getName() + "' found.", parameter);
+                }
+            }
+        }
+    }
+
     @Override
     public Parameter visitFormalParameter(FormalParameterContext ctx) {
         return this.processFormalParameter(ctx, ctx.variableModifiersOpt(), ctx.type(), null, ctx.variableDeclaratorId(), ctx.expression());

http://git-wip-us.apache.org/repos/asf/groovy/blob/df389dd6/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 432be7b..7d4ffa1 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
@@ -149,6 +149,8 @@ class SyntaxErrorTest extends GroovyTestCase {
 
     void "test groovy core - MethodDeclaration"() {
         TestUtils.shouldFail('fail/MethodDeclaration_01.groovy');
+        TestUtils.doRunAndShouldFail('fail/MethodDeclaration_02x.groovy');
+        TestUtils.doRunAndShouldFail('fail/MethodDeclaration_03x.groovy');
     }
 
     void "test groovy core - ConstructorDeclaration"() {

http://git-wip-us.apache.org/repos/asf/groovy/blob/df389dd6/subprojects/parser-antlr4/src/test/resources/fail/MethodDeclaration_02x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/resources/fail/MethodDeclaration_02x.groovy b/subprojects/parser-antlr4/src/test/resources/fail/MethodDeclaration_02x.groovy
new file mode 100644
index 0000000..9d766f3
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/fail/MethodDeclaration_02x.groovy
@@ -0,0 +1,21 @@
+/*
+ *  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 c = {a, a -> println a}
+c(1, 2)

http://git-wip-us.apache.org/repos/asf/groovy/blob/df389dd6/subprojects/parser-antlr4/src/test/resources/fail/MethodDeclaration_03x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/resources/fail/MethodDeclaration_03x.groovy b/subprojects/parser-antlr4/src/test/resources/fail/MethodDeclaration_03x.groovy
new file mode 100644
index 0000000..d2c61ab
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/fail/MethodDeclaration_03x.groovy
@@ -0,0 +1,21 @@
+/*
+ *  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 c(a, a) { println a}
+c(1, 2)