I have just started using Nom 5. Its been rough but I am getting there.
I have paser that detects variable names ie alpha character and _ etc.
My code is like the following
fn take_first_ident_char<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, &'a str, E> {
let first_chars = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
let c = take(1usize)(i);
match c {
Ok((remaining_input, character)) => {
if first_chars.contains(character) {
return Ok((i, character)); // Return original str don't return remaining input
}
return Ok(("", ""));
//Help
//return Err(());
//return Err(Error("ident lacks a valid character for the first position"));
//return Err(VerboseError<&str>("ident lacks a valid character for the first position"));
},
Err(e) => Err(e)
}
}
/// Must start with a letter or an underscore, can be followed by letters, digits or underscores.
fn ident<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, &'a str, E> {
let remaining_chars = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
preceded(take_first_ident_char, take_while(move |c| remaining_chars.contains(c)))(i)
}
What I am missing is what kind of error I should return in take_first_ident_char if the first character is not valid.
I am return return Ok(("", "")); as I can't get any error return to compile.
What is the best practive here for the latest Nom ?
Thanks