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

Using constants in #[regex()] macros #417

Closed
ccleve opened this issue Sep 11, 2024 · 2 comments
Closed

Using constants in #[regex()] macros #417

ccleve opened this issue Sep 11, 2024 · 2 comments
Labels
wontfix This will not be worked on

Comments

@ccleve
Copy link

ccleve commented Sep 11, 2024

I have multiple lexers that use common, and lengthy, regexes. It would be nice to put each regex in a const and reference it in multiple times.

In general, Rust doesn't allow constants in macros because macros get evaluated first. But I though that a nested macro might work:


#[macro_export]
macro_rules! myregex {
    () => {
        "complicated_thing_here"
    };
}
...
enum MyLexer {

    #[regex(myregex!())]
    MyTokenType,

}

but I get:

error: expected literal
   |
41 |     #[regex(myregex!())]
   |             ^^^^^^^

Is it possible to define a regex string in one place and use it in multiple places?

@jeertmans
Copy link
Collaborator

Hello! As far as I know, this is not possible, see rust-lang/rust#52393.

The short explanation is that the procedural macro #[regex(<input>)] needs to be able to read the content of <intput> as compilation time, which is not possible at the moment.

If you want to reuse some regex, you can still use subpatterns, see #162.

Now, I just noticed that this should be documented somewhere in the book ^^', so I'll open an issue for that.

@jeertmans jeertmans added the wontfix This will not be worked on label Sep 26, 2024
@jeertmans
Copy link
Collaborator

Closing this as this is a limitation of Rust, not Logos.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants