Class: BioSyntax::Span
- Inherits:
-
Object
- Object
- BioSyntax::Span
- Defined in:
- lib/biosyntax.rb
Overview
A highlighted byte range within one input line.
Offsets are byte offsets into the original line, not character indexes. This matches the native C API and keeps slicing correct for arbitrary encodings.
Instance Attribute Summary collapse
- #kind_id ⇒ Integer readonly
- #length ⇒ Integer readonly
- #start ⇒ Integer readonly
Instance Method Summary collapse
- #==(other) ⇒ Boolean (also: #eql?)
-
#deconstruct ⇒ Array(Integer, Integer, Symbol)
Pattern matching support.
-
#end ⇒ Integer
Byte offset just after the span.
- #hash ⇒ Integer
-
#initialize(start, length, kind_id) ⇒ Span
constructor
private
A new instance of Span.
- #inspect ⇒ String
-
#kind ⇒ Kind
Token kind metadata for this span.
-
#kind_name ⇒ Symbol
Token kind name.
-
#range ⇒ Range<Integer>
Byte range covered by this span.
-
#scope ⇒ String
Semantic scope for this span.
-
#to_a ⇒ Array(Integer, Integer, Symbol)
Start offset, end offset, and kind name.
-
#to_h ⇒ Hash
Serializable metadata for this span.
Constructor Details
#initialize(start, length, kind_id) ⇒ Span
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Span.
193 194 195 196 197 198 |
# File 'lib/biosyntax.rb', line 193 def initialize(start, length, kind_id) @start = Integer(start) @length = Integer(length) @kind_id = Integer(kind_id) freeze end |
Instance Attribute Details
#kind_id ⇒ Integer (readonly)
190 191 192 |
# File 'lib/biosyntax.rb', line 190 def kind_id @kind_id end |
#length ⇒ Integer (readonly)
190 191 192 |
# File 'lib/biosyntax.rb', line 190 def length @length end |
#start ⇒ Integer (readonly)
190 191 192 |
# File 'lib/biosyntax.rb', line 190 def start @start end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
251 252 253 254 255 256 |
# File 'lib/biosyntax.rb', line 251 def ==(other) other.is_a?(Span) && other.start == @start && other.length == @length && other.kind_id == @kind_id end |
#deconstruct ⇒ Array(Integer, Integer, Symbol)
Pattern matching support.
233 234 235 |
# File 'lib/biosyntax.rb', line 233 def deconstruct to_a end |
#end ⇒ Integer
Returns byte offset just after the span.
201 202 203 |
# File 'lib/biosyntax.rb', line 201 def end @start + @length end |
#hash ⇒ Integer
260 261 262 |
# File 'lib/biosyntax.rb', line 260 def hash [self.class, @start, @length, @kind_id].hash end |
#inspect ⇒ String
265 266 267 |
# File 'lib/biosyntax.rb', line 265 def inspect "#<#{self.class} start=#{@start} end=#{self.end} kind=#{kind.name.inspect}>" end |
#kind ⇒ Kind
Returns token kind metadata for this span.
206 207 208 |
# File 'lib/biosyntax.rb', line 206 def kind BioSyntax.kind(@kind_id) end |
#kind_name ⇒ Symbol
Returns token kind name.
211 212 213 |
# File 'lib/biosyntax.rb', line 211 def kind_name kind.name end |
#range ⇒ Range<Integer>
Returns byte range covered by this span.
221 222 223 |
# File 'lib/biosyntax.rb', line 221 def range @start...self.end end |
#scope ⇒ String
Returns semantic scope for this span.
216 217 218 |
# File 'lib/biosyntax.rb', line 216 def scope kind.scope end |
#to_a ⇒ Array(Integer, Integer, Symbol)
Returns start offset, end offset, and kind name.
226 227 228 |
# File 'lib/biosyntax.rb', line 226 def to_a [@start, self.end, kind.name] end |
#to_h ⇒ Hash
Returns serializable metadata for this span.
238 239 240 241 242 243 244 245 246 247 |
# File 'lib/biosyntax.rb', line 238 def to_h { start: @start, end: self.end, length: @length, kind: kind.name, kind_id: @kind_id, scope: kind.scope } end |