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) ⇒ ParseForestVisitor

Build a visitor for the given pforest.

Parameters:



48
49
50
51
52
53
54
# File 'lib/rley/parse_forest_visitor.rb', line 48

def initialize(aParseForest)
  @pforest = aParseForest
  @subscribers = []
  @prime_enum = Prime.instance.each
  @legs = []
  @node_accesses = Hash.new { |h, key| h[key] = Array.new }
end

Instance Attribute Details

#legsArray<SPPF::CompositeNode, Integer> (readonly)

path signature: an integer value that represents the set of edges used in traversal

Returns:



40
41
42
# File 'lib/rley/parse_forest_visitor.rb', line 40

def legs
  @legs
end

#node_accessesHash{SPPF::CompositeNode, Array<Integer>} (readonly)

Returns Keep trace from which path(s) a given node was accessed.

Returns:

  • (Hash{SPPF::CompositeNode, Array<Integer>})

    Keep trace from which path(s) a given node was accessed



44
45
46
# File 'lib/rley/parse_forest_visitor.rb', line 44

def node_accesses
  @node_accesses
end

#pforestSPPF::ParseForest (readonly)

Returns Link to the parse forest to visit.

Returns:



26
27
28
# File 'lib/rley/parse_forest_visitor.rb', line 26

def pforest
  @pforest
end

#prime_enumEnumerator (readonly)

Enumerator that generates a sequence of prime numbers

Returns:

  • (Enumerator)


34
35
36
# File 'lib/rley/parse_forest_visitor.rb', line 34

def prime_enum
  @prime_enum
end

#subscribersArray<Object> (readonly)

Returns List of objects that subscribed to the visit event notification.

Returns:

  • (Array<Object>)

    List of objects that subscribed to the visit event notification.



30
31
32
# File 'lib/rley/parse_forest_visitor.rb', line 30

def subscribers
  @subscribers
end

Instance Method Details

#end_visit_pforest(aParseForest) ⇒ Object

Visit event. The visitor has completed the visit of the pforest.

Parameters:

  • aParseForest (ParseForest)

    the pforest to visit.



130
131
132
# File 'lib/rley/parse_forest_visitor.rb', line 130

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

#startObject

The signal to begin the visit of the parse forest.



70
71
72
# File 'lib/rley/parse_forest_visitor.rb', line 70

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.



76
77
78
# File 'lib/rley/parse_forest_visitor.rb', line 76

def start_visit_pforest(aParseForest)
  broadcast(:before_pforest, aParseForest)
end

#subscribe(aSubscriber) ⇒ Object

Add a subscriber for the visit event notifications.

Parameters:

  • aSubscriber (Object)


58
59
60
# File 'lib/rley/parse_forest_visitor.rb', line 58

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)


65
66
67
# File 'lib/rley/parse_forest_visitor.rb', line 65

def unsubscribe(aSubscriber)
  subscribers.delete_if { |entry| entry == aSubscriber }
end

#visit_alternative(alternativeNd) ⇒ Object

TODO: control the logic of this method. Visit event. The visitor is visiting the given alternative node.

Parameters:

  • alternativeNd (AlternativeNode)

    the alternative node to visit.



95
96
97
98
99
100
101
102
103
# File 'lib/rley/parse_forest_visitor.rb', line 95

def visit_alternative(alternativeNd)
  broadcast(:before_alternative, alternativeNd)
  unless alternativeNd.signature_exist?
    alternativeNd.add_edge_signatures(prime_enum)
  end

  traverse_children(alternativeNd)
  broadcast(:after_alternative, alternativeNd)
end

#visit_epsilon(anEpsilonNode) ⇒ Object

Visit event. The visitor is visiting the given epsilon node.

Parameters:

  • anEpsilonNode (EpsilonNode)

    the terminal to visit.



116
117
118
119
# File 'lib/rley/parse_forest_visitor.rb', line 116

def visit_epsilon(anEpsilonNode)
  broadcast(:before_epsilon, anEpsilonNode)
  broadcast(:after_epsilon, anEpsilonNode)
end

#visit_nonterminal(nonTerminalNd) ⇒ Object

Visit event. The visitor is about to visit the given non terminal node.

Parameters:

  • nonTerminalNd (NonTerminalNode)

    the node to visit.



82
83
84
85
86
87
88
89
# File 'lib/rley/parse_forest_visitor.rb', line 82

def visit_nonterminal(nonTerminalNd)
  broadcast(:before_non_terminal, nonTerminalNd)
 unless nonTerminalNd.signature_exist?
   nonTerminalNd.add_edge_signatures(prime_enum)
 end
  traverse_children(nonTerminalNd)
  broadcast(:after_non_terminal, nonTerminalNd)
end

#visit_terminal(aTerminalNode) ⇒ Object

Visit event. The visitor is visiting the given terminal node.

Parameters:

  • aTerminalNode (TerminalNode)

    the terminal to visit.



108
109
110
111
# File 'lib/rley/parse_forest_visitor.rb', line 108

def visit_terminal(aTerminalNode)
  broadcast(:before_terminal, aTerminalNode)
  broadcast(:after_terminal, aTerminalNode)
end