| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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 class CodeBuffer implements StringBuffer { | 5 class CodeBuffer implements StringBuffer { |
| 6 StringBuffer buffer; | 6 StringBuffer buffer; |
| 7 List<SourceLocation> sourceLocations; | 7 List<SourceLocation> sourceLocations; |
| 8 int lastBufferOffset = 0; | 8 int lastBufferOffset = 0; |
| 9 int mappedRangeCounter = 0; |
| 9 | 10 |
| 10 CodeBuffer() | 11 CodeBuffer() |
| 11 : buffer = new StringBuffer(), | 12 : buffer = new StringBuffer(), |
| 12 sourceLocations = new List<SourceLocation>(); | 13 sourceLocations = new List<SourceLocation>(); |
| 13 | 14 |
| 14 int get length => buffer.length; | 15 int get length => buffer.length; |
| 15 | 16 |
| 16 bool isEmpty() { | 17 bool isEmpty() { |
| 17 return buffer.isEmpty(); | 18 return buffer.isEmpty(); |
| 18 } | 19 } |
| 19 | 20 |
| 20 /** | 21 /** |
| 21 * Converts [object] to a string and adds it to the buffer. If [object] is a | 22 * Converts [object] to a string and adds it to the buffer. If [object] is a |
| 22 * [CodeBuffer], adds its source locations to [sourceLocations]. | 23 * [CodeBuffer], adds its source locations to [sourceLocations]. |
| 23 */ | 24 */ |
| 24 CodeBuffer add(var object) { | 25 CodeBuffer add(var object) { |
| 25 if (object is CodeBuffer) { | 26 if (object is CodeBuffer) { |
| 26 return addBuffer(object); | 27 return addBuffer(object); |
| 27 } | 28 } |
| 29 if (mappedRangeCounter == 0) setSourceLocation(null, null); |
| 28 buffer.add(object.toString()); | 30 buffer.add(object.toString()); |
| 29 return this; | 31 return this; |
| 30 } | 32 } |
| 31 | 33 |
| 32 CodeBuffer addBuffer(CodeBuffer other) { | 34 CodeBuffer addBuffer(CodeBuffer other) { |
| 33 if (other.sourceLocations.length > 0) { | 35 if (other.sourceLocations.length > 0) { |
| 34 SourceLocation firstMapping = other.sourceLocations[0]; | 36 SourceLocation firstMapping = other.sourceLocations[0]; |
| 35 int offsetDelta = | 37 int offsetDelta = |
| 36 buffer.length + firstMapping.offsetDelta - lastBufferOffset; | 38 buffer.length + firstMapping.offsetDelta - lastBufferOffset; |
| 37 sourceLocations.add(new SourceLocation(firstMapping.element, | 39 sourceLocations.add(new SourceLocation(firstMapping.element, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 60 buffer.clear(); | 62 buffer.clear(); |
| 61 sourceLocations.clear(); | 63 sourceLocations.clear(); |
| 62 lastBufferOffset = 0; | 64 lastBufferOffset = 0; |
| 63 return this; | 65 return this; |
| 64 } | 66 } |
| 65 | 67 |
| 66 String toString() { | 68 String toString() { |
| 67 return buffer.toString(); | 69 return buffer.toString(); |
| 68 } | 70 } |
| 69 | 71 |
| 72 void beginMappedRange() { |
| 73 ++mappedRangeCounter; |
| 74 } |
| 75 |
| 76 void endMappedRange() { |
| 77 assert(mappedRangeCounter > 0); |
| 78 --mappedRangeCounter; |
| 79 } |
| 80 |
| 70 void setSourceLocation(Element element, Token token) { | 81 void setSourceLocation(Element element, Token token) { |
| 71 int offsetDelta = buffer.length - lastBufferOffset; | 82 int offsetDelta = buffer.length - lastBufferOffset; |
| 72 sourceLocations.add(new SourceLocation(element, token, offsetDelta)); | 83 sourceLocations.add(new SourceLocation(element, token, offsetDelta)); |
| 73 lastBufferOffset = buffer.length; | 84 lastBufferOffset = buffer.length; |
| 74 } | 85 } |
| 75 | 86 |
| 76 void forEachSourceLocation(void f(Element element, Token token, int offset)) { | 87 void forEachSourceLocation(void f(Element element, Token token, int offset)) { |
| 77 int offset = 0; | 88 int offset = 0; |
| 78 sourceLocations.forEach((sourceLocation) { | 89 sourceLocations.forEach((sourceLocation) { |
| 79 offset += sourceLocation.offsetDelta; | 90 offset += sourceLocation.offsetDelta; |
| 80 f(sourceLocation.element, sourceLocation.token, offset); | 91 f(sourceLocation.element, sourceLocation.token, offset); |
| 81 }); | 92 }); |
| 82 } | 93 } |
| 83 } | 94 } |
| 84 | 95 |
| 85 class SourceLocation { | 96 class SourceLocation { |
| 86 final Element element; | 97 final Element element; |
| 87 final Token token; | 98 final Token token; |
| 88 final int offsetDelta; | 99 final int offsetDelta; |
| 89 SourceLocation(this.element, this.token, this.offsetDelta); | 100 SourceLocation(this.element, this.token, this.offsetDelta); |
| 90 } | 101 } |
| OLD | NEW |