diff options
author | fzaiser <fzaiser@google.com> | 2017-10-02 19:59:28 +0200 |
---|---|---|
committer | Klaus Aehlig <aehlig@google.com> | 2017-10-06 19:42:00 +0200 |
commit | 2a46d749ed76357fcc24b9c91726735db57a31e7 (patch) | |
tree | a9012f8d1d14bc2a90f8ec345d660858d810a0fa /src/main/java/com/google/devtools/build | |
parent | b8d4be0fc8a368de1bba40a27d2daa7110f1dccc (diff) |
Skylark parser: make the end position of location ranges inclusive.
Previously, the end line and column of a location were the position past the actual end of a location. This makes sense for the end offset, because one can use `input.substring(startOffset, endOffset)` to get the string belonging to an ASTNode.
However the line and column (as opposed to the offset) aren't used for that. Therefore I made the change that the end line and column now point to the last character in the location. This is also they way every compiler I know does it.
RELNOTES: none
PiperOrigin-RevId: 170723732
Diffstat (limited to 'src/main/java/com/google/devtools/build')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/events/Location.java | 19 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/syntax/Lexer.java | 7 |
2 files changed, 18 insertions, 8 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/events/Location.java b/src/main/java/com/google/devtools/build/lib/events/Location.java index 842025436a..24d2c2dcb8 100644 --- a/src/main/java/com/google/devtools/build/lib/events/Location.java +++ b/src/main/java/com/google/devtools/build/lib/events/Location.java @@ -17,7 +17,6 @@ package com.google.devtools.build.lib.events; import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.lib.vfs.PathFragment; - import java.io.Serializable; import java.util.Objects; @@ -117,11 +116,11 @@ public abstract class Location implements Serializable { } /** - * Returns the end offset relative to the beginning of the file the object - * resides in. + * Returns the end offset relative to the beginning of the file the object resides in. * - * <p>The end offset is one position past the actual end position, making this method - * behave in a compatible fashion with {@link String#substring(int, int)}. + * <p>The end offset is one position past the last character in range, making this method behave + * in a compatible fashion with {@link String#substring(int, int)}. (By contrast, {@link + * #getEndLineAndColumn} returns the actual end position.) * * <p>To compute the length of this location, use {@code getEndOffset() - getStartOffset()}. */ @@ -161,8 +160,14 @@ public abstract class Location implements Serializable { } /** - * Returns a (line, column) pair corresponding to the position denoted by - * getEndOffset. Returns null if this information is not available. + * Returns a (line, column) pair corresponding to the end position or null if this information is + * unavailable. + * + * <p>The returned line and column are the position of the last character in the location range. + * (By contrast, {@link #getEndOffset} returns the position <strong>past</strong> the actual end + * position.) In particular, this means that the location spans {@code + * getEndLineAndColumn().getColumn() - getStartLineAndColumn().getColumn() + 1} columns but + * contains {@code getEndOffset() - getStartOffset()} characters. */ public LineAndColumn getEndLineAndColumn() { return null; diff --git a/src/main/java/com/google/devtools/build/lib/syntax/Lexer.java b/src/main/java/com/google/devtools/build/lib/syntax/Lexer.java index b1b63c822d..10ff03e100 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/Lexer.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/Lexer.java @@ -184,7 +184,12 @@ public final class Lexer { @Override public LineAndColumn getEndLineAndColumn() { - return lineNumberTable.getLineAndColumn(getEndOffset()); + // The end offset is the location *past* the actual end position --> subtract 1: + int endOffset = getEndOffset() - 1; + if (endOffset < 0) { + endOffset = 0; + } + return lineNumberTable.getLineAndColumn(endOffset); } |