Repository: groovy Updated Branches: refs/heads/GROOVY_2_6_X 926988e8f -> e5d950c10
Use ParserVersion to represent the version of a parser (cherry picked from commit 5a4974e) Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/71583cd5 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/71583cd5 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/71583cd5 Branch: refs/heads/GROOVY_2_6_X Commit: 71583cd5d60284f55e52f6e2f49ce3b84e19d67b Parents: 926988e Author: sunlan <sun...@apache.org> Authored: Sun Aug 6 19:02:37 2017 +0800 Committer: sunlan <sun...@apache.org> Committed: Sun Aug 6 19:12:32 2017 +0800 ---------------------------------------------------------------------- .../groovy/control/CompilerConfiguration.java | 10 ++-- .../codehaus/groovy/control/ParserVersion.java | 50 ++++++++++++++++++++ src/test/gls/generics/GenericsTest.groovy | 7 +-- src/test/groovy/bugs/Groovy5318Bug.groovy | 3 +- .../transform/stc/DelegatesToSTCTest.groovy | 5 +- .../groovy/transform/stc/GenericsSTCTest.groovy | 4 +- .../transform/stc/MethodCallsSTCTest.groovy | 4 +- 7 files changed, 69 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/71583cd5/src/main/org/codehaus/groovy/control/CompilerConfiguration.java ---------------------------------------------------------------------- diff --git a/src/main/org/codehaus/groovy/control/CompilerConfiguration.java b/src/main/org/codehaus/groovy/control/CompilerConfiguration.java index 8a0d26f..40cda8e 100644 --- a/src/main/org/codehaus/groovy/control/CompilerConfiguration.java +++ b/src/main/org/codehaus/groovy/control/CompilerConfiguration.java @@ -27,10 +27,8 @@ import org.objectweb.asm.Opcodes; import java.io.File; import java.io.PrintWriter; import java.util.Arrays; -import java.util.Collections; import java.util.HashMap; import java.util.HashSet; -import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; @@ -906,7 +904,11 @@ public class CompilerConfiguration { this.bytecodePostprocessor = bytecodePostprocessor; } - public boolean isAntlr2Parser() { - return antlr2Parser; + public ParserVersion getParserVersion() { + if (this.antlr2Parser) { + return ParserVersion.V_2; + } + + return ParserVersion.V_4; } } http://git-wip-us.apache.org/repos/asf/groovy/blob/71583cd5/src/main/org/codehaus/groovy/control/ParserVersion.java ---------------------------------------------------------------------- diff --git a/src/main/org/codehaus/groovy/control/ParserVersion.java b/src/main/org/codehaus/groovy/control/ParserVersion.java new file mode 100644 index 0000000..8440611 --- /dev/null +++ b/src/main/org/codehaus/groovy/control/ParserVersion.java @@ -0,0 +1,50 @@ +/* + * 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. + */ +package org.codehaus.groovy.control; + +/** + * Represents the version of a parser + * + * @since 2.6.0 + */ +public enum ParserVersion { + /** + * Before Groovy 2.6.0(including 2.6.0), the default version of parser is v2 + */ + V_2, + + /** + * After Groovy 3.0.0(including 3.0.0), the default version of parser is v4(i.e. the new parser Parrot) + */ + V_4("Parrot"); + + private String name; + + ParserVersion() { + this(null); + } + + ParserVersion(String name) { + this.name = name; + } + + public String getName() { + return name; + } +} http://git-wip-us.apache.org/repos/asf/groovy/blob/71583cd5/src/test/gls/generics/GenericsTest.groovy ---------------------------------------------------------------------- diff --git a/src/test/gls/generics/GenericsTest.groovy b/src/test/gls/generics/GenericsTest.groovy index e7a98fa..3fe0e69 100644 --- a/src/test/gls/generics/GenericsTest.groovy +++ b/src/test/gls/generics/GenericsTest.groovy @@ -20,6 +20,7 @@ package gls.generics import org.codehaus.groovy.control.CompilerConfiguration import org.codehaus.groovy.control.MultipleCompilationErrorsException +import org.codehaus.groovy.control.ParserVersion class GenericsTest extends GenericsTestBase { @@ -357,7 +358,7 @@ import java.util.concurrent.atomic.AtomicInteger } void testCompilationWithMissingClosingBracketsInGenerics() { - if (CompilerConfiguration.DEFAULT.antlr2Parser) { + if (ParserVersion.V_2 == CompilerConfiguration.DEFAULT.parserVersion) { shouldFailCompilationWithExpectedMessage """ def list1 = new ArrayList<Integer() """ @@ -517,7 +518,7 @@ import java.util.concurrent.atomic.AtomicInteger class MyList extends ArrayList<String, String> {} ''', ['(supplied with 2 type parameters)', 'which takes 1 parameter'] - if (CompilerConfiguration.DEFAULT.antlr2Parser) { + if (ParserVersion.V_2 == CompilerConfiguration.DEFAULT.parserVersion) { shouldFailCompilationWithMessages ''' class MyList extends ArrayList<> {} ''', ['(supplied with 0 type parameters)', 'which takes 1 parameter', 'invalid Diamond <> usage?'] @@ -534,7 +535,7 @@ import java.util.concurrent.atomic.AtomicInteger class MyList implements List<String, String> {} ''', ['(supplied with 2 type parameters)', 'which takes 1 parameter'] - if (CompilerConfiguration.DEFAULT.antlr2Parser) { + if (ParserVersion.V_2 == CompilerConfiguration.DEFAULT.parserVersion) { shouldFailCompilationWithMessages ''' class MyList implements Map<> {} ''', ['(supplied with 0 type parameters)', 'which takes 2 parameters', 'invalid Diamond <> usage?'] http://git-wip-us.apache.org/repos/asf/groovy/blob/71583cd5/src/test/groovy/bugs/Groovy5318Bug.groovy ---------------------------------------------------------------------- diff --git a/src/test/groovy/bugs/Groovy5318Bug.groovy b/src/test/groovy/bugs/Groovy5318Bug.groovy index d981ca2..bf2610a 100644 --- a/src/test/groovy/bugs/Groovy5318Bug.groovy +++ b/src/test/groovy/bugs/Groovy5318Bug.groovy @@ -20,6 +20,7 @@ package groovy.bugs import gls.CompilableTestSupport import org.codehaus.groovy.control.CompilerConfiguration +import org.codehaus.groovy.control.ParserVersion class Groovy5318Bug extends CompilableTestSupport { void testTypeArgumentsOnlyOnTheLastComponent() { @@ -27,7 +28,7 @@ class Groovy5318Bug extends CompilableTestSupport { def a = new java.util<Integer>.ArrayList<ArrayList<Integer>>() """ - if (CompilerConfiguration.DEFAULT.antlr2Parser) { + if (ParserVersion.V_2 == CompilerConfiguration.DEFAULT.parserVersion) { assert message.contains('Unexpected type arguments found prior to: ArrayList') } else { assert message.contains('Unexpected input: \'new java.util<Integer>.\'') http://git-wip-us.apache.org/repos/asf/groovy/blob/71583cd5/src/test/groovy/transform/stc/DelegatesToSTCTest.groovy ---------------------------------------------------------------------- diff --git a/src/test/groovy/transform/stc/DelegatesToSTCTest.groovy b/src/test/groovy/transform/stc/DelegatesToSTCTest.groovy index 9f8dd3f..6c4beff 100644 --- a/src/test/groovy/transform/stc/DelegatesToSTCTest.groovy +++ b/src/test/groovy/transform/stc/DelegatesToSTCTest.groovy @@ -18,6 +18,8 @@ */ package groovy.transform.stc +import org.codehaus.groovy.control.ParserVersion + /** * Units tests aimed at testing the behavior of {@link DelegatesTo} in combination * with static type checking. @@ -609,7 +611,6 @@ class DelegatesToSTCTest extends StaticTypeCheckingTestCase { // GROOVY-6165 void testDelegatesToGenericArgumentTypeAndTypo() { - boolean isAntlr2Parser = config.isAntlr2Parser() String code = '''import groovy.transform.* @@ -637,7 +638,7 @@ class DelegatesToSTCTest extends StaticTypeCheckingTestCase { String msg = 'Cannot find matching method' - if (isAntlr2Parser) { + if (ParserVersion.V_2 == config.parserVersion) { shouldFailWithMessages code, msg } else { /* http://git-wip-us.apache.org/repos/asf/groovy/blob/71583cd5/src/test/groovy/transform/stc/GenericsSTCTest.groovy ---------------------------------------------------------------------- diff --git a/src/test/groovy/transform/stc/GenericsSTCTest.groovy b/src/test/groovy/transform/stc/GenericsSTCTest.groovy index c65965c..b1d9fd7 100644 --- a/src/test/groovy/transform/stc/GenericsSTCTest.groovy +++ b/src/test/groovy/transform/stc/GenericsSTCTest.groovy @@ -19,6 +19,7 @@ package groovy.transform.stc import groovy.transform.NotYetImplemented +import org.codehaus.groovy.control.ParserVersion /** * Unit tests for static type checking : generics. @@ -434,7 +435,6 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase { } void testShouldComplainAboutToInteger() { - boolean isAntlr2Parser = config.isAntlr2Parser() String code = ''' class Test { @@ -459,7 +459,7 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase { new Test() ''' - if (isAntlr2Parser) { + if (ParserVersion.V_2 == config.parserVersion) { shouldFailWithMessages code, 'Cannot find matching method java.lang.Object#getAt(int)' } else { shouldFailWithMessages code, http://git-wip-us.apache.org/repos/asf/groovy/blob/71583cd5/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy ---------------------------------------------------------------------- diff --git a/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy b/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy index cba7bdd..7fd1e00 100644 --- a/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy +++ b/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy @@ -18,6 +18,7 @@ */ package groovy.transform.stc +import org.codehaus.groovy.control.ParserVersion import org.codehaus.groovy.control.customizers.ImportCustomizer; import static org.codehaus.groovy.control.CompilerConfiguration.DEFAULT as config @@ -794,7 +795,6 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase { } void testSpreadArgsForbiddenInClosureCall() { - boolean isAntlr2Parser = config.isAntlr2Parser() String code = ''' def closure = { String a, String b, String c -> println "$a $b $c" } @@ -802,7 +802,7 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase { closure(*strings) ''' - if (isAntlr2Parser) { + if (ParserVersion.V_2 == config.parserVersion) { shouldFailWithMessages code, 'The spread operator cannot be used as argument of method or closure calls with static type checking because the number of arguments cannot be determined at compile time' } else { shouldFailWithMessages code,