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 | aa77a85 | 2018-08-31 11:15:10 -0700 | [diff] [blame] | 4 | use parse::ParseBuffer; |
| 5 | #[cfg(any(feature = "full", feature = "derive"))] |
| 6 | use parse::ParseStream; |
David Tolnay | 94f0663 | 2018-08-31 10:17:17 -0700 | [diff] [blame] | 7 | use private; |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 8 | use token; |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 9 | |
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 | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 15 | pub struct Braces<'a> { |
| 16 | pub token: token::Brace, |
| 17 | pub content: ParseBuffer<'a>, |
| 18 | } |
| 19 | |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 20 | pub struct Brackets<'a> { |
| 21 | pub token: token::Bracket, |
| 22 | pub content: ParseBuffer<'a>, |
| 23 | } |
| 24 | |
David Tolnay | aa77a85 | 2018-08-31 11:15:10 -0700 | [diff] [blame] | 25 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 26 | pub struct Group<'a> { |
| 27 | pub token: token::Group, |
| 28 | pub content: ParseBuffer<'a>, |
| 29 | } |
| 30 | |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 31 | impl<'a> ParseBuffer<'a> { |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 32 | fn parse_delimited(&self, delimiter: Delimiter) -> Result<(Span, ParseBuffer<'a>)> { |
David Tolnay | b50c65a | 2018-08-30 21:14:57 -0700 | [diff] [blame] | 33 | self.step(|cursor| { |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 34 | if let Some((content, span, rest)) = cursor.group(delimiter) { |
David Tolnay | 10951d5 | 2018-08-31 10:27:39 -0700 | [diff] [blame] | 35 | let unexpected = private::get_unexpected(self); |
David Tolnay | 4ac232d | 2018-08-31 10:18:03 -0700 | [diff] [blame] | 36 | let content = |
David Tolnay | 10951d5 | 2018-08-31 10:27:39 -0700 | [diff] [blame] | 37 | private::new_parse_buffer(span, cursor.advance(content), unexpected); |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 38 | Ok(((span, content), rest)) |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 39 | } else { |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 40 | let message = match delimiter { |
| 41 | Delimiter::Parenthesis => "expected parentheses", |
| 42 | Delimiter::Brace => "expected curly braces", |
| 43 | Delimiter::Bracket => "expected square brackets", |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 44 | Delimiter::None => "expected invisible group", |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 45 | }; |
| 46 | Err(cursor.error(message)) |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 47 | } |
| 48 | }) |
| 49 | } |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 50 | |
| 51 | // Not public API. |
| 52 | #[doc(hidden)] |
| 53 | pub fn parse_parens(&self) -> Result<Parens<'a>> { |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 54 | self.parse_delimited(Delimiter::Parenthesis) |
David Tolnay | 8c39ac5 | 2018-08-25 08:46:21 -0400 | [diff] [blame] | 55 | .map(|(span, content)| Parens { |
| 56 | token: token::Paren(span), |
| 57 | content: content, |
| 58 | }) |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // Not public API. |
| 62 | #[doc(hidden)] |
| 63 | pub fn parse_braces(&self) -> Result<Braces<'a>> { |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 64 | self.parse_delimited(Delimiter::Brace) |
David Tolnay | 8c39ac5 | 2018-08-25 08:46:21 -0400 | [diff] [blame] | 65 | .map(|(span, content)| Braces { |
| 66 | token: token::Brace(span), |
| 67 | content: content, |
| 68 | }) |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // Not public API. |
| 72 | #[doc(hidden)] |
| 73 | pub fn parse_brackets(&self) -> Result<Brackets<'a>> { |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 74 | self.parse_delimited(Delimiter::Bracket) |
David Tolnay | 8c39ac5 | 2018-08-25 08:46:21 -0400 | [diff] [blame] | 75 | .map(|(span, content)| Brackets { |
| 76 | token: token::Bracket(span), |
| 77 | content: content, |
| 78 | }) |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 79 | } |
David Tolnay | f57f76f | 2018-08-31 10:23:17 -0700 | [diff] [blame] | 80 | } |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 81 | |
David Tolnay | aa77a85 | 2018-08-31 11:15:10 -0700 | [diff] [blame] | 82 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | 10951d5 | 2018-08-31 10:27:39 -0700 | [diff] [blame] | 83 | impl private { |
David Tolnay | f57f76f | 2018-08-31 10:23:17 -0700 | [diff] [blame] | 84 | pub fn parse_group(input: ParseStream) -> Result<Group> { |
| 85 | input.parse_delimited(Delimiter::None) |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 86 | .map(|(span, content)| Group { |
| 87 | token: token::Group(span), |
| 88 | content: content, |
| 89 | }) |
| 90 | } |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /// Parse a set of parentheses and expose their content to subsequent parsers. |
David Tolnay | 8bb9cca | 2018-09-01 01:50:19 -0700 | [diff] [blame^] | 94 | /// |
| 95 | /// ```rust |
| 96 | /// # #[macro_use] |
| 97 | /// # extern crate quote; |
| 98 | /// # |
| 99 | /// extern crate syn; |
| 100 | /// |
| 101 | /// use syn::{parenthesized, token, Ident, Token, Type}; |
| 102 | /// use syn::parse::{Parse, ParseStream, Result}; |
| 103 | /// 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 Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 136 | #[macro_export] |
| 137 | macro_rules! parenthesized { |
| 138 | ($content:ident in $cursor:expr) => { |
David Tolnay | b625418 | 2018-08-25 08:44:54 -0400 | [diff] [blame] | 139 | match $crate::parse::ParseBuffer::parse_parens(&$cursor) { |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 140 | $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 Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /// Parse a set of curly braces and expose their content to subsequent parsers. |
| 152 | /// |
| 153 | /// ```rust |
David Tolnay | 4496221 | 2018-09-01 01:41:44 -0700 | [diff] [blame] | 154 | /// # extern crate quote; |
| 155 | /// # use quote::quote; |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 156 | /// # |
David Tolnay | 4496221 | 2018-09-01 01:41:44 -0700 | [diff] [blame] | 157 | /// extern crate syn; |
| 158 | /// |
| 159 | /// use syn::{braced, token, Ident, Token, Type}; |
David Tolnay | b625418 | 2018-08-25 08:44:54 -0400 | [diff] [blame] | 160 | /// use syn::parse::{Parse, ParseStream, Result}; |
David Tolnay | e763533 | 2018-08-26 08:16:18 -0400 | [diff] [blame] | 161 | /// use syn::punctuated::Punctuated; |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 162 | /// |
| 163 | /// // Parse a simplified struct syntax like: |
| 164 | /// // |
| 165 | /// // struct S { |
| 166 | /// // a: A, |
| 167 | /// // b: B, |
| 168 | /// // } |
| 169 | /// struct Struct { |
David Tolnay | 4496221 | 2018-09-01 01:41:44 -0700 | [diff] [blame] | 170 | /// 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 Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 180 | /// } |
| 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 Tolnay | e763533 | 2018-08-26 08:16:18 -0400 | [diff] [blame] | 189 | /// fields: content.parse_terminated(Field::parse)?, |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 190 | /// }) |
| 191 | /// } |
| 192 | /// } |
David Tolnay | 4496221 | 2018-09-01 01:41:44 -0700 | [diff] [blame] | 193 | /// |
| 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 Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 213 | /// ``` |
| 214 | #[macro_export] |
| 215 | macro_rules! braced { |
| 216 | ($content:ident in $cursor:expr) => { |
David Tolnay | b625418 | 2018-08-25 08:44:54 -0400 | [diff] [blame] | 217 | match $crate::parse::ParseBuffer::parse_braces(&$cursor) { |
David Tolnay | 456c982 | 2018-08-25 08:09:46 -0400 | [diff] [blame] | 218 | $crate::export::Ok(braces) => { |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 219 | $content = braces.content; |
| 220 | braces.token |
| 221 | } |
David Tolnay | 456c982 | 2018-08-25 08:09:46 -0400 | [diff] [blame] | 222 | $crate::export::Err(error) => { |
| 223 | return $crate::export::Err(error); |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | }; |
| 227 | } |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 228 | |
| 229 | /// Parse a set of square brackets and expose their content to subsequent |
| 230 | /// parsers. |
| 231 | #[macro_export] |
| 232 | macro_rules! bracketed { |
| 233 | ($content:ident in $cursor:expr) => { |
David Tolnay | b625418 | 2018-08-25 08:44:54 -0400 | [diff] [blame] | 234 | match $crate::parse::ParseBuffer::parse_brackets(&$cursor) { |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 235 | $crate::export::Ok(brackets) => { |
| 236 | $content = brackets.content; |
| 237 | brackets.token |
| 238 | } |
| 239 | $crate::export::Err(error) => { |
| 240 | return $crate::export::Err(error); |
| 241 | } |
| 242 | } |
| 243 | }; |
| 244 | } |