Crates.io | derive-finite-automaton |
lib.rs | derive-finite-automaton |
version | 0.2.0 |
source | src |
created_at | 2021-12-06 21:34:18.152812 |
updated_at | 2024-03-01 09:11:12.335986 |
description | Procedural macro for generating finite automaton |
homepage | |
repository | https://github.com/kaleidawave/derive-finite-automaton |
max_upload_size | |
id | 493395 |
size | 15,004 |
A procedural macro for building a finite automaton. Main use is for lexing multiple character wide tokens see derive-finite-automaton/examples/main.rs
.
Run example:
cd derive-finite-automaton
cargo run --example main
View example macro expansion (requires cargo-expand):
cd derive-finite-automaton
cargo expand --example main
use derive_finite_automaton::{FiniteAutomata, FiniteAutomataConstructor};
#[derive(FiniteAutomataConstructor, Debug, PartialEq)]
#[automaton_mappings(
"{" => Tokens::OpenBrace,
"}" => Tokens::CloseBrace,
"=" => Tokens::Assign,
"=>" => Tokens::ArrowFunction,
"==" => Tokens::Equal,
"===" => Tokens::StrictEqual,
"." => Tokens::Dot,
"..." => Tokens::Spread,
)]
pub enum Tokens {
OpenBrace,
CloseBrace,
ArrowFunction,
Equal,
StrictEqual,
Assign,
Dot,
Spread,
}
You can add conditional mappings with the following
use derive_finite_automaton::FiniteAutomataConstructor;
#[derive(Debug, FiniteAutomataConstructor)]
#[automaton_mappings(
"{" => Tokens::OpenBrace,
"}" => Tokens::CloseBrace,
"=>" => Tokens::ArrowFunction,
"==" => Tokens::Equal,
"===" => Tokens::StrictEqual,
"=" => Tokens::Assign,
// Some mapping
"." => Tokens::Dot,
)]
#[cfg_attr(feature = "special", automaton_mappings(
".?." => Tokens::Magic,
))]
pub enum Tokens {
OpenBrace,
CloseBrace,
ArrowFunction,
Equal,
StrictEqual,
Assign,
Dot,
#[cfg(feature = "special")]
Magic,
}