Class SourceLocation

java.lang.Object
mars.assembler.log.SourceLocation
All Implemented Interfaces:
Comparable<SourceLocation>

public class SourceLocation extends Object implements Comparable<SourceLocation>
Class representing some location in a source file given to the assembler. The location can reference either a line as a whole or a specific column within the line.
Author:
Sean Clarke, October 2024
  • Constructor Details

    • SourceLocation

      public SourceLocation(String filename)
      Create a new SourceLocation referencing a file as a whole.
      Parameters:
      filename - The name of the source file.
    • SourceLocation

      public SourceLocation(String filename, int lineIndex)
      Create a new SourceLocation referencing a line as a whole.
      Parameters:
      filename - The name of the source file.
      lineIndex - The zero-based index of the line.
    • SourceLocation

      public SourceLocation(String filename, int lineIndex, int columnIndex)
      Create a new SourceLocation referencing a specific column in a line.
      Parameters:
      filename - The name of the source file.
      lineIndex - The zero-based index of the line.
      columnIndex - The zero-based index of the column.
    • SourceLocation

      public SourceLocation(SourceLocation location)
      Create a clone of an existing SourceLocation.
      Parameters:
      location - The object to clone.
  • Method Details

    • getFilename

      public String getFilename()
      Get the name of the source file referenced by this location.
      Returns:
      The source filename, or null if not applicable.
    • getLineIndex

      public int getLineIndex()
      Get the line number referenced by this location, where the first line in the file is line 0.
      Returns:
      The zero-based line index.
    • getColumnIndex

      public int getColumnIndex()
      Get the column number referenced by this location, where the beginning of the line is column 0. If this location represents the entire line, the column number will be negative.
      Returns:
      The zero-based column index, or -1 if not applicable.
    • toLineLocation

      public SourceLocation toLineLocation()
      Generates a new location referencing the line as a whole rather than a particular column in the line.
      Returns:
      The line location.
    • toColumnLocation

      public SourceLocation toColumnLocation(int columnIndex)
      Generates a new location referencing a column in the line referenced by this location.
      Parameters:
      columnIndex - The zero-based index of the column.
      Returns:
      The column location.
    • compareTo

      public int compareTo(SourceLocation that)
      Compare two locations. The path is compared first; if it is the same, the lineIndex is compared; if it is the same, the columnIndex is compared.
      Specified by:
      compareTo in interface Comparable<SourceLocation>
      Parameters:
      that - The location to compare against.
      Returns:
      An integer whose value is positive, negative, or zero to indicate whether this is greater than, less than, or equal to that, respectively.
    • equals

      public boolean equals(Object object)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Get the string representation of this location. For example, a location with a path of path/to/file.asm, a lineIndex of 10, and a columnIndex of 20 produces the following string:
      (path/to/file.asm, line 11, column 21)

      Fields are omitted from the output if they are not applicable (i.e. null or negative).

      Overrides:
      toString in class Object
      Returns:
      The string representation of this location.