Skip to content

Instantly share code, notes, and snippets.

@dherman
Created December 25, 2018 14:32
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 dherman/fac5fe937646117bdf41febffc7c8243 to your computer and use it in GitHub Desktop.
Save dherman/fac5fe937646117bdf41febffc7c8243 to your computer and use it in GitHub Desktop.
macro_rules! const1 {
($id:ident, $depth:expr, [$($prefix:tt)*])
=>
{
const $id: [&'static str; $depth] = [$($prefix),*];
};
}
macro_rules! const_tree_at {
($depth:tt ;
[$($prefix:tt)*] ;
;)
=>
{ };
($depth:tt ;
[$($prefix:tt)*] ;
{ })
=>
{ };
($depth:tt ;
[$($prefix:tt)*] ;
{ $name:tt => $id:ident $subtree:tt $($more:tt)* })
=>
{
const1!($id, ($depth + 1), [$($prefix)* $name]);
const_tree_at!(($depth + 1) ; [ $($prefix)* $name ] ; $subtree);
const_tree_at!($depth ; [$($prefix)*] ; { $($more)* });
};
}
macro_rules! const_tree {
($($t:tt)*) => {
const_tree_at!(0 ; [] ; { $($t)* });
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment