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