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 2017/05/13 05:16:21 UTC

groovy git commit: Refine node position of fields

Repository: groovy
Updated Branches:
  refs/heads/master 92ec412cb -> 7da88ad24


Refine node position of fields


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

Branch: refs/heads/master
Commit: 7da88ad2467a9a82f1c7df008b244e82f4d664b8
Parents: 92ec412
Author: sunlan <su...@apache.org>
Authored: Sat May 13 13:16:08 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Sat May 13 13:16:08 2017 +0800

----------------------------------------------------------------------
 .../groovy/ast/LineColumnCheck_antlr4.txt       |  4 +-
 .../apache/groovy/parser/antlr4/AstBuilder.java | 83 +++++++++++++++-----
 2 files changed, 67 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/7da88ad2/src/test/org/codehaus/groovy/ast/LineColumnCheck_antlr4.txt
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/ast/LineColumnCheck_antlr4.txt b/src/test/org/codehaus/groovy/ast/LineColumnCheck_antlr4.txt
index f959523..8659937 100644
--- a/src/test/org/codehaus/groovy/ast/LineColumnCheck_antlr4.txt
+++ b/src/test/org/codehaus/groovy/ast/LineColumnCheck_antlr4.txt
@@ -22,8 +22,8 @@ public class Test {
 	public attribute = 6, second = 9
 	String prop = "property"
 }
-:::[FieldNode,(2:2),(2:34)][ConstantExpression,(2:21),(2:22)];
-[FieldNode,(2:2),(2:34)][ConstantExpression,(2:33),(2:34)];
+:::[FieldNode,(2:2),(2:22)][ConstantExpression,(2:21),(2:22)];
+[FieldNode,(2:24),(2:34)][ConstantExpression,(2:33),(2:34)];
 [FieldNode,(3:2),(3:26)][ConstantExpression,(3:16),(3:26)]
 
 ###ifElse:::

http://git-wip-us.apache.org/repos/asf/groovy/blob/7da88ad2/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 9cbc030..2930f26 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
@@ -1525,12 +1525,13 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
     }
 
     private DeclarationListStatement createFieldDeclarationListStatement(VariableDeclarationContext ctx, ModifierManager modifierManager, ClassNode variableType, List<DeclarationExpression> declarationExpressionList, ClassNode classNode) {
-        declarationExpressionList.forEach(e -> {
-            VariableExpression variableExpression = (VariableExpression) e.getLeftExpression();
+        for (int i = 0, n = declarationExpressionList.size(); i < n; i++) {
+            DeclarationExpression declarationExpression = declarationExpressionList.get(i);
+            VariableExpression variableExpression = (VariableExpression) declarationExpression.getLeftExpression();
 
             int modifiers = modifierManager.getClassMemberModifiersOpValue();
 
-            Expression initialValue = EmptyExpression.INSTANCE.equals(e.getRightExpression()) ? null : e.getRightExpression();
+            Expression initialValue = EmptyExpression.INSTANCE.equals(declarationExpression.getRightExpression()) ? null : declarationExpression.getRightExpression();
             Object defaultValue = findDefaultValueByType(variableType);
 
             if (classNode.isInterface()) {
@@ -1552,7 +1553,12 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
 
                 groovydocManager.handle(fieldNode, ctx);
 
-                this.configureAST(fieldNode, ctx);
+                if (0 == i) {
+                    this.configureAST(fieldNode, ctx, initialValue);
+                } else {
+                    this.configureAST(fieldNode, variableExpression, initialValue);
+                }
+
             } else {
                 PropertyNode propertyNode =
                         classNode.addProperty(
@@ -1571,11 +1577,15 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
                 groovydocManager.handle(fieldNode, ctx);
                 groovydocManager.handle(propertyNode, ctx);
 
-                this.configureAST(fieldNode, ctx);
-                this.configureAST(propertyNode, ctx);
+                if (0 == i) {
+                    this.configureAST(fieldNode, ctx, initialValue);
+                    this.configureAST(propertyNode, ctx, initialValue);
+                } else {
+                    this.configureAST(fieldNode, variableExpression, initialValue);
+                    this.configureAST(propertyNode, variableExpression, initialValue);
+                }
             }
-
-        });
+        }
 
         return null;
     }
@@ -4008,7 +4018,18 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
         Token start = ctx.getStart();
         Token stop = ctx.getStop();
 
-        String stopText = stop.getText();
+        astNode.setLineNumber(start.getLine());
+        astNode.setColumnNumber(start.getCharPositionInLine() + 1);
+
+        Pair<Integer, Integer> stopTokenEndPosition = endPosition(stop);
+        astNode.setLastLineNumber(stopTokenEndPosition.getKey());
+        astNode.setLastColumnNumber(stopTokenEndPosition.getValue());
+
+        return astNode;
+    }
+
+    private Pair<Integer, Integer> endPosition(Token token) {
+        String stopText = token.getText();
         int stopTextLength = 0;
         int newLineCnt = 0;
         if (asBoolean((Object) stopText)) {
@@ -4016,18 +4037,11 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
             newLineCnt = (int) StringUtils.countChar(stopText, '\n');
         }
 
-        astNode.setLineNumber(start.getLine());
-        astNode.setColumnNumber(start.getCharPositionInLine() + 1);
-
         if (0 == newLineCnt) {
-            astNode.setLastLineNumber(stop.getLine());
-            astNode.setLastColumnNumber(stop.getCharPositionInLine() + 1 + stop.getText().length());
+            return new Pair<Integer, Integer>(token.getLine(), token.getCharPositionInLine() + 1 + token.getText().length());
         } else { // e.g. GStringEnd contains newlines, we should fix the location info
-            astNode.setLastLineNumber(stop.getLine() + newLineCnt);
-            astNode.setLastColumnNumber(stopTextLength - stopText.lastIndexOf('\n'));
+            return new Pair<Integer, Integer>(token.getLine() + newLineCnt, stopTextLength - stopText.lastIndexOf('\n'));
         }
-
-        return astNode;
     }
 
     private <T extends ASTNode> T configureAST(T astNode, TerminalNode terminalNode) {
@@ -4052,6 +4066,39 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
         return astNode;
     }
 
+    private <T extends ASTNode> T configureAST(T astNode, GroovyParserRuleContext ctx, ASTNode stop) {
+        Token start = ctx.getStart();
+
+        astNode.setLineNumber(start.getLine());
+        astNode.setColumnNumber(start.getCharPositionInLine() + 1);
+
+        if (asBoolean(stop)) {
+            astNode.setLastLineNumber(stop.getLastLineNumber());
+            astNode.setLastColumnNumber(stop.getLastColumnNumber());
+        } else {
+            Pair<Integer, Integer> endPosition = endPosition(start);
+            astNode.setLastLineNumber(endPosition.getKey());
+            astNode.setLastColumnNumber(endPosition.getValue());
+        }
+
+        return astNode;
+    }
+
+    private <T extends ASTNode> T configureAST(T astNode, ASTNode start, ASTNode stop) {
+        astNode.setLineNumber(start.getLineNumber());
+        astNode.setColumnNumber(start.getColumnNumber());
+
+        if (asBoolean(stop)) {
+            astNode.setLastLineNumber(stop.getLastLineNumber());
+            astNode.setLastColumnNumber(stop.getLastColumnNumber());
+        } else {
+            astNode.setLastLineNumber(start.getLastLineNumber());
+            astNode.setLastColumnNumber(start.getLastColumnNumber());
+        }
+
+        return astNode;
+    }
+
     private boolean isTrue(GroovyParserRuleContext ctx, String key) {
         Object nmd = ctx.getNodeMetaData(key);