You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2017/04/11 01:37:49 UTC

[13/50] groovy git commit: Refine the position information of postfix expression AST node

Refine the position information of postfix expression AST node


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

Branch: refs/heads/master
Commit: 65c2bf5244a260ed9821f1b23ba4e8f060d113b6
Parents: aaf704f
Author: sunlan <su...@apache.org>
Authored: Tue Jan 24 12:03:12 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Tue Jan 24 12:03:12 2017 +0800

----------------------------------------------------------------------
 .../apache/groovy/parser/antlr4/GroovyParser.g4 | 22 ++++++++++++++--
 .../apache/groovy/parser/antlr4/AstBuilder.java | 27 +++++++++++++++-----
 2 files changed, 40 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/65c2bf52/subprojects/groovy-parser-antlr4/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4
----------------------------------------------------------------------
diff --git a/subprojects/groovy-parser-antlr4/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4 b/subprojects/groovy-parser-antlr4/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4
index eb93aeb..5bf6f87 100644
--- a/subprojects/groovy-parser-antlr4/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4
+++ b/subprojects/groovy-parser-antlr4/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4
@@ -667,9 +667,13 @@ locals[boolean resourcesExists = false]
         )
     ;
 
+assertStatement
+locals[ String footprint = "" ]
+    :   ASSERT ce=expression ((COLON | COMMA) nls me=expression)?
+    ;
+
 statement
     :   block                                                                                               #blockStmtAlt
-    |   ASSERT ce=expression ((COLON | COMMA) nls me=expression)?                                           #assertStmtAlt
     |   IF parExpression nls tb=statement ((nls | sep) ELSE nls fb=statement)?                              #ifElseStmtAlt
     |   loopStatement                                                                                       #loopStmtAlt
 
@@ -688,6 +692,8 @@ statement
     // Import statement.  Can be used in any scope.  Has "import x as y" also.
     |   importDeclaration                                                                                   #importStmtAlt
 
+    |   assertStatement                                                                                     #assertStmtAlt
+
     |   typeDeclaration                                                                                     #typeDeclarationStmtAlt
     |   localVariableDeclaration                                                                            #localVariableDeclarationStmtAlt
 
@@ -786,11 +792,23 @@ statementExpression
     |   commandExpression                   #commandExprAlt
     ;
 
+postfixExpression
+locals[ boolean isInsideAssert ]
+@init {
+    try {
+        $isInsideAssert = null != $assertStatement::footprint;
+    } catch(NullPointerException e) {
+        $isInsideAssert = false;
+    }
+}
+    :   pathExpression op=(INC | DEC)?
+    ;
+
 expression
     // qualified names, array expressions, method invocation, post inc/dec, type casting (level 1)
     // The cast expression must be put before pathExpression to resovle the ambiguities between type casting and call on parentheses expression, e.g. (int)(1 / 2)
     :   castParExpression expression                                                        #castExprAlt
-    |   pathExpression op=(INC | DEC)?                                                      #postfixExprAlt
+    |   postfixExpression                                                                   #postfixExprAlt
 
     // ~(BNOT)/!(LNOT) (level 1)
     |   (BITNOT | NOT) nls expression                                                       #unaryNotExprAlt

http://git-wip-us.apache.org/repos/asf/groovy/blob/65c2bf52/subprojects/groovy-parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
----------------------------------------------------------------------
diff --git a/subprojects/groovy-parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java b/subprojects/groovy-parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
index d0b5c72..f3195ef 100644
--- a/subprojects/groovy-parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/subprojects/groovy-parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -262,8 +262,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
 
         PackageNode packageNode = moduleNode.getPackage();
 
-        this.visitAnnotationsOpt(ctx.annotationsOpt()).stream()
-                .forEach(packageNode::addAnnotation);
+        this.visitAnnotationsOpt(ctx.annotationsOpt()).forEach(packageNode::addAnnotation);
 
         return this.configureAST(packageNode, ctx);
     }
@@ -331,7 +330,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
 
     // statement {    --------------------------------------------------------------------
     @Override
-    public AssertStatement visitAssertStmtAlt(AssertStmtAltContext ctx) {
+    public AssertStatement visitAssertStatement(AssertStatementContext ctx) {
         Expression conditionExpression = (Expression) this.visit(ctx.ce);
         BooleanExpression booleanExpression =
                 this.configureAST(
@@ -345,7 +344,11 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
         return this.configureAST(new AssertStatement(booleanExpression,
                         (Expression) this.visit(ctx.me)),
                 ctx);
+    }
 
+    @Override
+    public AssertStatement visitAssertStmtAlt(AssertStmtAltContext ctx) {
+        return this.configureAST(this.visitAssertStatement(ctx.assertStatement()), ctx);
     }
 
     @Override
@@ -2214,19 +2217,29 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
     }
 
     @Override
-    public Expression visitPostfixExprAlt(PostfixExprAltContext ctx) {
+    public Expression visitPostfixExpression(PostfixExpressionContext ctx) {
         Expression pathExpr = this.visitPathExpression(ctx.pathExpression());
 
         if (asBoolean(ctx.op)) {
-            return this.configureAST(
-                    new PostfixExpression(pathExpr, createGroovyToken(ctx.op)),
-                    ctx.op/*powerassert requires different column for values, so we have to copy the location of op*/);
+            PostfixExpression postfixExpression = new PostfixExpression(pathExpr, createGroovyToken(ctx.op));
+
+            if (ctx.isInsideAssert) {
+                // powerassert requires different column for values, so we have to copy the location of op
+                return this.configureAST(postfixExpression, ctx.op);
+            } else {
+                return this.configureAST(postfixExpression, ctx);
+            }
         }
 
         return this.configureAST(pathExpr, ctx);
     }
 
     @Override
+    public Expression visitPostfixExprAlt(PostfixExprAltContext ctx) {
+        return this.visitPostfixExpression(ctx.postfixExpression());
+    }
+
+    @Override
     public Expression visitUnaryNotExprAlt(UnaryNotExprAltContext ctx) {
         if (asBoolean(ctx.NOT())) {
             return this.configureAST(