Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_6_X dddd234d5 -> f4d96d887


Support `var` keyword of Java10

(cherry picked from commit f30741f)


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

Branch: refs/heads/GROOVY_2_6_X
Commit: f4d96d887291e08f09a49c805ec3d9b22238e616
Parents: dddd234
Author: sunlan <sun...@apache.org>
Authored: Wed Mar 7 16:07:20 2018 +0800
Committer: sunlan <sun...@apache.org>
Committed: Wed Mar 7 16:32:46 2018 +0800

----------------------------------------------------------------------
 src/antlr/GroovyLexer.g4                        |  2 ++
 src/antlr/GroovyParser.g4                       |  5 +++-
 .../apache/groovy/parser/antlr4/AstBuilder.java | 10 ++++++-
 .../groovy/parser/antlr4/ModifierNode.java      | 18 +++++++++++--
 .../parser/antlr4/GroovyParserTest.groovy       |  4 +++
 .../groovy/parser/antlr4/SyntaxErrorTest.groovy |  4 +++
 .../src/test/resources/core/Var_01x.groovy      | 28 ++++++++++++++++++++
 .../src/test/resources/fail/Var_01x.groovy      | 20 ++++++++++++++
 8 files changed, 87 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/f4d96d88/src/antlr/GroovyLexer.g4
----------------------------------------------------------------------
diff --git a/src/antlr/GroovyLexer.g4 b/src/antlr/GroovyLexer.g4
index 402d364..0ae2866 100644
--- a/src/antlr/GroovyLexer.g4
+++ b/src/antlr/GroovyLexer.g4
@@ -347,6 +347,8 @@ IN              : 'in';
 TRAIT           : 'trait';
 THREADSAFE      : 'threadsafe'; // reserved keyword
 
+// Java10 keywords
+VAR             : 'var';
 
 // §3.9 Keywords
 BuiltInPrimitiveType

http://git-wip-us.apache.org/repos/asf/groovy/blob/f4d96d88/src/antlr/GroovyParser.g4
----------------------------------------------------------------------
diff --git a/src/antlr/GroovyParser.g4 b/src/antlr/GroovyParser.g4
index 7514ba2..f964c17 100644
--- a/src/antlr/GroovyParser.g4
+++ b/src/antlr/GroovyParser.g4
@@ -158,6 +158,7 @@ modifier
           |   TRANSIENT
           |   VOLATILE
           |   DEF
+          |   VAR
           )
     ;
 
@@ -194,6 +195,7 @@ variableModifier
     :   annotation
     |   m=( FINAL
           | DEF
+          | VAR
           // Groovy supports declaring local variables as instance/class 
fields,
           // e.g. import groovy.transform.*; @Field static List awe = [1, 2, 3]
           // e.g. import groovy.transform.*; def a = { @Field public List awe 
= [1, 2, 3] }
@@ -1166,7 +1168,7 @@ className
 identifier
     :   Identifier
     |   CapitalizedIdentifier
-
+    |   VAR
     |
         // if 'static' followed by DOT, we can treat them as identifiers, e.g. 
static.unused = { -> }
         { DOT == _input.LT(2).getType() }?
@@ -1220,6 +1222,7 @@ keywords
     |   TRAIT
     |   THREADSAFE
     |   TRY
+    |   VAR
     |   VOLATILE
     |   WHILE
 

http://git-wip-us.apache.org/repos/asf/groovy/blob/f4d96d88/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 5be23c5..bc9a2c5 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
@@ -330,6 +330,7 @@ import static 
org.apache.groovy.parser.antlr4.GroovyLangParser.TypeParameterCont
 import static 
org.apache.groovy.parser.antlr4.GroovyLangParser.TypeParametersContext;
 import static 
org.apache.groovy.parser.antlr4.GroovyLangParser.UnaryAddExprAltContext;
 import static 
org.apache.groovy.parser.antlr4.GroovyLangParser.UnaryNotExprAltContext;
+import static org.apache.groovy.parser.antlr4.GroovyLangParser.VAR;
 import static 
org.apache.groovy.parser.antlr4.GroovyLangParser.VariableDeclarationContext;
 import static 
org.apache.groovy.parser.antlr4.GroovyLangParser.VariableDeclaratorContext;
 import static 
org.apache.groovy.parser.antlr4.GroovyLangParser.VariableDeclaratorIdContext;
@@ -352,6 +353,7 @@ import static 
org.codehaus.groovy.runtime.DefaultGroovyMethods.last;
  *         Created on 2016/08/14
  */
 public class AstBuilder extends GroovyParserBaseVisitor<Object> implements 
GroovyParserVisitor<Object> {
+
     public AstBuilder(SourceUnit sourceUnit) {
         this.sourceUnit = sourceUnit;
         this.moduleNode = new ModuleNode(sourceUnit);
@@ -1169,6 +1171,11 @@ public class AstBuilder extends 
GroovyParserBaseVisitor<Object> implements Groov
         final ClassNode outerClass = classNodeStack.peek();
         ClassNode classNode;
         String className = this.visitIdentifier(ctx.identifier());
+
+        if (VAR_STR.equals(className)) {
+            throw createParsingFailedException("var cannot be used for type 
declarations", ctx.identifier());
+        }
+
         if (asBoolean(ctx.ENUM())) {
             classNode =
                     EnumHelper.makeEnumNode(
@@ -4486,7 +4493,7 @@ public class AstBuilder extends 
GroovyParserBaseVisitor<Object> implements Groov
                 modifierManager.containsVisibilityModifier(),
                 modifierManager.containsNonVisibilityModifier(),
                 hasReturnType,
-                modifierManager.contains(DEF));
+                modifierManager.contains(DEF) || 
modifierManager.contains(VAR));
     }
 
     /**
@@ -4833,6 +4840,7 @@ public class AstBuilder extends 
GroovyParserBaseVisitor<Object> implements Groov
     private static final String SQ_STR = "'";
     private static final String DQ_STR = "\"";
     private static final String DOLLAR_SLASH_STR = "$/";
+    private static final String VAR_STR = "var";
 
     private static final Map<String, String> QUOTATION_MAP = Maps.of(
             DQ_STR, DQ_STR,

http://git-wip-us.apache.org/repos/asf/groovy/blob/f4d96d88/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/ModifierNode.java
----------------------------------------------------------------------
diff --git 
a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/ModifierNode.java
 
b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/ModifierNode.java
index f2b92a6..1164254 100644
--- 
a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/ModifierNode.java
+++ 
b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/ModifierNode.java
@@ -26,7 +26,20 @@ import org.objectweb.asm.Opcodes;
 import java.util.Map;
 import java.util.Objects;
 
-import static org.apache.groovy.parser.antlr4.GroovyParser.*;
+import static org.apache.groovy.parser.antlr4.GroovyParser.ABSTRACT;
+import static org.apache.groovy.parser.antlr4.GroovyParser.DEF;
+import static org.apache.groovy.parser.antlr4.GroovyParser.DEFAULT;
+import static org.apache.groovy.parser.antlr4.GroovyParser.FINAL;
+import static org.apache.groovy.parser.antlr4.GroovyParser.NATIVE;
+import static org.apache.groovy.parser.antlr4.GroovyParser.PRIVATE;
+import static org.apache.groovy.parser.antlr4.GroovyParser.PROTECTED;
+import static org.apache.groovy.parser.antlr4.GroovyParser.PUBLIC;
+import static org.apache.groovy.parser.antlr4.GroovyParser.STATIC;
+import static org.apache.groovy.parser.antlr4.GroovyParser.STRICTFP;
+import static org.apache.groovy.parser.antlr4.GroovyParser.SYNCHRONIZED;
+import static org.apache.groovy.parser.antlr4.GroovyParser.TRANSIENT;
+import static org.apache.groovy.parser.antlr4.GroovyParser.VAR;
+import static org.apache.groovy.parser.antlr4.GroovyParser.VOLATILE;
 import static org.codehaus.groovy.runtime.DefaultGroovyMethods.asBoolean;
 
 /**
@@ -45,6 +58,7 @@ public class ModifierNode extends ASTNode {
     public static final Map<Integer, Integer> MODIFIER_OPCODE_MAP = Maps.of(
             ANNOTATION_TYPE, 0,
             DEF, 0,
+            VAR, 0,
 
             NATIVE, Opcodes.ACC_NATIVE,
             SYNCHRONIZED, Opcodes.ACC_SYNCHRONIZED,
@@ -115,7 +129,7 @@ public class ModifierNode extends ASTNode {
     }
 
     public boolean isDef() {
-        return Objects.equals(DEF, this.type);
+        return Objects.equals(DEF, this.type) || Objects.equals(VAR, 
this.type);
     }
 
     public Integer getType() {

http://git-wip-us.apache.org/repos/asf/groovy/blob/f4d96d88/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
----------------------------------------------------------------------
diff --git 
a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
 
b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
index bbe35c7..0d8f370 100644
--- 
a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
+++ 
b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
@@ -382,6 +382,10 @@ class GroovyParserTest extends GroovyTestCase {
         doRunAndTestAntlr4('core/SafeChainOperator.groovy')
     }
 
+    void "test groovy core - var"() {
+        doRunAndTestAntlr4('core/Var_01x.groovy');
+    }
+
     void "test groovy core - BUG"() {
         doRunAndTestAntlr4('bugs/BUG-GROOVY-4757.groovy')
         doRunAndTestAntlr4('bugs/BUG-GROOVY-5652.groovy')

http://git-wip-us.apache.org/repos/asf/groovy/blob/f4d96d88/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 80a6d62..93f741d 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
@@ -212,6 +212,10 @@ class SyntaxErrorTest extends GroovyTestCase {
         TestUtils.doRunAndShouldFail('fail/MethodCall_01x.groovy');
     }
 
+    void "test groovy core - var"() {
+        TestUtils.doRunAndShouldFail('fail/Var_01x.groovy');
+    }
+
     /**************************************/
     static unzipScriptAndShouldFail(String entryName, List ignoreClazzList, 
Map<String, String> replacementsMap=[:], boolean toCheckNewParserOnly = false) {
         ignoreClazzList.addAll(TestUtils.COMMON_IGNORE_CLASS_LIST)

http://git-wip-us.apache.org/repos/asf/groovy/blob/f4d96d88/subprojects/parser-antlr4/src/test/resources/core/Var_01x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/resources/core/Var_01x.groovy 
b/subprojects/parser-antlr4/src/test/resources/core/Var_01x.groovy
new file mode 100644
index 0000000..6b5225a
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/core/Var_01x.groovy
@@ -0,0 +1,28 @@
+/*
+ *  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.
+ */
+
+[1, 2, 3].each { var ->
+    assert 0 < var && var < 4
+}
+
+var name = "Daniel"
+assert "Daniel" == name
+
+var var = "var variable name"
+assert "var variable name" == var

http://git-wip-us.apache.org/repos/asf/groovy/blob/f4d96d88/subprojects/parser-antlr4/src/test/resources/fail/Var_01x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/resources/fail/Var_01x.groovy 
b/subprojects/parser-antlr4/src/test/resources/fail/Var_01x.groovy
new file mode 100644
index 0000000..bf98233
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/fail/Var_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.
+ */
+
+class var {}

Reply via email to