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 | |
| 5 | #[macro_export] |
| 6 | macro_rules! do_parse ( |
| 7 | ($i:expr, $($rest:tt)*) => ( |
| 8 | { |
| 9 | do_parse_impl!($i, 0usize, $($rest)*) |
| 10 | } |
| 11 | ); |
| 12 | ); |
| 13 | |
| 14 | /// Internal parser, do not use directly |
| 15 | #[doc(hidden)] |
| 16 | #[macro_export] |
| 17 | macro_rules! do_parse_impl ( |
| 18 | |
| 19 | ($i:expr, $consumed:expr, ( $($rest:expr),* )) => ( |
| 20 | ::nom::IResult::Done($i, ( $($rest),* )) |
| 21 | ); |
| 22 | |
| 23 | ($i:expr, $consumed:expr, $e:ident >> $($rest:tt)*) => ( |
| 24 | do_parse_impl!($i, $consumed, call!($e) >> $($rest)*); |
| 25 | ); |
| 26 | ($i:expr, $consumed:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => ( |
| 27 | { |
| 28 | match $submac!($i, $($args)*) { |
| 29 | ::nom::IResult::Error(e) => ::nom::IResult::Error(e), |
| 30 | ::nom::IResult::Incomplete(::nom::Needed::Unknown) => |
| 31 | ::nom::IResult::Incomplete(::nom::Needed::Unknown), |
| 32 | ::nom::IResult::Incomplete(::nom::Needed::Size(i)) => |
| 33 | ::nom::IResult::Incomplete(::nom::Needed::Size($consumed + i)), |
| 34 | ::nom::IResult::Done(i,_) => { |
| 35 | do_parse_impl!(i, |
| 36 | $consumed + (::nom::InputLength::input_len(&($i)) - |
| 37 | ::nom::InputLength::input_len(&i)), $($rest)*) |
| 38 | }, |
| 39 | } |
| 40 | } |
| 41 | ); |
| 42 | |
| 43 | ($i:expr, $consumed:expr, $field:ident : $e:ident >> $($rest:tt)*) => ( |
| 44 | do_parse_impl!($i, $consumed, $field: call!($e) >> $($rest)*); |
| 45 | ); |
| 46 | |
| 47 | ($i:expr, $consumed:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => ( |
| 48 | { |
| 49 | match $submac!($i, $($args)*) { |
| 50 | ::nom::IResult::Error(e) => ::nom::IResult::Error(e), |
| 51 | ::nom::IResult::Incomplete(::nom::Needed::Unknown) => |
| 52 | ::nom::IResult::Incomplete(::nom::Needed::Unknown), |
| 53 | ::nom::IResult::Incomplete(::nom::Needed::Size(i)) => |
| 54 | ::nom::IResult::Incomplete(::nom::Needed::Size($consumed + i)), |
| 55 | ::nom::IResult::Done(i,o) => { |
| 56 | let $field = o; |
| 57 | do_parse_impl!(i, |
| 58 | $consumed + (::nom::InputLength::input_len(&($i)) - |
| 59 | ::nom::InputLength::input_len(&i)), $($rest)*) |
| 60 | }, |
| 61 | } |
| 62 | } |
| 63 | ); |
| 64 | |
| 65 | // ending the chain |
| 66 | ($i:expr, $consumed:expr, $e:ident >> ( $($rest:tt)* )) => ( |
| 67 | do_parse_impl!($i, $consumed, call!($e) >> ( $($rest)* )); |
| 68 | ); |
| 69 | |
| 70 | ($i:expr, $consumed:expr, $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => ( |
| 71 | match $submac!($i, $($args)*) { |
| 72 | ::nom::IResult::Error(e) => ::nom::IResult::Error(e), |
| 73 | ::nom::IResult::Incomplete(::nom::Needed::Unknown) => |
| 74 | ::nom::IResult::Incomplete(::nom::Needed::Unknown), |
| 75 | ::nom::IResult::Incomplete(::nom::Needed::Size(i)) => |
| 76 | ::nom::IResult::Incomplete(::nom::Needed::Size($consumed + i)), |
| 77 | ::nom::IResult::Done(i,_) => { |
| 78 | ::nom::IResult::Done(i, ( $($rest)* )) |
| 79 | }, |
| 80 | } |
| 81 | ); |
| 82 | |
| 83 | ($i:expr, $consumed:expr, $field:ident : $e:ident >> ( $($rest:tt)* )) => ( |
| 84 | do_parse_impl!($i, $consumed, $field: call!($e) >> ( $($rest)* ) ); |
| 85 | ); |
| 86 | |
| 87 | ($i:expr, $consumed:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> ( $($rest:tt)* )) => ( |
| 88 | match $submac!($i, $($args)*) { |
| 89 | ::nom::IResult::Error(e) => ::nom::IResult::Error(e), |
| 90 | ::nom::IResult::Incomplete(::nom::Needed::Unknown) => |
| 91 | ::nom::IResult::Incomplete(::nom::Needed::Unknown), |
| 92 | ::nom::IResult::Incomplete(::nom::Needed::Size(i)) => |
| 93 | ::nom::IResult::Incomplete(::nom::Needed::Size($consumed + i)), |
| 94 | ::nom::IResult::Done(i,o) => { |
| 95 | let $field = o; |
| 96 | ::nom::IResult::Done(i, ( $($rest)* )) |
| 97 | }, |
| 98 | } |
| 99 | ); |
| 100 | |
| 101 | ); |