#tokenization #parser #proc-macro #macro-derive #utilities #generate

derive-finite-automaton

生成有限自动机的过程宏

5次发布

0.2.0 2024年3月1日
0.1.3 2023年9月20日
0.1.2 2023年9月20日
0.1.1 2023年9月19日
0.1.0 2021年12月6日

#119解析工具

Download history 17/week @ 2024-03-14 5/week @ 2024-03-21 26/week @ 2024-03-28 40/week @ 2024-04-04 9/week @ 2024-04-11 8/week @ 2024-04-18 5/week @ 2024-04-25 4/week @ 2024-05-02 8/week @ 2024-05-09 15/week @ 2024-05-16 21/week @ 2024-05-23 30/week @ 2024-05-30 19/week @ 2024-06-06 21/week @ 2024-06-13 25/week @ 2024-06-20 6/week @ 2024-06-27

74 每月下载量
用于 4 个crate(通过 ezno-parser

MIT 许可证

7KB
81

生成有限自动机

crates.io badge docs.rs badge

构建有限自动机的过程宏。主要用途是用于解析多个字符宽的令牌,请参阅derive-finite-automaton/examples/main.rs

运行示例

cd derive-finite-automaton
cargo run --example main

查看示例宏展开(需要 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,
}

您可以使用以下方式添加条件映射

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,
}

依赖项

~0.3–0.8MB
~19K SLoC