Class: Dendroid::GrmAnalysis::AlternativeItem
- Inherits:
-
DottedItem
- Object
- DottedItem
- Dendroid::GrmAnalysis::AlternativeItem
- Defined in:
- lib/dendroid/grm_analysis/alternative_item.rb
Overview
A specialization of DottedItem specific for Choice (rule)
Instance Attribute Summary collapse
-
#alt_index ⇒ Integer
readonly
The alternative number.
Attributes inherited from DottedItem
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Test for equality with another dotted item.
-
#empty? ⇒ Boolean
Indicate whether the rhs of the alternative is empty.
-
#final_pos? ⇒ Boolean
(also: #completed?)
Indicate whether the dot is at the start of rhs.
-
#initialize(aChoice, aPosition, index) ⇒ AlternativeItem
constructor
Constructor.
-
#next_symbol ⇒ Dendroid::Syntax::GrmSymbol, NilClass
Return the symbol right after the dot (if any).
-
#to_s ⇒ String
Return a String representation of the alternative item.
Methods inherited from DottedItem
#expecting?, #initial_pos?, #intermediate_pos?, #pre_scan?, #state
Constructor Details
#initialize(aChoice, aPosition, index) ⇒ AlternativeItem
Constructor.
16 17 18 19 |
# File 'lib/dendroid/grm_analysis/alternative_item.rb', line 16 def initialize(aChoice, aPosition, index) @alt_index = index super(aChoice, aPosition) end |
Instance Attribute Details
#alt_index ⇒ Integer (readonly)
10 11 12 |
# File 'lib/dendroid/grm_analysis/alternative_item.rb', line 10 def alt_index @alt_index end |
Instance Method Details
#==(other) ⇒ Boolean
Test for equality with another dotted item. Two dotted items are equal if they refer to the same rule and have both the same rhs and dot positions.
55 56 57 58 59 |
# File 'lib/dendroid/grm_analysis/alternative_item.rb', line 55 def ==(other) return true if eql?(other) (position == other.position) && rule.eql?(other.rule) && (alt_index == other.alt_index) end |
#empty? ⇒ Boolean
Indicate whether the rhs of the alternative is empty
31 32 33 |
# File 'lib/dendroid/grm_analysis/alternative_item.rb', line 31 def empty? rule.alternatives[alt_index].empty? end |
#final_pos? ⇒ Boolean Also known as: completed?
Indicate whether the dot is at the start of rhs
37 38 39 |
# File 'lib/dendroid/grm_analysis/alternative_item.rb', line 37 def final_pos? empty? || position == rule.alternatives[alt_index].size end |
#next_symbol ⇒ Dendroid::Syntax::GrmSymbol, NilClass
Return the symbol right after the dot (if any)
45 46 47 48 49 |
# File 'lib/dendroid/grm_analysis/alternative_item.rb', line 45 def next_symbol return nil if empty? || completed? rule.alternatives[alt_index].members[position] end |
#to_s ⇒ String
Return a String representation of the alternative item.
23 24 25 26 27 |
# File 'lib/dendroid/grm_analysis/alternative_item.rb', line 23 def to_s rhs_names = rule.alternatives[alt_index].members.map(&:to_s) dotted_rhs = rhs_names.insert(position, '.') "#{rule.head} => #{dotted_rhs.join(' ')}" end |