Skip to content

Commit

Permalink
Add source offsets for package, imports and annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Mar 13, 2018
1 parent 7ca789c commit 91eebf0
Show file tree
Hide file tree
Showing 8 changed files with 5,030 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -254,12 +254,12 @@ public void testBinaryExpr6() throws Exception {
"[:] + [:]");
}

@Test // Not right!!! ending whitespace is included, but shouldm't be.
@Test
public void testBinaryExpr7() throws Exception {
checkBinaryExprSLocs(
" a = b ",
new BinaryExpressionSLocTester(),
"a = b ");
Boolean.getBoolean("groovy.antlr4") ? "a = b" : "a = b ");
}

@Test
Expand Down
4,980 changes: 4,980 additions & 0 deletions base/org.codehaus.groovy26/src/org/apache/groovy/parser/antlr4/AstBuilder.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3722,8 +3722,7 @@ protected void configureAST(ASTNode node, AST ast) {
// openning '{', but shouldn't. If the new sloc is larger than the
// one being set, then ignore it and don't reset. Also numbers can
// result in an expression node that includes trailing whitespaces.
if ((node instanceof VariableExpression ||
(node instanceof ConstantExpression && ast.getType() == EXPR)) &&
if ((node instanceof VariableExpression || (node instanceof ConstantExpression && ast.getType() == EXPR)) &&
node.getEnd() > 0 && startoffset <= node.getStart() && endoffset >= node.getEnd()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,10 @@ public static ClassNode makeEnumNode(String name, int modifiers, ClassNode[] int
}

// GRECLIPSE edit
public static void addEnumConstant(ClassNode enumClass, String name, Expression init) {
addEnumConstant(enumClass, enumClass, name, init, -1, -1);
public static FieldNode addEnumConstant(ClassNode enumClass, String name, Expression init) {
return addEnumConstant(enumClass, enumClass, name, init, -1, -1);
}

// modified to return the FieldNode it creates, so that we can fix up the position
public static FieldNode addEnumConstant(ClassNode enumClassType, ClassNode enumClassOwner, String name, Expression init, int lineNumber, int colNumber) {
int modifiers = PUBLIC_FS | Opcodes.ACC_ENUM;
if (init != null && !(init instanceof ListExpression)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright 2009-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -16,6 +16,7 @@
package org.codehaus.groovy.antlr;

import java.util.List;
import java.util.Objects;

/**
* Maps lines/columns to offsets in a text file. Assumes '\n' is the newline
Expand All @@ -30,64 +31,65 @@
* <li> "a\nb" -> [0,2], [2,1]
* <li> "a\nbc\n" -> [0,2], [2,3], [5,0]
* </ul>
*
* @author Andrew Eisenberg
*/
public class LocationSupport {

public static final LocationSupport NO_LOCATIONS = new LocationSupport();
private final int[] lineEndings;

private static final int[] NO_LINE_ENDINGS = new int[0];

private final int[] lineEndings;
public static final LocationSupport NO_LOCATIONS = new LocationSupport();

public LocationSupport() {
lineEndings = NO_LINE_ENDINGS;
this(NO_LINE_ENDINGS);
}

public LocationSupport(int[] lineEndings) {
this.lineEndings = lineEndings;
this.lineEndings = Objects.requireNonNull(lineEndings);
}

public LocationSupport(List<StringBuffer> lines) {
if (lines != null) {
lineEndings = processLineEndings(lines);
} else {
lineEndings = NO_LINE_ENDINGS;
}
public LocationSupport(List<? extends CharSequence> lines) {
this(lines != null ? processLineEndings(lines) : NO_LINE_ENDINGS);
}

private int[] processLineEndings(List<? extends CharSequence> lines) {
int[] newLineEndings = new int[lines.size() + 1]; // last index stores end of file
private static int[] processLineEndings(List<? extends CharSequence> lines) {
int[] lineEndings = new int[lines.size() + 1]; // last index stores end of file
int total = 0;
int current = 1;
for (CharSequence line : lines) {
newLineEndings[current++] = (total += (line.length()));
lineEndings[current++] = (total += line.length());
}
return newLineEndings;
return lineEndings;
}

// TODO: Maybe should throw exception if out of bounds?
public int findOffset(int row, int col) {
return row <= lineEndings.length && row > 0 ? lineEndings[row - 1] + col - 1 : 0;
if (row > 0 && row <= lineEndings.length) {
return lineEndings[row - 1] + col - 1;
}
return 0;
}

public int getEnd() {
return lineEndings.length > 0 ? lineEndings[lineEndings.length - 1] : 0;
if (lineEndings.length > 0) {
return lineEndings[lineEndings.length - 1];
}
return 0;
}

public int getEndColumn() {
if (lineEndings.length > 1) {
return lineEndings[lineEndings.length - 1] - lineEndings[lineEndings.length - 2];
} else if (lineEndings.length > 0) {
return lineEndings[0];
} else {
return 0;
}
return 0;
}

public int getEndLine() {
return lineEndings.length > 0 ? lineEndings.length - 1 : 0; // last index contains length of document
if (lineEndings.length > 0) {
return lineEndings.length - 1;
}
return 0;
}

public int[] getRowCol(int offset) {
Expand All @@ -96,11 +98,10 @@ public int[] getRowCol(int offset) {
return new int[] {i, offset - lineEndings[i - 1] + 1};
}
}
// after end of document
throw new RuntimeException("Location is after end of document. Offset : " + offset);
throw new RuntimeException("Location is after end of document. Offset: " + offset);
}

public boolean isPopulated() {
return lineEndings.length > 0;
return (lineEndings.length > 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public class ASTNode implements NodeMetaDataHandler {
private int lastColumnNumber = -1;
// GRECLIPSE add
private int start = 0;
private int end = 0;
private int stop = 0;
// GRECLIPSE end
private Map metaDataMap = null;
private Map metaDataMap;
private NodeMetaDataHandlerHelper helper = new NodeMetaDataHandlerHelper(this);

public void visit(GroovyCodeVisitor visitor) {
Expand Down Expand Up @@ -104,17 +104,17 @@ public void setLastColumnNumber(int lastColumnNumber) {
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
public void setStart(int offset) {
start = offset;
}
public int getEnd() {
return end;
return stop;
}
public void setEnd(int end) {
this.end = end;
public void setEnd(int offset) {
stop = offset;
}
public int getLength() {
return end >= 0 && start >= 0 ? end - start : -1;
return (stop > 0 && start >= 0 ? stop - start : -1);
}
// GRECLIPSE end

Expand All @@ -133,7 +133,7 @@ public void setSourcePosition(ASTNode node) {
this.lineNumber = node.getLineNumber();
// GRECLIPSE add
this.start = node.getStart();
this.end = node.getEnd();
this.stop = node.getEnd();
// GRECLIPSE end
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public class AnnotatedNode extends ASTNode {
ClassNode declaringClass;
private boolean hasNoRealSourcePositionFlag;
// GRECLIPSE add
private int nameEnd;
private int nameStart;
private int nameStop;
// GRECLIPSE end

public AnnotatedNode() {
Expand Down Expand Up @@ -119,16 +119,16 @@ public int getNameStart() {
return nameStart;
}

public void setNameStart(int nameStart) {
this.nameStart = nameStart;
public void setNameStart(int offset) {
nameStart = offset;
}

public int getNameEnd() {
return nameEnd;
return nameStop;
}

public void setNameEnd(int nameEnd) {
this.nameEnd = nameEnd;
public void setNameEnd(int offset) {
nameStop = offset;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Collections;
import java.util.List;

/**
Expand Down Expand Up @@ -88,13 +89,13 @@ public class SourceUnit extends ProcessingUnit {
// GRECLIPSE add
public boolean isReconcile;

private List<Comment> comments;
public List<Comment> getComments() {
return comments;
}
public void setComments(List<Comment> comments) {
this.comments = comments;
}
private List<Comment> comments = Collections.emptyList();
// GRECLIPSE end

/**
Expand Down

0 comments on commit 91eebf0

Please sign in to comment.