Index: compiler/java/com/google/dart/compiler/ast/viz/DotWriter.java |
=================================================================== |
--- compiler/java/com/google/dart/compiler/ast/viz/DotWriter.java (revision 0) |
+++ compiler/java/com/google/dart/compiler/ast/viz/DotWriter.java (revision 0) |
@@ -0,0 +1,118 @@ |
+// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+package com.google.dart.compiler.ast.viz; |
+ |
+import java.io.File; |
+import java.io.FileWriter; |
+import java.io.IOException; |
+import java.util.HashMap; |
+import java.util.Map; |
+ |
+import com.google.dart.compiler.ast.DartNode; |
+import com.google.dart.compiler.ast.DartUnit; |
+ |
+/** |
+ * Write the AST in Dot format. Output file is placed |
+ * next to the JS file in the output directory |
+ */ |
+public class DotWriter extends BaseASTWriter{ |
+ |
+ Map<DartNode,String> nodeMap; |
zundel
2011/11/15 14:49:54
make final
shauvik
2011/11/15 23:53:14
Can't make final. I am reusing this class for all
|
+ FileWriter out; |
+ StringBuffer edges = new StringBuffer(); |
zundel
2011/11/15 14:49:54
make final
shauvik
2011/11/15 23:53:14
same as above
On 2011/11/15 14:49:54, zundel wrot
|
+ StringBuffer nodes = new StringBuffer(); |
zundel
2011/11/15 14:49:54
make final
shauvik
2011/11/15 23:53:14
same as above
On 2011/11/15 14:49:54, zundel wrot
|
+ |
+ public DotWriter(String outputDir) { |
+ super(outputDir); |
+ this.nodeMap = new HashMap<DartNode, String>(); |
+ } |
+ |
+ @Override |
+ protected void startHook() { |
+ String nodeData = String.format("%s", currentUnit.getSourceName()); |
+ nodeMap.put(this.currentUnit, nodeData); |
+ } |
+ |
+ @Override |
+ protected void endHook() { |
+ if(edges.length() > 0){ |
+ String dotGraph = "digraph G{\n" +nodes.toString() |
+ + edges.toString() + "}"; |
+ try { |
+ out.append(dotGraph); |
+ out.flush(); |
+ out.close(); |
+ } catch (IOException e) { |
+ System.err.println("Error while writing AST to dot file"); |
+ e.printStackTrace(); |
+ } |
+ } |
+ } |
+ |
+ public void write(String nodeType, DartNode node){ |
+ write(nodeType, node, ""); |
+ } |
+ |
+ public void write(String nodeType, DartNode node, String data) { |
+ String nodeData = node.getSourceLine() + ":"+node.getSourceStart() |
+ + ":" +node.getSourceLength() + "_" + nodeType; |
+ this.nodeMap.put(node, nodeData); |
+ DartNode parent = node.getParent(); |
+ if(parent == null){ |
+ parent = this.currentUnit; |
+ } |
+ String styleAttr = getStyleAttr(nodeType, node); |
+ String label = getLabel(nodeType,data); |
+ nodes.append(String.format("\"%s\" [label=\"%s\"%s];\n", |
+ nodeData, label, styleAttr)); |
+ edges.append(String.format("\"%s\" -> \"%s\";\n", |
+ nodeMap.get(parent), nodeData)); |
+ } |
+ |
+ private String getLabel(String nodeType, String data) { |
+ StringBuffer label = new StringBuffer(nodeType); |
+ if(nodeType.endsWith("Literal") || "DartIdentifier".equals(nodeType) |
+ || "DartClass".equals(nodeType) || "DartMethodDefinition".equals(nodeType) |
+ || "DartVariable".equals(nodeType) || "DartField".equals(nodeType) |
+ || "DartParameter".equals(nodeType) || nodeType.endsWith("Expression")) { |
+ label.append(String.format("\\n%s",data.replaceAll("\"", "'"))); |
zundel
2011/11/15 14:49:54
space after ,
shauvik
2011/11/15 23:53:14
Done.
|
+ } |
+ return label.toString(); |
+ } |
+ |
+ private String getStyleAttr(String nodeType, DartNode node) { |
+ StringBuffer style = new StringBuffer(); |
+ if(nodeType.endsWith("Literal") || "DartIdentifier".equals(nodeType)) { |
+ style.append(", shape=box"); // OR style=filled, color=yellow |
+ }else if("DartClass".equals(nodeType)) { |
+ style.append(", shape=doubleoctagon"); |
+ }else if("DartMethodDefinition".equals(nodeType)) { |
+ style.append(", color=blue"); |
+ } |
+ return style.toString(); |
+ } |
+ |
+ @Override |
+ public void visitChildren(DartNode node) { |
zundel
2011/11/15 14:49:54
is this override necessary?
shauvik
2011/11/15 23:53:14
Done.
|
+ node.visitChildren(this); |
+ } |
+ |
+ @Override |
+ public Object visitUnit(DartUnit node) { |
+ if(!isIgnored(node)){ |
+ String dotFilePath = outputDir + File.separator |
+ + currentUnit.getSource().getUri() + ".ast.dot"; |
+ makeParentDirs(dotFilePath); |
+ try { |
+ out = new FileWriter(new File(dotFilePath)); |
+ } catch (IOException e) { |
+ e.printStackTrace(); |
+ } |
+ visitChildren(node); |
+ } |
+ return null; |
+ } |
+ |
+} |