35
35
import org .antlr .v4 .runtime .misc .ParseCancellationException ;
36
36
import org .antlr .v4 .runtime .tree .ParseTree ;
37
37
import org .antlr .v4 .runtime .tree .TerminalNode ;
38
- import org .apache .groovy .parser .antlr4 .GroovyParser .*;
39
38
import org .apache .groovy .parser .antlr4 .internal .DescriptiveErrorStrategy ;
40
39
import org .apache .groovy .parser .antlr4 .internal .atnmanager .AtnManager ;
41
40
import org .apache .groovy .parser .antlr4 .util .StringUtils ;
146
145
import java .util .stream .Stream ;
147
146
148
147
import static groovy .lang .Tuple .tuple ;
149
- import static org .apache .groovy .parser .antlr4 .GroovyParser .ADD ;
150
- import static org .apache .groovy .parser .antlr4 .GroovyParser .AS ;
151
- import static org .apache .groovy .parser .antlr4 .GroovyParser .CASE ;
152
- import static org .apache .groovy .parser .antlr4 .GroovyParser .DEC ;
153
- import static org .apache .groovy .parser .antlr4 .GroovyParser .DEF ;
154
- import static org .apache .groovy .parser .antlr4 .GroovyParser .DEFAULT ;
155
- import static org .apache .groovy .parser .antlr4 .GroovyParser .GE ;
156
- import static org .apache .groovy .parser .antlr4 .GroovyParser .GT ;
157
- import static org .apache .groovy .parser .antlr4 .GroovyParser .IN ;
158
- import static org .apache .groovy .parser .antlr4 .GroovyParser .INC ;
159
- import static org .apache .groovy .parser .antlr4 .GroovyParser .INSTANCEOF ;
160
- import static org .apache .groovy .parser .antlr4 .GroovyParser .LE ;
161
- import static org .apache .groovy .parser .antlr4 .GroovyParser .LT ;
162
- import static org .apache .groovy .parser .antlr4 .GroovyParser .NOT_IN ;
163
- import static org .apache .groovy .parser .antlr4 .GroovyParser .NOT_INSTANCEOF ;
164
- import static org .apache .groovy .parser .antlr4 .GroovyParser .PRIVATE ;
165
- import static org .apache .groovy .parser .antlr4 .GroovyParser .STATIC ;
166
- import static org .apache .groovy .parser .antlr4 .GroovyParser .SUB ;
167
- import static org .apache .groovy .parser .antlr4 .GroovyParser .VAR ;
148
+ import static org .apache .groovy .parser .antlr4 .GroovyParser .*;
168
149
import static org .apache .groovy .parser .antlr4 .util .PositionConfigureUtils .configureAST ;
169
150
import static org .codehaus .groovy .classgen .asm .util .TypeUtil .isPrimitiveType ;
170
151
import static org .codehaus .groovy .runtime .DefaultGroovyMethods .asBoolean ;
@@ -445,29 +426,22 @@ public Statement visitConditionalStatement(final ConditionalStatementContext ctx
445
426
@ Override
446
427
public IfStatement visitIfElseStatement (final IfElseStatementContext ctx ) {
447
428
Expression conditionExpression = this .visitExpressionInPar (ctx .expressionInPar ());
448
- BooleanExpression booleanExpression =
449
- configureAST (
450
- new BooleanExpression (conditionExpression ), conditionExpression );
429
+ BooleanExpression booleanExpression = configureAST (new BooleanExpression (conditionExpression ), conditionExpression );
451
430
452
- Statement ifBlock =
453
- this .unpackStatement (
454
- (Statement ) this .visit (ctx .tb ));
455
- Statement elseBlock =
456
- this .unpackStatement (
457
- asBoolean (ctx .ELSE ())
458
- ? (Statement ) this .visit (ctx .fb )
459
- : EmptyStatement .INSTANCE );
431
+ Statement thenStatement = this .unpackStatement ((Statement ) this .visit (ctx .tb )) ;
432
+ Statement elseStatement = ctx .ELSE () != null ? this .unpackStatement ((Statement ) this .visit (ctx .fb )) : EmptyStatement .INSTANCE ;
460
433
461
- return configureAST (new IfStatement (booleanExpression , ifBlock , elseBlock ), ctx );
434
+ return configureAST (new IfStatement (booleanExpression , thenStatement , elseStatement ), ctx );
462
435
}
463
436
464
437
@ Override
465
438
public Statement visitLoopStmtAlt (final LoopStmtAltContext ctx ) {
466
439
visitingLoopStatementCount += 1 ;
467
- Statement result = configureAST ((Statement ) this .visit (ctx .loopStatement ()), ctx );
468
- visitingLoopStatementCount -= 1 ;
469
-
470
- return result ;
440
+ try {
441
+ return configureAST ((Statement ) this .visit (ctx .loopStatement ()), ctx );
442
+ } finally {
443
+ visitingLoopStatementCount -= 1 ;
444
+ }
471
445
}
472
446
473
447
@ Override
@@ -476,9 +450,7 @@ public ForStatement visitForStmtAlt(final ForStmtAltContext ctx) {
476
450
477
451
Statement loopBlock = this .unpackStatement ((Statement ) this .visit (ctx .statement ()));
478
452
479
- return configureAST (
480
- new ForStatement (controlTuple .getV1 (), controlTuple .getV2 (), asBoolean (loopBlock ) ? loopBlock : EmptyStatement .INSTANCE ),
481
- ctx );
453
+ return configureAST (new ForStatement (controlTuple .getV1 (), controlTuple .getV2 (), loopBlock ), ctx );
482
454
}
483
455
484
456
@ Override
@@ -502,12 +474,12 @@ public Expression visitForInit(final ForInitContext ctx) {
502
474
503
475
if (asBoolean (ctx .localVariableDeclaration ())) {
504
476
DeclarationListStatement declarationListStatement = this .visitLocalVariableDeclaration (ctx .localVariableDeclaration ());
505
- List <DeclarationExpression > declarationExpressions = declarationListStatement .getDeclarationExpressions ();
477
+ List <? extends Expression > declarationExpressions = declarationListStatement .getDeclarationExpressions ();
506
478
507
479
if (declarationExpressions .size () == 1 ) {
508
- return configureAST (( Expression ) declarationExpressions .get (0 ), ctx );
480
+ return configureAST (declarationExpressions .get (0 ), ctx );
509
481
} else {
510
- return configureAST (new ClosureListExpression ((List ) declarationExpressions ), ctx );
482
+ return configureAST (new ClosureListExpression ((List < Expression > ) declarationExpressions ), ctx );
511
483
}
512
484
}
513
485
@@ -563,28 +535,19 @@ public Tuple2<Parameter, Expression> visitClassicalForControl(final ClassicalFor
563
535
public WhileStatement visitWhileStmtAlt (final WhileStmtAltContext ctx ) {
564
536
Tuple2 <BooleanExpression , Statement > conditionAndBlock = createLoopConditionExpressionAndBlock (ctx .expressionInPar (), ctx .statement ());
565
537
566
- return configureAST (
567
- new WhileStatement (conditionAndBlock .getV1 (), asBoolean (conditionAndBlock .getV2 ()) ? conditionAndBlock .getV2 () : EmptyStatement .INSTANCE ),
568
- ctx );
538
+ return configureAST (new WhileStatement (conditionAndBlock .getV1 (), conditionAndBlock .getV2 ()), ctx );
569
539
}
570
540
571
541
@ Override
572
542
public DoWhileStatement visitDoWhileStmtAlt (final DoWhileStmtAltContext ctx ) {
573
543
Tuple2 <BooleanExpression , Statement > conditionAndBlock = createLoopConditionExpressionAndBlock (ctx .expressionInPar (), ctx .statement ());
574
544
575
- return configureAST (
576
- new DoWhileStatement (conditionAndBlock .getV1 (), asBoolean (conditionAndBlock .getV2 ()) ? conditionAndBlock .getV2 () : EmptyStatement .INSTANCE ),
577
- ctx );
545
+ return configureAST (new DoWhileStatement (conditionAndBlock .getV1 (), conditionAndBlock .getV2 ()), ctx );
578
546
}
579
547
580
548
private Tuple2 <BooleanExpression , Statement > createLoopConditionExpressionAndBlock (final ExpressionInParContext eipc , final StatementContext sc ) {
581
549
Expression conditionExpression = this .visitExpressionInPar (eipc );
582
-
583
- BooleanExpression booleanExpression =
584
- configureAST (
585
- new BooleanExpression (conditionExpression ),
586
- conditionExpression
587
- );
550
+ BooleanExpression booleanExpression = configureAST (new BooleanExpression (conditionExpression ), conditionExpression );
588
551
589
552
Statement loopBlock = this .unpackStatement ((Statement ) this .visit (sc ));
590
553
@@ -4080,16 +4043,12 @@ private BinaryExpression createBinaryExpression(final ExpressionContext left, fi
4080
4043
4081
4044
private Statement unpackStatement (final Statement statement ) {
4082
4045
if (statement instanceof DeclarationListStatement ) {
4083
- List <ExpressionStatement > expressionStatementList = ((DeclarationListStatement ) statement ).getDeclarationStatements ();
4084
-
4085
- if (1 == expressionStatementList .size ()) {
4086
- return expressionStatementList .get (0 );
4087
- }
4088
-
4089
- return configureAST (this .createBlockStatement (statement ), statement ); // if DeclarationListStatement contains more than 1 declarations, maybe it's better to create a block to hold them
4046
+ // if DeclarationListStatement contains more than 1 declarations, maybe it's better to create a block to hold them
4047
+ List <ExpressionStatement > expressionStatements = ((DeclarationListStatement ) statement ).getDeclarationStatements ();
4048
+ return expressionStatements .size () == 1 ? expressionStatements .get (0 ) : configureAST (this .createBlockStatement (statement ), statement );
4090
4049
}
4091
4050
4092
- return statement ;
4051
+ return Optional . ofNullable ( statement ). orElse ( EmptyStatement . INSTANCE ) ;
4093
4052
}
4094
4053
4095
4054
BlockStatement createBlockStatement (final Statement ... statements ) {
0 commit comments