pub trait Error<I>: Sized {
type Span: Span;
type Label;
fn expected_input_found<Iter: IntoIterator<Item = Option<I>>>(
span: Self::Span,
expected: Iter,
found: Option<I>
) -> Self;
fn with_label(self, label: Self::Label) -> Self;
fn merge(self, other: Self) -> Self;
fn unclosed_delimiter(
unclosed_span: Self::Span,
unclosed: I,
span: Self::Span,
expected: I,
found: Option<I>
) -> Self { ... }
}
Expand description
A trait that describes parser error types.
If you have a custom error type in your compiler, or your needs are not sufficiently met by Simple
, you should
implement this trait. If your error type has ‘extra’ features that allow for more specific error messages, you can
use the Parser::map_err
or Parser::try_map
functions to take advantage of these inline within your parser.
Examples
type Span = std::ops::Range<usize>;
// A custom error type
#[derive(Debug, PartialEq)]
enum MyError {
ExpectedFound(Span, Vec<Option<char>>, Option<char>),
NotADigit(Span, char),
}
impl chumsky::Error<char> for MyError {
type Span = Span;
type Label = ();
fn expected_input_found<Iter: IntoIterator<Item = Option<char>>>(
span: Span,
expected: Iter,
found: Option<char>,
) -> Self {
Self::ExpectedFound(span, expected.into_iter().collect(), found)
}
fn with_label(mut self, label: Self::Label) -> Self { self }
fn merge(mut self, mut other: Self) -> Self {
if let (Self::ExpectedFound(_, expected, _), Self::ExpectedFound(_, expected_other, _)) = (
&mut self,
&mut other,
) {
expected.append(expected_other);
}
self
}
}
let numeral = filter_map(|span, c: char| match c.to_digit(10) {
Some(x) => Ok(x),
None => Err(MyError::NotADigit(span, c)),
});
assert_eq!(numeral.parse("3"), Ok(3));
assert_eq!(numeral.parse("7"), Ok(7));
assert_eq!(numeral.parse("f"), Err(vec![MyError::NotADigit(0..1, 'f')]));
Required Associated Types§
Required Methods§
sourcefn expected_input_found<Iter: IntoIterator<Item = Option<I>>>(
span: Self::Span,
expected: Iter,
found: Option<I>
) -> Self
fn expected_input_found<Iter: IntoIterator<Item = Option<I>>>(
span: Self::Span,
expected: Iter,
found: Option<I>
) -> Self
Create a new error describing a conflict between expected inputs and that which was actually found.
found
having the value None
indicates that the end of input was reached, but was not expected.
An expected input having the value None
indicates that the end of input was expected.
sourcefn with_label(self, label: Self::Label) -> Self
fn with_label(self, label: Self::Label) -> Self
Indicate that the error occurred while parsing a particular syntactic structure.
How the error handles this information is up to it. It can append it to a list of structures to get a sort of ‘parse backtrace’, or it can just keep only the most recent label. If the latter, this method should have no effect when the error already has a label.
Provided Methods§
sourcefn unclosed_delimiter(
unclosed_span: Self::Span,
unclosed: I,
span: Self::Span,
expected: I,
found: Option<I>
) -> Self
fn unclosed_delimiter(
unclosed_span: Self::Span,
unclosed: I,
span: Self::Span,
expected: I,
found: Option<I>
) -> Self
Create a new error describing a delimiter that was not correctly closed.
Provided to this function is the span of the unclosed delimiter, the delimiter itself, the span of the input that was found in its place, the closing delimiter that was expected but not found, and the input that was found in its place.
The default implementation of this function uses Error::expected_input_found
, but you’ll probably want to
implement it yourself to take full advantage of the extra diagnostic information.