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