Function chumsky::recovery::skip_parser

source ·
pub fn skip_parser<R>(recovery_parser: R) -> SkipParser<R>
Expand description

A recovery mode that applies the provided recovery parser to determine the content to skip.

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
enum Token {
    GoodKeyword,
    BadKeyword,
    Newline,
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
enum AST {
    GoodLine,
    Error,
}

// The happy path...
let goodline = just::<Token, _, Simple<_>>(Token::GoodKeyword)
    .ignore_then(none_of(Token::Newline).repeated().to(AST::GoodLine))
    .then_ignore(just(Token::Newline));

// If it fails, swallow everything up to a newline, but only if the line
// didn't contain BadKeyword which marks an alternative parse route that
// we want to accept instead.
let goodline_with_recovery = goodline.recover_with(skip_parser(
    none_of([Token::Newline, Token::BadKeyword])
        .repeated()
        .then_ignore(just(Token::Newline))
        .to(AST::Error),
));