0

I'm parsing with the nom library. I would like to match on something that is followed by an 'end', without consuming it.

A end for me is either eof or a char that satisfies a function f: Fn(char) -> bool.

I can use f with nom::character::complete::satisfy, nom::bytes::take_while, or many other nom functions.

The incompatabilites between eof and f makes it impossible for me to compose, with nom combinators or my own combinators.

This is illegal, because of opaque type:

fn end(i: &str) -> IResult<&str, &str> {
    match eof(i) {
        o @ Ok(_) => o,
        e @ Err(_) => peek(satisfy(f)),
    }
}

This is also illegal:

alt((eof, satisfy(f)))

I can't even make this work:

alt((eof, char(' ')))

I can't even make THIS work: (because match arms has incompatible types??)

fn end(i: &str) -> IResult<&str, ()> {
    match eof(i) {
        o @ Ok((sur, res)) => Ok((i, ())),
        e @ Err(_) => match satisfy(f) {
            Ok(_) => Ok((i, ())),
            e @ Err(_) => e,
        },
    }
}

1 Answers1

0

I made it work by providing an error myself:

fn end(i: &str) -> IResult<&str, ()> {
    match eof::<&str, ()>(i) {
        o @ Ok((_, _)) => Ok((i, ())),
        e @ Err(_) => match satisfy::<_, _, ()>(is_url_terminative)(i) {
            Ok(_) => Ok((i, ())),
            Err(_) => Err(nom::Err::Error(Error {
                input: i,
                code: nom::error::ErrorKind::Permutation,
            })),
        },
    }
}