Skip to content

separated_list1 fails if separator is empty #1288

@reivilibre

Description

@reivilibre

The separated_list1 combinator fails if the separator is empty.

I suppose this is because of this excerpt here:

https://github.com/Geal/nom/blob/0b92df971a7d2090023b353e0283f9b2db6f2a84/src/multi/mod.rs#L353-L355

This behaviour is not obvious; perhaps it is unusual to desire it, though.

My use case was to have space-separated values, but allow the space to be omitted if a ( or [ bracket followed immediately, as in

    let space_or_peek_bracket = alt((
        char(' '),
        peek(one_of("(["))
    ));
    let (i, terms) = separated_list1(space_or_peek_bracket, parse_term)(i)?;

I notice that separated_list1 was previously extended to allow empty elements.
I also notice that many1 forbids empty elements (because of the potential for an infinite loop).

I'm sure I will be able to work around this without too much trouble, but perhaps separated_list1 could be made to allow this, or have documentation added that makes it obvious?

(I suspect the documentation route may be the best, because otherwise you could have both empty elements and empty separators and this would mean that you could have an infinite loop, similar to many1…)

In any case, thought it was worth dropping a note here in case anyone else scratches their head.

nom version : 6.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions