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.
18 19 20 21 22 23 24 |
# File 'lib/rley/parse_tree_visitor.rb', line 18 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
8 9 10 |
# File 'lib/rley/parse_tree_visitor.rb', line 8 def ptree @ptree end |
#subscribers ⇒ Object (readonly)
List of objects that subscribed to the visit event notification.
11 12 13 |
# File 'lib/rley/parse_tree_visitor.rb', line 11 def subscribers @subscribers end |
#traversal ⇒ Object (readonly)
Indicates the kind of tree traversal to perform: :post_order, :pre-order
14 15 16 |
# File 'lib/rley/parse_tree_visitor.rb', line 14 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.
74 75 76 |
# File 'lib/rley/parse_tree_visitor.rb', line 74 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.
80 81 82 |
# File 'lib/rley/parse_tree_visitor.rb', line 80 def end_visit_ptree(aParseTree) broadcast(:after_ptree, aParseTree) end |
#start ⇒ Object
The signal to begin the visit of the parse tree.
40 41 42 |
# File 'lib/rley/parse_tree_visitor.rb', line 40 def start ptree.accept(self) end |
#start_visit_ptree(aParseTree) ⇒ Object
Visit event. The visitor is about to visit the ptree.
46 47 48 |
# File 'lib/rley/parse_tree_visitor.rb', line 46 def start_visit_ptree(aParseTree) broadcast(:before_ptree, aParseTree) end |
#subscribe(aSubscriber) ⇒ Object
Add a subscriber for the visit event notifications.
28 29 30 |
# File 'lib/rley/parse_tree_visitor.rb', line 28 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.
35 36 37 |
# File 'lib/rley/parse_tree_visitor.rb', line 35 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.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rley/parse_tree_visitor.rb', line 52 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.
66 67 68 69 |
# File 'lib/rley/parse_tree_visitor.rb', line 66 def visit_terminal(aTerminalNode) broadcast(:before_terminal, aTerminalNode) broadcast(:after_terminal, aTerminalNode) end |