Class: Rley::ParseForestVisitor

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

Instance Method Summary collapse

Constructor Details

#initialize(aParseForest, aTraversalStrategy = :post_order) ⇒ ParseForestVisitor

Build a visitor for the given pforest.

Parameters:

  • aParseForest (ParseForest)

    the parse tree to visit.



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

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

#pforestObject (readonly)

Link to the parse forest to visit



6
7
8
# File 'lib/rley/parse_forest_visitor.rb', line 6

def pforest
  @pforest
end

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

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

Parameters:

  • aNonTerminalNode (NonTerminalNode)

    the node to visit.



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.

Parameters:

  • aParseForest (ParseForest)

    the pforest to visit.



82
83
84
# File 'lib/rley/parse_forest_visitor.rb', line 82

def end_visit_pforest(aParseForest)
  broadcast(:after_pforest, aParseForest)
end

#startObject

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.

Parameters:

  • aParseForest (ParseForest)

    the pforest to visit.



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.

Parameters:

  • aSubscriber (Object)


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.

Parameters:

  • aSubscriber (Object)


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.

Parameters:

  • aNonTerminalNode (NonTerminalNode)

    the node to visit.



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.

Parameters:

  • aTerminalNode (TerminalNode)

    the terminal to visit.



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