Class: Prism::Location

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

Overview

This represents a location in the source.

Instance Attribute 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.



302
303
304
305
306
307
308
309
310
311
312
# File 'lib/prism/parse_result.rb', line 302

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

#lengthObject (readonly)

The length of this location in bytes.



298
299
300
# File 'lib/prism/parse_result.rb', line 298

def length
  @length
end

#start_offsetObject (readonly)

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



295
296
297
# File 'lib/prism/parse_result.rb', line 295

def start_offset
  @start_offset
end

Instance Method Details

#==(other) ⇒ Object

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



490
491
492
493
494
# File 'lib/prism/parse_result.rb', line 490

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.



509
510
511
512
513
514
515
516
# File 'lib/prism/parse_result.rb', line 509

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

#cached_end_code_units_column(cache) ⇒ Object

The end column in code units using the given cache to fetch or calculate the value.



475
476
477
# File 'lib/prism/parse_result.rb', line 475

def cached_end_code_units_column(cache)
  cache[end_offset] - cache[source.line_start(end_offset)]
end

#cached_end_code_units_offset(cache) ⇒ Object

The end offset from the start of the file in code units using the given cache to fetch or calculate the value.



411
412
413
# File 'lib/prism/parse_result.rb', line 411

def cached_end_code_units_offset(cache)
  cache[end_offset]
end

#cached_start_code_units_column(cache) ⇒ Object

The start column in code units using the given cache to fetch or calculate the value.



451
452
453
# File 'lib/prism/parse_result.rb', line 451

def cached_start_code_units_column(cache)
  cache[start_offset] - cache[source.line_start(start_offset)]
end

#cached_start_code_units_offset(cache) ⇒ Object

The start offset from the start of the file in code units using the given cache to fetch or calculate the value.



389
390
391
# File 'lib/prism/parse_result.rb', line 389

def cached_start_code_units_offset(cache)
  cache[start_offset]
end

#chopObject

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



348
349
350
# File 'lib/prism/parse_result.rb', line 348

def chop
  copy(length: length == 0 ? length : length - 1)
end

#commentsObject

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



338
339
340
# File 'lib/prism/parse_result.rb', line 338

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.



343
344
345
# File 'lib/prism/parse_result.rb', line 343

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.



480
481
482
# File 'lib/prism/parse_result.rb', line 480

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.



463
464
465
# File 'lib/prism/parse_result.rb', line 463

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.



400
401
402
# File 'lib/prism/parse_result.rb', line 400

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.



469
470
471
# File 'lib/prism/parse_result.rb', line 469

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.



405
406
407
# File 'lib/prism/parse_result.rb', line 405

def end_code_units_offset(encoding = Encoding::UTF_16LE)
  source.code_units_offset(end_offset, encoding)
end

#end_columnObject

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



457
458
459
# File 'lib/prism/parse_result.rb', line 457

def end_column
  source.column(end_offset)
end

#end_lineObject

The line number where this location ends.



427
428
429
# File 'lib/prism/parse_result.rb', line 427

def end_line
  source.line(end_offset)
end

#end_offsetObject

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



394
395
396
# File 'lib/prism/parse_result.rb', line 394

def end_offset
  start_offset + length
end

#inspectObject

Returns a string representation of this location.



353
354
355
# File 'lib/prism/parse_result.rb', line 353

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.



499
500
501
502
503
504
# File 'lib/prism/parse_result.rb', line 499

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.



321
322
323
# File 'lib/prism/parse_result.rb', line 321

def leading_comment(comment)
  leading_comments << comment
end

#leading_commentsObject

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



316
317
318
# File 'lib/prism/parse_result.rb', line 316

def leading_comments
  @leading_comments ||= []
end

#pretty_print(q) ⇒ Object

Implement the pretty print interface for Location.



485
486
487
# File 'lib/prism/parse_result.rb', line 485

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

#sliceObject

The source code that this location represents.



363
364
365
# File 'lib/prism/parse_result.rb', line 363

def slice
  source.slice(start_offset, length)
end

#slice_linesObject

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.



370
371
372
373
374
# File 'lib/prism/parse_result.rb', line 370

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_linesObject

Returns all of the lines of the source code associated with this location.



358
359
360
# File 'lib/prism/parse_result.rb', line 358

def source_lines
  source.lines
end

#start_character_columnObject

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



439
440
441
# File 'lib/prism/parse_result.rb', line 439

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.



378
379
380
# File 'lib/prism/parse_result.rb', line 378

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.



445
446
447
# File 'lib/prism/parse_result.rb', line 445

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.



383
384
385
# File 'lib/prism/parse_result.rb', line 383

def start_code_units_offset(encoding = Encoding::UTF_16LE)
  source.code_units_offset(start_offset, encoding)
end

#start_columnObject

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



433
434
435
# File 'lib/prism/parse_result.rb', line 433

def start_column
  source.column(start_offset)
end

#start_lineObject

The line number where this location starts.



416
417
418
# File 'lib/prism/parse_result.rb', line 416

def start_line
  source.line(start_offset)
end

#start_line_sliceObject

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



421
422
423
424
# File 'lib/prism/parse_result.rb', line 421

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.



332
333
334
# File 'lib/prism/parse_result.rb', line 332

def trailing_comment(comment)
  trailing_comments << comment
end

#trailing_commentsObject

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



327
328
329
# File 'lib/prism/parse_result.rb', line 327

def trailing_comments
  @trailing_comments ||= []
end