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

impl<T: Logos> CallbackResult for T #295

Merged
merged 1 commit into from
Apr 13, 2023

Conversation

ExoticMatter
Copy link
Contributor

@ExoticMatter ExoticMatter commented Apr 13, 2023

With this patch, you can return tokens directly from callbacks--but only for unit variants. Closes #294. Related to #245.

#[derive(Logos, Debug, PartialEq)]
#[logos(skip r"[ \t\n\f]+")]
enum Token {
    #[regex(r"[a-zA-Z]+", |_| Token::Text)]
    #[regex(r"-?[0-9]+", |_| Token::Integer)]
    #[regex(r"-?[0-9]+\.[0-9]+", |_| Token::Float)]
    Text,
    Integer,
    Float,
}

@maciejhirsz
Copy link
Owner

Out of curiosity, do you have an example where that's something you'd do? It's always helpful to see user code for insights I might not have otherwise.

The PR looks fine to me by itself.

@ExoticMatter
Copy link
Contributor Author

ExoticMatter commented Apr 13, 2023

In my case it was for Python-like formatting. Indent, Dedent, and NewLine need to be distinct tokens, but they all share the same regex so they can't have separate callbacks.

@ExoticMatter ExoticMatter marked this pull request as ready for review April 13, 2023 20:40
@maciejhirsz
Copy link
Owner

Ah interesting! Makes sense, and thank you for the PR!

@maciejhirsz maciejhirsz merged commit 2c2fa6f into maciejhirsz:master Apr 13, 2023
@ExoticMatter ExoticMatter deleted the any-token-callback branch April 13, 2023 21:10
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

Successfully merging this pull request may close these issues.

Multiple token types from one match
2 participants