parsimonious icon indicating copy to clipboard operation
parsimonious copied to clipboard

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

Open alexchandel opened this issue 1 year ago • 0 comments

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.

alexchandel avatar May 06 '24 17:05 alexchandel