Class: LexerKit::Core::Span

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(start, len) ⇒ Span

Returns a new instance of Span.

Parameters:

  • start (Integer)

    start byte offset (0-based)

  • len (Integer)

    length in bytes



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

#lenObject (readonly)

Returns the value of attribute len.



8
9
10
# File 'lib/lexer_kit/core/span.rb', line 8

def len
  @len
end

#startObject (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

Parameters:

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


25
26
27
# File 'lib/lexer_kit/core/span.rb', line 25

def empty?
  @len == 0
end

#endInteger

End position (exclusive)

Returns:

  • (Integer)


19
20
21
# File 'lib/lexer_kit/core/span.rb', line 19

def end
  @start + @len
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/lexer_kit/core/span.rb', line 63

def eql?(other)
  self == other
end

#hashObject



67
68
69
# File 'lib/lexer_kit/core/span.rb', line 67

def hash
  [@start, @len].hash
end

#inspectObject



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

Parameters:

Returns:



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

Parameters:

Returns:

  • (Boolean)


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

Parameters:

  • bytes (String)

    source bytes (BINARY encoding)

Returns:

  • (String)


55
56
57
# File 'lib/lexer_kit/core/span.rb', line 55

def slice(bytes)
  bytes.byteslice(@start, @len)
end

#to_sObject



71
72
73
# File 'lib/lexer_kit/core/span.rb', line 71

def to_s
  "#{@start}..#{self.end}"
end