zevv / npeg

PEGs for Nim, another take
MIT License
330 stars 22 forks source link

When defining grammar block parameterized rules block name needed? #52

Closed thatrandomperson5 closed 1 year ago

thatrandomperson5 commented 1 year ago

I have a grammar block:

grammar "ib":
    EOL <- "\r\n" | "\n" | "\r"
    whitespace <- *Blank
    Static <- >whitespace

And with normal rules it works fine. But as soon as i add parameterized rules it requires me to add ib. in front of everything.

Erroring code:

grammar "ib":
    EOL <- "\r\n" | "\n" | "\r"
    whitespace <- *Blank
    Static <- >whitespace
    Line(content) <- Static * content * EOL

Working code:

grammar "ib":
    EOL <- "\r\n" | "\n" | "\r"
    whitespace <- *Blank
    Static <- >whitespace
    Line(content) <- ib.Static * content * ib.EOL 

I would assume either that this is a bug or i am missing something, either way how do fix this?

zevv commented 1 year ago

Hey Nilts,

I'd say you did not really hit a bug, but more of an under-specified and under-documented area of NPeg. What happens is that the template ends up verbatim in your final grammar, but any symbols it refers to are not automatically bound to the grammar where the template was defined, so your solution to explicitly prefix the library name for any rules it uses is correct.

I'll leave this ticket open for now, if I find some time I will take a look if I can change the behavior to make templates in libraries a bit more useful.