class Prism::Source

This represents a source of Ruby code that has been parsed. It is used in conjunction with locations to allow them to resolve line numbers and source ranges.

Attributes

offsets[R]

The list of newline byte offsets in the source code.

source[R]

The source code that this source object represents.

start_line[RW]

The line number where this source starts.

Public Class Methods

new(source, start_line = 1, offsets = compute_offsets(source)) click to toggle source

Create a new source object with the given source code and newline byte offsets. If no newline byte offsets are given, they will be computed from the source code.

# File prism/parse_result.rb, line 20
def initialize(source, start_line = 1, offsets = compute_offsets(source))
  @source = source
  @start_line = start_line
  @offsets = offsets
end

Public Instance Methods

column(value) click to toggle source

Return the column number for the given byte offset.

# File prism/parse_result.rb, line 45
def column(value)
  value - offsets[find_line(value)]
end
line(value) click to toggle source

Binary search through the offsets to find the line number for the given byte offset.

# File prism/parse_result.rb, line 34
def line(value)
  start_line + find_line(value)
end
line_offset(value) click to toggle source

Return the byte offset of the start of the line corresponding to the given byte offset.

# File prism/parse_result.rb, line 40
def line_offset(value)
  offsets[find_line(value)]
end
slice(offset, length) click to toggle source

Perform a byteslice on the source code using the given byte offset and byte length.

# File prism/parse_result.rb, line 28
def slice(offset, length)
  source.byteslice(offset, length)
end

Private Instance Methods

compute_offsets(code) click to toggle source

Find all of the newlines in the source code and return their byte offsets from the start of the string an array.

# File prism/parse_result.rb, line 73
def compute_offsets(code)
  offsets = [0]
  code.b.scan("\n") { offsets << $~.end(0) }
  offsets
end
find_line(value) click to toggle source

Binary search through the offsets to find the line number for the given byte offset.

# File prism/parse_result.rb, line 53
def find_line(value)
  left = 0
  right = offsets.length - 1

  while left <= right
    mid = left + (right - left) / 2
    return mid if offsets[mid] == value

    if offsets[mid] < value
      left = mid + 1
    else
      right = mid - 1
    end
  end

  left - 1
end