| Crates.io | vacro-parser |
| lib.rs | vacro-parser |
| version | 0.1.5 |
| created_at | 2025-12-29 06:22:59.083302+00 |
| updated_at | 2025-12-29 06:57:06.818728+00 |
| description | A declarative parsing library for Rust procedural macros, simplifying input handling. |
| homepage | |
| repository | https://github.com/FeVeR-Store/vacro |
| max_upload_size | |
| id | 2010042 |
| size | 141,234 |
The Declarative Parsing Kernel for Vacro
Vacro Parser is the core declarative parsing engine of the Vacro framework. It provides a macro_rules!-like DSL to simplify the writing of syn-based parsers for Rust Procedural Macros.
It allows you to define AST structures and parsing logic declaratively, eliminating the boilerplate of imperative input.parse()? calls.
Add this to your Cargo.toml:
[dependencies]
vacro-parser = "0.1.4"
define!: Define Parsing StructsUse define! to define a struct that automatically implements syn::parse::Parse.
use syn::{Ident, Type, GenericParam, FnArg, parse_macro_input};
// Define a struct named MyFn, it automatically implements the Parse trait
vacro::define!(MyFn:
fn
#(?: <#(generic*[,]: GenericParam)>)
#(name: Ident)
( #(args*[,]: FnArg) )
#(?: -> #(ret: Type))
);
// Usage in a proc-macro
// fn parse_my_fn(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
// let my_fn = parse_macro_input!(input as MyFn);
// println!("Function name: {}", my_fn.name);
// proc_macro::TokenStream::new()
// }
bind!: On-the-fly ParsingUse bind! to consume a portion of a TokenStream within existing imperative logic.
use syn::{Ident, Type, Token};
use vacro::bind;
fn parser(input: syn::parse::ParseStream) -> syn::Result<()> {
// Parse a function signature pattern on the fly
bind!(
let captured = (input ->
fn #(name: Ident) #(?: -> #(ret: Type))
)?;
);
// Access captured fields directly
println!("Name: {}", captured.name);
if let Some(ret_type) = captured.ret {
// ...
}
Ok(())
}
| Syntax | Description | Example |
|---|---|---|
literal |
Matches exact tokens | fn, ->, struct |
#(x: T) |
Named Capture: Captures type T into field x |
#(name: Ident) |
#(x?: T) |
Optional Capture: Option<T> |
#(ret?: Type) |
#(x*[sep]: T) |
Iterative Capture: Punctuated<T, sep> |
#(args*[,]: FnArg) |
#(T) |
Anonymous Match: Validates T exists but doesn't capture |
#(Ident) |
Licensed under either of Apache License, Version 2.0 or MIT license at your option.