logo

Error Model

Posted on: 2023-01-14

Error Models

Exceptions

Exceptions are problematic for a variety of reasons

Compiler Error Models

  1. For lex and parsing, try to sync as safe as possible. Better avoid false positives than detect more errors.
  2. Don’t do semantic analysis on code with parse errors. Chances it make sense is low.
  3. For semantic analysis, use poisoning to catch as many valid errors as possible.

source

Discussed for the D language compiler

Not a particularly new idea, but perhaps something should use more. In most code I have made failures fail fast - by returning an error code, and letting that propogate up the call stack. Thoughts

1.) The parser should be infallible, i.e. it always returns a parse tree. The return type of parse is not ParseTree OR Errors, it's ParseTree AND Errors. Within the parse tree there can be nodes corresponding to the errors found. This allows you to link the error back to the parsed text for a LSP/IDE environment (Language Server Protocol). You want to collect all parse errors that are not direct causes of each other so that you can report separate diagnostics to the user.

2.) Knowing when to stop collecting an error is a matter of recoverability. That could be looking for closing brace } or a keyword like var or something of that nature to stop the error parsing and restart the parser on a valid expression.

3.) Reporting errors to the user should attempt to maintain as much context as possible. Provide auto-fix capabilities through the LSP where feasible. Answer the question of why it is an error, not just stating the error. Please don't just make your error say "Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM"

4.) Be forgiving. If the language requires semicolons but the can still perfectly understand the statement without a semicolon, consider adding it for the user automatically with a formatting tool.

source

Aruadne is a "fancy compiler diagnostics crate" has pretty error reporting [https://docs.rs/ariadne/latest/ariadne/], which also try to explain the problem. It uses color on the terminal, as well as trying to point to where the problem is.

Links