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

[request] support repeated elements with separators (or intercalated matches) #243

Open
alexchandel opened this issue May 6, 2024 · 0 comments

Comments

@alexchandel
Copy link

Because separated lists are such a common component of grammars, Perl's Regexp::Grammars provides special syntax to specify them:

<rule: list>
    <[item]>+ % <separator>      # one-or-more
 
<rule: list_zom>
    <[item]>* % <separator>      # zero-or-more

Without separator syntax, separated lists become:

<rule: list_opt>
    <list>?                       # entire list may be missing
 
<rule: list>                      # as before...
    <item> <separator> <list>     #   recursive definition
  | <item>                        #   base case

# Or, more efficiently, but less prettily:

<rule: list>
    <[item]> (?: <separator> <[item]> )*           # one-or-more
 
<rule: list_opt>
    (?: <[item]> (?: <separator> <[item]> )* )?    # zero-or-more

The same is true for parsimonious. Separated lists currently require the hacky syntax above. Beyond cleaner syntax, the benefit of separator syntax is that it unifies common match terms. Rather than handling items multiple times (or recursively) and duplicating code, separator syntax collects the items.

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

1 participant