Skip to content

Instantly share code, notes, and snippets.

@RReverser
Last active November 30, 2017 16:33
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 RReverser/afee1c9f23d475604710da657a4b646f to your computer and use it in GitHub Desktop.
Save RReverser/afee1c9f23d475604710da657a4b646f to your computer and use it in GitHub Desktop.
macro_rules! err {
($i:expr, $kind: expr) => {
IResult::Error(error_position!($kind, $i))
};
}
macro_rules! alt_switch {
($i:expr, $($lhs_mac:ident ! ( $($lhs_arg:tt)* ) $(as $lhs_binding:pat)* => $rhs_mac:ident ! ( $($rhs_arg:tt)* ) |)+ _ => $def_mac:ident ! ( $($def_arg:tt)* )) => {{
$(if let IResult::Done(i, $($lhs_binding,)* ..) = $lhs_mac ! ( $i, $($lhs_arg)* ) {
$rhs_mac ! ( i, $($rhs_arg)* )
} else )+ {
$def_mac ! ( $i, $($def_arg)* )
}
}};
}
alt_switch!(
char!('(') => terminated!(sub_parse, char!(')') |
char!('!') => map!(sub_parse, Not) |
_ => err!(ErrorKind::Alt)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment