Index: lib/compiler/implementation/code_buffer.dart |
diff --git a/lib/compiler/implementation/code_buffer.dart b/lib/compiler/implementation/code_buffer.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a4141c88a860ecc93ced82678524fc89c1979a75 |
--- /dev/null |
+++ b/lib/compiler/implementation/code_buffer.dart |
@@ -0,0 +1,85 @@ |
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+class CodeBuffer implements StringBuffer { |
floitsch
2012/07/16 09:55:43
Please implement the 'addAll' method, too.
|
+ StringBuffer buffer; |
+ List<SourceLocation> sourceLocations; |
+ int lastBufferOffset = 0; |
+ |
+ CodeBuffer() |
+ : buffer = new StringBuffer(), |
+ sourceLocations = new List<SourceLocation>(); |
+ |
+ int get length() { |
sra1
2012/07/16 20:00:49
nit: int get length() => buffer.length;
|
+ return buffer.length; |
+ } |
+ |
+ bool isEmpty() { |
+ return buffer.isEmpty(); |
+ } |
+ |
+ /** |
+ * Converts [object] to a string and adds it to the buffer. If [object] is a |
+ * [CodeBuffer], adds its source locations to [sourceLocations]. |
+ */ |
+ CodeBuffer add(var object) { |
+ if (object is CodeBuffer) { |
+ return addBuffer(object); |
+ } |
+ buffer.add(object.toString()); |
+ return this; |
+ } |
+ |
+ CodeBuffer addBuffer(CodeBuffer other) { |
+ if (other.sourceLocations.length > 0) { |
+ SourceLocation firstMapping = other.sourceLocations[0]; |
+ int offsetDelta = |
+ buffer.length + firstMapping.offsetDelta - lastBufferOffset; |
+ sourceLocations.add(new SourceLocation(firstMapping.element, |
+ firstMapping.token, |
+ offsetDelta)); |
+ for (int i = 1; i < other.sourceLocations.length; ++i) { |
+ sourceLocations.add(other.sourceLocations[i]); |
+ } |
+ lastBufferOffset = buffer.length + other.lastBufferOffset; |
+ } |
+ buffer.add(other.toString()); |
+ } |
+ |
+ CodeBuffer addCharCode(int charCode) { |
+ return add(new String.fromCharCodes([charCode])); |
+ } |
+ |
+ CodeBuffer clear() { |
+ buffer.clear(); |
+ sourceLocations.clear(); |
+ lastBufferOffset = 0; |
+ return this; |
+ } |
+ |
+ String toString() { |
+ return buffer.toString(); |
+ } |
+ |
+ void setSourceLocation(Element element, Token token) { |
ahe
2012/08/02 19:31:55
I mentioned in a comment in another CL that it loo
|
+ int offsetDelta = buffer.length - lastBufferOffset; |
+ sourceLocations.add(new SourceLocation(element, token, offsetDelta)); |
+ lastBufferOffset = buffer.length; |
+ } |
+ |
+ void forEachSourceLocation(void f(Element element, Token token, int offset)) { |
+ int offset = 0; |
+ sourceLocations.forEach((sourceLocation) { |
+ offset += sourceLocation.offsetDelta; |
+ f(sourceLocation.element, sourceLocation.token, offset); |
+ }); |
+ } |
+} |
+ |
+class SourceLocation { |
+ Element element; |
sra1
2012/07/16 20:00:49
final?
|
+ Token token; |
+ int offsetDelta; |
+ SourceLocation(this.element, this.token, this.offsetDelta); |
+} |