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:



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

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

Instance Attribute Details

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

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

Returns:



42
43
44
# File 'lib/rley/parse_forest_visitor.rb', line 42

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



46
47
48
# File 'lib/rley/parse_forest_visitor.rb', line 46

def node_accesses
  @node_accesses
end

#pforestSPPF::ParseForest (readonly)

Returns Link to the parse forest to visit.

Returns:



28
29
30
# File 'lib/rley/parse_forest_visitor.rb', line 28

def pforest
  @pforest
end

#prime_enumEnumerator (readonly)

Enumerator that generates a sequence of prime numbers

Returns:

  • (Enumerator)


36
37
38
# File 'lib/rley/parse_forest_visitor.rb', line 36

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.



32
33
34
# File 'lib/rley/parse_forest_visitor.rb', line 32

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.



132
133
134
# File 'lib/rley/parse_forest_visitor.rb', line 132

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

#startObject

The signal to begin the visit of the parse forest.



72
73
74
# File 'lib/rley/parse_forest_visitor.rb', line 72

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.



78
79
80
# File 'lib/rley/parse_forest_visitor.rb', line 78

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

#subscribe(aSubscriber) ⇒ Object

Add a subscriber for the visit event notifications.

Parameters:

  • aSubscriber (Object)


60
61
62
# File 'lib/rley/parse_forest_visitor.rb', line 60

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)


67
68
69
# File 'lib/rley/parse_forest_visitor.rb', line 67

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.



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

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.



118
119
120
121
# File 'lib/rley/parse_forest_visitor.rb', line 118

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.



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

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.



110
111
112
113
# File 'lib/rley/parse_forest_visitor.rb', line 110

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