Class: ANTLR3::DOT::TreeGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/antlr3/dot.rb

Constant Summary collapse

TREE_TEMPLATE =
ERB.new( Util.tidy( "| digraph {\n|   ordering=out;\n|   ranksep=.4;\n|   node [shape=plaintext, fixedsize=true, fontsize=11, fontname=\"Courier\",\n|         width=.25, height=.25];\n|   edge [arrowsize=.5];\n|   <%= @nodes.join(\"\\n  \") %>\n|   <%= @edges.join(\"\\n  \") %>\n| }\n" ).chomp )
NODE_TEMPLATE =
ERB.new( Util.tidy( "| <%= @name %> [label=\"<%= @text %>\"];\n" ).chomp )
EDGE_TEMPLATE =
ERB.new( Util.tidy( "| <%= @parent %> -> <%= @child %>; // \"<%= @parent_text %>\" -> \"<%= @child_text %>\"\n" ).chomp )

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTreeGenerator


97
98
99
100
101
# File 'lib/antlr3/dot.rb', line 97

def initialize
  @node_registry = Hash.new do |map, id|
    map[ id ] = map.length
  end
end

Class Method Details

.generate(tree, adaptor = nil, tree_template = TREE_TEMPLATE, edge_template = EDGE_TEMPLATE) ⇒ Object


92
93
94
95
# File 'lib/antlr3/dot.rb', line 92

def self.generate( tree, adaptor = nil, tree_template = TREE_TEMPLATE,
                  edge_template = EDGE_TEMPLATE )
  new.to_dot( tree, adaptor, tree_template, edge_template )
end

Instance Method Details

#define_edges(tree, adaptor, tree_template, edge_template) ⇒ Object


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/antlr3/dot.rb', line 139

def define_edges( tree, adaptor, tree_template, edge_template )
  tree.nil? or adaptor.empty?( tree ) and return
  
  parent_name = 'n%i' % @node_registry[ tree.__id__ ]
  parent_text = adaptor.text_of( tree )
  
  adaptor.each_child( tree ) do | child |
    child_text = adaptor.text_of( child )
    child_name = 'n%i' % @node_registry[ child.__id__ ]
    
    edge = Context.new( edge_template,
      :parent => parent_name, :child => child_name,
      :parent_text => parent_text, :child_text => child_text
    )
    tree_template[ :edges ] << edge
    define_edges( child, adaptor, tree_template, edge_template )
  end
end

#define_nodes(tree, adaptor, tree_template, known_nodes = nil) ⇒ Object


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/antlr3/dot.rb', line 115

def define_nodes( tree, adaptor, tree_template, known_nodes = nil )
  known_nodes ||= Set.new
  tree.nil? and return
  
  adaptor.empty?( tree ) and return
  
  number = @node_registry[ tree.__id__ ]
  unless known_nodes.include?( number )
    parent_node_template = node_template_for( adaptor, tree )
    tree_template[ :nodes ] << parent_node_template
    known_nodes.add( number )
  end
  
  adaptor.each_child( tree ) do | child |
    number = @node_registry[ child.__id__ ]
    unless known_nodes.include?( number )
      node_template = node_template_for( adaptor, child )
      tree_template[ :nodes ] << node_template
      known_nodes.add( number )
    end
    define_nodes( child, adaptor, tree_template, known_nodes )
  end
end

#node_template_for(adaptor, tree) ⇒ Object


158
159
160
161
162
163
164
165
166
# File 'lib/antlr3/dot.rb', line 158

def node_template_for( adaptor, tree )
  text = adaptor.text_of( tree )
  node_template = Context.new( NODE_TEMPLATE )
  unique_name = 'n%i' % @node_registry[ tree.__id__ ]
  node_template[ :name ] = unique_name
  text and text = text.gsub( /"/, '\\"' )
  node_template[ :text ] = text
  return node_template
end

#to_dot(tree, adaptor = nil, tree_template = TREE_TEMPLATE, edge_template = EDGE_TEMPLATE) ⇒ Object


103
104
105
106
107
108
109
110
111
112
113
# File 'lib/antlr3/dot.rb', line 103

def to_dot( tree, adaptor = nil, tree_template = TREE_TEMPLATE,
           edge_template = EDGE_TEMPLATE )
  adaptor ||= AST::CommonTreeAdaptor.new
  @node_registry.clear
  tree_template = Context.new( tree_template, :nodes => [], :edges => [] )
  define_nodes( tree, adaptor, tree_template )
  
  @node_registry.clear
  define_edges( tree, adaptor, tree_template, edge_template )
  return tree_template.to_s
end