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
The list of newline byte offsets in the source code.
The source code that this source object represents.
The line number where this source starts.
Public Class Methods
Source
# File lib/prism/parse_result.rb, line 13defself.for(source,start_line =1,offsets = [])ifsource.ascii_only?ASCIISource.new(source,start_line,offsets)elsifsource.encoding==Encoding::BINARYsource.force_encoding(Encoding::UTF_8)ifsource.valid_encoding?new(source,start_line,offsets)else# This is an extremely niche use case where the file is marked as# binary, contains multi-byte characters, and those characters are not# valid UTF-8. In this case we'll mark it as binary and fall back to# treating everything as a single-byte character. This _may_ cause# problems when asking for code units, but it appears to be the# cleanest solution at the moment.source.force_encoding(Encoding::BINARY)ASCIISource.new(source,start_line,offsets)endelsenew(source,start_line,offsets)endend
Create a new source object with the given source code. This method should be used instead ofnew and it will return either aSource or a specialized and more performantASCIISource if no multibyte characters are present in the source code.
Source
# File lib/prism/parse_result.rb, line 46definitialize(source,start_line =1,offsets = [])@source =source@start_line =start_line# set after parsing is done@offsets =offsets# set after parsing is doneend
Create a new source object with the given source code.
Public Instance Methods
Source
# File lib/prism/parse_result.rb, line 108defcharacter_column(byte_offset)character_offset(byte_offset)-character_offset(line_start(byte_offset))end
Return the column number in characters for the given byte offset.
Source
# File lib/prism/parse_result.rb, line 103defcharacter_offset(byte_offset) (source.byteslice(0,byte_offset)orraise).lengthend
Return the character offset for the given byte offset.
Source
# File lib/prism/parse_result.rb, line 136defcode_units_cache(encoding)CodeUnitsCache.new(source,encoding)end
Generate a cache that targets a specific encoding for calculating code unit offsets.
Source
# File lib/prism/parse_result.rb, line 142defcode_units_column(byte_offset,encoding)code_units_offset(byte_offset,encoding)-code_units_offset(line_start(byte_offset),encoding)end
Returns the column number in code units for the given encoding for the given byte offset.
Source
# File lib/prism/parse_result.rb, line 124defcode_units_offset(byte_offset,encoding)byteslice = (source.byteslice(0,byte_offset)orraise).encode(encoding,invalid::replace,undef::replace)ifencoding==Encoding::UTF_16LE||encoding==Encoding::UTF_16BEbyteslice.bytesize/2elsebyteslice.lengthendend
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.
We purposefully replace invalid and undefined characters with replacement characters in this conversion. This happens for two reasons. First, it’s possible that the given byte offset will not occur on a character boundary. Second, it’s possible that the source code will contain a character that has no equivalent in the given encoding.
Source
# File lib/prism/parse_result.rb, line 98defcolumn(byte_offset)byte_offset-line_start(byte_offset)end
Return the column number for the given byte offset.
Source
# File lib/prism/parse_result.rb, line 147defdeep_freezesource.freezeoffsets.freezefreezeend
Freeze this object and the objects it contains.
Source
# File lib/prism/parse_result.rb, line 64defencodingsource.encodingend
Returns the encoding of the source code, which is set by parameters to the parser or by the encoding magic comment.
Source
# File lib/prism/parse_result.rb, line 81defline(byte_offset)start_line+find_line(byte_offset)end
Binary search through the offsets to find the line number for the given byte offset.
Source
# File lib/prism/parse_result.rb, line 93defline_end(byte_offset)offsets[find_line(byte_offset)+1]||source.bytesizeend
Returns the byte offset of the end of the line corresponding to the given byte offset.
Source
# File lib/prism/parse_result.rb, line 87defline_start(byte_offset)offsets[find_line(byte_offset)]end
Return the byte offset of the start of the line corresponding to the given byte offset.
Source
# File lib/prism/parse_result.rb, line 69deflinessource.linesend
Returns the lines of the source code as an array of strings.
Source
# File lib/prism/parse_result.rb, line 58defreplace_offsets(offsets)@offsets.replace(offsets)end
Replace the value of offsets with the given value.
Source
# File lib/prism/parse_result.rb, line 53defreplace_start_line(start_line)@start_line =start_lineend
Replace the value ofstart_line with the given value.
Source
# File lib/prism/parse_result.rb, line 75defslice(byte_offset,length)source.byteslice(byte_offset,length)orraiseend
Perform a byteslice on the source code using the given byte offset and byte length.
Private Instance Methods
Source
# File lib/prism/parse_result.rb, line 157deffind_line(byte_offset)left =0right =offsets.length-1whileleft<=rightmid =left+ (right-left)/2returnmidif (offset =offsets[mid])==byte_offsetifoffset<byte_offsetleft =mid+1elseright =mid-1endendleft-1end
Binary search through the offsets to find the line number for the given byte offset.