blob: 292e0dafba1bac9a3b87ebfb50f1b3a1bb9836c4 [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// Copyright 2018 Syn Developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
David Tolnaye79ae182018-01-06 19:23:37 -08009//! Tokens representing Rust punctuation, keywords, and delimiters.
Alex Crichton954046c2017-05-30 21:49:42 -070010//!
David Tolnaye79ae182018-01-06 19:23:37 -080011//! The type names in this module can be difficult to keep straight, so we
12//! prefer to use the [`Token!`] macro instead. This is a type-macro that
13//! expands to the token type of the given token.
14//!
15//! [`Token!`]: ../macro.Token.html
16//!
17//! # Example
18//!
19//! The [`ItemStatic`] syntax tree node is defined like this.
20//!
21//! [`ItemStatic`]: ../struct.ItemStatic.html
22//!
23//! ```
24//! # #[macro_use]
25//! # extern crate syn;
Alex Crichtonf0fea9a2018-05-17 11:19:34 -070026//! # extern crate proc_macro2;
David Tolnaye79ae182018-01-06 19:23:37 -080027//! #
Alex Crichtonf0fea9a2018-05-17 11:19:34 -070028//! # use proc_macro2::Ident;
29//! # use syn::{Attribute, Visibility, Type, Expr};
David Tolnaye79ae182018-01-06 19:23:37 -080030//! #
31//! pub struct ItemStatic {
32//! pub attrs: Vec<Attribute>,
33//! pub vis: Visibility,
34//! pub static_token: Token![static],
35//! pub mutability: Option<Token![mut]>,
36//! pub ident: Ident,
37//! pub colon_token: Token![:],
38//! pub ty: Box<Type>,
39//! pub eq_token: Token![=],
40//! pub expr: Box<Expr>,
41//! pub semi_token: Token![;],
42//! }
43//! #
44//! # fn main() {}
45//! ```
46//!
47//! # Parsing
48//!
49//! These tokens can be parsed using the [`Synom`] trait and the parser
50//! combinator macros [`punct!`], [`keyword!`], [`parens!`], [`braces!`], and
51//! [`brackets!`].
52//!
53//! [`Synom`]: ../synom/trait.Synom.html
54//! [`punct!`]: ../macro.punct.html
55//! [`keyword!`]: ../macro.keyword.html
56//! [`parens!`]: ../macro.parens.html
57//! [`braces!`]: ../macro.braces.html
58//! [`brackets!`]: ../macro.brackets.html
59//!
60//! ```
61//! #[macro_use]
62//! extern crate syn;
Alex Crichtonf0fea9a2018-05-17 11:19:34 -070063//! extern crate proc_macro2;
David Tolnaye79ae182018-01-06 19:23:37 -080064//!
Alex Crichtonf0fea9a2018-05-17 11:19:34 -070065//! use proc_macro2::Ident;
David Tolnaye79ae182018-01-06 19:23:37 -080066//! use syn::synom::Synom;
Alex Crichtonf0fea9a2018-05-17 11:19:34 -070067//! use syn::{Attribute, Visibility, Type, Expr};
David Tolnaye79ae182018-01-06 19:23:37 -080068//! #
69//! # struct ItemStatic;
70//! # use syn::ItemStatic as SynItemStatic;
71//!
72//! // Parse the ItemStatic struct shown above.
73//! impl Synom for ItemStatic {
74//! named!(parse -> Self, do_parse!(
75//! # (ItemStatic)
76//! # ));
77//! # }
78//! #
79//! # mod example {
80//! # use super::*;
81//! # use super::SynItemStatic as ItemStatic;
82//! #
83//! # named!(parse -> ItemStatic, do_parse!(
84//! attrs: many0!(Attribute::parse_outer) >>
85//! vis: syn!(Visibility) >>
86//! static_token: keyword!(static) >>
87//! mutability: option!(keyword!(mut)) >>
88//! ident: syn!(Ident) >>
89//! colon_token: punct!(:) >>
90//! ty: syn!(Type) >>
91//! eq_token: punct!(=) >>
92//! expr: syn!(Expr) >>
93//! semi_token: punct!(;) >>
94//! (ItemStatic {
95//! attrs, vis, static_token, mutability, ident, colon_token,
96//! ty: Box::new(ty), eq_token, expr: Box::new(expr), semi_token,
97//! })
98//! ));
99//! }
100//! #
101//! # fn main() {}
102//! ```
Alex Crichton954046c2017-05-30 21:49:42 -0700103
Alex Crichtona74a1c82018-05-16 10:20:44 -0700104use proc_macro2::{Span, Ident};
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700105
106macro_rules! tokens {
107 (
David Tolnay73c98de2017-12-31 15:56:56 -0500108 punct: {
109 $($punct:tt pub struct $punct_name:ident/$len:tt #[$punct_doc:meta])*
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700110 }
David Tolnay73c98de2017-12-31 15:56:56 -0500111 delimiter: {
112 $($delimiter:tt pub struct $delimiter_name:ident #[$delimiter_doc:meta])*
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700113 }
David Tolnay73c98de2017-12-31 15:56:56 -0500114 keyword: {
115 $($keyword:tt pub struct $keyword_name:ident #[$keyword_doc:meta])*
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700116 }
117 ) => (
Sergio Benitezd14d5362018-04-28 15:38:25 -0700118 $(token_punct_def! { #[$punct_doc] $punct pub struct $punct_name/$len })*
119 $(token_punct_parser! { $punct pub struct $punct_name })*
David Tolnay73c98de2017-12-31 15:56:56 -0500120 $(token_delimiter! { #[$delimiter_doc] $delimiter pub struct $delimiter_name })*
121 $(token_keyword! { #[$keyword_doc] $keyword pub struct $keyword_name })*
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700122 )
123}
124
Sergio Benitezd14d5362018-04-28 15:38:25 -0700125macro_rules! token_punct_def {
David Tolnay94d2b792018-04-29 12:26:10 -0700126 (#[$doc:meta] $s:tt pub struct $name:ident / $len:tt) => {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700127 #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))]
David Tolnay5a20f632017-12-26 22:11:28 -0500128 #[$doc]
David Tolnay1bb760c2018-01-07 11:18:15 -0800129 ///
130 /// Don't try to remember the name of this type -- use the [`Token!`]
131 /// macro instead.
132 ///
133 /// [`Token!`]: index.html
David Tolnay5a20f632017-12-26 22:11:28 -0500134 pub struct $name(pub [Span; $len]);
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700135
David Tolnay0bdb0552017-12-27 21:31:51 -0500136 impl $name {
137 pub fn new(span: Span) -> Self {
138 $name([span; $len])
139 }
140 }
141
David Tolnay66bb8d52018-01-08 08:22:31 -0800142 impl ::std::default::Default for $name {
143 fn default() -> Self {
Alex Crichton9a4dca22018-03-28 06:32:19 -0700144 $name([Span::call_site(); $len])
David Tolnay66bb8d52018-01-08 08:22:31 -0800145 }
146 }
147
Nika Layzelld73a3652017-10-24 08:57:05 -0400148 #[cfg(feature = "extra-traits")]
149 impl ::std::fmt::Debug for $name {
150 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
David Tolnay98942562017-12-26 21:24:35 -0500151 f.write_str(stringify!($name))
Nika Layzelld73a3652017-10-24 08:57:05 -0400152 }
153 }
154
David Tolnay98942562017-12-26 21:24:35 -0500155 #[cfg(feature = "extra-traits")]
156 impl ::std::cmp::Eq for $name {}
157
158 #[cfg(feature = "extra-traits")]
159 impl ::std::cmp::PartialEq for $name {
160 fn eq(&self, _other: &$name) -> bool {
161 true
162 }
163 }
164
165 #[cfg(feature = "extra-traits")]
166 impl ::std::hash::Hash for $name {
167 fn hash<H>(&self, _state: &mut H)
David Tolnay94d2b792018-04-29 12:26:10 -0700168 where
169 H: ::std::hash::Hasher,
170 {
171 }
David Tolnay98942562017-12-26 21:24:35 -0500172 }
173
Sergio Benitezd14d5362018-04-28 15:38:25 -0700174 impl From<Span> for $name {
175 fn from(span: Span) -> Self {
176 $name([span; $len])
177 }
178 }
David Tolnay94d2b792018-04-29 12:26:10 -0700179 };
Sergio Benitezd14d5362018-04-28 15:38:25 -0700180}
181
182macro_rules! token_punct_parser {
183 ($s:tt pub struct $name:ident) => {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700184 #[cfg(feature = "printing")]
185 impl ::quote::ToTokens for $name {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700186 fn to_tokens(&self, tokens: &mut ::proc_macro2::TokenStream) {
David Tolnay73c98de2017-12-31 15:56:56 -0500187 printing::punct($s, &self.0, tokens);
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700188 }
189 }
190
191 #[cfg(feature = "parsing")]
192 impl ::Synom for $name {
David Tolnaydfc886b2018-01-06 08:03:09 -0800193 fn parse(tokens: $crate::buffer::Cursor) -> $crate::synom::PResult<$name> {
David Tolnay73c98de2017-12-31 15:56:56 -0500194 parsing::punct($s, tokens, $name)
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700195 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800196
197 fn description() -> Option<&'static str> {
198 Some(concat!("`", $s, "`"))
199 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700200 }
David Tolnay94d2b792018-04-29 12:26:10 -0700201 };
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700202}
203
David Tolnay73c98de2017-12-31 15:56:56 -0500204macro_rules! token_keyword {
David Tolnay5a20f632017-12-26 22:11:28 -0500205 (#[$doc:meta] $s:tt pub struct $name:ident) => {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700206 #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))]
David Tolnay5a20f632017-12-26 22:11:28 -0500207 #[$doc]
David Tolnay1bb760c2018-01-07 11:18:15 -0800208 ///
209 /// Don't try to remember the name of this type -- use the [`Token!`]
210 /// macro instead.
211 ///
212 /// [`Token!`]: index.html
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700213 pub struct $name(pub Span);
214
David Tolnay66bb8d52018-01-08 08:22:31 -0800215 impl ::std::default::Default for $name {
216 fn default() -> Self {
Alex Crichton9a4dca22018-03-28 06:32:19 -0700217 $name(Span::call_site())
David Tolnay66bb8d52018-01-08 08:22:31 -0800218 }
219 }
220
David Tolnay98942562017-12-26 21:24:35 -0500221 #[cfg(feature = "extra-traits")]
222 impl ::std::fmt::Debug for $name {
223 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
224 f.write_str(stringify!($name))
225 }
226 }
227
228 #[cfg(feature = "extra-traits")]
229 impl ::std::cmp::Eq for $name {}
230
231 #[cfg(feature = "extra-traits")]
232 impl ::std::cmp::PartialEq for $name {
233 fn eq(&self, _other: &$name) -> bool {
234 true
235 }
236 }
237
238 #[cfg(feature = "extra-traits")]
239 impl ::std::hash::Hash for $name {
240 fn hash<H>(&self, _state: &mut H)
David Tolnay94d2b792018-04-29 12:26:10 -0700241 where
242 H: ::std::hash::Hasher,
243 {
244 }
David Tolnay98942562017-12-26 21:24:35 -0500245 }
246
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700247 #[cfg(feature = "printing")]
248 impl ::quote::ToTokens for $name {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700249 fn to_tokens(&self, tokens: &mut ::proc_macro2::TokenStream) {
David Tolnay73c98de2017-12-31 15:56:56 -0500250 printing::keyword($s, &self.0, tokens);
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700251 }
252 }
253
254 #[cfg(feature = "parsing")]
255 impl ::Synom for $name {
David Tolnaydfc886b2018-01-06 08:03:09 -0800256 fn parse(tokens: $crate::buffer::Cursor) -> $crate::synom::PResult<$name> {
David Tolnay73c98de2017-12-31 15:56:56 -0500257 parsing::keyword($s, tokens, $name)
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700258 }
David Tolnay79777332018-01-07 10:04:42 -0800259
260 fn description() -> Option<&'static str> {
261 Some(concat!("`", $s, "`"))
262 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700263 }
강동윤d5229da2018-01-12 13:11:33 +0900264
Alex Crichtona74a1c82018-05-16 10:20:44 -0700265 impl From<$name> for Ident {
266 fn from(me: $name) -> Ident {
267 Ident::new($s, me.0)
268 }
269 }
270
강동윤d5229da2018-01-12 13:11:33 +0900271 impl From<Span> for $name {
272 fn from(span: Span) -> Self {
273 $name(span)
274 }
275 }
David Tolnay94d2b792018-04-29 12:26:10 -0700276 };
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700277}
278
David Tolnay73c98de2017-12-31 15:56:56 -0500279macro_rules! token_delimiter {
David Tolnay5a20f632017-12-26 22:11:28 -0500280 (#[$doc:meta] $s:tt pub struct $name:ident) => {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700281 #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))]
David Tolnay5a20f632017-12-26 22:11:28 -0500282 #[$doc]
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700283 pub struct $name(pub Span);
284
David Tolnay66bb8d52018-01-08 08:22:31 -0800285 impl ::std::default::Default for $name {
286 fn default() -> Self {
Alex Crichton9a4dca22018-03-28 06:32:19 -0700287 $name(Span::call_site())
David Tolnay66bb8d52018-01-08 08:22:31 -0800288 }
289 }
290
David Tolnay98942562017-12-26 21:24:35 -0500291 #[cfg(feature = "extra-traits")]
292 impl ::std::fmt::Debug for $name {
293 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
294 f.write_str(stringify!($name))
295 }
296 }
297
298 #[cfg(feature = "extra-traits")]
299 impl ::std::cmp::Eq for $name {}
300
301 #[cfg(feature = "extra-traits")]
302 impl ::std::cmp::PartialEq for $name {
303 fn eq(&self, _other: &$name) -> bool {
304 true
305 }
306 }
307
308 #[cfg(feature = "extra-traits")]
309 impl ::std::hash::Hash for $name {
310 fn hash<H>(&self, _state: &mut H)
David Tolnay94d2b792018-04-29 12:26:10 -0700311 where
312 H: ::std::hash::Hasher,
313 {
314 }
David Tolnay98942562017-12-26 21:24:35 -0500315 }
316
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700317 impl $name {
318 #[cfg(feature = "printing")]
Alex Crichtona74a1c82018-05-16 10:20:44 -0700319 pub fn surround<F>(&self, tokens: &mut ::proc_macro2::TokenStream, f: F)
David Tolnay94d2b792018-04-29 12:26:10 -0700320 where
Alex Crichtona74a1c82018-05-16 10:20:44 -0700321 F: FnOnce(&mut ::proc_macro2::TokenStream),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700322 {
323 printing::delim($s, &self.0, tokens, f);
324 }
325
326 #[cfg(feature = "parsing")]
David Tolnay94d2b792018-04-29 12:26:10 -0700327 pub fn parse<F, R>(
328 tokens: $crate::buffer::Cursor,
329 f: F,
330 ) -> $crate::synom::PResult<($name, R)>
331 where
332 F: FnOnce($crate::buffer::Cursor) -> $crate::synom::PResult<R>,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700333 {
334 parsing::delim($s, tokens, $name, f)
335 }
336 }
강동윤d5229da2018-01-12 13:11:33 +0900337
338 impl From<Span> for $name {
339 fn from(span: Span) -> Self {
340 $name(span)
341 }
342 }
David Tolnay94d2b792018-04-29 12:26:10 -0700343 };
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700344}
345
Sergio Benitezd14d5362018-04-28 15:38:25 -0700346token_punct_def! {
347 /// `_`
348 "_" pub struct Underscore/1
349}
350
351#[cfg(feature = "printing")]
352impl ::quote::ToTokens for Underscore {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700353 fn to_tokens(&self, tokens: &mut ::proc_macro2::TokenStream) {
354 use quote::TokenStreamExt;
355 tokens.append(::proc_macro2::Ident::new("_", self.0[0]));
Sergio Benitezd14d5362018-04-28 15:38:25 -0700356 }
357}
358
359#[cfg(feature = "parsing")]
360impl ::Synom for Underscore {
361 fn parse(input: ::buffer::Cursor) -> ::synom::PResult<Underscore> {
362 match input.term() {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700363 Some((term, rest)) => {
364 if term.to_string() == "_" {
365 Ok((Underscore([term.span()]), rest))
366 } else {
367 ::parse_error()
368 }
369 }
David Tolnay94d2b792018-04-29 12:26:10 -0700370 None => parsing::punct("_", input, Underscore),
Sergio Benitezd14d5362018-04-28 15:38:25 -0700371 }
372 }
373
374 fn description() -> Option<&'static str> {
375 Some("`_`")
376 }
377}
378
Alex Crichton131308c2018-05-18 14:00:24 -0700379token_punct_def! {
380 /// `'`
381 "'" pub struct Apostrophe/1
382}
383
384#[cfg(feature = "printing")]
385impl ::quote::ToTokens for Apostrophe {
386 fn to_tokens(&self, tokens: &mut ::proc_macro2::TokenStream) {
387 use quote::TokenStreamExt;
388 let mut token = ::proc_macro2::Punct::new('\'', ::proc_macro2::Spacing::Joint);
389 token.set_span(self.0[0]);
390 tokens.append(token);
391 }
392}
393
394#[cfg(feature = "parsing")]
395impl ::Synom for Apostrophe {
396 fn parse(input: ::buffer::Cursor) -> ::synom::PResult<Apostrophe> {
397 match input.op() {
398 Some((op, rest)) => {
399 if op.as_char() == '\'' && op.spacing() == ::proc_macro2::Spacing::Joint {
400 Ok((Apostrophe([op.span()]), rest))
401 } else {
402 ::parse_error()
403 }
404 }
405 None => ::parse_error()
406 }
407 }
408
409 fn description() -> Option<&'static str> {
410 Some("`'`")
411 }
412}
413
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700414tokens! {
David Tolnay73c98de2017-12-31 15:56:56 -0500415 punct: {
David Tolnay5a20f632017-12-26 22:11:28 -0500416 "+" pub struct Add/1 /// `+`
417 "+=" pub struct AddEq/2 /// `+=`
418 "&" pub struct And/1 /// `&`
419 "&&" pub struct AndAnd/2 /// `&&`
420 "&=" pub struct AndEq/2 /// `&=`
421 "@" pub struct At/1 /// `@`
422 "!" pub struct Bang/1 /// `!`
423 "^" pub struct Caret/1 /// `^`
David Tolnay32954ef2017-12-26 22:43:16 -0500424 "^=" pub struct CaretEq/2 /// `^=`
David Tolnay5a20f632017-12-26 22:11:28 -0500425 ":" pub struct Colon/1 /// `:`
426 "::" pub struct Colon2/2 /// `::`
427 "," pub struct Comma/1 /// `,`
428 "/" pub struct Div/1 /// `/`
429 "/=" pub struct DivEq/2 /// `/=`
MSleepyPandaf5137d72018-05-05 18:57:10 +0200430 "$" pub struct Dollar/1 /// `$`
David Tolnay5a20f632017-12-26 22:11:28 -0500431 "." pub struct Dot/1 /// `.`
432 ".." pub struct Dot2/2 /// `..`
433 "..." pub struct Dot3/3 /// `...`
434 "..=" pub struct DotDotEq/3 /// `..=`
435 "=" pub struct Eq/1 /// `=`
436 "==" pub struct EqEq/2 /// `==`
437 ">=" pub struct Ge/2 /// `>=`
438 ">" pub struct Gt/1 /// `>`
439 "<=" pub struct Le/2 /// `<=`
440 "<" pub struct Lt/1 /// `<`
441 "*=" pub struct MulEq/2 /// `*=`
442 "!=" pub struct Ne/2 /// `!=`
443 "|" pub struct Or/1 /// `|`
444 "|=" pub struct OrEq/2 /// `|=`
445 "||" pub struct OrOr/2 /// `||`
446 "#" pub struct Pound/1 /// `#`
447 "?" pub struct Question/1 /// `?`
448 "->" pub struct RArrow/2 /// `->`
449 "<-" pub struct LArrow/2 /// `<-`
450 "%" pub struct Rem/1 /// `%`
451 "%=" pub struct RemEq/2 /// `%=`
David Tolnay17624152018-03-31 18:11:40 +0200452 "=>" pub struct FatArrow/2 /// `=>`
David Tolnay5a20f632017-12-26 22:11:28 -0500453 ";" pub struct Semi/1 /// `;`
454 "<<" pub struct Shl/2 /// `<<`
455 "<<=" pub struct ShlEq/3 /// `<<=`
456 ">>" pub struct Shr/2 /// `>>`
457 ">>=" pub struct ShrEq/3 /// `>>=`
458 "*" pub struct Star/1 /// `*`
459 "-" pub struct Sub/1 /// `-`
460 "-=" pub struct SubEq/2 /// `-=`
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700461 }
David Tolnay73c98de2017-12-31 15:56:56 -0500462 delimiter: {
David Tolnay5a20f632017-12-26 22:11:28 -0500463 "{" pub struct Brace /// `{...}`
464 "[" pub struct Bracket /// `[...]`
465 "(" pub struct Paren /// `(...)`
466 " " pub struct Group /// None-delimited group
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700467 }
David Tolnay73c98de2017-12-31 15:56:56 -0500468 keyword: {
David Tolnay5a20f632017-12-26 22:11:28 -0500469 "as" pub struct As /// `as`
470 "auto" pub struct Auto /// `auto`
471 "box" pub struct Box /// `box`
472 "break" pub struct Break /// `break`
473 "Self" pub struct CapSelf /// `Self`
474 "catch" pub struct Catch /// `catch`
475 "const" pub struct Const /// `const`
476 "continue" pub struct Continue /// `continue`
477 "crate" pub struct Crate /// `crate`
478 "default" pub struct Default /// `default`
479 "do" pub struct Do /// `do`
480 "dyn" pub struct Dyn /// `dyn`
481 "else" pub struct Else /// `else`
482 "enum" pub struct Enum /// `enum`
483 "extern" pub struct Extern /// `extern`
484 "fn" pub struct Fn /// `fn`
485 "for" pub struct For /// `for`
486 "if" pub struct If /// `if`
487 "impl" pub struct Impl /// `impl`
488 "in" pub struct In /// `in`
489 "let" pub struct Let /// `let`
490 "loop" pub struct Loop /// `loop`
491 "macro" pub struct Macro /// `macro`
492 "match" pub struct Match /// `match`
493 "mod" pub struct Mod /// `mod`
494 "move" pub struct Move /// `move`
495 "mut" pub struct Mut /// `mut`
496 "pub" pub struct Pub /// `pub`
497 "ref" pub struct Ref /// `ref`
498 "return" pub struct Return /// `return`
499 "self" pub struct Self_ /// `self`
500 "static" pub struct Static /// `static`
501 "struct" pub struct Struct /// `struct`
502 "super" pub struct Super /// `super`
503 "trait" pub struct Trait /// `trait`
504 "type" pub struct Type /// `type`
505 "union" pub struct Union /// `union`
506 "unsafe" pub struct Unsafe /// `unsafe`
507 "use" pub struct Use /// `use`
508 "where" pub struct Where /// `where`
509 "while" pub struct While /// `while`
510 "yield" pub struct Yield /// `yield`
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700511 }
512}
513
David Tolnayf005f962018-01-06 21:19:41 -0800514/// A type-macro that expands to the name of the Rust type representation of a
515/// given token.
516///
517/// See the [token module] documentation for details and examples.
518///
519/// [token module]: token/index.html
David Tolnayf8db7ba2017-11-11 22:52:16 -0800520// Unfortunate duplication due to a rustdoc bug.
521// https://github.com/rust-lang/rust/issues/45939
522#[macro_export]
David Tolnaya5ed2fe2018-04-29 12:23:34 -0700523#[cfg_attr(rustfmt, rustfmt_skip)]
David Tolnayf8db7ba2017-11-11 22:52:16 -0800524macro_rules! Token {
David Tolnay32954ef2017-12-26 22:43:16 -0500525 (+) => { $crate::token::Add };
526 (+=) => { $crate::token::AddEq };
527 (&) => { $crate::token::And };
528 (&&) => { $crate::token::AndAnd };
529 (&=) => { $crate::token::AndEq };
530 (@) => { $crate::token::At };
531 (!) => { $crate::token::Bang };
532 (^) => { $crate::token::Caret };
533 (^=) => { $crate::token::CaretEq };
534 (:) => { $crate::token::Colon };
535 (::) => { $crate::token::Colon2 };
536 (,) => { $crate::token::Comma };
537 (/) => { $crate::token::Div };
538 (/=) => { $crate::token::DivEq };
539 (.) => { $crate::token::Dot };
540 (..) => { $crate::token::Dot2 };
541 (...) => { $crate::token::Dot3 };
542 (..=) => { $crate::token::DotDotEq };
543 (=) => { $crate::token::Eq };
544 (==) => { $crate::token::EqEq };
545 (>=) => { $crate::token::Ge };
546 (>) => { $crate::token::Gt };
547 (<=) => { $crate::token::Le };
548 (<) => { $crate::token::Lt };
549 (*=) => { $crate::token::MulEq };
550 (!=) => { $crate::token::Ne };
551 (|) => { $crate::token::Or };
552 (|=) => { $crate::token::OrEq };
553 (||) => { $crate::token::OrOr };
554 (#) => { $crate::token::Pound };
555 (?) => { $crate::token::Question };
556 (->) => { $crate::token::RArrow };
557 (<-) => { $crate::token::LArrow };
558 (%) => { $crate::token::Rem };
559 (%=) => { $crate::token::RemEq };
David Tolnay17624152018-03-31 18:11:40 +0200560 (=>) => { $crate::token::FatArrow };
David Tolnay32954ef2017-12-26 22:43:16 -0500561 (;) => { $crate::token::Semi };
562 (<<) => { $crate::token::Shl };
563 (<<=) => { $crate::token::ShlEq };
564 (>>) => { $crate::token::Shr };
565 (>>=) => { $crate::token::ShrEq };
566 (*) => { $crate::token::Star };
567 (-) => { $crate::token::Sub };
568 (-=) => { $crate::token::SubEq };
569 (_) => { $crate::token::Underscore };
570 (as) => { $crate::token::As };
571 (auto) => { $crate::token::Auto };
572 (box) => { $crate::token::Box };
573 (break) => { $crate::token::Break };
574 (Self) => { $crate::token::CapSelf };
575 (catch) => { $crate::token::Catch };
576 (const) => { $crate::token::Const };
577 (continue) => { $crate::token::Continue };
578 (crate) => { $crate::token::Crate };
579 (default) => { $crate::token::Default };
580 (do) => { $crate::token::Do };
581 (dyn) => { $crate::token::Dyn };
582 (else) => { $crate::token::Else };
583 (enum) => { $crate::token::Enum };
584 (extern) => { $crate::token::Extern };
585 (fn) => { $crate::token::Fn };
586 (for) => { $crate::token::For };
587 (if) => { $crate::token::If };
588 (impl) => { $crate::token::Impl };
589 (in) => { $crate::token::In };
590 (let) => { $crate::token::Let };
591 (loop) => { $crate::token::Loop };
592 (macro) => { $crate::token::Macro };
593 (match) => { $crate::token::Match };
594 (mod) => { $crate::token::Mod };
595 (move) => { $crate::token::Move };
596 (mut) => { $crate::token::Mut };
597 (pub) => { $crate::token::Pub };
598 (ref) => { $crate::token::Ref };
599 (return) => { $crate::token::Return };
600 (self) => { $crate::token::Self_ };
601 (static) => { $crate::token::Static };
602 (struct) => { $crate::token::Struct };
603 (super) => { $crate::token::Super };
604 (trait) => { $crate::token::Trait };
605 (type) => { $crate::token::Type };
606 (union) => { $crate::token::Union };
607 (unsafe) => { $crate::token::Unsafe };
608 (use) => { $crate::token::Use };
609 (where) => { $crate::token::Where };
610 (while) => { $crate::token::While };
611 (yield) => { $crate::token::Yield };
David Tolnayf8db7ba2017-11-11 22:52:16 -0800612}
613
David Tolnayf005f962018-01-06 21:19:41 -0800614/// Parse a single Rust punctuation token.
615///
616/// See the [token module] documentation for details and examples.
617///
618/// [token module]: token/index.html
David Tolnay461d98e2018-01-07 11:07:19 -0800619///
620/// *This macro is available if Syn is built with the `"parsing"` feature.*
David Tolnay0fbe3282017-12-26 21:46:16 -0500621#[cfg(feature = "parsing")]
David Tolnayf8db7ba2017-11-11 22:52:16 -0800622#[macro_export]
David Tolnaya5ed2fe2018-04-29 12:23:34 -0700623#[cfg_attr(rustfmt, rustfmt_skip)]
David Tolnayf8db7ba2017-11-11 22:52:16 -0800624macro_rules! punct {
David Tolnay32954ef2017-12-26 22:43:16 -0500625 ($i:expr, +) => { call!($i, <$crate::token::Add as $crate::synom::Synom>::parse) };
626 ($i:expr, +=) => { call!($i, <$crate::token::AddEq as $crate::synom::Synom>::parse) };
627 ($i:expr, &) => { call!($i, <$crate::token::And as $crate::synom::Synom>::parse) };
628 ($i:expr, &&) => { call!($i, <$crate::token::AndAnd as $crate::synom::Synom>::parse) };
629 ($i:expr, &=) => { call!($i, <$crate::token::AndEq as $crate::synom::Synom>::parse) };
630 ($i:expr, @) => { call!($i, <$crate::token::At as $crate::synom::Synom>::parse) };
631 ($i:expr, !) => { call!($i, <$crate::token::Bang as $crate::synom::Synom>::parse) };
632 ($i:expr, ^) => { call!($i, <$crate::token::Caret as $crate::synom::Synom>::parse) };
633 ($i:expr, ^=) => { call!($i, <$crate::token::CaretEq as $crate::synom::Synom>::parse) };
634 ($i:expr, :) => { call!($i, <$crate::token::Colon as $crate::synom::Synom>::parse) };
635 ($i:expr, ::) => { call!($i, <$crate::token::Colon2 as $crate::synom::Synom>::parse) };
636 ($i:expr, ,) => { call!($i, <$crate::token::Comma as $crate::synom::Synom>::parse) };
637 ($i:expr, /) => { call!($i, <$crate::token::Div as $crate::synom::Synom>::parse) };
638 ($i:expr, /=) => { call!($i, <$crate::token::DivEq as $crate::synom::Synom>::parse) };
639 ($i:expr, .) => { call!($i, <$crate::token::Dot as $crate::synom::Synom>::parse) };
640 ($i:expr, ..) => { call!($i, <$crate::token::Dot2 as $crate::synom::Synom>::parse) };
641 ($i:expr, ...) => { call!($i, <$crate::token::Dot3 as $crate::synom::Synom>::parse) };
642 ($i:expr, ..=) => { call!($i, <$crate::token::DotDotEq as $crate::synom::Synom>::parse) };
643 ($i:expr, =) => { call!($i, <$crate::token::Eq as $crate::synom::Synom>::parse) };
644 ($i:expr, ==) => { call!($i, <$crate::token::EqEq as $crate::synom::Synom>::parse) };
645 ($i:expr, >=) => { call!($i, <$crate::token::Ge as $crate::synom::Synom>::parse) };
646 ($i:expr, >) => { call!($i, <$crate::token::Gt as $crate::synom::Synom>::parse) };
647 ($i:expr, <=) => { call!($i, <$crate::token::Le as $crate::synom::Synom>::parse) };
648 ($i:expr, <) => { call!($i, <$crate::token::Lt as $crate::synom::Synom>::parse) };
649 ($i:expr, *=) => { call!($i, <$crate::token::MulEq as $crate::synom::Synom>::parse) };
650 ($i:expr, !=) => { call!($i, <$crate::token::Ne as $crate::synom::Synom>::parse) };
651 ($i:expr, |) => { call!($i, <$crate::token::Or as $crate::synom::Synom>::parse) };
652 ($i:expr, |=) => { call!($i, <$crate::token::OrEq as $crate::synom::Synom>::parse) };
653 ($i:expr, ||) => { call!($i, <$crate::token::OrOr as $crate::synom::Synom>::parse) };
654 ($i:expr, #) => { call!($i, <$crate::token::Pound as $crate::synom::Synom>::parse) };
655 ($i:expr, ?) => { call!($i, <$crate::token::Question as $crate::synom::Synom>::parse) };
656 ($i:expr, ->) => { call!($i, <$crate::token::RArrow as $crate::synom::Synom>::parse) };
657 ($i:expr, <-) => { call!($i, <$crate::token::LArrow as $crate::synom::Synom>::parse) };
658 ($i:expr, %) => { call!($i, <$crate::token::Rem as $crate::synom::Synom>::parse) };
659 ($i:expr, %=) => { call!($i, <$crate::token::RemEq as $crate::synom::Synom>::parse) };
David Tolnay17624152018-03-31 18:11:40 +0200660 ($i:expr, =>) => { call!($i, <$crate::token::FatArrow as $crate::synom::Synom>::parse) };
David Tolnay32954ef2017-12-26 22:43:16 -0500661 ($i:expr, ;) => { call!($i, <$crate::token::Semi as $crate::synom::Synom>::parse) };
662 ($i:expr, <<) => { call!($i, <$crate::token::Shl as $crate::synom::Synom>::parse) };
663 ($i:expr, <<=) => { call!($i, <$crate::token::ShlEq as $crate::synom::Synom>::parse) };
664 ($i:expr, >>) => { call!($i, <$crate::token::Shr as $crate::synom::Synom>::parse) };
665 ($i:expr, >>=) => { call!($i, <$crate::token::ShrEq as $crate::synom::Synom>::parse) };
666 ($i:expr, *) => { call!($i, <$crate::token::Star as $crate::synom::Synom>::parse) };
667 ($i:expr, -) => { call!($i, <$crate::token::Sub as $crate::synom::Synom>::parse) };
668 ($i:expr, -=) => { call!($i, <$crate::token::SubEq as $crate::synom::Synom>::parse) };
669 ($i:expr, _) => { call!($i, <$crate::token::Underscore as $crate::synom::Synom>::parse) };
David Tolnayf8db7ba2017-11-11 22:52:16 -0800670}
671
David Tolnayf005f962018-01-06 21:19:41 -0800672/// Parse a single Rust keyword token.
673///
674/// See the [token module] documentation for details and examples.
675///
676/// [token module]: token/index.html
David Tolnay461d98e2018-01-07 11:07:19 -0800677///
678/// *This macro is available if Syn is built with the `"parsing"` feature.*
David Tolnay0fbe3282017-12-26 21:46:16 -0500679#[cfg(feature = "parsing")]
David Tolnayf8db7ba2017-11-11 22:52:16 -0800680#[macro_export]
David Tolnaya5ed2fe2018-04-29 12:23:34 -0700681#[cfg_attr(rustfmt, rustfmt_skip)]
David Tolnayf8db7ba2017-11-11 22:52:16 -0800682macro_rules! keyword {
David Tolnay32954ef2017-12-26 22:43:16 -0500683 ($i:expr, as) => { call!($i, <$crate::token::As as $crate::synom::Synom>::parse) };
684 ($i:expr, auto) => { call!($i, <$crate::token::Auto as $crate::synom::Synom>::parse) };
685 ($i:expr, box) => { call!($i, <$crate::token::Box as $crate::synom::Synom>::parse) };
686 ($i:expr, break) => { call!($i, <$crate::token::Break as $crate::synom::Synom>::parse) };
687 ($i:expr, Self) => { call!($i, <$crate::token::CapSelf as $crate::synom::Synom>::parse) };
688 ($i:expr, catch) => { call!($i, <$crate::token::Catch as $crate::synom::Synom>::parse) };
689 ($i:expr, const) => { call!($i, <$crate::token::Const as $crate::synom::Synom>::parse) };
690 ($i:expr, continue) => { call!($i, <$crate::token::Continue as $crate::synom::Synom>::parse) };
691 ($i:expr, crate) => { call!($i, <$crate::token::Crate as $crate::synom::Synom>::parse) };
692 ($i:expr, default) => { call!($i, <$crate::token::Default as $crate::synom::Synom>::parse) };
693 ($i:expr, do) => { call!($i, <$crate::token::Do as $crate::synom::Synom>::parse) };
694 ($i:expr, dyn) => { call!($i, <$crate::token::Dyn as $crate::synom::Synom>::parse) };
695 ($i:expr, else) => { call!($i, <$crate::token::Else as $crate::synom::Synom>::parse) };
696 ($i:expr, enum) => { call!($i, <$crate::token::Enum as $crate::synom::Synom>::parse) };
697 ($i:expr, extern) => { call!($i, <$crate::token::Extern as $crate::synom::Synom>::parse) };
698 ($i:expr, fn) => { call!($i, <$crate::token::Fn as $crate::synom::Synom>::parse) };
699 ($i:expr, for) => { call!($i, <$crate::token::For as $crate::synom::Synom>::parse) };
700 ($i:expr, if) => { call!($i, <$crate::token::If as $crate::synom::Synom>::parse) };
701 ($i:expr, impl) => { call!($i, <$crate::token::Impl as $crate::synom::Synom>::parse) };
702 ($i:expr, in) => { call!($i, <$crate::token::In as $crate::synom::Synom>::parse) };
703 ($i:expr, let) => { call!($i, <$crate::token::Let as $crate::synom::Synom>::parse) };
704 ($i:expr, loop) => { call!($i, <$crate::token::Loop as $crate::synom::Synom>::parse) };
705 ($i:expr, macro) => { call!($i, <$crate::token::Macro as $crate::synom::Synom>::parse) };
706 ($i:expr, match) => { call!($i, <$crate::token::Match as $crate::synom::Synom>::parse) };
707 ($i:expr, mod) => { call!($i, <$crate::token::Mod as $crate::synom::Synom>::parse) };
708 ($i:expr, move) => { call!($i, <$crate::token::Move as $crate::synom::Synom>::parse) };
709 ($i:expr, mut) => { call!($i, <$crate::token::Mut as $crate::synom::Synom>::parse) };
710 ($i:expr, pub) => { call!($i, <$crate::token::Pub as $crate::synom::Synom>::parse) };
711 ($i:expr, ref) => { call!($i, <$crate::token::Ref as $crate::synom::Synom>::parse) };
712 ($i:expr, return) => { call!($i, <$crate::token::Return as $crate::synom::Synom>::parse) };
713 ($i:expr, self) => { call!($i, <$crate::token::Self_ as $crate::synom::Synom>::parse) };
714 ($i:expr, static) => { call!($i, <$crate::token::Static as $crate::synom::Synom>::parse) };
715 ($i:expr, struct) => { call!($i, <$crate::token::Struct as $crate::synom::Synom>::parse) };
716 ($i:expr, super) => { call!($i, <$crate::token::Super as $crate::synom::Synom>::parse) };
717 ($i:expr, trait) => { call!($i, <$crate::token::Trait as $crate::synom::Synom>::parse) };
718 ($i:expr, type) => { call!($i, <$crate::token::Type as $crate::synom::Synom>::parse) };
719 ($i:expr, union) => { call!($i, <$crate::token::Union as $crate::synom::Synom>::parse) };
720 ($i:expr, unsafe) => { call!($i, <$crate::token::Unsafe as $crate::synom::Synom>::parse) };
721 ($i:expr, use) => { call!($i, <$crate::token::Use as $crate::synom::Synom>::parse) };
722 ($i:expr, where) => { call!($i, <$crate::token::Where as $crate::synom::Synom>::parse) };
723 ($i:expr, while) => { call!($i, <$crate::token::While as $crate::synom::Synom>::parse) };
724 ($i:expr, yield) => { call!($i, <$crate::token::Yield as $crate::synom::Synom>::parse) };
David Tolnayf8db7ba2017-11-11 22:52:16 -0800725}
726
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700727#[cfg(feature = "parsing")]
728mod parsing {
David Tolnay51382052017-12-27 13:46:21 -0500729 use proc_macro2::{Delimiter, Spacing, Span};
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700730
David Tolnaydfc886b2018-01-06 08:03:09 -0800731 use buffer::Cursor;
David Tolnay203557a2017-12-27 23:59:33 -0500732 use parse_error;
733 use synom::PResult;
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700734
735 pub trait FromSpans: Sized {
736 fn from_spans(spans: &[Span]) -> Self;
737 }
738
739 impl FromSpans for [Span; 1] {
740 fn from_spans(spans: &[Span]) -> Self {
741 [spans[0]]
742 }
743 }
744
745 impl FromSpans for [Span; 2] {
746 fn from_spans(spans: &[Span]) -> Self {
747 [spans[0], spans[1]]
748 }
749 }
750
751 impl FromSpans for [Span; 3] {
752 fn from_spans(spans: &[Span]) -> Self {
753 [spans[0], spans[1], spans[2]]
754 }
755 }
756
David Tolnay2b069542018-05-09 12:59:36 -0700757 pub fn punct<'a, T, R>(s: &str, mut tokens: Cursor<'a>, new: fn(T) -> R) -> PResult<'a, R>
David Tolnay51382052017-12-27 13:46:21 -0500758 where
759 T: FromSpans,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700760 {
Alex Crichton9a4dca22018-03-28 06:32:19 -0700761 let mut spans = [Span::call_site(); 3];
Alex Crichton954046c2017-05-30 21:49:42 -0700762 assert!(s.len() <= spans.len());
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700763 let chars = s.chars();
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700764
Alex Crichton954046c2017-05-30 21:49:42 -0700765 for (i, (ch, slot)) in chars.zip(&mut spans).enumerate() {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400766 match tokens.op() {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700767 Some((op, rest)) => {
768 if op.as_char() == ch {
769 if i != s.len() - 1 {
770 match op.spacing() {
771 Spacing::Joint => {}
772 _ => return parse_error(),
773 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400774 }
Alex Crichtona74a1c82018-05-16 10:20:44 -0700775 *slot = op.span();
776 tokens = rest;
777 } else {
778 return parse_error()
Michael Layzell0a1a6632017-06-02 18:07:43 -0400779 }
Alex Crichton954046c2017-05-30 21:49:42 -0700780 }
David Tolnay51382052017-12-27 13:46:21 -0500781 _ => return parse_error(),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700782 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700783 }
David Tolnay2b069542018-05-09 12:59:36 -0700784 Ok((new(T::from_spans(&spans)), tokens))
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700785 }
786
David Tolnayb57c8492018-05-05 00:32:04 -0700787 pub fn keyword<'a, T>(
788 keyword: &str,
789 tokens: Cursor<'a>,
David Tolnay2b069542018-05-09 12:59:36 -0700790 new: fn(Span) -> T,
David Tolnayb57c8492018-05-05 00:32:04 -0700791 ) -> PResult<'a, T> {
Alex Crichton9a4dca22018-03-28 06:32:19 -0700792 if let Some((term, rest)) = tokens.term() {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700793 if term.to_string() == keyword {
David Tolnay2b069542018-05-09 12:59:36 -0700794 return Ok((new(term.span()), rest));
Michael Layzell0a1a6632017-06-02 18:07:43 -0400795 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700796 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400797 parse_error()
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700798 }
799
David Tolnay51382052017-12-27 13:46:21 -0500800 pub fn delim<'a, F, R, T>(
801 delim: &str,
802 tokens: Cursor<'a>,
David Tolnay2b069542018-05-09 12:59:36 -0700803 new: fn(Span) -> T,
David Tolnay51382052017-12-27 13:46:21 -0500804 f: F,
David Tolnay8875fca2017-12-31 13:52:37 -0500805 ) -> PResult<'a, (T, R)>
David Tolnay51382052017-12-27 13:46:21 -0500806 where
807 F: FnOnce(Cursor) -> PResult<R>,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700808 {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400809 // NOTE: We should support none-delimited sequences here.
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700810 let delim = match delim {
811 "(" => Delimiter::Parenthesis,
812 "{" => Delimiter::Brace,
813 "[" => Delimiter::Bracket,
Michael Layzell93c36282017-06-04 20:43:14 -0400814 " " => Delimiter::None,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700815 _ => panic!("unknown delimiter: {}", delim),
816 };
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700817
David Tolnay65729482017-12-31 16:14:50 -0500818 if let Some((inside, span, rest)) = tokens.group(delim) {
819 match f(inside) {
David Tolnayf4aa6b42017-12-31 16:40:33 -0500820 Ok((ret, remaining)) => {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400821 if remaining.eof() {
David Tolnay2b069542018-05-09 12:59:36 -0700822 return Ok(((new(span), ret), rest));
Michael Layzell0a1a6632017-06-02 18:07:43 -0400823 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700824 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400825 Err(err) => return Err(err),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700826 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700827 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400828 parse_error()
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700829 }
830}
831
832#[cfg(feature = "printing")]
833mod printing {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700834 use proc_macro2::{Delimiter, Group, Punct, Spacing, Span, Ident, TokenStream};
835 use quote::TokenStreamExt;
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700836
Alex Crichtona74a1c82018-05-16 10:20:44 -0700837 pub fn punct(s: &str, spans: &[Span], tokens: &mut TokenStream) {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700838 assert_eq!(s.len(), spans.len());
839
840 let mut chars = s.chars();
841 let mut spans = spans.iter();
842 let ch = chars.next_back().unwrap();
843 let span = spans.next_back().unwrap();
844 for (ch, span) in chars.zip(spans) {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700845 let mut op = Punct::new(ch, Spacing::Joint);
Alex Crichton9a4dca22018-03-28 06:32:19 -0700846 op.set_span(*span);
847 tokens.append(op);
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700848 }
849
Alex Crichtona74a1c82018-05-16 10:20:44 -0700850 let mut op = Punct::new(ch, Spacing::Alone);
Alex Crichton9a4dca22018-03-28 06:32:19 -0700851 op.set_span(*span);
852 tokens.append(op);
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700853 }
854
Alex Crichtona74a1c82018-05-16 10:20:44 -0700855 pub fn keyword(s: &str, span: &Span, tokens: &mut TokenStream) {
856 tokens.append(Ident::new(s, *span));
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700857 }
858
Alex Crichtona74a1c82018-05-16 10:20:44 -0700859 pub fn delim<F>(s: &str, span: &Span, tokens: &mut TokenStream, f: F)
David Tolnay51382052017-12-27 13:46:21 -0500860 where
Alex Crichtona74a1c82018-05-16 10:20:44 -0700861 F: FnOnce(&mut TokenStream),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700862 {
David Tolnay00ab6982017-12-31 18:15:06 -0500863 let delim = match s {
864 "(" => Delimiter::Parenthesis,
865 "[" => Delimiter::Bracket,
866 "{" => Delimiter::Brace,
867 " " => Delimiter::None,
868 _ => panic!("unknown delimiter: {}", s),
869 };
Alex Crichtona74a1c82018-05-16 10:20:44 -0700870 let mut inner = TokenStream::empty();
David Tolnay00ab6982017-12-31 18:15:06 -0500871 f(&mut inner);
Alex Crichton9a4dca22018-03-28 06:32:19 -0700872 let mut g = Group::new(delim, inner.into());
873 g.set_span(*span);
874 tokens.append(g);
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700875 }
876}