Feature idea: template functionality for rules
See original GitHub issueI am fairly new to this whole grammar thing, so please forgive me if I am missing something.
I think that adding some kind of template-functionality to lark would be a good idea to avoid redundancy in rules and to increase readability. A template would be a rule that takes parameters. Such a template could then be instantiated into a new rule by giving concrete values to these parameters. A grammar that contains templates could then be transformed into a grammar without templates by instantiating all used template rules.
For example, consider this grammar with templates
spaced(content): " " content " "
r1: "+" spaced("hello")
r2: "-" spaced("world")
spaced
is a template that takes a parameter content
. spaced("hello")
and spaced("world")
would be concrete instances of spaced
. In a pre-processing step, the grammar can then be transformed into a grammar without templates:
spaced_0: " " "hello" " "
spaced_1: " " "world" " "
r1: "+" spaced_0
r2: "-" spaced_1
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (8 by maintainers)
Top GitHub Comments
Cool, so if it isn’t a problem, I could give it a try in the coming weeks or so.
This feature has been implemented, so I consider this issue closed. Good job everyone!