Class: Crokus::AstPrinter
- Inherits:
-
Object
- Object
- Crokus::AstPrinter
- Includes:
- Indent
- Defined in:
- lib/crokus/ast_printer.rb
Constant Summary
Constants included from Indent
Instance Attribute Summary collapse
-
#code ⇒ Object
Returns the value of attribute code.
-
#nodes_cnx ⇒ Object
Returns the value of attribute nodes_cnx.
-
#nodes_decl ⇒ Object
Returns the value of attribute nodes_decl.
Instance Method Summary collapse
-
#clean(code) ⇒ Object
suppress syndrom : name=“”not correct“”.
-
#print(ast) ⇒ Object
entry method.
- #process(node, level = 0) ⇒ Object
Methods included from Indent
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
12 13 14 |
# File 'lib/crokus/ast_printer.rb', line 12 def code @code end |
#nodes_cnx ⇒ Object
Returns the value of attribute nodes_cnx.
12 13 14 |
# File 'lib/crokus/ast_printer.rb', line 12 def nodes_cnx @nodes_cnx end |
#nodes_decl ⇒ Object
Returns the value of attribute nodes_decl.
12 13 14 |
# File 'lib/crokus/ast_printer.rb', line 12 def nodes_decl @nodes_decl end |
Instance Method Details
#clean(code) ⇒ Object
suppress syndrom : name=“”not correct“”
75 76 77 78 79 80 81 82 |
# File 'lib/crokus/ast_printer.rb', line 75 def clean code #puts "=> cleaning dot code" code.lines.each_with_index do |line,idx| if line=~/\"\"(.*)\"\"/ code.lines[idx].gsub!(/\"\"(.*)\"\"/,"\"#{$1}\"") end end end |
#print(ast) ⇒ Object
entry method
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/crokus/ast_printer.rb', line 14 def print ast #entry method @verbose=false @nodes_decl=Code.new @nodes_cnx=Code.new @printed_cnx={} #Cosmetic ! to keep track of already printed cnx source->sink @code=Code.new code << "digraph G {" code.indent=2 code << "ordering=out;" code << "ranksep=.4;" code << "bgcolor=\"lightgrey\";" code.newline code << "node [shape=box, fixedsize=false, fontsize=12, fontname=\"Helvetica-bold\", fontcolor=\"blue\"" code << " width=.25, height=.25, color=\"black\", fillcolor=\"white\", style=\"filled, solid, bold\"];" code << "edge [arrowsize=.5, color=\"black\", style=\"bold\"]" process(ast) code << @nodes_decl code << @nodes_cnx code.indent=0 code << "}" clean(code) return code end |
#process(node, level = 0) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/crokus/ast_printer.rb', line 38 def process node,level=0 #puts "processing #{node}" kname=node.class.name.split("::")[1] id=node.object_id (nodes_decl << "#{id} [label=\"#{kname}\"]") node.instance_variables.each{|vname| ivar=node.instance_variable_get(vname) vname=vname.to_s[1..-1] if ivar case ivar when Array ivar.each_with_index{|e,idx| sink=process(e,level+2) @printed_cnx[id]||=[] nodes_cnx << "#{id} -> #{sink} [label=\"#{vname}[#{idx}]\"]" if not @printed_cnx[id].include? sink @printed_cnx[id] << sink } when Token val=ivar.val sink="#{ivar.object_id}" nodes_decl << "#{sink} [label=\"#{val}\",color=\"red\"]" @printed_cnx[id]||=[] nodes_cnx << "#{id} -> #{sink} [label=\"#{vname}\"]" if not @printed_cnx[id].include? sink @printed_cnx[id] << sink else sink=process(ivar,level+2) @printed_cnx[id]||=[] nodes_cnx << "#{id} -> #{sink} [label=\"#{vname}\"]" if not @printed_cnx[id].include? sink @printed_cnx[id] << sink end end } return id end |