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