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)*) { |
| 28 | ::nom::IResult::Done(i, o) => ::nom::IResult::Done(i, Some(o)), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 29 | ::nom::IResult::Error => ::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)*) { |
| 41 | ::nom::IResult::Done(i, o) => ::nom::IResult::Done(i, o), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 42 | ::nom::IResult::Error => ::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 | 3a14d6b | 2016-09-14 00:00:06 -0700 | [diff] [blame] | 49 | value!($i, ()) |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 50 | }; |
| 51 | } |