parsimonious
parsimonious copied to clipboard
[request] support repeated elements with separators (or intercalated matches)
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.