Class: LexerKit::Core::Span
- Inherits:
-
Object
- Object
- LexerKit::Core::Span
- Defined in:
- lib/lexer_kit/core/span.rb
Overview
Span represents a range in the input as byte offsets. It uses a half-open interval [start, start+len).
Instance Attribute Summary collapse
-
#len ⇒ Object
readonly
Returns the value of attribute len.
-
#start ⇒ Object
readonly
Returns the value of attribute start.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#cover?(other) ⇒ Boolean
Check if this span fully covers another span.
-
#empty? ⇒ Boolean
Check if span is empty.
-
#end ⇒ Integer
End position (exclusive).
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(start, len) ⇒ Span
constructor
A new instance of Span.
- #inspect ⇒ Object
-
#merge(other) ⇒ Span
Create a new span that merges this and another span.
-
#overlap?(other) ⇒ Boolean
Check if this span overlaps with another span.
-
#slice(bytes) ⇒ String
Extract the text from a byte string.
- #to_s ⇒ Object
Constructor Details
#initialize(start, len) ⇒ Span
Returns a new instance of Span.
12 13 14 15 |
# File 'lib/lexer_kit/core/span.rb', line 12 def initialize(start, len) @start = start @len = len end |
Instance Attribute Details
#len ⇒ Object (readonly)
Returns the value of attribute len.
8 9 10 |
# File 'lib/lexer_kit/core/span.rb', line 8 def len @len end |
#start ⇒ Object (readonly)
Returns the value of attribute start.
8 9 10 |
# File 'lib/lexer_kit/core/span.rb', line 8 def start @start end |
Instance Method Details
#==(other) ⇒ Object
59 60 61 |
# File 'lib/lexer_kit/core/span.rb', line 59 def ==(other) other.is_a?(Span) && @start == other.start && @len == other.len end |
#cover?(other) ⇒ Boolean
Check if this span fully covers another span
32 33 34 |
# File 'lib/lexer_kit/core/span.rb', line 32 def cover?(other) @start <= other.start && self.end >= other.end end |
#empty? ⇒ Boolean
Check if span is empty
25 26 27 |
# File 'lib/lexer_kit/core/span.rb', line 25 def empty? @len == 0 end |
#end ⇒ Integer
End position (exclusive)
19 20 21 |
# File 'lib/lexer_kit/core/span.rb', line 19 def end @start + @len end |
#eql?(other) ⇒ Boolean
63 64 65 |
# File 'lib/lexer_kit/core/span.rb', line 63 def eql?(other) self == other end |
#hash ⇒ Object
67 68 69 |
# File 'lib/lexer_kit/core/span.rb', line 67 def hash [@start, @len].hash end |
#inspect ⇒ Object
75 76 77 |
# File 'lib/lexer_kit/core/span.rb', line 75 def inspect "#<LexerKit::Core::Span #{self}>" end |
#merge(other) ⇒ Span
Create a new span that merges this and another span
46 47 48 49 50 |
# File 'lib/lexer_kit/core/span.rb', line 46 def merge(other) new_start = [@start, other.start].min new_end = [self.end, other.end].max Span.new(new_start, new_end - new_start) end |
#overlap?(other) ⇒ Boolean
Check if this span overlaps with another span
39 40 41 |
# File 'lib/lexer_kit/core/span.rb', line 39 def overlap?(other) @start < other.end && self.end > other.start end |
#slice(bytes) ⇒ String
Extract the text from a byte string
55 56 57 |
# File 'lib/lexer_kit/core/span.rb', line 55 def slice(bytes) bytes.byteslice(@start, @len) end |
#to_s ⇒ Object
71 72 73 |
# File 'lib/lexer_kit/core/span.rb', line 71 def to_s "#{@start}..#{self.end}" end |