Class: YARP::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/yarp.rb,
ext/yarp/extension.c

Overview

This represents a location in the source.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, start_offset, length) ⇒ Location

Returns a new instance of Location.



49
50
51
52
53
# File 'lib/yarp.rb', line 49

def initialize(source, start_offset, length)
  @source = source
  @start_offset = start_offset
  @length = length
end

Instance Attribute Details

#lengthObject (readonly)

The length of this location in bytes.



47
48
49
# File 'lib/yarp.rb', line 47

def length
  @length
end

#start_offsetObject (readonly)

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



44
45
46
# File 'lib/yarp.rb', line 44

def start_offset
  @start_offset
end

Class Method Details

.nullObject



105
106
107
# File 'lib/yarp.rb', line 105

def self.null
  new(0, 0)
end

Instance Method Details

#==(other) ⇒ Object



99
100
101
102
103
# File 'lib/yarp.rb', line 99

def ==(other)
  other.is_a?(Location) &&
    other.start_offset == start_offset &&
    other.end_offset == end_offset
end

#deconstruct_keys(keys) ⇒ Object



91
92
93
# File 'lib/yarp.rb', line 91

def deconstruct_keys(keys)
  { start_offset: start_offset, end_offset: end_offset }
end

#end_columnObject

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



87
88
89
# File 'lib/yarp.rb', line 87

def end_column
  source.column(end_offset - 1)
end

#end_lineObject

The line number where this location ends.



75
76
77
# File 'lib/yarp.rb', line 75

def end_line
  source.line(end_offset - 1)
end

#end_offsetObject

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



65
66
67
# File 'lib/yarp.rb', line 65

def end_offset
  start_offset + length
end

#inspectObject



55
56
57
# File 'lib/yarp.rb', line 55

def inspect
  "#<YARP::Location @start_offset=#{@start_offset} @length=#{@length}>"
end

#pretty_print(q) ⇒ Object



95
96
97
# File 'lib/yarp.rb', line 95

def pretty_print(q)
  q.text("(#{start_offset}...#{end_offset})")
end

#sliceObject

The source code that this location represents.



60
61
62
# File 'lib/yarp.rb', line 60

def slice
  source.slice(start_offset, length)
end

#start_columnObject

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



81
82
83
# File 'lib/yarp.rb', line 81

def start_column
  source.column(start_offset)
end

#start_lineObject

The line number where this location starts.



70
71
72
# File 'lib/yarp.rb', line 70

def start_line
  source.line(start_offset)
end