ptal / oak

A typed parser generator embedded in Rust code for Parsing Expression Grammars
Apache License 2.0
142 stars 14 forks source link

Error reporting system #83

Open ptal opened 8 years ago

ptal commented 8 years ago

Instead of building errors in different way and everywhere in the code, we take the approach of implementing a file error.rs in each module (such as more or less done in rustc). We use an error enum such as:

enum TypingError {
  TypeMismatch(TypeMismatchInfo),
  ...
}

struct TypeMismatchInfo {
  span: Span;
  ...
}

Each error has a struct containing the relevant information for later printing the error. An error enum must implement a trait to retrieve an error code:

trait ErrorCode {
  fn error_code(&self) -> String;
}

That returns the name of the variant, here TypeMismatch. It is used in the error attribute #29.

Each error structure must implement a trait for printing the error:

trait DisplayError {
  fn display_error(&self, cx: &ExtCtxt);
}

This is flexible enough to allow different kind of printing, even for a JSON output or for deep explanation (as with --explain in rustc).

ptal commented 8 years ago

Actually we will implement an enum ErrorCategory that will contain the different enum of each module as well as the level of the current error:

enum ErrorCategory {
  Typing(TypingError),
  ...
}

struct Error {
  level: Level;
  error: ErrorCategory;
}

It allows to store the errors in the grammar with a field errors: Vec<Error>. We will also add utility function on the grammar to manipulate errors.