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 | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame^] | 4 | use unicode_xid::UnicodeXID; |
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 | aed77b0 | 2016-09-23 20:50:31 -0700 | [diff] [blame] | 13 | for (i, ch) in input.char_indices() { |
David Tolnay | 13e5da4 | 2016-09-04 16:18:34 -0700 | [diff] [blame] | 14 | if !ch.is_whitespace() { |
| 15 | return if input[i..].starts_with(token) { |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame^] | 16 | IResult::Done(&input[i + token.len()..], token) |
David Tolnay | 13e5da4 | 2016-09-04 16:18:34 -0700 | [diff] [blame] | 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 | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame^] | 25 | macro_rules! keyword { |
| 26 | ($i:expr, $keyword:expr) => { |
| 27 | $crate::helper::keyword($i, $keyword) |
| 28 | }; |
| 29 | } |
| 30 | |
| 31 | pub fn keyword<'a>(input: &'a str, token: &'static str) -> IResult<&'a str, &'a str> { |
| 32 | match punct(input, token) { |
| 33 | IResult::Done(rest, _) => { |
| 34 | match word_break(rest) { |
| 35 | IResult::Done(_, _) => IResult::Done(rest, token), |
| 36 | IResult::Error => IResult::Error, |
| 37 | } |
| 38 | } |
| 39 | IResult::Error => IResult::Error, |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | pub fn word_break<'a>(input: &'a str) -> IResult<&'a str, ()> { |
| 44 | match input.chars().next() { |
| 45 | Some(ch) if UnicodeXID::is_xid_continue(ch) => { |
| 46 | IResult::Error |
| 47 | } |
| 48 | Some(_) | None => { |
| 49 | IResult::Done(input, ()) |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 54 | macro_rules! option { |
| 55 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 56 | match $submac!($i, $($args)*) { |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 57 | $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, Some(o)), |
| 58 | $crate::nom::IResult::Error => $crate::nom::IResult::Done($i, None), |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 59 | } |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 60 | }; |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 61 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 62 | ($i:expr, $f:expr) => { |
| 63 | option!($i, call!($f)); |
| 64 | }; |
| 65 | } |
| 66 | |
| 67 | macro_rules! opt_vec { |
| 68 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 69 | match $submac!($i, $($args)*) { |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 70 | $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, o), |
| 71 | $crate::nom::IResult::Error => $crate::nom::IResult::Done($i, Vec::new()), |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 72 | } |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 73 | }; |
| 74 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 75 | |
| 76 | macro_rules! epsilon { |
| 77 | ($i:expr,) => { |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 78 | $crate::nom::IResult::Done($i, ()) |
| 79 | }; |
| 80 | } |
| 81 | |
| 82 | macro_rules! tap { |
| 83 | ($i:expr, $name:ident : $submac:ident!( $($args:tt)* ) => $e:expr) => { |
| 84 | match $submac!($i, $($args)*) { |
| 85 | $crate::nom::IResult::Done(i, o) => { |
| 86 | let $name = o; |
| 87 | $e; |
| 88 | $crate::nom::IResult::Done(i, ()) |
| 89 | } |
| 90 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | ($i:expr, $name:ident : $f:expr => $e:expr) => { |
| 95 | tap!($i, $name: call!($f) => $e); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 96 | }; |
| 97 | } |