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 2018/02/17 17:56:30 UTC

[1/3] groovy git commit: Refine constructor reference to support generics

Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_6_X dd431b34a -> 6ec8f8ed9


Refine constructor reference to support generics

(cherry picked from commit 2ac5b48)


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

Branch: refs/heads/GROOVY_2_6_X
Commit: ac14692eb5a7e7a1009389235c0de9d4714cbc82
Parents: dd431b3
Author: danielsun1106 <re...@hotmail.com>
Authored: Sat Feb 17 23:36:42 2018 +0800
Committer: danielsun1106 <re...@hotmail.com>
Committed: Sun Feb 18 01:46:45 2018 +0800

----------------------------------------------------------------------
 src/antlr/GroovyParser.g4                       | 40 ++++++++++----------
 .../apache/groovy/parser/antlr4/AstBuilder.java | 30 ++++++++++-----
 .../resources/core/MethodReference_01x.groovy   |  8 ++++
 3 files changed, 49 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/ac14692e/src/antlr/GroovyParser.g4
----------------------------------------------------------------------
diff --git a/src/antlr/GroovyParser.g4 b/src/antlr/GroovyParser.g4
index aa48bb5..108a79e 100644
--- a/src/antlr/GroovyParser.g4
+++ b/src/antlr/GroovyParser.g4
@@ -225,11 +225,11 @@ typeParameter
     ;
 
 typeBound
-    :   type (BITAND nls type)*
+    :   type[false] (BITAND nls type[false])*
     ;
 
 typeList
-    :   type (COMMA nls type)*
+    :   type[false] (COMMA nls type[false])*
     ;
 
 
@@ -256,7 +256,7 @@ locals[ int t ]
                         // Only interface can extend more than one super class
                         {1 == $t}? scs=typeList
                     |
-                        sc=type
+                        sc=type[false]
                     )
                 nls)?
             |
@@ -380,14 +380,13 @@ options { baseContext = type; }
         dimsOpt
     ;
 
-type
+type[boolean allowVoid]
     :   annotationsOpt
         (
             (
                 primitiveType
             |
-                 // !!! ERROR ALTERNATIVE !!!
-                 VOID { require(false, "void is not allowed here", -4); }
+                 VOID
             )
         |
                 generalClassOrInterfaceType
@@ -420,8 +419,8 @@ typeArguments
     ;
 
 typeArgument
-    :   type
-    |   annotationsOpt QUESTION ((EXTENDS | SUPER) nls type)?
+    :   type[false]
+    |   annotationsOpt QUESTION ((EXTENDS | SUPER) nls type[false])?
     ;
 
 annotatedQualifiedClassName
@@ -441,11 +440,11 @@ formalParameterList
     ;
 
 thisFormalParameter
-    :   type THIS
+    :   type[false] THIS
     ;
 
 formalParameter
-    :   variableModifiersOpt type? ELLIPSIS? variableDeclaratorId (nls ASSIGN nls expression)?
+    :   variableModifiersOpt type[false]? ELLIPSIS? variableDeclaratorId (nls ASSIGN nls expression)?
     ;
 
 methodBody
@@ -617,12 +616,12 @@ classifiedModifiers[int t]
 variableDeclaration[int t]
 @leftfactor { classifiedModifiers }
     :   classifiedModifiers[$t]
-        (   type? variableDeclarators
+        (   type[false]? variableDeclarators
         |   typeNamePairs nls ASSIGN nls variableInitializer
         )
     |
         classifiedModifiers[$t]?
-        type variableDeclarators
+        type[false] variableDeclarators
     ;
 
 typeNamePairs
@@ -630,7 +629,7 @@ typeNamePairs
     ;
 
 typeNamePair
-    :   type? variableDeclaratorId
+    :   type[false]? variableDeclaratorId
     ;
 
 variableNames
@@ -753,7 +752,7 @@ forControl
     ;
 
 enhancedForControl
-    :   variableModifiersOpt type? variableDeclaratorId (COLON | IN) expression
+    :   variableModifiersOpt type[false]? variableDeclaratorId (COLON | IN) expression
     ;
 
 classicalForControl
@@ -773,7 +772,7 @@ forUpdate
 // EXPRESSIONS
 
 castParExpression
-    :   LPAREN type rparen
+    :   LPAREN type[false] rparen
     ;
 
 parExpression
@@ -847,7 +846,7 @@ expression
         right=expression                                                                    #shiftExprAlt
 
     // boolean relational expressions (level 7)
-    |   left=expression nls op=(AS | INSTANCEOF | NOT_INSTANCEOF) nls type                  #relationalExprAlt
+    |   left=expression nls op=(AS | INSTANCEOF | NOT_INSTANCEOF) nls type[false]           #relationalExprAlt
     |   left=expression nls op=(LE | GE | GT | LT | IN | NOT_IN)  nls right=expression      #relationalExprAlt
 
     // equality/inequality (==/!=) (level 8)
@@ -1044,7 +1043,10 @@ namedPropertyArgs
     ;
 
 primary
-    :   identifier                                                                          #identifierPrmrAlt
+    :
+        // Append `typeArguments?` to `identifier` to support constructor reference with generics, e.g. HashMap<String, Integer>::new
+        // Though this is not a graceful solution, it is much faster than replacing `builtInType` with `type`
+        identifier typeArguments?                                                           #identifierPrmrAlt
     |   literal                                                                             #literalPrmrAlt
     |   gstring                                                                             #gstringPrmrAlt
     |   NEW nls creator                                                                     #newPrmrAlt
@@ -1055,7 +1057,7 @@ primary
     |   lambdaExpression                                                                    #lambdaPrmrAlt
     |   list                                                                                #listPrmrAlt
     |   map                                                                                 #mapPrmrAlt
-    |   builtInType                                                                         #typePrmrAlt
+    |   builtInType                                                                         #builtInTypePrmrAlt
     ;
 
 list
@@ -1243,5 +1245,3 @@ nls
 sep :   SEMI NL*
     |   NL+ (SEMI NL*)*
     ;
-
-

http://git-wip-us.apache.org/repos/asf/groovy/blob/ac14692e/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 0814a60..28d31b3 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
@@ -156,6 +156,7 @@ import static org.apache.groovy.parser.antlr4.GroovyLangParser.BooleanLiteralAlt
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.BreakStatementContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.BreakStmtAltContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.BuiltInTypeContext;
+import static org.apache.groovy.parser.antlr4.GroovyLangParser.BuiltInTypePrmrAltContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.CASE;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.CastExprAltContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.CastParExpressionContext;
@@ -328,7 +329,6 @@ import static org.apache.groovy.parser.antlr4.GroovyLangParser.TypeNamePairConte
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.TypeNamePairsContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.TypeParameterContext;
 import static org.apache.groovy.parser.antlr4.GroovyLangParser.TypeParametersContext;
-import static org.apache.groovy.parser.antlr4.GroovyLangParser.TypePrmrAltContext;
 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.VariableDeclarationContext;
@@ -2366,7 +2366,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
                 return configureAST(methodCallExpression, ctx);
             }
 
-            if (baseExpr instanceof VariableExpression) { // void and primitive type AST node must be an instance of VariableExpression
+            if (baseExpr instanceof ClassExpression) { // void and primitive type AST node must be an instance of ClassExpression
                 String baseExprText = baseExpr.getText();
                 if (VOID_STR.equals(baseExprText)) { // e.g. void()
                     return configureAST(this.createCallMethodCallExpression(this.createConstantExpression(baseExpr), argumentsExpr), ctx);
@@ -3084,7 +3084,16 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
 
     // primary {       --------------------------------------------------------------------
     @Override
-    public VariableExpression visitIdentifierPrmrAlt(IdentifierPrmrAltContext ctx) {
+    public Expression visitIdentifierPrmrAlt(IdentifierPrmrAltContext ctx) {
+        if (asBoolean(ctx.typeArguments())) {
+            ClassNode classNode = ClassHelper.make(ctx.identifier().getText());
+
+            classNode.setGenericsTypes(
+                    this.visitTypeArguments(ctx.typeArguments()));
+
+            return configureAST(new ClassExpression(classNode), ctx);
+        }
+
         return configureAST(new VariableExpression(this.visitIdentifier(ctx.identifier())), ctx);
     }
 
@@ -3142,10 +3151,8 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
     }
 
     @Override
-    public VariableExpression visitTypePrmrAlt(TypePrmrAltContext ctx) {
-        return configureAST(
-                this.visitBuiltInType(ctx.builtInType()),
-                ctx);
+    public ClassExpression visitBuiltInTypePrmrAlt(BuiltInTypePrmrAltContext ctx) {
+        return configureAST(this.visitBuiltInType(ctx.builtInType()), ctx);
     }
 
 
@@ -3399,7 +3406,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
     */
 
     @Override
-    public VariableExpression visitBuiltInType(BuiltInTypeContext ctx) {
+    public ClassExpression visitBuiltInType(BuiltInTypeContext ctx) {
         String text;
         if (asBoolean(ctx.VOID())) {
             text = ctx.VOID().getText();
@@ -3409,7 +3416,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
             throw createParsingFailedException("Unsupported built-in type: " + ctx, ctx);
         }
 
-        return configureAST(new VariableExpression(text), ctx);
+        return configureAST(new ClassExpression(ClassHelper.make(text)), ctx);
     }
 
 
@@ -3929,9 +3936,14 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
             classNode = this.visitClassOrInterfaceType(ctx.classOrInterfaceType());
         } else if (asBoolean(ctx.primitiveType())) {
             classNode = this.visitPrimitiveType(ctx.primitiveType());
+        } else if (asBoolean(ctx.VOID())) {
+            if (ctx.allowVoid) {
+                classNode = configureAST(ClassHelper.make(ctx.getText()), ctx.VOID());
+            }
         }
 
         if (!asBoolean(classNode)) {
+            // TODO refine error message for `void`
             throw createParsingFailedException("Unsupported type: " + ctx.getText(), ctx);
         }
 

http://git-wip-us.apache.org/repos/asf/groovy/blob/ac14692e/subprojects/parser-antlr4/src/test/resources/core/MethodReference_01x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/resources/core/MethodReference_01x.groovy b/subprojects/parser-antlr4/src/test/resources/core/MethodReference_01x.groovy
index a04f305..c25c0f3 100644
--- a/subprojects/parser-antlr4/src/test/resources/core/MethodReference_01x.groovy
+++ b/subprojects/parser-antlr4/src/test/resources/core/MethodReference_01x.groovy
@@ -21,6 +21,14 @@ def mr = String::toUpperCase
 assert 'ABC' == mr('abc')
 assert 'ABC' == String::toUpperCase('abc')
 
+def m = ['apple', 'banana', 'orange'].stream().collect(Collectors.toMap(e -> e.charAt(0), e -> e, (e1, e2) -> e1, LinkedHashMap<String, String>::new))
+assert m instanceof LinkedHashMap
+assert ['a', 'b', 'o'] as TreeSet == m.keySet() as TreeSet
+assert ['apple', 'banana', 'orange'] as TreeSet == m.values() as TreeSet
+
+assert new HashMap<String, Integer>() == HashMap<String, Integer>::new()
+assert new HashSet<Integer>() == HashSet<Integer>::new()
+
 assert new HashSet() == HashSet::new()
 assert new String() == String::new()
 assert 1 == Integer::new(1)


[2/3] groovy git commit: Revert error message for invalid `void`

Posted by su...@apache.org.
Revert error message for invalid `void`

(cherry picked from commit a1a3746)


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

Branch: refs/heads/GROOVY_2_6_X
Commit: e4133b5432ef45e853894c682acab07afaecc640
Parents: ac14692
Author: danielsun1106 <re...@hotmail.com>
Authored: Sun Feb 18 00:43:41 2018 +0800
Committer: danielsun1106 <re...@hotmail.com>
Committed: Sun Feb 18 01:47:04 2018 +0800

----------------------------------------------------------------------
 .../main/java/org/apache/groovy/parser/antlr4/AstBuilder.java   | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/e4133b54/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 28d31b3..8be9554 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
@@ -3943,7 +3943,10 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
         }
 
         if (!asBoolean(classNode)) {
-            // TODO refine error message for `void`
+            if (VOID_STR.equals(ctx.getText())) { // TODO refine error message for `void`
+                throw createParsingFailedException("void is not allowed here", ctx);
+            }
+
             throw createParsingFailedException("Unsupported type: " + ctx.getText(), ctx);
         }
 


[3/3] groovy git commit: Adjust error message of test

Posted by su...@apache.org.
Adjust error message of test

(cherry picked from commit cfc6c4d)


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

Branch: refs/heads/GROOVY_2_6_X
Commit: 6ec8f8ed9307d4301b4671a652e2805bccbed624
Parents: e4133b5
Author: danielsun1106 <re...@hotmail.com>
Authored: Sun Feb 18 01:26:53 2018 +0800
Committer: danielsun1106 <re...@hotmail.com>
Committed: Sun Feb 18 01:47:13 2018 +0800

----------------------------------------------------------------------
 src/test/gls/generics/GenericsTest.groovy | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/6ec8f8ed/src/test/gls/generics/GenericsTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/gls/generics/GenericsTest.groovy b/src/test/gls/generics/GenericsTest.groovy
index 7ae33e4..a7865cd 100644
--- a/src/test/gls/generics/GenericsTest.groovy
+++ b/src/test/gls/generics/GenericsTest.groovy
@@ -402,7 +402,7 @@ import java.util.concurrent.atomic.AtomicInteger
 
             shouldFailCompilationWithMessage """
                 List<Integer list2 = new ArrayList<Integer>()
-            """, "Unexpected input: 'list2'"
+            """, "Unexpected input: 'List<Integer list2'"
 
             shouldFailCompilationWithMessage """
                 def c = []