Misunderstanding precedence?

I’m trying to write a small codemirror language, and while lezer is very pleasant to use, it seems I’m misunderstanding precedence.
@top rule { Flag | ID }

Flag { "SA_" letters }
ID { letters ":" numbers }

@tokens {
  @precedence {"SA_", letters}
  letters { $[a-zA-Z_]+ }
  numbers { $[0-9]+ }
}

With the above grammar, lezer parses “SA_test” as an ID, with a parse error. However, I would think it should parse it as a flag. What would the correct precedence be to allow lezer to parse the string as a Flag?

1 Like

Longer tokens always win, regardless of precedence (which just makes overlapping tokens explicit and breaks ties between same-length tokens).

In this case I’d make Flag a single token—that should just work. Possibly ID as well.

3 Likes

Awesome, thanks for your help! I must’ve missed it while reading the documentation.