d637d0dce8
CodeBuffer stores source location markers along with emitted JavaScript code. When CodeBuffer content is added to another CodeBuffer, the markers are added as well. CodeBuffers will be used to propagate source mappings from inside ssa codegen to emitter, see https://chromiumcodereview.appspot.com/10695174/ for details. R=floitsch@google.com,kasperl@google.com Review URL: https://chromiumcodereview.appspot.com//10696194 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@9628 260f80e4-7a28-3924-810f-c04153c831b5
86 lines
2.4 KiB
Dart
86 lines
2.4 KiB
Dart
// 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 {
|
|
StringBuffer buffer;
|
|
List<SourceLocation> sourceLocations;
|
|
int lastBufferOffset = 0;
|
|
|
|
CodeBuffer()
|
|
: buffer = new StringBuffer(),
|
|
sourceLocations = new List<SourceLocation>();
|
|
|
|
int get 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) {
|
|
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;
|
|
Token token;
|
|
int offsetDelta;
|
|
SourceLocation(this.element, this.token, this.offsetDelta);
|
|
}
|