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