blob: 47385025cc832294f7e8ea55d17e81ba105c37b1 [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// Copyright 2018 Syn Developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
David Tolnaydfc886b2018-01-06 08:03:09 -08009use buffer::Cursor;
David Tolnay203557a2017-12-27 23:59:33 -050010use parse_error;
11use synom::PResult;
Alex Crichton7b9e02f2017-05-30 15:54:33 -070012
David Tolnaydc03aec2017-12-30 01:54:18 -050013/// Define a parser function with the signature expected by syn parser
14/// combinators.
15///
16/// The function may be the `parse` function of the [`Synom`] trait, or it may
17/// be a free-standing function with an arbitrary name. When implementing the
18/// `Synom` trait, the function name is `parse` and the return type is `Self`.
19///
20/// [`Synom`]: synom/trait.Synom.html
Michael Layzell24645a32017-02-04 13:19:26 -050021///
David Tolnay1f16b602017-02-07 20:06:55 -050022/// - **Syntax:** `named!(NAME -> TYPE, PARSER)` or `named!(pub NAME -> TYPE, PARSER)`
23///
24/// ```rust
David Tolnaydc03aec2017-12-30 01:54:18 -050025/// #[macro_use]
26/// extern crate syn;
27///
28/// use syn::Type;
David Tolnayf2cfd722017-12-31 18:02:51 -050029/// use syn::punctuated::Punctuated;
David Tolnaydc03aec2017-12-30 01:54:18 -050030/// use syn::synom::Synom;
31///
32/// /// Parses one or more Rust types separated by commas.
33/// ///
34/// /// Example: `String, Vec<T>, [u8; LEN + 1]`
David Tolnayf2cfd722017-12-31 18:02:51 -050035/// named!(pub comma_separated_types -> Punctuated<Type, Token![,]>,
36/// call!(Punctuated::parse_separated_nonempty)
David Tolnay1f16b602017-02-07 20:06:55 -050037/// );
David Tolnaydc03aec2017-12-30 01:54:18 -050038///
39/// /// The same function as a `Synom` implementation.
40/// struct CommaSeparatedTypes {
David Tolnayf2cfd722017-12-31 18:02:51 -050041/// types: Punctuated<Type, Token![,]>,
David Tolnaydc03aec2017-12-30 01:54:18 -050042/// }
43///
44/// impl Synom for CommaSeparatedTypes {
45/// /// As the default behavior, we want there to be at least 1 type.
46/// named!(parse -> Self, do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -050047/// types: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayddc5dfa2017-12-31 15:35:07 -050048/// (CommaSeparatedTypes { types })
David Tolnaydc03aec2017-12-30 01:54:18 -050049/// ));
50/// }
51///
52/// impl CommaSeparatedTypes {
53/// /// A separate parser that the user can invoke explicitly which allows
54/// /// for parsing 0 or more types, rather than the default 1 or more.
55/// named!(pub parse0 -> Self, do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -050056/// types: call!(Punctuated::parse_separated) >>
David Tolnayddc5dfa2017-12-31 15:35:07 -050057/// (CommaSeparatedTypes { types })
David Tolnaydc03aec2017-12-30 01:54:18 -050058/// ));
59/// }
60/// #
David Tolnay1f16b602017-02-07 20:06:55 -050061/// # fn main() {}
62/// ```
David Tolnay461d98e2018-01-07 11:07:19 -080063///
64/// *This macro is available if Syn is built with the `"parsing"` feature.*
Michael Layzell5bde96f2017-01-24 17:59:21 -050065#[macro_export]
David Tolnayb5a7b142016-09-13 22:46:39 -070066macro_rules! named {
67 ($name:ident -> $o:ty, $submac:ident!( $($args:tt)* )) => {
David Tolnaydfc886b2018-01-06 08:03:09 -080068 fn $name(i: $crate::buffer::Cursor) -> $crate::synom::PResult<$o> {
David Tolnayb5a7b142016-09-13 22:46:39 -070069 $submac!(i, $($args)*)
70 }
71 };
72
73 (pub $name:ident -> $o:ty, $submac:ident!( $($args:tt)* )) => {
David Tolnaydfc886b2018-01-06 08:03:09 -080074 pub fn $name(i: $crate::buffer::Cursor) -> $crate::synom::PResult<$o> {
David Tolnayb5a7b142016-09-13 22:46:39 -070075 $submac!(i, $($args)*)
76 }
77 };
Michael Layzellf8334e32017-06-04 19:01:08 -040078
79 // These two variants are for defining named parsers which have custom
80 // arguments, and are called with `call!()`
81 ($name:ident($($params:tt)*) -> $o:ty, $submac:ident!( $($args:tt)* )) => {
David Tolnaydfc886b2018-01-06 08:03:09 -080082 fn $name(i: $crate::buffer::Cursor, $($params)*) -> $crate::synom::PResult<$o> {
Michael Layzellf8334e32017-06-04 19:01:08 -040083 $submac!(i, $($args)*)
84 }
85 };
86
87 (pub $name:ident($($params:tt)*) -> $o:ty, $submac:ident!( $($args:tt)* )) => {
David Tolnaydfc886b2018-01-06 08:03:09 -080088 pub fn $name(i: $crate::buffer::Cursor, $($params)*) -> $crate::synom::PResult<$o> {
Michael Layzellf8334e32017-06-04 19:01:08 -040089 $submac!(i, $($args)*)
90 }
91 };
David Tolnayb5a7b142016-09-13 22:46:39 -070092}
93
David Tolnayd1ec6ec2018-01-03 00:23:45 -080094#[cfg(synom_verbose_trace)]
Nika Layzellae81b372017-12-05 14:12:33 -050095#[macro_export]
96macro_rules! call {
David Tolnaydc03aec2017-12-30 01:54:18 -050097 ($i:expr, $fun:expr $(, $args:expr)*) => {{
98 let i = $i;
99 eprintln!(concat!(" -> ", stringify!($fun), " @ {:?}"), i);
100 let r = $fun(i $(, $args)*);
101 match r {
David Tolnayf4aa6b42017-12-31 16:40:33 -0500102 Ok((_, i)) => eprintln!(concat!("OK ", stringify!($fun), " @ {:?}"), i),
David Tolnaydc03aec2017-12-30 01:54:18 -0500103 Err(_) => eprintln!(concat!("ERR ", stringify!($fun), " @ {:?}"), i),
Nika Layzellae81b372017-12-05 14:12:33 -0500104 }
David Tolnaydc03aec2017-12-30 01:54:18 -0500105 r
106 }};
Nika Layzellae81b372017-12-05 14:12:33 -0500107}
108
David Tolnaydc03aec2017-12-30 01:54:18 -0500109/// Invoke the given parser function with zero or more arguments.
Michael Layzell24645a32017-02-04 13:19:26 -0500110///
David Tolnaydc03aec2017-12-30 01:54:18 -0500111/// - **Syntax:** `call!(FN, ARGS...)`
David Tolnay1f16b602017-02-07 20:06:55 -0500112///
David Tolnaydc03aec2017-12-30 01:54:18 -0500113/// where the signature of the function is `fn(Cursor, ARGS...) -> PResult<T>`
114///
115/// - **Output:** `T`, the result of invoking the function `FN`
116///
117/// ```rust
118/// #[macro_use]
119/// extern crate syn;
120///
121/// use syn::Type;
David Tolnayf2cfd722017-12-31 18:02:51 -0500122/// use syn::punctuated::Punctuated;
David Tolnaydc03aec2017-12-30 01:54:18 -0500123/// use syn::synom::Synom;
124///
125/// /// Parses one or more Rust types separated by commas.
126/// ///
127/// /// Example: `String, Vec<T>, [u8; LEN + 1]`
128/// struct CommaSeparatedTypes {
David Tolnayf2cfd722017-12-31 18:02:51 -0500129/// types: Punctuated<Type, Token![,]>,
David Tolnaydc03aec2017-12-30 01:54:18 -0500130/// }
131///
132/// impl Synom for CommaSeparatedTypes {
133/// named!(parse -> Self, do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500134/// types: call!(Punctuated::parse_separated_nonempty) >>
David Tolnayddc5dfa2017-12-31 15:35:07 -0500135/// (CommaSeparatedTypes { types })
David Tolnaydc03aec2017-12-30 01:54:18 -0500136/// ));
137/// }
138/// #
139/// # fn main() {}
140/// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800141///
142/// *This macro is available if Syn is built with the `"parsing"` feature.*
David Tolnayd1ec6ec2018-01-03 00:23:45 -0800143#[cfg(not(synom_verbose_trace))]
Michael Layzell5bde96f2017-01-24 17:59:21 -0500144#[macro_export]
David Tolnayb5a7b142016-09-13 22:46:39 -0700145macro_rules! call {
David Tolnayaf2557e2016-10-24 11:52:21 -0700146 ($i:expr, $fun:expr $(, $args:expr)*) => {
147 $fun($i $(, $args)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700148 };
149}
150
David Tolnay1f16b602017-02-07 20:06:55 -0500151/// Transform the result of a parser by applying a function or closure.
Michael Layzell24645a32017-02-04 13:19:26 -0500152///
David Tolnay1f16b602017-02-07 20:06:55 -0500153/// - **Syntax:** `map!(THING, FN)`
154/// - **Output:** the return type of function FN applied to THING
Michael Layzell24645a32017-02-04 13:19:26 -0500155///
156/// ```rust
David Tolnayc5ab8c62017-12-26 16:43:39 -0500157/// #[macro_use]
Michael Layzell24645a32017-02-04 13:19:26 -0500158/// extern crate syn;
Michael Layzell24645a32017-02-04 13:19:26 -0500159///
David Tolnayb21bb0c2017-06-03 20:39:19 -0700160/// use syn::{Expr, ExprIf};
Michael Layzell24645a32017-02-04 13:19:26 -0500161///
David Tolnaydc03aec2017-12-30 01:54:18 -0500162/// /// Extracts the branch condition of an `if`-expression.
David Tolnayb21bb0c2017-06-03 20:39:19 -0700163/// fn get_cond(if_: ExprIf) -> Expr {
164/// *if_.cond
Michael Layzell24645a32017-02-04 13:19:26 -0500165/// }
166///
David Tolnaydc03aec2017-12-30 01:54:18 -0500167/// /// Parses a full `if`-expression but returns the condition part only.
168/// ///
169/// /// Example: `if x > 0xFF { "big" } else { "small" }`
170/// /// The return would be the expression `x > 0xFF`.
David Tolnayb21bb0c2017-06-03 20:39:19 -0700171/// named!(if_condition -> Expr,
172/// map!(syn!(ExprIf), get_cond)
David Tolnay1f16b602017-02-07 20:06:55 -0500173/// );
174///
David Tolnaydc03aec2017-12-30 01:54:18 -0500175/// /// Equivalent using a closure.
David Tolnayb21bb0c2017-06-03 20:39:19 -0700176/// named!(if_condition2 -> Expr,
David Tolnaybc7d7d92017-06-03 20:54:05 -0700177/// map!(syn!(ExprIf), |if_| *if_.cond)
David Tolnay1f16b602017-02-07 20:06:55 -0500178/// );
David Tolnayb21bb0c2017-06-03 20:39:19 -0700179/// #
Alex Crichton954046c2017-05-30 21:49:42 -0700180/// # fn main() {}
Michael Layzell24645a32017-02-04 13:19:26 -0500181/// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800182///
183/// *This macro is available if Syn is built with the `"parsing"` feature.*
Michael Layzell5bde96f2017-01-24 17:59:21 -0500184#[macro_export]
David Tolnayb5a7b142016-09-13 22:46:39 -0700185macro_rules! map {
186 ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => {
David Tolnayb5a7b142016-09-13 22:46:39 -0700187 match $submac!($i, $($args)*) {
Michael Layzell760fd662017-05-31 22:46:05 -0400188 ::std::result::Result::Err(err) =>
189 ::std::result::Result::Err(err),
David Tolnayf4aa6b42017-12-31 16:40:33 -0500190 ::std::result::Result::Ok((o, i)) =>
191 ::std::result::Result::Ok(($crate::parsers::invoke($g, o), i)),
David Tolnayb5a7b142016-09-13 22:46:39 -0700192 }
193 };
David Tolnay1f16b602017-02-07 20:06:55 -0500194
195 ($i:expr, $f:expr, $g:expr) => {
196 map!($i, call!($f), $g)
197 };
David Tolnayb5a7b142016-09-13 22:46:39 -0700198}
199
David Tolnay995bff22017-12-17 23:44:43 -0800200// Somehow this helps with type inference in `map!` and `alt!`.
David Tolnaybc7d7d92017-06-03 20:54:05 -0700201//
202// Not public API.
203#[doc(hidden)]
204pub fn invoke<T, R, F: FnOnce(T) -> R>(f: F, t: T) -> R {
205 f(t)
206}
207
David Tolnaydc03aec2017-12-30 01:54:18 -0500208/// Invert the result of a parser by parsing successfully if the given parser
209/// fails to parse and vice versa.
210///
211/// Does not consume any of the input.
Michael Layzell24645a32017-02-04 13:19:26 -0500212///
213/// - **Syntax:** `not!(THING)`
David Tolnay1f16b602017-02-07 20:06:55 -0500214/// - **Output:** `()`
David Tolnaydc03aec2017-12-30 01:54:18 -0500215///
216/// ```rust
217/// #[macro_use]
218/// extern crate syn;
219///
220/// use syn::{Expr, Ident};
221///
222/// /// Parses any expression that does not begin with a `-` minus sign.
223/// named!(not_negative_expr -> Expr, do_parse!(
224/// not!(punct!(-)) >>
225/// e: syn!(Expr) >>
226/// (e)
227/// ));
228/// #
229/// # fn main() {}
230/// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800231///
232/// *This macro is available if Syn is built with the `"parsing"` feature.*
Michael Layzell5bde96f2017-01-24 17:59:21 -0500233#[macro_export]
David Tolnayb5a7b142016-09-13 22:46:39 -0700234macro_rules! not {
235 ($i:expr, $submac:ident!( $($args:tt)* )) => {
236 match $submac!($i, $($args)*) {
Michael Layzell760fd662017-05-31 22:46:05 -0400237 ::std::result::Result::Ok(_) => $crate::parse_error(),
238 ::std::result::Result::Err(_) =>
David Tolnayf4aa6b42017-12-31 16:40:33 -0500239 ::std::result::Result::Ok(((), $i)),
David Tolnayb5a7b142016-09-13 22:46:39 -0700240 }
241 };
242}
243
David Tolnaydc03aec2017-12-30 01:54:18 -0500244/// Execute a parser only if a condition is met, otherwise return None.
Michael Layzell24645a32017-02-04 13:19:26 -0500245///
David Tolnay1f16b602017-02-07 20:06:55 -0500246/// If you are familiar with nom, this is nom's `cond_with_error` parser.
247///
248/// - **Syntax:** `cond!(CONDITION, THING)`
249/// - **Output:** `Some(THING)` if the condition is true, else `None`
David Tolnaydc03aec2017-12-30 01:54:18 -0500250///
251/// ```rust
252/// #[macro_use]
253/// extern crate syn;
254///
David Tolnayddc5dfa2017-12-31 15:35:07 -0500255/// use syn::{Ident, MacroDelimiter};
David Tolnaydc03aec2017-12-30 01:54:18 -0500256/// use syn::token::{Paren, Bracket, Brace};
257/// use syn::synom::Synom;
258///
259/// /// Parses a macro call with empty input. If the macro is written with
260/// /// parentheses or brackets, a trailing semicolon is required.
261/// ///
262/// /// Example: `my_macro!{}` or `my_macro!();` or `my_macro![];`
263/// struct EmptyMacroCall {
264/// name: Ident,
265/// bang_token: Token![!],
David Tolnayddc5dfa2017-12-31 15:35:07 -0500266/// empty_body: MacroDelimiter,
David Tolnaydc03aec2017-12-30 01:54:18 -0500267/// semi_token: Option<Token![;]>,
268/// }
269///
David Tolnayddc5dfa2017-12-31 15:35:07 -0500270/// fn requires_semi(delimiter: &MacroDelimiter) -> bool {
271/// match *delimiter {
272/// MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => true,
273/// MacroDelimiter::Brace(_) => false,
David Tolnaydc03aec2017-12-30 01:54:18 -0500274/// }
275/// }
276///
277/// impl Synom for EmptyMacroCall {
278/// named!(parse -> Self, do_parse!(
279/// name: syn!(Ident) >>
David Tolnayddc5dfa2017-12-31 15:35:07 -0500280/// bang_token: punct!(!) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500281/// empty_body: alt!(
David Tolnay8875fca2017-12-31 13:52:37 -0500282/// parens!(epsilon!()) => { |d| MacroDelimiter::Paren(d.0) }
David Tolnaydc03aec2017-12-30 01:54:18 -0500283/// |
David Tolnay8875fca2017-12-31 13:52:37 -0500284/// brackets!(epsilon!()) => { |d| MacroDelimiter::Bracket(d.0) }
David Tolnaydc03aec2017-12-30 01:54:18 -0500285/// |
David Tolnay8875fca2017-12-31 13:52:37 -0500286/// braces!(epsilon!()) => { |d| MacroDelimiter::Brace(d.0) }
David Tolnaydc03aec2017-12-30 01:54:18 -0500287/// ) >>
David Tolnayddc5dfa2017-12-31 15:35:07 -0500288/// semi_token: cond!(requires_semi(&empty_body), punct!(;)) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500289/// (EmptyMacroCall {
David Tolnayddc5dfa2017-12-31 15:35:07 -0500290/// name,
291/// bang_token,
292/// empty_body,
293/// semi_token,
David Tolnaydc03aec2017-12-30 01:54:18 -0500294/// })
295/// ));
296/// }
297/// #
298/// # fn main() {}
299/// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800300///
301/// *This macro is available if Syn is built with the `"parsing"` feature.*
Michael Layzell5bde96f2017-01-24 17:59:21 -0500302#[macro_export]
David Tolnayb5a7b142016-09-13 22:46:39 -0700303macro_rules! cond {
304 ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => {
305 if $cond {
306 match $submac!($i, $($args)*) {
David Tolnayf4aa6b42017-12-31 16:40:33 -0500307 ::std::result::Result::Ok((o, i)) =>
308 ::std::result::Result::Ok((::std::option::Option::Some(o), i)),
Michael Layzell760fd662017-05-31 22:46:05 -0400309 ::std::result::Result::Err(x) => ::std::result::Result::Err(x),
David Tolnayb5a7b142016-09-13 22:46:39 -0700310 }
311 } else {
David Tolnayf4aa6b42017-12-31 16:40:33 -0500312 ::std::result::Result::Ok((::std::option::Option::None, $i))
David Tolnayb5a7b142016-09-13 22:46:39 -0700313 }
David Tolnaycfe55022016-10-02 22:02:27 -0700314 };
315
316 ($i:expr, $cond:expr, $f:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700317 cond!($i, $cond, call!($f))
David Tolnaycfe55022016-10-02 22:02:27 -0700318 };
David Tolnayb5a7b142016-09-13 22:46:39 -0700319}
320
David Tolnaydc03aec2017-12-30 01:54:18 -0500321/// Execute a parser only if a condition is met, otherwise fail to parse.
Michael Layzell24645a32017-02-04 13:19:26 -0500322///
David Tolnaydc03aec2017-12-30 01:54:18 -0500323/// This is typically used inside of [`option!`] or [`alt!`].
324///
325/// [`option!`]: macro.option.html
326/// [`alt!`]: macro.alt.html
David Tolnay1f16b602017-02-07 20:06:55 -0500327///
328/// - **Syntax:** `cond_reduce!(CONDITION, THING)`
329/// - **Output:** `THING`
David Tolnaydc03aec2017-12-30 01:54:18 -0500330///
David Tolnay96c6fbe2018-01-11 17:51:56 -0800331/// The subparser may be omitted in which case it defaults to [`epsilon!`].
332///
333/// [`epsilon!`]: macro.epsilon.html
334///
335/// - **Syntax:** `cond_reduce!(CONDITION)`
336/// - **Output:** `()`
337///
David Tolnaydc03aec2017-12-30 01:54:18 -0500338/// ```rust
339/// #[macro_use]
340/// extern crate syn;
341///
342/// use syn::Type;
343/// use syn::token::Paren;
David Tolnayf2cfd722017-12-31 18:02:51 -0500344/// use syn::punctuated::Punctuated;
David Tolnaydc03aec2017-12-30 01:54:18 -0500345/// use syn::synom::Synom;
346///
347/// /// Parses a possibly variadic function signature.
348/// ///
349/// /// Example: `fn(A) or `fn(A, B, C, ...)` or `fn(...)`
350/// /// Rejected: `fn(A, B...)`
351/// struct VariadicFn {
352/// fn_token: Token![fn],
353/// paren_token: Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500354/// types: Punctuated<Type, Token![,]>,
David Tolnaydc03aec2017-12-30 01:54:18 -0500355/// variadic: Option<Token![...]>,
356/// }
357///
358/// // Example of using `cond_reduce!` inside of `option!`.
359/// impl Synom for VariadicFn {
360/// named!(parse -> Self, do_parse!(
361/// fn_token: keyword!(fn) >>
362/// params: parens!(do_parse!(
David Tolnayf2cfd722017-12-31 18:02:51 -0500363/// types: call!(Punctuated::parse_terminated) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500364/// // Allow, but do not require, an ending `...` but only if the
365/// // preceding list of types is empty or ends with a trailing comma.
366/// variadic: option!(cond_reduce!(types.empty_or_trailing(), punct!(...))) >>
367/// (types, variadic)
368/// )) >>
369/// ({
David Tolnaye3d41b72017-12-31 15:24:00 -0500370/// let (paren_token, (types, variadic)) = params;
David Tolnaydc03aec2017-12-30 01:54:18 -0500371/// VariadicFn {
372/// fn_token,
373/// paren_token,
374/// types,
375/// variadic,
376/// }
377/// })
378/// ));
379/// }
380/// #
381/// # fn main() {}
382/// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800383///
384/// *This macro is available if Syn is built with the `"parsing"` feature.*
Michael Layzell5bde96f2017-01-24 17:59:21 -0500385#[macro_export]
David Tolnayaf2557e2016-10-24 11:52:21 -0700386macro_rules! cond_reduce {
387 ($i:expr, $cond:expr, $submac:ident!( $($args:tt)* )) => {
388 if $cond {
389 $submac!($i, $($args)*)
390 } else {
Michael Layzell760fd662017-05-31 22:46:05 -0400391 $crate::parse_error()
David Tolnayaf2557e2016-10-24 11:52:21 -0700392 }
393 };
394
David Tolnay96c6fbe2018-01-11 17:51:56 -0800395 ($i:expr, $cond:expr) => {
396 cond_reduce!($i, $cond, epsilon!())
397 };
398
David Tolnayaf2557e2016-10-24 11:52:21 -0700399 ($i:expr, $cond:expr, $f:expr) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700400 cond_reduce!($i, $cond, call!($f))
David Tolnayaf2557e2016-10-24 11:52:21 -0700401 };
402}
403
David Tolnay1f16b602017-02-07 20:06:55 -0500404/// Parse zero or more values using the given parser.
Michael Layzell24645a32017-02-04 13:19:26 -0500405///
406/// - **Syntax:** `many0!(THING)`
407/// - **Output:** `Vec<THING>`
408///
David Tolnay1f16b602017-02-07 20:06:55 -0500409/// You may also be looking for:
410///
David Tolnayf2cfd722017-12-31 18:02:51 -0500411/// - `call!(Punctuated::parse_separated)` - zero or more values with separator
412/// - `call!(Punctuated::parse_separated_nonempty)` - one or more values
413/// - `call!(Punctuated::parse_terminated)` - zero or more, allows trailing separator
414/// - `call!(Punctuated::parse_terminated_nonempty)` - one or more
David Tolnay1f16b602017-02-07 20:06:55 -0500415///
Michael Layzell24645a32017-02-04 13:19:26 -0500416/// ```rust
David Tolnayc5ab8c62017-12-26 16:43:39 -0500417/// #[macro_use]
Michael Layzell24645a32017-02-04 13:19:26 -0500418/// extern crate syn;
Michael Layzell24645a32017-02-04 13:19:26 -0500419///
David Tolnaydc03aec2017-12-30 01:54:18 -0500420/// use syn::{Ident, Item};
421/// use syn::token::Brace;
422/// use syn::synom::Synom;
Michael Layzell24645a32017-02-04 13:19:26 -0500423///
David Tolnaydc03aec2017-12-30 01:54:18 -0500424/// /// Parses a module containing zero or more Rust items.
425/// ///
426/// /// Example: `mod m { type Result<T> = ::std::result::Result<T, MyError>; }`
427/// struct SimpleMod {
428/// mod_token: Token![mod],
429/// name: Ident,
430/// brace_token: Brace,
431/// items: Vec<Item>,
432/// }
Michael Layzell24645a32017-02-04 13:19:26 -0500433///
David Tolnaydc03aec2017-12-30 01:54:18 -0500434/// impl Synom for SimpleMod {
435/// named!(parse -> Self, do_parse!(
436/// mod_token: keyword!(mod) >>
437/// name: syn!(Ident) >>
438/// body: braces!(many0!(syn!(Item))) >>
439/// (SimpleMod {
David Tolnayddc5dfa2017-12-31 15:35:07 -0500440/// mod_token,
441/// name,
David Tolnay8875fca2017-12-31 13:52:37 -0500442/// brace_token: body.0,
443/// items: body.1,
David Tolnaydc03aec2017-12-30 01:54:18 -0500444/// })
445/// ));
446/// }
447/// #
Alex Crichton954046c2017-05-30 21:49:42 -0700448/// # fn main() {}
David Tolnaydc03aec2017-12-30 01:54:18 -0500449/// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800450///
451/// *This macro is available if Syn is built with the `"parsing"` feature.*
Michael Layzell5bde96f2017-01-24 17:59:21 -0500452#[macro_export]
David Tolnayb5a7b142016-09-13 22:46:39 -0700453macro_rules! many0 {
454 ($i:expr, $submac:ident!( $($args:tt)* )) => {{
455 let ret;
456 let mut res = ::std::vec::Vec::new();
457 let mut input = $i;
458
459 loop {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400460 if input.eof() {
David Tolnayf4aa6b42017-12-31 16:40:33 -0500461 ret = ::std::result::Result::Ok((res, input));
David Tolnayb5a7b142016-09-13 22:46:39 -0700462 break;
463 }
464
465 match $submac!(input, $($args)*) {
Michael Layzell760fd662017-05-31 22:46:05 -0400466 ::std::result::Result::Err(_) => {
David Tolnayf4aa6b42017-12-31 16:40:33 -0500467 ret = ::std::result::Result::Ok((res, input));
David Tolnayb5a7b142016-09-13 22:46:39 -0700468 break;
469 }
David Tolnayf4aa6b42017-12-31 16:40:33 -0500470 ::std::result::Result::Ok((o, i)) => {
David Tolnayb5a7b142016-09-13 22:46:39 -0700471 // loop trip must always consume (otherwise infinite loops)
Michael Layzell0a1a6632017-06-02 18:07:43 -0400472 if i == input {
Michael Layzell760fd662017-05-31 22:46:05 -0400473 ret = $crate::parse_error();
David Tolnayb5a7b142016-09-13 22:46:39 -0700474 break;
475 }
476
477 res.push(o);
478 input = i;
479 }
480 }
481 }
482
483 ret
484 }};
485
486 ($i:expr, $f:expr) => {
David Tolnayc5ab8c62017-12-26 16:43:39 -0500487 $crate::parsers::many0($i, $f)
David Tolnayb5a7b142016-09-13 22:46:39 -0700488 };
489}
490
David Tolnay1f16b602017-02-07 20:06:55 -0500491// Improve compile time by compiling this loop only once per type it is used
492// with.
493//
David Tolnay5fe14fc2017-01-27 16:22:08 -0800494// Not public API.
495#[doc(hidden)]
David Tolnay1c03d8c2017-12-26 23:17:06 -0500496pub fn many0<T>(mut input: Cursor, f: fn(Cursor) -> PResult<T>) -> PResult<Vec<T>> {
David Tolnaybc84d5a2016-10-08 13:20:57 -0700497 let mut res = Vec::new();
498
499 loop {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400500 if input.eof() {
David Tolnayf4aa6b42017-12-31 16:40:33 -0500501 return Ok((res, input));
David Tolnaybc84d5a2016-10-08 13:20:57 -0700502 }
503
504 match f(input) {
Michael Layzell760fd662017-05-31 22:46:05 -0400505 Err(_) => {
David Tolnayf4aa6b42017-12-31 16:40:33 -0500506 return Ok((res, input));
David Tolnaybc84d5a2016-10-08 13:20:57 -0700507 }
David Tolnayf4aa6b42017-12-31 16:40:33 -0500508 Ok((o, i)) => {
David Tolnaybc84d5a2016-10-08 13:20:57 -0700509 // loop trip must always consume (otherwise infinite loops)
Michael Layzell0a1a6632017-06-02 18:07:43 -0400510 if i == input {
Michael Layzell760fd662017-05-31 22:46:05 -0400511 return parse_error();
David Tolnaybc84d5a2016-10-08 13:20:57 -0700512 }
513
514 res.push(o);
515 input = i;
516 }
517 }
518 }
519}
520
David Tolnay1f16b602017-02-07 20:06:55 -0500521/// Pattern-match the result of a parser to select which other parser to run.
522///
523/// - **Syntax:** `switch!(TARGET, PAT1 => THEN1 | PAT2 => THEN2 | ...)`
524/// - **Output:** `T`, the return type of `THEN1` and `THEN2` and ...
525///
526/// ```rust
David Tolnayc5ab8c62017-12-26 16:43:39 -0500527/// #[macro_use]
David Tolnay1f16b602017-02-07 20:06:55 -0500528/// extern crate syn;
David Tolnay1f16b602017-02-07 20:06:55 -0500529///
David Tolnaydc03aec2017-12-30 01:54:18 -0500530/// use syn::Ident;
531/// use syn::token::Brace;
532/// use syn::synom::Synom;
David Tolnay1f16b602017-02-07 20:06:55 -0500533///
David Tolnaydc03aec2017-12-30 01:54:18 -0500534/// /// Parse a unit struct or enum: either `struct S;` or `enum E { V }`.
David Tolnay1f16b602017-02-07 20:06:55 -0500535/// enum UnitType {
536/// Struct {
David Tolnaydc03aec2017-12-30 01:54:18 -0500537/// struct_token: Token![struct],
David Tolnay1f16b602017-02-07 20:06:55 -0500538/// name: Ident,
David Tolnaydc03aec2017-12-30 01:54:18 -0500539/// semi_token: Token![;],
David Tolnay1f16b602017-02-07 20:06:55 -0500540/// },
541/// Enum {
David Tolnaydc03aec2017-12-30 01:54:18 -0500542/// enum_token: Token![enum],
David Tolnay1f16b602017-02-07 20:06:55 -0500543/// name: Ident,
David Tolnaydc03aec2017-12-30 01:54:18 -0500544/// brace_token: Brace,
David Tolnay1f16b602017-02-07 20:06:55 -0500545/// variant: Ident,
546/// },
547/// }
548///
David Tolnaydc03aec2017-12-30 01:54:18 -0500549/// enum StructOrEnum {
550/// Struct(Token![struct]),
551/// Enum(Token![enum]),
552/// }
David Tolnay1f16b602017-02-07 20:06:55 -0500553///
David Tolnaydc03aec2017-12-30 01:54:18 -0500554/// impl Synom for StructOrEnum {
555/// named!(parse -> Self, alt!(
556/// keyword!(struct) => { StructOrEnum::Struct }
557/// |
558/// keyword!(enum) => { StructOrEnum::Enum }
559/// ));
560/// }
561///
562/// impl Synom for UnitType {
563/// named!(parse -> Self, do_parse!(
564/// which: syn!(StructOrEnum) >>
565/// name: syn!(Ident) >>
566/// item: switch!(value!(which),
567/// StructOrEnum::Struct(struct_token) => map!(
568/// punct!(;),
569/// |semi_token| UnitType::Struct {
David Tolnayddc5dfa2017-12-31 15:35:07 -0500570/// struct_token,
571/// name,
572/// semi_token,
David Tolnaydc03aec2017-12-30 01:54:18 -0500573/// }
574/// )
575/// |
576/// StructOrEnum::Enum(enum_token) => map!(
577/// braces!(syn!(Ident)),
David Tolnay8875fca2017-12-31 13:52:37 -0500578/// |(brace_token, variant)| UnitType::Enum {
David Tolnayddc5dfa2017-12-31 15:35:07 -0500579/// enum_token,
580/// name,
581/// brace_token,
582/// variant,
David Tolnaydc03aec2017-12-30 01:54:18 -0500583/// }
584/// )
585/// ) >>
586/// (item)
587/// ));
588/// }
589/// #
Alex Crichton954046c2017-05-30 21:49:42 -0700590/// # fn main() {}
David Tolnay1f16b602017-02-07 20:06:55 -0500591/// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800592///
593/// *This macro is available if Syn is built with the `"parsing"` feature.*
Michael Layzell5bde96f2017-01-24 17:59:21 -0500594#[macro_export]
David Tolnayb5a7b142016-09-13 22:46:39 -0700595macro_rules! switch {
596 ($i:expr, $submac:ident!( $($args:tt)* ), $($p:pat => $subrule:ident!( $($args2:tt)* ))|* ) => {
597 match $submac!($i, $($args)*) {
Michael Layzell760fd662017-05-31 22:46:05 -0400598 ::std::result::Result::Err(err) => ::std::result::Result::Err(err),
David Tolnayf4aa6b42017-12-31 16:40:33 -0500599 ::std::result::Result::Ok((o, i)) => match o {
David Tolnayb5a7b142016-09-13 22:46:39 -0700600 $(
601 $p => $subrule!(i, $($args2)*),
602 )*
David Tolnayb5a7b142016-09-13 22:46:39 -0700603 }
604 }
605 };
606}
607
David Tolnaydc03aec2017-12-30 01:54:18 -0500608/// Produce the given value without parsing anything.
609///
610/// This can be needed where you have an existing parsed value but a parser
611/// macro's syntax expects you to provide a submacro, such as in the first
612/// argument of [`switch!`] or one of the branches of [`alt!`].
613///
614/// [`switch!`]: macro.switch.html
615/// [`alt!`]: macro.alt.html
David Tolnay1f16b602017-02-07 20:06:55 -0500616///
617/// - **Syntax:** `value!(VALUE)`
618/// - **Output:** `VALUE`
619///
620/// ```rust
David Tolnayc5ab8c62017-12-26 16:43:39 -0500621/// #[macro_use]
David Tolnay1f16b602017-02-07 20:06:55 -0500622/// extern crate syn;
David Tolnay1f16b602017-02-07 20:06:55 -0500623///
David Tolnay92a56512017-11-10 00:02:14 -0800624/// use syn::Ident;
David Tolnaydc03aec2017-12-30 01:54:18 -0500625/// use syn::token::Brace;
626/// use syn::synom::Synom;
David Tolnay1f16b602017-02-07 20:06:55 -0500627///
David Tolnaydc03aec2017-12-30 01:54:18 -0500628/// /// Parse a unit struct or enum: either `struct S;` or `enum E { V }`.
David Tolnay1f16b602017-02-07 20:06:55 -0500629/// enum UnitType {
630/// Struct {
David Tolnaydc03aec2017-12-30 01:54:18 -0500631/// struct_token: Token![struct],
David Tolnay1f16b602017-02-07 20:06:55 -0500632/// name: Ident,
David Tolnaydc03aec2017-12-30 01:54:18 -0500633/// semi_token: Token![;],
David Tolnay1f16b602017-02-07 20:06:55 -0500634/// },
635/// Enum {
David Tolnaydc03aec2017-12-30 01:54:18 -0500636/// enum_token: Token![enum],
David Tolnay1f16b602017-02-07 20:06:55 -0500637/// name: Ident,
David Tolnaydc03aec2017-12-30 01:54:18 -0500638/// brace_token: Brace,
David Tolnay1f16b602017-02-07 20:06:55 -0500639/// variant: Ident,
640/// },
641/// }
642///
David Tolnaydc03aec2017-12-30 01:54:18 -0500643/// enum StructOrEnum {
644/// Struct(Token![struct]),
645/// Enum(Token![enum]),
646/// }
David Tolnay1f16b602017-02-07 20:06:55 -0500647///
David Tolnaydc03aec2017-12-30 01:54:18 -0500648/// impl Synom for StructOrEnum {
649/// named!(parse -> Self, alt!(
650/// keyword!(struct) => { StructOrEnum::Struct }
651/// |
652/// keyword!(enum) => { StructOrEnum::Enum }
653/// ));
654/// }
655///
656/// impl Synom for UnitType {
657/// named!(parse -> Self, do_parse!(
658/// which: syn!(StructOrEnum) >>
659/// name: syn!(Ident) >>
660/// item: switch!(value!(which),
661/// StructOrEnum::Struct(struct_token) => map!(
662/// punct!(;),
663/// |semi_token| UnitType::Struct {
David Tolnayddc5dfa2017-12-31 15:35:07 -0500664/// struct_token,
665/// name,
666/// semi_token,
David Tolnaydc03aec2017-12-30 01:54:18 -0500667/// }
668/// )
669/// |
670/// StructOrEnum::Enum(enum_token) => map!(
671/// braces!(syn!(Ident)),
David Tolnay8875fca2017-12-31 13:52:37 -0500672/// |(brace_token, variant)| UnitType::Enum {
David Tolnayddc5dfa2017-12-31 15:35:07 -0500673/// enum_token,
674/// name,
675/// brace_token,
676/// variant,
David Tolnaydc03aec2017-12-30 01:54:18 -0500677/// }
678/// )
679/// ) >>
680/// (item)
681/// ));
682/// }
683/// #
Alex Crichton954046c2017-05-30 21:49:42 -0700684/// # fn main() {}
David Tolnay1f16b602017-02-07 20:06:55 -0500685/// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800686///
687/// *This macro is available if Syn is built with the `"parsing"` feature.*
Michael Layzell5bde96f2017-01-24 17:59:21 -0500688#[macro_export]
David Tolnayb5a7b142016-09-13 22:46:39 -0700689macro_rules! value {
690 ($i:expr, $res:expr) => {
David Tolnayf4aa6b42017-12-31 16:40:33 -0500691 ::std::result::Result::Ok(($res, $i))
David Tolnayb5a7b142016-09-13 22:46:39 -0700692 };
693}
694
David Tolnaydc03aec2017-12-30 01:54:18 -0500695/// Unconditionally fail to parse anything.
696///
697/// This may be useful in rejecting some arms of a `switch!` parser.
David Tolnay92a56512017-11-10 00:02:14 -0800698///
699/// - **Syntax:** `reject!()`
700/// - **Output:** never succeeds
701///
702/// ```rust
David Tolnayc5ab8c62017-12-26 16:43:39 -0500703/// #[macro_use]
David Tolnay92a56512017-11-10 00:02:14 -0800704/// extern crate syn;
David Tolnay92a56512017-11-10 00:02:14 -0800705///
706/// use syn::Item;
707///
708/// // Parse any item, except for a module.
709/// named!(almost_any_item -> Item,
710/// switch!(syn!(Item),
711/// Item::Mod(_) => reject!()
712/// |
713/// ok => value!(ok)
714/// )
715/// );
David Tolnaydc03aec2017-12-30 01:54:18 -0500716/// #
David Tolnay92a56512017-11-10 00:02:14 -0800717/// # fn main() {}
718/// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800719///
720/// *This macro is available if Syn is built with the `"parsing"` feature.*
David Tolnay92a56512017-11-10 00:02:14 -0800721#[macro_export]
722macro_rules! reject {
David Tolnay2bd17422017-12-25 18:44:20 -0500723 ($i:expr,) => {{
724 let _ = $i;
David Tolnay92a56512017-11-10 00:02:14 -0800725 $crate::parse_error()
David Tolnay2bd17422017-12-25 18:44:20 -0500726 }}
David Tolnay92a56512017-11-10 00:02:14 -0800727}
728
David Tolnay1f16b602017-02-07 20:06:55 -0500729/// Run a series of parsers and produce all of the results in a tuple.
Michael Layzell24645a32017-02-04 13:19:26 -0500730///
David Tolnay1f16b602017-02-07 20:06:55 -0500731/// - **Syntax:** `tuple!(A, B, C, ...)`
732/// - **Output:** `(A, B, C, ...)`
Michael Layzell24645a32017-02-04 13:19:26 -0500733///
734/// ```rust
David Tolnayc5ab8c62017-12-26 16:43:39 -0500735/// #[macro_use]
Michael Layzell24645a32017-02-04 13:19:26 -0500736/// extern crate syn;
Michael Layzell24645a32017-02-04 13:19:26 -0500737///
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800738/// use syn::Type;
Michael Layzell24645a32017-02-04 13:19:26 -0500739///
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800740/// named!(two_types -> (Type, Type), tuple!(syn!(Type), syn!(Type)));
David Tolnaydc03aec2017-12-30 01:54:18 -0500741/// #
Alex Crichton954046c2017-05-30 21:49:42 -0700742/// # fn main() {}
Michael Layzell24645a32017-02-04 13:19:26 -0500743/// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800744///
745/// *This macro is available if Syn is built with the `"parsing"` feature.*
Michael Layzell5bde96f2017-01-24 17:59:21 -0500746#[macro_export]
David Tolnayb5a7b142016-09-13 22:46:39 -0700747macro_rules! tuple {
748 ($i:expr, $($rest:tt)*) => {
749 tuple_parser!($i, (), $($rest)*)
750 };
751}
752
David Tolnaydc03aec2017-12-30 01:54:18 -0500753// Internal parser, do not use directly.
Michael Layzell5bde96f2017-01-24 17:59:21 -0500754#[doc(hidden)]
755#[macro_export]
David Tolnayb5a7b142016-09-13 22:46:39 -0700756macro_rules! tuple_parser {
757 ($i:expr, ($($parsed:tt),*), $e:ident, $($rest:tt)*) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700758 tuple_parser!($i, ($($parsed),*), call!($e), $($rest)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700759 };
760
761 ($i:expr, (), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => {
762 match $submac!($i, $($args)*) {
Michael Layzell760fd662017-05-31 22:46:05 -0400763 ::std::result::Result::Err(err) =>
764 ::std::result::Result::Err(err),
David Tolnayf4aa6b42017-12-31 16:40:33 -0500765 ::std::result::Result::Ok((o, i)) =>
David Tolnayb5a7b142016-09-13 22:46:39 -0700766 tuple_parser!(i, (o), $($rest)*),
767 }
768 };
769
770 ($i:expr, ($($parsed:tt)*), $submac:ident!( $($args:tt)* ), $($rest:tt)*) => {
771 match $submac!($i, $($args)*) {
Michael Layzell760fd662017-05-31 22:46:05 -0400772 ::std::result::Result::Err(err) =>
773 ::std::result::Result::Err(err),
David Tolnayf4aa6b42017-12-31 16:40:33 -0500774 ::std::result::Result::Ok((o, i)) =>
David Tolnayb5a7b142016-09-13 22:46:39 -0700775 tuple_parser!(i, ($($parsed)* , o), $($rest)*),
776 }
777 };
778
779 ($i:expr, ($($parsed:tt),*), $e:ident) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700780 tuple_parser!($i, ($($parsed),*), call!($e))
David Tolnayb5a7b142016-09-13 22:46:39 -0700781 };
782
783 ($i:expr, (), $submac:ident!( $($args:tt)* )) => {
784 $submac!($i, $($args)*)
785 };
786
787 ($i:expr, ($($parsed:expr),*), $submac:ident!( $($args:tt)* )) => {
788 match $submac!($i, $($args)*) {
Michael Layzell760fd662017-05-31 22:46:05 -0400789 ::std::result::Result::Err(err) =>
790 ::std::result::Result::Err(err),
David Tolnayf4aa6b42017-12-31 16:40:33 -0500791 ::std::result::Result::Ok((o, i)) =>
792 ::std::result::Result::Ok((($($parsed),*, o), i)),
David Tolnayb5a7b142016-09-13 22:46:39 -0700793 }
794 };
795
796 ($i:expr, ($($parsed:expr),*)) => {
David Tolnayf4aa6b42017-12-31 16:40:33 -0500797 ::std::result::Result::Ok((($($parsed),*), $i))
David Tolnayb5a7b142016-09-13 22:46:39 -0700798 };
799}
800
David Tolnay1f16b602017-02-07 20:06:55 -0500801/// Run a series of parsers, returning the result of the first one which
802/// succeeds.
Michael Layzell24645a32017-02-04 13:19:26 -0500803///
804/// Optionally allows for the result to be transformed.
805///
806/// - **Syntax:** `alt!(THING1 | THING2 => { FUNC } | ...)`
David Tolnay1f16b602017-02-07 20:06:55 -0500807/// - **Output:** `T`, the return type of `THING1` and `FUNC(THING2)` and ...
Michael Layzell24645a32017-02-04 13:19:26 -0500808///
David Tolnay1beb9242018-01-06 19:51:43 -0800809/// # Example
810///
Michael Layzell24645a32017-02-04 13:19:26 -0500811/// ```rust
David Tolnayc5ab8c62017-12-26 16:43:39 -0500812/// #[macro_use]
Michael Layzell24645a32017-02-04 13:19:26 -0500813/// extern crate syn;
Michael Layzell24645a32017-02-04 13:19:26 -0500814///
815/// use syn::Ident;
Michael Layzell24645a32017-02-04 13:19:26 -0500816///
David Tolnay1beb9242018-01-06 19:51:43 -0800817/// // Parse any identifier token, or the `!` token in which case the
818/// // identifier is treated as `"BANG"`.
819/// named!(ident_or_bang -> Ident, alt!(
820/// syn!(Ident)
821/// |
822/// punct!(!) => { |_| "BANG".into() }
823/// ));
824/// #
825/// # fn main() {}
826/// ```
827///
828/// The `alt!` macro is most commonly seen when parsing a syntax tree enum such
829/// as the [`Item`] enum.
830///
831/// [`Item`]: enum.Item.html
832///
833/// ```
834/// # #[macro_use]
835/// # extern crate syn;
836/// #
837/// # use syn::synom::Synom;
838/// #
839/// # struct Item;
840/// #
841/// impl Synom for Item {
842/// named!(parse -> Self, alt!(
843/// # epsilon!() => { |_| unimplemented!() }
844/// # ));
845/// # }
846/// #
847/// # mod example {
848/// # use syn::*;
849/// #
850/// # named!(parse -> Item, alt!(
851/// syn!(ItemExternCrate) => { Item::ExternCrate }
David Tolnay1f16b602017-02-07 20:06:55 -0500852/// |
David Tolnay1beb9242018-01-06 19:51:43 -0800853/// syn!(ItemUse) => { Item::Use }
854/// |
855/// syn!(ItemStatic) => { Item::Static }
856/// |
857/// syn!(ItemConst) => { Item::Const }
858/// |
859/// /* ... */
860/// # syn!(ItemFn) => { Item::Fn }
861/// ));
862/// }
David Tolnaydc03aec2017-12-30 01:54:18 -0500863/// #
Alex Crichton954046c2017-05-30 21:49:42 -0700864/// # fn main() {}
Michael Layzell24645a32017-02-04 13:19:26 -0500865/// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800866///
867/// *This macro is available if Syn is built with the `"parsing"` feature.*
Michael Layzell5bde96f2017-01-24 17:59:21 -0500868#[macro_export]
David Tolnayb5a7b142016-09-13 22:46:39 -0700869macro_rules! alt {
870 ($i:expr, $e:ident | $($rest:tt)*) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700871 alt!($i, call!($e) | $($rest)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700872 };
873
874 ($i:expr, $subrule:ident!( $($args:tt)*) | $($rest:tt)*) => {
875 match $subrule!($i, $($args)*) {
Michael Layzell760fd662017-05-31 22:46:05 -0400876 res @ ::std::result::Result::Ok(_) => res,
David Tolnayb5a7b142016-09-13 22:46:39 -0700877 _ => alt!($i, $($rest)*)
878 }
879 };
880
881 ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr } | $($rest:tt)+) => {
882 match $subrule!($i, $($args)*) {
David Tolnayf4aa6b42017-12-31 16:40:33 -0500883 ::std::result::Result::Ok((o, i)) =>
884 ::std::result::Result::Ok(($crate::parsers::invoke($gen, o), i)),
Michael Layzell760fd662017-05-31 22:46:05 -0400885 ::std::result::Result::Err(_) => alt!($i, $($rest)*),
David Tolnayb5a7b142016-09-13 22:46:39 -0700886 }
887 };
888
889 ($i:expr, $e:ident => { $gen:expr } | $($rest:tt)*) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700890 alt!($i, call!($e) => { $gen } | $($rest)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700891 };
892
893 ($i:expr, $e:ident => { $gen:expr }) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700894 alt!($i, call!($e) => { $gen })
David Tolnayb5a7b142016-09-13 22:46:39 -0700895 };
896
897 ($i:expr, $subrule:ident!( $($args:tt)* ) => { $gen:expr }) => {
898 match $subrule!($i, $($args)*) {
David Tolnayf4aa6b42017-12-31 16:40:33 -0500899 ::std::result::Result::Ok((o, i)) =>
900 ::std::result::Result::Ok(($crate::parsers::invoke($gen, o), i)),
Michael Layzell760fd662017-05-31 22:46:05 -0400901 ::std::result::Result::Err(err) =>
902 ::std::result::Result::Err(err),
David Tolnayb5a7b142016-09-13 22:46:39 -0700903 }
904 };
905
906 ($i:expr, $e:ident) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700907 alt!($i, call!($e))
David Tolnayb5a7b142016-09-13 22:46:39 -0700908 };
909
910 ($i:expr, $subrule:ident!( $($args:tt)*)) => {
David Tolnay5377b172016-10-25 01:13:12 -0700911 $subrule!($i, $($args)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700912 };
913}
914
David Tolnaydc03aec2017-12-30 01:54:18 -0500915/// Run a series of parsers, optionally naming each intermediate result,
916/// followed by a step to combine the intermediate results.
Michael Layzell24645a32017-02-04 13:19:26 -0500917///
David Tolnay1f16b602017-02-07 20:06:55 -0500918/// Produces the result of evaluating the final expression in parentheses with
919/// all of the previously named results bound.
Michael Layzell24645a32017-02-04 13:19:26 -0500920///
David Tolnay1f16b602017-02-07 20:06:55 -0500921/// - **Syntax:** `do_parse!(name: THING1 >> THING2 >> (RESULT))`
Michael Layzell24645a32017-02-04 13:19:26 -0500922/// - **Output:** `RESULT`
923///
924/// ```rust
David Tolnayc5ab8c62017-12-26 16:43:39 -0500925/// #[macro_use]
Michael Layzell24645a32017-02-04 13:19:26 -0500926/// extern crate syn;
Alex Crichton954046c2017-05-30 21:49:42 -0700927/// extern crate proc_macro2;
Michael Layzell24645a32017-02-04 13:19:26 -0500928///
David Tolnay9c76bcb2017-12-26 23:14:59 -0500929/// use syn::Ident;
David Tolnay32954ef2017-12-26 22:43:16 -0500930/// use syn::token::Paren;
David Tolnaydc03aec2017-12-30 01:54:18 -0500931/// use syn::synom::Synom;
Alex Crichton954046c2017-05-30 21:49:42 -0700932/// use proc_macro2::TokenStream;
Michael Layzell24645a32017-02-04 13:19:26 -0500933///
David Tolnaydc03aec2017-12-30 01:54:18 -0500934/// /// Parse a macro invocation that uses `(` `)` parentheses.
935/// ///
936/// /// Example: `stringify!($args)`.
937/// struct Macro {
938/// name: Ident,
939/// bang_token: Token![!],
940/// paren_token: Paren,
941/// tts: TokenStream,
942/// }
Michael Layzell24645a32017-02-04 13:19:26 -0500943///
David Tolnaydc03aec2017-12-30 01:54:18 -0500944/// impl Synom for Macro {
945/// named!(parse -> Self, do_parse!(
946/// name: syn!(Ident) >>
David Tolnayddc5dfa2017-12-31 15:35:07 -0500947/// bang_token: punct!(!) >>
David Tolnaydc03aec2017-12-30 01:54:18 -0500948/// body: parens!(syn!(TokenStream)) >>
949/// (Macro {
David Tolnayddc5dfa2017-12-31 15:35:07 -0500950/// name,
951/// bang_token,
David Tolnaye3d41b72017-12-31 15:24:00 -0500952/// paren_token: body.0,
953/// tts: body.1,
David Tolnaydc03aec2017-12-30 01:54:18 -0500954/// })
955/// ));
956/// }
957/// #
Alex Crichton954046c2017-05-30 21:49:42 -0700958/// # fn main() {}
Michael Layzell24645a32017-02-04 13:19:26 -0500959/// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800960///
961/// *This macro is available if Syn is built with the `"parsing"` feature.*
Michael Layzell5bde96f2017-01-24 17:59:21 -0500962#[macro_export]
David Tolnayb5a7b142016-09-13 22:46:39 -0700963macro_rules! do_parse {
964 ($i:expr, ( $($rest:expr),* )) => {
David Tolnayf4aa6b42017-12-31 16:40:33 -0500965 ::std::result::Result::Ok((( $($rest),* ), $i))
David Tolnayb5a7b142016-09-13 22:46:39 -0700966 };
967
968 ($i:expr, $e:ident >> $($rest:tt)*) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700969 do_parse!($i, call!($e) >> $($rest)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700970 };
971
972 ($i:expr, $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
973 match $submac!($i, $($args)*) {
Michael Layzell760fd662017-05-31 22:46:05 -0400974 ::std::result::Result::Err(err) =>
975 ::std::result::Result::Err(err),
David Tolnayf4aa6b42017-12-31 16:40:33 -0500976 ::std::result::Result::Ok((_, i)) =>
David Tolnayb5a7b142016-09-13 22:46:39 -0700977 do_parse!(i, $($rest)*),
978 }
979 };
980
981 ($i:expr, $field:ident : $e:ident >> $($rest:tt)*) => {
David Tolnayb81c7a42016-10-25 10:12:12 -0700982 do_parse!($i, $field: call!($e) >> $($rest)*)
David Tolnayb5a7b142016-09-13 22:46:39 -0700983 };
984
985 ($i:expr, $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
986 match $submac!($i, $($args)*) {
Michael Layzell760fd662017-05-31 22:46:05 -0400987 ::std::result::Result::Err(err) =>
988 ::std::result::Result::Err(err),
David Tolnayf4aa6b42017-12-31 16:40:33 -0500989 ::std::result::Result::Ok((o, i)) => {
David Tolnayb5a7b142016-09-13 22:46:39 -0700990 let $field = o;
991 do_parse!(i, $($rest)*)
992 },
993 }
994 };
995
David Tolnayfa0edf22016-09-23 22:58:24 -0700996 ($i:expr, mut $field:ident : $e:ident >> $($rest:tt)*) => {
David Tolnay7184b132016-10-30 10:06:37 -0700997 do_parse!($i, mut $field: call!($e) >> $($rest)*)
David Tolnayfa0edf22016-09-23 22:58:24 -0700998 };
999
1000 ($i:expr, mut $field:ident : $submac:ident!( $($args:tt)* ) >> $($rest:tt)*) => {
1001 match $submac!($i, $($args)*) {
Michael Layzell760fd662017-05-31 22:46:05 -04001002 ::std::result::Result::Err(err) =>
1003 ::std::result::Result::Err(err),
David Tolnayf4aa6b42017-12-31 16:40:33 -05001004 ::std::result::Result::Ok((o, i)) => {
David Tolnayfa0edf22016-09-23 22:58:24 -07001005 let mut $field = o;
1006 do_parse!(i, $($rest)*)
1007 },
1008 }
1009 };
David Tolnayb5a7b142016-09-13 22:46:39 -07001010}
Michael Layzell416724e2017-05-24 21:12:34 -04001011
David Tolnaydc03aec2017-12-30 01:54:18 -05001012/// Parse nothing and succeed only if the end of the enclosing block has been
1013/// reached.
1014///
1015/// The enclosing block may be the full input if we are parsing at the top
1016/// level, or the surrounding parenthesis/bracket/brace if we are parsing within
1017/// those.
1018///
1019/// - **Syntax:** `input_end!()`
1020/// - **Output:** `()`
1021///
1022/// ```rust
1023/// #[macro_use]
1024/// extern crate syn;
1025///
1026/// use syn::Expr;
1027/// use syn::synom::Synom;
1028///
1029/// /// Parses any Rust expression followed either by a semicolon or by the end
1030/// /// of the input.
1031/// ///
1032/// /// For example `many0!(syn!(TerminatedExpr))` would successfully parse the
1033/// /// following input into three expressions.
1034/// ///
1035/// /// 1 + 1; second.two(); third!()
1036/// ///
1037/// /// Similarly within a block, `braced!(many0!(syn!(TerminatedExpr)))` would
1038/// /// successfully parse three expressions.
1039/// ///
1040/// /// { 1 + 1; second.two(); third!() }
1041/// struct TerminatedExpr {
1042/// expr: Expr,
1043/// semi_token: Option<Token![;]>,
1044/// }
1045///
1046/// impl Synom for TerminatedExpr {
1047/// named!(parse -> Self, do_parse!(
1048/// expr: syn!(Expr) >>
David Tolnayddc5dfa2017-12-31 15:35:07 -05001049/// semi_token: alt!(
David Tolnaydc03aec2017-12-30 01:54:18 -05001050/// input_end!() => { |_| None }
1051/// |
1052/// punct!(;) => { Some }
1053/// ) >>
1054/// (TerminatedExpr {
David Tolnayddc5dfa2017-12-31 15:35:07 -05001055/// expr,
1056/// semi_token,
David Tolnaydc03aec2017-12-30 01:54:18 -05001057/// })
1058/// ));
1059/// }
1060/// #
1061/// # fn main() {}
1062/// ```
David Tolnay461d98e2018-01-07 11:07:19 -08001063///
1064/// *This macro is available if Syn is built with the `"parsing"` feature.*
Michael Layzell416724e2017-05-24 21:12:34 -04001065#[macro_export]
1066macro_rules! input_end {
1067 ($i:expr,) => {
David Tolnayc5ab8c62017-12-26 16:43:39 -05001068 $crate::parsers::input_end($i)
Michael Layzell416724e2017-05-24 21:12:34 -04001069 };
1070}
1071
1072// Not a public API
1073#[doc(hidden)]
David Tolnay1fc4e492017-11-11 22:17:22 -08001074pub fn input_end(input: Cursor) -> PResult<'static, ()> {
Michael Layzell0a1a6632017-06-02 18:07:43 -04001075 if input.eof() {
David Tolnayf4aa6b42017-12-31 16:40:33 -05001076 Ok(((), Cursor::empty()))
Michael Layzell416724e2017-05-24 21:12:34 -04001077 } else {
Michael Layzell760fd662017-05-31 22:46:05 -04001078 parse_error()
Michael Layzell416724e2017-05-24 21:12:34 -04001079 }
1080}
David Tolnayf03cdb82017-12-30 00:05:58 -05001081
1082/// Turn a failed parse into `None` and a successful parse into `Some`.
1083///
David Tolnaydc03aec2017-12-30 01:54:18 -05001084/// A failed parse consumes none of the input.
1085///
David Tolnayf03cdb82017-12-30 00:05:58 -05001086/// - **Syntax:** `option!(THING)`
1087/// - **Output:** `Option<THING>`
1088///
1089/// ```rust
1090/// #[macro_use]
1091/// extern crate syn;
1092///
David Tolnaydc03aec2017-12-30 01:54:18 -05001093/// use syn::{Label, Block};
1094/// use syn::synom::Synom;
David Tolnayf03cdb82017-12-30 00:05:58 -05001095///
David Tolnaydc03aec2017-12-30 01:54:18 -05001096/// /// Parses a Rust loop. Equivalent to syn::ExprLoop.
1097/// ///
1098/// /// Examples:
1099/// /// loop { println!("y"); }
1100/// /// 'x: loop { break 'x; }
1101/// struct ExprLoop {
1102/// label: Option<Label>,
1103/// loop_token: Token![loop],
1104/// body: Block,
1105/// }
David Tolnayf03cdb82017-12-30 00:05:58 -05001106///
David Tolnaydc03aec2017-12-30 01:54:18 -05001107/// impl Synom for ExprLoop {
1108/// named!(parse -> Self, do_parse!(
1109/// // Loop may or may not have a label.
1110/// label: option!(syn!(Label)) >>
David Tolnayddc5dfa2017-12-31 15:35:07 -05001111/// loop_token: keyword!(loop) >>
1112/// body: syn!(Block) >>
David Tolnaydc03aec2017-12-30 01:54:18 -05001113/// (ExprLoop {
David Tolnayddc5dfa2017-12-31 15:35:07 -05001114/// label,
1115/// loop_token,
1116/// body,
David Tolnaydc03aec2017-12-30 01:54:18 -05001117/// })
1118/// ));
1119/// }
1120/// #
David Tolnayf03cdb82017-12-30 00:05:58 -05001121/// # fn main() {}
1122/// ```
David Tolnay461d98e2018-01-07 11:07:19 -08001123///
1124/// *This macro is available if Syn is built with the `"parsing"` feature.*
David Tolnayf03cdb82017-12-30 00:05:58 -05001125#[macro_export]
1126macro_rules! option {
1127 ($i:expr, $submac:ident!( $($args:tt)* )) => {
1128 match $submac!($i, $($args)*) {
David Tolnayf4aa6b42017-12-31 16:40:33 -05001129 ::std::result::Result::Ok((o, i)) =>
1130 ::std::result::Result::Ok((Some(o), i)),
David Tolnayf03cdb82017-12-30 00:05:58 -05001131 ::std::result::Result::Err(_) =>
David Tolnayf4aa6b42017-12-31 16:40:33 -05001132 ::std::result::Result::Ok((None, $i)),
David Tolnayf03cdb82017-12-30 00:05:58 -05001133 }
1134 };
1135
1136 ($i:expr, $f:expr) => {
1137 option!($i, call!($f));
1138 };
1139}
1140
David Tolnayf03cdb82017-12-30 00:05:58 -05001141/// Parses nothing and always succeeds.
1142///
David Tolnaydc03aec2017-12-30 01:54:18 -05001143/// This can be useful as a fallthrough case in [`alt!`], as shown below. Also
1144/// useful for parsing empty delimiters using [`parens!`] or [`brackets!`] or
1145/// [`braces!`] by parsing for example `braces!(epsilon!())` for an empty `{}`.
1146///
1147/// [`alt!`]: macro.alt.html
1148/// [`parens!`]: macro.parens.html
1149/// [`brackets!`]: macro.brackets.html
1150/// [`braces!`]: macro.braces.html
David Tolnayf03cdb82017-12-30 00:05:58 -05001151///
1152/// - **Syntax:** `epsilon!()`
1153/// - **Output:** `()`
1154///
1155/// ```rust
1156/// #[macro_use]
1157/// extern crate syn;
1158///
David Tolnaydc03aec2017-12-30 01:54:18 -05001159/// use syn::synom::Synom;
1160///
David Tolnayf03cdb82017-12-30 00:05:58 -05001161/// enum Mutability {
1162/// Mutable(Token![mut]),
1163/// Immutable,
1164/// }
1165///
David Tolnaydc03aec2017-12-30 01:54:18 -05001166/// impl Synom for Mutability {
1167/// named!(parse -> Self, alt!(
1168/// keyword!(mut) => { Mutability::Mutable }
1169/// |
1170/// epsilon!() => { |_| Mutability::Immutable }
1171/// ));
1172/// }
1173/// #
David Tolnayf03cdb82017-12-30 00:05:58 -05001174/// # fn main() {}
David Tolnaydc03aec2017-12-30 01:54:18 -05001175/// ```
David Tolnay461d98e2018-01-07 11:07:19 -08001176///
1177/// *This macro is available if Syn is built with the `"parsing"` feature.*
David Tolnayf03cdb82017-12-30 00:05:58 -05001178#[macro_export]
1179macro_rules! epsilon {
1180 ($i:expr,) => {
David Tolnayf4aa6b42017-12-31 16:40:33 -05001181 ::std::result::Result::Ok(((), $i))
David Tolnayf03cdb82017-12-30 00:05:58 -05001182 };
1183}
1184
1185/// Run a parser, binding the result to a name, and then evaluating an
1186/// expression.
1187///
1188/// Discards the result of the expression and parser.
1189///
1190/// - **Syntax:** `tap!(NAME : THING => EXPR)`
1191/// - **Output:** `()`
David Tolnayf03cdb82017-12-30 00:05:58 -05001192#[doc(hidden)]
1193#[macro_export]
1194macro_rules! tap {
1195 ($i:expr, $name:ident : $submac:ident!( $($args:tt)* ) => $e:expr) => {
1196 match $submac!($i, $($args)*) {
David Tolnayf4aa6b42017-12-31 16:40:33 -05001197 ::std::result::Result::Ok((o, i)) => {
David Tolnayf03cdb82017-12-30 00:05:58 -05001198 let $name = o;
1199 $e;
David Tolnayf4aa6b42017-12-31 16:40:33 -05001200 ::std::result::Result::Ok(((), i))
David Tolnayf03cdb82017-12-30 00:05:58 -05001201 }
1202 ::std::result::Result::Err(err) =>
1203 ::std::result::Result::Err(err),
1204 }
1205 };
1206
1207 ($i:expr, $name:ident : $f:expr => $e:expr) => {
1208 tap!($i, $name: call!($f) => $e);
1209 };
1210}
1211
David Tolnaydc03aec2017-12-30 01:54:18 -05001212/// Parse any type that implements the `Synom` trait.
David Tolnayf03cdb82017-12-30 00:05:58 -05001213///
David Tolnaydc03aec2017-12-30 01:54:18 -05001214/// Any type implementing [`Synom`] can be used with this parser, whether the
1215/// implementation is provided by Syn or is one that you write.
David Tolnayf03cdb82017-12-30 00:05:58 -05001216///
David Tolnaydc03aec2017-12-30 01:54:18 -05001217/// [`Synom`]: synom/trait.Synom.html
David Tolnay61037c62018-01-05 16:21:03 -08001218///
David Tolnayf03cdb82017-12-30 00:05:58 -05001219/// - **Syntax:** `syn!(TYPE)`
1220/// - **Output:** `TYPE`
1221///
1222/// ```rust
1223/// #[macro_use]
1224/// extern crate syn;
1225///
David Tolnaydc03aec2017-12-30 01:54:18 -05001226/// use syn::{Ident, Item};
1227/// use syn::token::Brace;
1228/// use syn::synom::Synom;
David Tolnayf03cdb82017-12-30 00:05:58 -05001229///
David Tolnaydc03aec2017-12-30 01:54:18 -05001230/// /// Parses a module containing zero or more Rust items.
1231/// ///
1232/// /// Example: `mod m { type Result<T> = ::std::result::Result<T, MyError>; }`
1233/// struct SimpleMod {
1234/// mod_token: Token![mod],
1235/// name: Ident,
1236/// brace_token: Brace,
1237/// items: Vec<Item>,
1238/// }
David Tolnayf03cdb82017-12-30 00:05:58 -05001239///
David Tolnaydc03aec2017-12-30 01:54:18 -05001240/// impl Synom for SimpleMod {
1241/// named!(parse -> Self, do_parse!(
1242/// mod_token: keyword!(mod) >>
1243/// name: syn!(Ident) >>
1244/// body: braces!(many0!(syn!(Item))) >>
1245/// (SimpleMod {
David Tolnayddc5dfa2017-12-31 15:35:07 -05001246/// mod_token,
1247/// name,
David Tolnay8875fca2017-12-31 13:52:37 -05001248/// brace_token: body.0,
1249/// items: body.1,
David Tolnaydc03aec2017-12-30 01:54:18 -05001250/// })
1251/// ));
1252/// }
1253/// #
David Tolnayf03cdb82017-12-30 00:05:58 -05001254/// # fn main() {}
1255/// ```
David Tolnay461d98e2018-01-07 11:07:19 -08001256///
1257/// *This macro is available if Syn is built with the `"parsing"` feature.*
David Tolnayf03cdb82017-12-30 00:05:58 -05001258#[macro_export]
1259macro_rules! syn {
1260 ($i:expr, $t:ty) => {
David Tolnaydc03aec2017-12-30 01:54:18 -05001261 <$t as $crate::synom::Synom>::parse($i)
David Tolnayf03cdb82017-12-30 00:05:58 -05001262 };
1263}
1264
David Tolnaydc03aec2017-12-30 01:54:18 -05001265/// Parse inside of `(` `)` parentheses.
David Tolnayf03cdb82017-12-30 00:05:58 -05001266///
David Tolnaydc03aec2017-12-30 01:54:18 -05001267/// This macro parses a set of balanced parentheses and invokes a sub-parser on
1268/// the content inside. The sub-parser is required to consume all tokens within
1269/// the parentheses in order for this parser to return successfully.
David Tolnayf03cdb82017-12-30 00:05:58 -05001270///
David Tolnay8875fca2017-12-31 13:52:37 -05001271/// - **Syntax:** `parens!(CONTENT)`
Zach Mitchellff82d092018-01-16 21:48:25 -05001272/// - **Output:** `(token::Paren, CONTENT)`
David Tolnayf03cdb82017-12-30 00:05:58 -05001273///
1274/// ```rust
1275/// #[macro_use]
1276/// extern crate syn;
1277///
1278/// use syn::Expr;
1279/// use syn::token::Paren;
1280///
David Tolnaydc03aec2017-12-30 01:54:18 -05001281/// /// Parses an expression inside of parentheses.
1282/// ///
1283/// /// Example: `(1 + 1)`
David Tolnay8875fca2017-12-31 13:52:37 -05001284/// named!(expr_paren -> (Paren, Expr), parens!(syn!(Expr)));
David Tolnaydc03aec2017-12-30 01:54:18 -05001285/// #
David Tolnayf03cdb82017-12-30 00:05:58 -05001286/// # fn main() {}
1287/// ```
David Tolnay461d98e2018-01-07 11:07:19 -08001288///
1289/// *This macro is available if Syn is built with the `"parsing"` feature.*
David Tolnayf03cdb82017-12-30 00:05:58 -05001290#[macro_export]
1291macro_rules! parens {
1292 ($i:expr, $submac:ident!( $($args:tt)* )) => {
1293 $crate::token::Paren::parse($i, |i| $submac!(i, $($args)*))
1294 };
1295
1296 ($i:expr, $f:expr) => {
1297 parens!($i, call!($f));
1298 };
1299}
1300
David Tolnaydc03aec2017-12-30 01:54:18 -05001301/// Parse inside of `[` `]` square brackets.
1302///
1303/// This macro parses a set of balanced brackets and invokes a sub-parser on the
1304/// content inside. The sub-parser is required to consume all tokens within the
1305/// brackets in order for this parser to return successfully.
1306///
David Tolnay8875fca2017-12-31 13:52:37 -05001307/// - **Syntax:** `brackets!(CONTENT)`
1308/// - **Output:** `(token::Bracket, CONTENT)`
David Tolnaydc03aec2017-12-30 01:54:18 -05001309///
1310/// ```rust
1311/// #[macro_use]
1312/// extern crate syn;
1313///
1314/// use syn::Expr;
1315/// use syn::token::Bracket;
1316///
1317/// /// Parses an expression inside of brackets.
1318/// ///
1319/// /// Example: `[1 + 1]`
David Tolnay8875fca2017-12-31 13:52:37 -05001320/// named!(expr_paren -> (Bracket, Expr), brackets!(syn!(Expr)));
David Tolnaydc03aec2017-12-30 01:54:18 -05001321/// #
1322/// # fn main() {}
1323/// ```
David Tolnay461d98e2018-01-07 11:07:19 -08001324///
1325/// *This macro is available if Syn is built with the `"parsing"` feature.*
David Tolnayf03cdb82017-12-30 00:05:58 -05001326#[macro_export]
1327macro_rules! brackets {
1328 ($i:expr, $submac:ident!( $($args:tt)* )) => {
1329 $crate::token::Bracket::parse($i, |i| $submac!(i, $($args)*))
1330 };
1331
1332 ($i:expr, $f:expr) => {
1333 brackets!($i, call!($f));
1334 };
1335}
1336
David Tolnaydc03aec2017-12-30 01:54:18 -05001337/// Parse inside of `{` `}` curly braces.
1338///
1339/// This macro parses a set of balanced braces and invokes a sub-parser on the
1340/// content inside. The sub-parser is required to consume all tokens within the
1341/// braces in order for this parser to return successfully.
1342///
David Tolnay8875fca2017-12-31 13:52:37 -05001343/// - **Syntax:** `braces!(CONTENT)`
1344/// - **Output:** `(token::Brace, CONTENT)`
David Tolnaydc03aec2017-12-30 01:54:18 -05001345///
1346/// ```rust
1347/// #[macro_use]
1348/// extern crate syn;
1349///
1350/// use syn::Expr;
1351/// use syn::token::Brace;
1352///
1353/// /// Parses an expression inside of braces.
1354/// ///
1355/// /// Example: `{1 + 1}`
David Tolnay8875fca2017-12-31 13:52:37 -05001356/// named!(expr_paren -> (Brace, Expr), braces!(syn!(Expr)));
David Tolnaydc03aec2017-12-30 01:54:18 -05001357/// #
1358/// # fn main() {}
1359/// ```
David Tolnay461d98e2018-01-07 11:07:19 -08001360///
1361/// *This macro is available if Syn is built with the `"parsing"` feature.*
David Tolnayf03cdb82017-12-30 00:05:58 -05001362#[macro_export]
1363macro_rules! braces {
1364 ($i:expr, $submac:ident!( $($args:tt)* )) => {
1365 $crate::token::Brace::parse($i, |i| $submac!(i, $($args)*))
1366 };
1367
1368 ($i:expr, $f:expr) => {
1369 braces!($i, call!($f));
1370 };
1371}
1372
David Tolnaydc03aec2017-12-30 01:54:18 -05001373// Not public API.
1374#[doc(hidden)]
David Tolnayf03cdb82017-12-30 00:05:58 -05001375#[macro_export]
1376macro_rules! grouped {
1377 ($i:expr, $submac:ident!( $($args:tt)* )) => {
1378 $crate::token::Group::parse($i, |i| $submac!(i, $($args)*))
1379 };
1380
1381 ($i:expr, $f:expr) => {
1382 grouped!($i, call!($f));
1383 };
1384}