David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 1 | #![cfg(feature = "parsing")] |
| 2 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 3 | use nom::{self, IResult}; |
| 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 { |
| 19 | IResult::Error(nom::Err::Position(nom::ErrorKind::TagStr, input)) |
| 20 | }; |
| 21 | } |
| 22 | } |
| 23 | IResult::Error(nom::Err::Position(nom::ErrorKind::TagStr, input)) |
| 24 | } |
| 25 | |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 26 | macro_rules! option ( |
| 27 | ($i:expr, $submac:ident!( $($args:tt)* )) => ({ |
| 28 | match $submac!($i, $($args)*) { |
| 29 | ::nom::IResult::Done(i, o) => ::nom::IResult::Done(i, Some(o)), |
| 30 | ::nom::IResult::Error(_) => ::nom::IResult::Done($i, None), |
| 31 | ::nom::IResult::Incomplete(_) => ::nom::IResult::Done($i, None), |
| 32 | } |
| 33 | }); |
| 34 | ($i:expr, $f:expr) => ( |
| 35 | option!($i, call!($f)); |
| 36 | ); |
| 37 | ); |
| 38 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 39 | macro_rules! opt_vec ( |
| 40 | ($i:expr, $submac:ident!( $($args:tt)* )) => ({ |
| 41 | match $submac!($i, $($args)*) { |
| 42 | ::nom::IResult::Done(i, o) => ::nom::IResult::Done(i, o), |
| 43 | ::nom::IResult::Error(_) => ::nom::IResult::Done($i, Vec::new()), |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 44 | ::nom::IResult::Incomplete(_) => ::nom::IResult::Done($i, Vec::new()), |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 45 | } |
| 46 | }); |
| 47 | ); |
| 48 | |
| 49 | macro_rules! epsilon { |
| 50 | ($i:expr,) => { |
| 51 | call!($i, { |
| 52 | fn epsilon<T>(input: T) -> ::nom::IResult<T, ()> { |
| 53 | ::nom::IResult::Done(input, ()) |
| 54 | } |
| 55 | epsilon |
| 56 | }) |
| 57 | }; |
| 58 | } |