Class: Prism::Source
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Inherits: | Object |
Defined in: | lib/prism/parse_result.rb |
Overview
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.
Class Method Summary
-
.for(source, start_line = 1, offsets = [])
Create a new source object with the given source code.
-
.new(source, start_line = 1, offsets = []) ⇒ Source
constructor
Create a new source object with the given source code.
Instance Attribute Summary
-
#offsets
readonly
The list of newline byte offsets in the source code.
-
#source
readonly
The source code that this source object represents.
-
#start_line
readonly
The line number where this source starts.
Instance Method Summary
-
#character_column(byte_offset)
Return the column number in characters for the given byte offset.
-
#character_offset(byte_offset)
Return the character offset for the given byte offset.
-
#code_units_column(byte_offset, encoding)
Returns the column number in code units for the given encoding for the given byte offset.
-
#code_units_offset(byte_offset, encoding)
Returns the offset from the start of the file for the given byte offset counting in code units for the given encoding.
-
#column(byte_offset)
Return the column number for the given byte offset.
-
#encoding
Returns the encoding of the source code, which is set by parameters to the parser or by the encoding magic comment.
-
#line(byte_offset)
Binary search through the offsets to find the line number for the given byte offset.
-
#line_end(byte_offset)
Returns the byte offset of the end of the line corresponding to the given byte offset.
-
#line_start(byte_offset)
Return the byte offset of the start of the line corresponding to the given byte offset.
-
#lines
Returns the lines of the source code as an array of strings.
-
#slice(byte_offset, length)
Perform a byteslice on the source code using the given byte offset and byte length.
-
#find_line(byte_offset)
private
Binary search through the offsets to find the line number for the given byte offset.
Constructor Details
.new(source, start_line = 1, offsets = []) ⇒ Source
Create a new source object with the given source code.
# File 'lib/prism/parse_result.rb', line 26
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
Class Method Details
.for(source, start_line = 1, offsets = [])
Create a new source object with the given source code. This method should be used instead of .new and it will return either a Source
or a specialized and more performant ASCIISource
if no multibyte characters are present in the source code.
# File 'lib/prism/parse_result.rb', line 12
def self.for(source, start_line = 1, offsets = []) source.ascii_only? ? ASCIISource.new(source, start_line, offsets): new(source, start_line, offsets) end
Instance Attribute Details
#offsets (readonly)
The list of newline byte offsets in the source code.
# File 'lib/prism/parse_result.rb', line 23
attr_reader :offsets
#source (readonly)
The source code that this source object represents.
# File 'lib/prism/parse_result.rb', line 17
attr_reader :source
#start_line (readonly)
The line number where this source starts.
# File 'lib/prism/parse_result.rb', line 20
attr_reader :start_line
Instance Method Details
#character_column(byte_offset)
Return the column number in characters for the given byte offset.
# File 'lib/prism/parse_result.rb', line 78
def character_column(byte_offset) character_offset(byte_offset) - character_offset(line_start(byte_offset)) end
#character_offset(byte_offset)
Return the character offset for the given byte offset.
# File 'lib/prism/parse_result.rb', line 73
def character_offset(byte_offset) (source.byteslice(0, byte_offset) or raise).length end
#code_units_column(byte_offset, encoding)
Returns the column number in code units for the given encoding for the given byte offset.
# File 'lib/prism/parse_result.rb', line 95
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)
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.
#column(byte_offset)
Return the column number for the given byte offset.
# File 'lib/prism/parse_result.rb', line 68
def column(byte_offset) byte_offset - line_start(byte_offset) end
#encoding
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 34
def encoding source.encoding end
#find_line(byte_offset) (private)
Binary search through the offsets to find the line number for the given byte offset.
#line(byte_offset)
Binary search through the offsets to find the line number for the given byte offset.
# File 'lib/prism/parse_result.rb', line 51
def line(byte_offset) start_line + find_line(byte_offset) end
#line_end(byte_offset)
Returns the byte offset of the end of the line corresponding to the given byte offset.
#line_start(byte_offset)
Return the byte offset of the start of the line corresponding to the given byte offset.
#lines
Returns the lines of the source code as an array of strings.
# File 'lib/prism/parse_result.rb', line 39
def lines source.lines end
#slice(byte_offset, length)
Perform a byteslice on the source code using the given byte offset and byte length.
# File 'lib/prism/parse_result.rb', line 45
def slice(byte_offset, length) source.byteslice(byte_offset, length) or raise end