123456789_123456789_123456789_123456789_123456789_

Class: Prism::Location

Relationships & Source Files
Inherits: Object
Defined in: lib/prism/parse_result.rb,
prism/extension.c

Overview

This represents a location in the source.

Class Method Summary

Instance Attribute Summary

  • #comments readonly

    The list of comments attached to this location.

  • #length readonly

    The length of this location in bytes.

  • #source readonly
  • #start_offset readonly

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

Instance Method Summary

  • #==(other)

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

  • #copy(**options)

    Create a new location object with the given options.

  • #deconstruct_keys(keys)

    Implement the hash pattern matching interface for Location.

  • #end_character_column

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

  • #end_character_offset

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

  • #end_column

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

  • #end_line

    The line number where this location ends.

  • #end_offset

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

  • #inspect

    Returns a string representation of this location.

  • #join(other)

    Returns a new location that stretches from this location to the given other location.

  • #pretty_print(q)

    Implement the pretty print interface for Location.

  • #slice

    The source code that this location represents.

  • #start_character_column

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

  • #start_character_offset

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

  • #start_column

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

  • #start_line

    The line number where this location starts.

  • #start_line_slice

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

Constructor Details

.new(source, start_offset, length) ⇒ Location

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

[ GitHub ]

  
# File 'lib/prism/parse_result.rb', line 108

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

Class Method Details

.null

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.

[ GitHub ]

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

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

Instance Attribute Details

#comments (readonly)

The list of comments attached to this location

[ GitHub ]

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

attr_reader :comments

#length (readonly)

The length of this location in bytes.

[ GitHub ]

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

attr_reader :length

#source (readonly)

[ GitHub ]

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

protected attr_reader :source

#start_offset (readonly)

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

[ GitHub ]

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

attr_reader :start_offset

Instance Method Details

#==(other)

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

[ GitHub ]

  
# 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)

Create a new location object with the given options.

[ GitHub ]

  
# 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)

Implement the hash pattern matching interface for Location.

[ GitHub ]

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

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

#end_character_column

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

[ GitHub ]

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

def end_character_column
  source.character_column(end_offset)
end

#end_character_offset

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

[ GitHub ]

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

def end_character_offset
  source.character_offset(end_offset)
end

#end_column

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

[ GitHub ]

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

def end_column
  source.column(end_offset)
end

#end_line

The line number where this location ends.

[ GitHub ]

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

def end_line
  source.line(end_offset)
end

#end_offset

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

[ GitHub ]

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

def end_offset
  start_offset + length
end

#inspect

Returns a string representation of this location.

[ GitHub ]

  
# 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)

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.

[ GitHub ]

  
# 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)

Implement the pretty print interface for Location.

[ GitHub ]

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

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

#slice

The source code that this location represents.

[ GitHub ]

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

def slice
  source.slice(start_offset, length)
end

#start_character_column

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

[ GitHub ]

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

def start_character_column
  source.character_column(start_offset)
end

#start_character_offset

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

[ GitHub ]

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

def start_character_offset
  source.character_offset(start_offset)
end

#start_column

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

[ GitHub ]

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

def start_column
  source.column(start_offset)
end

#start_line

The line number where this location starts.

[ GitHub ]

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

def start_line
  source.line(start_offset)
end

#start_line_slice

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

[ GitHub ]

  
# 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