Class: Rley::ParseTreeVisitor
- Inherits:
-
Object
- Object
- Rley::ParseTreeVisitor
- Defined in:
- lib/rley/parse_tree_visitor.rb
Overview
A visitor class dedicated in the visit of a parse tree. It combines the Visitor and Observer patterns.
Instance Attribute Summary collapse
-
#ptree ⇒ Object
readonly
Link to the parse tree to visit.
-
#subscribers ⇒ Object
readonly
List of objects that subscribed to the visit event notification.
-
#traversal ⇒ Object
readonly
Indicates the kind of tree traversal to perform: :post_order, :pre-order.
Instance Method Summary collapse
-
#end_visit_nonterminal(aNonTerminalNode) ⇒ Object
Visit event.
-
#end_visit_ptree(aParseTree) ⇒ Object
Visit event.
-
#initialize(aParseTree, aTraversalStrategy = :post_order) ⇒ ParseTreeVisitor
constructor
Build a visitor for the given ptree.
-
#start ⇒ Object
The signal to begin the visit of the parse tree.
-
#start_visit_ptree(aParseTree) ⇒ 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(aParseTree, aTraversalStrategy = :post_order) ⇒ ParseTreeVisitor
Build a visitor for the given ptree.
16 17 18 19 20 21 |
# File 'lib/rley/parse_tree_visitor.rb', line 16 def initialize(aParseTree, aTraversalStrategy = :post_order) raise StandardError if aParseTree.nil? @ptree = aParseTree @subscribers = [] @traversal = aTraversalStrategy end |
Instance Attribute Details
#ptree ⇒ Object (readonly)
Link to the parse tree to visit
6 7 8 |
# File 'lib/rley/parse_tree_visitor.rb', line 6 def ptree @ptree end |
#subscribers ⇒ Object (readonly)
List of objects that subscribed to the visit event notification.
9 10 11 |
# File 'lib/rley/parse_tree_visitor.rb', line 9 def subscribers @subscribers end |
#traversal ⇒ Object (readonly)
Indicates the kind of tree traversal to perform: :post_order, :pre-order
12 13 14 |
# File 'lib/rley/parse_tree_visitor.rb', line 12 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.
71 72 73 |
# File 'lib/rley/parse_tree_visitor.rb', line 71 def end_visit_nonterminal(aNonTerminalNode) broadcast(:after_non_terminal, aNonTerminalNode) end |
#end_visit_ptree(aParseTree) ⇒ Object
Visit event. The visitor has completed the visit of the ptree.
77 78 79 |
# File 'lib/rley/parse_tree_visitor.rb', line 77 def end_visit_ptree(aParseTree) broadcast(:after_ptree, aParseTree) end |
#start ⇒ Object
The signal to begin the visit of the parse tree.
37 38 39 |
# File 'lib/rley/parse_tree_visitor.rb', line 37 def start() ptree.accept(self) end |
#start_visit_ptree(aParseTree) ⇒ Object
Visit event. The visitor is about to visit the ptree.
43 44 45 |
# File 'lib/rley/parse_tree_visitor.rb', line 43 def start_visit_ptree(aParseTree) broadcast(:before_ptree, aParseTree) end |
#subscribe(aSubscriber) ⇒ Object
Add a subscriber for the visit event notifications.
25 26 27 |
# File 'lib/rley/parse_tree_visitor.rb', line 25 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.
32 33 34 |
# File 'lib/rley/parse_tree_visitor.rb', line 32 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.
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rley/parse_tree_visitor.rb', line 49 def visit_nonterminal(aNonTerminalNode) if @traversal == :post_order broadcast(:before_non_terminal, aNonTerminalNode) traverse_subnodes(aNonTerminalNode) else traverse_subnodes(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.
63 64 65 66 |
# File 'lib/rley/parse_tree_visitor.rb', line 63 def visit_terminal(aTerminalNode) broadcast(:before_terminal, aTerminalNode) broadcast(:after_terminal, aTerminalNode) end |