Skip to content

Instantly share code, notes, and snippets.

@Tristramg
Created August 9, 2019 13:21
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/40f4aba693adaa9b0cafb3e17a122efe to your computer and use it in GitHub Desktop.
Save Tristramg/40f4aba693adaa9b0cafb3e17a122efe 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))};
($ns:ident : $val:ident) => {concat!(stringify!($ns), ":", stringify($val))}
);
macro_rules! opt_whitespace(
() => {""};
(.$($tail:tt)*) => {concat!(".\n", next_token!($($tail)*))};
($($tail:tt)*) => {concat!(" ", next_token!($($tail)*))};
);
macro_rules! next_token(
() => {""};
($ns:ident : $val:ident $($tail:tt)*) => {concat!(fmt!($ns : $val), opt_whitespace!($($tail)*))};
(?$var:ident $($tail:tt)*) => {concat!(fmt!(?$var), opt_whitespace!($($tail)*))};
($any:tt $($tail:tt)*) => {concat!(fmt!($any), opt_whitespace!($($tail)*))};
);
macro_rules! select(
($($var:tt),* where $($statement:tt)*) => {
concat!(
"SELECT",
select_variables!($($var)*),
"\nWHERE\n",
next_token!($($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