Inferring the regex pattern from terminals
If a token type should only produce terminals with
known literal values (for example, a set of keywords),
regex
can be omitted since it can be inferred from
the terminals.
use teleparse::prelude::*; #[derive_lexicon] #[teleparse(terminal_parse)] pub enum MyToken { #[teleparse(terminal(Pizza = "pizza", Pasta = "pasta"))] Food, } fn main() {}
This is the Don't-Repeat-Yourself (DRY) principle.
In fact, derive_lexicon
enforces it:
use teleparse::prelude::*; #[derive_lexicon] pub enum TokenType { #[teleparse(terminal( OpAdd = "+", OpSub = "-", OpMul = "*", OpDiv = "/", ), regex(r"[\+\-\*/]"))] Operator, } fn main() {}
error: Defining `regex` here is redundant because all terminals have a literal match pattern, so the rule can already be inferred.
--> tests/ui/lex_redundant_regex.rs:11:5
|
11 | Operator,
| ^^^^^^^^