lark
lark copied to clipboard
Nearley macros not supported
I had a go at converting my nearley grammar but I encountered the error:
File "/home/kaspar/.local/lib/python3.5/site-packages/lark/tools/nearley.py", line 116, in _nearley_to_lark
assert False, directive
AssertionError: include
So I am guessing @include
is not supported yet. I also noticed the TODO for macros. Is my assessment correct that includes and macros are not yet supported?
That is correct! Do you need both? I imagine includes would be easier to add.
I do make use of both in my grammar, yes. There are always workarounds of course. I think I'll have a go at implementing includes when I get a chance though.
Cool, let me know if you have any questions, about the code or in general.
Do you agree that the best way to add macros would be to add support for them in Lark and then translate them?
That would be my choice as well. Since you are familiar with macros in nearley, can you describe their main properties (i.e. features) ?
They are simple pre-processor functions with variables, that are expanded to several rules. I added a test case to nearley yesterday that should be a simple example of a macro.
The way I understand it, there are the following sub-tasks, all in load_grammar.py:
- Add macro syntax, both definition and usage
- In GrammarLoader.load_grammar, verify and create macros (also add macros to Grammar object)
- In Grammar.compile, apply the macros using a transformer
So, starting with syntax, are you opposed to e.g.:
combine[X, Y]: $X | $Y | $X "/" $Y | $Y "/" $X
Well, square brackets are already used for optional expressions. But I'm fine with
combine{X, Y}: $X | $Y | $X "/" $Y | $Y "/" $X
Now that Lark supports templates, this should be a lot easier to do.