David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 1 | // Copied from nom master: |
| 2 | // https://github.com/Geal/nom/blob/a38188f333c29d00c32a3082bec5491d2eefa33f/src/sequence.rs#L591-L687 |
| 3 | // Will be released in nom 2.0. |
| 4 | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 5 | #![cfg(feature = "parsing")] |
| 6 | |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 7 | #[macro_export] |
| 8 | macro_rules! do_parse ( |
| 9 | ($i:expr, $($rest:tt)*) => ( |
| 10 | { |
| 11 | do_parse_impl!($i, 0usize, $($rest)*) |
| 12 | } |
| 13 | ); |
| 14 | ); |
| 15 | |
| 16 | /// Internal parser, do not use directly |
| 17 | #[doc(hidden)] |
| 18 | #[macro_export] |
| 19 | macro_rules! do_parse_impl ( |
| 20 | |
| 21 | ($i:expr, $consumed:expr, ( $($rest:expr),* )) => ( |
| 22 | ::nom::IResult::Done($i, ( $($rest),* )) |
| 23 | ); |
| 24 | |
| 25 | ($i:expr, $consumed:expr, $e:ident >> $($rest:tt)*) => ( |
| 26 | do_parse_impl!($i, $consumed, call!($e) >> $($rest)*); |
| 27 | ); |
| 28 | ($i:expr, $consumed:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => ( |
| 29 | { |
| 30 | match $submac!($i, $($args)*) { |
| 31 | ::nom::IResult::Error(e) => ::nom::IResult::Error(e), |
| 32 | ::nom::IResult::Incomplete(::nom::Needed::Unknown) => |
| 33 | ::nom::IResult::Incomplete(::nom::Needed::Unknown), |
| 34 | ::nom::IResult::Incomplete(::nom::Needed::Size(i)) => |
| 35 | ::nom::IResult::Incomplete(::nom::Needed::Size($consumed + i)), |
| 36 | ::nom::IResult::Done(i,_) => { |
| 37 | do_parse_impl!(i, |
| 38 | $consumed + (::nom::InputLength::input_len(&($i)) - |
| 39 | ::nom::InputLength::input_len(&i)), $($rest)*) |
| 40 | }, |
| 41 | } |
| 42 | } |
| 43 | ); |
| 44 | |
| 45 | ($i:expr, $consumed:expr, $field:ident : $e:ident >> $($rest:tt)*) => ( |
| 46 | do_parse_impl!($i, $consumed, $field: call!($e) >> $($rest)*); |
| 47 | ); |
| 48 | |
| 49 | ($i:expr, $consumed:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => ( |
| 50 | { |
| 51 | match $submac!($i, $($args)*) { |
| 52 | ::nom::IResult::Error(e) => ::nom::IResult::Error(e), |
| 53 | ::nom::IResult::Incomplete(::nom::Needed::Unknown) => |
| 54 | ::nom::IResult::Incomplete(::nom::Needed::Unknown), |
| 55 | ::nom::IResult::Incomplete(::nom::Needed::Size(i)) => |
| 56 | ::nom::IResult::Incomplete(::nom::Needed::Size($consumed + i)), |
| 57 | ::nom::IResult::Done(i,o) => { |
| 58 | let $field = o; |
| 59 | do_parse_impl!(i, |
| 60 | $consumed + (::nom::InputLength::input_len(&($i)) - |
| 61 | ::nom::InputLength::input_len(&i)), $($rest)*) |
| 62 | }, |
| 63 | } |
| 64 | } |
| 65 | ); |
| 66 | |
| 67 | // ending the chain |
| 68 | ($i:expr, $consumed:expr, $e:ident >> ( $($rest:tt)* )) => ( |
| 69 | do_parse_impl!($i, $consumed, call!($e) >> ( $($rest)* )); |
| 70 | ); |
| 71 | |
| 72 | ($i:expr, $consumed:expr, $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => ( |
| 73 | match $submac!($i, $($args)*) { |
| 74 | ::nom::IResult::Error(e) => ::nom::IResult::Error(e), |
| 75 | ::nom::IResult::Incomplete(::nom::Needed::Unknown) => |
| 76 | ::nom::IResult::Incomplete(::nom::Needed::Unknown), |
| 77 | ::nom::IResult::Incomplete(::nom::Needed::Size(i)) => |
| 78 | ::nom::IResult::Incomplete(::nom::Needed::Size($consumed + i)), |
| 79 | ::nom::IResult::Done(i,_) => { |
| 80 | ::nom::IResult::Done(i, ( $($rest)* )) |
| 81 | }, |
| 82 | } |
| 83 | ); |
| 84 | |
| 85 | ($i:expr, $consumed:expr, $field:ident : $e:ident >> ( $($rest:tt)* )) => ( |
| 86 | do_parse_impl!($i, $consumed, $field: call!($e) >> ( $($rest)* ) ); |
| 87 | ); |
| 88 | |
| 89 | ($i:expr, $consumed:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => ( |
| 90 | match $submac!($i, $($args)*) { |
| 91 | ::nom::IResult::Error(e) => ::nom::IResult::Error(e), |
| 92 | ::nom::IResult::Incomplete(::nom::Needed::Unknown) => |
| 93 | ::nom::IResult::Incomplete(::nom::Needed::Unknown), |
| 94 | ::nom::IResult::Incomplete(::nom::Needed::Size(i)) => |
| 95 | ::nom::IResult::Incomplete(::nom::Needed::Size($consumed + i)), |
| 96 | ::nom::IResult::Done(i,o) => { |
| 97 | let $field = o; |
| 98 | ::nom::IResult::Done(i, ( $($rest)* )) |
| 99 | }, |
| 100 | } |
| 101 | ); |
| 102 | |
| 103 | ); |