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