Class: Rley::Lexical::TokenRange
- Inherits:
-
Object
- Object
- Rley::Lexical::TokenRange
- Defined in:
- lib/rley/lexical/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
-
#high ⇒ Integer
readonly
The index of the upper bound of token range.
-
#low ⇒ Integer
readonly
The index of the lower bound of token range.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Test for equality of ranges.
-
#assign(aRange) ⇒ Object
Conditional assign.
-
#bounded? ⇒ Boolean
true when both bounds aren't nil.
-
#initialize(aRangeRep) ⇒ TokenRange
constructor
Constructor.
-
#out_of_range?(index) ⇒ Boolean
Tell whether the given index value lies outside the range.
-
#to_string(_indentation) ⇒ Object
Emit a (formatted) string representation of the range.
Constructor Details
#initialize(aRangeRep) ⇒ TokenRange
Constructor
24 25 26 27 |
# File 'lib/rley/lexical/token_range.rb', line 24 def initialize(aRangeRep) assign_low(aRangeRep) assign_high(aRangeRep) end |
Instance Attribute Details
#high ⇒ Integer (readonly)
The index of the upper bound of token range
20 21 22 |
# File 'lib/rley/lexical/token_range.rb', line 20 def high @high end |
#low ⇒ Integer (readonly)
The index of the lower bound of token range
16 17 18 |
# File 'lib/rley/lexical/token_range.rb', line 16 def low @low end |
Instance Method Details
#==(other) ⇒ Boolean
Test for equality of ranges.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# 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 |
#assign(aRange) ⇒ Object
Conditional assign
55 56 57 58 59 60 |
# 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 |
#bounded? ⇒ Boolean
true when both bounds aren't nil.
50 51 52 |
# File 'lib/rley/lexical/token_range.rb', line 50 def bounded? !(low.nil? || high.nil?) end |
#out_of_range?(index) ⇒ Boolean
Tell whether the given index value lies outside the range
63 64 65 66 67 68 69 |
# 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 |
#to_string(_indentation) ⇒ Object
Emit a (formatted) string representation of the range. Mainly used for diagnosis/debugging purposes.
73 74 75 76 77 78 |
# 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 |