class Prism::Location

This represents a location in the source.

Attributes

comments[R]

The list of comments attached to this location

length[R]

The length of this location in bytes.

start_offset[R]

The byte offset from the beginning of the source where this location starts.

Public Class Methods

new(source, start_offset, length) click to toggle source

Create a new location object with the given source, start byte offset, and byte length.

# File prism/parse_result.rb, line 98
def initialize(source, start_offset, length)
  @source = source
  @start_offset = start_offset
  @length = length
  @comments = []
end
null() click to toggle source

Returns a null location that does not correspond to a source and points to the beginning of the file. Useful for when you want a location object but do not care where it points.

# File prism/parse_result.rb, line 187
def self.null
  new(nil, 0, 0)
end

Public Instance Methods

==(other) click to toggle source

Returns true if the given other location is equal to this location.

# File prism/parse_result.rb, line 168
def ==(other)
  other.is_a?(Location) &&
    other.start_offset == start_offset &&
    other.end_offset == end_offset
end
copy(**options) click to toggle source

Create a new location object with the given options.

# File prism/parse_result.rb, line 106
def copy(**options)
  Location.new(
    options.fetch(:source) { source },
    options.fetch(:start_offset) { start_offset },
    options.fetch(:length) { length }
  )
end
deconstruct_keys(keys) click to toggle source

Implement the hash pattern matching interface for Location.

# File prism/parse_result.rb, line 158
def deconstruct_keys(keys)
  { start_offset: start_offset, end_offset: end_offset }
end
end_column() click to toggle source

The column number in bytes where this location ends from the start of the line.

# File prism/parse_result.rb, line 153
def end_column
  source.column(end_offset)
end
end_line() click to toggle source

The line number where this location ends.

# File prism/parse_result.rb, line 141
def end_line
  source.line(end_offset)
end
end_offset() click to toggle source

The byte offset from the beginning of the source where this location ends.

# File prism/parse_result.rb, line 125
def end_offset
  start_offset + length
end
inspect() click to toggle source

Returns a string representation of this location.

# File prism/parse_result.rb, line 115
def inspect
  "#<Prism::Location @start_offset=#{@start_offset} @length=#{@length} start_line=#{start_line}>"
end
join(other) click to toggle source

Returns a new location that stretches from this location to the given other location. Raises an error if this location is not before the other location or if they don’t share the same source.

# File prism/parse_result.rb, line 177
def join(other)
  raise "Incompatible sources" if source != other.source
  raise "Incompatible locations" if start_offset > other.start_offset

  Location.new(source, start_offset, other.end_offset - start_offset)
end
pretty_print(q) click to toggle source

Implement the pretty print interface for Location.

# File prism/parse_result.rb, line 163
def pretty_print(q)
  q.text("(#{start_line},#{start_column})-(#{end_line},#{end_column})")
end
slice() click to toggle source

The source code that this location represents.

# File prism/parse_result.rb, line 120
def slice
  source.slice(start_offset, length)
end
start_column() click to toggle source

The column number in bytes where this location starts from the start of the line.

# File prism/parse_result.rb, line 147
def start_column
  source.column(start_offset)
end
start_line() click to toggle source

The line number where this location starts.

# File prism/parse_result.rb, line 130
def start_line
  source.line(start_offset)
end
start_line_slice() click to toggle source

The content of the line where this location starts before this location.

# File prism/parse_result.rb, line 135
def start_line_slice
  offset = source.line_offset(start_offset)
  source.slice(offset, start_offset - offset)
end