class ANTLR3::DOT::TreeGenerator

Constants

EDGE_TEMPLATE
NODE_TEMPLATE
TREE_TEMPLATE

Public Class Methods

generate( tree, adaptor = nil, tree_template = TREE_TEMPLATE, edge_template = EDGE_TEMPLATE ) click to toggle source
# 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
new() click to toggle source
# File lib/antlr3/dot.rb, line 97
def initialize
  @node_registry = Hash.new do |map, id|
    map[ id ] = map.length
  end
end

Public Instance Methods

define_edges( tree, adaptor, tree_template, edge_template ) click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source
# 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