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 library dart2js.io.location_provider; |
| 6 |
| 7 import 'code_output.dart' show CodeOutputListener; |
| 8 |
| 9 import 'package:kernel/ast.dart' show Location, Source; |
| 10 |
| 11 /// Interface for providing line/column information. |
| 12 abstract class LocationProvider { |
| 13 /// Translates the zero-based character [offset] (from the beginning of a |
| 14 /// file) to a [Location]. |
| 15 Location getLocation(int offset); |
| 16 } |
| 17 |
| 18 /// [CodeOutputListener] that collects line information. |
| 19 class LocationCollector extends CodeOutputListener implements LocationProvider { |
| 20 int length = 0; |
| 21 List<int> lineStarts = <int>[0]; |
| 22 |
| 23 void _collect(String text) { |
| 24 int index = 0; |
| 25 while (index < text.length) { |
| 26 // Unix uses '\n' and Windows uses '\r\n', so this algorithm works for |
| 27 // both platforms. |
| 28 index = text.indexOf('\n', index) + 1; |
| 29 if (index <= 0) break; |
| 30 lineStarts.add(length + index); |
| 31 } |
| 32 length += text.length; |
| 33 } |
| 34 |
| 35 @override |
| 36 void onText(String text) { |
| 37 _collect(text); |
| 38 } |
| 39 |
| 40 @override |
| 41 Location getLocation(int offset) { |
| 42 RangeError.checkValueInInterval(offset, 0, length, "offset", |
| 43 "The value of 'offset' ($offset) must be between 0 and $length."); |
| 44 return new Source(lineStarts, null).getLocation(null, offset); |
| 45 } |
| 46 |
| 47 @override |
| 48 void onDone(int length) { |
| 49 lineStarts.add(length + 1); |
| 50 this.length = length; |
| 51 } |
| 52 |
| 53 String toString() { |
| 54 return 'lineStarts=$lineStarts,length=$length'; |
| 55 } |
| 56 } |
OLD | NEW |