Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there an easy way of doing this using nom? #1794

Open
amokfa opened this issue Jan 8, 2025 · 1 comment
Open

Is there an easy way of doing this using nom? #1794

amokfa opened this issue Jan 8, 2025 · 1 comment

Comments

@amokfa
Copy link

amokfa commented Jan 8, 2025

Prerequisites

Here are a few things you should provide to help me understand the issue:

  • Rust version : 1.83.0
  • nom version : 7.1
  • nom compilation features used: ["alloc"]

Test case

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!

@marcdejonge
Copy link

The permutation only works by applying one of the parsers that you give fully and then it doesn't try that one again. So you are correct, the many0 inside the permutation doesn't work like you expected. But this is by design.

You basically want a permutation, but where parsers can be applied multiple times? I don't think that exists. The second parser you wrote there sounds like a good alternative, but mostly when you have an enum to capture all of those in. Because all the alternatives in the many0 block should have the same resulting type.

Do you have a bit more detailed information what you're trying to do? This is a bit too high-level to give a good answer I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants