blob: f89d6b9f0a4287a0e764202852955b366e913417 [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 Tolnayaa77a852018-08-31 11:15:10 -07004use parse::ParseBuffer;
5#[cfg(any(feature = "full", feature = "derive"))]
6use parse::ParseStream;
David Tolnay94f06632018-08-31 10:17:17 -07007use private;
David Tolnay776f8e02018-08-24 22:32:10 -04008use token;
David Tolnay18c754c2018-08-21 23:26:58 -04009
David Tolnay42342c82018-08-25 08:36:35 -040010pub struct Parens<'a> {
11 pub token: token::Paren,
12 pub content: ParseBuffer<'a>,
13}
14
David Tolnay18c754c2018-08-21 23:26:58 -040015pub struct Braces<'a> {
16 pub token: token::Brace,
17 pub content: ParseBuffer<'a>,
18}
19
David Tolnay42342c82018-08-25 08:36:35 -040020pub struct Brackets<'a> {
21 pub token: token::Bracket,
22 pub content: ParseBuffer<'a>,
23}
24
David Tolnayaa77a852018-08-31 11:15:10 -070025#[cfg(any(feature = "full", feature = "derive"))]
David Tolnaya7d69fc2018-08-26 13:30:24 -040026pub struct Group<'a> {
27 pub token: token::Group,
28 pub content: ParseBuffer<'a>,
29}
30
David Tolnay18c754c2018-08-21 23:26:58 -040031impl<'a> ParseBuffer<'a> {
David Tolnaya7d69fc2018-08-26 13:30:24 -040032 fn parse_delimited(&self, delimiter: Delimiter) -> Result<(Span, ParseBuffer<'a>)> {
David Tolnayb50c65a2018-08-30 21:14:57 -070033 self.step(|cursor| {
David Tolnay42342c82018-08-25 08:36:35 -040034 if let Some((content, span, rest)) = cursor.group(delimiter) {
David Tolnay10951d52018-08-31 10:27:39 -070035 let unexpected = private::get_unexpected(self);
David Tolnay4ac232d2018-08-31 10:18:03 -070036 let content =
David Tolnay10951d52018-08-31 10:27:39 -070037 private::new_parse_buffer(span, cursor.advance(content), unexpected);
David Tolnay42342c82018-08-25 08:36:35 -040038 Ok(((span, content), rest))
David Tolnay18c754c2018-08-21 23:26:58 -040039 } else {
David Tolnay42342c82018-08-25 08:36:35 -040040 let message = match delimiter {
41 Delimiter::Parenthesis => "expected parentheses",
42 Delimiter::Brace => "expected curly braces",
43 Delimiter::Bracket => "expected square brackets",
David Tolnaya7d69fc2018-08-26 13:30:24 -040044 Delimiter::None => "expected invisible group",
David Tolnay42342c82018-08-25 08:36:35 -040045 };
46 Err(cursor.error(message))
David Tolnay18c754c2018-08-21 23:26:58 -040047 }
48 })
49 }
David Tolnay42342c82018-08-25 08:36:35 -040050
51 // Not public API.
52 #[doc(hidden)]
53 pub fn parse_parens(&self) -> Result<Parens<'a>> {
David Tolnaya7d69fc2018-08-26 13:30:24 -040054 self.parse_delimited(Delimiter::Parenthesis)
David Tolnay8c39ac52018-08-25 08:46:21 -040055 .map(|(span, content)| Parens {
56 token: token::Paren(span),
57 content: content,
58 })
David Tolnay42342c82018-08-25 08:36:35 -040059 }
60
61 // Not public API.
62 #[doc(hidden)]
63 pub fn parse_braces(&self) -> Result<Braces<'a>> {
David Tolnaya7d69fc2018-08-26 13:30:24 -040064 self.parse_delimited(Delimiter::Brace)
David Tolnay8c39ac52018-08-25 08:46:21 -040065 .map(|(span, content)| Braces {
66 token: token::Brace(span),
67 content: content,
68 })
David Tolnay42342c82018-08-25 08:36:35 -040069 }
70
71 // Not public API.
72 #[doc(hidden)]
73 pub fn parse_brackets(&self) -> Result<Brackets<'a>> {
David Tolnaya7d69fc2018-08-26 13:30:24 -040074 self.parse_delimited(Delimiter::Bracket)
David Tolnay8c39ac52018-08-25 08:46:21 -040075 .map(|(span, content)| Brackets {
76 token: token::Bracket(span),
77 content: content,
78 })
David Tolnay42342c82018-08-25 08:36:35 -040079 }
David Tolnayf57f76f2018-08-31 10:23:17 -070080}
David Tolnaya7d69fc2018-08-26 13:30:24 -040081
David Tolnayaa77a852018-08-31 11:15:10 -070082#[cfg(any(feature = "full", feature = "derive"))]
David Tolnay10951d52018-08-31 10:27:39 -070083impl private {
David Tolnayf57f76f2018-08-31 10:23:17 -070084 pub fn parse_group(input: ParseStream) -> Result<Group> {
85 input.parse_delimited(Delimiter::None)
David Tolnaya7d69fc2018-08-26 13:30:24 -040086 .map(|(span, content)| Group {
87 token: token::Group(span),
88 content: content,
89 })
90 }
David Tolnay42342c82018-08-25 08:36:35 -040091}
92
93/// Parse a set of parentheses and expose their content to subsequent parsers.
94#[macro_export]
95macro_rules! parenthesized {
96 ($content:ident in $cursor:expr) => {
David Tolnayb6254182018-08-25 08:44:54 -040097 match $crate::parse::ParseBuffer::parse_parens(&$cursor) {
David Tolnay42342c82018-08-25 08:36:35 -040098 $crate::export::Ok(parens) => {
99 $content = parens.content;
100 parens.token
101 }
102 $crate::export::Err(error) => {
103 return $crate::export::Err(error);
104 }
105 }
106 };
David Tolnay18c754c2018-08-21 23:26:58 -0400107}
108
109/// Parse a set of curly braces and expose their content to subsequent parsers.
110///
111/// ```rust
David Tolnay6d67c742018-08-24 20:42:39 -0400112/// # extern crate syn;
David Tolnay18c754c2018-08-21 23:26:58 -0400113/// #
David Tolnayb6254182018-08-25 08:44:54 -0400114/// use syn::{braced, token, Ident, Token};
115/// use syn::parse::{Parse, ParseStream, Result};
David Tolnaye7635332018-08-26 08:16:18 -0400116/// use syn::punctuated::Punctuated;
David Tolnay6d67c742018-08-24 20:42:39 -0400117/// #
David Tolnaye7635332018-08-26 08:16:18 -0400118/// # type Field = Ident;
David Tolnay18c754c2018-08-21 23:26:58 -0400119///
120/// // Parse a simplified struct syntax like:
121/// //
122/// // struct S {
123/// // a: A,
124/// // b: B,
125/// // }
126/// struct Struct {
127/// pub struct_token: Token![struct],
128/// pub ident: Ident,
129/// pub brace_token: token::Brace,
David Tolnaye7635332018-08-26 08:16:18 -0400130/// pub fields: Punctuated<Field, Token![,]>,
David Tolnay18c754c2018-08-21 23:26:58 -0400131/// }
132///
133/// impl Parse for Struct {
134/// fn parse(input: ParseStream) -> Result<Self> {
135/// let content;
136/// Ok(Struct {
137/// struct_token: input.parse()?,
138/// ident: input.parse()?,
139/// brace_token: braced!(content in input),
David Tolnaye7635332018-08-26 08:16:18 -0400140/// fields: content.parse_terminated(Field::parse)?,
David Tolnay18c754c2018-08-21 23:26:58 -0400141/// })
142/// }
143/// }
144/// ```
145#[macro_export]
146macro_rules! braced {
147 ($content:ident in $cursor:expr) => {
David Tolnayb6254182018-08-25 08:44:54 -0400148 match $crate::parse::ParseBuffer::parse_braces(&$cursor) {
David Tolnay456c9822018-08-25 08:09:46 -0400149 $crate::export::Ok(braces) => {
David Tolnay18c754c2018-08-21 23:26:58 -0400150 $content = braces.content;
151 braces.token
152 }
David Tolnay456c9822018-08-25 08:09:46 -0400153 $crate::export::Err(error) => {
154 return $crate::export::Err(error);
David Tolnay18c754c2018-08-21 23:26:58 -0400155 }
156 }
157 };
158}
David Tolnay42342c82018-08-25 08:36:35 -0400159
160/// Parse a set of square brackets and expose their content to subsequent
161/// parsers.
162#[macro_export]
163macro_rules! bracketed {
164 ($content:ident in $cursor:expr) => {
David Tolnayb6254182018-08-25 08:44:54 -0400165 match $crate::parse::ParseBuffer::parse_brackets(&$cursor) {
David Tolnay42342c82018-08-25 08:36:35 -0400166 $crate::export::Ok(brackets) => {
167 $content = brackets.content;
168 brackets.token
169 }
170 $crate::export::Err(error) => {
171 return $crate::export::Err(error);
172 }
173 }
174 };
175}