lark icon indicating copy to clipboard operation
lark copied to clipboard

Nearley macros not supported

Open kasbah opened this issue 6 years ago • 10 comments

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?

kasbah avatar Sep 25 '17 14:09 kasbah

That is correct! Do you need both? I imagine includes would be easier to add.

erezsh avatar Sep 25 '17 20:09 erezsh

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.

kasbah avatar Oct 01 '17 01:10 kasbah

Cool, let me know if you have any questions, about the code or in general.

erezsh avatar Oct 01 '17 18:10 erezsh

Do you agree that the best way to add macros would be to add support for them in Lark and then translate them?

kasbah avatar Oct 07 '17 23:10 kasbah

That would be my choice as well. Since you are familiar with macros in nearley, can you describe their main properties (i.e. features) ?

erezsh avatar Oct 08 '17 07:10 erezsh

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.

kasbah avatar Oct 08 '17 13:10 kasbah

The way I understand it, there are the following sub-tasks, all in load_grammar.py:

  1. Add macro syntax, both definition and usage
  2. In GrammarLoader.load_grammar, verify and create macros (also add macros to Grammar object)
  3. In Grammar.compile, apply the macros using a transformer

erezsh avatar Oct 08 '17 14:10 erezsh

So, starting with syntax, are you opposed to e.g.:

combine[X, Y]: $X | $Y | $X "/" $Y | $Y "/" $X

kasbah avatar Oct 08 '17 14:10 kasbah

Well, square brackets are already used for optional expressions. But I'm fine with

combine{X, Y}: $X | $Y | $X "/" $Y | $Y "/" $X

erezsh avatar Oct 08 '17 15:10 erezsh

Now that Lark supports templates, this should be a lot easier to do.

erezsh avatar Aug 06 '20 19:08 erezsh