Class: Sequitur::SymbolSequence
- Inherits:
-
Object
- Object
- Sequitur::SymbolSequence
- Defined in:
- lib/sequitur/symbol_sequence.rb
Overview
Represents a sequence (concatenation) of grammar symbols as they appear in rhs of productions
Instance Attribute Summary collapse
-
#symbols ⇒ Object
readonly
The sequence of symbols itself.
Instance Method Summary collapse
-
#<<(aSymbol) ⇒ Object
Append a grammar symbol at the end of the sequence.
-
#==(other) ⇒ Object
Equality testing.
-
#[](anIndex) ⇒ Object
Retrieve the element from the sequence at given position.
-
#accept(aVisitor) ⇒ Object
Part of the 'visitee' role in Visitor design pattern.
-
#clear ⇒ Object
Clear the symbol sequence.
-
#delete_at(position) ⇒ Object
Remove the element at given position.
-
#empty? ⇒ true / false
Tell whether the sequence is empty.
-
#initialize ⇒ SymbolSequence
constructor
Create an empty sequence.
-
#initialize_copy(orig) ⇒ Object
Copy constructor invoked by dup or clone methods.
-
#insert_at(position, another) ⇒ Object
Insert at position the elements from another sequence.
-
#reduce_step(index, aProduction) ⇒ Object
Given that the production P passed as argument has exactly 2 symbols in its rhs s1 s2, substitute in the rhs of self all occurrences of s1 s2 by a reference to P.
-
#references ⇒ Array of ProductionRef
Select the references to production appearing in the rhs.
-
#size ⇒ Object
Count the number of elements in the sequence.
-
#to_string ⇒ String
Emit a text representation of the symbol sequence.
Constructor Details
#initialize ⇒ SymbolSequence
Create an empty sequence
9 10 11 |
# File 'lib/sequitur/symbol_sequence.rb', line 9 def initialize() @symbols = [] end |
Instance Attribute Details
#symbols ⇒ Object (readonly)
The sequence of symbols itself
6 7 8 |
# File 'lib/sequitur/symbol_sequence.rb', line 6 def symbols @symbols end |
Instance Method Details
#<<(aSymbol) ⇒ Object
Append a grammar symbol at the end of the sequence.
45 46 47 |
# File 'lib/sequitur/symbol_sequence.rb', line 45 def <<(aSymbol) symbols << aSymbol end |
#==(other) ⇒ Object
Equality testing.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/sequitur/symbol_sequence.rb', line 60 def ==(other) return true if self.object_id == other.object_id case other when SymbolSequence same = self.symbols == other.symbols when Array same = self.symbols == other else same = false end return same end |
#[](anIndex) ⇒ Object
Retrieve the element from the sequence at given position.
51 52 53 |
# File 'lib/sequitur/symbol_sequence.rb', line 51 def [](anIndex) return symbols[anIndex] end |
#accept(aVisitor) ⇒ Object
Part of the 'visitee' role in Visitor design pattern.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/sequitur/symbol_sequence.rb', line 133 def accept(aVisitor) aVisitor.start_visit_rhs(self) # Let's proceed with the visit of productions symbols.each do |a_symb| if a_symb.is_a?(ProductionRef) a_symb.accept(aVisitor) else aVisitor.visit_terminal(a_symb) end end aVisitor.end_visit_rhs(self) end |
#clear ⇒ Object
Clear the symbol sequence.
25 26 27 28 29 |
# File 'lib/sequitur/symbol_sequence.rb', line 25 def clear() refs = references refs.each(&:unbind) @symbols = [] end |
#delete_at(position) ⇒ Object
Remove the element at given position
126 127 128 |
# File 'lib/sequitur/symbol_sequence.rb', line 126 def delete_at(position) symbols.delete_at(position) end |
#empty? ⇒ true / false
Tell whether the sequence is empty.
33 34 35 |
# File 'lib/sequitur/symbol_sequence.rb', line 33 def empty?() return symbols.empty? end |
#initialize_copy(orig) ⇒ Object
Copy constructor invoked by dup or clone methods.
15 16 17 18 19 20 |
# File 'lib/sequitur/symbol_sequence.rb', line 15 def initialize_copy(orig) # Deep copy to avoid the aliasing of production reference @symbols = orig.symbols.map do |sym| sym.is_a?(Symbol) ? sym : sym.dup end end |
#insert_at(position, another) ⇒ Object
Insert at position the elements from another sequence.
101 102 103 104 |
# File 'lib/sequitur/symbol_sequence.rb', line 101 def insert_at(position, another) klone = another.dup symbols.insert(position, *klone.symbols) end |
#reduce_step(index, aProduction) ⇒ Object
Given that the production P passed as argument has exactly 2 symbols in its rhs s1 s2, substitute in the rhs of self all occurrences of s1 s2 by a reference to P.
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/sequitur/symbol_sequence.rb', line 113 def reduce_step(index, aProduction) if symbols[index].is_a?(ProductionRef) symbols[index].bind_to(aProduction) else symbols[index] = ProductionRef.new(aProduction) end index1 = index + 1 symbols[index1].unbind if symbols[index1].is_a?(ProductionRef) delete_at(index1) end |
#references ⇒ Array of ProductionRef
Select the references to production appearing in the rhs.
78 79 80 |
# File 'lib/sequitur/symbol_sequence.rb', line 78 def references() return symbols.select { |symb| symb.is_a?(ProductionRef) } end |
#size ⇒ Object
Count the number of elements in the sequence. @return [Fixnum] the number of elements
39 40 41 |
# File 'lib/sequitur/symbol_sequence.rb', line 39 def size() return symbols.size end |
#to_string ⇒ String
Emit a text representation of the symbol sequence. Text is of the form: space-separated sequence of symbols.
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/sequitur/symbol_sequence.rb', line 86 def to_string() rhs_text = symbols.map do |elem| case elem when String then "'#{elem}'" else elem.to_s end end return rhs_text.join(' ') end |