Class: ANTLR3::DOT::TreeGenerator
- Inherits:
-
Object
- Object
- ANTLR3::DOT::TreeGenerator
- Defined in:
- lib/antlr3/dot.rb
Constant Summary collapse
- TREE_TEMPLATE =
ERB.new( Util.tidy( <<-END ).chomp ) | digraph { | ordering=out; | ranksep=.4; | node [shape=plaintext, fixedsize=true, fontsize=11, fontname="Courier", | width=.25, height=.25]; | edge [arrowsize=.5]; | <%= @nodes.join("\n ") %> | <%= @edges.join("\n ") %> | } END
- NODE_TEMPLATE =
ERB.new( Util.tidy( <<-END ).chomp ) | <%= @name %> [label="<%= @text %>"]; END
- EDGE_TEMPLATE =
ERB.new( Util.tidy( <<-END ).chomp ) | <%= @parent %> -> <%= @child %>; // "<%= @parent_text %>" -> "<%= @child_text %>" END
Class Method Summary collapse
Instance Method Summary collapse
- #define_edges(tree, adaptor, tree_template, edge_template) ⇒ Object
- #define_nodes(tree, adaptor, tree_template, known_nodes = nil) ⇒ Object
-
#initialize ⇒ TreeGenerator
constructor
A new instance of TreeGenerator.
- #node_template_for(adaptor, tree) ⇒ Object
- #to_dot(tree, adaptor = nil, tree_template = TREE_TEMPLATE, edge_template = EDGE_TEMPLATE) ⇒ Object
Constructor Details
#initialize ⇒ TreeGenerator
Returns a new instance of TreeGenerator.
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 |