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 /** The base type for expressions. */ |
| 26 // TODO(terry): Should be abstract class; but frog doesn't support abstract. |
| 27 class Expression extends ASTNode { |
| 28 Expression(SourceSpan span): super(span); |
| 29 |
| 30 visit(TreeVisitor visitor) {} // TODO(terry): remove when abstract. |
| 31 } |
| 32 |
| 33 /** The base type for a reference to a [Type]. */ |
| 34 // TODO(terry): Should be abstract class; but frog doesn't support abstract. |
| 35 class TypeReference extends ASTNode { |
| 36 TypeReference(SourceSpan span): super(span); |
| 37 |
| 38 visit(TreeVisitor visitor) {} // TODO(terry): remove when abstract. |
| 39 } |
| 40 |
| 41 // TODO(jimhug): Clean-up and factor out of core. |
| 42 /** Simple class to provide a textual dump of trees for debugging. */ |
| 43 class TreeOutput { |
| 44 int depth; |
| 45 StringBuffer buf; |
| 46 |
| 47 var printer; |
| 48 |
| 49 static void dump(ASTNode node) { |
| 50 var o = new TreeOutput(); |
| 51 node.visit(new TreePrinter(o)); |
| 52 print(o.buf); |
| 53 } |
| 54 |
| 55 TreeOutput(): this.depth = 0, this.buf = new StringBuffer() { |
| 56 } |
| 57 |
| 58 void write(String s) { |
| 59 for (int i=0; i < depth; i++) { |
| 60 buf.add(' '); |
| 61 } |
| 62 buf.add(s); |
| 63 } |
| 64 |
| 65 void writeln(String s) { |
| 66 write(s); |
| 67 buf.add('\n'); |
| 68 } |
| 69 |
| 70 void heading(String name, span) { |
| 71 write(name); |
| 72 buf.add(' (${span.locationText})'); |
| 73 buf.add('\n'); |
| 74 } |
| 75 |
| 76 String toValue(value) { |
| 77 if (value == null) return 'null'; |
| 78 else if (value is Identifier) return value.name; |
| 79 else return value.toString(); |
| 80 } |
| 81 |
| 82 void writeNode(String label, ASTNode node) { |
| 83 write(label + ': '); |
| 84 depth += 1; |
| 85 if (node != null) node.visit(printer); |
| 86 else writeln('null'); |
| 87 depth -= 1; |
| 88 } |
| 89 |
| 90 void writeValue(String label, value) { |
| 91 var v = toValue(value); |
| 92 writeln('${label}: ${v}'); |
| 93 } |
| 94 |
| 95 void writeList(String label, List list) { |
| 96 write(label + ': '); |
| 97 if (list == null) { |
| 98 buf.add('null'); |
| 99 buf.add('\n'); |
| 100 } else { |
| 101 for (var item in list) { |
| 102 buf.add(item.toString()); |
| 103 buf.add(', '); |
| 104 } |
| 105 buf.add('\n'); |
| 106 } |
| 107 } |
| 108 |
| 109 void writeNodeList(String label, List list) { |
| 110 writeln('${label} ['); |
| 111 if (list != null) { |
| 112 depth += 1; |
| 113 for (var node in list) { |
| 114 if (node != null) { |
| 115 node.visit(printer); |
| 116 } else { |
| 117 writeln('null'); |
| 118 } |
| 119 } |
| 120 depth -= 1; |
| 121 writeln(']'); |
| 122 } |
| 123 } |
| 124 } |
OLD | NEW |