Files
sdk/lib/compiler/implementation/code_buffer.dart
T
podivilov@google.com 6fd0e7fc8b Add empty mapping entries for the ranges of generated JavaScript that don't map to any Dart source code.
For such ranges we want to show generated JavaScript rather than arbitrary location in Dart sources.

R=floitsch@google.com

Review URL: https://chromiumcodereview.appspot.com//10911091

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@11934 260f80e4-7a28-3924-810f-c04153c831b5
2012-09-06 10:52:41 +00:00

102 lines
2.8 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;
int mappedRangeCounter = 0;
CodeBuffer()
: buffer = new StringBuffer(),
sourceLocations = new List<SourceLocation>();
int get length => 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);
}
if (mappedRangeCounter == 0) setSourceLocation(null, null);
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 addAll(Collection<Object> objects) {
for (Object obj in objects) {
add(obj);
}
return this;
}
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 beginMappedRange() {
++mappedRangeCounter;
}
void endMappedRange() {
assert(mappedRangeCounter > 0);
--mappedRangeCounter;
}
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 {
final Element element;
final Token token;
final int offsetDelta;
SourceLocation(this.element, this.token, this.offsetDelta);
}