janet-lang / janet

A dynamic language and bytecode vm
https://janet-lang.org
MIT License
3.44k stars 223 forks source link

peg/match should give a stack trace on errors #1010

Open Techcable opened 2 years ago

Techcable commented 2 years ago

Working with PEG parsers for the first time. They are very cool (along with the rest of Janet).

However, debugging a failed "peg/match" can be difficult. Although PEG operates with an internal stack, it is not printed out on errors.

Ocassionally I will overflow the stack or encounter some other sort of error deep in matching. It is pretty easily to write left-recursive grammar accidentally. In that case, error: peg/match recursed too deeply is not a particularly helpful error message. 😉

It would be nice to (somehow) track the internal PEG stack and print it on error.

If the performance cost of this is unacceptable, we could keep it behind a debug flag.

Maybe there is some sort of compromise (short of full stack traces) that could track the PEG stack's last three or four positions?

Ideally, this would be the first step toward more tools to debug PEG parsers. An (optional) peg/debug submodule would be awesome.

sogaiu commented 2 years ago

Possibly related: https://github.com/janet-lang/janet/issues/255

Techcable commented 2 years ago

In #255, @bakpakin mentioned an "original peg debugging script", but I can't find that anywhere :(

I learned about Margaret after filing this issue, and the debugging tools there look very useful (although I haven't used them a single time).

I think in terms of "peg debugging", there are two options:

  1. Emulate some of the the debugging features (especially stack tracing) in peg.c.
  2. Make Margaret an semi-official tool, using regression tests to keep it in sync

I think option (2) probably looks better. We could have the CI mark any incompatibilities between the two as warnings. That way we won't block development, and we could eventually get around to fixing any incompatibilities.

sogaiu commented 2 years ago

With respect to "original peg debugging script", perhaps @bakpakin meant the bit posted by @andrewchambers initially:

(peg/match (freeze (merge large-g {:main ...})))
sogaiu commented 2 months ago

FWIW, margaret now supports all PEG specials (including the newer additions like sub and split) and AFAIK there aren't any serious incompatibilies.

There are a few differences mentioned here, but I doubt those make a big difference in practice.

sogaiu commented 1 month ago

There is now a tracing tool of sorts available via here.

It's possible it might help in investigating some kinds of situations.