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

Custom parser error #1239

Closed
stoically opened this issue Oct 31, 2022 · 1 comment
Closed

Custom parser error #1239

stoically opened this issue Oct 31, 2022 · 1 comment

Comments

@stoically
Copy link

In my use case I'm providing a library that helps with parsing a specific kind of token stream syntax, namely something HTML/JSX-ish: syn-rsx. I'd like consumers of the library to customize their error behavior depending on what goes wrong while parsing the provided tokens. E.g. if a closing HTML tag is missing there's currently a hard-coded string provided to input.error. Instead I'd like to return an error enum variant such as syn_rsx::Error::MissingClosingTag, ideally with additional context information attached.

AFAIK this currently isn't possible since Parser always expects a syn::Error to be returned, which does not allow to attach custom context information, but maybe I'm missing something?

Possible solutions

Maybe Parser could allow to return everything that implements std::error::Error as an error?

Alternatively syn::Error could allow attaching arbitrary context information? E.g. via adding

  • syn::Error::set_context(context: T)
  • syn::Error::get_context() -> T

methods?

Possible workaround

Allow consumers of syn_rsx to customize just the error strings that are passed to input.error via some sort of configuration.

Downstream issue: stoically/syn-rsx#12

@dtolnay
Copy link
Owner

dtolnay commented Apr 17, 2024

I would prefer not to build anything for this into this library. But there is room in the ecosystem for a different library to implement something for this use case.

@dtolnay dtolnay closed this as completed Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants