Repository: groovy Updated Branches: refs/heads/master 1546b1918 -> a26c190f9
GROOVY-8598: Possible bug in AstBuilder Antlr4 Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/a26c190f Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/a26c190f Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/a26c190f Branch: refs/heads/master Commit: a26c190f9a70b0c430004d80c56e37703c2edfe8 Parents: 1546b19 Author: sunlan <[email protected]> Authored: Wed May 23 15:37:16 2018 +0800 Committer: sunlan <[email protected]> Committed: Wed May 23 15:37:16 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/a26c190f/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 b530c20..37f951d 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 @@ -4068,10 +4068,18 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov if (asBoolean(ctx.statement())) { Object astNode = this.visit(ctx.statement()); //this.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/a26c190f/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/a26c190f/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/a26c190f/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.* +}
