class Prism::Location

This represents a location in the source.

Attributes

length[R]

The length of this location in bytes.

source[R]

A Source object that is used to determine more information from the given offset and length.

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 lib/prism/parse_result.rb, line 119
def initialize(source, start_offset, length)
  @source = source
  @start_offset = start_offset
  @length = length

  # These are used to store comments that are associated with this location.
  # They are initialized to `nil` to save on memory when there are no
  # comments to be attached and/or the comment-related APIs are not used.
  @leading_comments = nil
  @trailing_comments = nil
end

Public Instance Methods

==(other) click to toggle source

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

# File lib/prism/parse_result.rb, line 269
def ==(other)
  Location === other &&
    other.start_offset == start_offset &&
    other.end_offset == end_offset
end
chop() click to toggle source

Returns a new location that is the result of chopping off the last byte.

# File lib/prism/parse_result.rb, line 165
def chop
  copy(length: length == 0 ? length : length - 1)
end
comments() click to toggle source

Returns all comments that are associated with this location (both leading and trailing comments).

# File lib/prism/parse_result.rb, line 155
def comments
  [*@leading_comments, *@trailing_comments]
end
copy(source: self.source, start_offset: self.start_offset, length: self.length) click to toggle source

Create a new location object with the given options.

# File lib/prism/parse_result.rb, line 160
def copy(source: self.source, start_offset: self.start_offset, length: self.length)
  Location.new(source, start_offset, length)
end
deconstruct_keys(keys) click to toggle source

Implement the hash pattern matching interface for Location.

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

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

# File lib/prism/parse_result.rb, line 248
def end_character_column
  source.character_column(end_offset)
end
end_character_offset() click to toggle source

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

# File lib/prism/parse_result.rb, line 197
def end_character_offset
  source.character_offset(end_offset)
end
end_code_units_column(encoding = Encoding::UTF_16LE) click to toggle source

The column number in code units of the given encoding where this location ends from the start of the line.

# File lib/prism/parse_result.rb, line 254
def end_code_units_column(encoding = Encoding::UTF_16LE)
  source.code_units_column(end_offset, encoding)
end
end_code_units_offset(encoding = Encoding::UTF_16LE) click to toggle source

The offset from the start of the file in code units of the given encoding.

# File lib/prism/parse_result.rb, line 202
def end_code_units_offset(encoding = Encoding::UTF_16LE)
  source.code_units_offset(end_offset, encoding)
end
end_column() click to toggle source

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

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

The line number where this location ends.

# File lib/prism/parse_result.rb, line 218
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 lib/prism/parse_result.rb, line 191
def end_offset
  start_offset + length
end
inspect() click to toggle source

Returns a string representation of this location.

# File lib/prism/parse_result.rb, line 170
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 lib/prism/parse_result.rb, line 278
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
leading_comment(comment) click to toggle source

Attach a comment to the leading comments of this location.

# File lib/prism/parse_result.rb, line 138
def leading_comment(comment)
  leading_comments << comment
end
leading_comments() click to toggle source

These are the comments that are associated with this location that exist before the start of this location.

# File lib/prism/parse_result.rb, line 133
def leading_comments
  @leading_comments ||= []
end
pretty_print(q) click to toggle source

Implement the pretty print interface for Location.

# File lib/prism/parse_result.rb, line 264
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 lib/prism/parse_result.rb, line 175
def slice
  source.slice(start_offset, length)
end
start_character_column() click to toggle source

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

# File lib/prism/parse_result.rb, line 230
def start_character_column
  source.character_column(start_offset)
end
start_character_offset() click to toggle source

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

# File lib/prism/parse_result.rb, line 181
def start_character_offset
  source.character_offset(start_offset)
end
start_code_units_column(encoding = Encoding::UTF_16LE) click to toggle source

The column number in code units of the given encoding where this location starts from the start of the line.

# File lib/prism/parse_result.rb, line 236
def start_code_units_column(encoding = Encoding::UTF_16LE)
  source.code_units_column(start_offset, encoding)
end
start_code_units_offset(encoding = Encoding::UTF_16LE) click to toggle source

The offset from the start of the file in code units of the given encoding.

# File lib/prism/parse_result.rb, line 186
def start_code_units_offset(encoding = Encoding::UTF_16LE)
  source.code_units_offset(start_offset, encoding)
end
start_column() click to toggle source

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

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

The line number where this location starts.

# File lib/prism/parse_result.rb, line 207
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 lib/prism/parse_result.rb, line 212
def start_line_slice
  offset = source.line_start(start_offset)
  source.slice(offset, start_offset - offset)
end
trailing_comment(comment) click to toggle source

Attach a comment to the trailing comments of this location.

# File lib/prism/parse_result.rb, line 149
def trailing_comment(comment)
  trailing_comments << comment
end
trailing_comments() click to toggle source

These are the comments that are associated with this location that exist after the end of this location.

# File lib/prism/parse_result.rb, line 144
def trailing_comments
  @trailing_comments ||= []
end