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

Ergonomics of alt() taking tuple versus slice #1720

Closed
t-mart opened this issue Jan 6, 2024 · 3 comments · Fixed by #1754
Closed

Ergonomics of alt() taking tuple versus slice #1720

t-mart opened this issue Jan 6, 2024 · 3 comments · Fixed by #1754

Comments

@t-mart
Copy link

t-mart commented Jan 6, 2024

Is there a reason alt() cannot take a slice of child parsers?

Instead, it takes a List, which is any of the 1- to 21-tuple implementations.

This is fine when the format for which you are trying to parse is known at compile-time/statically, but unusable when the format is only known at runtime.

For example, I'm writing a software-version bumper right now, and the user provides both their arbitrary format (e.g. yyyy.patch) and their current version (e.g. 2024.5) as inputs to my package/program. First, I have a parser for the format string (which is static-defined), but then I need to create a new parser from its result for the actual version string, which is runtime-defined. Therefore, this second parser cannot be made from a tuple.

I have been able to work around this by continually building a parser up in a "reduce" fashion, something like

parsers.reduce(|acc, p| acc.or(p))

(I believe that applying pairwise or() calls has the same effect as alt(), right?) Nonetheless, it's cumbersome, especially with the type annotations, for a newer Rust user like me: plenty of Box, dyn, lifetimes, Sized, etc.

So, I wonder why there isn't a version of alt that could take in a dynamically sized slice of child parsers.

Prerequisites

  • Rust version: rustc 1.77.0-nightly (e51e98dde 2023-12-31)
  • nom version : 7.1.3
  • nom compilation features used: default ones
@epage
Copy link
Contributor

epage commented Jan 7, 2024

alt for arrays was added in #1556 but that was after the start of 8.0. I've not seen any indication of how much remaining work Geal intends for the 8.0 release.

@epage
Copy link
Contributor

epage commented Jan 7, 2024

Hmm, that is still just for a fixed size array and it sounds like you don't know the size of your array until runtime, right?

@Geal
Copy link
Collaborator

Geal commented May 5, 2024

impemented in #1754, this will ship in nom 8, thank you for the suggestion

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

Successfully merging a pull request may close this issue.

3 participants