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 /** | |
6 * Support for generating code to be extended with source location mapping. | |
7 */ | |
8 class CodeWriter { | |
9 static final INDENTATION = ' '; | |
10 | |
11 static final int INC_INDENT = +1; | |
12 static final int DEC_INDENT = -1; | |
13 static final NEWLINE = null; // Anything but an int, String or List. | |
14 | |
15 List _buf; | |
16 bool writeComments; | |
17 | |
18 CodeWriter(): _buf = [], writeComments = options.emitCodeComments; | |
19 | |
20 bool get isEmpty() => _buf.length == 0; | |
21 | |
22 String get text() { | |
23 StringBuffer sb = new StringBuffer(); | |
24 int indentation = 0; | |
25 bool pendingIndent = false; | |
26 void _walk(list) { | |
27 for (var thing in list) { | |
28 if (thing is String) { | |
29 if (pendingIndent) { | |
30 for (int i = 0; i < indentation; i++) { | |
31 sb.add(INDENTATION); | |
32 } | |
33 pendingIndent = false; | |
34 } | |
35 sb.add(thing); | |
36 } else if (thing === NEWLINE) { | |
37 sb.add('\n'); | |
38 pendingIndent = true; | |
39 } else if (thing is int) { | |
40 indentation += thing; | |
41 } else if (thing is CodeWriter) { | |
42 _walk(thing._buf); | |
43 } | |
44 } | |
45 } | |
46 _walk(_buf); | |
47 return sb.toString(); | |
48 } | |
49 | |
50 /** Returns a CodeWriter that writes at the current position. */ | |
51 CodeWriter subWriter() { | |
52 CodeWriter sub = new CodeWriter(); | |
53 sub.writeComments = writeComments; | |
54 _buf.add(sub); // Splice subwriter's output into this parent writer. | |
55 return sub; | |
56 } | |
57 | |
58 comment(String text) { | |
59 if (writeComments) { | |
60 writeln(text); | |
61 } | |
62 } | |
63 | |
64 _writeFragment(String text) { | |
65 if (text.length == 0) return; | |
66 _buf.add(text); | |
67 } | |
68 | |
69 write(String text) { | |
70 if (text.length == 0) return; | |
71 | |
72 // TODO(jimhug): Check perf consequences of this split. | |
73 if (text.indexOf('\n') != -1) { | |
74 var lines = text.split('\n'); | |
75 _writeFragment(lines[0]); | |
76 for (int i = 1; i < lines.length; i++) { | |
77 _buf.add(NEWLINE); | |
78 _writeFragment(lines[i]); | |
79 } | |
80 } else { | |
81 _buf.add(text); | |
82 } | |
83 } | |
84 | |
85 writeln([String text = null]) { | |
86 if (text == null) { | |
87 _buf.add(NEWLINE); | |
88 } else { | |
89 write(text); | |
90 if (!text.endsWith('\n')) _buf.add(NEWLINE); | |
91 } | |
92 } | |
93 | |
94 enterBlock(String text) { | |
95 writeln(text); | |
96 _buf.add(INC_INDENT); | |
97 } | |
98 | |
99 exitBlock(String text) { | |
100 _buf.add(DEC_INDENT); | |
101 writeln(text); | |
102 } | |
103 | |
104 /** Switch to an adjacent block in one line, e.g. "} else if (...) {" */ | |
105 nextBlock(String text) { | |
106 _buf.add(DEC_INDENT); | |
107 writeln(text); | |
108 _buf.add(INC_INDENT); | |
109 } | |
110 } | |
OLD | NEW |