blob: 5982a8fc1c0a98661cc4f6099089e8ee3c829643 [file] [log] [blame]
David Tolnay42342c82018-08-25 08:36:35 -04001use proc_macro2::{Delimiter, Span};
David Tolnay18c754c2018-08-21 23:26:58 -04002
David Tolnayad4b2472018-08-25 08:25:24 -04003use error::Result;
David Tolnayd658dab2018-09-01 02:47:42 -07004use parse::{ParseBuffer, ParseStream};
David Tolnay94f06632018-08-31 10:17:17 -07005use private;
David Tolnay776f8e02018-08-24 22:32:10 -04006use token;
David Tolnay18c754c2018-08-21 23:26:58 -04007
David Tolnay734079e2018-09-01 02:03:37 -07008// Not public API.
9#[doc(hidden)]
David Tolnay42342c82018-08-25 08:36:35 -040010pub struct Parens<'a> {
11 pub token: token::Paren,
12 pub content: ParseBuffer<'a>,
13}
14
David Tolnay734079e2018-09-01 02:03:37 -070015// Not public API.
16#[doc(hidden)]
David Tolnay18c754c2018-08-21 23:26:58 -040017pub struct Braces<'a> {
18 pub token: token::Brace,
19 pub content: ParseBuffer<'a>,
20}
21
David Tolnay734079e2018-09-01 02:03:37 -070022// Not public API.
23#[doc(hidden)]
David Tolnay42342c82018-08-25 08:36:35 -040024pub struct Brackets<'a> {
25 pub token: token::Bracket,
26 pub content: ParseBuffer<'a>,
27}
28
David Tolnay734079e2018-09-01 02:03:37 -070029// Not public API.
David Tolnayaa77a852018-08-31 11:15:10 -070030#[cfg(any(feature = "full", feature = "derive"))]
David Tolnay734079e2018-09-01 02:03:37 -070031#[doc(hidden)]
David Tolnaya7d69fc2018-08-26 13:30:24 -040032pub struct Group<'a> {
33 pub token: token::Group,
34 pub content: ParseBuffer<'a>,
35}
36
David Tolnay734079e2018-09-01 02:03:37 -070037// Not public API.
38#[doc(hidden)]
39pub fn parse_parens(input: ParseStream) -> Result<Parens> {
David Tolnay70f30e92018-09-01 02:04:17 -070040 parse_delimited(input, Delimiter::Parenthesis).map(|(span, content)| Parens {
41 token: token::Paren(span),
42 content: content,
43 })
David Tolnay734079e2018-09-01 02:03:37 -070044}
David Tolnay42342c82018-08-25 08:36:35 -040045
David Tolnay734079e2018-09-01 02:03:37 -070046// Not public API.
47#[doc(hidden)]
48pub fn parse_braces(input: ParseStream) -> Result<Braces> {
David Tolnay70f30e92018-09-01 02:04:17 -070049 parse_delimited(input, Delimiter::Brace).map(|(span, content)| Braces {
50 token: token::Brace(span),
51 content: content,
52 })
David Tolnay734079e2018-09-01 02:03:37 -070053}
David Tolnay42342c82018-08-25 08:36:35 -040054
David Tolnay734079e2018-09-01 02:03:37 -070055// Not public API.
56#[doc(hidden)]
57pub fn parse_brackets(input: ParseStream) -> Result<Brackets> {
David Tolnay70f30e92018-09-01 02:04:17 -070058 parse_delimited(input, Delimiter::Bracket).map(|(span, content)| Brackets {
59 token: token::Bracket(span),
60 content: content,
61 })
David Tolnayf57f76f2018-08-31 10:23:17 -070062}
David Tolnaya7d69fc2018-08-26 13:30:24 -040063
David Tolnayaa77a852018-08-31 11:15:10 -070064#[cfg(any(feature = "full", feature = "derive"))]
David Tolnay10951d52018-08-31 10:27:39 -070065impl private {
David Tolnayf57f76f2018-08-31 10:23:17 -070066 pub fn parse_group(input: ParseStream) -> Result<Group> {
David Tolnay70f30e92018-09-01 02:04:17 -070067 parse_delimited(input, Delimiter::None).map(|(span, content)| Group {
68 token: token::Group(span),
69 content: content,
70 })
David Tolnaya7d69fc2018-08-26 13:30:24 -040071 }
David Tolnay42342c82018-08-25 08:36:35 -040072}
73
David Tolnay734079e2018-09-01 02:03:37 -070074fn parse_delimited(input: ParseStream, delimiter: Delimiter) -> Result<(Span, ParseBuffer)> {
75 input.step(|cursor| {
76 if let Some((content, span, rest)) = cursor.group(delimiter) {
77 let unexpected = private::get_unexpected(input);
David Tolnay6ea3fdc2018-09-01 13:30:53 -070078 let nested = private::advance_step_cursor(cursor, content);
79 let content = private::new_parse_buffer(span, nested, unexpected);
David Tolnay734079e2018-09-01 02:03:37 -070080 Ok(((span, content), rest))
81 } else {
82 let message = match delimiter {
83 Delimiter::Parenthesis => "expected parentheses",
84 Delimiter::Brace => "expected curly braces",
85 Delimiter::Bracket => "expected square brackets",
86 Delimiter::None => "expected invisible group",
87 };
88 Err(cursor.error(message))
89 }
90 })
91}
92
David Tolnay42342c82018-08-25 08:36:35 -040093/// Parse a set of parentheses and expose their content to subsequent parsers.
David Tolnay8bb9cca2018-09-01 01:50:19 -070094///
David Tolnay0e5a4cb2018-09-01 01:58:02 -070095/// # Example
96///
David Tolnay95989db2019-01-01 15:05:57 -050097/// ```edition2018
David Tolnay8bb9cca2018-09-01 01:50:19 -070098/// # extern crate quote;
David Tolnayfd5b1172018-12-31 17:54:36 -050099/// # use quote::quote;
David Tolnay8bb9cca2018-09-01 01:50:19 -0700100/// #
David Tolnayfd5b1172018-12-31 17:54:36 -0500101/// use syn::{parenthesized, token, Ident, Result, Token, Type};
David Tolnay67fea042018-11-24 14:50:20 -0800102/// use syn::parse::{Parse, ParseStream};
David Tolnay8bb9cca2018-09-01 01:50:19 -0700103/// use syn::punctuated::Punctuated;
104///
105/// // Parse a simplified tuple struct syntax like:
106/// //
107/// // struct S(A, B);
108/// struct TupleStruct {
109/// struct_token: Token![struct],
110/// ident: Ident,
111/// paren_token: token::Paren,
112/// fields: Punctuated<Type, Token![,]>,
113/// semi_token: Token![;],
114/// }
115///
116/// impl Parse for TupleStruct {
117/// fn parse(input: ParseStream) -> Result<Self> {
118/// let content;
119/// Ok(TupleStruct {
120/// struct_token: input.parse()?,
121/// ident: input.parse()?,
122/// paren_token: parenthesized!(content in input),
123/// fields: content.parse_terminated(Type::parse)?,
124/// semi_token: input.parse()?,
125/// })
126/// }
127/// }
128/// #
129/// # fn main() {
130/// # let input = quote! {
131/// # struct S(A, B);
132/// # };
133/// # syn::parse2::<TupleStruct>(input).unwrap();
134/// # }
135/// ```
David Tolnay42342c82018-08-25 08:36:35 -0400136#[macro_export]
137macro_rules! parenthesized {
138 ($content:ident in $cursor:expr) => {
David Tolnay734079e2018-09-01 02:03:37 -0700139 match $crate::group::parse_parens(&$cursor) {
David Tolnay42342c82018-08-25 08:36:35 -0400140 $crate::export::Ok(parens) => {
141 $content = parens.content;
142 parens.token
143 }
144 $crate::export::Err(error) => {
145 return $crate::export::Err(error);
146 }
147 }
148 };
David Tolnay18c754c2018-08-21 23:26:58 -0400149}
150
151/// Parse a set of curly braces and expose their content to subsequent parsers.
152///
David Tolnay0e5a4cb2018-09-01 01:58:02 -0700153/// # Example
154///
David Tolnay95989db2019-01-01 15:05:57 -0500155/// ```edition2018
David Tolnay44962212018-09-01 01:41:44 -0700156/// # extern crate quote;
David Tolnayfd5b1172018-12-31 17:54:36 -0500157/// # use quote::quote;
David Tolnay9b00f652018-09-01 10:31:02 -0700158/// #
David Tolnayfd5b1172018-12-31 17:54:36 -0500159/// use syn::{braced, token, Ident, Result, Token, Type};
David Tolnay67fea042018-11-24 14:50:20 -0800160/// use syn::parse::{Parse, ParseStream};
David Tolnaye7635332018-08-26 08:16:18 -0400161/// use syn::punctuated::Punctuated;
David Tolnay18c754c2018-08-21 23:26:58 -0400162///
163/// // Parse a simplified struct syntax like:
164/// //
165/// // struct S {
166/// // a: A,
167/// // b: B,
168/// // }
169/// struct Struct {
David Tolnay44962212018-09-01 01:41:44 -0700170/// struct_token: Token![struct],
171/// ident: Ident,
172/// brace_token: token::Brace,
173/// fields: Punctuated<Field, Token![,]>,
174/// }
175///
176/// struct Field {
177/// name: Ident,
178/// colon_token: Token![:],
179/// ty: Type,
David Tolnay18c754c2018-08-21 23:26:58 -0400180/// }
181///
182/// impl Parse for Struct {
183/// fn parse(input: ParseStream) -> Result<Self> {
184/// let content;
185/// Ok(Struct {
186/// struct_token: input.parse()?,
187/// ident: input.parse()?,
188/// brace_token: braced!(content in input),
David Tolnaye7635332018-08-26 08:16:18 -0400189/// fields: content.parse_terminated(Field::parse)?,
David Tolnay18c754c2018-08-21 23:26:58 -0400190/// })
191/// }
192/// }
David Tolnay44962212018-09-01 01:41:44 -0700193///
194/// impl Parse for Field {
195/// fn parse(input: ParseStream) -> Result<Self> {
196/// Ok(Field {
197/// name: input.parse()?,
198/// colon_token: input.parse()?,
199/// ty: input.parse()?,
200/// })
201/// }
202/// }
203/// #
204/// # fn main() {
205/// # let input = quote! {
206/// # struct S {
207/// # a: A,
208/// # b: B,
209/// # }
210/// # };
211/// # syn::parse2::<Struct>(input).unwrap();
212/// # }
David Tolnay18c754c2018-08-21 23:26:58 -0400213/// ```
214#[macro_export]
215macro_rules! braced {
216 ($content:ident in $cursor:expr) => {
David Tolnay734079e2018-09-01 02:03:37 -0700217 match $crate::group::parse_braces(&$cursor) {
David Tolnay456c9822018-08-25 08:09:46 -0400218 $crate::export::Ok(braces) => {
David Tolnay18c754c2018-08-21 23:26:58 -0400219 $content = braces.content;
220 braces.token
221 }
David Tolnay456c9822018-08-25 08:09:46 -0400222 $crate::export::Err(error) => {
223 return $crate::export::Err(error);
David Tolnay18c754c2018-08-21 23:26:58 -0400224 }
225 }
226 };
227}
David Tolnay42342c82018-08-25 08:36:35 -0400228
229/// Parse a set of square brackets and expose their content to subsequent
230/// parsers.
David Tolnay1d2960c2018-09-01 01:57:19 -0700231///
David Tolnay0e5a4cb2018-09-01 01:58:02 -0700232/// # Example
233///
David Tolnay95989db2019-01-01 15:05:57 -0500234/// ```edition2018
David Tolnayfd5b1172018-12-31 17:54:36 -0500235/// # extern crate proc_macro2;
David Tolnay1d2960c2018-09-01 01:57:19 -0700236/// # extern crate quote;
237/// #
David Tolnayfd5b1172018-12-31 17:54:36 -0500238/// # use quote::quote;
239/// #
David Tolnay1d2960c2018-09-01 01:57:19 -0700240/// use proc_macro2::TokenStream;
David Tolnayfd5b1172018-12-31 17:54:36 -0500241/// use syn::{bracketed, token, Result, Token};
David Tolnay67fea042018-11-24 14:50:20 -0800242/// use syn::parse::{Parse, ParseStream};
David Tolnay1d2960c2018-09-01 01:57:19 -0700243///
244/// // Parse an outer attribute like:
245/// //
246/// // #[repr(C, packed)]
247/// struct OuterAttribute {
248/// pound_token: Token![#],
249/// bracket_token: token::Bracket,
250/// content: TokenStream,
251/// }
252///
253/// impl Parse for OuterAttribute {
254/// fn parse(input: ParseStream) -> Result<Self> {
255/// let content;
256/// Ok(OuterAttribute {
257/// pound_token: input.parse()?,
258/// bracket_token: bracketed!(content in input),
259/// content: content.parse()?,
260/// })
261/// }
262/// }
263/// #
264/// # fn main() {
265/// # let input = quote! {
266/// # #[repr(C, packed)]
267/// # };
268/// # syn::parse2::<OuterAttribute>(input).unwrap();
269/// # }
270/// ```
David Tolnay42342c82018-08-25 08:36:35 -0400271#[macro_export]
272macro_rules! bracketed {
273 ($content:ident in $cursor:expr) => {
David Tolnay734079e2018-09-01 02:03:37 -0700274 match $crate::group::parse_brackets(&$cursor) {
David Tolnay42342c82018-08-25 08:36:35 -0400275 $crate::export::Ok(brackets) => {
276 $content = brackets.content;
277 brackets.token
278 }
279 $crate::export::Err(error) => {
280 return $crate::export::Err(error);
281 }
282 }
283 };
284}