peggyjs / peggy

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

Fix a bug with named NEVER_MATCH expressions #454

Open markw65 opened 8 months ago

markw65 commented 8 months ago

Found while working on #452.

The condition was backwards, so the error got misreported:

Before the fix:

% echo 'start "start" = []' | node bin/peggy.js -t "x"
Error running test
Error: Expected , or undefined but "x" found.
 --> command line:1:1
  |
1 | x
  | ^

After the fix:

% echo 'start "start" = []' | node bin/peggy.js -t "x"
Error running test
Error: Expected start but "x" found.
 --> command line:1:1
  |
1 | x
  | ^

There was already a test, but it was testing for the incorrect result...

markw65 commented 8 months ago

This grammar does not expect "start" rule at this position, it expect literally nothing

But that was true whether or not it's a named rule.

Probably, it would be better to emit compiler error here instead

I guess thats ok if it's a start rule that always fails - but my examples only used start rules to exemplify the problem. You could use it as a catch all where a promising start goes off the rails:

echo 'start = [ab] fail / [cde]; fail "bzzt try again" = []' | node bin/peggy.js -t "ax"

Ok, its still not a great example, but it could be useful to have a named, NEVER_MATCH expression.