Nom 5 and nested `separated_list`


I encountered a problem with nom5 when trying to use nested separated lists.

Here's the code (can't use playground since it still has nom 4):

fn and_parser(i: &str) -> IResult<&str, Vec<&str>> {
    separated_list(tag(" and "), tag("foo"))(i)

fn or_parser(i: &str) -> IResult<&str, Vec<Vec<&str>>> {
    separated_list(tag(" or "), and_parser)(i)

assert_eq!(parser("foo and foo or foo"), Ok(("", vec![vec!["foo", "foo"], vec!["foo"]])));

// FAILS with `Err(Error(("", SeparatedList)))`
assert_eq!(or_parser(""), Ok(("", vec![vec![]])));

Anyone has any insight into what may be causing this error? There is an explicit check of whether the element parser accepted an empty string.
I can understand the check is necessary for many* combinators because it would be an infinite loop but for separated_list it should be fine as long as the separator parser doesn't accept an empty string if I'm understanding correctly.
This seems a bug of nom for me.

1 Like

Thank you! I reported it in the issue

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.