groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject groovy git commit: Forbid `var` to declare methods
Date Thu, 08 Mar 2018 08:27:34 GMT
Repository: groovy
Updated Branches:
  refs/heads/master 2ac378a74 -> 9ede855f3


Forbid `var` to declare methods


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

Branch: refs/heads/master
Commit: 9ede855f3fc82a98c1da644b3ed25c63e299b48a
Parents: 2ac378a
Author: sunlan <sunlan@apache.org>
Authored: Thu Mar 8 16:27:26 2018 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Thu Mar 8 16:27:26 2018 +0800

----------------------------------------------------------------------
 src/antlr/GroovyLexer.g4                        |  2 +-
 .../apache/groovy/parser/antlr4/AstBuilder.java |  5 +++++
 .../groovy/parser/antlr4/SyntaxErrorTest.groovy |  1 +
 .../src/test/resources/fail/Var_02x.groovy      | 20 ++++++++++++++++++++
 4 files changed, 27 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/9ede855f/src/antlr/GroovyLexer.g4
----------------------------------------------------------------------
diff --git a/src/antlr/GroovyLexer.g4 b/src/antlr/GroovyLexer.g4
index 0ae2866..bf855bf 100644
--- a/src/antlr/GroovyLexer.g4
+++ b/src/antlr/GroovyLexer.g4
@@ -347,7 +347,7 @@ IN              : 'in';
 TRAIT           : 'trait';
 THREADSAFE      : 'threadsafe'; // reserved keyword
 
-// Java10 keywords
+// the reserved type name of Java10
 VAR             : 'var';
 
 // ยง3.9 Keywords

http://git-wip-us.apache.org/repos/asf/groovy/blob/9ede855f/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 7cebb18..5af6995 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
@@ -1550,6 +1550,11 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object>
implements Groov
         validateMethodDeclaration(ctx);
 
         ModifierManager modifierManager = createModifierManager(ctx);
+
+        if (modifierManager.contains(VAR)) {
+            throw createParsingFailedException("var cannot be used for method declarations",
ctx);
+        }
+
         String methodName = this.visitMethodName(ctx.methodName());
         ClassNode returnType = this.visitReturnType(ctx.returnType());
         Parameter[] parameters = this.visitFormalParameters(ctx.formalParameters());

http://git-wip-us.apache.org/repos/asf/groovy/blob/9ede855f/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 93f741d..a956e4c 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
@@ -214,6 +214,7 @@ class SyntaxErrorTest extends GroovyTestCase {
 
     void "test groovy core - var"() {
         TestUtils.doRunAndShouldFail('fail/Var_01x.groovy');
+        TestUtils.doRunAndShouldFail('fail/Var_02x.groovy');
     }
 
     /**************************************/

http://git-wip-us.apache.org/repos/asf/groovy/blob/9ede855f/subprojects/parser-antlr4/src/test/resources/fail/Var_02x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/resources/fail/Var_02x.groovy b/subprojects/parser-antlr4/src/test/resources/fail/Var_02x.groovy
new file mode 100644
index 0000000..8855299
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/fail/Var_02x.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.
+ */
+
+var someMethod() {}


Mime
View raw message