Class: Johnson::Visitors::DotVisitor

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

Defined Under Namespace

Classes: Link, Node

Constant Summary collapse

ESCAPE =
/([<>"\\])/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ DotVisitor

Returns a new instance of DotVisitor.

Yields:

  • (_self)

Yield Parameters:



29
30
31
32
33
34
35
# File 'lib/johnson/visitors/dot_visitor.rb', line 29

def initialize
  @nodes = []
  @links = []
  @color = 'lightblue2'
  @style = 'filled'
  yield self if block_given?
end

Instance Attribute Details

Returns the value of attribute links.



4
5
6
# File 'lib/johnson/visitors/dot_visitor.rb', line 4

def links
  @links
end

#nodesObject

Returns the value of attribute nodes.



4
5
6
# File 'lib/johnson/visitors/dot_visitor.rb', line 4

def nodes
  @nodes
end

Instance Method Details

#accept(target) ⇒ Object



164
165
166
# File 'lib/johnson/visitors/dot_visitor.rb', line 164

def accept(target)
  target.accept(self)
end

#to_sObject



37
38
39
40
41
42
43
# File 'lib/johnson/visitors/dot_visitor.rb', line 37

def to_s
  "digraph parsetree {\n" +
    "  node [color=#{@color}, style=#{@style}];\n" +
    @nodes.map { |n| n.to_s }.join("\n") +
    @links.map { |n| n.to_s }.join("\n") +
    "\n}"
end

#visit_Function(o) ⇒ Object

FUNCTION NODES ###



135
136
137
138
139
# File 'lib/johnson/visitors/dot_visitor.rb', line 135

def visit_Function(o)
  @nodes << Node.new(o.object_id, 'Function', [o.arguments.join(', ')])
  @links << Link.new(o.object_id, o.body.object_id, { 'label' => 'body' })
  o.body.accept(self)
end

#visit_SourceElements(o) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/johnson/visitors/dot_visitor.rb', line 45

def visit_SourceElements(o)
  @nodes << Node.new(o.object_id, 'SourceElements', [])
  o.value.each { |x|
    @links << Link.new(o.object_id, x.object_id)
    x.accept(self)
  }
end

#visit_Try(o) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/johnson/visitors/dot_visitor.rb', line 85

def visit_Try(o)
  @nodes << Node.new(o.object_id, 'Try', [])
  @links << Link.new(o.object_id, o.cond.object_id, {'label' => 'cond' })
  o.cond.accept(self)
  if o.b_then
    o.b_then.each { |x|
      @links << Link.new(o.object_id, x.object_id, {'label' => 'b_then' })
      x.accept(self)
    }
  end
  if x = o.b_else
    @links << Link.new(o.object_id, x.object_id, {'label' => 'b_else' })
    x.accept(self)
  end
end