class YARP::Location
This represents a location in the source.
Attributes
The list of comments attached to this location
The length of this location in bytes.
The byte offset from the beginning of the source where this location starts.
Public Class Methods
# File yarp.rb, line 56 def initialize(source, start_offset, length) @source = source @start_offset = start_offset @length = length @comments = [] end
# File yarp.rb, line 139 def self.null new(0, 0) end
Public Instance Methods
# File yarp.rb, line 123 def ==(other) other.is_a?(Location) && other.start_offset == start_offset && other.end_offset == end_offset end
Create a new location object with the given options.
# File yarp.rb, line 64 def copy(**options) Location.new( options.fetch(:source) { source }, options.fetch(:start_offset) { start_offset }, options.fetch(:length) { length } ) end
# File yarp.rb, line 115 def deconstruct_keys(keys) { start_offset: start_offset, end_offset: end_offset } end
The column number in bytes where this location ends from the start of the line.
# File yarp.rb, line 111 def end_column source.column(end_offset) end
The line number where this location ends.
# File yarp.rb, line 99 def end_line source.line(end_offset - 1) end
The byte offset from the beginning of the source where this location ends.
# File yarp.rb, line 83 def end_offset start_offset + length end
Returns a string representation of this location.
# File yarp.rb, line 73 def inspect "#<YARP::Location @start_offset=#{@start_offset} @length=#{@length}>" end
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 yarp.rb, line 132 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
# File yarp.rb, line 119 def pretty_print(q) q.text("(#{start_offset}...#{end_offset})") end
The source code that this location represents.
# File yarp.rb, line 78 def slice source.slice(start_offset, length) end
The column number in bytes where this location starts from the start of the line.
# File yarp.rb, line 105 def start_column source.column(start_offset) end
The line number where this location starts.
# File yarp.rb, line 88 def start_line source.line(start_offset) end
The content of the line where this location starts before this location.
# File yarp.rb, line 93 def start_line_slice offset = source.line_offset(start_offset) source.slice(offset, start_offset - offset) end