Class: Prism::Source
- Inherits:
-
Object
- Object
- Prism::Source
- Defined in:
- lib/prism/parse_result.rb,
ext/prism/extension.c
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.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#offsets ⇒ Object
readonly
The list of newline byte offsets in the source code.
-
#source ⇒ Object
readonly
The source code that this source object represents.
-
#start_line ⇒ Object
readonly
The line number where this source starts.
Class Method Summary collapse
-
.for(source, start_line = 1, offsets = []) ⇒ Object
Create a new source object with the given source code.
Instance Method Summary collapse
-
#character_column(byte_offset) ⇒ Object
Return the column number in characters for the given byte offset.
-
#character_offset(byte_offset) ⇒ Object
Return the character offset for the given byte offset.
-
#code_units_column(byte_offset, encoding) ⇒ Object
Returns the column number in code units for the given encoding for the given byte offset.
-
#code_units_offset(byte_offset, encoding) ⇒ Object
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) ⇒ Object
Return the column number for the given byte offset.
-
#encoding ⇒ Object
Returns the encoding of the source code, which is set by parameters to the parser or by the encoding magic comment.
-
#initialize(source, start_line = 1, offsets = []) ⇒ Source
constructor
Create a new source object with the given source code.
-
#line(byte_offset) ⇒ Object
Binary search through the offsets to find the line number for the given byte offset.
-
#line_end(byte_offset) ⇒ Object
Returns the byte offset of the end of the line corresponding to the given byte offset.
-
#line_start(byte_offset) ⇒ Object
Return the byte offset of the start of the line corresponding to the given byte offset.
-
#lines ⇒ Object
Returns the lines of the source code as an array of strings.
-
#slice(byte_offset, length) ⇒ Object
Perform a byteslice on the source code using the given byte offset and byte length.
Constructor Details
#initialize(source, start_line = 1, offsets = []) ⇒ Source
Create a new source object with the given source code.
30 31 32 33 34 |
# File 'lib/prism/parse_result.rb', line 30 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 |
Instance Attribute Details
#offsets ⇒ Object (readonly)
The list of newline byte offsets in the source code.
27 28 29 |
# File 'lib/prism/parse_result.rb', line 27 def offsets @offsets end |
#source ⇒ Object (readonly)
The source code that this source object represents.
21 22 23 |
# File 'lib/prism/parse_result.rb', line 21 def source @source end |
#start_line ⇒ Object (readonly)
The line number where this source starts.
24 25 26 |
# File 'lib/prism/parse_result.rb', line 24 def start_line @start_line end |
Class Method Details
.for(source, start_line = 1, offsets = []) ⇒ Object
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.
12 13 14 15 16 17 18 |
# File 'lib/prism/parse_result.rb', line 12 def self.for(source, start_line = 1, offsets = []) if source.ascii_only? ASCIISource.new(source, start_line, offsets) else new(source, start_line, offsets) end end |
Instance Method Details
#character_column(byte_offset) ⇒ Object
Return the column number in characters for the given byte offset.
82 83 84 |
# File 'lib/prism/parse_result.rb', line 82 def character_column(byte_offset) character_offset(byte_offset) - character_offset(line_start(byte_offset)) end |
#character_offset(byte_offset) ⇒ Object
Return the character offset for the given byte offset.
77 78 79 |
# File 'lib/prism/parse_result.rb', line 77 def character_offset(byte_offset) (source.byteslice(0, byte_offset) or raise).length end |
#code_units_column(byte_offset, encoding) ⇒ Object
Returns the column number in code units for the given encoding for the given byte offset.
104 105 106 |
# File 'lib/prism/parse_result.rb', line 104 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) ⇒ Object
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.
92 93 94 95 96 97 98 99 100 |
# File 'lib/prism/parse_result.rb', line 92 def code_units_offset(byte_offset, encoding) byteslice = (source.byteslice(0, byte_offset) or raise).encode(encoding) if encoding == Encoding::UTF_16LE || encoding == Encoding::UTF_16BE byteslice.bytesize / 2 else byteslice.length end end |
#column(byte_offset) ⇒ Object
Return the column number for the given byte offset.
72 73 74 |
# File 'lib/prism/parse_result.rb', line 72 def column(byte_offset) byte_offset - line_start(byte_offset) end |
#encoding ⇒ Object
Returns the encoding of the source code, which is set by parameters to the parser or by the encoding magic comment.
38 39 40 |
# File 'lib/prism/parse_result.rb', line 38 def encoding source.encoding end |
#line(byte_offset) ⇒ Object
Binary search through the offsets to find the line number for the given byte offset.
55 56 57 |
# File 'lib/prism/parse_result.rb', line 55 def line(byte_offset) start_line + find_line(byte_offset) end |
#line_end(byte_offset) ⇒ Object
Returns the byte offset of the end of the line corresponding to the given byte offset.
67 68 69 |
# File 'lib/prism/parse_result.rb', line 67 def line_end(byte_offset) offsets[find_line(byte_offset) + 1] || source.bytesize end |
#line_start(byte_offset) ⇒ Object
Return the byte offset of the start of the line corresponding to the given byte offset.
61 62 63 |
# File 'lib/prism/parse_result.rb', line 61 def line_start(byte_offset) offsets[find_line(byte_offset)] end |
#lines ⇒ Object
Returns the lines of the source code as an array of strings.
43 44 45 |
# File 'lib/prism/parse_result.rb', line 43 def lines source.lines end |
#slice(byte_offset, length) ⇒ Object
Perform a byteslice on the source code using the given byte offset and byte length.
49 50 51 |
# File 'lib/prism/parse_result.rb', line 49 def slice(byte_offset, length) source.byteslice(byte_offset, length) or raise end |