Line data Source code
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 : import 'span.dart';
6 :
7 : /// A class for exceptions that have source span information attached.
8 : class SourceSpanException implements Exception {
9 : // This is a getter so that subclasses can override it.
10 : /// A message describing the exception.
11 0 : String get message => _message;
12 : final String _message;
13 :
14 : // This is a getter so that subclasses can override it.
15 : /// The span associated with this exception.
16 : ///
17 : /// This may be `null` if the source location can't be determined.
18 0 : SourceSpan get span => _span;
19 : final SourceSpan _span;
20 :
21 0 : SourceSpanException(this._message, this._span);
22 :
23 : /// Returns a string representation of [this].
24 : ///
25 : /// [color] may either be a [String], a [bool], or `null`. If it's a string,
26 : /// it indicates an ANSII terminal color escape that should be used to
27 : /// highlight the span's text. If it's `true`, it indicates that the text
28 : /// should be highlighted using the default color. If it's `false` or `null`,
29 : /// it indicates that the text shouldn't be highlighted.
30 : String toString({color}) {
31 0 : if (span == null) return message;
32 0 : return "Error on " + span.message(message, color: color);
33 : }
34 : }
35 :
36 : /// A [SourceSpanException] that's also a [FormatException].
37 : class SourceSpanFormatException extends SourceSpanException
38 : implements FormatException {
39 : // This is a getter so that subclasses can override it.
40 0 : dynamic get source => _source;
41 : final _source;
42 :
43 0 : int get offset => span == null ? null : span.start.offset;
44 :
45 : SourceSpanFormatException(String message, SourceSpan span, [this._source])
46 0 : : super(message, span);
47 : }
|