Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012, 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 class CodeBuffer implements StringBuffer { | |
|
floitsch
2012/07/16 09:55:43
Please implement the 'addAll' method, too.
| |
| 6 StringBuffer buffer; | |
| 7 List<SourceLocation> sourceLocations; | |
| 8 int lastBufferOffset = 0; | |
| 9 | |
| 10 CodeBuffer() | |
| 11 : buffer = new StringBuffer(), | |
| 12 sourceLocations = new List<SourceLocation>(); | |
| 13 | |
| 14 int get length() { | |
|
sra1
2012/07/16 20:00:49
nit: int get length() => buffer.length;
| |
| 15 return buffer.length; | |
| 16 } | |
| 17 | |
| 18 bool isEmpty() { | |
| 19 return buffer.isEmpty(); | |
| 20 } | |
| 21 | |
| 22 /** | |
| 23 * Converts [object] to a string and adds it to the buffer. If [object] is a | |
| 24 * [CodeBuffer], adds its source locations to [sourceLocations]. | |
| 25 */ | |
| 26 CodeBuffer add(var object) { | |
| 27 if (object is CodeBuffer) { | |
| 28 return addBuffer(object); | |
| 29 } | |
| 30 buffer.add(object.toString()); | |
| 31 return this; | |
| 32 } | |
| 33 | |
| 34 CodeBuffer addBuffer(CodeBuffer other) { | |
| 35 if (other.sourceLocations.length > 0) { | |
| 36 SourceLocation firstMapping = other.sourceLocations[0]; | |
| 37 int offsetDelta = | |
| 38 buffer.length + firstMapping.offsetDelta - lastBufferOffset; | |
| 39 sourceLocations.add(new SourceLocation(firstMapping.element, | |
| 40 firstMapping.token, | |
| 41 offsetDelta)); | |
| 42 for (int i = 1; i < other.sourceLocations.length; ++i) { | |
| 43 sourceLocations.add(other.sourceLocations[i]); | |
| 44 } | |
| 45 lastBufferOffset = buffer.length + other.lastBufferOffset; | |
| 46 } | |
| 47 buffer.add(other.toString()); | |
| 48 } | |
| 49 | |
| 50 CodeBuffer addCharCode(int charCode) { | |
| 51 return add(new String.fromCharCodes([charCode])); | |
| 52 } | |
| 53 | |
| 54 CodeBuffer clear() { | |
| 55 buffer.clear(); | |
| 56 sourceLocations.clear(); | |
| 57 lastBufferOffset = 0; | |
| 58 return this; | |
| 59 } | |
| 60 | |
| 61 String toString() { | |
| 62 return buffer.toString(); | |
| 63 } | |
| 64 | |
| 65 void setSourceLocation(Element element, Token token) { | |
|
ahe
2012/08/02 19:31:55
I mentioned in a comment in another CL that it loo
| |
| 66 int offsetDelta = buffer.length - lastBufferOffset; | |
| 67 sourceLocations.add(new SourceLocation(element, token, offsetDelta)); | |
| 68 lastBufferOffset = buffer.length; | |
| 69 } | |
| 70 | |
| 71 void forEachSourceLocation(void f(Element element, Token token, int offset)) { | |
| 72 int offset = 0; | |
| 73 sourceLocations.forEach((sourceLocation) { | |
| 74 offset += sourceLocation.offsetDelta; | |
| 75 f(sourceLocation.element, sourceLocation.token, offset); | |
| 76 }); | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 class SourceLocation { | |
| 81 Element element; | |
|
sra1
2012/07/16 20:00:49
final?
| |
| 82 Token token; | |
| 83 int offsetDelta; | |
| 84 SourceLocation(this.element, this.token, this.offsetDelta); | |
| 85 } | |
| OLD | NEW |