parsimonious
parsimonious copied to clipboard
Support Grammar Fragments by disallowing rules
It would be nice to disallow the use of certain rules when parsing.
There is already the ability to parse the grammar starting on particular rule:
GRAMMAR[rule].parse(foo)
What I would like is the ability to also disallow rules from being used:
GRAMMAR[rule1, (rule3, False)].parse(foo)
Or maybe
GRAMMAR[rule1].disallow(rule2, rule3).parse(foo)
Use case:
I have been writing DSL to act the API for a set of tools. Some tools only support a subset of the grammar. I would like to just parse that fragment and otherwise error.
The subsets are not hierarchical so simple inheritance would work, and thinking about it compositionally is slightly unnatural.
Also, my current work around is to filter the lines the start with a disallowed rule when creating the Grammar object.
It works, but it's hard to justify packaging it put
If you want to whitelist, @mvcisback's solution is the thing. But if you want to blacklist, I think the ultimate solution is to make a subgrammar (once #30 is implemented) and override the rules you hate with a rule that throws an explicit error. I eventually want to implement "naughty ORs" in Parsimonious to improve error reporting, for just such situations as yours.