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 | b625418 | 2018-08-25 08:44:54 -0400 | [diff] [blame] | 4 | use 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 | |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 7 | pub struct Parens<'a> { |
| 8 | pub token: token::Paren, |
| 9 | pub content: ParseBuffer<'a>, |
| 10 | } |
| 11 | |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 12 | pub struct Braces<'a> { |
| 13 | pub token: token::Brace, |
| 14 | pub content: ParseBuffer<'a>, |
| 15 | } |
| 16 | |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 17 | pub struct Brackets<'a> { |
| 18 | pub token: token::Bracket, |
| 19 | pub content: ParseBuffer<'a>, |
| 20 | } |
| 21 | |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 22 | impl<'a> ParseBuffer<'a> { |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 23 | fn parse_group(&self, delimiter: Delimiter) -> Result<(Span, ParseBuffer<'a>)> { |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 24 | self.step_cursor(|cursor| { |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 25 | if let Some((content, span, rest)) = cursor.group(delimiter) { |
David Tolnay | eafc805 | 2018-08-25 16:33:53 -0400 | [diff] [blame] | 26 | let content = |
| 27 | ParseBuffer::new(span, cursor.advance(content), self.get_unexpected()); |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 28 | Ok(((span, content), rest)) |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 29 | } else { |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 30 | let message = match delimiter { |
| 31 | Delimiter::Parenthesis => "expected parentheses", |
| 32 | Delimiter::Brace => "expected curly braces", |
| 33 | Delimiter::Bracket => "expected square brackets", |
| 34 | Delimiter::None => unreachable!(), |
| 35 | }; |
| 36 | Err(cursor.error(message)) |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 37 | } |
| 38 | }) |
| 39 | } |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 40 | |
| 41 | // Not public API. |
| 42 | #[doc(hidden)] |
| 43 | pub fn parse_parens(&self) -> Result<Parens<'a>> { |
David Tolnay | 8c39ac5 | 2018-08-25 08:46:21 -0400 | [diff] [blame] | 44 | self.parse_group(Delimiter::Parenthesis) |
| 45 | .map(|(span, content)| Parens { |
| 46 | token: token::Paren(span), |
| 47 | content: content, |
| 48 | }) |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // Not public API. |
| 52 | #[doc(hidden)] |
| 53 | pub fn parse_braces(&self) -> Result<Braces<'a>> { |
David Tolnay | 8c39ac5 | 2018-08-25 08:46:21 -0400 | [diff] [blame] | 54 | self.parse_group(Delimiter::Brace) |
| 55 | .map(|(span, content)| Braces { |
| 56 | token: token::Brace(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_brackets(&self) -> Result<Brackets<'a>> { |
David Tolnay | 8c39ac5 | 2018-08-25 08:46:21 -0400 | [diff] [blame] | 64 | self.parse_group(Delimiter::Bracket) |
| 65 | .map(|(span, content)| Brackets { |
| 66 | token: token::Bracket(span), |
| 67 | content: content, |
| 68 | }) |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | /// Parse a set of parentheses and expose their content to subsequent parsers. |
| 73 | #[macro_export] |
| 74 | macro_rules! parenthesized { |
| 75 | ($content:ident in $cursor:expr) => { |
David Tolnay | b625418 | 2018-08-25 08:44:54 -0400 | [diff] [blame] | 76 | match $crate::parse::ParseBuffer::parse_parens(&$cursor) { |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 77 | $crate::export::Ok(parens) => { |
| 78 | $content = parens.content; |
| 79 | parens.token |
| 80 | } |
| 81 | $crate::export::Err(error) => { |
| 82 | return $crate::export::Err(error); |
| 83 | } |
| 84 | } |
| 85 | }; |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /// Parse a set of curly braces and expose their content to subsequent parsers. |
| 89 | /// |
| 90 | /// ```rust |
David Tolnay | 6d67c74 | 2018-08-24 20:42:39 -0400 | [diff] [blame] | 91 | /// # extern crate syn; |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 92 | /// # |
David Tolnay | b625418 | 2018-08-25 08:44:54 -0400 | [diff] [blame] | 93 | /// use syn::{braced, token, Ident, Token}; |
| 94 | /// use syn::parse::{Parse, ParseStream, Result}; |
David Tolnay | e763533 | 2018-08-26 08:16:18 -0400 | [diff] [blame^] | 95 | /// use syn::punctuated::Punctuated; |
David Tolnay | 6d67c74 | 2018-08-24 20:42:39 -0400 | [diff] [blame] | 96 | /// # |
David Tolnay | e763533 | 2018-08-26 08:16:18 -0400 | [diff] [blame^] | 97 | /// # type Field = Ident; |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 98 | /// |
| 99 | /// // Parse a simplified struct syntax like: |
| 100 | /// // |
| 101 | /// // struct S { |
| 102 | /// // a: A, |
| 103 | /// // b: B, |
| 104 | /// // } |
| 105 | /// struct Struct { |
| 106 | /// pub struct_token: Token![struct], |
| 107 | /// pub ident: Ident, |
| 108 | /// pub brace_token: token::Brace, |
David Tolnay | e763533 | 2018-08-26 08:16:18 -0400 | [diff] [blame^] | 109 | /// pub fields: Punctuated<Field, Token![,]>, |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 110 | /// } |
| 111 | /// |
| 112 | /// impl Parse for Struct { |
| 113 | /// fn parse(input: ParseStream) -> Result<Self> { |
| 114 | /// let content; |
| 115 | /// Ok(Struct { |
| 116 | /// struct_token: input.parse()?, |
| 117 | /// ident: input.parse()?, |
| 118 | /// brace_token: braced!(content in input), |
David Tolnay | e763533 | 2018-08-26 08:16:18 -0400 | [diff] [blame^] | 119 | /// fields: content.parse_terminated(Field::parse)?, |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 120 | /// }) |
| 121 | /// } |
| 122 | /// } |
| 123 | /// ``` |
| 124 | #[macro_export] |
| 125 | macro_rules! braced { |
| 126 | ($content:ident in $cursor:expr) => { |
David Tolnay | b625418 | 2018-08-25 08:44:54 -0400 | [diff] [blame] | 127 | match $crate::parse::ParseBuffer::parse_braces(&$cursor) { |
David Tolnay | 456c982 | 2018-08-25 08:09:46 -0400 | [diff] [blame] | 128 | $crate::export::Ok(braces) => { |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 129 | $content = braces.content; |
| 130 | braces.token |
| 131 | } |
David Tolnay | 456c982 | 2018-08-25 08:09:46 -0400 | [diff] [blame] | 132 | $crate::export::Err(error) => { |
| 133 | return $crate::export::Err(error); |
David Tolnay | 18c754c | 2018-08-21 23:26:58 -0400 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | }; |
| 137 | } |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 138 | |
| 139 | /// Parse a set of square brackets and expose their content to subsequent |
| 140 | /// parsers. |
| 141 | #[macro_export] |
| 142 | macro_rules! bracketed { |
| 143 | ($content:ident in $cursor:expr) => { |
David Tolnay | b625418 | 2018-08-25 08:44:54 -0400 | [diff] [blame] | 144 | match $crate::parse::ParseBuffer::parse_brackets(&$cursor) { |
David Tolnay | 42342c8 | 2018-08-25 08:36:35 -0400 | [diff] [blame] | 145 | $crate::export::Ok(brackets) => { |
| 146 | $content = brackets.content; |
| 147 | brackets.token |
| 148 | } |
| 149 | $crate::export::Err(error) => { |
| 150 | return $crate::export::Err(error); |
| 151 | } |
| 152 | } |
| 153 | }; |
| 154 | } |