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> { |
| 12 | let mut chars = input.char_indices(); |
| 13 | while let Some((i, ch)) = chars.next() { |
| 14 | if !ch.is_whitespace() { |
| 15 | return if input[i..].starts_with(token) { |
| 16 | let end = i + token.len(); |
| 17 | IResult::Done(&input[end..], &input[i..end]) |
| 18 | } else { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame^] | 19 | IResult::Error |
David Tolnay | 13e5da4 | 2016-09-04 16:18:34 -0700 | [diff] [blame] | 20 | }; |
| 21 | } |
| 22 | } |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame^] | 23 | IResult::Error |
David Tolnay | 13e5da4 | 2016-09-04 16:18:34 -0700 | [diff] [blame] | 24 | } |
| 25 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame^] | 26 | macro_rules! option { |
| 27 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 28 | match $submac!($i, $($args)*) { |
| 29 | ::nom::IResult::Done(i, o) => ::nom::IResult::Done(i, Some(o)), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame^] | 30 | ::nom::IResult::Error => ::nom::IResult::Done($i, None), |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 31 | } |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame^] | 32 | }; |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 33 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame^] | 34 | ($i:expr, $f:expr) => { |
| 35 | option!($i, call!($f)); |
| 36 | }; |
| 37 | } |
| 38 | |
| 39 | macro_rules! opt_vec { |
| 40 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 41 | match $submac!($i, $($args)*) { |
| 42 | ::nom::IResult::Done(i, o) => ::nom::IResult::Done(i, o), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame^] | 43 | ::nom::IResult::Error => ::nom::IResult::Done($i, Vec::new()), |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 44 | } |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame^] | 45 | }; |
| 46 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 47 | |
| 48 | macro_rules! epsilon { |
| 49 | ($i:expr,) => { |
| 50 | call!($i, { |
| 51 | fn epsilon<T>(input: T) -> ::nom::IResult<T, ()> { |
| 52 | ::nom::IResult::Done(input, ()) |
| 53 | } |
| 54 | epsilon |
| 55 | }) |
| 56 | }; |
| 57 | } |