| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library source_writer; | 5 library source_writer; |
| 6 | 6 |
| 7 | 7 |
| 8 class Line { | 8 class Line { |
| 9 | 9 |
| 10 final int lineLength; | 10 final int lineLength; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 } | 34 } |
| 35 | 35 |
| 36 bool isWhitespace() => tokens.every((tok) => tok is SpaceToken); | 36 bool isWhitespace() => tokens.every((tok) => tok is SpaceToken); |
| 37 | 37 |
| 38 void _indent(int n) { | 38 void _indent(int n) { |
| 39 tokens.add(useTabs ? new TabToken(n) : new SpaceToken(n * spacesPerIndent)); | 39 tokens.add(useTabs ? new TabToken(n) : new SpaceToken(n * spacesPerIndent)); |
| 40 } | 40 } |
| 41 | 41 |
| 42 String toString() { | 42 String toString() { |
| 43 var buffer = new StringBuffer(); | 43 var buffer = new StringBuffer(); |
| 44 for (var tok in tokens) { | 44 tokens.forEach((tok) => buffer.write(tok.toString())); |
| 45 buffer.write(tok.toString()); | |
| 46 } | |
| 47 return buffer.toString(); | 45 return buffer.toString(); |
| 48 } | 46 } |
| 49 | 47 |
| 50 } | 48 } |
| 51 | 49 |
| 52 | 50 |
| 53 class LineToken { | 51 class LineToken { |
| 54 | 52 |
| 55 final String value; | 53 final String value; |
| 56 | 54 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 77 | 75 |
| 78 | 76 |
| 79 | 77 |
| 80 class SourceWriter { | 78 class SourceWriter { |
| 81 | 79 |
| 82 final StringBuffer buffer = new StringBuffer(); | 80 final StringBuffer buffer = new StringBuffer(); |
| 83 Line currentLine; | 81 Line currentLine; |
| 84 | 82 |
| 85 final String lineSeparator; | 83 final String lineSeparator; |
| 86 int indentCount = 0; | 84 int indentCount = 0; |
| 87 | 85 |
| 86 LineToken _lastToken; |
| 87 |
| 88 SourceWriter({this.indentCount: 0, this.lineSeparator: NEW_LINE}) { | 88 SourceWriter({this.indentCount: 0, this.lineSeparator: NEW_LINE}) { |
| 89 currentLine = new Line(indent: indentCount); | 89 currentLine = new Line(indent: indentCount); |
| 90 } | 90 } |
| 91 | 91 |
| 92 LineToken get lastToken => _lastToken; |
| 93 |
| 94 _addToken(LineToken token) { |
| 95 _lastToken = token; |
| 96 currentLine.addToken(token); |
| 97 } |
| 98 |
| 92 void indent() { | 99 void indent() { |
| 93 ++indentCount; | 100 ++indentCount; |
| 94 } | 101 } |
| 95 | 102 |
| 96 void unindent() { | |
| 97 --indentCount; | |
| 98 } | |
| 99 | |
| 100 void print(x) { | |
| 101 currentLine.addToken(new LineToken(x)); | |
| 102 } | |
| 103 | |
| 104 void newline() { | 103 void newline() { |
| 105 if (currentLine.isWhitespace()) { | 104 if (currentLine.isWhitespace()) { |
| 106 currentLine.tokens.clear(); | 105 currentLine.tokens.clear(); |
| 107 } | 106 } |
| 108 currentLine.addToken(new NewlineToken(this.lineSeparator)); | 107 _addToken(new NewlineToken(this.lineSeparator)); |
| 109 buffer.write(currentLine.toString()); | 108 buffer.write(currentLine.toString()); |
| 110 currentLine = new Line(indent: indentCount); | 109 currentLine = new Line(indent: indentCount); |
| 111 } | 110 } |
| 112 | 111 |
| 113 void newlines(int num) { | 112 void newlines(int num) { |
| 114 while (num-- > 0) { | 113 while (num-- > 0) { |
| 115 newline(); | 114 newline(); |
| 116 } | 115 } |
| 117 } | 116 } |
| 118 | 117 |
| 118 void print(x) { |
| 119 _addToken(new LineToken(x)); |
| 120 } |
| 121 |
| 122 void println(String s) { |
| 123 print(s); |
| 124 newline(); |
| 125 } |
| 126 |
| 119 void space() { | 127 void space() { |
| 120 spaces(1); | 128 spaces(1); |
| 121 } | 129 } |
| 122 | 130 |
| 123 void spaces(n) { | 131 void spaces(n) { |
| 124 currentLine.addSpaces(n); | 132 currentLine.addSpaces(n); |
| 125 } | 133 } |
| 126 | 134 |
| 127 void println(String s) { | 135 void unindent() { |
| 128 print(s); | 136 --indentCount; |
| 129 newline(); | |
| 130 } | 137 } |
| 131 | 138 |
| 132 String toString() { | 139 String toString() { |
| 133 var source = new StringBuffer(buffer.toString()); | 140 var source = new StringBuffer(buffer.toString()); |
| 134 if (!currentLine.isWhitespace()) { | 141 if (!currentLine.isWhitespace()) { |
| 135 source.write(currentLine); | 142 source.write(currentLine); |
| 136 } | 143 } |
| 137 return source.toString(); | 144 return source.toString(); |
| 138 } | 145 } |
| 139 | 146 |
| 140 } | 147 } |
| 141 | 148 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 | 193 |
| 187 String getTabs(int n) => n < TABS.length ? TABS[n] : repeat('\t', n); | 194 String getTabs(int n) => n < TABS.length ? TABS[n] : repeat('\t', n); |
| 188 | 195 |
| 189 String repeat(String ch, int times) { | 196 String repeat(String ch, int times) { |
| 190 var sb = new StringBuffer(); | 197 var sb = new StringBuffer(); |
| 191 for (var i = 0; i < times; ++i) { | 198 for (var i = 0; i < times; ++i) { |
| 192 sb.write(ch); | 199 sb.write(ch); |
| 193 } | 200 } |
| 194 return sb.toString(); | 201 return sb.toString(); |
| 195 } | 202 } |
| OLD | NEW |