David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 1 | use proc_macro2::{Delimiter, Span}; |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 2 | |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 3 | use error::Result; |
David Tolnay | d658dab | 2018-09-01 02:47:42 -0700 | [diff] [blame] | 4 | use parse::{ParseBuffer, ParseStream}; |
David Tolnay | 94f0663 | 2018-08-31 10:17:17 -0700 | [diff] [blame] | 5 | use private; |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 6 | use token; |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 7 | |
David Tolnay | 734079e | 2018-09-01 02:03:37 -0700 | [diff] [blame] | 8 | // Not public API. |
| 9 | #[doc(hidden)] |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 10 | pub struct Parens<'a> { |
| 11 | pub token: token::Paren, |
| 12 | pub content: ParseBuffer<'a>, |
| 13 | } |
| 14 | |
David Tolnay | 734079e | 2018-09-01 02:03:37 -0700 | [diff] [blame] | 15 | // Not public API. |
| 16 | #[doc(hidden)] |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 17 | pub struct Braces<'a> { |
| 18 | pub token: token::Brace, |
| 19 | pub content: ParseBuffer<'a>, |
| 20 | } |
| 21 | |
David Tolnay | 734079e | 2018-09-01 02:03:37 -0700 | [diff] [blame] | 22 | // Not public API. |
| 23 | #[doc(hidden)] |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 24 | pub struct Brackets<'a> { |
| 25 | pub token: token::Bracket, |
| 26 | pub content: ParseBuffer<'a>, |
| 27 | } |
| 28 | |
David Tolnay | 734079e | 2018-09-01 02:03:37 -0700 | [diff] [blame] | 29 | // Not public API. |
David Tolnay | aa77a85 | 2018-08-31 11:15:10 -0700 | [diff] [blame] | 30 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | 734079e | 2018-09-01 02:03:37 -0700 | [diff] [blame] | 31 | #[doc(hidden)] |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 32 | pub struct Group<'a> { |
| 33 | pub token: token::Group, |
| 34 | pub content: ParseBuffer<'a>, |
| 35 | } |
| 36 | |
David Tolnay | 734079e | 2018-09-01 02:03:37 -0700 | [diff] [blame] | 37 | // Not public API. |
| 38 | #[doc(hidden)] |
| 39 | pub fn parse_parens(input: ParseStream) -> Result<Parens> { |
David Tolnay | 70f30e9 | 2018-09-01 02:04:17 -0700 | [diff] [blame] | 40 | parse_delimited(input, Delimiter::Parenthesis).map(|(span, content)| Parens { |
| 41 | token: token::Paren(span), |
| 42 | content: content, |
| 43 | }) |
David Tolnay | 734079e | 2018-09-01 02:03:37 -0700 | [diff] [blame] | 44 | } |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 45 | |
David Tolnay | 734079e | 2018-09-01 02:03:37 -0700 | [diff] [blame] | 46 | // Not public API. |
| 47 | #[doc(hidden)] |
| 48 | pub fn parse_braces(input: ParseStream) -> Result<Braces> { |
David Tolnay | 70f30e9 | 2018-09-01 02:04:17 -0700 | [diff] [blame] | 49 | parse_delimited(input, Delimiter::Brace).map(|(span, content)| Braces { |
| 50 | token: token::Brace(span), |
| 51 | content: content, |
| 52 | }) |
David Tolnay | 734079e | 2018-09-01 02:03:37 -0700 | [diff] [blame] | 53 | } |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 54 | |
David Tolnay | 734079e | 2018-09-01 02:03:37 -0700 | [diff] [blame] | 55 | // Not public API. |
| 56 | #[doc(hidden)] |
| 57 | pub fn parse_brackets(input: ParseStream) -> Result<Brackets> { |
David Tolnay | 70f30e9 | 2018-09-01 02:04:17 -0700 | [diff] [blame] | 58 | parse_delimited(input, Delimiter::Bracket).map(|(span, content)| Brackets { |
| 59 | token: token::Bracket(span), |
| 60 | content: content, |
| 61 | }) |
David Tolnay | f57f76f | 2018-08-31 10:23:17 -0700 | [diff] [blame] | 62 | } |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 63 | |
David Tolnay | aa77a85 | 2018-08-31 11:15:10 -0700 | [diff] [blame] | 64 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | 10951d5 | 2018-08-31 10:27:39 -0700 | [diff] [blame] | 65 | impl private { |
David Tolnay | f57f76f | 2018-08-31 10:23:17 -0700 | [diff] [blame] | 66 | pub fn parse_group(input: ParseStream) -> Result<Group> { |
David Tolnay | 70f30e9 | 2018-09-01 02:04:17 -0700 | [diff] [blame] | 67 | parse_delimited(input, Delimiter::None).map(|(span, content)| Group { |
| 68 | token: token::Group(span), |
| 69 | content: content, |
| 70 | }) |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 71 | } |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 72 | } |
| 73 | |
David Tolnay | 734079e | 2018-09-01 02:03:37 -0700 | [diff] [blame] | 74 | fn 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 Tolnay | 70f30e9 | 2018-09-01 02:04:17 -0700 | [diff] [blame] | 78 | let content = private::new_parse_buffer(span, cursor.advance(content), unexpected); |
David Tolnay | 734079e | 2018-09-01 02:03:37 -0700 | [diff] [blame] | 79 | Ok(((span, content), rest)) |
| 80 | } else { |
| 81 | let message = match delimiter { |
| 82 | Delimiter::Parenthesis => "expected parentheses", |
| 83 | Delimiter::Brace => "expected curly braces", |
| 84 | Delimiter::Bracket => "expected square brackets", |
| 85 | Delimiter::None => "expected invisible group", |
| 86 | }; |
| 87 | Err(cursor.error(message)) |
| 88 | } |
| 89 | }) |
| 90 | } |
| 91 | |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 92 | /// Parse a set of parentheses and expose their content to subsequent parsers. |
David Tolnay | 8bb9cca | 2018-09-01 01:50:19 -0700 | [diff] [blame] | 93 | /// |
David Tolnay | 0e5a4cb | 2018-09-01 01:58:02 -0700 | [diff] [blame] | 94 | /// # Example |
| 95 | /// |
David Tolnay | 8bb9cca | 2018-09-01 01:50:19 -0700 | [diff] [blame] | 96 | /// ```rust |
David Tolnay | 8bb9cca | 2018-09-01 01:50:19 -0700 | [diff] [blame] | 97 | /// # extern crate quote; |
David Tolnay | 9b00f65 | 2018-09-01 10:31:02 -0700 | [diff] [blame] | 98 | /// # extern crate syn; |
David Tolnay | 8bb9cca | 2018-09-01 01:50:19 -0700 | [diff] [blame] | 99 | /// # |
David Tolnay | 9b00f65 | 2018-09-01 10:31:02 -0700 | [diff] [blame] | 100 | /// # use quote::quote; |
| 101 | /// # |
David Tolnay | 8bb9cca | 2018-09-01 01:50:19 -0700 | [diff] [blame] | 102 | /// use syn::{parenthesized, token, Ident, Token, Type}; |
| 103 | /// use syn::parse::{Parse, ParseStream, Result}; |
| 104 | /// use syn::punctuated::Punctuated; |
| 105 | /// |
| 106 | /// // Parse a simplified tuple struct syntax like: |
| 107 | /// // |
| 108 | /// // struct S(A, B); |
| 109 | /// struct TupleStruct { |
| 110 | /// struct_token: Token![struct], |
| 111 | /// ident: Ident, |
| 112 | /// paren_token: token::Paren, |
| 113 | /// fields: Punctuated<Type, Token![,]>, |
| 114 | /// semi_token: Token![;], |
| 115 | /// } |
| 116 | /// |
| 117 | /// impl Parse for TupleStruct { |
| 118 | /// fn parse(input: ParseStream) -> Result<Self> { |
| 119 | /// let content; |
| 120 | /// Ok(TupleStruct { |
| 121 | /// struct_token: input.parse()?, |
| 122 | /// ident: input.parse()?, |
| 123 | /// paren_token: parenthesized!(content in input), |
| 124 | /// fields: content.parse_terminated(Type::parse)?, |
| 125 | /// semi_token: input.parse()?, |
| 126 | /// }) |
| 127 | /// } |
| 128 | /// } |
| 129 | /// # |
| 130 | /// # fn main() { |
| 131 | /// # let input = quote! { |
| 132 | /// # struct S(A, B); |
| 133 | /// # }; |
| 134 | /// # syn::parse2::<TupleStruct>(input).unwrap(); |
| 135 | /// # } |
| 136 | /// ``` |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 137 | #[macro_export] |
| 138 | macro_rules! parenthesized { |
| 139 | ($content:ident in $cursor:expr) => { |
David Tolnay | 734079e | 2018-09-01 02:03:37 -0700 | [diff] [blame] | 140 | match $crate::group::parse_parens(&$cursor) { |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 141 | $crate::export::Ok(parens) => { |
| 142 | $content = parens.content; |
| 143 | parens.token |
| 144 | } |
| 145 | $crate::export::Err(error) => { |
| 146 | return $crate::export::Err(error); |
| 147 | } |
| 148 | } |
| 149 | }; |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /// Parse a set of curly braces and expose their content to subsequent parsers. |
| 153 | /// |
David Tolnay | 0e5a4cb | 2018-09-01 01:58:02 -0700 | [diff] [blame] | 154 | /// # Example |
| 155 | /// |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 156 | /// ```rust |
David Tolnay | 4496221 | 2018-09-01 01:41:44 -0700 | [diff] [blame] | 157 | /// # extern crate quote; |
David Tolnay | 9b00f65 | 2018-09-01 10:31:02 -0700 | [diff] [blame] | 158 | /// # extern crate syn; |
| 159 | /// # |
David Tolnay | 4496221 | 2018-09-01 01:41:44 -0700 | [diff] [blame] | 160 | /// # use quote::quote; |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 161 | /// # |
David Tolnay | 4496221 | 2018-09-01 01:41:44 -0700 | [diff] [blame] | 162 | /// use syn::{braced, token, Ident, Token, Type}; |
David Tolnay | b625418 | 2018-08-25 08:44:54 -0400 | [diff] [blame] | 163 | /// use syn::parse::{Parse, ParseStream, Result}; |
David Tolnay | e763533 | 2018-08-26 08:16:18 -0400 | [diff] [blame] | 164 | /// use syn::punctuated::Punctuated; |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 165 | /// |
| 166 | /// // Parse a simplified struct syntax like: |
| 167 | /// // |
| 168 | /// // struct S { |
| 169 | /// // a: A, |
| 170 | /// // b: B, |
| 171 | /// // } |
| 172 | /// struct Struct { |
David Tolnay | 4496221 | 2018-09-01 01:41:44 -0700 | [diff] [blame] | 173 | /// struct_token: Token![struct], |
| 174 | /// ident: Ident, |
| 175 | /// brace_token: token::Brace, |
| 176 | /// fields: Punctuated<Field, Token![,]>, |
| 177 | /// } |
| 178 | /// |
| 179 | /// struct Field { |
| 180 | /// name: Ident, |
| 181 | /// colon_token: Token![:], |
| 182 | /// ty: Type, |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 183 | /// } |
| 184 | /// |
| 185 | /// impl Parse for Struct { |
| 186 | /// fn parse(input: ParseStream) -> Result<Self> { |
| 187 | /// let content; |
| 188 | /// Ok(Struct { |
| 189 | /// struct_token: input.parse()?, |
| 190 | /// ident: input.parse()?, |
| 191 | /// brace_token: braced!(content in input), |
David Tolnay | e763533 | 2018-08-26 08:16:18 -0400 | [diff] [blame] | 192 | /// fields: content.parse_terminated(Field::parse)?, |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 193 | /// }) |
| 194 | /// } |
| 195 | /// } |
David Tolnay | 4496221 | 2018-09-01 01:41:44 -0700 | [diff] [blame] | 196 | /// |
| 197 | /// impl Parse for Field { |
| 198 | /// fn parse(input: ParseStream) -> Result<Self> { |
| 199 | /// Ok(Field { |
| 200 | /// name: input.parse()?, |
| 201 | /// colon_token: input.parse()?, |
| 202 | /// ty: input.parse()?, |
| 203 | /// }) |
| 204 | /// } |
| 205 | /// } |
| 206 | /// # |
| 207 | /// # fn main() { |
| 208 | /// # let input = quote! { |
| 209 | /// # struct S { |
| 210 | /// # a: A, |
| 211 | /// # b: B, |
| 212 | /// # } |
| 213 | /// # }; |
| 214 | /// # syn::parse2::<Struct>(input).unwrap(); |
| 215 | /// # } |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 216 | /// ``` |
| 217 | #[macro_export] |
| 218 | macro_rules! braced { |
| 219 | ($content:ident in $cursor:expr) => { |
David Tolnay | 734079e | 2018-09-01 02:03:37 -0700 | [diff] [blame] | 220 | match $crate::group::parse_braces(&$cursor) { |
David Tolnay | 456c982 | 2018-08-25 08:09:46 -0400 | [diff] [blame] | 221 | $crate::export::Ok(braces) => { |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 222 | $content = braces.content; |
| 223 | braces.token |
| 224 | } |
David Tolnay | 456c982 | 2018-08-25 08:09:46 -0400 | [diff] [blame] | 225 | $crate::export::Err(error) => { |
| 226 | return $crate::export::Err(error); |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | }; |
| 230 | } |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 231 | |
| 232 | /// Parse a set of square brackets and expose their content to subsequent |
| 233 | /// parsers. |
David Tolnay | 1d2960c | 2018-09-01 01:57:19 -0700 | [diff] [blame] | 234 | /// |
David Tolnay | 0e5a4cb | 2018-09-01 01:58:02 -0700 | [diff] [blame] | 235 | /// # Example |
| 236 | /// |
David Tolnay | 1d2960c | 2018-09-01 01:57:19 -0700 | [diff] [blame] | 237 | /// ```rust |
David Tolnay | 9b00f65 | 2018-09-01 10:31:02 -0700 | [diff] [blame] | 238 | /// # extern crate proc_macro2; |
David Tolnay | 1d2960c | 2018-09-01 01:57:19 -0700 | [diff] [blame] | 239 | /// # extern crate quote; |
David Tolnay | 9b00f65 | 2018-09-01 10:31:02 -0700 | [diff] [blame] | 240 | /// # extern crate syn; |
David Tolnay | 1d2960c | 2018-09-01 01:57:19 -0700 | [diff] [blame] | 241 | /// # |
David Tolnay | 9b00f65 | 2018-09-01 10:31:02 -0700 | [diff] [blame] | 242 | /// # use quote::quote; |
| 243 | /// # |
David Tolnay | 1d2960c | 2018-09-01 01:57:19 -0700 | [diff] [blame] | 244 | /// use proc_macro2::TokenStream; |
| 245 | /// use syn::{bracketed, token, Token}; |
| 246 | /// use syn::parse::{Parse, ParseStream, Result}; |
| 247 | /// |
| 248 | /// // Parse an outer attribute like: |
| 249 | /// // |
| 250 | /// // #[repr(C, packed)] |
| 251 | /// struct OuterAttribute { |
| 252 | /// pound_token: Token![#], |
| 253 | /// bracket_token: token::Bracket, |
| 254 | /// content: TokenStream, |
| 255 | /// } |
| 256 | /// |
| 257 | /// impl Parse for OuterAttribute { |
| 258 | /// fn parse(input: ParseStream) -> Result<Self> { |
| 259 | /// let content; |
| 260 | /// Ok(OuterAttribute { |
| 261 | /// pound_token: input.parse()?, |
| 262 | /// bracket_token: bracketed!(content in input), |
| 263 | /// content: content.parse()?, |
| 264 | /// }) |
| 265 | /// } |
| 266 | /// } |
| 267 | /// # |
| 268 | /// # fn main() { |
| 269 | /// # let input = quote! { |
| 270 | /// # #[repr(C, packed)] |
| 271 | /// # }; |
| 272 | /// # syn::parse2::<OuterAttribute>(input).unwrap(); |
| 273 | /// # } |
| 274 | /// ``` |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 275 | #[macro_export] |
| 276 | macro_rules! bracketed { |
| 277 | ($content:ident in $cursor:expr) => { |
David Tolnay | 734079e | 2018-09-01 02:03:37 -0700 | [diff] [blame] | 278 | match $crate::group::parse_brackets(&$cursor) { |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 279 | $crate::export::Ok(brackets) => { |
| 280 | $content = brackets.content; |
| 281 | brackets.token |
| 282 | } |
| 283 | $crate::export::Err(error) => { |
| 284 | return $crate::export::Err(error); |
| 285 | } |
| 286 | } |
| 287 | }; |
| 288 | } |