David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 1 | #![cfg(feature = "parsing")] |
| 2 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 3 | use nom::IResult; |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 4 | |
| 5 | macro_rules! punct { |
| 6 | ($i:expr, $punct:expr) => { |
David Tolnay | 13e5da4 | 2016-09-04 16:18:34 -0700 | [diff] [blame] | 7 | $crate::helper::punct($i, $punct) |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 8 | }; |
| 9 | } |
| 10 | |
David Tolnay | 13e5da4 | 2016-09-04 16:18:34 -0700 | [diff] [blame] | 11 | pub fn punct<'a>(input: &'a str, token: &'static str) -> IResult<&'a str, &'a str> { |
David Tolnay | aed77b0 | 2016-09-23 20:50:31 -0700 | [diff] [blame] | 12 | for (i, ch) in input.char_indices() { |
David Tolnay | 13e5da4 | 2016-09-04 16:18:34 -0700 | [diff] [blame] | 13 | if !ch.is_whitespace() { |
| 14 | return if input[i..].starts_with(token) { |
| 15 | let end = i + token.len(); |
| 16 | IResult::Done(&input[end..], &input[i..end]) |
| 17 | } else { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 18 | IResult::Error |
David Tolnay | 13e5da4 | 2016-09-04 16:18:34 -0700 | [diff] [blame] | 19 | }; |
| 20 | } |
| 21 | } |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 22 | IResult::Error |
David Tolnay | 13e5da4 | 2016-09-04 16:18:34 -0700 | [diff] [blame] | 23 | } |
| 24 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 25 | macro_rules! option { |
| 26 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 27 | match $submac!($i, $($args)*) { |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 28 | $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, Some(o)), |
| 29 | $crate::nom::IResult::Error => $crate::nom::IResult::Done($i, None), |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 30 | } |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 31 | }; |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 32 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 33 | ($i:expr, $f:expr) => { |
| 34 | option!($i, call!($f)); |
| 35 | }; |
| 36 | } |
| 37 | |
| 38 | macro_rules! opt_vec { |
| 39 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 40 | match $submac!($i, $($args)*) { |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 41 | $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, o), |
| 42 | $crate::nom::IResult::Error => $crate::nom::IResult::Done($i, Vec::new()), |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 43 | } |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 44 | }; |
| 45 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 46 | |
| 47 | macro_rules! epsilon { |
| 48 | ($i:expr,) => { |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 49 | $crate::nom::IResult::Done($i, ()) |
| 50 | }; |
| 51 | } |
| 52 | |
| 53 | macro_rules! tap { |
| 54 | ($i:expr, $name:ident : $submac:ident!( $($args:tt)* ) => $e:expr) => { |
| 55 | match $submac!($i, $($args)*) { |
| 56 | $crate::nom::IResult::Done(i, o) => { |
| 57 | let $name = o; |
| 58 | $e; |
| 59 | $crate::nom::IResult::Done(i, ()) |
| 60 | } |
| 61 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | ($i:expr, $name:ident : $f:expr => $e:expr) => { |
| 66 | tap!($i, $name: call!($f) => $e); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 67 | }; |
| 68 | } |