OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014, 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; | |
6 | |
7 import 'span.dart'; | |
8 | |
9 // TODO(nweiz): Use SourceLocationMixin once we decide to cut a release with | |
10 // breaking changes. See SourceLocationMixin for details. | |
11 | |
12 /// A class that describes a single location within a source file. | |
13 /// | |
14 /// This class should not be extended. Instead, [SourceLocationBase] should be | |
15 /// extended instead. | |
16 class SourceLocation implements Comparable<SourceLocation> { | |
17 /// URL of the source containing this location. | |
18 /// | |
19 /// This may be null, indicating that the source URL is unknown or | |
20 /// unavailable. | |
21 final Uri sourceUrl; | |
22 | |
23 /// The 0-based offset of this location in the source. | |
24 final int offset; | |
25 | |
26 /// The 0-based line of this location in the source. | |
27 final int line; | |
28 | |
29 /// The 0-based column of this location in the source | |
30 final int column; | |
31 | |
32 /// Returns a representation of this location in the `source:line:column` | |
33 /// format used by text editors. | |
34 /// | |
35 /// This prints 1-based lines and columns. | |
36 String get toolString { | |
37 var source = sourceUrl == null ? 'unknown source' : sourceUrl; | |
38 return '$source:${line + 1}:${column + 1}'; | |
39 } | |
40 | |
41 /// Creates a new location indicating [offset] within [sourceUrl]. | |
42 /// | |
43 /// [line] and [column] default to assuming the source is a single line. This | |
44 /// means that [line] defaults to 0 and [column] defaults to [offset]. | |
45 /// | |
46 /// [sourceUrl] may be either a [String], a [Uri], or `null`. | |
47 SourceLocation(int offset, {sourceUrl, int line, int column}) | |
48 : sourceUrl = sourceUrl is String ? Uri.parse(sourceUrl) : sourceUrl, | |
49 offset = offset, | |
50 line = line == null ? 0 : line, | |
51 column = column == null ? offset : column { | |
52 if (offset < 0) { | |
53 throw new RangeError("Offset may not be negative, was $offset."); | |
54 } else if (line != null && line < 0) { | |
55 throw new RangeError("Line may not be negative, was $line."); | |
56 } else if (column != null && column < 0) { | |
57 throw new RangeError("Column may not be negative, was $column."); | |
58 } | |
59 } | |
60 | |
61 /// Returns the distance in characters between [this] and [other]. | |
62 /// | |
63 /// This always returns a non-negative value. | |
64 int distance(SourceLocation other) { | |
65 if (sourceUrl != other.sourceUrl) { | |
66 throw new ArgumentError("Source URLs \"${sourceUrl}\" and " | |
67 "\"${other.sourceUrl}\" don't match."); | |
68 } | |
69 return (offset - other.offset).abs(); | |
70 } | |
71 | |
72 /// Returns a span that covers only a single point: this location. | |
73 SourceSpan pointSpan() => new SourceSpan(this, this, ""); | |
74 | |
75 /// Compares two locations. | |
76 /// | |
77 /// [other] must have the same source URL as [this]. | |
78 int compareTo(SourceLocation other) { | |
79 if (sourceUrl != other.sourceUrl) { | |
80 throw new ArgumentError("Source URLs \"${sourceUrl}\" and " | |
81 "\"${other.sourceUrl}\" don't match."); | |
82 } | |
83 return offset - other.offset; | |
84 } | |
85 | |
86 bool operator ==(other) => | |
87 other is SourceLocation && sourceUrl == other.sourceUrl && | |
88 offset == other.offset; | |
89 | |
90 int get hashCode => sourceUrl.hashCode + offset; | |
91 | |
92 String toString() => '<$runtimeType: $offset $toolString>'; | |
93 } | |
94 | |
95 /// A base class for source locations with [offset], [line], and [column] known | |
96 /// at construction time. | |
97 class SourceLocationBase extends SourceLocation { | |
98 SourceLocationBase(int offset, {sourceUrl, int line, int column}) | |
99 : super(offset, sourceUrl: sourceUrl, line: line, column: column); | |
100 } | |
OLD | NEW |