peggyjs / peggy

Peggy: Parser generator for JavaScript
https://peggyjs.org/
MIT License
943 stars 65 forks source link

Question about behavior of repetitions #381

Closed MarcelBolten closed 1 year ago

MarcelBolten commented 1 year ago

Hi all,

I think there is a discrepancy between the documentation for repetitions in conjunction with code blocks and results of preceding labeled expressions and the actual behavior. In particular the statement

Any non-number values, returned by the code block, will be interpreted as 0.

seems to be wrong. A grammar like:

start = text:"a"|{ return 'some string' }|

does not return a number and hence should be equivalent to

start = text:"a"|0|

However, the parser accepts infinite input of "a".

This is probably due to the following lines, in particular |0 and the operator precedence?: https://github.com/peggyjs/peggy/blob/d2f5bb928f5dbd7759e7d41601ea300e161fc362/lib/compiler/passes/generate-js.js#L490-L496

Any clarification is appreciated.

Mingun commented 1 year ago

Yes, this is a bug. Need to wrap boundary|0 in parenthesis