Chromium Code Reviews| Index: compiler/java/com/google/dart/compiler/ast/viz/ConsoleWriter.java |
| =================================================================== |
| --- compiler/java/com/google/dart/compiler/ast/viz/ConsoleWriter.java (revision 0) |
| +++ compiler/java/com/google/dart/compiler/ast/viz/ConsoleWriter.java (revision 0) |
| @@ -0,0 +1,91 @@ |
| +// 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.IOException; |
| +import java.io.OutputStreamWriter; |
| +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 to System out. |
| + */ |
| +public class ConsoleWriter extends BaseASTWriter { |
| + Map<DartNode,Integer> indentMap; |
| + OutputStreamWriter out; |
| + |
| + public ConsoleWriter(String outputDir) { |
| + super(outputDir); |
| + this.indentMap = new HashMap<DartNode, Integer>(); |
| + out = new OutputStreamWriter(System.out); |
| + } |
| + |
| + @Override |
| + protected void startHook() { |
| + // Do nothing |
| + } |
| + |
| + @Override |
| + 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.
|
| + try { |
| + out.flush(); |
| + out.close(); |
| + } catch (IOException e) { |
| + System.err.println("Error flushing AST output stream"); |
| + e.printStackTrace(); |
| + } |
| + } |
| + |
| + public void write(String nodeType, DartNode node){ |
| + write(nodeType, node, ""); |
| + } |
| + |
| + public void write(String nodeType, DartNode node, String data) { |
| + StringBuffer sb = new StringBuffer(); |
| + int indent = 0; |
| + DartNode parent = node.getParent(); |
| + if(parent != null){ |
| + indent = this.indentMap.get(parent) + 1; |
| + } |
| + for(int i=0;i<indent;i++) { |
| + sb.append(TAB); |
| + } |
| + try{ |
| + out.write(sb.toString()); |
| + out.write(nodeType); |
| + if(!data.equals("")){ |
| + out.write(" ("+data+")"); |
|
zundel
2011/11/15 14:49:54
style: spaces before and after +
shauvik
2011/11/15 23:53:14
Done.
|
| + } |
| + out.write('\n'); |
| + }catch (IOException e) { |
| + |
| + } |
| + } |
| + |
| + @Override |
| + public void visitChildren(DartNode node) { |
| + //Setup Indentation |
|
zundel
2011/11/15 14:49:54
style: space after //
shauvik
2011/11/15 23:53:14
Done.
|
| + DartNode parent = node.getParent(); |
| + if(parent == null){ //DartUnit's parent is null |
| + this.indentMap.put(node, 0); |
| + }else{ |
|
zundel
2011/11/15 14:49:54
style: } else {
shauvik
2011/11/15 23:53:14
Done.
|
| + int parentIndent = this.indentMap.get(parent); |
| + this.indentMap.put(node, parentIndent+1); |
| + } |
| + |
| + node.visitChildren(this); |
| + } |
| + |
| + @Override |
| + public Object visitUnit(DartUnit node) { |
| + if(!isIgnored(node)){ |
| + visitChildren(node); |
| + } |
| + return null; |
| + } |
| +} |