Nom 5 and nested `separated_list`

Hi,

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?

https://docs.rs/nom/5.0.1/src/nom/multi/mod.rs.html#253-255 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 Nested separated_list for empty input fails? · Issue #1063 · Geal/nom · GitHub

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