Use YamlExceptions directly + tidy up error reporting
diff --git a/pkgs/yaml/lib/src/error_listener.dart b/pkgs/yaml/lib/src/error_listener.dart
index d156655..c302a4d 100644
--- a/pkgs/yaml/lib/src/error_listener.dart
+++ b/pkgs/yaml/lib/src/error_listener.dart
@@ -1,33 +1,15 @@
-import 'package:source_span/source_span.dart';
-
import 'yaml_exception.dart';
/// A listener that is notified of [YamlError]s during scanning/parsing.
abstract class ErrorListener {
/// This method is invoked when an [error] has been found in the YAML.
- void onError(YamlError error);
-}
-
-/// An error found in the YAML.
-class YamlError {
- /// A message describing the exception.
- final String message;
-
- /// The span associated with this exception.
- final FileSpan span;
-
- YamlError(this.message, this.span);
-}
-
-extension YamlErrorExtensions on YamlError {
- /// Creates a [YamlException] from a [YamlError].
- YamlException toException() => YamlException(message, span);
+ void onError(YamlException error);
}
/// An [ErrorListener] that collects all errors into [errors].
class ErrorCollector extends ErrorListener {
- final List<YamlError> errors = [];
+ final List<YamlException> errors = [];
@override
- void onError(YamlError error) => errors.add(error);
+ void onError(YamlException error) => errors.add(error);
}
diff --git a/pkgs/yaml/lib/src/scanner.dart b/pkgs/yaml/lib/src/scanner.dart
index 260da1c..e6e2062 100644
--- a/pkgs/yaml/lib/src/scanner.dart
+++ b/pkgs/yaml/lib/src/scanner.dart
@@ -496,13 +496,9 @@
if (key.line == _scanner.line) continue;
if (key.required) {
- final error = _reportError("Expected ':'.", _scanner.emptySpan);
- if (_recover) {
- _tokens.insert(key.tokenNumber - _tokensParsed,
- Token(TokenType.key, key.location.pointSpan() as FileSpan));
- } else {
- throw error.toException();
- }
+ _reportError(YamlException("Expected ':'.", _scanner.emptySpan));
+ _tokens.insert(key.tokenNumber - _tokensParsed,
+ Token(TokenType.key, key.location.pointSpan() as FileSpan));
}
_simpleKeys[i] = null;
@@ -1641,11 +1637,13 @@
}
}
- /// Reports an error to [_errorListener] and returns the [YamlError].
- YamlError _reportError(String message, FileSpan span) {
- final error = YamlError("Expected ':'.", _scanner.emptySpan);
- _errorListener?.onError(error);
- return error;
+ /// Reports a [YamlException] to [_errorListener] and if [_recover] is false,
+ /// throw the exception.
+ void _reportError(YamlException exception) {
+ _errorListener?.onError(exception);
+ if (!_recover) {
+ throw exception;
+ }
}
}
diff --git a/pkgs/yaml/test/utils.dart b/pkgs/yaml/test/utils.dart
index 0904458..f90ac5b 100644
--- a/pkgs/yaml/test/utils.dart
+++ b/pkgs/yaml/test/utils.dart
@@ -4,7 +4,6 @@
import 'package:test/test.dart';
import 'package:yaml/src/equality.dart' as equality;
-import 'package:yaml/src/error_listener.dart' show YamlError;
import 'package:yaml/yaml.dart';
/// A matcher that validates that a closure or Future throws a [YamlException].
@@ -24,10 +23,11 @@
}
/// Asserts that an error has the given message and starts at the given line/col.
-void expectErrorAtLineCol(YamlError error, String message, int line, int col) {
+void expectErrorAtLineCol(
+ YamlException error, String message, int line, int col) {
expect(error.message, equals(message));
- expect(error.span.start.line, equals(line));
- expect(error.span.start.column, equals(col));
+ expect(error.span!.start.line, equals(line));
+ expect(error.span!.start.column, equals(col));
}
/// Asserts that a string containing a single YAML document produces a given