class Rley::Lexical::TokenRange
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)
Attributes
The index of the upper bound of token range @return [Integer]
The index of the lower bound of token range @return [Integer]
Public Class Methods
Source
# File lib/rley/lexical/token_range.rb, line 24 def initialize(aRangeRep) assign_low(aRangeRep) assign_high(aRangeRep) end
Constructor @param aRangeRep [Hash] A hash with keys :low and :high
Public Instance Methods
Source
# File lib/rley/lexical/token_range.rb, line 32 def ==(other) return true if equal?(other) case other when Hash result = low == other[:low] && high == other[:high] when TokenRange result = low == other.low && high == other.high when Range result = low == other.first && high == other.last when Array result = low == other[0] && high == other[1] end return result end
Test for equality of ranges. @param other [TokenRange, Range, Hash, Array<Integer>] @return [Boolean]
Source
# File lib/rley/lexical/token_range.rb, line 55 def assign(aRange) return if bounded? assign_low(aRange) if low.nil? assign_high(aRange) if high.nil? end
Conditional assign
Source
# File lib/rley/lexical/token_range.rb, line 50 def bounded? !(low.nil? || high.nil?) end
true when both bounds aren’t nil.
Source
# File lib/rley/lexical/token_range.rb, line 63 def out_of_range?(index) result = false result = true if !low.nil? && index < low result = true if !high.nil? && index > high return result end
Tell whether the given index value lies outside the range
Source
# File lib/rley/lexical/token_range.rb, line 73 def to_string(_indentation) low_text = low.nil? ? '?' : low.to_s high_text = high.nil? ? '?' : high.to_s "[#{low_text}, #{high_text}]" end
Emit a (formatted) string representation of the range. Mainly used for diagnosis/debugging purposes.
Private Instance Methods
Source
# File lib/rley/lexical/token_range.rb, line 89 def assign_high(aRange) case aRange when Hash then @high = aRange.fetch(:high, nil) when TokenRange then @high = aRange.high end end
Source
# File lib/rley/lexical/token_range.rb, line 82 def assign_low(aRange) case aRange when Hash then @low = aRange.fetch(:low, nil) when TokenRange then @low = aRange.low end end