Skip to content

Instantly share code, notes, and snippets.

@Tristramg
Created August 9, 2019 12:53
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 Tristramg/8cd7e61c1390a38c53c2210da71e0f84 to your computer and use it in GitHub Desktop.
Save Tristramg/8cd7e61c1390a38c53c2210da71e0f84 to your computer and use it in GitHub Desktop.
macro_rules! select_variables (
() => {""};
($head:ident $($tail:ident)*) => {
concat!(" ?", stringify!($head), select_variables!($($tail)*));
};
);
macro_rules! fmt(
($a:tt) => {stringify!($a)};
(?$a:tt) => {concat!("?", stringify!($a))};
);
macro_rules! single_statement(
($a:tt $b:tt $c:tt) => {concat!(fmt!($a), " ", fmt!($b), " ", fmt!($c), ".\n");};
(?$a:tt $b:tt $c:tt) => {concat!(fmt!(?$a), " ", fmt!($b), " ", fmt!($c), ".\n");};
($a:tt ?$b:tt $c:tt) => {concat!(fmt!($a), " ", fmt!(?$b), " ", fmt!($c), ".\n");};
($a:tt $b:tt ?$c:tt) => {concat!(fmt!($a), " ", fmt!($b), " ", fmt!(?$c), ".\n");};
(?$a:tt ?$b:tt $c:tt) => {concat!(fmt!(?$a), " ", fmt!(?$b), " ", fmt!($c), ".\n");};
(?$a:tt $b:tt $c:tt) => {concat!(fmt!(?$a), " ", fmt!($b), " ", fmt!(?$c), ".\n");};
($a:tt ?$b:tt ?$c:tt) => {concat!(fmt!($a), " ", fmt!(?$b), " ", fmt!(?$c), ".\n");};
(?$a:tt ?$b:tt ?$c:tt) => {concat!(fmt!(?$a), " ", fmt!(?$b), " ", fmt!(?$c), ".\n");};
);
macro_rules! statement(
() => {""};
($a:tt $b:tt $c:tt. $($tail:tt)*) => {concat!(single_statement!($a $b $c), statement!($($tail)*));};
(?$a:tt $b:tt $c:tt. $($tail:tt)*) => {concat!(single_statement!(?$a $b $c), statement!($($tail)*));};
($a:tt ?$b:tt $c:tt. $($tail:tt)*) => {concat!(single_statement!($a ?$b $c), statement!($($tail)*));};
($a:tt $b:tt ?$c:tt. $($tail:tt)*) => {concat!(single_statement!($a $b ?$c), statement!($($tail)*));};
(?$a:tt ?$b:tt $c:tt. $($tail:tt)*) => {concat!(single_statement!(?$a ?$b $c), statement!($($tail)*));};
(?$a:tt $b:tt $c:tt. ?$($tail:tt)*) => {concat!(single_statement!(?$a $b ?$c), statement!($($tail)*));};
($a:tt ?$b:tt ?$c:tt. $($tail:tt)*) => {concat!(single_statement!($a ?$b ?$c), statement!($($tail)*));};
(?$a:tt ?$b:tt ?$c:tt. $($tail:tt)*) => {concat!(single_statement!(?$a ?$b ?$c), statement!($($tail)*));};
);
macro_rules! select(
($($var:tt),* where $($statement:tt)*) => {
concat!(
"SELECT",
select_variables!($($var)*),
"\nWHERE\n",
statement!($($statement)*),
"SERVICE wikibase:label { bd:serviceParam wikibase:language \"en\". }"
);
};
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment