OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, 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 source_span.location_mixin; | |
6 | |
7 import 'location.dart'; | |
8 import 'span.dart'; | |
9 | |
10 // Note: this class duplicates a lot of functionality of [SourceLocation]. This | |
11 // is because in order for SourceLocation to use SourceLocationMixin, | |
12 // SourceLocationMixin couldn't implement SourceLocation. In SourceSpan we | |
13 // handle this by making the class itself non-extensible, but that would be a | |
14 // breaking change for SourceLocation. So until we want to endure the pain of | |
15 // cutting a release with breaking changes, we duplicate the code here. | |
16 | |
17 /// A mixin for easily implementing [SourceLocation]. | |
18 abstract class SourceLocationMixin implements SourceLocation { | |
19 String get toolString { | |
20 var source = sourceUrl == null ? 'unknown source' : sourceUrl; | |
21 return '$source:${line + 1}:${column + 1}'; | |
22 } | |
23 | |
24 int distance(SourceLocation other) { | |
25 if (sourceUrl != other.sourceUrl) { | |
26 throw new ArgumentError("Source URLs \"${sourceUrl}\" and " | |
27 "\"${other.sourceUrl}\" don't match."); | |
28 } | |
29 return (offset - other.offset).abs(); | |
30 } | |
31 | |
32 SourceSpan pointSpan() => new SourceSpan(this, this, ""); | |
33 | |
34 int compareTo(SourceLocation other) { | |
35 if (sourceUrl != other.sourceUrl) { | |
36 throw new ArgumentError("Source URLs \"${sourceUrl}\" and " | |
37 "\"${other.sourceUrl}\" don't match."); | |
38 } | |
39 return offset - other.offset; | |
40 } | |
41 | |
42 bool operator ==(other) => | |
43 other is SourceLocation && | |
44 sourceUrl == other.sourceUrl && | |
45 offset == other.offset; | |
46 | |
47 int get hashCode => sourceUrl.hashCode + offset; | |
48 | |
49 String toString() => '<$runtimeType: $offset $toolString>'; | |
50 } | |
51 | |
OLD | NEW |