Class SourceLocation

java.lang.Object
nz.org.riskscape.dsl.SourceLocation
All Implemented Interfaces:
Comparable<SourceLocation>

public class SourceLocation extends Object implements Comparable<SourceLocation>

Information about a specific character location within a string of source code

  • Constructor Details

    • SourceLocation

      public SourceLocation(URI uri, int index, int line, int column)
  • Method Details

    • parseUri

      public static Optional<SourceLocation> parseUri(URI uri)

      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. See toUri() for the inverse method.

      Parameters:
      uri - a URI that might contain a url fragment, can be null, see URI.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

      public static SourceLocation unlined(URI uri, int index)
      Returns:
      a location in source that has lost track of line numbers.
    • index

      public static SourceLocation index(URI uri, int index)
      Returns:
      a single lined location at the given index
    • start

      public static SourceLocation start(URI uri)

      Starting position.

    • toString

      public String toString()
      Overrides:
      toString in class Object
    • advance

      public SourceLocation advance(int moveByChars)
      Returns:
      a new SourceLocation shifted forward by moveByChars.
    • newline

      public SourceLocation 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

      public String toUriFragment()

      Creates a string that can be embedded in a URI as an anchor fragment, see #parseUriFragment(String) for the inverse method.

    • addToUri

      public URI addToUri(URI existing)

      Creates that extends/modifies the given URI so that it includes an anchor fragment that points to this source code location

    • toUri

      public URI toUri()
      Returns:
      a URI that describes this source location
    • getBaseUri

      public URI getBaseUri()
      Returns:
      the URI without an anchor fragment, i.e without the bit that describes the position in the source code
    • compareTo

      public int compareTo(SourceLocation rhs)
      Specified by:
      compareTo in interface Comparable<SourceLocation>
    • plus

      public SourceLocation plus(SourceLocation add)

      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 the add location) 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 add location is on line 1, e.g. it's a single line of code, then the new source locations's column is moved by column amount.

      • If the add location's line is > 1, then the line is incremented by that amount and and the new location's column is set to add.column

    • column

      public SourceLocation column(int newColumnLocation)
    • line

      public SourceLocation line(int newLineLocation)
    • getUri

      public URI 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

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • canEqual

      protected boolean canEqual(Object other)
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object