Class: Prism::Location
- Inherits:
-
Object
- Object
- Prism::Location
- Defined in:
- lib/prism/parse_result.rb,
ext/prism/extension.c
Overview
This represents a location in the source.
Instance Attribute Summary collapse
-
#length ⇒ Object
readonly
The length of this location in bytes.
-
#start_offset ⇒ Object
readonly
The byte offset from the beginning of the source where this location starts.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Returns true if the given other location is equal to this location.
-
#adjoin(string) ⇒ Object
Join this location with the first occurrence of the string in the source that occurs after this location on the same line, and return the new location.
-
#chop ⇒ Object
Returns a new location that is the result of chopping off the last byte.
-
#comments ⇒ Object
Returns all comments that are associated with this location (both leading and trailing comments).
-
#copy(source: self.source, start_offset: self.start_offset, length: self.length) ⇒ Object
Create a new location object with the given options.
-
#deconstruct_keys(keys) ⇒ Object
Implement the hash pattern matching interface for Location.
-
#end_character_column ⇒ Object
The column number in characters where this location ends from the start of the line.
-
#end_character_offset ⇒ Object
The character offset from the beginning of the source where this location ends.
-
#end_code_units_column(encoding = Encoding::UTF_16LE) ⇒ Object
The column number in code units of the given encoding where this location ends from the start of the line.
-
#end_code_units_offset(encoding = Encoding::UTF_16LE) ⇒ Object
The offset from the start of the file in code units of the given encoding.
-
#end_column ⇒ Object
The column number in bytes where this location ends from the start of the line.
-
#end_line ⇒ Object
The line number where this location ends.
-
#end_offset ⇒ Object
The byte offset from the beginning of the source where this location ends.
-
#initialize(source, start_offset, length) ⇒ Location
constructor
Create a new location object with the given source, start byte offset, and byte length.
-
#inspect ⇒ Object
Returns a string representation of this location.
-
#join(other) ⇒ Object
Returns a new location that stretches from this location to the given other location.
-
#leading_comment(comment) ⇒ Object
Attach a comment to the leading comments of this location.
-
#leading_comments ⇒ Object
These are the comments that are associated with this location that exist before the start of this location.
-
#pretty_print(q) ⇒ Object
Implement the pretty print interface for Location.
-
#slice ⇒ Object
The source code that this location represents.
-
#slice_lines ⇒ Object
The source code that this location represents starting from the beginning of the line that this location starts on to the end of the line that this location ends on.
-
#source_lines ⇒ Object
Returns all of the lines of the source code associated with this location.
-
#start_character_column ⇒ Object
The column number in characters where this location ends from the start of the line.
-
#start_character_offset ⇒ Object
The character offset from the beginning of the source where this location starts.
-
#start_code_units_column(encoding = Encoding::UTF_16LE) ⇒ Object
The column number in code units of the given encoding where this location starts from the start of the line.
-
#start_code_units_offset(encoding = Encoding::UTF_16LE) ⇒ Object
The offset from the start of the file in code units of the given encoding.
-
#start_column ⇒ Object
The column number in bytes where this location starts from the start of the line.
-
#start_line ⇒ Object
The line number where this location starts.
-
#start_line_slice ⇒ Object
The content of the line where this location starts before this location.
-
#trailing_comment(comment) ⇒ Object
Attach a comment to the trailing comments of this location.
-
#trailing_comments ⇒ Object
These are the comments that are associated with this location that exist after the end of this location.
Constructor Details
#initialize(source, start_offset, length) ⇒ Location
Create a new location object with the given source, start byte offset, and byte length.
180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/prism/parse_result.rb', line 180 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 |
Instance Attribute Details
#length ⇒ Object (readonly)
The length of this location in bytes.
176 177 178 |
# File 'lib/prism/parse_result.rb', line 176 def length @length end |
#start_offset ⇒ Object (readonly)
The byte offset from the beginning of the source where this location starts.
173 174 175 |
# File 'lib/prism/parse_result.rb', line 173 def start_offset @start_offset end |
Instance Method Details
#==(other) ⇒ Object
Returns true if the given other location is equal to this location.
344 345 346 347 348 |
# File 'lib/prism/parse_result.rb', line 344 def ==(other) Location === other && other.start_offset == start_offset && other.end_offset == end_offset end |
#adjoin(string) ⇒ Object
Join this location with the first occurrence of the string in the source that occurs after this location on the same line, and return the new location. This will raise an error if the string does not exist.
363 364 365 366 367 368 369 370 |
# File 'lib/prism/parse_result.rb', line 363 def adjoin(string) line_suffix = source.slice(end_offset, source.line_end(end_offset) - end_offset) line_suffix_index = line_suffix.byteindex(string) raise "Could not find #{string}" if line_suffix_index.nil? Location.new(source, start_offset, length + line_suffix_index + string.bytesize) end |
#chop ⇒ Object
Returns a new location that is the result of chopping off the last byte.
226 227 228 |
# File 'lib/prism/parse_result.rb', line 226 def chop copy(length: length == 0 ? length : length - 1) end |
#comments ⇒ Object
Returns all comments that are associated with this location (both leading and trailing comments).
216 217 218 |
# File 'lib/prism/parse_result.rb', line 216 def comments [*@leading_comments, *@trailing_comments] end |
#copy(source: self.source, start_offset: self.start_offset, length: self.length) ⇒ Object
Create a new location object with the given options.
221 222 223 |
# File 'lib/prism/parse_result.rb', line 221 def copy(source: self.source, start_offset: self.start_offset, length: self.length) Location.new(source, start_offset, length) end |
#deconstruct_keys(keys) ⇒ Object
Implement the hash pattern matching interface for Location.
334 335 336 |
# File 'lib/prism/parse_result.rb', line 334 def deconstruct_keys(keys) { start_offset: start_offset, end_offset: end_offset } end |
#end_character_column ⇒ Object
The column number in characters where this location ends from the start of the line.
323 324 325 |
# File 'lib/prism/parse_result.rb', line 323 def end_character_column source.character_column(end_offset) end |
#end_character_offset ⇒ Object
The character offset from the beginning of the source where this location ends.
272 273 274 |
# File 'lib/prism/parse_result.rb', line 272 def end_character_offset source.character_offset(end_offset) end |
#end_code_units_column(encoding = Encoding::UTF_16LE) ⇒ Object
The column number in code units of the given encoding where this location ends from the start of the line.
329 330 331 |
# File 'lib/prism/parse_result.rb', line 329 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) ⇒ Object
The offset from the start of the file in code units of the given encoding.
277 278 279 |
# File 'lib/prism/parse_result.rb', line 277 def end_code_units_offset(encoding = Encoding::UTF_16LE) source.code_units_offset(end_offset, encoding) end |
#end_column ⇒ Object
The column number in bytes where this location ends from the start of the line.
317 318 319 |
# File 'lib/prism/parse_result.rb', line 317 def end_column source.column(end_offset) end |
#end_line ⇒ Object
The line number where this location ends.
293 294 295 |
# File 'lib/prism/parse_result.rb', line 293 def end_line source.line(end_offset) end |
#end_offset ⇒ Object
The byte offset from the beginning of the source where this location ends.
266 267 268 |
# File 'lib/prism/parse_result.rb', line 266 def end_offset start_offset + length end |
#inspect ⇒ Object
Returns a string representation of this location.
231 232 233 |
# File 'lib/prism/parse_result.rb', line 231 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.
353 354 355 356 357 358 |
# File 'lib/prism/parse_result.rb', line 353 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) ⇒ Object
Attach a comment to the leading comments of this location.
199 200 201 |
# File 'lib/prism/parse_result.rb', line 199 def leading_comment(comment) leading_comments << comment end |
#leading_comments ⇒ Object
These are the comments that are associated with this location that exist before the start of this location.
194 195 196 |
# File 'lib/prism/parse_result.rb', line 194 def leading_comments @leading_comments ||= [] end |
#pretty_print(q) ⇒ Object
Implement the pretty print interface for Location.
339 340 341 |
# File 'lib/prism/parse_result.rb', line 339 def pretty_print(q) q.text("(#{start_line},#{start_column})-(#{end_line},#{end_column})") end |
#slice ⇒ Object
The source code that this location represents.
241 242 243 |
# File 'lib/prism/parse_result.rb', line 241 def slice source.slice(start_offset, length) end |
#slice_lines ⇒ Object
The source code that this location represents starting from the beginning of the line that this location starts on to the end of the line that this location ends on.
248 249 250 251 252 |
# File 'lib/prism/parse_result.rb', line 248 def slice_lines line_start = source.line_start(start_offset) line_end = source.line_end(end_offset) source.slice(line_start, line_end - line_start) end |
#source_lines ⇒ Object
Returns all of the lines of the source code associated with this location.
236 237 238 |
# File 'lib/prism/parse_result.rb', line 236 def source_lines source.lines end |
#start_character_column ⇒ Object
The column number in characters where this location ends from the start of the line.
305 306 307 |
# File 'lib/prism/parse_result.rb', line 305 def start_character_column source.character_column(start_offset) end |
#start_character_offset ⇒ Object
The character offset from the beginning of the source where this location starts.
256 257 258 |
# File 'lib/prism/parse_result.rb', line 256 def start_character_offset source.character_offset(start_offset) end |
#start_code_units_column(encoding = Encoding::UTF_16LE) ⇒ Object
The column number in code units of the given encoding where this location starts from the start of the line.
311 312 313 |
# File 'lib/prism/parse_result.rb', line 311 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) ⇒ Object
The offset from the start of the file in code units of the given encoding.
261 262 263 |
# File 'lib/prism/parse_result.rb', line 261 def start_code_units_offset(encoding = Encoding::UTF_16LE) source.code_units_offset(start_offset, encoding) end |
#start_column ⇒ Object
The column number in bytes where this location starts from the start of the line.
299 300 301 |
# File 'lib/prism/parse_result.rb', line 299 def start_column source.column(start_offset) end |
#start_line ⇒ Object
The line number where this location starts.
282 283 284 |
# File 'lib/prism/parse_result.rb', line 282 def start_line source.line(start_offset) end |
#start_line_slice ⇒ Object
The content of the line where this location starts before this location.
287 288 289 290 |
# File 'lib/prism/parse_result.rb', line 287 def start_line_slice offset = source.line_start(start_offset) source.slice(offset, start_offset - offset) end |
#trailing_comment(comment) ⇒ Object
Attach a comment to the trailing comments of this location.
210 211 212 |
# File 'lib/prism/parse_result.rb', line 210 def trailing_comment(comment) trailing_comments << comment end |
#trailing_comments ⇒ Object
These are the comments that are associated with this location that exist after the end of this location.
205 206 207 |
# File 'lib/prism/parse_result.rb', line 205 def trailing_comments @trailing_comments ||= [] end |