maciejhirsz / logos

Create ridiculously fast Lexers
https://logos.maciej.codes
Apache License 2.0
2.85k stars 115 forks source link

Feature Request: Span in custom error #352

Open Sniper10754 opened 9 months ago

Sniper10754 commented 9 months ago

When using a custom error type, is there a way to have a span provided with the error? what i mean is that a custom error type needs to satisfy the Default bound, and because of this logos has no way to provide a span describing where the error happened, something like this would be nice

enum Error {
    UnknownToken {
        span: Range<usize>
    }
}

impl CustomError for Error {
    fn new_custom_error(span: Range<usize> /* Other info if needed */) -> Self {
         Self { span }
    }
}

This would greatly improve diagnostics in my language since i have no way to tell the user of the language where the error happened.

jeertmans commented 9 months ago

Hello! I see why this feature might be interesting, but that would need quite some work, and I don't really have time to work on that at the moment :/

If someone wants to give it a try, please do!

A few suggestions:

In the meantime, you can simply get span information with lexer.spanned() or with different iterator style:

while let Some(token) = lexer.next() {
   /* you can use `lexer` to get current span and other information. */
}