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[R]

The line number where this source starts.

Public Class Methods

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

Create a new source object with the given source code.

# File lib/prism/parse_result.rb, line 18
def initialize(source, start_line = 1, offsets = [])
  @source = source
  @start_line = start_line # set after parsing is done
  @offsets = offsets # set after parsing is done
end

Public Instance Methods

character_column(byte_offset) click to toggle source

Return the column number in characters for the given byte offset.

# File lib/prism/parse_result.rb, line 59
def character_column(byte_offset)
  character_offset(byte_offset) - character_offset(line_start(byte_offset))
end
character_offset(byte_offset) click to toggle source

Return the character offset for the given byte offset.

# File lib/prism/parse_result.rb, line 54
def character_offset(byte_offset)
  (source.byteslice(0, byte_offset) or raise).length
end
code_units_column(byte_offset, encoding) click to toggle source

Returns the column number in code units for the given encoding for the given byte offset.

# File lib/prism/parse_result.rb, line 76
def code_units_column(byte_offset, encoding)
  code_units_offset(byte_offset, encoding) - code_units_offset(line_start(byte_offset), encoding)
end
code_units_offset(byte_offset, encoding) click to toggle source

Returns the offset from the start of the file for the given byte offset counting in code units for the given encoding.

This method is tested with UTF-8, UTF-16, and UTF-32. If there is the concept of code units that differs from the number of characters in other encodings, it is not captured here.

# File lib/prism/parse_result.rb, line 69
def code_units_offset(byte_offset, encoding)
  byteslice = (source.byteslice(0, byte_offset) or raise).encode(encoding)
  (encoding == Encoding::UTF_16LE || encoding == Encoding::UTF_16BE) ? (byteslice.bytesize / 2) : byteslice.length
end
column(byte_offset) click to toggle source

Return the column number for the given byte offset.

# File lib/prism/parse_result.rb, line 49
def column(byte_offset)
  byte_offset - line_start(byte_offset)
end
encoding() click to toggle source

Returns the encoding of the source code, which is set by parameters to the parser or by the encoding magic comment.

# File lib/prism/parse_result.rb, line 26
def encoding
  source.encoding
end
line(byte_offset) click to toggle source

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

# File lib/prism/parse_result.rb, line 38
def line(byte_offset)
  start_line + find_line(byte_offset)
end
line_start(byte_offset) click to toggle source

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

# File lib/prism/parse_result.rb, line 44
def line_start(byte_offset)
  offsets[find_line(byte_offset)]
end
slice(byte_offset, length) click to toggle source

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

# File lib/prism/parse_result.rb, line 32
def slice(byte_offset, length)
  source.byteslice(byte_offset, length) or raise
end

Private Instance Methods

find_line(byte_offset) click to toggle source

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

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

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

    if offset < byte_offset
      left = mid + 1
    else
      right = mid - 1
    end
  end

  left - 1
end