Trait chumsky::error::Error

source ·
pub trait Error<I>: Sized {
    type Span: Span;
    type Label;

    // Required methods
    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;

    // Provided method
    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§

source

type Span: Span

The type of spans to be used in the error.

source

type Label

The label used to describe a syntactic structure currently being parsed.

This can be used to generate errors that tell the user what syntactic structure was currently being parsed when the error occurred.

Required Methods§

source

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.

source

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.

source

fn merge(self, other: Self) -> Self

Merge two errors that point to the same input together, combining their information.

Provided Methods§

source

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.

Implementors§

source§

impl<I, S: Span + Clone + Debug> Error<I> for Cheap<I, S>

§

type Span = S

§

type Label = &'static str

source§

impl<I: Hash + Eq, S: Span + Clone + Debug> Error<I> for Simple<I, S>

§

type Span = S

§

type Label = &'static str