-
-
Save durka/bed7b5d9b590c98173a3 to your computer and use it in GitHub Desktop.
Simple TT muncher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// switch to nightly and uncomment these for debugging | |
//#![feature(trace_macros)] | |
//trace_macros!(true); | |
/// Trick the macro parser into outputting an item without checking its syntax | |
macro_rules! as_item( ($i:item) => ($i) ); | |
macro_rules! foo( | |
// first rule: define the invocation syntax and call myself again with some | |
// easily parseable parameters (notice everything is bracketed) | |
( | |
pub struct $name:ident $body:tt | |
) => ( | |
foo!(parse [pub struct $name] [] $body); | |
); | |
// the following rules are all internal to the macro | |
// this is the base case: all struct members have been munched, | |
// so proceed to output | |
(parse $decl:tt [$($member:tt)*] {}) | |
=> ( foo!(output $decl [$($member)*]); ); | |
// parse one public struct member, then call myself to parse the rest | |
(parse $decl:tt [$($member:tt)*] { pub $name:ident: $typ:ty, $($t:tt)* }) | |
=> ( foo!(parse $decl [$($member)* pub $name: $typ,] { $($t)* }); ); | |
// parse one private struct member, then call myself to parse the rest | |
(parse $decl:tt [$($member:tt)*] { $name:ident: $typ:ty, $($t:tt)* }) | |
=> ( foo!(parse $decl [$($member)* $name: $typ,] { $($t)* }); ); | |
// output the finished struct | |
(output [$($decls:tt)*] [$($member:tt)*]) | |
=> ( as_item!(#[derive(Debug)] $($decls)* { $($member)* }); ); | |
); | |
foo! { | |
pub struct Foo { | |
pub bar: usize, | |
baz: isize, // <-- Suppose, this should be private. | |
} | |
} | |
fn main() { | |
println!("{:#?}", Foo { bar: 0, baz: 0 }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment