disco-lang/disco

Bad parse error messages in definitions

byorgey opened this issue · 2 comments

Any syntax error in a definition now yields a really terrible error message:

Disco> f : N -> N
Disco> f(x) = 3x + 2 -
1:6:
  |
1 | f(x) = 3x + 2 -
  |      ^
unexpected '='
expecting end of input, expression, operator, or type annotation

The problem is that since we can now have top-level expressions in addition to declarations, we currently wrap the call to the definition parser in try, and fall back to parsing an expression, since e.g. we can't tell whether f(x) is supposed to be the start of an expression or definition, until e.g. we get to an = sign. So if there is a parse error in the definition, the parser fails, backtracks, then tries parsing an expression, which of course fails on the =, generating the error.

I am not sure of the right way to fix this. (1) Somehow combine the errors from both branches? (2) Left-factor the expression/definition parser somehow? (3) Swap the order?

In any case, this is super critical, since students aren't getting any good feedback about what's wrong with their syntax.

Or maybe there's some way to prevent it from backtracking once it actually sees an = sign?

Yes, that worked!