nearley icon indicating copy to clipboard operation
nearley copied to clipboard

Does Nearley allow named parameters in grammar rules?

Open jarble opened this issue 3 years ago • 2 comments

In some parser generators (such as PEG.js), it's possible to declare parameters in a grammar rule, and then use them in the parser's output:

add
  = (left:mul "+" right:add / "sum of " left:mul " and " right:add) { return left + right; }
  / mul

I wish I could also do this in Nearley, so that the parser's output could be defined more conveniently:

add
  -> (left:mul "+" right:add | "sum of " left:mul " and " right:add) 
       {% return left + right; %}
  | mul

Does Nearley allow variables to be defined in this way, like PEG.js?

jarble avatar Sep 11 '20 19:09 jarble

This is what I miss most from PEG.js. I guess what the authors want you to do is to decompose the arguments in the arrow function:

expression ->
    number "+" number {% ([fst, _, snd]) => fst + snd %}

(Above example is taken from this.)

bandaloo avatar Nov 27 '20 14:11 bandaloo

This was on the "to do" list for a while, but never really got done. I think a better solution, actually, is a syntactic notation that lets you explicitly include/exclude pieces of the rule from what gets reported in the d array. That way if you have a long rule with many pieces but only one or two "important" pieces, you wouldn't need to fish around for the index into the array passed to the postprocessor.

kach avatar Nov 27 '20 16:11 kach