Class: Rley::ParseTreeVisitor

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(aParseTree, aTraversalStrategy = :post_order) ⇒ ParseTreeVisitor

Build a visitor for the given ptree.

Parameters:

  • aParseTree (ParseTree)

    the parse tree to visit.

Raises:

  • (StandardError)


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

#ptreeObject (readonly)

Link to the parse tree to visit



8
9
10
# File 'lib/rley/parse_tree_visitor.rb', line 8

def ptree
  @ptree
end

#subscribersObject (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

#traversalObject (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.

Parameters:

  • aNonTerminalNode (NonTerminalNode)

    the node to visit.



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.

Parameters:

  • aParseTree (ParseTree)

    the ptree to visit.



80
81
82
# File 'lib/rley/parse_tree_visitor.rb', line 80

def end_visit_ptree(aParseTree)
  broadcast(:after_ptree, aParseTree)
end

#startObject

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.

Parameters:

  • aParseTree (ParseTree)

    the ptree to visit.



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.

Parameters:

  • aSubscriber (Object)


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.

Parameters:

  • aSubscriber (Object)


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.

Parameters:

  • aNonTerminalNode (NonTerminalNode)

    the node to visit.



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.

Parameters:

  • aTerminalNode (TerminalNode)

    the terminal to visit.



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