Skip to content

Instantly share code, notes, and snippets.

@bzm3r
Created July 16, 2023 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bzm3r/68f8ecb371ace90ce5af3fef624750a0 to your computer and use it in GitHub Desktop.
Save bzm3r/68f8ecb371ace90ce5af3fef624750a0 to your computer and use it in GitHub Desktop.
meta generation of macros
use paste::paste;
macro_rules! get_char_len {
($x:tt) => {
stringify!($x).len()
}
}
macro_rules! one {
$(x:tt) => {
1
}
}
macro_rules! meta_def_generator_dd {
(
$name:ident |
{
$(
( $($meta_matcher_dd:tt)* ) => { $($meta_body_dd:tt)* }
);+
} |
$bang:tt | $d:tt
) => {
macro_rules $bang $name {
$(
( $($meta_matcher)* ) => { $($meta_body)* }
);+
}
}
}
macro_rules! meta_counter_def {
( $name:ident ) => {
meta_def_generator_dd!(
$name:ident |
{
// ( () | ( $($current:tt)* ) | [ $map:ident ] )
( () | ( $dd ($dd current:tt)* ) | [ $dd map:ident ] ) => {
vec![$dd ($dd current)*]
};
// ( ( $first:tt $( $rest:tt )* ) | ( $($current:tt)* ) | [ $map:ident ] )
( ( $dd first:tt $dd ( $dd rest:tt )* ) | ( $dd ($dd current:tt)* ) | [ $dd map:ident ] ) => {
counter!( ( $dd ( $dd rest )* ) | $dd map!($dd first) + $dd ($dd current)* )
};
// ( ( $first:tt $( $rest:tt )* ) | [ $map:ident ] )
( ( $dd first:tt $dd ( $dd rest:tt )* ) | [ $dd map:ident ] ) => {
counter!( ( $dd ( $dd rest )* ) | $dd map!($dd first) )
};
} |
! | $
)
}
}
macro_rules! meta_generate {
( $macro_meta_def:ident, $name:ident ) => {
$macro_meta_def!($name)
}
}
macro_rules! meta_call_with_dollar {
( ( $macro_name:ident, $($args:tt)* ) | $bang:tt ) => {
[< _ $macro_name >] ! ( $($args:tt)* | $)
}
}
macro_rules! meta_generate_then_call {
( $macro_name:ident, $meta_macro_def:ident, $($args:tt)* ) => {
meta_generate( $meta_macro_def | ! | $ )
meta_call_with_dollar( $macro_name, $($args:tt)* )
}
}
macro_rules! generate {
( $macro_generator:ident, $macro_name:ident, $($args:tt)* ) => {
$macro_generator!( $($args:tt)* | ! | $ );
}
}
macro_rules! call_with_dollar {
( $macro_name:ident, $($args:tt)* ) => {
[< _ $macro_name >]!($($args:tt)* | $)
}
}
macro_rules! counter {
( () | $($current:tt)* $([ $map:ident ])? ) => {
vec![$($current)*]
};
( ( $first:tt $( $rest:tt )* ) | $($current:tt)* | $([ $map:ident ])? ) => {
counter!( ( $( $rest )* ) | $map!($first), $($current)* )
};
( ( $first:tt $( $rest:tt )* ) ) => {
counter!( ( $( $rest )* ) | get_char_len!($first) )
};
}
macro_rules! count_len {
($($input:tt)*) => {
fn tally_count() -> usize {
counter!( ($($input)*) )
}
println!("\"{}\" has {} token-trees", stringify!($($input)*), tally_count() );
}
}
// macro_rules! cascade1 {
// ( ($($paste_start:tt)*), ($($paste_end:tt)*) | $d:tt ) => {
// macro_rules! cascade_final {
// () => {
// println!(stringify!($d $($paste_start:tt)* $var _world ($($paste_end:tt)*) ));
// }
// }
// }
// }
// macro_rules! cascade0 {
// ( $var:ident ) => {
// cascade1!($);
// }
// }
fn main() {
// cascade0!(hello);
// count_len!(hello);
tester!()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment