David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 1 | use proc_macro2::Delimiter; |
| 2 | |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 3 | use error::Result; |
David Tolnay | 7c6f21b | 2018-08-25 08:28:55 -0400 | [diff] [blame^] | 4 | use next::parse::ParseBuffer; |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 5 | use token; |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 6 | |
| 7 | pub struct Braces<'a> { |
| 8 | pub token: token::Brace, |
| 9 | pub content: ParseBuffer<'a>, |
| 10 | } |
| 11 | |
| 12 | impl<'a> ParseBuffer<'a> { |
| 13 | // Not public API. |
| 14 | #[doc(hidden)] |
| 15 | pub fn parse_braces(&self) -> Result<Braces<'a>> { |
| 16 | self.step_cursor(|cursor| { |
| 17 | if let Some((content, span, rest)) = cursor.group(Delimiter::Brace) { |
| 18 | let braces = Braces { |
| 19 | token: token::Brace(span), |
| 20 | content: ParseBuffer::new(span, cursor.advance(content)), |
| 21 | }; |
| 22 | Ok((braces, rest)) |
| 23 | } else { |
| 24 | Err(cursor.error("expected curly braces")) |
| 25 | } |
| 26 | }) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | /// Parse a set of curly braces and expose their content to subsequent parsers. |
| 31 | /// |
| 32 | /// ```rust |
David Tolnay | 6d67c74 | 2018-08-24 20:42:39 -0400 | [diff] [blame] | 33 | /// # extern crate syn; |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 34 | /// # |
David Tolnay | 6d67c74 | 2018-08-24 20:42:39 -0400 | [diff] [blame] | 35 | /// use syn::{braced, Token}; |
| 36 | /// use syn::next::{token, Ident}; |
| 37 | /// use syn::next::parse::{Parse, ParseStream, Result}; |
| 38 | /// # |
| 39 | /// # mod example { |
| 40 | /// # use super::{syn, braced, token, Ident, Parse, ParseStream, Result}; |
| 41 | /// # |
| 42 | /// # macro_rules! Token { |
| 43 | /// # (struct) => { |
| 44 | /// # syn::next::token::Struct |
| 45 | /// # }; |
| 46 | /// # } |
| 47 | /// # |
| 48 | /// # type Field = Ident; |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 49 | /// |
| 50 | /// // Parse a simplified struct syntax like: |
| 51 | /// // |
| 52 | /// // struct S { |
| 53 | /// // a: A, |
| 54 | /// // b: B, |
| 55 | /// // } |
| 56 | /// struct Struct { |
| 57 | /// pub struct_token: Token![struct], |
| 58 | /// pub ident: Ident, |
| 59 | /// pub brace_token: token::Brace, |
| 60 | /// pub fields: Vec<Field>, |
| 61 | /// } |
| 62 | /// |
| 63 | /// impl Parse for Struct { |
| 64 | /// fn parse(input: ParseStream) -> Result<Self> { |
| 65 | /// let content; |
| 66 | /// Ok(Struct { |
| 67 | /// struct_token: input.parse()?, |
| 68 | /// ident: input.parse()?, |
| 69 | /// brace_token: braced!(content in input), |
| 70 | /// fields: content.parse()?, |
| 71 | /// }) |
| 72 | /// } |
| 73 | /// } |
David Tolnay | 6d67c74 | 2018-08-24 20:42:39 -0400 | [diff] [blame] | 74 | /// # } |
| 75 | /// # |
| 76 | /// # fn main() {} |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 77 | /// ``` |
| 78 | #[macro_export] |
| 79 | macro_rules! braced { |
| 80 | ($content:ident in $cursor:expr) => { |
David Tolnay | 544a90f | 2018-08-24 20:34:03 -0400 | [diff] [blame] | 81 | match $crate::next::parse::ParseBuffer::parse_braces(&$cursor) { |
David Tolnay | 456c982 | 2018-08-25 08:09:46 -0400 | [diff] [blame] | 82 | $crate::export::Ok(braces) => { |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 83 | $content = braces.content; |
| 84 | braces.token |
| 85 | } |
David Tolnay | 456c982 | 2018-08-25 08:09:46 -0400 | [diff] [blame] | 86 | $crate::export::Err(error) => { |
| 87 | return $crate::export::Err(error); |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | }; |
| 91 | } |