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 | |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 5 | /// Define a parser function with the signature expected by syn parser |
| 6 | /// combinators. |
| 7 | /// |
| 8 | /// The function may be the `parse` function of the [`Synom`] trait, or it may |
| 9 | /// be a free-standing function with an arbitrary name. When implementing the |
| 10 | /// `Synom` trait, the function name is `parse` and the return type is `Self`. |
| 11 | /// |
| 12 | /// [`Synom`]: synom/trait.Synom.html |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 13 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 14 | /// - **Syntax:** `named!(NAME -> TYPE, PARSER)` or `named!(pub NAME -> TYPE, PARSER)` |
| 15 | /// |
| 16 | /// ```rust |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 17 | /// #[macro_use] |
| 18 | /// extern crate syn; |
| 19 | /// |
| 20 | /// use syn::Type; |
| 21 | /// use syn::delimited::Delimited; |
| 22 | /// use syn::synom::Synom; |
| 23 | /// |
| 24 | /// /// Parses one or more Rust types separated by commas. |
| 25 | /// /// |
| 26 | /// /// Example: `String, Vec<T>, [u8; LEN + 1]` |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 27 | /// named!(pub comma_separated_types -> Delimited<Type, Token![,]>, |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 28 | /// call!(Delimited::parse_separated_nonempty) |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 29 | /// ); |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 30 | /// |
| 31 | /// /// The same function as a `Synom` implementation. |
| 32 | /// struct CommaSeparatedTypes { |
| 33 | /// types: Delimited<Type, Token![,]>, |
| 34 | /// } |
| 35 | /// |
| 36 | /// impl Synom for CommaSeparatedTypes { |
| 37 | /// /// As the default behavior, we want there to be at least 1 type. |
| 38 | /// named!(parse -> Self, do_parse!( |
| 39 | /// types: call!(Delimited::parse_separated_nonempty) >> |
| 40 | /// (CommaSeparatedTypes { |
| 41 | /// types: types, |
| 42 | /// }) |
| 43 | /// )); |
| 44 | /// } |
| 45 | /// |
| 46 | /// impl CommaSeparatedTypes { |
| 47 | /// /// A separate parser that the user can invoke explicitly which allows |
| 48 | /// /// for parsing 0 or more types, rather than the default 1 or more. |
| 49 | /// named!(pub parse0 -> Self, do_parse!( |
| 50 | /// types: call!(Delimited::parse_separated) >> |
| 51 | /// (CommaSeparatedTypes { |
| 52 | /// types: types, |
| 53 | /// }) |
| 54 | /// )); |
| 55 | /// } |
| 56 | /// # |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 57 | /// # fn main() {} |
| 58 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 59 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 60 | macro_rules! named { |
| 61 | ($name:ident -> $o:ty, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | 203557a | 2017-12-27 23:59:33 -0500 | [diff] [blame] | 62 | fn $name(i: $crate::synom::Cursor) -> $crate::synom::PResult<$o> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 63 | $submac!(i, $($args)*) |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | (pub $name:ident -> $o:ty, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | 203557a | 2017-12-27 23:59:33 -0500 | [diff] [blame] | 68 | pub fn $name(i: $crate::synom::Cursor) -> $crate::synom::PResult<$o> { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 69 | $submac!(i, $($args)*) |
| 70 | } |
| 71 | }; |
Michael Layzell | f8334e3 | 2017-06-04 19:01:08 -0400 | [diff] [blame] | 72 | |
| 73 | // These two variants are for defining named parsers which have custom |
| 74 | // arguments, and are called with `call!()` |
| 75 | ($name:ident($($params:tt)*) -> $o:ty, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | 203557a | 2017-12-27 23:59:33 -0500 | [diff] [blame] | 76 | fn $name(i: $crate::synom::Cursor, $($params)*) -> $crate::synom::PResult<$o> { |
Michael Layzell | f8334e3 | 2017-06-04 19:01:08 -0400 | [diff] [blame] | 77 | $submac!(i, $($args)*) |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | (pub $name:ident($($params:tt)*) -> $o:ty, $submac:ident!( $($args:tt)* )) => { |
David Tolnay | 203557a | 2017-12-27 23:59:33 -0500 | [diff] [blame] | 82 | pub fn $name(i: $crate::synom::Cursor, $($params)*) -> $crate::synom::PResult<$o> { |
Michael Layzell | f8334e3 | 2017-06-04 19:01:08 -0400 | [diff] [blame] | 83 | $submac!(i, $($args)*) |
| 84 | } |
| 85 | }; |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 86 | } |
| 87 | |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 88 | #[cfg(all(feature = "verbose-trace", not(feature = "all-features")))] |
Nika Layzell | ae81b37 | 2017-12-05 14:12:33 -0500 | [diff] [blame] | 89 | #[macro_export] |
| 90 | macro_rules! call { |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 91 | ($i:expr, $fun:expr $(, $args:expr)*) => {{ |
| 92 | let i = $i; |
| 93 | eprintln!(concat!(" -> ", stringify!($fun), " @ {:?}"), i); |
| 94 | let r = $fun(i $(, $args)*); |
| 95 | match r { |
| 96 | Ok((i, _)) => eprintln!(concat!("OK ", stringify!($fun), " @ {:?}"), i), |
| 97 | Err(_) => eprintln!(concat!("ERR ", stringify!($fun), " @ {:?}"), i), |
Nika Layzell | ae81b37 | 2017-12-05 14:12:33 -0500 | [diff] [blame] | 98 | } |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 99 | r |
| 100 | }}; |
Nika Layzell | ae81b37 | 2017-12-05 14:12:33 -0500 | [diff] [blame] | 101 | } |
| 102 | |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 103 | /// Invoke the given parser function with zero or more arguments. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 104 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 105 | /// - **Syntax:** `call!(FN, ARGS...)` |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 106 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 107 | /// where the signature of the function is `fn(Cursor, ARGS...) -> PResult<T>` |
| 108 | /// |
| 109 | /// - **Output:** `T`, the result of invoking the function `FN` |
| 110 | /// |
| 111 | /// ```rust |
| 112 | /// #[macro_use] |
| 113 | /// extern crate syn; |
| 114 | /// |
| 115 | /// use syn::Type; |
| 116 | /// use syn::delimited::Delimited; |
| 117 | /// use syn::synom::Synom; |
| 118 | /// |
| 119 | /// /// Parses one or more Rust types separated by commas. |
| 120 | /// /// |
| 121 | /// /// Example: `String, Vec<T>, [u8; LEN + 1]` |
| 122 | /// struct CommaSeparatedTypes { |
| 123 | /// types: Delimited<Type, Token![,]>, |
| 124 | /// } |
| 125 | /// |
| 126 | /// impl Synom for CommaSeparatedTypes { |
| 127 | /// named!(parse -> Self, do_parse!( |
| 128 | /// types: call!(Delimited::parse_separated_nonempty) >> |
| 129 | /// (CommaSeparatedTypes { |
| 130 | /// types: types, |
| 131 | /// }) |
| 132 | /// )); |
| 133 | /// } |
| 134 | /// # |
| 135 | /// # fn main() {} |
| 136 | /// ``` |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 137 | #[cfg(any(not(feature = "verbose-trace"), feature = "all-features"))] |
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! call { |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 140 | ($i:expr, $fun:expr $(, $args:expr)*) => { |
| 141 | $fun($i $(, $args)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 142 | }; |
| 143 | } |
| 144 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 145 | /// Transform the result of a parser by applying a function or closure. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 146 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 147 | /// - **Syntax:** `map!(THING, FN)` |
| 148 | /// - **Output:** the return type of function FN applied to THING |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 149 | /// |
| 150 | /// ```rust |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 151 | /// #[macro_use] |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 152 | /// extern crate syn; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 153 | /// |
David Tolnay | b21bb0c | 2017-06-03 20:39:19 -0700 | [diff] [blame] | 154 | /// use syn::{Expr, ExprIf}; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 155 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 156 | /// /// Extracts the branch condition of an `if`-expression. |
David Tolnay | b21bb0c | 2017-06-03 20:39:19 -0700 | [diff] [blame] | 157 | /// fn get_cond(if_: ExprIf) -> Expr { |
| 158 | /// *if_.cond |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 159 | /// } |
| 160 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 161 | /// /// Parses a full `if`-expression but returns the condition part only. |
| 162 | /// /// |
| 163 | /// /// Example: `if x > 0xFF { "big" } else { "small" }` |
| 164 | /// /// The return would be the expression `x > 0xFF`. |
David Tolnay | b21bb0c | 2017-06-03 20:39:19 -0700 | [diff] [blame] | 165 | /// named!(if_condition -> Expr, |
| 166 | /// map!(syn!(ExprIf), get_cond) |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 167 | /// ); |
| 168 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 169 | /// /// Equivalent using a closure. |
David Tolnay | b21bb0c | 2017-06-03 20:39:19 -0700 | [diff] [blame] | 170 | /// named!(if_condition2 -> Expr, |
David Tolnay | bc7d7d9 | 2017-06-03 20:54:05 -0700 | [diff] [blame] | 171 | /// map!(syn!(ExprIf), |if_| *if_.cond) |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 172 | /// ); |
David Tolnay | b21bb0c | 2017-06-03 20:39:19 -0700 | [diff] [blame] | 173 | /// # |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 174 | /// # fn main() {} |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 175 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 176 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 177 | macro_rules! map { |
| 178 | ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 179 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 180 | ::std::result::Result::Err(err) => |
| 181 | ::std::result::Result::Err(err), |
| 182 | ::std::result::Result::Ok((i, o)) => |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 183 | ::std::result::Result::Ok((i, $crate::parsers::invoke($g, o))), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 184 | } |
| 185 | }; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 186 | |
| 187 | ($i:expr, $f:expr, $g:expr) => { |
| 188 | map!($i, call!($f), $g) |
| 189 | }; |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 190 | } |
| 191 | |
David Tolnay | 995bff2 | 2017-12-17 23:44:43 -0800 | [diff] [blame] | 192 | // Somehow this helps with type inference in `map!` and `alt!`. |
David Tolnay | bc7d7d9 | 2017-06-03 20:54:05 -0700 | [diff] [blame] | 193 | // |
| 194 | // Not public API. |
| 195 | #[doc(hidden)] |
| 196 | pub fn invoke<T, R, F: FnOnce(T) -> R>(f: F, t: T) -> R { |
| 197 | f(t) |
| 198 | } |
| 199 | |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 200 | /// Invert the result of a parser by parsing successfully if the given parser |
| 201 | /// fails to parse and vice versa. |
| 202 | /// |
| 203 | /// Does not consume any of the input. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 204 | /// |
| 205 | /// - **Syntax:** `not!(THING)` |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 206 | /// - **Output:** `()` |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 207 | /// |
| 208 | /// ```rust |
| 209 | /// #[macro_use] |
| 210 | /// extern crate syn; |
| 211 | /// |
| 212 | /// use syn::{Expr, Ident}; |
| 213 | /// |
| 214 | /// /// Parses any expression that does not begin with a `-` minus sign. |
| 215 | /// named!(not_negative_expr -> Expr, do_parse!( |
| 216 | /// not!(punct!(-)) >> |
| 217 | /// e: syn!(Expr) >> |
| 218 | /// (e) |
| 219 | /// )); |
| 220 | /// # |
| 221 | /// # fn main() {} |
| 222 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 223 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 224 | macro_rules! not { |
| 225 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
| 226 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 227 | ::std::result::Result::Ok(_) => $crate::parse_error(), |
| 228 | ::std::result::Result::Err(_) => |
| 229 | ::std::result::Result::Ok(($i, ())), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 230 | } |
| 231 | }; |
| 232 | } |
| 233 | |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 234 | /// Execute a parser only if a condition is met, otherwise return None. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 235 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 236 | /// If you are familiar with nom, this is nom's `cond_with_error` parser. |
| 237 | /// |
| 238 | /// - **Syntax:** `cond!(CONDITION, THING)` |
| 239 | /// - **Output:** `Some(THING)` if the condition is true, else `None` |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 240 | /// |
| 241 | /// ```rust |
| 242 | /// #[macro_use] |
| 243 | /// extern crate syn; |
| 244 | /// |
| 245 | /// use syn::Ident; |
| 246 | /// use syn::token::{Paren, Bracket, Brace}; |
| 247 | /// use syn::synom::Synom; |
| 248 | /// |
| 249 | /// /// Parses a macro call with empty input. If the macro is written with |
| 250 | /// /// parentheses or brackets, a trailing semicolon is required. |
| 251 | /// /// |
| 252 | /// /// Example: `my_macro!{}` or `my_macro!();` or `my_macro![];` |
| 253 | /// struct EmptyMacroCall { |
| 254 | /// name: Ident, |
| 255 | /// bang_token: Token![!], |
| 256 | /// delimiter: MacroDelimiter, |
| 257 | /// semi_token: Option<Token![;]>, |
| 258 | /// } |
| 259 | /// |
| 260 | /// enum MacroDelimiter { |
| 261 | /// Paren(Paren), |
| 262 | /// Bracket(Bracket), |
| 263 | /// Brace(Brace), |
| 264 | /// } |
| 265 | /// |
| 266 | /// impl MacroDelimiter { |
| 267 | /// fn requires_semi(&self) -> bool { |
| 268 | /// match *self { |
| 269 | /// MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => true, |
| 270 | /// MacroDelimiter::Brace(_) => false, |
| 271 | /// } |
| 272 | /// } |
| 273 | /// } |
| 274 | /// |
| 275 | /// impl Synom for EmptyMacroCall { |
| 276 | /// named!(parse -> Self, do_parse!( |
| 277 | /// name: syn!(Ident) >> |
| 278 | /// bang: punct!(!) >> |
| 279 | /// empty_body: alt!( |
| 280 | /// parens!(epsilon!()) => { |d| MacroDelimiter::Paren(d.1) } |
| 281 | /// | |
| 282 | /// brackets!(epsilon!()) => { |d| MacroDelimiter::Bracket(d.1) } |
| 283 | /// | |
| 284 | /// braces!(epsilon!()) => { |d| MacroDelimiter::Brace(d.1) } |
| 285 | /// ) >> |
| 286 | /// semi: cond!(empty_body.requires_semi(), punct!(;)) >> |
| 287 | /// (EmptyMacroCall { |
| 288 | /// name: name, |
| 289 | /// bang_token: bang, |
| 290 | /// delimiter: empty_body, |
| 291 | /// semi_token: semi, |
| 292 | /// }) |
| 293 | /// )); |
| 294 | /// } |
| 295 | /// # |
| 296 | /// # fn main() {} |
| 297 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 298 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 299 | macro_rules! cond { |
| 300 | ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => { |
| 301 | if $cond { |
| 302 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 303 | ::std::result::Result::Ok((i, o)) => |
| 304 | ::std::result::Result::Ok((i, ::std::option::Option::Some(o))), |
| 305 | ::std::result::Result::Err(x) => ::std::result::Result::Err(x), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 306 | } |
| 307 | } else { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 308 | ::std::result::Result::Ok(($i, ::std::option::Option::None)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 309 | } |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 310 | }; |
| 311 | |
| 312 | ($i:expr, $cond:expr, $f:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 313 | cond!($i, $cond, call!($f)) |
David Tolnay | cfe5502 | 2016-10-02 22:02:27 -0700 | [diff] [blame] | 314 | }; |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 315 | } |
| 316 | |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 317 | /// Execute a parser only if a condition is met, otherwise fail to parse. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 318 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 319 | /// This is typically used inside of [`option!`] or [`alt!`]. |
| 320 | /// |
| 321 | /// [`option!`]: macro.option.html |
| 322 | /// [`alt!`]: macro.alt.html |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 323 | /// |
| 324 | /// - **Syntax:** `cond_reduce!(CONDITION, THING)` |
| 325 | /// - **Output:** `THING` |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 326 | /// |
| 327 | /// ```rust |
| 328 | /// #[macro_use] |
| 329 | /// extern crate syn; |
| 330 | /// |
| 331 | /// use syn::Type; |
| 332 | /// use syn::token::Paren; |
| 333 | /// use syn::delimited::Delimited; |
| 334 | /// use syn::synom::Synom; |
| 335 | /// |
| 336 | /// /// Parses a possibly variadic function signature. |
| 337 | /// /// |
| 338 | /// /// Example: `fn(A) or `fn(A, B, C, ...)` or `fn(...)` |
| 339 | /// /// Rejected: `fn(A, B...)` |
| 340 | /// struct VariadicFn { |
| 341 | /// fn_token: Token![fn], |
| 342 | /// paren_token: Paren, |
| 343 | /// types: Delimited<Type, Token![,]>, |
| 344 | /// variadic: Option<Token![...]>, |
| 345 | /// } |
| 346 | /// |
| 347 | /// // Example of using `cond_reduce!` inside of `option!`. |
| 348 | /// impl Synom for VariadicFn { |
| 349 | /// named!(parse -> Self, do_parse!( |
| 350 | /// fn_token: keyword!(fn) >> |
| 351 | /// params: parens!(do_parse!( |
| 352 | /// types: call!(Delimited::parse_terminated) >> |
| 353 | /// // Allow, but do not require, an ending `...` but only if the |
| 354 | /// // preceding list of types is empty or ends with a trailing comma. |
| 355 | /// variadic: option!(cond_reduce!(types.empty_or_trailing(), punct!(...))) >> |
| 356 | /// (types, variadic) |
| 357 | /// )) >> |
| 358 | /// ({ |
| 359 | /// let ((types, variadic), paren_token) = params; |
| 360 | /// VariadicFn { |
| 361 | /// fn_token, |
| 362 | /// paren_token, |
| 363 | /// types, |
| 364 | /// variadic, |
| 365 | /// } |
| 366 | /// }) |
| 367 | /// )); |
| 368 | /// } |
| 369 | /// # |
| 370 | /// # fn main() {} |
| 371 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 372 | #[macro_export] |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 373 | macro_rules! cond_reduce { |
| 374 | ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => { |
| 375 | if $cond { |
| 376 | $submac!($i, $($args)*) |
| 377 | } else { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 378 | $crate::parse_error() |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 379 | } |
| 380 | }; |
| 381 | |
| 382 | ($i:expr, $cond:expr, $f:expr) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 383 | cond_reduce!($i, $cond, call!($f)) |
David Tolnay | af2557e | 2016-10-24 11:52:21 -0700 | [diff] [blame] | 384 | }; |
| 385 | } |
| 386 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 387 | /// Parse zero or more values using the given parser. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 388 | /// |
| 389 | /// - **Syntax:** `many0!(THING)` |
| 390 | /// - **Output:** `Vec<THING>` |
| 391 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 392 | /// You may also be looking for: |
| 393 | /// |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 394 | /// - `call!(Delimited::parse_separated)` - zero or more values with separator |
| 395 | /// - `call!(Delimited::parse_separated_nonempty)` - one or more values |
| 396 | /// - `call!(Delimited::parse_terminated)` - zero or more, allows trailing separator |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 397 | /// - `call!(Delimited::parse_terminated_nonempty)` - one or more |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 398 | /// |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 399 | /// ```rust |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 400 | /// #[macro_use] |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 401 | /// extern crate syn; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 402 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 403 | /// use syn::{Ident, Item}; |
| 404 | /// use syn::token::Brace; |
| 405 | /// use syn::synom::Synom; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 406 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 407 | /// /// Parses a module containing zero or more Rust items. |
| 408 | /// /// |
| 409 | /// /// Example: `mod m { type Result<T> = ::std::result::Result<T, MyError>; }` |
| 410 | /// struct SimpleMod { |
| 411 | /// mod_token: Token![mod], |
| 412 | /// name: Ident, |
| 413 | /// brace_token: Brace, |
| 414 | /// items: Vec<Item>, |
| 415 | /// } |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 416 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 417 | /// impl Synom for SimpleMod { |
| 418 | /// named!(parse -> Self, do_parse!( |
| 419 | /// mod_token: keyword!(mod) >> |
| 420 | /// name: syn!(Ident) >> |
| 421 | /// body: braces!(many0!(syn!(Item))) >> |
| 422 | /// (SimpleMod { |
| 423 | /// mod_token: mod_token, |
| 424 | /// name: name, |
| 425 | /// brace_token: body.1, |
| 426 | /// items: body.0, |
| 427 | /// }) |
| 428 | /// )); |
| 429 | /// } |
| 430 | /// # |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 431 | /// # fn main() {} |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 432 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 433 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 434 | macro_rules! many0 { |
| 435 | ($i:expr, $submac:ident!( $($args:tt)* )) => {{ |
| 436 | let ret; |
| 437 | let mut res = ::std::vec::Vec::new(); |
| 438 | let mut input = $i; |
| 439 | |
| 440 | loop { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 441 | if input.eof() { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 442 | ret = ::std::result::Result::Ok((input, res)); |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 443 | break; |
| 444 | } |
| 445 | |
| 446 | match $submac!(input, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 447 | ::std::result::Result::Err(_) => { |
| 448 | ret = ::std::result::Result::Ok((input, res)); |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 449 | break; |
| 450 | } |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 451 | ::std::result::Result::Ok((i, o)) => { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 452 | // loop trip must always consume (otherwise infinite loops) |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 453 | if i == input { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 454 | ret = $crate::parse_error(); |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 455 | break; |
| 456 | } |
| 457 | |
| 458 | res.push(o); |
| 459 | input = i; |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | ret |
| 465 | }}; |
| 466 | |
| 467 | ($i:expr, $f:expr) => { |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 468 | $crate::parsers::many0($i, $f) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 469 | }; |
| 470 | } |
| 471 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 472 | // Improve compile time by compiling this loop only once per type it is used |
| 473 | // with. |
| 474 | // |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 475 | // Not public API. |
| 476 | #[doc(hidden)] |
David Tolnay | 1c03d8c | 2017-12-26 23:17:06 -0500 | [diff] [blame] | 477 | 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] | 478 | let mut res = Vec::new(); |
| 479 | |
| 480 | loop { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 481 | if input.eof() { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 482 | return Ok((input, res)); |
David Tolnay | bc84d5a | 2016-10-08 13:20:57 -0700 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | match f(input) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 486 | Err(_) => { |
| 487 | return Ok((input, res)); |
David Tolnay | bc84d5a | 2016-10-08 13:20:57 -0700 | [diff] [blame] | 488 | } |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 489 | Ok((i, o)) => { |
David Tolnay | bc84d5a | 2016-10-08 13:20:57 -0700 | [diff] [blame] | 490 | // loop trip must always consume (otherwise infinite loops) |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 491 | if i == input { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 492 | return parse_error(); |
David Tolnay | bc84d5a | 2016-10-08 13:20:57 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | res.push(o); |
| 496 | input = i; |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 502 | /// Pattern-match the result of a parser to select which other parser to run. |
| 503 | /// |
| 504 | /// - **Syntax:** `switch!(TARGET, PAT1 => THEN1 | PAT2 => THEN2 | ...)` |
| 505 | /// - **Output:** `T`, the return type of `THEN1` and `THEN2` and ... |
| 506 | /// |
| 507 | /// ```rust |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 508 | /// #[macro_use] |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 509 | /// extern crate syn; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 510 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 511 | /// use syn::Ident; |
| 512 | /// use syn::token::Brace; |
| 513 | /// use syn::synom::Synom; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 514 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 515 | /// /// Parse a unit struct or enum: either `struct S;` or `enum E { V }`. |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 516 | /// enum UnitType { |
| 517 | /// Struct { |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 518 | /// struct_token: Token![struct], |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 519 | /// name: Ident, |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 520 | /// semi_token: Token![;], |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 521 | /// }, |
| 522 | /// Enum { |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 523 | /// enum_token: Token![enum], |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 524 | /// name: Ident, |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 525 | /// brace_token: Brace, |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 526 | /// variant: Ident, |
| 527 | /// }, |
| 528 | /// } |
| 529 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 530 | /// enum StructOrEnum { |
| 531 | /// Struct(Token![struct]), |
| 532 | /// Enum(Token![enum]), |
| 533 | /// } |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 534 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 535 | /// impl Synom for StructOrEnum { |
| 536 | /// named!(parse -> Self, alt!( |
| 537 | /// keyword!(struct) => { StructOrEnum::Struct } |
| 538 | /// | |
| 539 | /// keyword!(enum) => { StructOrEnum::Enum } |
| 540 | /// )); |
| 541 | /// } |
| 542 | /// |
| 543 | /// impl Synom for UnitType { |
| 544 | /// named!(parse -> Self, do_parse!( |
| 545 | /// which: syn!(StructOrEnum) >> |
| 546 | /// name: syn!(Ident) >> |
| 547 | /// item: switch!(value!(which), |
| 548 | /// StructOrEnum::Struct(struct_token) => map!( |
| 549 | /// punct!(;), |
| 550 | /// |semi_token| UnitType::Struct { |
| 551 | /// struct_token: struct_token, |
| 552 | /// name: name, |
| 553 | /// semi_token: semi_token, |
| 554 | /// } |
| 555 | /// ) |
| 556 | /// | |
| 557 | /// StructOrEnum::Enum(enum_token) => map!( |
| 558 | /// braces!(syn!(Ident)), |
| 559 | /// |(variant, brace_token)| UnitType::Enum { |
| 560 | /// enum_token: enum_token, |
| 561 | /// name: name, |
| 562 | /// brace_token: brace_token, |
| 563 | /// variant: variant, |
| 564 | /// } |
| 565 | /// ) |
| 566 | /// ) >> |
| 567 | /// (item) |
| 568 | /// )); |
| 569 | /// } |
| 570 | /// # |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 571 | /// # fn main() {} |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 572 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 573 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 574 | macro_rules! switch { |
| 575 | ($i:expr, $submac:ident!( $($args:tt)* ), $($p:pat => $subrule:ident!( $($args2:tt)* ))|* ) => { |
| 576 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 577 | ::std::result::Result::Err(err) => ::std::result::Result::Err(err), |
| 578 | ::std::result::Result::Ok((i, o)) => match o { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 579 | $( |
| 580 | $p => $subrule!(i, $($args2)*), |
| 581 | )* |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | }; |
| 585 | } |
| 586 | |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 587 | /// Produce the given value without parsing anything. |
| 588 | /// |
| 589 | /// This can be needed where you have an existing parsed value but a parser |
| 590 | /// macro's syntax expects you to provide a submacro, such as in the first |
| 591 | /// argument of [`switch!`] or one of the branches of [`alt!`]. |
| 592 | /// |
| 593 | /// [`switch!`]: macro.switch.html |
| 594 | /// [`alt!`]: macro.alt.html |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 595 | /// |
| 596 | /// - **Syntax:** `value!(VALUE)` |
| 597 | /// - **Output:** `VALUE` |
| 598 | /// |
| 599 | /// ```rust |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 600 | /// #[macro_use] |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 601 | /// extern crate syn; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 602 | /// |
David Tolnay | 92a5651 | 2017-11-10 00:02:14 -0800 | [diff] [blame] | 603 | /// use syn::Ident; |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 604 | /// use syn::token::Brace; |
| 605 | /// use syn::synom::Synom; |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 606 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 607 | /// /// Parse a unit struct or enum: either `struct S;` or `enum E { V }`. |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 608 | /// enum UnitType { |
| 609 | /// Struct { |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 610 | /// struct_token: Token![struct], |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 611 | /// name: Ident, |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 612 | /// semi_token: Token![;], |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 613 | /// }, |
| 614 | /// Enum { |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 615 | /// enum_token: Token![enum], |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 616 | /// name: Ident, |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 617 | /// brace_token: Brace, |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 618 | /// variant: Ident, |
| 619 | /// }, |
| 620 | /// } |
| 621 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 622 | /// enum StructOrEnum { |
| 623 | /// Struct(Token![struct]), |
| 624 | /// Enum(Token![enum]), |
| 625 | /// } |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 626 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 627 | /// impl Synom for StructOrEnum { |
| 628 | /// named!(parse -> Self, alt!( |
| 629 | /// keyword!(struct) => { StructOrEnum::Struct } |
| 630 | /// | |
| 631 | /// keyword!(enum) => { StructOrEnum::Enum } |
| 632 | /// )); |
| 633 | /// } |
| 634 | /// |
| 635 | /// impl Synom for UnitType { |
| 636 | /// named!(parse -> Self, do_parse!( |
| 637 | /// which: syn!(StructOrEnum) >> |
| 638 | /// name: syn!(Ident) >> |
| 639 | /// item: switch!(value!(which), |
| 640 | /// StructOrEnum::Struct(struct_token) => map!( |
| 641 | /// punct!(;), |
| 642 | /// |semi_token| UnitType::Struct { |
| 643 | /// struct_token: struct_token, |
| 644 | /// name: name, |
| 645 | /// semi_token: semi_token, |
| 646 | /// } |
| 647 | /// ) |
| 648 | /// | |
| 649 | /// StructOrEnum::Enum(enum_token) => map!( |
| 650 | /// braces!(syn!(Ident)), |
| 651 | /// |(variant, brace_token)| UnitType::Enum { |
| 652 | /// enum_token: enum_token, |
| 653 | /// name: name, |
| 654 | /// brace_token: brace_token, |
| 655 | /// variant: variant, |
| 656 | /// } |
| 657 | /// ) |
| 658 | /// ) >> |
| 659 | /// (item) |
| 660 | /// )); |
| 661 | /// } |
| 662 | /// # |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 663 | /// # fn main() {} |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 664 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 665 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 666 | macro_rules! value { |
| 667 | ($i:expr, $res:expr) => { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 668 | ::std::result::Result::Ok(($i, $res)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 669 | }; |
| 670 | } |
| 671 | |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 672 | /// Unconditionally fail to parse anything. |
| 673 | /// |
| 674 | /// This may be useful in rejecting some arms of a `switch!` parser. |
David Tolnay | 92a5651 | 2017-11-10 00:02:14 -0800 | [diff] [blame] | 675 | /// |
| 676 | /// - **Syntax:** `reject!()` |
| 677 | /// - **Output:** never succeeds |
| 678 | /// |
| 679 | /// ```rust |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 680 | /// #[macro_use] |
David Tolnay | 92a5651 | 2017-11-10 00:02:14 -0800 | [diff] [blame] | 681 | /// extern crate syn; |
David Tolnay | 92a5651 | 2017-11-10 00:02:14 -0800 | [diff] [blame] | 682 | /// |
| 683 | /// use syn::Item; |
| 684 | /// |
| 685 | /// // Parse any item, except for a module. |
| 686 | /// named!(almost_any_item -> Item, |
| 687 | /// switch!(syn!(Item), |
| 688 | /// Item::Mod(_) => reject!() |
| 689 | /// | |
| 690 | /// ok => value!(ok) |
| 691 | /// ) |
| 692 | /// ); |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 693 | /// # |
David Tolnay | 92a5651 | 2017-11-10 00:02:14 -0800 | [diff] [blame] | 694 | /// # fn main() {} |
| 695 | /// ``` |
| 696 | #[macro_export] |
| 697 | macro_rules! reject { |
David Tolnay | 2bd1742 | 2017-12-25 18:44:20 -0500 | [diff] [blame] | 698 | ($i:expr,) => {{ |
| 699 | let _ = $i; |
David Tolnay | 92a5651 | 2017-11-10 00:02:14 -0800 | [diff] [blame] | 700 | $crate::parse_error() |
David Tolnay | 2bd1742 | 2017-12-25 18:44:20 -0500 | [diff] [blame] | 701 | }} |
David Tolnay | 92a5651 | 2017-11-10 00:02:14 -0800 | [diff] [blame] | 702 | } |
| 703 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 704 | /// 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] | 705 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 706 | /// - **Syntax:** `tuple!(A, B, C, ...)` |
| 707 | /// - **Output:** `(A, B, C, ...)` |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 708 | /// |
| 709 | /// ```rust |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 710 | /// #[macro_use] |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 711 | /// extern crate syn; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 712 | /// |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 713 | /// use syn::Type; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 714 | /// |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 715 | /// named!(two_types -> (Type, Type), tuple!(syn!(Type), syn!(Type))); |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 716 | /// # |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 717 | /// # fn main() {} |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 718 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 719 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 720 | macro_rules! tuple { |
| 721 | ($i:expr, $($rest:tt)*) => { |
| 722 | tuple_parser!($i, (), $($rest)*) |
| 723 | }; |
| 724 | } |
| 725 | |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 726 | // Internal parser, do not use directly. |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 727 | #[doc(hidden)] |
| 728 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 729 | macro_rules! tuple_parser { |
| 730 | ($i:expr, ($($parsed:tt),*), $e:ident, $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 731 | tuple_parser!($i, ($($parsed),*), call!($e), $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 732 | }; |
| 733 | |
| 734 | ($i:expr, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => { |
| 735 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 736 | ::std::result::Result::Err(err) => |
| 737 | ::std::result::Result::Err(err), |
| 738 | ::std::result::Result::Ok((i, o)) => |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 739 | tuple_parser!(i, (o), $($rest)*), |
| 740 | } |
| 741 | }; |
| 742 | |
| 743 | ($i:expr, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => { |
| 744 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 745 | ::std::result::Result::Err(err) => |
| 746 | ::std::result::Result::Err(err), |
| 747 | ::std::result::Result::Ok((i, o)) => |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 748 | tuple_parser!(i, ($($parsed)* , o), $($rest)*), |
| 749 | } |
| 750 | }; |
| 751 | |
| 752 | ($i:expr, ($($parsed:tt),*), $e:ident) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 753 | tuple_parser!($i, ($($parsed),*), call!($e)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 754 | }; |
| 755 | |
| 756 | ($i:expr, (), $submac:ident!( $($args:tt)* )) => { |
| 757 | $submac!($i, $($args)*) |
| 758 | }; |
| 759 | |
| 760 | ($i:expr, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => { |
| 761 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 762 | ::std::result::Result::Err(err) => |
| 763 | ::std::result::Result::Err(err), |
| 764 | ::std::result::Result::Ok((i, o)) => |
| 765 | ::std::result::Result::Ok((i, ($($parsed),*, o))), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 766 | } |
| 767 | }; |
| 768 | |
| 769 | ($i:expr, ($($parsed:expr),*)) => { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 770 | ::std::result::Result::Ok(($i, ($($parsed),*))) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 771 | }; |
| 772 | } |
| 773 | |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 774 | /// Run a series of parsers, returning the result of the first one which |
| 775 | /// succeeds. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 776 | /// |
| 777 | /// Optionally allows for the result to be transformed. |
| 778 | /// |
| 779 | /// - **Syntax:** `alt!(THING1 | THING2 => { FUNC } | ...)` |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 780 | /// - **Output:** `T`, the return type of `THING1` and `FUNC(THING2)` and ... |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 781 | /// |
| 782 | /// ```rust |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 783 | /// #[macro_use] |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 784 | /// extern crate syn; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 785 | /// |
| 786 | /// use syn::Ident; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 787 | /// |
| 788 | /// named!(ident_or_bang -> Ident, |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 789 | /// alt!( |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 790 | /// syn!(Ident) |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 791 | /// | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 792 | /// punct!(!) => { |_| "BANG".into() } |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 793 | /// ) |
| 794 | /// ); |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 795 | /// # |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 796 | /// # fn main() {} |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 797 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 798 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 799 | macro_rules! alt { |
| 800 | ($i:expr, $e:ident | $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 801 | alt!($i, call!($e) | $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 802 | }; |
| 803 | |
| 804 | ($i:expr, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => { |
| 805 | match $subrule!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 806 | res @ ::std::result::Result::Ok(_) => res, |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 807 | _ => alt!($i, $($rest)*) |
| 808 | } |
| 809 | }; |
| 810 | |
| 811 | ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => { |
| 812 | match $subrule!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 813 | ::std::result::Result::Ok((i, o)) => |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 814 | ::std::result::Result::Ok((i, $crate::parsers::invoke($gen, o))), |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 815 | ::std::result::Result::Err(_) => alt!($i, $($rest)*), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 816 | } |
| 817 | }; |
| 818 | |
| 819 | ($i:expr, $e:ident => { $gen:expr } | $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 820 | alt!($i, call!($e) => { $gen } | $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 821 | }; |
| 822 | |
| 823 | ($i:expr, $e:ident => { $gen:expr }) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 824 | alt!($i, call!($e) => { $gen }) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 825 | }; |
| 826 | |
| 827 | ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => { |
| 828 | match $subrule!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 829 | ::std::result::Result::Ok((i, o)) => |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 830 | ::std::result::Result::Ok((i, $crate::parsers::invoke($gen, o))), |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 831 | ::std::result::Result::Err(err) => |
| 832 | ::std::result::Result::Err(err), |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 833 | } |
| 834 | }; |
| 835 | |
| 836 | ($i:expr, $e:ident) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 837 | alt!($i, call!($e)) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 838 | }; |
| 839 | |
| 840 | ($i:expr, $subrule:ident!( $($args:tt)*)) => { |
David Tolnay | 5377b17 | 2016-10-25 01:13:12 -0700 | [diff] [blame] | 841 | $subrule!($i, $($args)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 842 | }; |
| 843 | } |
| 844 | |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 845 | /// Run a series of parsers, optionally naming each intermediate result, |
| 846 | /// followed by a step to combine the intermediate results. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 847 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 848 | /// Produces the result of evaluating the final expression in parentheses with |
| 849 | /// all of the previously named results bound. |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 850 | /// |
David Tolnay | 1f16b60 | 2017-02-07 20:06:55 -0500 | [diff] [blame] | 851 | /// - **Syntax:** `do_parse!(name: THING1 >> THING2 >> (RESULT))` |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 852 | /// - **Output:** `RESULT` |
| 853 | /// |
| 854 | /// ```rust |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 855 | /// #[macro_use] |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 856 | /// extern crate syn; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 857 | /// extern crate proc_macro2; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 858 | /// |
David Tolnay | 9c76bcb | 2017-12-26 23:14:59 -0500 | [diff] [blame] | 859 | /// use syn::Ident; |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 860 | /// use syn::token::Paren; |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 861 | /// use syn::synom::Synom; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 862 | /// use proc_macro2::TokenStream; |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 863 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 864 | /// /// Parse a macro invocation that uses `(` `)` parentheses. |
| 865 | /// /// |
| 866 | /// /// Example: `stringify!($args)`. |
| 867 | /// struct Macro { |
| 868 | /// name: Ident, |
| 869 | /// bang_token: Token![!], |
| 870 | /// paren_token: Paren, |
| 871 | /// tts: TokenStream, |
| 872 | /// } |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 873 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 874 | /// impl Synom for Macro { |
| 875 | /// named!(parse -> Self, do_parse!( |
| 876 | /// name: syn!(Ident) >> |
| 877 | /// bang: punct!(!) >> |
| 878 | /// body: parens!(syn!(TokenStream)) >> |
| 879 | /// (Macro { |
| 880 | /// name: name, |
| 881 | /// bang_token: bang, |
| 882 | /// paren_token: body.1, |
| 883 | /// tts: body.0, |
| 884 | /// }) |
| 885 | /// )); |
| 886 | /// } |
| 887 | /// # |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 888 | /// # fn main() {} |
Michael Layzell | 24645a3 | 2017-02-04 13:19:26 -0500 | [diff] [blame] | 889 | /// ``` |
Michael Layzell | 5bde96f | 2017-01-24 17:59:21 -0500 | [diff] [blame] | 890 | #[macro_export] |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 891 | macro_rules! do_parse { |
| 892 | ($i:expr, ( $($rest:expr),* )) => { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 893 | ::std::result::Result::Ok(($i, ( $($rest),* ))) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 894 | }; |
| 895 | |
| 896 | ($i:expr, $e:ident >> $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 897 | do_parse!($i, call!($e) >> $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 898 | }; |
| 899 | |
| 900 | ($i:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => { |
| 901 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 902 | ::std::result::Result::Err(err) => |
| 903 | ::std::result::Result::Err(err), |
| 904 | ::std::result::Result::Ok((i, _)) => |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 905 | do_parse!(i, $($rest)*), |
| 906 | } |
| 907 | }; |
| 908 | |
| 909 | ($i:expr, $field:ident : $e:ident >> $($rest:tt)*) => { |
David Tolnay | b81c7a4 | 2016-10-25 10:12:12 -0700 | [diff] [blame] | 910 | do_parse!($i, $field: call!($e) >> $($rest)*) |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 911 | }; |
| 912 | |
| 913 | ($i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => { |
| 914 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 915 | ::std::result::Result::Err(err) => |
| 916 | ::std::result::Result::Err(err), |
| 917 | ::std::result::Result::Ok((i, o)) => { |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 918 | let $field = o; |
| 919 | do_parse!(i, $($rest)*) |
| 920 | }, |
| 921 | } |
| 922 | }; |
| 923 | |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 924 | ($i:expr, mut $field:ident : $e:ident >> $($rest:tt)*) => { |
David Tolnay | 7184b13 | 2016-10-30 10:06:37 -0700 | [diff] [blame] | 925 | do_parse!($i, mut $field: call!($e) >> $($rest)*) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 926 | }; |
| 927 | |
| 928 | ($i:expr, mut $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => { |
| 929 | match $submac!($i, $($args)*) { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 930 | ::std::result::Result::Err(err) => |
| 931 | ::std::result::Result::Err(err), |
| 932 | ::std::result::Result::Ok((i, o)) => { |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 933 | let mut $field = o; |
| 934 | do_parse!(i, $($rest)*) |
| 935 | }, |
| 936 | } |
| 937 | }; |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 938 | } |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 939 | |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 940 | /// Parse nothing and succeed only if the end of the enclosing block has been |
| 941 | /// reached. |
| 942 | /// |
| 943 | /// The enclosing block may be the full input if we are parsing at the top |
| 944 | /// level, or the surrounding parenthesis/bracket/brace if we are parsing within |
| 945 | /// those. |
| 946 | /// |
| 947 | /// - **Syntax:** `input_end!()` |
| 948 | /// - **Output:** `()` |
| 949 | /// |
| 950 | /// ```rust |
| 951 | /// #[macro_use] |
| 952 | /// extern crate syn; |
| 953 | /// |
| 954 | /// use syn::Expr; |
| 955 | /// use syn::synom::Synom; |
| 956 | /// |
| 957 | /// /// Parses any Rust expression followed either by a semicolon or by the end |
| 958 | /// /// of the input. |
| 959 | /// /// |
| 960 | /// /// For example `many0!(syn!(TerminatedExpr))` would successfully parse the |
| 961 | /// /// following input into three expressions. |
| 962 | /// /// |
| 963 | /// /// 1 + 1; second.two(); third!() |
| 964 | /// /// |
| 965 | /// /// Similarly within a block, `braced!(many0!(syn!(TerminatedExpr)))` would |
| 966 | /// /// successfully parse three expressions. |
| 967 | /// /// |
| 968 | /// /// { 1 + 1; second.two(); third!() } |
| 969 | /// struct TerminatedExpr { |
| 970 | /// expr: Expr, |
| 971 | /// semi_token: Option<Token![;]>, |
| 972 | /// } |
| 973 | /// |
| 974 | /// impl Synom for TerminatedExpr { |
| 975 | /// named!(parse -> Self, do_parse!( |
| 976 | /// expr: syn!(Expr) >> |
| 977 | /// semi: alt!( |
| 978 | /// input_end!() => { |_| None } |
| 979 | /// | |
| 980 | /// punct!(;) => { Some } |
| 981 | /// ) >> |
| 982 | /// (TerminatedExpr { |
| 983 | /// expr: expr, |
| 984 | /// semi_token: semi, |
| 985 | /// }) |
| 986 | /// )); |
| 987 | /// } |
| 988 | /// # |
| 989 | /// # fn main() {} |
| 990 | /// ``` |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 991 | #[macro_export] |
| 992 | macro_rules! input_end { |
| 993 | ($i:expr,) => { |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 994 | $crate::parsers::input_end($i) |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 995 | }; |
| 996 | } |
| 997 | |
| 998 | // Not a public API |
| 999 | #[doc(hidden)] |
David Tolnay | 1fc4e49 | 2017-11-11 22:17:22 -0800 | [diff] [blame] | 1000 | pub fn input_end(input: Cursor) -> PResult<'static, ()> { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 1001 | if input.eof() { |
David Tolnay | 1fc4e49 | 2017-11-11 22:17:22 -0800 | [diff] [blame] | 1002 | Ok((Cursor::empty(), ())) |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 1003 | } else { |
Michael Layzell | 760fd66 | 2017-05-31 22:46:05 -0400 | [diff] [blame] | 1004 | parse_error() |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 1005 | } |
| 1006 | } |
David Tolnay | f03cdb8 | 2017-12-30 00:05:58 -0500 | [diff] [blame] | 1007 | |
| 1008 | /// Turn a failed parse into `None` and a successful parse into `Some`. |
| 1009 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 1010 | /// A failed parse consumes none of the input. |
| 1011 | /// |
David Tolnay | f03cdb8 | 2017-12-30 00:05:58 -0500 | [diff] [blame] | 1012 | /// - **Syntax:** `option!(THING)` |
| 1013 | /// - **Output:** `Option<THING>` |
| 1014 | /// |
| 1015 | /// ```rust |
| 1016 | /// #[macro_use] |
| 1017 | /// extern crate syn; |
| 1018 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 1019 | /// use syn::{Label, Block}; |
| 1020 | /// use syn::synom::Synom; |
David Tolnay | f03cdb8 | 2017-12-30 00:05:58 -0500 | [diff] [blame] | 1021 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 1022 | /// /// Parses a Rust loop. Equivalent to syn::ExprLoop. |
| 1023 | /// /// |
| 1024 | /// /// Examples: |
| 1025 | /// /// loop { println!("y"); } |
| 1026 | /// /// 'x: loop { break 'x; } |
| 1027 | /// struct ExprLoop { |
| 1028 | /// label: Option<Label>, |
| 1029 | /// loop_token: Token![loop], |
| 1030 | /// body: Block, |
| 1031 | /// } |
David Tolnay | f03cdb8 | 2017-12-30 00:05:58 -0500 | [diff] [blame] | 1032 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 1033 | /// impl Synom for ExprLoop { |
| 1034 | /// named!(parse -> Self, do_parse!( |
| 1035 | /// // Loop may or may not have a label. |
| 1036 | /// label: option!(syn!(Label)) >> |
| 1037 | /// loop_: keyword!(loop) >> |
| 1038 | /// block: syn!(Block) >> |
| 1039 | /// (ExprLoop { |
| 1040 | /// label: label, |
| 1041 | /// loop_token: loop_, |
| 1042 | /// body: block, |
| 1043 | /// }) |
| 1044 | /// )); |
| 1045 | /// } |
| 1046 | /// # |
David Tolnay | f03cdb8 | 2017-12-30 00:05:58 -0500 | [diff] [blame] | 1047 | /// # fn main() {} |
| 1048 | /// ``` |
| 1049 | #[macro_export] |
| 1050 | macro_rules! option { |
| 1051 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
| 1052 | match $submac!($i, $($args)*) { |
| 1053 | ::std::result::Result::Ok((i, o)) => |
| 1054 | ::std::result::Result::Ok((i, Some(o))), |
| 1055 | ::std::result::Result::Err(_) => |
| 1056 | ::std::result::Result::Ok(($i, None)), |
| 1057 | } |
| 1058 | }; |
| 1059 | |
| 1060 | ($i:expr, $f:expr) => { |
| 1061 | option!($i, call!($f)); |
| 1062 | }; |
| 1063 | } |
| 1064 | |
David Tolnay | f03cdb8 | 2017-12-30 00:05:58 -0500 | [diff] [blame] | 1065 | /// Parses nothing and always succeeds. |
| 1066 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 1067 | /// This can be useful as a fallthrough case in [`alt!`], as shown below. Also |
| 1068 | /// useful for parsing empty delimiters using [`parens!`] or [`brackets!`] or |
| 1069 | /// [`braces!`] by parsing for example `braces!(epsilon!())` for an empty `{}`. |
| 1070 | /// |
| 1071 | /// [`alt!`]: macro.alt.html |
| 1072 | /// [`parens!`]: macro.parens.html |
| 1073 | /// [`brackets!`]: macro.brackets.html |
| 1074 | /// [`braces!`]: macro.braces.html |
David Tolnay | f03cdb8 | 2017-12-30 00:05:58 -0500 | [diff] [blame] | 1075 | /// |
| 1076 | /// - **Syntax:** `epsilon!()` |
| 1077 | /// - **Output:** `()` |
| 1078 | /// |
| 1079 | /// ```rust |
| 1080 | /// #[macro_use] |
| 1081 | /// extern crate syn; |
| 1082 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 1083 | /// use syn::synom::Synom; |
| 1084 | /// |
David Tolnay | f03cdb8 | 2017-12-30 00:05:58 -0500 | [diff] [blame] | 1085 | /// enum Mutability { |
| 1086 | /// Mutable(Token![mut]), |
| 1087 | /// Immutable, |
| 1088 | /// } |
| 1089 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 1090 | /// impl Synom for Mutability { |
| 1091 | /// named!(parse -> Self, alt!( |
| 1092 | /// keyword!(mut) => { Mutability::Mutable } |
| 1093 | /// | |
| 1094 | /// epsilon!() => { |_| Mutability::Immutable } |
| 1095 | /// )); |
| 1096 | /// } |
| 1097 | /// # |
David Tolnay | f03cdb8 | 2017-12-30 00:05:58 -0500 | [diff] [blame] | 1098 | /// # fn main() {} |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 1099 | /// ``` |
David Tolnay | f03cdb8 | 2017-12-30 00:05:58 -0500 | [diff] [blame] | 1100 | #[macro_export] |
| 1101 | macro_rules! epsilon { |
| 1102 | ($i:expr,) => { |
| 1103 | ::std::result::Result::Ok(($i, ())) |
| 1104 | }; |
| 1105 | } |
| 1106 | |
| 1107 | /// Run a parser, binding the result to a name, and then evaluating an |
| 1108 | /// expression. |
| 1109 | /// |
| 1110 | /// Discards the result of the expression and parser. |
| 1111 | /// |
| 1112 | /// - **Syntax:** `tap!(NAME : THING => EXPR)` |
| 1113 | /// - **Output:** `()` |
David Tolnay | f03cdb8 | 2017-12-30 00:05:58 -0500 | [diff] [blame] | 1114 | #[doc(hidden)] |
| 1115 | #[macro_export] |
| 1116 | macro_rules! tap { |
| 1117 | ($i:expr, $name:ident : $submac:ident!( $($args:tt)* ) => $e:expr) => { |
| 1118 | match $submac!($i, $($args)*) { |
| 1119 | ::std::result::Result::Ok((i, o)) => { |
| 1120 | let $name = o; |
| 1121 | $e; |
| 1122 | ::std::result::Result::Ok((i, ())) |
| 1123 | } |
| 1124 | ::std::result::Result::Err(err) => |
| 1125 | ::std::result::Result::Err(err), |
| 1126 | } |
| 1127 | }; |
| 1128 | |
| 1129 | ($i:expr, $name:ident : $f:expr => $e:expr) => { |
| 1130 | tap!($i, $name: call!($f) => $e); |
| 1131 | }; |
| 1132 | } |
| 1133 | |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 1134 | /// Parse any type that implements the `Synom` trait. |
David Tolnay | f03cdb8 | 2017-12-30 00:05:58 -0500 | [diff] [blame] | 1135 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 1136 | /// Any type implementing [`Synom`] can be used with this parser, whether the |
| 1137 | /// implementation is provided by Syn or is one that you write. |
David Tolnay | f03cdb8 | 2017-12-30 00:05:58 -0500 | [diff] [blame] | 1138 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 1139 | /// [`Synom`]: synom/trait.Synom.html |
| 1140 | /// |
David Tolnay | f03cdb8 | 2017-12-30 00:05:58 -0500 | [diff] [blame] | 1141 | /// - **Syntax:** `syn!(TYPE)` |
| 1142 | /// - **Output:** `TYPE` |
| 1143 | /// |
| 1144 | /// ```rust |
| 1145 | /// #[macro_use] |
| 1146 | /// extern crate syn; |
| 1147 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 1148 | /// use syn::{Ident, Item}; |
| 1149 | /// use syn::token::Brace; |
| 1150 | /// use syn::synom::Synom; |
David Tolnay | f03cdb8 | 2017-12-30 00:05:58 -0500 | [diff] [blame] | 1151 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 1152 | /// /// Parses a module containing zero or more Rust items. |
| 1153 | /// /// |
| 1154 | /// /// Example: `mod m { type Result<T> = ::std::result::Result<T, MyError>; }` |
| 1155 | /// struct SimpleMod { |
| 1156 | /// mod_token: Token![mod], |
| 1157 | /// name: Ident, |
| 1158 | /// brace_token: Brace, |
| 1159 | /// items: Vec<Item>, |
| 1160 | /// } |
David Tolnay | f03cdb8 | 2017-12-30 00:05:58 -0500 | [diff] [blame] | 1161 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 1162 | /// impl Synom for SimpleMod { |
| 1163 | /// named!(parse -> Self, do_parse!( |
| 1164 | /// mod_token: keyword!(mod) >> |
| 1165 | /// name: syn!(Ident) >> |
| 1166 | /// body: braces!(many0!(syn!(Item))) >> |
| 1167 | /// (SimpleMod { |
| 1168 | /// mod_token: mod_token, |
| 1169 | /// name: name, |
| 1170 | /// brace_token: body.1, |
| 1171 | /// items: body.0, |
| 1172 | /// }) |
| 1173 | /// )); |
| 1174 | /// } |
| 1175 | /// # |
David Tolnay | f03cdb8 | 2017-12-30 00:05:58 -0500 | [diff] [blame] | 1176 | /// # fn main() {} |
| 1177 | /// ``` |
| 1178 | #[macro_export] |
| 1179 | macro_rules! syn { |
| 1180 | ($i:expr, $t:ty) => { |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 1181 | <$t as $crate::synom::Synom>::parse($i) |
David Tolnay | f03cdb8 | 2017-12-30 00:05:58 -0500 | [diff] [blame] | 1182 | }; |
| 1183 | } |
| 1184 | |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 1185 | /// Parse inside of `(` `)` parentheses. |
David Tolnay | f03cdb8 | 2017-12-30 00:05:58 -0500 | [diff] [blame] | 1186 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 1187 | /// This macro parses a set of balanced parentheses and invokes a sub-parser on |
| 1188 | /// the content inside. The sub-parser is required to consume all tokens within |
| 1189 | /// the parentheses in order for this parser to return successfully. |
David Tolnay | f03cdb8 | 2017-12-30 00:05:58 -0500 | [diff] [blame] | 1190 | /// |
| 1191 | /// - **Syntax:** `parens!(SUBPARSER)` |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 1192 | /// - **Output:** `(SUBPARSER, token::Paren)` |
David Tolnay | f03cdb8 | 2017-12-30 00:05:58 -0500 | [diff] [blame] | 1193 | /// |
| 1194 | /// ```rust |
| 1195 | /// #[macro_use] |
| 1196 | /// extern crate syn; |
| 1197 | /// |
| 1198 | /// use syn::Expr; |
| 1199 | /// use syn::token::Paren; |
| 1200 | /// |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 1201 | /// /// Parses an expression inside of parentheses. |
| 1202 | /// /// |
| 1203 | /// /// Example: `(1 + 1)` |
David Tolnay | f03cdb8 | 2017-12-30 00:05:58 -0500 | [diff] [blame] | 1204 | /// named!(expr_paren -> (Expr, Paren), parens!(syn!(Expr))); |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 1205 | /// # |
David Tolnay | f03cdb8 | 2017-12-30 00:05:58 -0500 | [diff] [blame] | 1206 | /// # fn main() {} |
| 1207 | /// ``` |
| 1208 | #[macro_export] |
| 1209 | macro_rules! parens { |
| 1210 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
| 1211 | $crate::token::Paren::parse($i, |i| $submac!(i, $($args)*)) |
| 1212 | }; |
| 1213 | |
| 1214 | ($i:expr, $f:expr) => { |
| 1215 | parens!($i, call!($f)); |
| 1216 | }; |
| 1217 | } |
| 1218 | |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 1219 | /// Parse inside of `[` `]` square brackets. |
| 1220 | /// |
| 1221 | /// This macro parses a set of balanced brackets and invokes a sub-parser on the |
| 1222 | /// content inside. The sub-parser is required to consume all tokens within the |
| 1223 | /// brackets in order for this parser to return successfully. |
| 1224 | /// |
| 1225 | /// - **Syntax:** `brackets!(SUBPARSER)` |
| 1226 | /// - **Output:** `(SUBPARSER, token::Bracket)` |
| 1227 | /// |
| 1228 | /// ```rust |
| 1229 | /// #[macro_use] |
| 1230 | /// extern crate syn; |
| 1231 | /// |
| 1232 | /// use syn::Expr; |
| 1233 | /// use syn::token::Bracket; |
| 1234 | /// |
| 1235 | /// /// Parses an expression inside of brackets. |
| 1236 | /// /// |
| 1237 | /// /// Example: `[1 + 1]` |
| 1238 | /// named!(expr_paren -> (Expr, Bracket), brackets!(syn!(Expr))); |
| 1239 | /// # |
| 1240 | /// # fn main() {} |
| 1241 | /// ``` |
David Tolnay | f03cdb8 | 2017-12-30 00:05:58 -0500 | [diff] [blame] | 1242 | #[macro_export] |
| 1243 | macro_rules! brackets { |
| 1244 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
| 1245 | $crate::token::Bracket::parse($i, |i| $submac!(i, $($args)*)) |
| 1246 | }; |
| 1247 | |
| 1248 | ($i:expr, $f:expr) => { |
| 1249 | brackets!($i, call!($f)); |
| 1250 | }; |
| 1251 | } |
| 1252 | |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 1253 | /// Parse inside of `{` `}` curly braces. |
| 1254 | /// |
| 1255 | /// This macro parses a set of balanced braces and invokes a sub-parser on the |
| 1256 | /// content inside. The sub-parser is required to consume all tokens within the |
| 1257 | /// braces in order for this parser to return successfully. |
| 1258 | /// |
| 1259 | /// - **Syntax:** `braces!(SUBPARSER)` |
| 1260 | /// - **Output:** `(SUBPARSER, token::Brace)` |
| 1261 | /// |
| 1262 | /// ```rust |
| 1263 | /// #[macro_use] |
| 1264 | /// extern crate syn; |
| 1265 | /// |
| 1266 | /// use syn::Expr; |
| 1267 | /// use syn::token::Brace; |
| 1268 | /// |
| 1269 | /// /// Parses an expression inside of braces. |
| 1270 | /// /// |
| 1271 | /// /// Example: `{1 + 1}` |
| 1272 | /// named!(expr_paren -> (Expr, Brace), braces!(syn!(Expr))); |
| 1273 | /// # |
| 1274 | /// # fn main() {} |
| 1275 | /// ``` |
David Tolnay | f03cdb8 | 2017-12-30 00:05:58 -0500 | [diff] [blame] | 1276 | #[macro_export] |
| 1277 | macro_rules! braces { |
| 1278 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
| 1279 | $crate::token::Brace::parse($i, |i| $submac!(i, $($args)*)) |
| 1280 | }; |
| 1281 | |
| 1282 | ($i:expr, $f:expr) => { |
| 1283 | braces!($i, call!($f)); |
| 1284 | }; |
| 1285 | } |
| 1286 | |
David Tolnay | dc03aec | 2017-12-30 01:54:18 -0500 | [diff] [blame^] | 1287 | // Not public API. |
| 1288 | #[doc(hidden)] |
David Tolnay | f03cdb8 | 2017-12-30 00:05:58 -0500 | [diff] [blame] | 1289 | #[macro_export] |
| 1290 | macro_rules! grouped { |
| 1291 | ($i:expr, $submac:ident!( $($args:tt)* )) => { |
| 1292 | $crate::token::Group::parse($i, |i| $submac!(i, $($args)*)) |
| 1293 | }; |
| 1294 | |
| 1295 | ($i:expr, $f:expr) => { |
| 1296 | grouped!($i, call!($f)); |
| 1297 | }; |
| 1298 | } |