[][src]Function autumn::parsers::throw

pub fn throw<'p, T: Clone + 'p, E: Clone + 'p>(
    value: T,
    error: E
) -> Boxed<dyn Parser<T, E> + 'p>

Creates a parser that consumes no input and produces the given error as an exception

See parsers that produce errors.

This can be used if a particular value will be produced within an and_then combinator. The exception produced can be turned into an error with the catch combinator.

fn alphabet(source: &str, location: Span) -> ParseResult<String, &'static str> {
    "abcde"
        .and(digit)
        .copy_string()
        .and_then(|text| {
            if text.ends_with("0") {
                throw(text, "Token must not end with 0")
            } else {
                value(text)
            }
        })
        .catch()
        .parse(source, location)
}