Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Span in custom error #352

Open
Sniper10754 opened this issue Dec 14, 2023 · 1 comment
Open

Feature Request: Span in custom error #352

Sniper10754 opened this issue Dec 14, 2023 · 1 comment
Labels
enhancement New feature or request help wanted Extra attention is needed nice to have

Comments

@Sniper10754
Copy link

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
Copy link
Collaborator

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:

  • look at the code that parses #[logos(error = T)] macros;
  • add an option to pass an immutable reference to the lexer, e.g., #[logos(pass_lexer_to_error)];
  • requires that error type T implements from (E, &Lexer) instead of from E for all error types E.

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. */
}

@jeertmans jeertmans added enhancement New feature or request help wanted Extra attention is needed nice to have labels Dec 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed nice to have
Projects
None yet
Development

No branches or pull requests

2 participants