groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject groovy git commit: GROOVY-8598: Possible bug in AstBuilder Antlr4
Date Wed, 23 May 2018 07:38:22 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_6_X 970162478 -> 0c0e7a635


GROOVY-8598: Possible bug in AstBuilder Antlr4

(cherry picked from commit a26c190)


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

Branch: refs/heads/GROOVY_2_6_X
Commit: 0c0e7a635f51594a7724e6eb1c5dd1df8ad85682
Parents: 9701624
Author: sunlan <sunlan@apache.org>
Authored: Wed May 23 15:37:16 2018 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Wed May 23 15:38:10 2018 +0800

----------------------------------------------------------------------
 .../apache/groovy/parser/antlr4/AstBuilder.java | 12 +++++++++--
 .../groovy/parser/antlr4/SyntaxErrorTest.groovy |  5 +++++
 .../src/test/resources/fail/Import_01x.groovy   | 22 ++++++++++++++++++++
 .../src/test/resources/fail/Import_02x.groovy   | 22 ++++++++++++++++++++
 4 files changed, 59 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/0c0e7a63/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 17eb878..2942439 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
@@ -4142,10 +4142,18 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object>
implements Groov
         if (asBoolean(ctx.statement())) {
             Object astNode = this.visit(ctx.statement()); //configureAST((Statement) this.visit(ctx.statement()),
ctx);
 
-            if (astNode instanceof MethodNode) {
+            if (null == astNode) {
+                return null;
+            }
+
+            if (astNode instanceof Statement) {
+                return (Statement) astNode;
+            } else if (astNode instanceof MethodNode) {
                 throw createParsingFailedException("Method definition not expected here",
ctx);
+            } else if (astNode instanceof ImportNode) {
+                throw createParsingFailedException("Import statement not expected here",
ctx);
             } else {
-                return (Statement) astNode;
+                throw createParsingFailedException("The statement(" + astNode.getClass()
+ ") not expected here", ctx);
             }
         }
 

http://git-wip-us.apache.org/repos/asf/groovy/blob/0c0e7a63/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 0fdef50..68c551a 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
@@ -226,6 +226,11 @@ class SyntaxErrorTest extends GroovyTestCase {
         TestUtils.doRunAndShouldFail('fail/NonStaticClass_01x.groovy');
     }
 
+    void "test groovy core - Import"() {
+        TestUtils.shouldFail('fail/Import_01x.groovy');
+        TestUtils.shouldFail('fail/Import_02x.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/0c0e7a63/subprojects/parser-antlr4/src/test/resources/fail/Import_01x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/resources/fail/Import_01x.groovy b/subprojects/parser-antlr4/src/test/resources/fail/Import_01x.groovy
new file mode 100644
index 0000000..55da244
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/fail/Import_01x.groovy
@@ -0,0 +1,22 @@
+/*
+ *  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.
+ */
+
+{
+    import java.util.*
+}

http://git-wip-us.apache.org/repos/asf/groovy/blob/0c0e7a63/subprojects/parser-antlr4/src/test/resources/fail/Import_02x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/resources/fail/Import_02x.groovy b/subprojects/parser-antlr4/src/test/resources/fail/Import_02x.groovy
new file mode 100644
index 0000000..f94f917
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/fail/Import_02x.groovy
@@ -0,0 +1,22 @@
+/*
+ *  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 m() {
+    import java.util.*
+}


Mime
View raw message