Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(953)

Side by Side Diff: pkg/compiler/lib/src/util/indentation.dart

Issue 1196433002: Create and test source mapping for invocations. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Handle the new Name class in the JS Printer. Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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("<", "&lt;")
131 .replaceAll(">", "&gt;")
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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698