| 04-24-2007, 08:22 AM | #1 |
Ruby script to extract a call graph into a GraphViz format. Run it on Blizzard.j for an image you'll need google maps style viewing to handle. Code:
#!/usr/local/bin/ruby
#jasscg.rb - generate a graphviz graph
#use: cat file1.j file2.j | ruby jasscg.rb | dot -T png -o dep.png
@calls = {}
keywords = { "if", "elseif" }
STDIN.readlines.each{|l| l.sub(/\/\/.*/,"")}.join("").gsub(/"[^"\\]*(\\.[^"\\]*)*"/,"").split("\n").each {|l|
f = l.slice(/^\s*function\s+\w+/)
if l =~ /^\s*globals/ then
@func = "globals"
@calls[@func] = {} unless @calls.has_key?(@func)
elsif f == nil then
calls = l.scan(/\w+\s*\(/).each {|f| f = f.scan(/\w+/)[0]; @calls[@func][f] = true unless keywords.has_key?(f)}
else
@func = f.slice(/\w+$/)
@calls[@func] = {}
end
}
def draw_node(key)
if not @already_drawn.has_key?(key) then
@already_drawn[key] = true
print key + " [label=\""+key+"\",fillcolor=\"#f8c85c\"];\n"
end
end
def draw()
@already_drawn = {}
print "digraph G {\n"
print " ranksep=1.0;\n"
print "node [stype=filled,fontname=Helvetica,fontsize=10];\n"
@calls.each {|k,v|
draw_node(k)
v.each_key {|dep|
draw_node(dep)
print k + " -> " + dep + "\n"
}
}
print "}\n"
end
draw() |
