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