Class: Rley::Tokens::TokenRange

Inherits:
Object
  • Object
show all
Defined in:
lib/rley/tokens/token_range.rb

Overview

A token range (also called an extent) represents an interval of token positions that is matched by a given grammar symbol. For instance consider the expression E: 3 + 11, let's assume that the integer literal '3' is the fifth input token and that the '+' and '11' tokens are respectively at position 6 and 7; then the token range associated with E is [5, 7] While the parse tree/forest is being constructed the boundaries of the token range can be temporarily undefined (= set to nil)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aRangeRep) ⇒ TokenRange

Returns a new instance of TokenRange.

Parameters:

  • aRangeRep (Hash)


19
20
21
22
# File 'lib/rley/tokens/token_range.rb', line 19

def initialize(aRangeRep)
  assign_low(aRangeRep)
  assign_high(aRangeRep)
end

Instance Attribute Details

#highObject (readonly)

The index of the upper bound of token range



16
17
18
# File 'lib/rley/tokens/token_range.rb', line 16

def high
  @high
end

#lowObject (readonly)

The index of the lower bound of token range



13
14
15
# File 'lib/rley/tokens/token_range.rb', line 13

def low
  @low
end

Instance Method Details

#==(other) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rley/tokens/token_range.rb', line 25

def ==(other)
  return true if object_id == other.object_id

  case other
    when Hash
      result = low == other[:low] && high == other[:high]
    when TokenRange
      result = low == other.low && high == other.high
    when Array
      result = low == other[0] && high == other[1]
  end

  return result
end

#assign(aRange) ⇒ Object

Conditional assign



46
47
48
49
50
51
# File 'lib/rley/tokens/token_range.rb', line 46

def assign(aRange)
  return if bounded?

  assign_low(aRange) if low.nil?
  assign_high(aRange) if high.nil?
end

#bounded?Boolean

true when both bounds aren't nil.

Returns:

  • (Boolean)


41
42
43
# File 'lib/rley/tokens/token_range.rb', line 41

def bounded?()
  return !(low.nil? || high.nil?)
end

#out_of_range?(index) ⇒ Boolean

Tell whether the given index value lies outside the range

Returns:

  • (Boolean)


54
55
56
57
58
59
60
# File 'lib/rley/tokens/token_range.rb', line 54

def out_of_range?(index)
  result = false
  result = true if !low.nil? && index < low
  result = true if !high.nil? && index > high

  return result
end

#to_string(_indentation) ⇒ Object

Emit a (formatted) string representation of the range. Mainly used for diagnosis/debugging purposes.



64
65
66
67
68
69
# File 'lib/rley/tokens/token_range.rb', line 64

def to_string(_indentation)
  low_text = low.nil? ? '?' : low.to_s
  high_text = high.nil? ? '?' : high.to_s
  
  return "[#{low_text}, #{high_text}]"
end