Class: Prism::Location
- Inherits:
-
Object
- Object
- Prism::Location
- Defined in:
- lib/prism/parse_result.rb,
ext/prism/extension.c
Overview
This represents a location in the source.
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
The list of comments attached to this location.
-
#length ⇒ Object
readonly
The length of this location in bytes.
-
#start_offset ⇒ Object
readonly
The byte offset from the beginning of the source where this location starts.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#copy(**options) ⇒ Object
Create a new location object with the given options.
- #deconstruct_keys(keys) ⇒ Object
-
#end_column ⇒ Object
The column number in bytes where this location ends from the start of the line.
-
#end_line ⇒ Object
The line number where this location ends.
-
#end_offset ⇒ Object
The byte offset from the beginning of the source where this location ends.
-
#initialize(source, start_offset, length) ⇒ Location
constructor
A new instance of Location.
-
#inspect ⇒ Object
Returns a string representation of this location.
-
#join(other) ⇒ Object
Returns a new location that stretches from this location to the given other location.
- #pretty_print(q) ⇒ Object
-
#slice ⇒ Object
The source code that this location represents.
-
#start_column ⇒ Object
The column number in bytes where this location starts from the start of the line.
-
#start_line ⇒ Object
The line number where this location starts.
-
#start_line_slice ⇒ Object
The content of the line where this location starts before this location.
Constructor Details
#initialize(source, start_offset, length) ⇒ Location
Returns a new instance of Location.
56 57 58 59 60 61 |
# File 'lib/prism/parse_result.rb', line 56 def initialize(source, start_offset, length) @source = source @start_offset = start_offset @length = length @comments = [] end |
Instance Attribute Details
#comments ⇒ Object (readonly)
The list of comments attached to this location
54 55 56 |
# File 'lib/prism/parse_result.rb', line 54 def comments @comments end |
#length ⇒ Object (readonly)
The length of this location in bytes.
51 52 53 |
# File 'lib/prism/parse_result.rb', line 51 def length @length end |
#start_offset ⇒ Object (readonly)
The byte offset from the beginning of the source where this location starts.
48 49 50 |
# File 'lib/prism/parse_result.rb', line 48 def start_offset @start_offset end |
Class Method Details
.null ⇒ Object
139 140 141 |
# File 'lib/prism/parse_result.rb', line 139 def self.null new(0, 0) end |
Instance Method Details
#==(other) ⇒ Object
123 124 125 126 127 |
# File 'lib/prism/parse_result.rb', line 123 def ==(other) other.is_a?(Location) && other.start_offset == start_offset && other.end_offset == end_offset end |
#copy(**options) ⇒ Object
Create a new location object with the given options.
64 65 66 67 68 69 70 |
# File 'lib/prism/parse_result.rb', line 64 def copy(**) Location.new( .fetch(:source) { source }, .fetch(:start_offset) { start_offset }, .fetch(:length) { length } ) end |
#deconstruct_keys(keys) ⇒ Object
115 116 117 |
# File 'lib/prism/parse_result.rb', line 115 def deconstruct_keys(keys) { start_offset: start_offset, end_offset: end_offset } end |
#end_column ⇒ Object
The column number in bytes where this location ends from the start of the line.
111 112 113 |
# File 'lib/prism/parse_result.rb', line 111 def end_column source.column(end_offset) end |
#end_line ⇒ Object
The line number where this location ends.
99 100 101 |
# File 'lib/prism/parse_result.rb', line 99 def end_line source.line(end_offset - 1) end |
#end_offset ⇒ Object
The byte offset from the beginning of the source where this location ends.
83 84 85 |
# File 'lib/prism/parse_result.rb', line 83 def end_offset start_offset + length end |
#inspect ⇒ Object
Returns a string representation of this location.
73 74 75 |
# File 'lib/prism/parse_result.rb', line 73 def inspect "#<Prism::Location @start_offset=#{@start_offset} @length=#{@length} start_line=#{start_line}>" end |
#join(other) ⇒ Object
Returns a new location that stretches from this location to the given other location. Raises an error if this location is not before the other location or if they don’t share the same source.
132 133 134 135 136 137 |
# File 'lib/prism/parse_result.rb', line 132 def join(other) raise "Incompatible sources" if source != other.source raise "Incompatible locations" if start_offset > other.start_offset Location.new(source, start_offset, other.end_offset - start_offset) end |
#pretty_print(q) ⇒ Object
119 120 121 |
# File 'lib/prism/parse_result.rb', line 119 def pretty_print(q) q.text("(#{start_line},#{start_column})-(#{end_line},#{end_column}))") end |
#slice ⇒ Object
The source code that this location represents.
78 79 80 |
# File 'lib/prism/parse_result.rb', line 78 def slice source.slice(start_offset, length) end |
#start_column ⇒ Object
The column number in bytes where this location starts from the start of the line.
105 106 107 |
# File 'lib/prism/parse_result.rb', line 105 def start_column source.column(start_offset) end |
#start_line ⇒ Object
The line number where this location starts.
88 89 90 |
# File 'lib/prism/parse_result.rb', line 88 def start_line source.line(start_offset) end |
#start_line_slice ⇒ Object
The content of the line where this location starts before this location.
93 94 95 96 |
# File 'lib/prism/parse_result.rb', line 93 def start_line_slice offset = source.line_offset(start_offset) source.slice(offset, start_offset - offset) end |