Class: Rley::ParseForestVisitor
- Inherits:
-
Object
- Object
- Rley::ParseForestVisitor
- Defined in:
- lib/rley/parse_forest_visitor.rb
Overview
A visitor class dedicated in the visit of a parse forest. It combines the Visitor and Observer patterns.
Instance Attribute Summary collapse
-
#legs ⇒ Array<SPPF::CompositeNode, Integer>
readonly
path signature: an integer value that represents the set of edges used in traversal.
-
#node_accesses ⇒ Hash{SPPF::CompositeNode, Array<Integer>}
readonly
Keep trace from which path(s) a given node was accessed.
-
#pforest ⇒ SPPF::ParseForest
readonly
Link to the parse forest to visit.
-
#prime_enum ⇒ Enumerator
readonly
Enumerator that generates a sequence of prime numbers.
-
#subscribers ⇒ Array<Object>
readonly
List of objects that subscribed to the visit event notification.
Instance Method Summary collapse
-
#end_visit_pforest(aParseForest) ⇒ Object
Visit event.
-
#initialize(aParseForest) ⇒ ParseForestVisitor
constructor
Build a visitor for the given pforest.
-
#start ⇒ Object
The signal to begin the visit of the parse forest.
-
#start_visit_pforest(aParseForest) ⇒ Object
Visit event.
-
#subscribe(aSubscriber) ⇒ Object
Add a subscriber for the visit event notifications.
-
#unsubscribe(aSubscriber) ⇒ Object
Remove the given object from the subscription list.
-
#visit_alternative(alternativeNd) ⇒ Object
TODO: control the logic of this method.
-
#visit_epsilon(anEpsilonNode) ⇒ Object
Visit event.
-
#visit_nonterminal(nonTerminalNd) ⇒ Object
Visit event.
-
#visit_terminal(aTerminalNode) ⇒ Object
Visit event.
Constructor Details
#initialize(aParseForest) ⇒ ParseForestVisitor
Build a visitor for the given pforest.
50 51 52 53 54 55 56 |
# File 'lib/rley/parse_forest_visitor.rb', line 50 def initialize(aParseForest) @pforest = aParseForest @subscribers = [] @prime_enum = Prime.instance.each @legs = [] @node_accesses = Hash.new { |h, key| h[key] = [] } end |
Instance Attribute Details
#legs ⇒ Array<SPPF::CompositeNode, Integer> (readonly)
path signature: an integer value that represents the set of edges used in traversal
42 43 44 |
# File 'lib/rley/parse_forest_visitor.rb', line 42 def legs @legs end |
#node_accesses ⇒ Hash{SPPF::CompositeNode, Array<Integer>} (readonly)
Returns Keep trace from which path(s) a given node was accessed.
46 47 48 |
# File 'lib/rley/parse_forest_visitor.rb', line 46 def node_accesses @node_accesses end |
#pforest ⇒ SPPF::ParseForest (readonly)
Returns Link to the parse forest to visit.
28 29 30 |
# File 'lib/rley/parse_forest_visitor.rb', line 28 def pforest @pforest end |
#prime_enum ⇒ Enumerator (readonly)
Enumerator that generates a sequence of prime numbers
36 37 38 |
# File 'lib/rley/parse_forest_visitor.rb', line 36 def prime_enum @prime_enum end |
#subscribers ⇒ Array<Object> (readonly)
Returns List of objects that subscribed to the visit event notification.
32 33 34 |
# File 'lib/rley/parse_forest_visitor.rb', line 32 def subscribers @subscribers end |
Instance Method Details
#end_visit_pforest(aParseForest) ⇒ Object
Visit event. The visitor has completed the visit of the pforest.
132 133 134 |
# File 'lib/rley/parse_forest_visitor.rb', line 132 def end_visit_pforest(aParseForest) broadcast(:after_pforest, aParseForest) end |
#start ⇒ Object
The signal to begin the visit of the parse forest.
72 73 74 |
# File 'lib/rley/parse_forest_visitor.rb', line 72 def start pforest.accept(self) end |
#start_visit_pforest(aParseForest) ⇒ Object
Visit event. The visitor is about to visit the pforest.
78 79 80 |
# File 'lib/rley/parse_forest_visitor.rb', line 78 def start_visit_pforest(aParseForest) broadcast(:before_pforest, aParseForest) end |
#subscribe(aSubscriber) ⇒ Object
Add a subscriber for the visit event notifications.
60 61 62 |
# File 'lib/rley/parse_forest_visitor.rb', line 60 def subscribe(aSubscriber) subscribers << aSubscriber end |
#unsubscribe(aSubscriber) ⇒ Object
Remove the given object from the subscription list. The object won't be notified of visit events.
67 68 69 |
# File 'lib/rley/parse_forest_visitor.rb', line 67 def unsubscribe(aSubscriber) subscribers.delete_if { |entry| entry == aSubscriber } end |
#visit_alternative(alternativeNd) ⇒ Object
TODO: control the logic of this method. Visit event. The visitor is visiting the given alternative node.
97 98 99 100 101 102 103 104 105 |
# File 'lib/rley/parse_forest_visitor.rb', line 97 def visit_alternative(alternativeNd) broadcast(:before_alternative, alternativeNd) unless alternativeNd.signature_exist? alternativeNd.add_edge_signatures(prime_enum) end traverse_children(alternativeNd) broadcast(:after_alternative, alternativeNd) end |
#visit_epsilon(anEpsilonNode) ⇒ Object
Visit event. The visitor is visiting the given epsilon node.
118 119 120 121 |
# File 'lib/rley/parse_forest_visitor.rb', line 118 def visit_epsilon(anEpsilonNode) broadcast(:before_epsilon, anEpsilonNode) broadcast(:after_epsilon, anEpsilonNode) end |
#visit_nonterminal(nonTerminalNd) ⇒ Object
Visit event. The visitor is about to visit the given non terminal node.
84 85 86 87 88 89 90 91 |
# File 'lib/rley/parse_forest_visitor.rb', line 84 def visit_nonterminal(nonTerminalNd) broadcast(:before_non_terminal, nonTerminalNd) unless nonTerminalNd.signature_exist? nonTerminalNd.add_edge_signatures(prime_enum) end traverse_children(nonTerminalNd) broadcast(:after_non_terminal, nonTerminalNd) end |
#visit_terminal(aTerminalNode) ⇒ Object
Visit event. The visitor is visiting the given terminal node.
110 111 112 113 |
# File 'lib/rley/parse_forest_visitor.rb', line 110 def visit_terminal(aTerminalNode) broadcast(:before_terminal, aTerminalNode) broadcast(:after_terminal, aTerminalNode) end |