David Tolnay | 14cbdeb | 2016-10-01 12:13:59 -0700 | [diff] [blame] | 1 | use nom::IResult; |
David Tolnay | def6637 | 2016-10-24 21:51:32 -0700 | [diff] [blame^] | 2 | use space::{skip_whitespace, word_break}; |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 3 | |
| 4 | macro_rules! punct { |
| 5 | ($i:expr, $punct:expr) => { |
David Tolnay | 13e5da4 | 2016-09-04 16:18:34 -0700 | [diff] [blame] | 6 | $crate::helper::punct($i, $punct) |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 7 | }; |
| 8 | } |
| 9 | |
David Tolnay | 13e5da4 | 2016-09-04 16:18:34 -0700 | [diff] [blame] | 10 | pub fn punct<'a>(input: &'a str, token: &'static str) -> IResult<&'a str, &'a str> { |
David Tolnay | def6637 | 2016-10-24 21:51:32 -0700 | [diff] [blame^] | 11 | let input = skip_whitespace(input); |
David Tolnay | 14cbdeb | 2016-10-01 12:13:59 -0700 | [diff] [blame] | 12 | if input.starts_with(token) { |
| 13 | IResult::Done(&input[token.len()..], token) |
| 14 | } else { |
| 15 | IResult::Error |
David Tolnay | 13e5da4 | 2016-09-04 16:18:34 -0700 | [diff] [blame] | 16 | } |
David Tolnay | 13e5da4 | 2016-09-04 16:18:34 -0700 | [diff] [blame] | 17 | } |
| 18 | |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 19 | macro_rules! keyword { |
| 20 | ($i:expr, $keyword:expr) => { |
| 21 | $crate::helper::keyword($i, $keyword) |
| 22 | }; |
| 23 | } |
| 24 | |
| 25 | pub fn keyword<'a>(input: &'a str, token: &'static str) -> IResult<&'a str, &'a str> { |
| 26 | match punct(input, token) { |
| 27 | IResult::Done(rest, _) => { |
| 28 | match word_break(rest) { |
| 29 | IResult::Done(_, _) => IResult::Done(rest, token), |
| 30 | IResult::Error => IResult::Error, |
| 31 | } |
| 32 | } |
| 33 | IResult::Error => IResult::Error, |
| 34 | } |
| 35 | } |
| 36 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 37 | macro_rules! option { |
| 38 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 39 | match $submac!($i, $($args)*) { |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 40 | $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, Some(o)), |
| 41 | $crate::nom::IResult::Error => $crate::nom::IResult::Done($i, None), |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 42 | } |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 43 | }; |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 44 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 45 | ($i:expr, $f:expr) => { |
| 46 | option!($i, call!($f)); |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | macro_rules! opt_vec { |
| 51 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 52 | match $submac!($i, $($args)*) { |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 53 | $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, o), |
| 54 | $crate::nom::IResult::Error => $crate::nom::IResult::Done($i, Vec::new()), |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 55 | } |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 56 | }; |
| 57 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 58 | |
| 59 | macro_rules! epsilon { |
| 60 | ($i:expr,) => { |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 61 | $crate::nom::IResult::Done($i, ()) |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | macro_rules! tap { |
| 66 | ($i:expr, $name:ident : $submac:ident!( $($args:tt)* ) => $e:expr) => { |
| 67 | match $submac!($i, $($args)*) { |
| 68 | $crate::nom::IResult::Done(i, o) => { |
| 69 | let $name = o; |
| 70 | $e; |
| 71 | $crate::nom::IResult::Done(i, ()) |
| 72 | } |
| 73 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
| 74 | } |
| 75 | }; |
| 76 | |
| 77 | ($i:expr, $name:ident : $f:expr => $e:expr) => { |
| 78 | tap!($i, $name: call!($f) => $e); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 79 | }; |
| 80 | } |
David Tolnay | 674258d | 2016-10-08 13:30:45 -0700 | [diff] [blame] | 81 | |
| 82 | macro_rules! separated_list { |
| 83 | ($i:expr, punct!($sep:expr), $f:expr) => { |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 84 | $crate::helper::separated_list($i, $sep, $f, false) |
David Tolnay | 674258d | 2016-10-08 13:30:45 -0700 | [diff] [blame] | 85 | }; |
| 86 | } |
| 87 | |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 88 | macro_rules! terminated_list { |
| 89 | ($i:expr, punct!($sep:expr), $f:expr) => { |
| 90 | $crate::helper::separated_list($i, $sep, $f, true) |
| 91 | }; |
| 92 | } |
| 93 | |
David Tolnay | c7f646a | 2016-10-16 10:54:39 -0700 | [diff] [blame] | 94 | pub fn separated_list<'a, T>(mut input: &'a str, |
| 95 | sep: &'static str, |
| 96 | f: fn(&'a str) -> IResult<&'a str, T>, |
| 97 | terminated: bool) |
| 98 | -> IResult<&'a str, Vec<T>> { |
David Tolnay | 674258d | 2016-10-08 13:30:45 -0700 | [diff] [blame] | 99 | let mut res = Vec::new(); |
| 100 | |
| 101 | // get the first element |
| 102 | match f(input) { |
| 103 | IResult::Error => IResult::Done(input, Vec::new()), |
| 104 | IResult::Done(i, o) => { |
| 105 | if i.len() == input.len() { |
| 106 | IResult::Error |
| 107 | } else { |
| 108 | res.push(o); |
| 109 | input = i; |
| 110 | |
| 111 | // get the separator first |
| 112 | while let IResult::Done(i2, _) = punct(input, sep) { |
| 113 | if i2.len() == input.len() { |
| 114 | break; |
| 115 | } |
| 116 | |
| 117 | // get the element next |
| 118 | if let IResult::Done(i3, o3) = f(i2) { |
| 119 | if i3.len() == i2.len() { |
| 120 | break; |
| 121 | } |
| 122 | res.push(o3); |
| 123 | input = i3; |
| 124 | } else { |
| 125 | break; |
| 126 | } |
| 127 | } |
David Tolnay | ff46fd2 | 2016-10-08 13:53:28 -0700 | [diff] [blame] | 128 | if terminated { |
| 129 | if let IResult::Done(after, _) = punct(input, sep) { |
| 130 | input = after; |
| 131 | } |
| 132 | } |
David Tolnay | 674258d | 2016-10-08 13:30:45 -0700 | [diff] [blame] | 133 | IResult::Done(input, res) |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |