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