ptal / oak

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

Return fatal parsing errors using Result. #74

Closed ghost closed 8 years ago

ghost commented 8 years ago

Return fatal parsing errors using Result.

This approach avoids double panic caused by unwrap. First panic is caused by unwrap of Result with Err, second one by destructor of DiagnosticBuilder stored in Err - it requires that diagnostic is either emitted or cancelled.

To give one example where this is useful, consider '|' used incorrectly instead of '/'. Previously this would cause following error:

error: internal compiler error: Error constructed but not emitted
thread panicked while panicking. aborting.

After introduced changes, this results in more useful error message:

src/main.rs:138:7: 138:8 error: expected ident, found `|`
src/main.rs:138       | LPAREN ty RPAREN            > ty
                      ^~
error: aborting due to previous error

Error message itself is not necessarily perfect, but at least it is possible to locate the error.

ptal commented 8 years ago

Thank you for improving the quality of the error messages (including fixing several typos). Your changes are clean and easy to read so I'm going to merge this pull-request and upgrade the Cargo package now.