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
-
#agenda ⇒ Object
readonly
A Hash with pairs of the form: Node => node visit data.
-
#pforest ⇒ Object
readonly
Link to the parse forest to visit.
-
#subscribers ⇒ Object
readonly
List of objects that subscribed to the visit event notification.
-
#traversal ⇒ Object
readonly
Indicates the kind of forest traversal to perform: :post_order, :pre-order.
Instance Method Summary collapse
-
#end_visit_nonterminal(aNonTerminalNode) ⇒ Object
Visit event.
-
#end_visit_pforest(aParseForest) ⇒ Object
Visit event.
-
#initialize(aParseForest, aTraversalStrategy = :post_order) ⇒ 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_nonterminal(aNonTerminalNode) ⇒ Object
Visit event.
-
#visit_terminal(aTerminalNode) ⇒ Object
Visit event.
Constructor Details
#initialize(aParseForest, aTraversalStrategy = :post_order) ⇒ ParseForestVisitor
Build a visitor for the given pforest.
19 20 21 22 23 |
# File 'lib/rley/parse_forest_visitor.rb', line 19 def initialize(aParseForest, aTraversalStrategy = :post_order) @pforest = aParseForest @subscribers = [] @traversal = aTraversalStrategy end |
Instance Attribute Details
#agenda ⇒ Object (readonly)
A Hash with pairs of the form: Node => node visit data
12 13 14 |
# File 'lib/rley/parse_forest_visitor.rb', line 12 def agenda @agenda end |
#pforest ⇒ Object (readonly)
Link to the parse forest to visit
6 7 8 |
# File 'lib/rley/parse_forest_visitor.rb', line 6 def pforest @pforest end |
#subscribers ⇒ Object (readonly)
List of objects that subscribed to the visit event notification.
9 10 11 |
# File 'lib/rley/parse_forest_visitor.rb', line 9 def subscribers @subscribers end |
#traversal ⇒ Object (readonly)
Indicates the kind of forest traversal to perform: :post_order, :pre-order
15 16 17 |
# File 'lib/rley/parse_forest_visitor.rb', line 15 def traversal @traversal end |
Instance Method Details
#end_visit_nonterminal(aNonTerminalNode) ⇒ Object
Visit event. The visitor has completed its visit of the given non-terminal node.
76 77 78 |
# File 'lib/rley/parse_forest_visitor.rb', line 76 def end_visit_nonterminal(aNonTerminalNode) broadcast(:after_non_terminal, aNonTerminalNode) end |
#end_visit_pforest(aParseForest) ⇒ Object
Visit event. The visitor has completed the visit of the pforest.
82 83 84 |
# File 'lib/rley/parse_forest_visitor.rb', line 82 def end_visit_pforest(aParseForest) broadcast(:after_pforest, aParseForest) end |
#start ⇒ Object
The signal to begin the visit of the parse forest.
39 40 41 |
# File 'lib/rley/parse_forest_visitor.rb', line 39 def start() pforest.accept(self) end |
#start_visit_pforest(aParseForest) ⇒ Object
Visit event. The visitor is about to visit the pforest.
46 47 48 |
# File 'lib/rley/parse_forest_visitor.rb', line 46 def start_visit_pforest(aParseForest) broadcast(:before_pforest, aParseForest) end |
#subscribe(aSubscriber) ⇒ Object
Add a subscriber for the visit event notifications.
27 28 29 |
# File 'lib/rley/parse_forest_visitor.rb', line 27 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.
34 35 36 |
# File 'lib/rley/parse_forest_visitor.rb', line 34 def unsubscribe(aSubscriber) subscribers.delete_if { |entry| entry == aSubscriber } end |
#visit_nonterminal(aNonTerminalNode) ⇒ Object
Visit event. The visitor is about to visit the given non terminal node.
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/rley/parse_forest_visitor.rb', line 53 def visit_nonterminal(aNonTerminalNode) if @traversal == :post_order broadcast(:before_non_terminal, aNonTerminalNode) traverse_children(aNonTerminalNode) else traverse_children(aNonTerminalNode) broadcast(:before_non_terminal, aNonTerminalNode) end broadcast(:after_non_terminal, aNonTerminalNode) end |
#visit_terminal(aTerminalNode) ⇒ Object
Visit event. The visitor is visiting the given terminal node.
67 68 69 70 |
# File 'lib/rley/parse_forest_visitor.rb', line 67 def visit_terminal(aTerminalNode) broadcast(:before_terminal, aTerminalNode) broadcast(:after_terminal, aTerminalNode) end |