Class: Parslet::GraphvizVisitor

Inherits:
Object
  • Object
show all
Defined in:
lib/parslet/graphviz.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(g) ⇒ GraphvizVisitor

Returns a new instance of GraphvizVisitor.


16
17
18
19
20
# File 'lib/parslet/graphviz.rb', line 16

def initialize g
  @graph = g
  @known_links = Set.new
  @visited = Set.new
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent


22
23
24
# File 'lib/parslet/graphviz.rb', line 22

def parent
  @parent
end

Instance Method Details

#downwards(child) ⇒ Object


71
72
73
74
75
76
# File 'lib/parslet/graphviz.rb', line 71

def downwards child
  if @parent && !@known_links.include?([@parent, child])
    @graph.add_edges(@parent, child)
    @known_links << [@parent, child]
  end
end

#escape(str) ⇒ Object


65
66
67
# File 'lib/parslet/graphviz.rb', line 65

def escape str
  str.gsub('"', "'")
end

#node(name, opts = {}) ⇒ Object


68
69
70
# File 'lib/parslet/graphviz.rb', line 68

def node name, opts={}
  @graph.add_nodes name.to_s, opts
end

#recurse(node, current) ⇒ Object


77
78
79
80
# File 'lib/parslet/graphviz.rb', line 77

def recurse node, current
  @parent = current
  node.accept(self)
end

#visit_alternative(alternatives) ⇒ Object


43
44
45
46
47
48
# File 'lib/parslet/graphviz.rb', line 43

def visit_alternative(alternatives)
  p = parent
  alternatives.each do |atom|
    recurse atom, p
  end
end

#visit_entity(name, block) ⇒ Object


27
28
29
30
31
32
33
34
35
36
# File 'lib/parslet/graphviz.rb', line 27

def visit_entity(name, block)
  s = node(name)

  downwards s

  return if @visited.include?(name)
  @visited << name

  recurse block.call, s
end

#visit_lookahead(positive, atom) ⇒ Object


55
56
57
# File 'lib/parslet/graphviz.rb', line 55

def visit_lookahead(positive, atom)
  recurse atom, parent
end

#visit_named(name, atom) ⇒ Object


37
38
39
# File 'lib/parslet/graphviz.rb', line 37

def visit_named(name, atom)
  recurse atom, parent
end

#visit_parser(root) ⇒ Object


24
25
26
# File 'lib/parslet/graphviz.rb', line 24

def visit_parser(root)
  recurse root, node('parser')
end

#visit_re(regexp) ⇒ Object


58
59
60
# File 'lib/parslet/graphviz.rb', line 58

def visit_re(regexp)
  # downwards node(regexp.object_id, label: escape("re(#{regexp.inspect})"))
end

#visit_repetition(tag, min, max, atom) ⇒ Object


40
41
42
# File 'lib/parslet/graphviz.rb', line 40

def visit_repetition(tag, min, max, atom)
  recurse atom, parent
end

#visit_sequence(sequence) ⇒ Object


49
50
51
52
53
54
# File 'lib/parslet/graphviz.rb', line 49

def visit_sequence(sequence)
  p = parent
  sequence.each do |atom|
    recurse atom, p
  end
end

#visit_str(str) ⇒ Object


61
62
63
# File 'lib/parslet/graphviz.rb', line 61

def visit_str(str)
  # downwards node(str.object_id, label: escape("#{str.inspect}"))
end