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 | |
| 235 | macro_rules! take_until { |
| 236 | ($input:expr, $substr:expr) => {{ |
| 237 | if $substr.len() > $input.len() { |
| 238 | $crate::nom::IResult::Error |
| 239 | } else { |
David Tolnay | f2c452b | 2016-12-21 22:45:28 -0500 | [diff] [blame^] | 240 | let substr_vec: Vec<char> = $substr.chars().collect(); |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 241 | let mut window: Vec<char> = vec![]; |
| 242 | let mut offset = $input.len(); |
| 243 | let mut parsed = false; |
| 244 | for (o, c) in $input.char_indices() { |
| 245 | window.push(c); |
| 246 | if window.len() > substr_vec.len() { |
| 247 | window.remove(0); |
| 248 | } |
| 249 | if window == substr_vec { |
| 250 | parsed = true; |
| 251 | window.pop(); |
| 252 | let window_len: usize = window.iter() |
| 253 | .map(|x| x.len_utf8()) |
| 254 | .fold(0, |x, y| x + y); |
| 255 | offset = o - window_len; |
| 256 | break; |
| 257 | } |
| 258 | } |
| 259 | if parsed { |
| 260 | $crate::nom::IResult::Done(&$input[offset..], &$input[..offset]) |
| 261 | } else { |
| 262 | $crate::nom::IResult::Error |
| 263 | } |
| 264 | } |
| 265 | }}; |
| 266 | } |
| 267 | |
| 268 | macro_rules! tag { |
| 269 | ($i:expr, $tag: expr) => { |
David Tolnay | 0b154ea | 2016-10-01 15:42:50 -0700 | [diff] [blame] | 270 | if $i.starts_with($tag) { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 271 | $crate::nom::IResult::Done(&$i[$tag.len()..], &$i[0..$tag.len()]) |
| 272 | } else { |
| 273 | $crate::nom::IResult::Error |
| 274 | } |
| 275 | }; |
| 276 | } |
| 277 | |
| 278 | macro_rules! switch { |
| 279 | ($i:expr, $submac:ident!( $($args:tt)* ), $($p:pat => $subrule:ident!( $($args2:tt)* ))|* ) => { |
| 280 | match $submac!($i, $($args)*) { |
| 281 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
| 282 | $crate::nom::IResult::Done(i, o) => match o { |
| 283 | $( |
| 284 | $p => $subrule!(i, $($args2)*), |
| 285 | )* |
| 286 | _ => $crate::nom::IResult::Error, |
| 287 | } |
| 288 | } |
| 289 | }; |
| 290 | } |
| 291 | |
| 292 | macro_rules! value { |
| 293 | ($i:expr, $res:expr) => { |
| 294 | $crate::nom::IResult::Done($i, $res) |
| 295 | }; |
| 296 | } |
| 297 | |
| 298 | macro_rules! delimited { |
| 299 | ($i:expr, $submac:ident!( $($args:tt)* ), $($rest:tt)+) => { |
| 300 | match tuple_parser!($i, (), $submac!($($args)*), $($rest)*) { |
| 301 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
| 302 | $crate::nom::IResult::Done(i1, (_, o, _)) => $crate::nom::IResult::Done(i1, o) |
| 303 | } |
| 304 | }; |
| 305 | |
| 306 | ($i:expr, $f:expr, $($rest:tt)+) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 307 | delimited!($i, call!($f), $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 308 | }; |
| 309 | } |
| 310 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 311 | macro_rules! separated_nonempty_list { |
| 312 | ($i:expr, $sep:ident!( $($args:tt)* ), $submac:ident!( $($args2:tt)* )) => {{ |
| 313 | let mut res = ::std::vec::Vec::new(); |
| 314 | let mut input = $i; |
| 315 | |
| 316 | // get the first element |
| 317 | match $submac!(input, $($args2)*) { |
| 318 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
David Tolnay | de95548 | 2016-10-08 13:10:27 -0700 | [diff] [blame] | 319 | $crate::nom::IResult::Done(i, o) => { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 320 | if i.len() == input.len() { |
| 321 | $crate::nom::IResult::Error |
| 322 | } else { |
| 323 | res.push(o); |
| 324 | input = i; |
| 325 | |
| 326 | while let $crate::nom::IResult::Done(i2, _) = $sep!(input, $($args)*) { |
| 327 | if i2.len() == input.len() { |
| 328 | break; |
| 329 | } |
| 330 | |
David Tolnay | de95548 | 2016-10-08 13:10:27 -0700 | [diff] [blame] | 331 | if let $crate::nom::IResult::Done(i3, o3) = $submac!(i2, $($args2)*) { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 332 | if i3.len() == i2.len() { |
| 333 | break; |
| 334 | } |
| 335 | res.push(o3); |
| 336 | input = i3; |
| 337 | } else { |
| 338 | break; |
| 339 | } |
| 340 | } |
| 341 | $crate::nom::IResult::Done(input, res) |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | }}; |
| 346 | |
| 347 | ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 348 | separated_nonempty_list!($i, $submac!($($args)*), call!($g)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 349 | }; |
| 350 | |
| 351 | ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 352 | separated_nonempty_list!($i, call!($f), $submac!($($args)*)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 353 | }; |
| 354 | |
| 355 | ($i:expr, $f:expr, $g:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 356 | separated_nonempty_list!($i, call!($f), call!($g)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 357 | }; |
| 358 | } |
| 359 | |
| 360 | macro_rules! tuple { |
| 361 | ($i:expr, $($rest:tt)*) => { |
| 362 | tuple_parser!($i, (), $($rest)*) |
| 363 | }; |
| 364 | } |
| 365 | |
| 366 | /// Internal parser, do not use directly |
| 367 | macro_rules! tuple_parser { |
| 368 | ($i:expr, ($($parsed:tt),*), $e:ident, $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 369 | tuple_parser!($i, ($($parsed),*), call!($e), $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 370 | }; |
| 371 | |
| 372 | ($i:expr, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => { |
| 373 | match $submac!($i, $($args)*) { |
| 374 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
David Tolnay | de95548 | 2016-10-08 13:10:27 -0700 | [diff] [blame] | 375 | $crate::nom::IResult::Done(i, o) => |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 376 | tuple_parser!(i, (o), $($rest)*), |
| 377 | } |
| 378 | }; |
| 379 | |
| 380 | ($i:expr, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => { |
| 381 | match $submac!($i, $($args)*) { |
| 382 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
David Tolnay | de95548 | 2016-10-08 13:10:27 -0700 | [diff] [blame] | 383 | $crate::nom::IResult::Done(i, o) => |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 384 | tuple_parser!(i, ($($parsed)* , o), $($rest)*), |
| 385 | } |
| 386 | }; |
| 387 | |
| 388 | ($i:expr, ($($parsed:tt),*), $e:ident) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 389 | tuple_parser!($i, ($($parsed),*), call!($e)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 390 | }; |
| 391 | |
| 392 | ($i:expr, (), $submac:ident!( $($args:tt)* )) => { |
| 393 | $submac!($i, $($args)*) |
| 394 | }; |
| 395 | |
| 396 | ($i:expr, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => { |
| 397 | match $submac!($i, $($args)*) { |
| 398 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
| 399 | $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, ($($parsed),*, o)) |
| 400 | } |
| 401 | }; |
| 402 | |
| 403 | ($i:expr, ($($parsed:expr),*)) => { |
| 404 | $crate::nom::IResult::Done($i, ($($parsed),*)) |
| 405 | }; |
| 406 | } |
| 407 | |
| 408 | macro_rules! alt { |
| 409 | ($i:expr, $e:ident | $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 410 | alt!($i, call!($e) | $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 411 | }; |
| 412 | |
| 413 | ($i:expr, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => { |
| 414 | match $subrule!($i, $($args)*) { |
| 415 | res @ $crate::nom::IResult::Done(_, _) => res, |
| 416 | _ => alt!($i, $($rest)*) |
| 417 | } |
| 418 | }; |
| 419 | |
| 420 | ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => { |
| 421 | match $subrule!($i, $($args)*) { |
| 422 | $crate::nom::IResult::Done(i, o) => $crate::nom::IResult::Done(i, $gen(o)), |
| 423 | $crate::nom::IResult::Error => alt!($i, $($rest)*) |
| 424 | } |
| 425 | }; |
| 426 | |
| 427 | ($i:expr, $e:ident => { $gen:expr } | $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 428 | alt!($i, call!($e) => { $gen } | $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 429 | }; |
| 430 | |
| 431 | ($i:expr, $e:ident => { $gen:expr }) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 432 | alt!($i, call!($e) => { $gen }) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 433 | }; |
| 434 | |
| 435 | ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => { |
| 436 | match $subrule!($i, $($args)*) { |
| 437 | $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] | 438 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 439 | } |
| 440 | }; |
| 441 | |
| 442 | ($i:expr, $e:ident) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 443 | alt!($i, call!($e)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 444 | }; |
| 445 | |
| 446 | ($i:expr, $subrule:ident!( $($args:tt)*)) => { |
David Tolnay | 5377b17 | 2016-10-25 01:13:12 -0700 | [diff] [blame] | 447 | $subrule!($i, $($args)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 448 | }; |
| 449 | } |
| 450 | |
| 451 | macro_rules! do_parse { |
| 452 | ($i:expr, ( $($rest:expr),* )) => { |
| 453 | $crate::nom::IResult::Done($i, ( $($rest),* )) |
| 454 | }; |
| 455 | |
| 456 | ($i:expr, $e:ident >> $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 457 | do_parse!($i, call!($e) >> $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 458 | }; |
| 459 | |
| 460 | ($i:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => { |
| 461 | match $submac!($i, $($args)*) { |
| 462 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
| 463 | $crate::nom::IResult::Done(i, _) => |
| 464 | do_parse!(i, $($rest)*), |
| 465 | } |
| 466 | }; |
| 467 | |
| 468 | ($i:expr, $field:ident : $e:ident >> $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 469 | do_parse!($i, $field: call!($e) >> $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 470 | }; |
| 471 | |
| 472 | ($i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => { |
| 473 | match $submac!($i, $($args)*) { |
| 474 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
David Tolnay | de95548 | 2016-10-08 13:10:27 -0700 | [diff] [blame] | 475 | $crate::nom::IResult::Done(i, o) => { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 476 | let $field = o; |
| 477 | do_parse!(i, $($rest)*) |
| 478 | }, |
| 479 | } |
| 480 | }; |
| 481 | |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 482 | ($i:expr, mut $field:ident : $e:ident >> $($rest:tt)*) => { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 483 | do_parse!($i, mut $field: call!($e) >> $($rest)*) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 484 | }; |
| 485 | |
| 486 | ($i:expr, mut $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => { |
| 487 | match $submac!($i, $($args)*) { |
| 488 | $crate::nom::IResult::Error => $crate::nom::IResult::Error, |
David Tolnay | de95548 | 2016-10-08 13:10:27 -0700 | [diff] [blame] | 489 | $crate::nom::IResult::Done(i, o) => { |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 490 | let mut $field = o; |
| 491 | do_parse!(i, $($rest)*) |
| 492 | }, |
| 493 | } |
| 494 | }; |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 495 | } |