OLD | NEW |
(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 * The base type for all nodes in a dart abstract syntax tree. |
| 6 */ |
| 7 class ASTNode { |
| 8 /** The source code this [ASTNode] represents. */ |
| 9 SourceSpan span; |
| 10 |
| 11 ASTNode(this.span) {} |
| 12 |
| 13 /** Classic double-dispatch visitor for implementing passes. */ |
| 14 abstract visit(TreeVisitor visitor); |
| 15 |
| 16 /** A multiline string showing the node and its children. */ |
| 17 String toDebugString() { |
| 18 var to = new TreeOutput(); |
| 19 var tp = new TreePrinter(to); |
| 20 this.visit(tp); |
| 21 return to.buf.toString(); |
| 22 } |
| 23 } |
| 24 |
| 25 // TODO(jimhug): Clean-up and factor out of core. |
| 26 /** Simple class to provide a textual dump of trees for debugging. */ |
| 27 class TreeOutput { |
| 28 int depth; |
| 29 StringBuffer buf; |
| 30 |
| 31 var printer; |
| 32 |
| 33 static void dump(ASTNode node) { |
| 34 var o = new TreeOutput(); |
| 35 node.visit(new TreePrinter(o)); |
| 36 print(o.buf); |
| 37 } |
| 38 |
| 39 TreeOutput(): this.depth = 0, this.buf = new StringBuffer() { |
| 40 } |
| 41 |
| 42 void write(String s) { |
| 43 for (int i=0; i < depth; i++) { |
| 44 buf.add(' '); |
| 45 } |
| 46 buf.add(s); |
| 47 } |
| 48 |
| 49 void writeln(String s) { |
| 50 write(s); |
| 51 buf.add('\n'); |
| 52 } |
| 53 |
| 54 void heading(String name, span) { |
| 55 write(name); |
| 56 buf.add(' (${span.locationText})'); |
| 57 buf.add('\n'); |
| 58 } |
| 59 |
| 60 String toValue(value) { |
| 61 if (value == null) return 'null'; |
| 62 else if (value is Identifier) return value.name; |
| 63 else return value.toString(); |
| 64 } |
| 65 |
| 66 void writeNode(String label, ASTNode node) { |
| 67 write(label + ': '); |
| 68 depth += 1; |
| 69 if (node != null) node.visit(printer); |
| 70 else writeln('null'); |
| 71 depth -= 1; |
| 72 } |
| 73 |
| 74 void writeValue(String label, value) { |
| 75 var v = toValue(value); |
| 76 writeln('${label}: ${v}'); |
| 77 } |
| 78 |
| 79 void writeList(String label, List list) { |
| 80 write(label + ': '); |
| 81 if (list == null) { |
| 82 buf.add('null'); |
| 83 buf.add('\n'); |
| 84 } else { |
| 85 for (var item in list) { |
| 86 buf.add(item.toString()); |
| 87 buf.add(', '); |
| 88 } |
| 89 buf.add('\n'); |
| 90 } |
| 91 } |
| 92 |
| 93 void writeNodeList(String label, List list) { |
| 94 writeln('${label} ['); |
| 95 if (list != null) { |
| 96 depth += 1; |
| 97 for (var node in list) { |
| 98 if (node != null) { |
| 99 node.visit(printer); |
| 100 } else { |
| 101 writeln('null'); |
| 102 } |
| 103 } |
| 104 depth -= 1; |
| 105 writeln(']'); |
| 106 } |
| 107 } |
| 108 } |
OLD | NEW |