David Tolnay | 14cbdeb | 2016-10-01 12:13:59 -0700 | [diff] [blame] | 1 | use nom::IResult; |
| 2 | use space::{whitespace, word_break}; |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 3 | |
| 4 | macro_rules! punct { |
| 5 | ($i:expr, $punct:expr) => { |
David Tolnay | 13e5da4 | 2016-09-04 16:18:34 -0700 | [diff] [blame] | 6 | $crate::helper::punct($i, $punct) |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 7 | }; |
| 8 | } |
| 9 | |
David Tolnay | 13e5da4 | 2016-09-04 16:18:34 -0700 | [diff] [blame] | 10 | pub fn punct<'a>(input: &'a str, token: &'static str) -> IResult<&'a str, &'a str> { |
David Tolnay | 14cbdeb | 2016-10-01 12:13:59 -0700 | [diff] [blame] | 11 | let input = match whitespace(input) { |
| 12 | IResult::Done(rest, _) => rest, |
| 13 | IResult::Error => input, |
| 14 | }; |
| 15 | if input.starts_with(token) { |
| 16 | IResult::Done(&input[token.len()..], token) |
| 17 | } else { |
| 18 | IResult::Error |
David Tolnay | 13e5da4 | 2016-09-04 16:18:34 -0700 | [diff] [blame] | 19 | } |
David Tolnay | 13e5da4 | 2016-09-04 16:18:34 -0700 | [diff] [blame] | 20 | } |
| 21 | |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 22 | macro_rules! keyword { |
| 23 | ($i:expr, $keyword:expr) => { |
| 24 | $crate::helper::keyword($i, $keyword) |
| 25 | }; |
| 26 | } |
| 27 | |
| 28 | pub fn keyword<'a>(input: &'a str, token: &'static str) -> IResult<&'a str, &'a str> { |
| 29 | match punct(input, token) { |
| 30 | IResult::Done(rest, _) => { |
| 31 | match word_break(rest) { |
| 32 | IResult::Done(_, _) => IResult::Done(rest, token), |
| 33 | IResult::Error => IResult::Error, |
| 34 | } |
| 35 | } |
| 36 | IResult::Error => IResult::Error, |
| 37 | } |
| 38 | } |
| 39 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 40 | macro_rules! option { |
| 41 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 42 | match $submac!($i, $($args)*) { |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 43 | $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, Some(o)), |
| 44 | $crate::nom::IResult::Error => $crate::nom::IResult::Done($i, None), |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 45 | } |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 46 | }; |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 47 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 48 | ($i:expr, $f:expr) => { |
| 49 | option!($i, call!($f)); |
| 50 | }; |
| 51 | } |
| 52 | |
| 53 | macro_rules! opt_vec { |
| 54 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 55 | match $submac!($i, $($args)*) { |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 56 | $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, o), |
| 57 | $crate::nom::IResult::Error => $crate::nom::IResult::Done($i, Vec::new()), |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 58 | } |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 59 | }; |
| 60 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 61 | |
| 62 | macro_rules! epsilon { |
| 63 | ($i:expr,) => { |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 64 | $crate::nom::IResult::Done($i, ()) |
| 65 | }; |
| 66 | } |
| 67 | |
| 68 | macro_rules! tap { |
| 69 | ($i:expr, $name:ident : $submac:ident!( $($args:tt)* ) => $e:expr) => { |
| 70 | match $submac!($i, $($args)*) { |
| 71 | $crate::nom::IResult::Done(i, o) => { |
| 72 | let $name = o; |
| 73 | $e; |
| 74 | $crate::nom::IResult::Done(i, ()) |
| 75 | } |
| 76 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | ($i:expr, $name:ident : $f:expr => $e:expr) => { |
| 81 | tap!($i, $name: call!($f) => $e); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 82 | }; |
| 83 | } |