Chromium Code Reviews| 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) { | |
|
floitsch
2012/09/05 14:22:18
one line.
podivilov
2012/09/06 10:52:25
Done.
| |
| 30 setSourceLocation(null, null); | |
| 31 } | |
| 28 buffer.add(object.toString()); | 32 buffer.add(object.toString()); |
| 29 return this; | 33 return this; |
| 30 } | 34 } |
| 31 | 35 |
| 32 CodeBuffer addBuffer(CodeBuffer other) { | 36 CodeBuffer addBuffer(CodeBuffer other) { |
| 33 if (other.sourceLocations.length > 0) { | 37 if (other.sourceLocations.length > 0) { |
| 34 SourceLocation firstMapping = other.sourceLocations[0]; | 38 SourceLocation firstMapping = other.sourceLocations[0]; |
| 35 int offsetDelta = | 39 int offsetDelta = |
| 36 buffer.length + firstMapping.offsetDelta - lastBufferOffset; | 40 buffer.length + firstMapping.offsetDelta - lastBufferOffset; |
| 37 sourceLocations.add(new SourceLocation(firstMapping.element, | 41 sourceLocations.add(new SourceLocation(firstMapping.element, |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 60 buffer.clear(); | 64 buffer.clear(); |
| 61 sourceLocations.clear(); | 65 sourceLocations.clear(); |
| 62 lastBufferOffset = 0; | 66 lastBufferOffset = 0; |
| 63 return this; | 67 return this; |
| 64 } | 68 } |
| 65 | 69 |
| 66 String toString() { | 70 String toString() { |
| 67 return buffer.toString(); | 71 return buffer.toString(); |
| 68 } | 72 } |
| 69 | 73 |
| 74 void beginMappedRange() { | |
| 75 mappedRangeCounter += 1; | |
|
floitsch
2012/09/05 14:22:18
++
podivilov
2012/09/06 10:52:25
Done.
| |
| 76 } | |
| 77 | |
| 78 void endMappedRange() { | |
| 79 assert(mappedRangeCounter > 0); | |
| 80 mappedRangeCounter -= 1; | |
|
floitsch
2012/09/05 14:22:18
--
podivilov
2012/09/06 10:52:25
Done.
| |
| 81 } | |
| 82 | |
| 70 void setSourceLocation(Element element, Token token) { | 83 void setSourceLocation(Element element, Token token) { |
| 71 int offsetDelta = buffer.length - lastBufferOffset; | 84 int offsetDelta = buffer.length - lastBufferOffset; |
| 72 sourceLocations.add(new SourceLocation(element, token, offsetDelta)); | 85 sourceLocations.add(new SourceLocation(element, token, offsetDelta)); |
| 73 lastBufferOffset = buffer.length; | 86 lastBufferOffset = buffer.length; |
| 74 } | 87 } |
| 75 | 88 |
| 76 void forEachSourceLocation(void f(Element element, Token token, int offset)) { | 89 void forEachSourceLocation(void f(Element element, Token token, int offset)) { |
| 77 int offset = 0; | 90 int offset = 0; |
| 78 sourceLocations.forEach((sourceLocation) { | 91 sourceLocations.forEach((sourceLocation) { |
| 79 offset += sourceLocation.offsetDelta; | 92 offset += sourceLocation.offsetDelta; |
| 80 f(sourceLocation.element, sourceLocation.token, offset); | 93 f(sourceLocation.element, sourceLocation.token, offset); |
| 81 }); | 94 }); |
| 82 } | 95 } |
| 83 } | 96 } |
| 84 | 97 |
| 85 class SourceLocation { | 98 class SourceLocation { |
| 86 final Element element; | 99 final Element element; |
| 87 final Token token; | 100 final Token token; |
| 88 final int offsetDelta; | 101 final int offsetDelta; |
| 89 SourceLocation(this.element, this.token, this.offsetDelta); | 102 SourceLocation(this.element, this.token, this.offsetDelta); |
| 90 } | 103 } |
| OLD | NEW |