Is there an easy way of doing this using nom parser?

Hi there! We are using a parsing library that has a permutation that looks like this:

permutation::<_, _, (), _>((many0(tag("tag1")), many0(tag("tag2"))))

Note that this is a heavily simplified example (reference code). This parser matches multiple instances of tag1, followed by multiple instances of tag2. That is not the expected behavior. Instances of tag1 and tag2 can be intermixed with each other (eg tag1 tag2 tag2 tag1).
To fix this, it looks like we would have to use some combination of alt and many0, something like the following:

many0(alt((tag("tag1"), tag("tag2"))))

But I can't figure out how. I was also wondering if there is an easy way of doing this, because this looks like a fairly common use case (intermixed permutations of a bunch of subparsers, result being a bunch of lists). Thanks!

That works as-is.

1 Like