Class: RKelly::Visitors::DotVisitor

Inherits:
Visitor
  • Object
show all
Defined in:
lib/rkelly/visitors/dot_visitor.rb

Defined Under Namespace

Classes: Arrow, Node

Constant Summary

Constants inherited from Visitor

Visitor::ALL_NODES, Visitor::ARRAY_VALUE_NODES, Visitor::BINARY_NODES, Visitor::CONDITIONAL_NODES, Visitor::FUNC_CALL_NODES, Visitor::FUNC_DECL_NODES, Visitor::NAME_VALUE_NODES, Visitor::PREFIX_POSTFIX_NODES, Visitor::SINGLE_VALUE_NODES, Visitor::TERMINAL_NODES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Visitor

#accept

Constructor Details

#initializeDotVisitor

Returns a new instance of DotVisitor.



24
25
26
27
28
29
# File 'lib/rkelly/visitors/dot_visitor.rb', line 24

def initialize
  @stack = []
  @node_index = 0
  @nodes  = []
  @arrows = []
end

Instance Attribute Details

#arrowsObject (readonly)

Returns the value of attribute arrows.



23
24
25
# File 'lib/rkelly/visitors/dot_visitor.rb', line 23

def arrows
  @arrows
end

#nodesObject (readonly)

Returns the value of attribute nodes.



23
24
25
# File 'lib/rkelly/visitors/dot_visitor.rb', line 23

def nodes
  @nodes
end

Instance Method Details

#typeObject

Terminal nodes



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rkelly/visitors/dot_visitor.rb', line 32

%w{
  BreakNode ContinueNode EmptyStatementNode FalseNode
  NullNode NumberNode ParameterNode RegexpNode ResolveNode StringNode
  ThisNode TrueNode
}.each do |type|
  define_method(:"visit_#{type}") do |o|
    node = Node.new(@node_index += 1, [type, o.value].compact)
    add_arrow_for(node)
    @nodes << node
  end
end

#visit_BracketAccessorNode(o) ⇒ Object



174
175
176
177
178
179
180
181
182
183
# File 'lib/rkelly/visitors/dot_visitor.rb', line 174

def visit_BracketAccessorNode(o)
  node = Node.new(@node_index += 1, ['BracketAccessorNode'])
  add_arrow_for(node)
  @nodes << node
  @stack.push(node)
  [:value, :accessor].each do |method|
    o.send(method) && o.send(method).accept(self)
  end
  @stack.pop
end

#visit_DotAccessorNode(o) ⇒ Object



210
211
212
213
214
215
216
217
218
219
# File 'lib/rkelly/visitors/dot_visitor.rb', line 210

def visit_DotAccessorNode(o)
  node = Node.new(@node_index += 1, ['DotAccessorNode', o.accessor])
  add_arrow_for(node)
  @nodes << node
  @stack.push(node)
  [:value].each do |method|
    o.send(method) && o.send(method).accept(self)
  end
  @stack.pop
end

#visit_ForInNode(o) ⇒ Object



152
153
154
155
156
157
158
159
160
161
# File 'lib/rkelly/visitors/dot_visitor.rb', line 152

def visit_ForInNode(o)
  node = Node.new(@node_index += 1, ['ForInNode'])
  add_arrow_for(node)
  @nodes << node
  @stack.push(node)
  [:left, :right, :value].each do |method|
    o.send(method) && o.send(method).accept(self)
  end
  @stack.pop
end

#visit_ForNode(o) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/rkelly/visitors/dot_visitor.rb', line 128

def visit_ForNode(o)
  node = Node.new(@node_index += 1, ['ForNode'])
  add_arrow_for(node)
  @nodes << node
  @stack.push(node)
  [:init, :test, :counter, :value].each do |method|
    o.send(method) && o.send(method).accept(self)
  end
  @stack.pop
end

#visit_TryNode(o) ⇒ Object



163
164
165
166
167
168
169
170
171
172
# File 'lib/rkelly/visitors/dot_visitor.rb', line 163

def visit_TryNode(o)
  node = Node.new(@node_index += 1, ['TryNode', o.catch_var || 'NULL'])
  add_arrow_for(node)
  @nodes << node
  @stack.push(node)
  [:value, :catch_block, :finally_block].each do |method|
    o.send(method) && o.send(method).accept(self)
  end
  @stack.pop
end