David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 1 | // Adapted from nom <https://github.com/Geal/nom> by removing the |
| 2 | // IResult::Incomplete variant, which we don't use and which unfortunately more |
| 3 | // than doubles the compilation time. |
| 4 | |
| 5 | #[derive(Debug, PartialEq, Eq, Clone)] |
| 6 | pub enum IResult<I, O> { |
| 7 | /// indicates a correct parsing, the first field containing the rest of the |
| 8 | /// unparsed data, the second field contains the parsed data |
| 9 | Done(I, O), |
| 10 | Error, |
| 11 | } |
| 12 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 13 | macro_rules! named { |
| 14 | ($name:ident -> $o:ty, $submac:ident!( $($args:tt)* )) => { |
| 15 | fn $name(i: &str) -> $crate::nom::IResult<&str, $o> { |
| 16 | $submac!(i, $($args)*) |
| 17 | } |
| 18 | }; |
| 19 | |
| 20 | (pub $name:ident -> $o:ty, $submac:ident!( $($args:tt)* )) => { |
| 21 | pub fn $name(i: &str) -> $crate::nom::IResult<&str, $o> { |
| 22 | $submac!(i, $($args)*) |
| 23 | } |
| 24 | }; |
| 25 | } |
| 26 | |
| 27 | macro_rules! call { |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 28 | ($i:expr, $fun:expr $(, $args:expr)*) => { |
| 29 | $fun($i $(, $args)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 30 | }; |
| 31 | } |
| 32 | |
| 33 | macro_rules! map { |
| 34 | ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 35 | map_impl!($i, $submac!($($args)*), call!($g)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | ($i:expr, $f:expr, $g:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 39 | map_impl!($i, call!($f), call!($g)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 40 | }; |
| 41 | } |
| 42 | |
| 43 | /// Internal parser, do not use directly |
| 44 | macro_rules! map_impl { |
| 45 | ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => { |
| 46 | match $submac!($i, $($args)*) { |
| 47 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
| 48 | $crate::nom::IResult::Done(i, o) => { |
| 49 | $crate::nom::IResult::Done(i, $submac2!(o, $($args2)*)) |
| 50 | } |
| 51 | } |
| 52 | }; |
| 53 | } |
| 54 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 55 | macro_rules! not { |
| 56 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
| 57 | match $submac!($i, $($args)*) { |
| 58 | $crate::nom::IResult::Done(_, _) => $crate::nom::IResult::Error, |
| 59 | $crate::nom::IResult::Error => $crate::nom::IResult::Done($i, ""), |
| 60 | } |
| 61 | }; |
| 62 | } |
| 63 | |
David Tolnay | eea28d6 | 2016-10-25 20:44:08 -0700 | [diff] [blame] | 64 | // This is actually nom's cond_with_error. |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 65 | macro_rules! cond { |
| 66 | ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => { |
| 67 | if $cond { |
| 68 | match $submac!($i, $($args)*) { |
David Tolnay | de95548 | 2016-10-08 13:10:27 -0700 | [diff] [blame] | 69 | $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, ::std::option::Option::Some(o)), |
David Tolnay | eea28d6 | 2016-10-25 20:44:08 -0700 | [diff] [blame] | 70 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 71 | } |
| 72 | } else { |
| 73 | $crate::nom::IResult::Done($i, ::std::option::Option::None) |
| 74 | } |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | ($i:expr, $cond:expr, $f:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 78 | cond!($i, $cond, call!($f)) |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 79 | }; |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 80 | } |
| 81 | |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 82 | macro_rules! cond_reduce { |
| 83 | ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => { |
| 84 | if $cond { |
| 85 | $submac!($i, $($args)*) |
| 86 | } else { |
| 87 | $crate::nom::IResult::Error |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | ($i:expr, $cond:expr, $f:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 92 | cond_reduce!($i, $cond, call!($f)) |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 93 | }; |
| 94 | } |
| 95 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 96 | macro_rules! preceded { |
| 97 | ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => { |
| 98 | match tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) { |
| 99 | $crate::nom::IResult::Done(remaining, (_, o)) => $crate::nom::IResult::Done(remaining, o), |
| 100 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 105 | preceded!($i, $submac!($($args)*), call!($g)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 109 | preceded!($i, call!($f), $submac!($($args)*)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | ($i:expr, $f:expr, $g:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 113 | preceded!($i, call!($f), call!($g)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 114 | }; |
| 115 | } |
| 116 | |
| 117 | macro_rules! terminated { |
| 118 | ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => { |
| 119 | match tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) { |
| 120 | $crate::nom::IResult::Done(remaining, (o, _)) => $crate::nom::IResult::Done(remaining, o), |
| 121 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
| 122 | } |
| 123 | }; |
| 124 | |
| 125 | ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 126 | terminated!($i, $submac!($($args)*), call!($g)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 130 | terminated!($i, call!($f), $submac!($($args)*)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | ($i:expr, $f:expr, $g:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 134 | terminated!($i, call!($f), call!($g)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 135 | }; |
| 136 | } |
| 137 | |
| 138 | macro_rules! many0 { |
| 139 | ($i:expr, $submac:ident!( $($args:tt)* )) => {{ |
| 140 | let ret; |
| 141 | let mut res = ::std::vec::Vec::new(); |
| 142 | let mut input = $i; |
| 143 | |
| 144 | loop { |
| 145 | if input.is_empty() { |
| 146 | ret = $crate::nom::IResult::Done(input, res); |
| 147 | break; |
| 148 | } |
| 149 | |
| 150 | match $submac!(input, $($args)*) { |
| 151 | $crate::nom::IResult::Error => { |
| 152 | ret = $crate::nom::IResult::Done(input, res); |
| 153 | break; |
| 154 | } |
| 155 | $crate::nom::IResult::Done(i, o) => { |
| 156 | // loop trip must always consume (otherwise infinite loops) |
David Tolnay | bc84d5a | 2016-10-08 13:20:57 -0700 | [diff] [blame] | 157 | if i.len() == input.len() { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 158 | ret = $crate::nom::IResult::Error; |
| 159 | break; |
| 160 | } |
| 161 | |
| 162 | res.push(o); |
| 163 | input = i; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | ret |
| 169 | }}; |
| 170 | |
| 171 | ($i:expr, $f:expr) => { |
David Tolnay | bc84d5a | 2016-10-08 13:20:57 -0700 | [diff] [blame] | 172 | $crate::nom::many0($i, $f) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 173 | }; |
| 174 | } |
| 175 | |
David Tolnay | c7f646a | 2016-10-16 10:54:39 -0700 | [diff] [blame] | 176 | pub fn many0<'a, T>(mut input: &'a str, |
| 177 | f: fn(&'a str) -> IResult<&'a str, T>) |
| 178 | -> IResult<&'a str, Vec<T>> { |
David Tolnay | bc84d5a | 2016-10-08 13:20:57 -0700 | [diff] [blame] | 179 | let mut res = Vec::new(); |
| 180 | |
| 181 | loop { |
| 182 | if input.is_empty() { |
| 183 | return IResult::Done(input, res); |
| 184 | } |
| 185 | |
| 186 | match f(input) { |
| 187 | IResult::Error => { |
| 188 | return IResult::Done(input, res); |
| 189 | } |
| 190 | IResult::Done(i, o) => { |
| 191 | // loop trip must always consume (otherwise infinite loops) |
| 192 | if i.len() == input.len() { |
| 193 | return IResult::Error; |
| 194 | } |
| 195 | |
| 196 | res.push(o); |
| 197 | input = i; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 203 | macro_rules! peek { |
| 204 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
| 205 | match $submac!($i, $($args)*) { |
| 206 | $crate::nom::IResult::Done(_, o) => $crate::nom::IResult::Done($i, o), |
| 207 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
| 208 | } |
| 209 | }; |
| 210 | } |
| 211 | |
| 212 | macro_rules! take_while1 { |
| 213 | ($input:expr, $submac:ident!( $($args:tt)* )) => {{ |
| 214 | let mut offset = $input.len(); |
| 215 | for (o, c) in $input.char_indices() { |
| 216 | if !$submac!(c, $($args)*) { |
| 217 | offset = o; |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | if offset == 0 { |
| 222 | $crate::nom::IResult::Error |
| 223 | } else if offset < $input.len() { |
| 224 | $crate::nom::IResult::Done(&$input[offset..], &$input[..offset]) |
| 225 | } else { |
| 226 | $crate::nom::IResult::Done("", $input) |
| 227 | } |
| 228 | }}; |
| 229 | |
| 230 | ($input:expr, $f:expr) => { |
| 231 | take_while1!($input, call!($f)); |
| 232 | }; |
| 233 | } |
| 234 | |
David Tolnay | 62604d9 | 2016-09-30 14:47:48 -0700 | [diff] [blame] | 235 | pub fn str_chars(s: &str) -> Vec<char> { |
| 236 | // Can't do `s.chars().collect()` because it triggers a compiler bug in 1.12.0 |
| 237 | // https://github.com/dtolnay/syn/issues/20 |
| 238 | let mut result = Vec::new(); |
| 239 | for ch in s.chars() { |
| 240 | result.push(ch); |
| 241 | } |
| 242 | result |
| 243 | } |
| 244 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 245 | macro_rules! take_until { |
| 246 | ($input:expr, $substr:expr) => {{ |
| 247 | if $substr.len() > $input.len() { |
| 248 | $crate::nom::IResult::Error |
| 249 | } else { |
David Tolnay | 62604d9 | 2016-09-30 14:47:48 -0700 | [diff] [blame] | 250 | let substr_vec: Vec<char> = $crate::nom::str_chars($substr); |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 251 | let mut window: Vec<char> = vec![]; |
| 252 | let mut offset = $input.len(); |
| 253 | let mut parsed = false; |
| 254 | for (o, c) in $input.char_indices() { |
| 255 | window.push(c); |
| 256 | if window.len() > substr_vec.len() { |
| 257 | window.remove(0); |
| 258 | } |
| 259 | if window == substr_vec { |
| 260 | parsed = true; |
| 261 | window.pop(); |
| 262 | let window_len: usize = window.iter() |
| 263 | .map(|x| x.len_utf8()) |
| 264 | .fold(0, |x, y| x + y); |
| 265 | offset = o - window_len; |
| 266 | break; |
| 267 | } |
| 268 | } |
| 269 | if parsed { |
| 270 | $crate::nom::IResult::Done(&$input[offset..], &$input[..offset]) |
| 271 | } else { |
| 272 | $crate::nom::IResult::Error |
| 273 | } |
| 274 | } |
| 275 | }}; |
| 276 | } |
| 277 | |
| 278 | macro_rules! tag { |
| 279 | ($i:expr, $tag: expr) => { |
David Tolnay | 0b154ea | 2016-10-01 15:42:50 -0700 | [diff] [blame] | 280 | if $i.starts_with($tag) { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 281 | $crate::nom::IResult::Done(&$i[$tag.len()..], &$i[0..$tag.len()]) |
| 282 | } else { |
| 283 | $crate::nom::IResult::Error |
| 284 | } |
| 285 | }; |
| 286 | } |
| 287 | |
| 288 | macro_rules! switch { |
| 289 | ($i:expr, $submac:ident!( $($args:tt)* ), $($p:pat => $subrule:ident!( $($args2:tt)* ))|* ) => { |
| 290 | match $submac!($i, $($args)*) { |
| 291 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
| 292 | $crate::nom::IResult::Done(i, o) => match o { |
| 293 | $( |
| 294 | $p => $subrule!(i, $($args2)*), |
| 295 | )* |
| 296 | _ => $crate::nom::IResult::Error, |
| 297 | } |
| 298 | } |
| 299 | }; |
| 300 | } |
| 301 | |
| 302 | macro_rules! value { |
| 303 | ($i:expr, $res:expr) => { |
| 304 | $crate::nom::IResult::Done($i, $res) |
| 305 | }; |
| 306 | } |
| 307 | |
| 308 | macro_rules! delimited { |
| 309 | ($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)+) => { |
| 310 | match tuple_parser!($i, (), $submac!($($args)*), $($rest)*) { |
| 311 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
| 312 | $crate::nom::IResult::Done(i1, (_, o, _)) => $crate::nom::IResult::Done(i1, o) |
| 313 | } |
| 314 | }; |
| 315 | |
| 316 | ($i:expr, $f:expr, $($rest:tt)+) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 317 | delimited!($i, call!($f), $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 318 | }; |
| 319 | } |
| 320 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 321 | macro_rules! separated_nonempty_list { |
| 322 | ($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => {{ |
| 323 | let mut res = ::std::vec::Vec::new(); |
| 324 | let mut input = $i; |
| 325 | |
| 326 | // get the first element |
| 327 | match $submac!(input, $($args2)*) { |
| 328 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
David Tolnay | de95548 | 2016-10-08 13:10:27 -0700 | [diff] [blame] | 329 | $crate::nom::IResult::Done(i, o) => { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 330 | if i.len() == input.len() { |
| 331 | $crate::nom::IResult::Error |
| 332 | } else { |
| 333 | res.push(o); |
| 334 | input = i; |
| 335 | |
| 336 | while let $crate::nom::IResult::Done(i2, _) = $sep!(input, $($args)*) { |
| 337 | if i2.len() == input.len() { |
| 338 | break; |
| 339 | } |
| 340 | |
David Tolnay | de95548 | 2016-10-08 13:10:27 -0700 | [diff] [blame] | 341 | if let $crate::nom::IResult::Done(i3, o3) = $submac!(i2, $($args2)*) { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 342 | if i3.len() == i2.len() { |
| 343 | break; |
| 344 | } |
| 345 | res.push(o3); |
| 346 | input = i3; |
| 347 | } else { |
| 348 | break; |
| 349 | } |
| 350 | } |
| 351 | $crate::nom::IResult::Done(input, res) |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | }}; |
| 356 | |
| 357 | ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 358 | separated_nonempty_list!($i, $submac!($($args)*), call!($g)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 359 | }; |
| 360 | |
| 361 | ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 362 | separated_nonempty_list!($i, call!($f), $submac!($($args)*)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 363 | }; |
| 364 | |
| 365 | ($i:expr, $f:expr, $g:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 366 | separated_nonempty_list!($i, call!($f), call!($g)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 367 | }; |
| 368 | } |
| 369 | |
| 370 | macro_rules! tuple { |
| 371 | ($i:expr, $($rest:tt)*) => { |
| 372 | tuple_parser!($i, (), $($rest)*) |
| 373 | }; |
| 374 | } |
| 375 | |
| 376 | /// Internal parser, do not use directly |
| 377 | macro_rules! tuple_parser { |
| 378 | ($i:expr, ($($parsed:tt),*), $e:ident, $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 379 | tuple_parser!($i, ($($parsed),*), call!($e), $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 380 | }; |
| 381 | |
| 382 | ($i:expr, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => { |
| 383 | match $submac!($i, $($args)*) { |
| 384 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
David Tolnay | de95548 | 2016-10-08 13:10:27 -0700 | [diff] [blame] | 385 | $crate::nom::IResult::Done(i, o) => |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 386 | tuple_parser!(i, (o), $($rest)*), |
| 387 | } |
| 388 | }; |
| 389 | |
| 390 | ($i:expr, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => { |
| 391 | match $submac!($i, $($args)*) { |
| 392 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
David Tolnay | de95548 | 2016-10-08 13:10:27 -0700 | [diff] [blame] | 393 | $crate::nom::IResult::Done(i, o) => |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 394 | tuple_parser!(i, ($($parsed)* , o), $($rest)*), |
| 395 | } |
| 396 | }; |
| 397 | |
| 398 | ($i:expr, ($($parsed:tt),*), $e:ident) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 399 | tuple_parser!($i, ($($parsed),*), call!($e)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 400 | }; |
| 401 | |
| 402 | ($i:expr, (), $submac:ident!( $($args:tt)* )) => { |
| 403 | $submac!($i, $($args)*) |
| 404 | }; |
| 405 | |
| 406 | ($i:expr, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => { |
| 407 | match $submac!($i, $($args)*) { |
| 408 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
| 409 | $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, ($($parsed),*, o)) |
| 410 | } |
| 411 | }; |
| 412 | |
| 413 | ($i:expr, ($($parsed:expr),*)) => { |
| 414 | $crate::nom::IResult::Done($i, ($($parsed),*)) |
| 415 | }; |
| 416 | } |
| 417 | |
| 418 | macro_rules! alt { |
| 419 | ($i:expr, $e:ident | $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 420 | alt!($i, call!($e) | $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 421 | }; |
| 422 | |
| 423 | ($i:expr, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => { |
| 424 | match $subrule!($i, $($args)*) { |
| 425 | res @ $crate::nom::IResult::Done(_, _) => res, |
| 426 | _ => alt!($i, $($rest)*) |
| 427 | } |
| 428 | }; |
| 429 | |
| 430 | ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => { |
| 431 | match $subrule!($i, $($args)*) { |
| 432 | $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, $gen(o)), |
| 433 | $crate::nom::IResult::Error => alt!($i, $($rest)*) |
| 434 | } |
| 435 | }; |
| 436 | |
| 437 | ($i:expr, $e:ident => { $gen:expr } | $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 438 | alt!($i, call!($e) => { $gen } | $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 439 | }; |
| 440 | |
| 441 | ($i:expr, $e:ident => { $gen:expr }) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 442 | alt!($i, call!($e) => { $gen }) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 443 | }; |
| 444 | |
| 445 | ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => { |
| 446 | match $subrule!($i, $($args)*) { |
| 447 | $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, $gen(o)), |
David Tolnay | 5377b17 | 2016-10-25 01:13:12 -0700 | [diff] [blame] | 448 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 449 | } |
| 450 | }; |
| 451 | |
| 452 | ($i:expr, $e:ident) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 453 | alt!($i, call!($e)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 454 | }; |
| 455 | |
| 456 | ($i:expr, $subrule:ident!( $($args:tt)*)) => { |
David Tolnay | 5377b17 | 2016-10-25 01:13:12 -0700 | [diff] [blame] | 457 | $subrule!($i, $($args)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 458 | }; |
| 459 | } |
| 460 | |
| 461 | macro_rules! do_parse { |
| 462 | ($i:expr, ( $($rest:expr),* )) => { |
| 463 | $crate::nom::IResult::Done($i, ( $($rest),* )) |
| 464 | }; |
| 465 | |
| 466 | ($i:expr, $e:ident >> $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 467 | do_parse!($i, call!($e) >> $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 468 | }; |
| 469 | |
| 470 | ($i:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => { |
| 471 | match $submac!($i, $($args)*) { |
| 472 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
| 473 | $crate::nom::IResult::Done(i, _) => |
| 474 | do_parse!(i, $($rest)*), |
| 475 | } |
| 476 | }; |
| 477 | |
| 478 | ($i:expr, $field:ident : $e:ident >> $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 479 | do_parse!($i, $field: call!($e) >> $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 480 | }; |
| 481 | |
| 482 | ($i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => { |
| 483 | match $submac!($i, $($args)*) { |
| 484 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
David Tolnay | de95548 | 2016-10-08 13:10:27 -0700 | [diff] [blame] | 485 | $crate::nom::IResult::Done(i, o) => { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 486 | let $field = o; |
| 487 | do_parse!(i, $($rest)*) |
| 488 | }, |
| 489 | } |
| 490 | }; |
| 491 | |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 492 | ($i:expr, mut $field:ident : $e:ident >> $($rest:tt)*) => { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame^] | 493 | do_parse!($i, mut $field: call!($e) >> $($rest)*) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 494 | }; |
| 495 | |
| 496 | ($i:expr, mut $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => { |
| 497 | match $submac!($i, $($args)*) { |
| 498 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
David Tolnay | de95548 | 2016-10-08 13:10:27 -0700 | [diff] [blame] | 499 | $crate::nom::IResult::Done(i, o) => { |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 500 | let mut $field = o; |
| 501 | do_parse!(i, $($rest)*) |
| 502 | }, |
| 503 | } |
| 504 | }; |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 505 | } |