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/d822809a Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/d822809a Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/d822809a Branch: refs/heads/GROOVY_2_6_X Commit: d822809a2ac61e0bc1b4cbb8c2ad659cf0755568 Parents: 4925753 Author: sunlan <[email protected]> Authored: Sat May 13 13:16:08 2017 +0800 Committer: paulk <[email protected]> Committed: Sat May 13 22:01:10 2017 +1000 ---------------------------------------------------------------------- .../groovy/ast/LineColumnCheck_antlr4.txt | 4 +- .../apache/groovy/parser/antlr4/AstBuilder.java | 81 ++++++++++++++++---- 2 files changed, 66 insertions(+), 19 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/d822809a/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/d822809a/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 b193083..f50af1c 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 @@ -1554,12 +1554,13 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov } private DeclarationListStatement createFieldDeclarationListStatement(VariableDeclarationContext ctx, ModifierManager modifierManager, ClassNode variableType, List<DeclarationExpression> declarationExpressionList, ClassNode classNode) { - for (DeclarationExpression e : declarationExpressionList) { - 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()) { @@ -1581,7 +1582,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( @@ -1600,10 +1606,14 @@ 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; @@ -4087,7 +4097,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)) { @@ -4095,18 +4116,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) { @@ -4131,6 +4145,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);
