HigherOrderCO/Bend

Retrieving diagnostics from `TermParser` in parsable format

Opened this issue · 1 comments

Hi!

First of all, amazing work! Really excited to use Bend!

Is your feature request related to a problem? Please describe.
I am working on a hobby project where I want to transpile a higher level language down to Bend code. I would like to propagate Bend compilation errors to the higher level language transpiler. However, TermParser::parse_book() returns error messages in prettified String which are hard to parse and propagate. For example:

def main():
  1 * (2 + 3)

results in:

\u{1b}[1m- expected:\u{1b}[0m statement\n\u{1b}[1m- detected:\u{1b}[0m\n\u{1b}[0m  2 |   \u{1b}[4m\u{1b}[31m1\u{1b}[0m * (2 + 3)\u{1b}[0m

which is not really ideal to try to propagate as a contextful error message to the higher level language.

Describe the solution you'd like
I’d like to be able to access a collection of low-level errors when constructing a Book.
Access to the start index, end index and the general plain error message would be really helpful.

Describe alternatives you've considered

  • I’ve considered trying to parse the prettified String output of the ParseResult, but that seems like the wrong approach for this.
  • Using check_book, but that requires a Book, which requires to use TermParser::parse_book(), which fails when the original source code was invalid.

Yes, I'm aware of this and originally that's how it was implemented.
But to cut down on complexity and make development faster we moved to only using strings.

Unless this is very important to users, I plan to keep it that way for now, at least until Bend is more stable.

The way I thought about implementing it is by making type ParseResult<T> = Result<T, ParseError>, where ParseError is either an enum of possible errors, or a struct with a message, an error code and some meta information like the range of the error like you said.
The second one is how we've implemented diagnostics for the other compiler passes.