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

Tokens that are not enums #861

Open
ccleve opened this issue Jan 8, 2024 · 0 comments
Open

Tokens that are not enums #861

ccleve opened this issue Jan 8, 2024 · 0 comments

Comments

@ccleve
Copy link

ccleve commented Jan 8, 2024

Similar to #803, I have an existing Token type (widely used throughout the system) that looks like this:

pub struct Token {
    pub token_type: TokenType,
    // other stuff
}

Token is not an enum, but it contains one.

I'm trying to figure out how to use this with lalrpop. I have a TokenIterator that produces tokens, and I've gotten it working with my grammar. My grammar looks like this:

extern {
  type Location = usize;
  type Error = LexicalError;

  enum Token {
    "Word" => TokenType::Word,
    "(" => TokenType::OpenParen,
    ...
  }
}

The problem is hooking things up so the generated code matches on token.token_type instead of token.

When I generate the code, I get this compiler error:

error[E0308]: mismatched types
   --> src/query/lalrpop/query_parser.rs:222:13
    |
221 |         match *__token {
    |               -------- this expression has type `tokens::Token`
222 |             TokenType::OpenParen if true => Some(0),
    |             ^^^^^^^^^^^^^^^^^^^^ expected `Token`, found `TokenType`

If I hand-edit the generated code to change this:

match *__token {

to this:

match __token.token_type {

everything works beautifully.

How do I get the system to generate the code appropriately?

@ethindp It look like you got this working. What is your secret?

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

1 participant