Class: Prism::Location

Inherits:
Object show all
Defined in:
lib/prism/parse_result.rb

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

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



108
109
110
111
112
113
# File 'lib/prism/parse_result.rb', line 108

def initialize(source, start_offset, length)
  @source = source
  @start_offset = start_offset
  @length = length
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

The list of comments attached to this location



104
105
106
# File 'lib/prism/parse_result.rb', line 104

def comments
  @comments
end

#lengthObject (readonly)

The length of this location in bytes.



101
102
103
# File 'lib/prism/parse_result.rb', line 101

def length
  @length
end

#sourceObject (readonly)

Returns the value of attribute source.



94
95
96
# File 'lib/prism/parse_result.rb', line 94

def source
  @source
end

#start_offsetObject (readonly)

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



98
99
100
# File 'lib/prism/parse_result.rb', line 98

def start_offset
  @start_offset
end

Class Method Details

.nullObject

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.



221
222
223
# File 'lib/prism/parse_result.rb', line 221

def self.null
  new(nil, 0, 0)
end

Instance Method Details

#==(other) ⇒ Object

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



202
203
204
205
206
# File 'lib/prism/parse_result.rb', line 202

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

#copy(**options) ⇒ Object

Create a new location object with the given options.



116
117
118
119
120
121
122
# File 'lib/prism/parse_result.rb', line 116

def copy(**options)
  Location.new(
    options.fetch(:source) { source },
    options.fetch(:start_offset) { start_offset },
    options.fetch(:length) { length }
  )
end

#deconstruct_keys(keys) ⇒ Object

Implement the hash pattern matching interface for Location.



192
193
194
# File 'lib/prism/parse_result.rb', line 192

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

#end_character_columnObject

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



187
188
189
# File 'lib/prism/parse_result.rb', line 187

def end_character_column
  source.character_column(end_offset)
end

#end_character_offsetObject

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



147
148
149
# File 'lib/prism/parse_result.rb', line 147

def end_character_offset
  source.character_offset(end_offset)
end

#end_columnObject

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



181
182
183
# File 'lib/prism/parse_result.rb', line 181

def end_column
  source.column(end_offset)
end

#end_lineObject

The line number where this location ends.



163
164
165
# File 'lib/prism/parse_result.rb', line 163

def end_line
  source.line(end_offset)
end

#end_offsetObject

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



141
142
143
# File 'lib/prism/parse_result.rb', line 141

def end_offset
  start_offset + length
end

#inspectObject

Returns a string representation of this location.



125
126
127
# File 'lib/prism/parse_result.rb', line 125

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

#join(other) ⇒ Object

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.



211
212
213
214
215
216
# File 'lib/prism/parse_result.rb', line 211

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) ⇒ Object

Implement the pretty print interface for Location.



197
198
199
# File 'lib/prism/parse_result.rb', line 197

def pretty_print(q)
  q.text("(#{start_line},#{start_column})-(#{end_line},#{end_column})")
end

#sliceObject

The source code that this location represents.



130
131
132
# File 'lib/prism/parse_result.rb', line 130

def slice
  source.slice(start_offset, length)
end

#start_character_columnObject

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



175
176
177
# File 'lib/prism/parse_result.rb', line 175

def start_character_column
  source.character_column(start_offset)
end

#start_character_offsetObject

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



136
137
138
# File 'lib/prism/parse_result.rb', line 136

def start_character_offset
  source.character_offset(start_offset)
end

#start_columnObject

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



169
170
171
# File 'lib/prism/parse_result.rb', line 169

def start_column
  source.column(start_offset)
end

#start_lineObject

The line number where this location starts.



152
153
154
# File 'lib/prism/parse_result.rb', line 152

def start_line
  source.line(start_offset)
end

#start_line_sliceObject

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



157
158
159
160
# File 'lib/prism/parse_result.rb', line 157

def start_line_slice
  offset = source.line_start(start_offset)
  source.slice(offset, start_offset - offset)
end