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 2020/06/30 04:43:56 UTC
[groovy] 02/02: Tweak `methodDeclaration` rule
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 87da0a5598156965e53bbad80aa28a5534d56c3f
Author: Daniel Sun <su...@apache.org>
AuthorDate: Tue Jun 30 11:30:52 2020 +0800
Tweak `methodDeclaration` rule
(cherry picked from commit 076bce0eb516c2fe7524b223bd4697232536dcca)
---
src/antlr/GroovyParser.g4 | 12 ++++++------
.../org/apache/groovy/parser/antlr4/AstBuilder.java | 10 +++++++++-
.../apache/groovy/parser/antlr4/SyntaxErrorTest.groovy | 17 +++++++++++++++++
.../test/resources/core/AnnotationDeclaration_01.groovy | 4 ++++
4 files changed, 36 insertions(+), 7 deletions(-)
diff --git a/src/antlr/GroovyParser.g4 b/src/antlr/GroovyParser.g4
index 52d7e4a..9feca64 100644
--- a/src/antlr/GroovyParser.g4
+++ b/src/antlr/GroovyParser.g4
@@ -267,14 +267,14 @@ memberDeclaration[int t]
* ct 9: script, other see the comment of classDeclaration
*/
methodDeclaration[int t, int ct]
- : modifiersOpt
- ( { 3 == $ct }?
- returnType[$ct] methodName LPAREN rparen (DEFAULT nls elementValue)?
+ : modifiersOpt typeParameters? returnType[$ct]?
+ methodName formalParameters
+ (
+ DEFAULT nls elementValue
|
- typeParameters? returnType[$ct]?
- methodName formalParameters (nls THROWS nls qualifiedClassNameList)?
+ (nls THROWS nls qualifiedClassNameList)?
(nls methodBody)?
- )
+ )?
;
methodName
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 e3f2f29..6b69578 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
@@ -1582,7 +1582,9 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
}
boolean isAbstractMethod = methodNode.isAbstract();
- boolean hasMethodBody = asBoolean(methodNode.getCode());
+ boolean hasMethodBody =
+ asBoolean(methodNode.getCode())
+ && !(methodNode.getCode() instanceof ExpressionStatement);
if (9 == ctx.ct) { // script
if (isAbstractMethod || !hasMethodBody) { // method should not be declared abstract in the script
@@ -1595,6 +1597,12 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
}
}
+ if (3 == ctx.ct) { // annotation
+ if (hasMethodBody) {
+ throw createParsingFailedException("Annotation type element should not have body", ctx);
+ }
+ }
+
if (!isAbstractMethod && !hasMethodBody) { // non-abstract method without body in the non-script(e.g. class, enum, trait) is not allowed!
throw createParsingFailedException("You defined a method[" + methodNode.getName() + "] without a body. Try adding a method body, or declare it abstract", methodNode);
}
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 54d87f2..4bbe12e 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
@@ -272,6 +272,23 @@ final class SyntaxErrorTest extends GroovyTestCase {
|'''.stripMargin()
}
+ void 'test groovy core - AnnotationDeclaration 5'() {
+ def err = expectParseError '''\
+ |@interface A {
+ | String a() {
+ | }
+ |}'''.stripMargin()
+ println err
+ assert err == '''\
+ |startup failed:
+ |test.groovy: 2: Annotation type element should not have body @ line 2, column 5.
+ | String a() {
+ | ^
+ |
+ |1 error
+ |'''.stripMargin()
+ }
+
void 'test groovy core - MethodDeclaration'() {
TestUtils.shouldFail('fail/MethodDeclaration_01.groovy')
TestUtils.doRunAndShouldFail('fail/MethodDeclaration_02x.groovy')
diff --git a/subprojects/parser-antlr4/src/test/resources/core/AnnotationDeclaration_01.groovy b/subprojects/parser-antlr4/src/test/resources/core/AnnotationDeclaration_01.groovy
index 88f3a3d..8a08591 100644
--- a/subprojects/parser-antlr4/src/test/resources/core/AnnotationDeclaration_01.groovy
+++ b/subprojects/parser-antlr4/src/test/resources/core/AnnotationDeclaration_01.groovy
@@ -54,4 +54,8 @@ import java.lang.annotation.Target
@interface b {
String name()
+}
+
+@interface C {
+ String name() default ''
}
\ No newline at end of file