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