Class SourceLocation
- All Implemented Interfaces:
Comparable<SourceLocation>
Information about a specific character location within a string of source code
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCreates that extends/modifies the given URI so that it includes an anchor fragment that points to this source code locationadvance(int moveByChars) protected booleancolumn(int newColumnLocation) intcompareTo(SourceLocation rhs) booleanintColumn number within the current line (1 based)intgetIndex()Absolute index in to the source string.intgetLine()Line number (1 based)getUri()the source being parsed.inthashCode()static SourceLocationbooleanbooleanline(int newLineLocation) newline()static Optional<SourceLocation>Attempt to parse the given URI and turn it in to aSourceLocation, returning empty if it doesn't have a fragment, or if the fragment doesn't parse.plus(SourceLocation add) Add another source location to this one, effectively moving this source location by the amount inadd, taking in to account the line and columns.static SourceLocationStarting position.toString()toUri()Creates a string that can be embedded in a URI as an anchor fragment, see#parseUriFragment(String)for the inverse method.static SourceLocation
-
Constructor Details
-
SourceLocation
-
-
Method Details
-
parseUri
Attempt to parse the given URI and turn it in to a
SourceLocation, returning empty if it doesn't have a fragment, or if the fragment doesn't parse. SeetoUri()for the inverse method.- Parameters:
uri- a URI that might contain a url fragment, can be null, seeURI.getFragment().Note that we can probably drop this method now - it was a bit of a kludge to embed line locations into URIs for error messages before SourceLocation wrapped a URI as well. As we start to build ASTs from Source objects, we can avoid this kludge and print error messages using the object model instead.
-
unlined
- Returns:
- a location in source that has lost track of line numbers.
-
index
- Returns:
- a single lined location at the given index
-
start
Starting position.
-
toString
-
advance
- Returns:
- a new SourceLocation shifted forward by moveByChars.
-
newline
- Returns:
- a new SourceLocation at the same overall index, but at the start of a new line, i.e. index stays the same, line is incremented and column is set to 1.
-
isUnlined
public boolean isUnlined()- Returns:
- true if this source location has lost track of lines
-
isAnonymous
public boolean isAnonymous()- Returns:
- true if the uri for this source is the unknown URI
-
toUriFragment
Creates a string that can be embedded in a URI as an anchor fragment, see
#parseUriFragment(String)for the inverse method. -
addToUri
Creates that extends/modifies the given URI so that it includes an anchor fragment that points to this source code location
-
toUri
- Returns:
- a URI that describes this source location
-
getBaseUri
- Returns:
- the URI without an anchor fragment, i.e without the bit that describes the position in the source code
-
compareTo
- Specified by:
compareToin interfaceComparable<SourceLocation>
-
plus
Add another source location to this one, effectively moving this source location by the amount in
add, taking in to account the line and columns. You can think of this method as returning the cursor location after pasting a block of code (whose length is represented theaddlocation) at the position represented by this location.This method is here to support generation of a source location for an embedded block of code that was extracted from a configuration file. This source location represents the position where the embedded block of code was defined and the add location is the amount of whitespace that was eaten before the code actually started.
If the
addlocation is on line 1, e.g. it's a single line of code, then the new source locations's column is moved bycolumnamount.If the
addlocation's line is > 1, then the line is incremented by that amount and and the new location's column is set toadd.column
-
column
-
line
-
getUri
the source being parsed.
-
getIndex
public int getIndex()Absolute index in to the source string.
-
getLine
public int getLine()Line number (1 based)
-
getColumn
public int getColumn()Column number within the current line (1 based)
-
equals
-
canEqual
-
hashCode
public int hashCode()
-