Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(500)

Side by Side Diff: compiler/java/com/google/dart/compiler/ast/viz/ConsoleWriter.java

Issue 8566019: Added support for dumping AST to console, text file or dot file (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: '' Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 package com.google.dart.compiler.ast.viz;
6
7 import java.io.IOException;
8 import java.io.OutputStreamWriter;
9 import java.util.HashMap;
10 import java.util.Map;
11
12 import com.google.dart.compiler.ast.DartNode;
13 import com.google.dart.compiler.ast.DartUnit;
14
15 /**
16 * Write the AST in to System out.
17 */
18 public class ConsoleWriter extends BaseASTWriter {
19 Map<DartNode,Integer> indentMap;
20 OutputStreamWriter out;
21
22 public ConsoleWriter(String outputDir) {
23 super(outputDir);
24 this.indentMap = new HashMap<DartNode, Integer>();
25 out = new OutputStreamWriter(System.out);
26 }
27
28 @Override
29 protected void startHook() {
30 // Do nothing
31 }
32
33 @Override
34 protected void endHook() {
zundel 2011/11/15 14:49:54 Guava has a shortcut for closing streams like this
shauvik 2011/11/15 23:53:14 Cool! On 2011/11/15 14:49:54, zundel wrote:
shauvik 2011/11/15 23:53:14 Done.
35 try {
36 out.flush();
37 out.close();
38 } catch (IOException e) {
39 System.err.println("Error flushing AST output stream");
40 e.printStackTrace();
41 }
42 }
43
44 public void write(String nodeType, DartNode node){
45 write(nodeType, node, "");
46 }
47
48 public void write(String nodeType, DartNode node, String data) {
49 StringBuffer sb = new StringBuffer();
50 int indent = 0;
51 DartNode parent = node.getParent();
52 if(parent != null){
53 indent = this.indentMap.get(parent) + 1;
54 }
55 for(int i=0;i<indent;i++) {
56 sb.append(TAB);
57 }
58 try{
59 out.write(sb.toString());
60 out.write(nodeType);
61 if(!data.equals("")){
62 out.write(" ("+data+")");
zundel 2011/11/15 14:49:54 style: spaces before and after +
shauvik 2011/11/15 23:53:14 Done.
63 }
64 out.write('\n');
65 }catch (IOException e) {
66
67 }
68 }
69
70 @Override
71 public void visitChildren(DartNode node) {
72 //Setup Indentation
zundel 2011/11/15 14:49:54 style: space after //
shauvik 2011/11/15 23:53:14 Done.
73 DartNode parent = node.getParent();
74 if(parent == null){ //DartUnit's parent is null
75 this.indentMap.put(node, 0);
76 }else{
zundel 2011/11/15 14:49:54 style: } else {
shauvik 2011/11/15 23:53:14 Done.
77 int parentIndent = this.indentMap.get(parent);
78 this.indentMap.put(node, parentIndent+1);
79 }
80
81 node.visitChildren(this);
82 }
83
84 @Override
85 public Object visitUnit(DartUnit node) {
86 if(!isIgnored(node)){
87 visitChildren(node);
88 }
89 return null;
90 }
91 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698