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,
},
}
}