My question is quite simliar to where I want to parse some text that could be one of many options (possibly only known at runtime). Usually I could just use the alt
combinator (for the ones known at compile time) but that does not have a 'dynamic' length option. In the question , I linked above contains an answer (but for nom 4 , I am using nom 7.1.3 ) , which goes like
/// Dynamic version of `alt` that takes a slice of strings
fn alternative<T>(input: T, alternatives: &[&'static str]) -> IResult<T, T>
where
T: InputTake,
T: Compare<&'static str>,
T: InputLength,
T: AtEof,
T: Clone,
{
let mut last_err = None;
for alternative in alternatives {
let inp = input.clone();
match tag!(inp, &**alternative) {
done @ Ok(..) => return done,
err @ Err(..) => last_err = Some(err), // continue
}
}
last_err.unwrap()
}
/// Usage
named!(test<Span, Span>,
call!(alternative, &["a", "b", "c"])
);
However I am not really sure on how to convert this macro nom into the functional nom that I know. Any help is appreactiated !