| Crates.io | enum_dispatch_pest_parser |
| lib.rs | enum_dispatch_pest_parser |
| version | 0.1.1 |
| created_at | 2025-02-16 15:55:05.921535+00 |
| updated_at | 2025-02-17 15:57:43.453616+00 |
| description | Generate pest-based parsers with enum_dispatch integration for static method dispatch |
| homepage | |
| repository | https://github.com/AwpMsnSoft/enum_dispatch_pest_parser |
| max_upload_size | |
| id | 1557858 |
| size | 18,422 |
A procedural macro for generating pest-based parsers with integrated enum_dispatch support, enabling type-safe static dispatch of parsing rules.
enum_dispatch for efficient method resolutionAdd dependencies to Cargo.toml:
[dependencies]
pest = { version = "2.5", features = ["derive"] }
enum_dispatch = "0.3"
enum_dispatch_pest_parser = { version = "0.1" } # This crate
Define a trait interface for parser rules
Apply the #[pest_parser] attribute to a struct
use anyhow::Result;
use enum_dispatch::enum_dispatch;
use enum_dispatch_pest_parser::pest_parser;
use pest::Parser;
// Define parser trait interface
#[enum_dispatch]
trait ParserInterface {
fn parse_rule(&self, arg: &str) -> Result<()>;
}
// Generate parser implementation
#[pest_parser(grammar = "grammar.pest", interface = "ParserInterface")]
pub struct LanguageParser;
// Implement trait for individual rules
#[derive(Default)]
struct Statement;
impl ParserInterface for Statement {
fn parse_rule(&self, arg: &str) -> Result<()> {
// do something here...
Ok(())
}
}
// Usage example
fn main() -> Result<()> {
let content = read_to_string("input.txt")?;
let node = LanguageParser::parse(Rule::Statement(Statement {}), &content)?;
node.parse_rule("argument")?;
// Dispatches to Statement::parse_rule automatically
}
pest_generator to create initial parsing codeenum Rule definition from generated codestruct Statement;)#[enum_dispatch] attribute on enum Rulepest_parser using:
println!("{}", raw_codes); // Add temporary debug output
enum Rule extraction boundaries