OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart2js.util; | 5 part of dart2js.util; |
6 | 6 |
7 /// Indentation utility class. Should be used as a mixin in most cases. | 7 /// Indentation utility class. Should be used as a mixin in most cases. |
8 class Indentation { | 8 class Indentation { |
9 /// The current indentation string. | 9 /// The current indentation string. |
10 String get indentation { | 10 String get indentation { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 | 44 |
45 /// Calls [f] with one more indentation level, restoring indentation context | 45 /// Calls [f] with one more indentation level, restoring indentation context |
46 /// upon return of [f] and returning its result. | 46 /// upon return of [f] and returning its result. |
47 indentBlock(Function f) { | 47 indentBlock(Function f) { |
48 indentMore(); | 48 indentMore(); |
49 var result = f(); | 49 var result = f(); |
50 indentLess(); | 50 indentLess(); |
51 return result; | 51 return result; |
52 } | 52 } |
53 } | 53 } |
| 54 |
| 55 abstract class Tagging<N> implements Indentation { |
| 56 |
| 57 StringBuffer sb = new StringBuffer(); |
| 58 Link<String> tagStack = const Link<String>(); |
| 59 |
| 60 void pushTag(String tag) { |
| 61 tagStack = tagStack.prepend(tag); |
| 62 indentMore(); |
| 63 } |
| 64 |
| 65 String popTag() { |
| 66 assert(!tagStack.isEmpty); |
| 67 String tag = tagStack.head; |
| 68 tagStack = tagStack.tail; |
| 69 indentLess(); |
| 70 return tag; |
| 71 } |
| 72 |
| 73 /** |
| 74 * Adds given string to result string. |
| 75 */ |
| 76 void add(String string) { |
| 77 sb.write(string); |
| 78 } |
| 79 |
| 80 /// Adds default parameters for [node] into [params]. |
| 81 void addDefaultParameters(N node, Map params) {} |
| 82 |
| 83 /** |
| 84 * Adds given node type to result string. |
| 85 * The method "opens" the node, meaning that all output after calling |
| 86 * this method and before calling closeNode() will represent contents |
| 87 * of given node. |
| 88 */ |
| 89 void openNode(N node, String type, [Map params]) { |
| 90 if (params == null) params = new Map(); |
| 91 addCurrentIndent(); |
| 92 sb.write("<"); |
| 93 addDefaultParameters(node, params); |
| 94 addTypeWithParams(type, params); |
| 95 sb.write(">\n"); |
| 96 pushTag(type); |
| 97 } |
| 98 |
| 99 /** |
| 100 * Adds given node to result string. |
| 101 */ |
| 102 void openAndCloseNode(N node, String type, [Map params]) { |
| 103 if (params == null) params = {}; |
| 104 addCurrentIndent(); |
| 105 sb.write("<"); |
| 106 addDefaultParameters(node, params); |
| 107 addTypeWithParams(type, params); |
| 108 sb.write("/>\n"); |
| 109 } |
| 110 |
| 111 /** |
| 112 * Closes current node type. |
| 113 */ |
| 114 void closeNode() { |
| 115 String tag = popTag(); |
| 116 addCurrentIndent(); |
| 117 sb.write("</"); |
| 118 addTypeWithParams(tag); |
| 119 sb.write(">\n"); |
| 120 } |
| 121 |
| 122 void addTypeWithParams(String type, [Map params]) { |
| 123 if (params == null) params = new Map(); |
| 124 sb.write("${type}"); |
| 125 params.forEach((k, v) { |
| 126 String value; |
| 127 if (v != null) { |
| 128 String str = valueToString(v); |
| 129 value = str |
| 130 .replaceAll("<", "<") |
| 131 .replaceAll(">", ">") |
| 132 .replaceAll('"', "'"); |
| 133 } else { |
| 134 value = "[null]"; |
| 135 } |
| 136 sb.write(' $k="$value"'); |
| 137 }); |
| 138 } |
| 139 |
| 140 void addCurrentIndent() { |
| 141 sb.write(indentation); |
| 142 } |
| 143 |
| 144 /// Converts a parameter value into a string. |
| 145 String valueToString(var value) => value; |
| 146 } |
OLD | NEW |