David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 1 | use cursor::Cursor; |
David Tolnay | 203557a | 2017-12-27 23:59:33 -0500 | [diff] [blame] | 2 | use parse_error; |
| 3 | use synom::PResult; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 4 | |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 5 | /// Define a function from a parser combination. |
| 6 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 7 | /// - **Syntax:** `named!(NAME -> TYPE, PARSER)` or `named!(pub NAME -> TYPE, PARSER)` |
| 8 | /// |
| 9 | /// ```rust |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 10 | /// # #[macro_use] |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 11 | /// # extern crate syn; |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 12 | /// # |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 13 | /// # use syn::Type; |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 14 | /// # use syn::delimited::Delimited; |
| 15 | /// # |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 16 | /// // One or more Rust types separated by commas. |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 17 | /// named!(pub comma_separated_types -> Delimited<Type, Token![,]>, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 18 | /// call!(Delimited::parse_separated_nonempty) |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 19 | /// ); |
| 20 | /// # fn main() {} |
| 21 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 22 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 23 | macro_rules! named { |
| 24 | ($name:ident -> $o:ty, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | 203557a | 2017-12-27 23:59:33 -0500 | [diff] [blame] | 25 | fn $name(i: $crate::synom::Cursor) -> $crate::synom::PResult<$o> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 26 | $submac!(i, $($args)*) |
| 27 | } |
| 28 | }; |
| 29 | |
| 30 | (pub $name:ident -> $o:ty, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | 203557a | 2017-12-27 23:59:33 -0500 | [diff] [blame] | 31 | pub fn $name(i: $crate::synom::Cursor) -> $crate::synom::PResult<$o> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 32 | $submac!(i, $($args)*) |
| 33 | } |
| 34 | }; |
Michael Layzell | f8334e3 | 2017-06-04 19:01:08 -0400 | [diff] [blame] | 35 | |
| 36 | // These two variants are for defining named parsers which have custom |
| 37 | // arguments, and are called with `call!()` |
| 38 | ($name:ident($($params:tt)*) -> $o:ty, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | 203557a | 2017-12-27 23:59:33 -0500 | [diff] [blame] | 39 | fn $name(i: $crate::synom::Cursor, $($params)*) -> $crate::synom::PResult<$o> { |
Michael Layzell | f8334e3 | 2017-06-04 19:01:08 -0400 | [diff] [blame] | 40 | $submac!(i, $($args)*) |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | (pub $name:ident($($params:tt)*) -> $o:ty, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | 203557a | 2017-12-27 23:59:33 -0500 | [diff] [blame] | 45 | pub fn $name(i: $crate::synom::Cursor, $($params)*) -> $crate::synom::PResult<$o> { |
Michael Layzell | f8334e3 | 2017-06-04 19:01:08 -0400 | [diff] [blame] | 46 | $submac!(i, $($args)*) |
| 47 | } |
| 48 | }; |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 49 | } |
| 50 | |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 51 | #[cfg(all(feature = "verbose-trace", not(feature = "all-features")))] |
Nika Layzell | ae81b37 | 2017-12-05 14:12:33 -0500 | [diff] [blame] | 52 | #[macro_export] |
| 53 | macro_rules! call { |
| 54 | ($i:expr, $fun:expr $(, $args:expr)*) => { |
| 55 | { |
| 56 | let i = $i; |
| 57 | eprintln!(concat!(" -> ", stringify!($fun), " @ {:?}"), i); |
| 58 | let r = $fun(i $(, $args)*); |
| 59 | match r { |
| 60 | Ok((i, _)) => eprintln!(concat!("OK ", stringify!($fun), " @ {:?}"), i), |
| 61 | Err(_) => eprintln!(concat!("ERR ", stringify!($fun), " @ {:?}"), i), |
| 62 | } |
| 63 | r |
| 64 | } |
| 65 | }; |
| 66 | } |
| 67 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 68 | /// Invoke the given parser function with the passed in arguments. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 69 | /// |
| 70 | /// - **Syntax:** `call!(FUNCTION, ARGS...)` |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 71 | /// |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 72 | /// where the signature of the function is `fn(&[U], ARGS...) -> IPResult<&[U], T>` |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 73 | /// - **Output:** `T`, the result of invoking the function `FUNCTION` |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 74 | #[cfg(any(not(feature = "verbose-trace"), feature = "all-features"))] |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 75 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 76 | macro_rules! call { |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 77 | ($i:expr, $fun:expr $(, $args:expr)*) => { |
| 78 | $fun($i $(, $args)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 79 | }; |
| 80 | } |
| 81 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 82 | /// Transform the result of a parser by applying a function or closure. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 83 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 84 | /// - **Syntax:** `map!(THING, FN)` |
| 85 | /// - **Output:** the return type of function FN applied to THING |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 86 | /// |
| 87 | /// ```rust |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 88 | /// #[macro_use] |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 89 | /// extern crate syn; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 90 | /// |
David Tolnay | b21bb0c | 2017-06-03 20:39:19 -0700 | [diff] [blame] | 91 | /// use syn::{Expr, ExprIf}; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 92 | /// |
David Tolnay | b21bb0c | 2017-06-03 20:39:19 -0700 | [diff] [blame] | 93 | /// fn get_cond(if_: ExprIf) -> Expr { |
| 94 | /// *if_.cond |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 95 | /// } |
| 96 | /// |
David Tolnay | b21bb0c | 2017-06-03 20:39:19 -0700 | [diff] [blame] | 97 | /// // Parses an `if` statement but returns the condition part only. |
| 98 | /// named!(if_condition -> Expr, |
| 99 | /// map!(syn!(ExprIf), get_cond) |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 100 | /// ); |
| 101 | /// |
| 102 | /// // Or equivalently: |
David Tolnay | b21bb0c | 2017-06-03 20:39:19 -0700 | [diff] [blame] | 103 | /// named!(if_condition2 -> Expr, |
David Tolnay | bc7d7d9 | 2017-06-03 20:54:05 -0700 | [diff] [blame] | 104 | /// map!(syn!(ExprIf), |if_| *if_.cond) |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 105 | /// ); |
David Tolnay | b21bb0c | 2017-06-03 20:39:19 -0700 | [diff] [blame] | 106 | /// # |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 107 | /// # fn main() {} |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 108 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 109 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 110 | macro_rules! map { |
| 111 | ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 112 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 113 | ::std::result::Result::Err(err) => |
| 114 | ::std::result::Result::Err(err), |
| 115 | ::std::result::Result::Ok((i, o)) => |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 116 | ::std::result::Result::Ok((i, $crate::parsers::invoke($g, o))), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 117 | } |
| 118 | }; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 119 | |
| 120 | ($i:expr, $f:expr, $g:expr) => { |
| 121 | map!($i, call!($f), $g) |
| 122 | }; |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 123 | } |
| 124 | |
David Tolnay | 995bff2 | 2017-12-17 23:44:43 -0800 | [diff] [blame] | 125 | // Somehow this helps with type inference in `map!` and `alt!`. |
David Tolnay | bc7d7d9 | 2017-06-03 20:54:05 -0700 | [diff] [blame] | 126 | // |
| 127 | // Not public API. |
| 128 | #[doc(hidden)] |
| 129 | pub fn invoke<T, R, F: FnOnce(T) -> R>(f: F, t: T) -> R { |
| 130 | f(t) |
| 131 | } |
| 132 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 133 | /// Parses successfully if the given parser fails to parse. Does not consume any |
| 134 | /// of the input. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 135 | /// |
| 136 | /// - **Syntax:** `not!(THING)` |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 137 | /// - **Output:** `()` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 138 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 139 | macro_rules! not { |
| 140 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
| 141 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 142 | ::std::result::Result::Ok(_) => $crate::parse_error(), |
| 143 | ::std::result::Result::Err(_) => |
| 144 | ::std::result::Result::Ok(($i, ())), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 145 | } |
| 146 | }; |
| 147 | } |
| 148 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 149 | /// Conditionally execute the given parser. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 150 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 151 | /// If you are familiar with nom, this is nom's `cond_with_error` parser. |
| 152 | /// |
| 153 | /// - **Syntax:** `cond!(CONDITION, THING)` |
| 154 | /// - **Output:** `Some(THING)` if the condition is true, else `None` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 155 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 156 | macro_rules! cond { |
| 157 | ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => { |
| 158 | if $cond { |
| 159 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 160 | ::std::result::Result::Ok((i, o)) => |
| 161 | ::std::result::Result::Ok((i, ::std::option::Option::Some(o))), |
| 162 | ::std::result::Result::Err(x) => ::std::result::Result::Err(x), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 163 | } |
| 164 | } else { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 165 | ::std::result::Result::Ok(($i, ::std::option::Option::None)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 166 | } |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | ($i:expr, $cond:expr, $f:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 170 | cond!($i, $cond, call!($f)) |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 171 | }; |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 172 | } |
| 173 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 174 | /// Fail to parse if condition is false, otherwise parse the given parser. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 175 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 176 | /// This is typically used inside of `option!` or `alt!`. |
| 177 | /// |
| 178 | /// - **Syntax:** `cond_reduce!(CONDITION, THING)` |
| 179 | /// - **Output:** `THING` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 180 | #[macro_export] |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 181 | macro_rules! cond_reduce { |
| 182 | ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => { |
| 183 | if $cond { |
| 184 | $submac!($i, $($args)*) |
| 185 | } else { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 186 | $crate::parse_error() |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 187 | } |
| 188 | }; |
| 189 | |
| 190 | ($i:expr, $cond:expr, $f:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 191 | cond_reduce!($i, $cond, call!($f)) |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 192 | }; |
| 193 | } |
| 194 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 195 | /// Parse two things, returning the value of the first. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 196 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 197 | /// - **Syntax:** `terminated!(THING, AFTER)` |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 198 | /// - **Output:** `THING` |
| 199 | /// |
| 200 | /// ```rust |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 201 | /// #[macro_use] |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 202 | /// extern crate syn; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 203 | /// |
| 204 | /// use syn::Expr; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 205 | /// |
| 206 | /// // An expression terminated by ##. |
| 207 | /// named!(expr_pound_pound -> Expr, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 208 | /// terminated!(syn!(Expr), tuple!(punct!(#), punct!(#))) |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 209 | /// ); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 210 | /// |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 211 | /// # fn main() {} |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 212 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 213 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 214 | macro_rules! terminated { |
| 215 | ($i:expr, $submac:ident!( $($args:tt)* ), $submac2:ident!( $($args2:tt)* )) => { |
| 216 | match tuple!($i, $submac!($($args)*), $submac2!($($args2)*)) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 217 | ::std::result::Result::Ok((i, (o, _))) => |
| 218 | ::std::result::Result::Ok((i, o)), |
| 219 | ::std::result::Result::Err(err) => |
| 220 | ::std::result::Result::Err(err), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 221 | } |
| 222 | }; |
| 223 | |
| 224 | ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 225 | terminated!($i, $submac!($($args)*), call!($g)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | ($i:expr, $f:expr, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 229 | terminated!($i, call!($f), $submac!($($args)*)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 230 | }; |
| 231 | |
| 232 | ($i:expr, $f:expr, $g:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 233 | terminated!($i, call!($f), call!($g)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 234 | }; |
| 235 | } |
| 236 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 237 | /// Parse zero or more values using the given parser. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 238 | /// |
| 239 | /// - **Syntax:** `many0!(THING)` |
| 240 | /// - **Output:** `Vec<THING>` |
| 241 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 242 | /// You may also be looking for: |
| 243 | /// |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 244 | /// - `call!(Delimited::parse_separated)` - zero or more values with separator |
| 245 | /// - `call!(Delimited::parse_separated_nonempty)` - one or more values |
| 246 | /// - `call!(Delimited::parse_terminated)` - zero or more, allows trailing separator |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 247 | /// |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 248 | /// ```rust |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 249 | /// #[macro_use] |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 250 | /// extern crate syn; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 251 | /// |
| 252 | /// use syn::Item; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 253 | /// |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 254 | /// named!(items -> Vec<Item>, many0!(syn!(Item))); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 255 | /// |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 256 | /// # fn main() {} |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 257 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 258 | macro_rules! many0 { |
| 259 | ($i:expr, $submac:ident!( $($args:tt)* )) => {{ |
| 260 | let ret; |
| 261 | let mut res = ::std::vec::Vec::new(); |
| 262 | let mut input = $i; |
| 263 | |
| 264 | loop { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 265 | if input.eof() { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 266 | ret = ::std::result::Result::Ok((input, res)); |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 267 | break; |
| 268 | } |
| 269 | |
| 270 | match $submac!(input, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 271 | ::std::result::Result::Err(_) => { |
| 272 | ret = ::std::result::Result::Ok((input, res)); |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 273 | break; |
| 274 | } |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 275 | ::std::result::Result::Ok((i, o)) => { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 276 | // loop trip must always consume (otherwise infinite loops) |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 277 | if i == input { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 278 | ret = $crate::parse_error(); |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 279 | break; |
| 280 | } |
| 281 | |
| 282 | res.push(o); |
| 283 | input = i; |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | ret |
| 289 | }}; |
| 290 | |
| 291 | ($i:expr, $f:expr) => { |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 292 | $crate::parsers::many0($i, $f) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 293 | }; |
| 294 | } |
| 295 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 296 | // Improve compile time by compiling this loop only once per type it is used |
| 297 | // with. |
| 298 | // |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 299 | // Not public API. |
| 300 | #[doc(hidden)] |
David Tolnay | 1c03d8c | 2017-12-26 23:17:06 -0500 | [diff] [blame] | 301 | pub fn many0<T>(mut input: Cursor, f: fn(Cursor) -> PResult<T>) -> PResult<Vec<T>> { |
David Tolnay | bc84d5a | 2016-10-08 13:20:57 -0700 | [diff] [blame] | 302 | let mut res = Vec::new(); |
| 303 | |
| 304 | loop { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 305 | if input.eof() { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 306 | return Ok((input, res)); |
David Tolnay | bc84d5a | 2016-10-08 13:20:57 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | match f(input) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 310 | Err(_) => { |
| 311 | return Ok((input, res)); |
David Tolnay | bc84d5a | 2016-10-08 13:20:57 -0700 | [diff] [blame] | 312 | } |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 313 | Ok((i, o)) => { |
David Tolnay | bc84d5a | 2016-10-08 13:20:57 -0700 | [diff] [blame] | 314 | // loop trip must always consume (otherwise infinite loops) |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 315 | if i == input { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 316 | return parse_error(); |
David Tolnay | bc84d5a | 2016-10-08 13:20:57 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | res.push(o); |
| 320 | input = i; |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 326 | /// Parse a value without consuming it from the input data. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 327 | /// |
| 328 | /// - **Syntax:** `peek!(THING)` |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 329 | /// - **Output:** `THING` |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 330 | /// |
| 331 | /// ```rust |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 332 | /// #[macro_use] |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 333 | /// extern crate syn; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 334 | /// |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 335 | /// use syn::{Expr, Ident}; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 336 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 337 | /// // Parse an expression that begins with an identifier. |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 338 | /// named!(ident_expr -> (Ident, Expr), |
| 339 | /// tuple!(peek!(syn!(Ident)), syn!(Expr)) |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 340 | /// ); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 341 | /// |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 342 | /// # fn main() {} |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 343 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 344 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 345 | macro_rules! peek { |
| 346 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
| 347 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 348 | ::std::result::Result::Ok((_, o)) => ::std::result::Result::Ok(($i, o)), |
| 349 | ::std::result::Result::Err(err) => ::std::result::Result::Err(err), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 350 | } |
| 351 | }; |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 352 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 353 | ($i:expr, $f:expr) => { |
| 354 | peek!($i, call!($f)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 355 | }; |
| 356 | } |
| 357 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 358 | /// Pattern-match the result of a parser to select which other parser to run. |
| 359 | /// |
| 360 | /// - **Syntax:** `switch!(TARGET, PAT1 => THEN1 | PAT2 => THEN2 | ...)` |
| 361 | /// - **Output:** `T`, the return type of `THEN1` and `THEN2` and ... |
| 362 | /// |
| 363 | /// ```rust |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 364 | /// #[macro_use] |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 365 | /// extern crate syn; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 366 | /// |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 367 | /// use syn::{Ident, Type}; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 368 | /// |
| 369 | /// #[derive(Debug)] |
| 370 | /// enum UnitType { |
| 371 | /// Struct { |
| 372 | /// name: Ident, |
| 373 | /// }, |
| 374 | /// Enum { |
| 375 | /// name: Ident, |
| 376 | /// variant: Ident, |
| 377 | /// }, |
| 378 | /// } |
| 379 | /// |
| 380 | /// // Parse a unit struct or enum: either `struct S;` or `enum E { V }`. |
| 381 | /// named!(unit_type -> UnitType, do_parse!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 382 | /// which: alt!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 383 | /// keyword!(struct) => { |_| 0 } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 384 | /// | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 385 | /// keyword!(enum) => { |_| 1 } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 386 | /// ) >> |
| 387 | /// id: syn!(Ident) >> |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 388 | /// item: switch!(value!(which), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 389 | /// 0 => map!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 390 | /// punct!(;), |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 391 | /// move |_| UnitType::Struct { |
| 392 | /// name: id, |
| 393 | /// } |
| 394 | /// ) |
| 395 | /// | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 396 | /// 1 => map!( |
| 397 | /// braces!(syn!(Ident)), |
| 398 | /// move |(variant, _)| UnitType::Enum { |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 399 | /// name: id, |
| 400 | /// variant: variant, |
| 401 | /// } |
| 402 | /// ) |
David Tolnay | 92a5651 | 2017-11-10 00:02:14 -0800 | [diff] [blame] | 403 | /// | |
| 404 | /// _ => reject!() |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 405 | /// ) >> |
| 406 | /// (item) |
| 407 | /// )); |
| 408 | /// |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 409 | /// # fn main() {} |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 410 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 411 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 412 | macro_rules! switch { |
| 413 | ($i:expr, $submac:ident!( $($args:tt)* ), $($p:pat => $subrule:ident!( $($args2:tt)* ))|* ) => { |
| 414 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 415 | ::std::result::Result::Err(err) => ::std::result::Result::Err(err), |
| 416 | ::std::result::Result::Ok((i, o)) => match o { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 417 | $( |
| 418 | $p => $subrule!(i, $($args2)*), |
| 419 | )* |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | }; |
| 423 | } |
| 424 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 425 | /// Produce the given value without parsing anything. Useful as an argument to |
| 426 | /// `switch!`. |
| 427 | /// |
| 428 | /// - **Syntax:** `value!(VALUE)` |
| 429 | /// - **Output:** `VALUE` |
| 430 | /// |
| 431 | /// ```rust |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 432 | /// #[macro_use] |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 433 | /// extern crate syn; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 434 | /// |
David Tolnay | 92a5651 | 2017-11-10 00:02:14 -0800 | [diff] [blame] | 435 | /// use syn::Ident; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 436 | /// |
| 437 | /// #[derive(Debug)] |
| 438 | /// enum UnitType { |
| 439 | /// Struct { |
| 440 | /// name: Ident, |
| 441 | /// }, |
| 442 | /// Enum { |
| 443 | /// name: Ident, |
| 444 | /// variant: Ident, |
| 445 | /// }, |
| 446 | /// } |
| 447 | /// |
| 448 | /// // Parse a unit struct or enum: either `struct S;` or `enum E { V }`. |
| 449 | /// named!(unit_type -> UnitType, do_parse!( |
David Tolnay | 92a5651 | 2017-11-10 00:02:14 -0800 | [diff] [blame] | 450 | /// is_struct: alt!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 451 | /// keyword!(struct) => { |_| true } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 452 | /// | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 453 | /// keyword!(enum) => { |_| false } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 454 | /// ) >> |
| 455 | /// id: syn!(Ident) >> |
David Tolnay | 92a5651 | 2017-11-10 00:02:14 -0800 | [diff] [blame] | 456 | /// item: switch!(value!(is_struct), |
| 457 | /// true => map!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 458 | /// punct!(;), |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 459 | /// move |_| UnitType::Struct { |
| 460 | /// name: id, |
| 461 | /// } |
| 462 | /// ) |
| 463 | /// | |
David Tolnay | 92a5651 | 2017-11-10 00:02:14 -0800 | [diff] [blame] | 464 | /// false => map!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 465 | /// braces!(syn!(Ident)), |
| 466 | /// move |(variant, _)| UnitType::Enum { |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 467 | /// name: id, |
| 468 | /// variant: variant, |
| 469 | /// } |
| 470 | /// ) |
| 471 | /// ) >> |
| 472 | /// (item) |
| 473 | /// )); |
| 474 | /// |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 475 | /// # fn main() {} |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 476 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 477 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 478 | macro_rules! value { |
| 479 | ($i:expr, $res:expr) => { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 480 | ::std::result::Result::Ok(($i, $res)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 481 | }; |
| 482 | } |
| 483 | |
David Tolnay | 92a5651 | 2017-11-10 00:02:14 -0800 | [diff] [blame] | 484 | /// Unconditionally fail to parse anything. This may be useful in ignoring some |
| 485 | /// arms of a `switch!` parser. |
| 486 | /// |
| 487 | /// - **Syntax:** `reject!()` |
| 488 | /// - **Output:** never succeeds |
| 489 | /// |
| 490 | /// ```rust |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 491 | /// #[macro_use] |
David Tolnay | 92a5651 | 2017-11-10 00:02:14 -0800 | [diff] [blame] | 492 | /// extern crate syn; |
David Tolnay | 92a5651 | 2017-11-10 00:02:14 -0800 | [diff] [blame] | 493 | /// |
| 494 | /// use syn::Item; |
| 495 | /// |
| 496 | /// // Parse any item, except for a module. |
| 497 | /// named!(almost_any_item -> Item, |
| 498 | /// switch!(syn!(Item), |
| 499 | /// Item::Mod(_) => reject!() |
| 500 | /// | |
| 501 | /// ok => value!(ok) |
| 502 | /// ) |
| 503 | /// ); |
| 504 | /// |
| 505 | /// # fn main() {} |
| 506 | /// ``` |
| 507 | #[macro_export] |
| 508 | macro_rules! reject { |
David Tolnay | 2bd1742 | 2017-12-25 18:44:20 -0500 | [diff] [blame] | 509 | ($i:expr,) => {{ |
| 510 | let _ = $i; |
David Tolnay | 92a5651 | 2017-11-10 00:02:14 -0800 | [diff] [blame] | 511 | $crate::parse_error() |
David Tolnay | 2bd1742 | 2017-12-25 18:44:20 -0500 | [diff] [blame] | 512 | }} |
David Tolnay | 92a5651 | 2017-11-10 00:02:14 -0800 | [diff] [blame] | 513 | } |
| 514 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 515 | /// Run a series of parsers and produce all of the results in a tuple. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 516 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 517 | /// - **Syntax:** `tuple!(A, B, C, ...)` |
| 518 | /// - **Output:** `(A, B, C, ...)` |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 519 | /// |
| 520 | /// ```rust |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 521 | /// #[macro_use] |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 522 | /// extern crate syn; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 523 | /// |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 524 | /// use syn::Type; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 525 | /// |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 526 | /// named!(two_types -> (Type, Type), tuple!(syn!(Type), syn!(Type))); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 527 | /// |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 528 | /// # fn main() {} |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 529 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 530 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 531 | macro_rules! tuple { |
| 532 | ($i:expr, $($rest:tt)*) => { |
| 533 | tuple_parser!($i, (), $($rest)*) |
| 534 | }; |
| 535 | } |
| 536 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 537 | /// Internal parser, do not use directly. |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 538 | #[doc(hidden)] |
| 539 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 540 | macro_rules! tuple_parser { |
| 541 | ($i:expr, ($($parsed:tt),*), $e:ident, $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 542 | tuple_parser!($i, ($($parsed),*), call!($e), $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 543 | }; |
| 544 | |
| 545 | ($i:expr, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => { |
| 546 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 547 | ::std::result::Result::Err(err) => |
| 548 | ::std::result::Result::Err(err), |
| 549 | ::std::result::Result::Ok((i, o)) => |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 550 | tuple_parser!(i, (o), $($rest)*), |
| 551 | } |
| 552 | }; |
| 553 | |
| 554 | ($i:expr, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => { |
| 555 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 556 | ::std::result::Result::Err(err) => |
| 557 | ::std::result::Result::Err(err), |
| 558 | ::std::result::Result::Ok((i, o)) => |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 559 | tuple_parser!(i, ($($parsed)* , o), $($rest)*), |
| 560 | } |
| 561 | }; |
| 562 | |
| 563 | ($i:expr, ($($parsed:tt),*), $e:ident) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 564 | tuple_parser!($i, ($($parsed),*), call!($e)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 565 | }; |
| 566 | |
| 567 | ($i:expr, (), $submac:ident!( $($args:tt)* )) => { |
| 568 | $submac!($i, $($args)*) |
| 569 | }; |
| 570 | |
| 571 | ($i:expr, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => { |
| 572 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 573 | ::std::result::Result::Err(err) => |
| 574 | ::std::result::Result::Err(err), |
| 575 | ::std::result::Result::Ok((i, o)) => |
| 576 | ::std::result::Result::Ok((i, ($($parsed),*, o))), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 577 | } |
| 578 | }; |
| 579 | |
| 580 | ($i:expr, ($($parsed:expr),*)) => { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 581 | ::std::result::Result::Ok(($i, ($($parsed),*))) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 582 | }; |
| 583 | } |
| 584 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 585 | /// Run a series of parsers, returning the result of the first one which |
| 586 | /// succeeds. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 587 | /// |
| 588 | /// Optionally allows for the result to be transformed. |
| 589 | /// |
| 590 | /// - **Syntax:** `alt!(THING1 | THING2 => { FUNC } | ...)` |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 591 | /// - **Output:** `T`, the return type of `THING1` and `FUNC(THING2)` and ... |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 592 | /// |
| 593 | /// ```rust |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 594 | /// #[macro_use] |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 595 | /// extern crate syn; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 596 | /// |
| 597 | /// use syn::Ident; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 598 | /// |
| 599 | /// named!(ident_or_bang -> Ident, |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 600 | /// alt!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 601 | /// syn!(Ident) |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 602 | /// | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 603 | /// punct!(!) => { |_| "BANG".into() } |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 604 | /// ) |
| 605 | /// ); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 606 | /// |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 607 | /// # fn main() {} |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 608 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 609 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 610 | macro_rules! alt { |
| 611 | ($i:expr, $e:ident | $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 612 | alt!($i, call!($e) | $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 613 | }; |
| 614 | |
| 615 | ($i:expr, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => { |
| 616 | match $subrule!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 617 | res @ ::std::result::Result::Ok(_) => res, |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 618 | _ => alt!($i, $($rest)*) |
| 619 | } |
| 620 | }; |
| 621 | |
| 622 | ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => { |
| 623 | match $subrule!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 624 | ::std::result::Result::Ok((i, o)) => |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 625 | ::std::result::Result::Ok((i, $crate::parsers::invoke($gen, o))), |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 626 | ::std::result::Result::Err(_) => alt!($i, $($rest)*), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 627 | } |
| 628 | }; |
| 629 | |
| 630 | ($i:expr, $e:ident => { $gen:expr } | $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 631 | alt!($i, call!($e) => { $gen } | $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 632 | }; |
| 633 | |
| 634 | ($i:expr, $e:ident => { $gen:expr }) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 635 | alt!($i, call!($e) => { $gen }) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 636 | }; |
| 637 | |
| 638 | ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => { |
| 639 | match $subrule!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 640 | ::std::result::Result::Ok((i, o)) => |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 641 | ::std::result::Result::Ok((i, $crate::parsers::invoke($gen, o))), |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 642 | ::std::result::Result::Err(err) => |
| 643 | ::std::result::Result::Err(err), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 644 | } |
| 645 | }; |
| 646 | |
| 647 | ($i:expr, $e:ident) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 648 | alt!($i, call!($e)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 649 | }; |
| 650 | |
| 651 | ($i:expr, $subrule:ident!( $($args:tt)*)) => { |
David Tolnay | 5377b17 | 2016-10-25 01:13:12 -0700 | [diff] [blame] | 652 | $subrule!($i, $($args)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 653 | }; |
| 654 | } |
| 655 | |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 656 | /// Run a series of parsers, one after another, optionally assigning the results |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 657 | /// a name. Fail if any of the parsers fails. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 658 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 659 | /// Produces the result of evaluating the final expression in parentheses with |
| 660 | /// all of the previously named results bound. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 661 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 662 | /// - **Syntax:** `do_parse!(name: THING1 >> THING2 >> (RESULT))` |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 663 | /// - **Output:** `RESULT` |
| 664 | /// |
| 665 | /// ```rust |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 666 | /// #[macro_use] |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 667 | /// extern crate syn; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 668 | /// extern crate proc_macro2; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 669 | /// |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 670 | /// use syn::Ident; |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 671 | /// use syn::token::Paren; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 672 | /// use proc_macro2::TokenStream; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 673 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 674 | /// // Parse a macro invocation like `stringify!($args)`. |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 675 | /// named!(simple_mac -> (Ident, (TokenStream, Paren)), do_parse!( |
| 676 | /// name: syn!(Ident) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 677 | /// punct!(!) >> |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 678 | /// body: parens!(syn!(TokenStream)) >> |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 679 | /// (name, body) |
| 680 | /// )); |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 681 | /// |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 682 | /// # fn main() {} |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 683 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 684 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 685 | macro_rules! do_parse { |
| 686 | ($i:expr, ( $($rest:expr),* )) => { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 687 | ::std::result::Result::Ok(($i, ( $($rest),* ))) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 688 | }; |
| 689 | |
| 690 | ($i:expr, $e:ident >> $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 691 | do_parse!($i, call!($e) >> $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 692 | }; |
| 693 | |
| 694 | ($i:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => { |
| 695 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 696 | ::std::result::Result::Err(err) => |
| 697 | ::std::result::Result::Err(err), |
| 698 | ::std::result::Result::Ok((i, _)) => |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 699 | do_parse!(i, $($rest)*), |
| 700 | } |
| 701 | }; |
| 702 | |
| 703 | ($i:expr, $field:ident : $e:ident >> $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 704 | do_parse!($i, $field: call!($e) >> $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 705 | }; |
| 706 | |
| 707 | ($i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => { |
| 708 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 709 | ::std::result::Result::Err(err) => |
| 710 | ::std::result::Result::Err(err), |
| 711 | ::std::result::Result::Ok((i, o)) => { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 712 | let $field = o; |
| 713 | do_parse!(i, $($rest)*) |
| 714 | }, |
| 715 | } |
| 716 | }; |
| 717 | |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 718 | ($i:expr, mut $field:ident : $e:ident >> $($rest:tt)*) => { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 719 | do_parse!($i, mut $field: call!($e) >> $($rest)*) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 720 | }; |
| 721 | |
| 722 | ($i:expr, mut $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => { |
| 723 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 724 | ::std::result::Result::Err(err) => |
| 725 | ::std::result::Result::Err(err), |
| 726 | ::std::result::Result::Ok((i, o)) => { |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 727 | let mut $field = o; |
| 728 | do_parse!(i, $($rest)*) |
| 729 | }, |
| 730 | } |
| 731 | }; |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 732 | } |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 733 | |
| 734 | #[macro_export] |
| 735 | macro_rules! input_end { |
| 736 | ($i:expr,) => { |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 737 | $crate::parsers::input_end($i) |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 738 | }; |
| 739 | } |
| 740 | |
| 741 | // Not a public API |
| 742 | #[doc(hidden)] |
David Tolnay | 1fc4e49 | 2017-11-11 22:17:22 -0800 | [diff] [blame] | 743 | pub fn input_end(input: Cursor) -> PResult<'static, ()> { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 744 | if input.eof() { |
David Tolnay | 1fc4e49 | 2017-11-11 22:17:22 -0800 | [diff] [blame] | 745 | Ok((Cursor::empty(), ())) |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 746 | } else { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 747 | parse_error() |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 748 | } |
| 749 | } |
David Tolnay | f03cdb8 | 2017-12-30 00:05:58 -0500 | [diff] [blame] | 750 | |
| 751 | /// Turn a failed parse into `None` and a successful parse into `Some`. |
| 752 | /// |
| 753 | /// - **Syntax:** `option!(THING)` |
| 754 | /// - **Output:** `Option<THING>` |
| 755 | /// |
| 756 | /// ```rust |
| 757 | /// #[macro_use] |
| 758 | /// extern crate syn; |
| 759 | /// |
| 760 | /// use syn::token::Bang; |
| 761 | /// |
| 762 | /// named!(maybe_bang -> Option<Bang>, option!(punct!(!))); |
| 763 | /// |
| 764 | /// # fn main() {} |
| 765 | /// ``` |
| 766 | #[macro_export] |
| 767 | macro_rules! option { |
| 768 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
| 769 | match $submac!($i, $($args)*) { |
| 770 | ::std::result::Result::Ok((i, o)) => |
| 771 | ::std::result::Result::Ok((i, Some(o))), |
| 772 | ::std::result::Result::Err(_) => |
| 773 | ::std::result::Result::Ok(($i, None)), |
| 774 | } |
| 775 | }; |
| 776 | |
| 777 | ($i:expr, $f:expr) => { |
| 778 | option!($i, call!($f)); |
| 779 | }; |
| 780 | } |
| 781 | |
| 782 | /// Turn a failed parse into an empty vector. The argument parser must itself |
| 783 | /// return a vector. |
| 784 | /// |
| 785 | /// This is often more convenient than `option!(...)` when the argument produces |
| 786 | /// a vector. |
| 787 | /// |
| 788 | /// - **Syntax:** `opt_vec!(THING)` |
| 789 | /// - **Output:** `THING`, which must be `Vec<T>` |
| 790 | /// |
| 791 | /// ```rust |
| 792 | /// #[macro_use] |
| 793 | /// extern crate syn; |
| 794 | /// |
| 795 | /// use syn::{Lifetime, Type}; |
| 796 | /// use syn::delimited::Delimited; |
| 797 | /// use syn::token::*; |
| 798 | /// |
| 799 | /// named!(bound_lifetimes -> (Vec<Lifetime>, Type), tuple!( |
| 800 | /// opt_vec!(do_parse!( |
| 801 | /// keyword!(for) >> |
| 802 | /// punct!(<) >> |
| 803 | /// lifetimes: call!(Delimited::<Lifetime, Comma>::parse_terminated) >> |
| 804 | /// punct!(>) >> |
| 805 | /// (lifetimes.into_vec()) |
| 806 | /// )), |
| 807 | /// syn!(Type) |
| 808 | /// )); |
| 809 | /// |
| 810 | /// # fn main() {} |
| 811 | /// ``` |
| 812 | #[macro_export] |
| 813 | macro_rules! opt_vec { |
| 814 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
| 815 | match $submac!($i, $($args)*) { |
| 816 | ::std::result::Result::Ok((i, o)) => |
| 817 | ::std::result::Result::Ok((i, o)), |
| 818 | ::std::result::Result::Err(_) => |
| 819 | ::std::result::Result::Ok(($i, Vec::new())) |
| 820 | } |
| 821 | }; |
| 822 | } |
| 823 | |
| 824 | /// Parses nothing and always succeeds. |
| 825 | /// |
| 826 | /// This can be useful as a fallthrough case in `alt!`. |
| 827 | /// |
| 828 | /// - **Syntax:** `epsilon!()` |
| 829 | /// - **Output:** `()` |
| 830 | /// |
| 831 | /// ```rust |
| 832 | /// #[macro_use] |
| 833 | /// extern crate syn; |
| 834 | /// |
| 835 | /// enum Mutability { |
| 836 | /// Mutable(Token![mut]), |
| 837 | /// Immutable, |
| 838 | /// } |
| 839 | /// |
| 840 | /// named!(mutability -> Mutability, alt!( |
| 841 | /// keyword!(mut) => { Mutability::Mutable } |
| 842 | /// | |
| 843 | /// epsilon!() => { |_| Mutability::Immutable } |
| 844 | /// )); |
| 845 | /// |
| 846 | /// # fn main() {} |
| 847 | #[macro_export] |
| 848 | macro_rules! epsilon { |
| 849 | ($i:expr,) => { |
| 850 | ::std::result::Result::Ok(($i, ())) |
| 851 | }; |
| 852 | } |
| 853 | |
| 854 | /// Run a parser, binding the result to a name, and then evaluating an |
| 855 | /// expression. |
| 856 | /// |
| 857 | /// Discards the result of the expression and parser. |
| 858 | /// |
| 859 | /// - **Syntax:** `tap!(NAME : THING => EXPR)` |
| 860 | /// - **Output:** `()` |
| 861 | /// |
| 862 | /// ```rust |
| 863 | /// #[macro_use] |
| 864 | /// extern crate syn; |
| 865 | /// |
| 866 | /// use syn::{Expr, ExprCall}; |
| 867 | /// use syn::token::RArrow; |
| 868 | /// |
| 869 | /// named!(expr_with_arrow_call -> Expr, do_parse!( |
| 870 | /// mut e: syn!(Expr) >> |
| 871 | /// many0!(tap!(arg: tuple!(punct!(->), syn!(Expr)) => { |
| 872 | /// e = Expr::Call(ExprCall { |
| 873 | /// attrs: Vec::new(), |
| 874 | /// func: Box::new(e), |
| 875 | /// args: vec![arg.1].into(), |
| 876 | /// paren_token: Default::default(), |
| 877 | /// }); |
| 878 | /// })) >> |
| 879 | /// (e) |
| 880 | /// )); |
| 881 | /// |
| 882 | /// # fn main() {} |
| 883 | /// ``` |
| 884 | #[doc(hidden)] |
| 885 | #[macro_export] |
| 886 | macro_rules! tap { |
| 887 | ($i:expr, $name:ident : $submac:ident!( $($args:tt)* ) => $e:expr) => { |
| 888 | match $submac!($i, $($args)*) { |
| 889 | ::std::result::Result::Ok((i, o)) => { |
| 890 | let $name = o; |
| 891 | $e; |
| 892 | ::std::result::Result::Ok((i, ())) |
| 893 | } |
| 894 | ::std::result::Result::Err(err) => |
| 895 | ::std::result::Result::Err(err), |
| 896 | } |
| 897 | }; |
| 898 | |
| 899 | ($i:expr, $name:ident : $f:expr => $e:expr) => { |
| 900 | tap!($i, $name: call!($f) => $e); |
| 901 | }; |
| 902 | } |
| 903 | |
| 904 | /// Parse a type through the `Synom` trait. |
| 905 | /// |
| 906 | /// This is a convenience macro used to invoke the `Synom::parse` method for a |
| 907 | /// type, you'll find this in quite a few parsers. This is also the primary way |
| 908 | /// to parse punctuation. |
| 909 | /// |
| 910 | /// - **Syntax:** `syn!(TYPE)` |
| 911 | /// - **Output:** `TYPE` |
| 912 | /// |
| 913 | /// ```rust |
| 914 | /// #[macro_use] |
| 915 | /// extern crate syn; |
| 916 | /// |
| 917 | /// use syn::Expr; |
| 918 | /// |
| 919 | /// named!(expression -> Expr, syn!(Expr)); |
| 920 | /// |
| 921 | /// named!(expression_dot -> (Expr, Token![.]), tuple!(syn!(Expr), punct!(.))); |
| 922 | /// |
| 923 | /// # fn main() {} |
| 924 | /// ``` |
| 925 | #[macro_export] |
| 926 | macro_rules! syn { |
| 927 | ($i:expr, $t:ty) => { |
| 928 | call!($i, <$t as $crate::synom::Synom>::parse) |
| 929 | }; |
| 930 | } |
| 931 | |
| 932 | /// Parse a parenthesized-surrounded subtree. |
| 933 | /// |
| 934 | /// This macro will invoke a sub-parser inside of all tokens contained in |
| 935 | /// parenthesis. The sub-parser is required to consume all tokens within the |
| 936 | /// parens or else this parser will return an error. |
| 937 | /// |
| 938 | /// - **Syntax:** `parens!(SUBPARSER)` |
| 939 | /// - **Output:** `(SUBPARSER_RET, Paren)` |
| 940 | /// |
| 941 | /// ```rust |
| 942 | /// #[macro_use] |
| 943 | /// extern crate syn; |
| 944 | /// |
| 945 | /// use syn::Expr; |
| 946 | /// use syn::token::Paren; |
| 947 | /// |
| 948 | /// named!(expr_paren -> (Expr, Paren), parens!(syn!(Expr))); |
| 949 | /// |
| 950 | /// # fn main() {} |
| 951 | /// ``` |
| 952 | #[macro_export] |
| 953 | macro_rules! parens { |
| 954 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
| 955 | $crate::token::Paren::parse($i, |i| $submac!(i, $($args)*)) |
| 956 | }; |
| 957 | |
| 958 | ($i:expr, $f:expr) => { |
| 959 | parens!($i, call!($f)); |
| 960 | }; |
| 961 | } |
| 962 | |
| 963 | /// Same as the `parens` macro, but for brackets. |
| 964 | #[macro_export] |
| 965 | macro_rules! brackets { |
| 966 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
| 967 | $crate::token::Bracket::parse($i, |i| $submac!(i, $($args)*)) |
| 968 | }; |
| 969 | |
| 970 | ($i:expr, $f:expr) => { |
| 971 | brackets!($i, call!($f)); |
| 972 | }; |
| 973 | } |
| 974 | |
| 975 | /// Same as the `parens` macro, but for braces. |
| 976 | #[macro_export] |
| 977 | macro_rules! braces { |
| 978 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
| 979 | $crate::token::Brace::parse($i, |i| $submac!(i, $($args)*)) |
| 980 | }; |
| 981 | |
| 982 | ($i:expr, $f:expr) => { |
| 983 | braces!($i, call!($f)); |
| 984 | }; |
| 985 | } |
| 986 | |
| 987 | /// Same as the `parens` macro, but for none-delimited sequences (groups). |
| 988 | #[macro_export] |
| 989 | macro_rules! grouped { |
| 990 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
| 991 | $crate::token::Group::parse($i, |i| $submac!(i, $($args)*)) |
| 992 | }; |
| 993 | |
| 994 | ($i:expr, $f:expr) => { |
| 995 | grouped!($i, call!($f)); |
| 996 | }; |
| 997 | } |