blob: 4aa8cef760e9f217515fd220ec84c06b75d87e41 [file] [log] [blame]
David Tolnay42342c82018-08-25 08:36:35 -04001use proc_macro2::{Delimiter, Span};
David Tolnay18c754c2018-08-21 23:26:58 -04002
David Tolnayad4b2472018-08-25 08:25:24 -04003use error::Result;
David Tolnayb6254182018-08-25 08:44:54 -04004use parse::ParseBuffer;
David Tolnay94f06632018-08-31 10:17:17 -07005use private;
David Tolnay776f8e02018-08-24 22:32:10 -04006use token;
David Tolnay18c754c2018-08-21 23:26:58 -04007
David Tolnay42342c82018-08-25 08:36:35 -04008pub struct Parens<'a> {
9 pub token: token::Paren,
10 pub content: ParseBuffer<'a>,
11}
12
David Tolnay18c754c2018-08-21 23:26:58 -040013pub struct Braces<'a> {
14 pub token: token::Brace,
15 pub content: ParseBuffer<'a>,
16}
17
David Tolnay42342c82018-08-25 08:36:35 -040018pub struct Brackets<'a> {
19 pub token: token::Bracket,
20 pub content: ParseBuffer<'a>,
21}
22
David Tolnaya7d69fc2018-08-26 13:30:24 -040023pub struct Group<'a> {
24 pub token: token::Group,
25 pub content: ParseBuffer<'a>,
26}
27
David Tolnay18c754c2018-08-21 23:26:58 -040028impl<'a> ParseBuffer<'a> {
David Tolnaya7d69fc2018-08-26 13:30:24 -040029 fn parse_delimited(&self, delimiter: Delimiter) -> Result<(Span, ParseBuffer<'a>)> {
David Tolnayb50c65a2018-08-30 21:14:57 -070030 self.step(|cursor| {
David Tolnay42342c82018-08-25 08:36:35 -040031 if let Some((content, span, rest)) = cursor.group(delimiter) {
David Tolnay94f06632018-08-31 10:17:17 -070032 let unexpected = private::<ParseBuffer>::get_unexpected(self);
33 let content = private::<ParseBuffer>::new(span, cursor.advance(content), unexpected);
David Tolnay42342c82018-08-25 08:36:35 -040034 Ok(((span, content), rest))
David Tolnay18c754c2018-08-21 23:26:58 -040035 } else {
David Tolnay42342c82018-08-25 08:36:35 -040036 let message = match delimiter {
37 Delimiter::Parenthesis => "expected parentheses",
38 Delimiter::Brace => "expected curly braces",
39 Delimiter::Bracket => "expected square brackets",
David Tolnaya7d69fc2018-08-26 13:30:24 -040040 Delimiter::None => "expected invisible group",
David Tolnay42342c82018-08-25 08:36:35 -040041 };
42 Err(cursor.error(message))
David Tolnay18c754c2018-08-21 23:26:58 -040043 }
44 })
45 }
David Tolnay42342c82018-08-25 08:36:35 -040046
47 // Not public API.
48 #[doc(hidden)]
49 pub fn parse_parens(&self) -> Result<Parens<'a>> {
David Tolnaya7d69fc2018-08-26 13:30:24 -040050 self.parse_delimited(Delimiter::Parenthesis)
David Tolnay8c39ac52018-08-25 08:46:21 -040051 .map(|(span, content)| Parens {
52 token: token::Paren(span),
53 content: content,
54 })
David Tolnay42342c82018-08-25 08:36:35 -040055 }
56
57 // Not public API.
58 #[doc(hidden)]
59 pub fn parse_braces(&self) -> Result<Braces<'a>> {
David Tolnaya7d69fc2018-08-26 13:30:24 -040060 self.parse_delimited(Delimiter::Brace)
David Tolnay8c39ac52018-08-25 08:46:21 -040061 .map(|(span, content)| Braces {
62 token: token::Brace(span),
63 content: content,
64 })
David Tolnay42342c82018-08-25 08:36:35 -040065 }
66
67 // Not public API.
68 #[doc(hidden)]
69 pub fn parse_brackets(&self) -> Result<Brackets<'a>> {
David Tolnaya7d69fc2018-08-26 13:30:24 -040070 self.parse_delimited(Delimiter::Bracket)
David Tolnay8c39ac52018-08-25 08:46:21 -040071 .map(|(span, content)| Brackets {
72 token: token::Bracket(span),
73 content: content,
74 })
David Tolnay42342c82018-08-25 08:36:35 -040075 }
David Tolnaya7d69fc2018-08-26 13:30:24 -040076
77 // Not public API.
78 #[doc(hidden)]
79 pub fn parse_group(&self) -> Result<Group<'a>> {
80 self.parse_delimited(Delimiter::None)
81 .map(|(span, content)| Group {
82 token: token::Group(span),
83 content: content,
84 })
85 }
David Tolnay42342c82018-08-25 08:36:35 -040086}
87
88/// Parse a set of parentheses and expose their content to subsequent parsers.
89#[macro_export]
90macro_rules! parenthesized {
91 ($content:ident in $cursor:expr) => {
David Tolnayb6254182018-08-25 08:44:54 -040092 match $crate::parse::ParseBuffer::parse_parens(&$cursor) {
David Tolnay42342c82018-08-25 08:36:35 -040093 $crate::export::Ok(parens) => {
94 $content = parens.content;
95 parens.token
96 }
97 $crate::export::Err(error) => {
98 return $crate::export::Err(error);
99 }
100 }
101 };
David Tolnay18c754c2018-08-21 23:26:58 -0400102}
103
104/// Parse a set of curly braces and expose their content to subsequent parsers.
105///
106/// ```rust
David Tolnay6d67c742018-08-24 20:42:39 -0400107/// # extern crate syn;
David Tolnay18c754c2018-08-21 23:26:58 -0400108/// #
David Tolnayb6254182018-08-25 08:44:54 -0400109/// use syn::{braced, token, Ident, Token};
110/// use syn::parse::{Parse, ParseStream, Result};
David Tolnaye7635332018-08-26 08:16:18 -0400111/// use syn::punctuated::Punctuated;
David Tolnay6d67c742018-08-24 20:42:39 -0400112/// #
David Tolnaye7635332018-08-26 08:16:18 -0400113/// # type Field = Ident;
David Tolnay18c754c2018-08-21 23:26:58 -0400114///
115/// // Parse a simplified struct syntax like:
116/// //
117/// // struct S {
118/// // a: A,
119/// // b: B,
120/// // }
121/// struct Struct {
122/// pub struct_token: Token![struct],
123/// pub ident: Ident,
124/// pub brace_token: token::Brace,
David Tolnaye7635332018-08-26 08:16:18 -0400125/// pub fields: Punctuated<Field, Token![,]>,
David Tolnay18c754c2018-08-21 23:26:58 -0400126/// }
127///
128/// impl Parse for Struct {
129/// fn parse(input: ParseStream) -> Result<Self> {
130/// let content;
131/// Ok(Struct {
132/// struct_token: input.parse()?,
133/// ident: input.parse()?,
134/// brace_token: braced!(content in input),
David Tolnaye7635332018-08-26 08:16:18 -0400135/// fields: content.parse_terminated(Field::parse)?,
David Tolnay18c754c2018-08-21 23:26:58 -0400136/// })
137/// }
138/// }
139/// ```
140#[macro_export]
141macro_rules! braced {
142 ($content:ident in $cursor:expr) => {
David Tolnayb6254182018-08-25 08:44:54 -0400143 match $crate::parse::ParseBuffer::parse_braces(&$cursor) {
David Tolnay456c9822018-08-25 08:09:46 -0400144 $crate::export::Ok(braces) => {
David Tolnay18c754c2018-08-21 23:26:58 -0400145 $content = braces.content;
146 braces.token
147 }
David Tolnay456c9822018-08-25 08:09:46 -0400148 $crate::export::Err(error) => {
149 return $crate::export::Err(error);
David Tolnay18c754c2018-08-21 23:26:58 -0400150 }
151 }
152 };
153}
David Tolnay42342c82018-08-25 08:36:35 -0400154
155/// Parse a set of square brackets and expose their content to subsequent
156/// parsers.
157#[macro_export]
158macro_rules! bracketed {
159 ($content:ident in $cursor:expr) => {
David Tolnayb6254182018-08-25 08:44:54 -0400160 match $crate::parse::ParseBuffer::parse_brackets(&$cursor) {
David Tolnay42342c82018-08-25 08:36:35 -0400161 $crate::export::Ok(brackets) => {
162 $content = brackets.content;
163 brackets.token
164 }
165 $crate::export::Err(error) => {
166 return $crate::export::Err(error);
167 }
168 }
169 };
170}
David Tolnaya7d69fc2018-08-26 13:30:24 -0400171
172#[doc(hidden)]
173#[macro_export]
174macro_rules! grouped {
175 ($content:ident in $cursor:expr) => {
176 match $crate::parse::ParseBuffer::parse_group(&$cursor) {
177 $crate::export::Ok(group) => {
178 $content = group.content;
179 group.token
180 }
181 $crate::export::Err(error) => {
182 return $crate::export::Err(error);
183 }
184 }
185 };
186}