degory / ghul

compiler for the ghūl programming language
https://ghul.dev
GNU Affero General Public License v3.0
4 stars 0 forks source link

Better parser error recovery #1086

Open degory opened 9 months ago

degory commented 9 months ago

Lots of common syntax errors cause huge cascades of errors due to the parser losing its place in the subsequent input.

When we detect a syntax error, we can take advantage of the variety of block terminating keywords to try to re-synchronize the parser with the block structure of the input. We can also use indentation as a hint in error recovery.

Recovery strategies:

Apparently nested functions

When encountering an apparent attempt to nest a named function inside something it may be (and is probably more likely that) the actual error is a missing si on the preceding definition. We can use a simple heuristic to determine which:

If the apparently nested function is indented the same amount as the preceding global definition, it's very likely intended to be a global definition, and the actual error was a missing si In this case we need to:

If the apparently nested function is indented the same amount as the preceding class, trait or struct member, it's very likely intended to be a method

If the apparently nested function is indented more than the enclosing class member, it may be a genuine attempt to nest a named method

Incomplete function signatures

If we detect the beginning of second function signature when we're parsing function formal arguments or type, then the first function signature is probably incomplete, especially if the first function signature is all on one line and the second signature is on a different line.