- ast.rs: Full AST for .mrs files (MacroDef, Pattern, OutputBody, TemplateItem) - parser.rs: Hand-written recursive descent parser with line/col error reporting - codegen.rs: Transforms AST to Rust proc macro code using syn/quote/proc_macro2 - main.rs: CLI reads .mrs file, runs parse→codegen, writes to stdout or file - Cargo.toml: Workspace with syn, quote, proc-macro2, prettyplease deps - examples/: hello.mrs, derive_builder.mrs, declare_table.mrs reference examples All three reference examples parse and produce prettyplease-formatted Rust output. Generated code uses syn::parse::Parser closures for structural patterns, quote! for token templates, spread iteration via .iter().map().collect(), and format_ident! for identifier construction. https://claude.ai/code/session_01Tppkab4eLsL21asHGjQJoF
22 lines
646 B
Plaintext
22 lines
646 B
Plaintext
macro derive_builder! {
|
|
struct $name:ident { $($field:ident : $ty:ty),* } => {
|
|
@{
|
|
let builder_name = format_ident!("{}_Builder", name.to_string());
|
|
}
|
|
T~{
|
|
pub struct ${ builder_name } {
|
|
for field in $fields =>
|
|
pub $field.name: Option<$field.ty>,
|
|
}
|
|
impl ${ builder_name } {
|
|
pub fn build(self) -> $name {
|
|
$name {
|
|
for field in $fields =>
|
|
$field.name: self.$field.name.unwrap(),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|