Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 31 additions & 19 deletions src/antlr/GroovyParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,10 @@ packageDeclaration
;

importDeclaration
: annotationsOpt IMPORT STATIC? qualifiedName (DOT MUL | AS alias=identifier)?
| annotationsOpt IMPORT MODULE qualifiedName
: annotationsOpt IMPORT
( MODULE qualifiedName
| STATIC? qualifiedName (DOT MUL | AS alias=identifier)?
)
;


Expand Down Expand Up @@ -557,8 +559,7 @@ block
;

blockStatement
: localVariableDeclaration
| statement
: statement
;

localVariableDeclaration
Expand All @@ -579,8 +580,11 @@ variableDeclaration[int t]
;

typeNamePairs
: LPAREN typeNamePair (COMMA typeNamePair)* RPAREN
| LPAREN keyedPair (COMMA keyedPair)* RPAREN
: LPAREN
( typeNamePair (COMMA typeNamePair)*
| keyedPair (COMMA keyedPair)*
)
RPAREN
;

typeNamePair
Expand Down Expand Up @@ -978,21 +982,29 @@ pathExpression returns [int t]
pathElement returns [int t]
: nls
(
DOT nls NEW creator[1]
{ $t = 6; }
|
// AT: foo.@bar selects the field (or attribute), not property
(
( DOT // The all-powerful dot.
| SPREAD_DOT // Spread operator: x*.y === x?.collect{it.y}
| SAFE_DOT // Optional-null operator: x?.y === (x==null)?null:x.y
| SAFE_CHAIN_DOT // Optional-null chain operator: x??.y.z === x?.y?.z
) nls (AT | nonWildcardTypeArguments)?
|
METHOD_POINTER nls // Method pointer operator: foo.&y == foo.metaClass.getMethodPointer(foo, "y")
DOT nls
( NEW creator[1]
{ $t = 6; }
|
METHOD_REFERENCE nls (nonWildcardTypeArguments)? // Method reference: System.out::println
// AT: foo.@bar selects the field (or attribute), not property
(AT | nonWildcardTypeArguments)?
namePart
{ $t = 1; }
)
|
// Non-DOT member selection operators (still share namePart tail)
( SPREAD_DOT // Spread operator: x*.y === x?.collect{it.y}
| SAFE_DOT // Optional-null operator: x?.y === (x==null)?null:x.y
| SAFE_CHAIN_DOT // Optional-null chain operator: x??.y.z === x?.y?.z
) nls (AT | nonWildcardTypeArguments)?
namePart
{ $t = 1; }
|
METHOD_POINTER nls // Method pointer operator: foo.&y == foo.metaClass.getMethodPointer(foo, "y")
namePart
{ $t = 1; }
|
METHOD_REFERENCE nls (nonWildcardTypeArguments)? // Method reference: System.out::println
namePart
{ $t = 1; }

Expand Down
32 changes: 12 additions & 20 deletions src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3440,7 +3440,7 @@
// } primary ---------------------------------------------------------------

@Override
public Expression visitCreator(final CreatorContext ctx) {

Check warning on line 3443 in src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

A "Brain Method" was detected. Refactor it to reduce at least one of the following metrics: LOC from 70 to 64, Complexity from 17 to 14, Nesting Level from 3 to 2, Number of Variables from 24 to 6.

See more on https://sonarcloud.io/project/issues?id=apache_groovy&issues=AZ9045NDGPmZJSHOvOgU&open=AZ9045NDGPmZJSHOvOgU&pullRequest=2720
ClassNode classNode = this.visitCreatedName(ctx.createdName());

if (asBoolean(ctx.arguments())) { // create instance of class
Expand Down Expand Up @@ -4351,29 +4351,21 @@

@Override
public Statement visitBlockStatement(final BlockStatementContext ctx) {
if (asBoolean(ctx.localVariableDeclaration())) {
return configureAST(this.visitLocalVariableDeclaration(ctx.localVariableDeclaration()), ctx);
}

if (asBoolean(ctx.statement())) {
Object astNode = this.visit(ctx.statement()); //this.configureAST((Statement) this.visit(ctx.statement()), ctx);

if (null == astNode) {
return null;
}
Object astNode = this.visit(ctx.statement());

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 {
throw createParsingFailedException("The statement(" + astNode.getClass() + ") not expected here", ctx);
}
if (null == astNode) {
return null;
}

throw createParsingFailedException("Unsupported block statement: " + ctx.getText(), ctx);
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 {
throw createParsingFailedException("The statement(" + astNode.getClass() + ") not expected here", ctx);
}
}

@Override
Expand Down
Loading
Loading