blob: 7e07545cc6303a7334e073a31f46c48a47d7e00d [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;
26//! #
David Tolnaye303b7c2018-05-20 16:46:35 -070027//! # use syn::{Attribute, Visibility, Ident, Type, Expr};
David Tolnaye79ae182018-01-06 19:23:37 -080028//! #
29//! pub struct ItemStatic {
30//! pub attrs: Vec<Attribute>,
31//! pub vis: Visibility,
32//! pub static_token: Token![static],
33//! pub mutability: Option<Token![mut]>,
34//! pub ident: Ident,
35//! pub colon_token: Token![:],
36//! pub ty: Box<Type>,
37//! pub eq_token: Token![=],
38//! pub expr: Box<Expr>,
39//! pub semi_token: Token![;],
40//! }
41//! #
42//! # fn main() {}
43//! ```
44//!
45//! # Parsing
46//!
47//! These tokens can be parsed using the [`Synom`] trait and the parser
48//! combinator macros [`punct!`], [`keyword!`], [`parens!`], [`braces!`], and
49//! [`brackets!`].
50//!
51//! [`Synom`]: ../synom/trait.Synom.html
52//! [`punct!`]: ../macro.punct.html
53//! [`keyword!`]: ../macro.keyword.html
54//! [`parens!`]: ../macro.parens.html
55//! [`braces!`]: ../macro.braces.html
56//! [`brackets!`]: ../macro.brackets.html
57//!
58//! ```
59//! #[macro_use]
60//! extern crate syn;
61//!
62//! use syn::synom::Synom;
David Tolnaye303b7c2018-05-20 16:46:35 -070063//! use syn::{Attribute, Visibility, Ident, Type, Expr};
David Tolnaye79ae182018-01-06 19:23:37 -080064//! #
65//! # struct ItemStatic;
66//! # use syn::ItemStatic as SynItemStatic;
67//!
68//! // Parse the ItemStatic struct shown above.
69//! impl Synom for ItemStatic {
70//! named!(parse -> Self, do_parse!(
71//! # (ItemStatic)
72//! # ));
73//! # }
74//! #
75//! # mod example {
76//! # use super::*;
77//! # use super::SynItemStatic as ItemStatic;
78//! #
79//! # named!(parse -> ItemStatic, do_parse!(
80//! attrs: many0!(Attribute::parse_outer) >>
81//! vis: syn!(Visibility) >>
82//! static_token: keyword!(static) >>
83//! mutability: option!(keyword!(mut)) >>
84//! ident: syn!(Ident) >>
85//! colon_token: punct!(:) >>
86//! ty: syn!(Type) >>
87//! eq_token: punct!(=) >>
88//! expr: syn!(Expr) >>
89//! semi_token: punct!(;) >>
90//! (ItemStatic {
91//! attrs, vis, static_token, mutability, ident, colon_token,
92//! ty: Box::new(ty), eq_token, expr: Box::new(expr), semi_token,
93//! })
94//! ));
95//! }
96//! #
97//! # fn main() {}
98//! ```
Alex Crichton954046c2017-05-30 21:49:42 -070099
Alex Crichtona74a1c82018-05-16 10:20:44 -0700100use proc_macro2::{Span, Ident};
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700101
102macro_rules! tokens {
103 (
David Tolnay73c98de2017-12-31 15:56:56 -0500104 punct: {
105 $($punct:tt pub struct $punct_name:ident/$len:tt #[$punct_doc:meta])*
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700106 }
David Tolnay73c98de2017-12-31 15:56:56 -0500107 delimiter: {
108 $($delimiter:tt pub struct $delimiter_name:ident #[$delimiter_doc:meta])*
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700109 }
David Tolnay73c98de2017-12-31 15:56:56 -0500110 keyword: {
111 $($keyword:tt pub struct $keyword_name:ident #[$keyword_doc:meta])*
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700112 }
113 ) => (
Sergio Benitezd14d5362018-04-28 15:38:25 -0700114 $(token_punct_def! { #[$punct_doc] $punct pub struct $punct_name/$len })*
115 $(token_punct_parser! { $punct pub struct $punct_name })*
David Tolnay73c98de2017-12-31 15:56:56 -0500116 $(token_delimiter! { #[$delimiter_doc] $delimiter pub struct $delimiter_name })*
117 $(token_keyword! { #[$keyword_doc] $keyword pub struct $keyword_name })*
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700118 )
119}
120
Sergio Benitezd14d5362018-04-28 15:38:25 -0700121macro_rules! token_punct_def {
David Tolnay94d2b792018-04-29 12:26:10 -0700122 (#[$doc:meta] $s:tt pub struct $name:ident / $len:tt) => {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700123 #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))]
David Tolnay5a20f632017-12-26 22:11:28 -0500124 #[$doc]
David Tolnay1bb760c2018-01-07 11:18:15 -0800125 ///
126 /// Don't try to remember the name of this type -- use the [`Token!`]
127 /// macro instead.
128 ///
129 /// [`Token!`]: index.html
David Tolnay5a20f632017-12-26 22:11:28 -0500130 pub struct $name(pub [Span; $len]);
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700131
David Tolnay0bdb0552017-12-27 21:31:51 -0500132 impl $name {
133 pub fn new(span: Span) -> Self {
134 $name([span; $len])
135 }
136 }
137
David Tolnay66bb8d52018-01-08 08:22:31 -0800138 impl ::std::default::Default for $name {
139 fn default() -> Self {
Alex Crichton9a4dca22018-03-28 06:32:19 -0700140 $name([Span::call_site(); $len])
David Tolnay66bb8d52018-01-08 08:22:31 -0800141 }
142 }
143
Nika Layzelld73a3652017-10-24 08:57:05 -0400144 #[cfg(feature = "extra-traits")]
145 impl ::std::fmt::Debug for $name {
146 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
David Tolnay98942562017-12-26 21:24:35 -0500147 f.write_str(stringify!($name))
Nika Layzelld73a3652017-10-24 08:57:05 -0400148 }
149 }
150
David Tolnay98942562017-12-26 21:24:35 -0500151 #[cfg(feature = "extra-traits")]
152 impl ::std::cmp::Eq for $name {}
153
154 #[cfg(feature = "extra-traits")]
155 impl ::std::cmp::PartialEq for $name {
156 fn eq(&self, _other: &$name) -> bool {
157 true
158 }
159 }
160
161 #[cfg(feature = "extra-traits")]
162 impl ::std::hash::Hash for $name {
163 fn hash<H>(&self, _state: &mut H)
David Tolnay94d2b792018-04-29 12:26:10 -0700164 where
165 H: ::std::hash::Hasher,
166 {
167 }
David Tolnay98942562017-12-26 21:24:35 -0500168 }
169
Sergio Benitezd14d5362018-04-28 15:38:25 -0700170 impl From<Span> for $name {
171 fn from(span: Span) -> Self {
172 $name([span; $len])
173 }
174 }
David Tolnay94d2b792018-04-29 12:26:10 -0700175 };
Sergio Benitezd14d5362018-04-28 15:38:25 -0700176}
177
178macro_rules! token_punct_parser {
179 ($s:tt pub struct $name:ident) => {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700180 #[cfg(feature = "printing")]
181 impl ::quote::ToTokens for $name {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700182 fn to_tokens(&self, tokens: &mut ::proc_macro2::TokenStream) {
David Tolnay73c98de2017-12-31 15:56:56 -0500183 printing::punct($s, &self.0, tokens);
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700184 }
185 }
186
187 #[cfg(feature = "parsing")]
188 impl ::Synom for $name {
David Tolnaydfc886b2018-01-06 08:03:09 -0800189 fn parse(tokens: $crate::buffer::Cursor) -> $crate::synom::PResult<$name> {
David Tolnay73c98de2017-12-31 15:56:56 -0500190 parsing::punct($s, tokens, $name)
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700191 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800192
193 fn description() -> Option<&'static str> {
194 Some(concat!("`", $s, "`"))
195 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700196 }
David Tolnay94d2b792018-04-29 12:26:10 -0700197 };
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700198}
199
David Tolnay73c98de2017-12-31 15:56:56 -0500200macro_rules! token_keyword {
David Tolnay5a20f632017-12-26 22:11:28 -0500201 (#[$doc:meta] $s:tt pub struct $name:ident) => {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700202 #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))]
David Tolnay5a20f632017-12-26 22:11:28 -0500203 #[$doc]
David Tolnay1bb760c2018-01-07 11:18:15 -0800204 ///
205 /// Don't try to remember the name of this type -- use the [`Token!`]
206 /// macro instead.
207 ///
208 /// [`Token!`]: index.html
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700209 pub struct $name(pub Span);
210
David Tolnay66bb8d52018-01-08 08:22:31 -0800211 impl ::std::default::Default for $name {
212 fn default() -> Self {
Alex Crichton9a4dca22018-03-28 06:32:19 -0700213 $name(Span::call_site())
David Tolnay66bb8d52018-01-08 08:22:31 -0800214 }
215 }
216
David Tolnay98942562017-12-26 21:24:35 -0500217 #[cfg(feature = "extra-traits")]
218 impl ::std::fmt::Debug for $name {
219 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
220 f.write_str(stringify!($name))
221 }
222 }
223
224 #[cfg(feature = "extra-traits")]
225 impl ::std::cmp::Eq for $name {}
226
227 #[cfg(feature = "extra-traits")]
228 impl ::std::cmp::PartialEq for $name {
229 fn eq(&self, _other: &$name) -> bool {
230 true
231 }
232 }
233
234 #[cfg(feature = "extra-traits")]
235 impl ::std::hash::Hash for $name {
236 fn hash<H>(&self, _state: &mut H)
David Tolnay94d2b792018-04-29 12:26:10 -0700237 where
238 H: ::std::hash::Hasher,
239 {
240 }
David Tolnay98942562017-12-26 21:24:35 -0500241 }
242
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700243 #[cfg(feature = "printing")]
244 impl ::quote::ToTokens for $name {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700245 fn to_tokens(&self, tokens: &mut ::proc_macro2::TokenStream) {
David Tolnay73c98de2017-12-31 15:56:56 -0500246 printing::keyword($s, &self.0, tokens);
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700247 }
248 }
249
250 #[cfg(feature = "parsing")]
251 impl ::Synom for $name {
David Tolnaydfc886b2018-01-06 08:03:09 -0800252 fn parse(tokens: $crate::buffer::Cursor) -> $crate::synom::PResult<$name> {
David Tolnay73c98de2017-12-31 15:56:56 -0500253 parsing::keyword($s, tokens, $name)
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700254 }
David Tolnay79777332018-01-07 10:04:42 -0800255
256 fn description() -> Option<&'static str> {
257 Some(concat!("`", $s, "`"))
258 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700259 }
강동윤d5229da2018-01-12 13:11:33 +0900260
Alex Crichtona74a1c82018-05-16 10:20:44 -0700261 impl From<$name> for Ident {
262 fn from(me: $name) -> Ident {
263 Ident::new($s, me.0)
264 }
265 }
266
강동윤d5229da2018-01-12 13:11:33 +0900267 impl From<Span> for $name {
268 fn from(span: Span) -> Self {
269 $name(span)
270 }
271 }
David Tolnay94d2b792018-04-29 12:26:10 -0700272 };
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700273}
274
David Tolnay73c98de2017-12-31 15:56:56 -0500275macro_rules! token_delimiter {
David Tolnay5a20f632017-12-26 22:11:28 -0500276 (#[$doc:meta] $s:tt pub struct $name:ident) => {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700277 #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))]
David Tolnay5a20f632017-12-26 22:11:28 -0500278 #[$doc]
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700279 pub struct $name(pub Span);
280
David Tolnay66bb8d52018-01-08 08:22:31 -0800281 impl ::std::default::Default for $name {
282 fn default() -> Self {
Alex Crichton9a4dca22018-03-28 06:32:19 -0700283 $name(Span::call_site())
David Tolnay66bb8d52018-01-08 08:22:31 -0800284 }
285 }
286
David Tolnay98942562017-12-26 21:24:35 -0500287 #[cfg(feature = "extra-traits")]
288 impl ::std::fmt::Debug for $name {
289 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
290 f.write_str(stringify!($name))
291 }
292 }
293
294 #[cfg(feature = "extra-traits")]
295 impl ::std::cmp::Eq for $name {}
296
297 #[cfg(feature = "extra-traits")]
298 impl ::std::cmp::PartialEq for $name {
299 fn eq(&self, _other: &$name) -> bool {
300 true
301 }
302 }
303
304 #[cfg(feature = "extra-traits")]
305 impl ::std::hash::Hash for $name {
306 fn hash<H>(&self, _state: &mut H)
David Tolnay94d2b792018-04-29 12:26:10 -0700307 where
308 H: ::std::hash::Hasher,
309 {
310 }
David Tolnay98942562017-12-26 21:24:35 -0500311 }
312
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700313 impl $name {
314 #[cfg(feature = "printing")]
Alex Crichtona74a1c82018-05-16 10:20:44 -0700315 pub fn surround<F>(&self, tokens: &mut ::proc_macro2::TokenStream, f: F)
David Tolnay94d2b792018-04-29 12:26:10 -0700316 where
Alex Crichtona74a1c82018-05-16 10:20:44 -0700317 F: FnOnce(&mut ::proc_macro2::TokenStream),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700318 {
319 printing::delim($s, &self.0, tokens, f);
320 }
321
322 #[cfg(feature = "parsing")]
David Tolnay94d2b792018-04-29 12:26:10 -0700323 pub fn parse<F, R>(
324 tokens: $crate::buffer::Cursor,
325 f: F,
326 ) -> $crate::synom::PResult<($name, R)>
327 where
328 F: FnOnce($crate::buffer::Cursor) -> $crate::synom::PResult<R>,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700329 {
330 parsing::delim($s, tokens, $name, f)
331 }
332 }
강동윤d5229da2018-01-12 13:11:33 +0900333
334 impl From<Span> for $name {
335 fn from(span: Span) -> Self {
336 $name(span)
337 }
338 }
David Tolnay94d2b792018-04-29 12:26:10 -0700339 };
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700340}
341
Sergio Benitezd14d5362018-04-28 15:38:25 -0700342token_punct_def! {
343 /// `_`
344 "_" pub struct Underscore/1
345}
346
347#[cfg(feature = "printing")]
348impl ::quote::ToTokens for Underscore {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700349 fn to_tokens(&self, tokens: &mut ::proc_macro2::TokenStream) {
350 use quote::TokenStreamExt;
351 tokens.append(::proc_macro2::Ident::new("_", self.0[0]));
Sergio Benitezd14d5362018-04-28 15:38:25 -0700352 }
353}
354
355#[cfg(feature = "parsing")]
356impl ::Synom for Underscore {
357 fn parse(input: ::buffer::Cursor) -> ::synom::PResult<Underscore> {
358 match input.term() {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700359 Some((term, rest)) => {
360 if term.to_string() == "_" {
361 Ok((Underscore([term.span()]), rest))
362 } else {
363 ::parse_error()
364 }
365 }
David Tolnay94d2b792018-04-29 12:26:10 -0700366 None => parsing::punct("_", input, Underscore),
Sergio Benitezd14d5362018-04-28 15:38:25 -0700367 }
368 }
369
370 fn description() -> Option<&'static str> {
371 Some("`_`")
372 }
373}
374
Alex Crichton131308c2018-05-18 14:00:24 -0700375token_punct_def! {
376 /// `'`
377 "'" pub struct Apostrophe/1
378}
379
380#[cfg(feature = "printing")]
381impl ::quote::ToTokens for Apostrophe {
382 fn to_tokens(&self, tokens: &mut ::proc_macro2::TokenStream) {
383 use quote::TokenStreamExt;
384 let mut token = ::proc_macro2::Punct::new('\'', ::proc_macro2::Spacing::Joint);
385 token.set_span(self.0[0]);
386 tokens.append(token);
387 }
388}
389
390#[cfg(feature = "parsing")]
391impl ::Synom for Apostrophe {
392 fn parse(input: ::buffer::Cursor) -> ::synom::PResult<Apostrophe> {
393 match input.op() {
394 Some((op, rest)) => {
395 if op.as_char() == '\'' && op.spacing() == ::proc_macro2::Spacing::Joint {
396 Ok((Apostrophe([op.span()]), rest))
397 } else {
398 ::parse_error()
399 }
400 }
401 None => ::parse_error()
402 }
403 }
404
405 fn description() -> Option<&'static str> {
406 Some("`'`")
407 }
408}
409
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700410tokens! {
David Tolnay73c98de2017-12-31 15:56:56 -0500411 punct: {
David Tolnay5a20f632017-12-26 22:11:28 -0500412 "+" pub struct Add/1 /// `+`
413 "+=" pub struct AddEq/2 /// `+=`
414 "&" pub struct And/1 /// `&`
415 "&&" pub struct AndAnd/2 /// `&&`
416 "&=" pub struct AndEq/2 /// `&=`
417 "@" pub struct At/1 /// `@`
418 "!" pub struct Bang/1 /// `!`
419 "^" pub struct Caret/1 /// `^`
David Tolnay32954ef2017-12-26 22:43:16 -0500420 "^=" pub struct CaretEq/2 /// `^=`
David Tolnay5a20f632017-12-26 22:11:28 -0500421 ":" pub struct Colon/1 /// `:`
422 "::" pub struct Colon2/2 /// `::`
423 "," pub struct Comma/1 /// `,`
424 "/" pub struct Div/1 /// `/`
425 "/=" pub struct DivEq/2 /// `/=`
MSleepyPandaf5137d72018-05-05 18:57:10 +0200426 "$" pub struct Dollar/1 /// `$`
David Tolnay5a20f632017-12-26 22:11:28 -0500427 "." pub struct Dot/1 /// `.`
428 ".." pub struct Dot2/2 /// `..`
429 "..." pub struct Dot3/3 /// `...`
430 "..=" pub struct DotDotEq/3 /// `..=`
431 "=" pub struct Eq/1 /// `=`
432 "==" pub struct EqEq/2 /// `==`
433 ">=" pub struct Ge/2 /// `>=`
434 ">" pub struct Gt/1 /// `>`
435 "<=" pub struct Le/2 /// `<=`
436 "<" pub struct Lt/1 /// `<`
437 "*=" pub struct MulEq/2 /// `*=`
438 "!=" pub struct Ne/2 /// `!=`
439 "|" pub struct Or/1 /// `|`
440 "|=" pub struct OrEq/2 /// `|=`
441 "||" pub struct OrOr/2 /// `||`
442 "#" pub struct Pound/1 /// `#`
443 "?" pub struct Question/1 /// `?`
444 "->" pub struct RArrow/2 /// `->`
445 "<-" pub struct LArrow/2 /// `<-`
446 "%" pub struct Rem/1 /// `%`
447 "%=" pub struct RemEq/2 /// `%=`
David Tolnay17624152018-03-31 18:11:40 +0200448 "=>" pub struct FatArrow/2 /// `=>`
David Tolnay5a20f632017-12-26 22:11:28 -0500449 ";" pub struct Semi/1 /// `;`
450 "<<" pub struct Shl/2 /// `<<`
451 "<<=" pub struct ShlEq/3 /// `<<=`
452 ">>" pub struct Shr/2 /// `>>`
453 ">>=" pub struct ShrEq/3 /// `>>=`
454 "*" pub struct Star/1 /// `*`
455 "-" pub struct Sub/1 /// `-`
456 "-=" pub struct SubEq/2 /// `-=`
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700457 }
David Tolnay73c98de2017-12-31 15:56:56 -0500458 delimiter: {
David Tolnay5a20f632017-12-26 22:11:28 -0500459 "{" pub struct Brace /// `{...}`
460 "[" pub struct Bracket /// `[...]`
461 "(" pub struct Paren /// `(...)`
462 " " pub struct Group /// None-delimited group
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700463 }
David Tolnay73c98de2017-12-31 15:56:56 -0500464 keyword: {
David Tolnay5a20f632017-12-26 22:11:28 -0500465 "as" pub struct As /// `as`
466 "auto" pub struct Auto /// `auto`
467 "box" pub struct Box /// `box`
468 "break" pub struct Break /// `break`
469 "Self" pub struct CapSelf /// `Self`
470 "catch" pub struct Catch /// `catch`
471 "const" pub struct Const /// `const`
472 "continue" pub struct Continue /// `continue`
473 "crate" pub struct Crate /// `crate`
474 "default" pub struct Default /// `default`
475 "do" pub struct Do /// `do`
476 "dyn" pub struct Dyn /// `dyn`
477 "else" pub struct Else /// `else`
478 "enum" pub struct Enum /// `enum`
479 "extern" pub struct Extern /// `extern`
480 "fn" pub struct Fn /// `fn`
481 "for" pub struct For /// `for`
482 "if" pub struct If /// `if`
483 "impl" pub struct Impl /// `impl`
484 "in" pub struct In /// `in`
485 "let" pub struct Let /// `let`
486 "loop" pub struct Loop /// `loop`
487 "macro" pub struct Macro /// `macro`
488 "match" pub struct Match /// `match`
489 "mod" pub struct Mod /// `mod`
490 "move" pub struct Move /// `move`
491 "mut" pub struct Mut /// `mut`
492 "pub" pub struct Pub /// `pub`
493 "ref" pub struct Ref /// `ref`
494 "return" pub struct Return /// `return`
495 "self" pub struct Self_ /// `self`
496 "static" pub struct Static /// `static`
497 "struct" pub struct Struct /// `struct`
498 "super" pub struct Super /// `super`
499 "trait" pub struct Trait /// `trait`
500 "type" pub struct Type /// `type`
501 "union" pub struct Union /// `union`
502 "unsafe" pub struct Unsafe /// `unsafe`
503 "use" pub struct Use /// `use`
504 "where" pub struct Where /// `where`
505 "while" pub struct While /// `while`
506 "yield" pub struct Yield /// `yield`
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700507 }
508}
509
David Tolnayf005f962018-01-06 21:19:41 -0800510/// A type-macro that expands to the name of the Rust type representation of a
511/// given token.
512///
513/// See the [token module] documentation for details and examples.
514///
515/// [token module]: token/index.html
David Tolnayf8db7ba2017-11-11 22:52:16 -0800516// Unfortunate duplication due to a rustdoc bug.
517// https://github.com/rust-lang/rust/issues/45939
518#[macro_export]
David Tolnaya5ed2fe2018-04-29 12:23:34 -0700519#[cfg_attr(rustfmt, rustfmt_skip)]
David Tolnayf8db7ba2017-11-11 22:52:16 -0800520macro_rules! Token {
David Tolnay32954ef2017-12-26 22:43:16 -0500521 (+) => { $crate::token::Add };
522 (+=) => { $crate::token::AddEq };
523 (&) => { $crate::token::And };
524 (&&) => { $crate::token::AndAnd };
525 (&=) => { $crate::token::AndEq };
526 (@) => { $crate::token::At };
527 (!) => { $crate::token::Bang };
528 (^) => { $crate::token::Caret };
529 (^=) => { $crate::token::CaretEq };
530 (:) => { $crate::token::Colon };
531 (::) => { $crate::token::Colon2 };
532 (,) => { $crate::token::Comma };
533 (/) => { $crate::token::Div };
534 (/=) => { $crate::token::DivEq };
535 (.) => { $crate::token::Dot };
536 (..) => { $crate::token::Dot2 };
537 (...) => { $crate::token::Dot3 };
538 (..=) => { $crate::token::DotDotEq };
539 (=) => { $crate::token::Eq };
540 (==) => { $crate::token::EqEq };
541 (>=) => { $crate::token::Ge };
542 (>) => { $crate::token::Gt };
543 (<=) => { $crate::token::Le };
544 (<) => { $crate::token::Lt };
545 (*=) => { $crate::token::MulEq };
546 (!=) => { $crate::token::Ne };
547 (|) => { $crate::token::Or };
548 (|=) => { $crate::token::OrEq };
549 (||) => { $crate::token::OrOr };
550 (#) => { $crate::token::Pound };
551 (?) => { $crate::token::Question };
552 (->) => { $crate::token::RArrow };
553 (<-) => { $crate::token::LArrow };
554 (%) => { $crate::token::Rem };
555 (%=) => { $crate::token::RemEq };
David Tolnay17624152018-03-31 18:11:40 +0200556 (=>) => { $crate::token::FatArrow };
David Tolnay32954ef2017-12-26 22:43:16 -0500557 (;) => { $crate::token::Semi };
558 (<<) => { $crate::token::Shl };
559 (<<=) => { $crate::token::ShlEq };
560 (>>) => { $crate::token::Shr };
561 (>>=) => { $crate::token::ShrEq };
562 (*) => { $crate::token::Star };
563 (-) => { $crate::token::Sub };
564 (-=) => { $crate::token::SubEq };
565 (_) => { $crate::token::Underscore };
566 (as) => { $crate::token::As };
567 (auto) => { $crate::token::Auto };
568 (box) => { $crate::token::Box };
569 (break) => { $crate::token::Break };
570 (Self) => { $crate::token::CapSelf };
571 (catch) => { $crate::token::Catch };
572 (const) => { $crate::token::Const };
573 (continue) => { $crate::token::Continue };
574 (crate) => { $crate::token::Crate };
575 (default) => { $crate::token::Default };
576 (do) => { $crate::token::Do };
577 (dyn) => { $crate::token::Dyn };
578 (else) => { $crate::token::Else };
579 (enum) => { $crate::token::Enum };
580 (extern) => { $crate::token::Extern };
581 (fn) => { $crate::token::Fn };
582 (for) => { $crate::token::For };
583 (if) => { $crate::token::If };
584 (impl) => { $crate::token::Impl };
585 (in) => { $crate::token::In };
586 (let) => { $crate::token::Let };
587 (loop) => { $crate::token::Loop };
588 (macro) => { $crate::token::Macro };
589 (match) => { $crate::token::Match };
590 (mod) => { $crate::token::Mod };
591 (move) => { $crate::token::Move };
592 (mut) => { $crate::token::Mut };
593 (pub) => { $crate::token::Pub };
594 (ref) => { $crate::token::Ref };
595 (return) => { $crate::token::Return };
596 (self) => { $crate::token::Self_ };
597 (static) => { $crate::token::Static };
598 (struct) => { $crate::token::Struct };
599 (super) => { $crate::token::Super };
600 (trait) => { $crate::token::Trait };
601 (type) => { $crate::token::Type };
602 (union) => { $crate::token::Union };
603 (unsafe) => { $crate::token::Unsafe };
604 (use) => { $crate::token::Use };
605 (where) => { $crate::token::Where };
606 (while) => { $crate::token::While };
607 (yield) => { $crate::token::Yield };
David Tolnayf8db7ba2017-11-11 22:52:16 -0800608}
609
David Tolnayf005f962018-01-06 21:19:41 -0800610/// Parse a single Rust punctuation token.
611///
612/// See the [token module] documentation for details and examples.
613///
614/// [token module]: token/index.html
David Tolnay461d98e2018-01-07 11:07:19 -0800615///
616/// *This macro is available if Syn is built with the `"parsing"` feature.*
David Tolnay0fbe3282017-12-26 21:46:16 -0500617#[cfg(feature = "parsing")]
David Tolnayf8db7ba2017-11-11 22:52:16 -0800618#[macro_export]
David Tolnaya5ed2fe2018-04-29 12:23:34 -0700619#[cfg_attr(rustfmt, rustfmt_skip)]
David Tolnayf8db7ba2017-11-11 22:52:16 -0800620macro_rules! punct {
David Tolnay32954ef2017-12-26 22:43:16 -0500621 ($i:expr, +) => { call!($i, <$crate::token::Add as $crate::synom::Synom>::parse) };
622 ($i:expr, +=) => { call!($i, <$crate::token::AddEq as $crate::synom::Synom>::parse) };
623 ($i:expr, &) => { call!($i, <$crate::token::And as $crate::synom::Synom>::parse) };
624 ($i:expr, &&) => { call!($i, <$crate::token::AndAnd as $crate::synom::Synom>::parse) };
625 ($i:expr, &=) => { call!($i, <$crate::token::AndEq as $crate::synom::Synom>::parse) };
626 ($i:expr, @) => { call!($i, <$crate::token::At as $crate::synom::Synom>::parse) };
627 ($i:expr, !) => { call!($i, <$crate::token::Bang as $crate::synom::Synom>::parse) };
628 ($i:expr, ^) => { call!($i, <$crate::token::Caret as $crate::synom::Synom>::parse) };
629 ($i:expr, ^=) => { call!($i, <$crate::token::CaretEq as $crate::synom::Synom>::parse) };
630 ($i:expr, :) => { call!($i, <$crate::token::Colon as $crate::synom::Synom>::parse) };
631 ($i:expr, ::) => { call!($i, <$crate::token::Colon2 as $crate::synom::Synom>::parse) };
632 ($i:expr, ,) => { call!($i, <$crate::token::Comma as $crate::synom::Synom>::parse) };
633 ($i:expr, /) => { call!($i, <$crate::token::Div as $crate::synom::Synom>::parse) };
634 ($i:expr, /=) => { call!($i, <$crate::token::DivEq as $crate::synom::Synom>::parse) };
635 ($i:expr, .) => { call!($i, <$crate::token::Dot as $crate::synom::Synom>::parse) };
636 ($i:expr, ..) => { call!($i, <$crate::token::Dot2 as $crate::synom::Synom>::parse) };
637 ($i:expr, ...) => { call!($i, <$crate::token::Dot3 as $crate::synom::Synom>::parse) };
638 ($i:expr, ..=) => { call!($i, <$crate::token::DotDotEq as $crate::synom::Synom>::parse) };
639 ($i:expr, =) => { call!($i, <$crate::token::Eq as $crate::synom::Synom>::parse) };
640 ($i:expr, ==) => { call!($i, <$crate::token::EqEq as $crate::synom::Synom>::parse) };
641 ($i:expr, >=) => { call!($i, <$crate::token::Ge as $crate::synom::Synom>::parse) };
642 ($i:expr, >) => { call!($i, <$crate::token::Gt as $crate::synom::Synom>::parse) };
643 ($i:expr, <=) => { call!($i, <$crate::token::Le as $crate::synom::Synom>::parse) };
644 ($i:expr, <) => { call!($i, <$crate::token::Lt as $crate::synom::Synom>::parse) };
645 ($i:expr, *=) => { call!($i, <$crate::token::MulEq as $crate::synom::Synom>::parse) };
646 ($i:expr, !=) => { call!($i, <$crate::token::Ne as $crate::synom::Synom>::parse) };
647 ($i:expr, |) => { call!($i, <$crate::token::Or as $crate::synom::Synom>::parse) };
648 ($i:expr, |=) => { call!($i, <$crate::token::OrEq as $crate::synom::Synom>::parse) };
649 ($i:expr, ||) => { call!($i, <$crate::token::OrOr as $crate::synom::Synom>::parse) };
650 ($i:expr, #) => { call!($i, <$crate::token::Pound as $crate::synom::Synom>::parse) };
651 ($i:expr, ?) => { call!($i, <$crate::token::Question as $crate::synom::Synom>::parse) };
652 ($i:expr, ->) => { call!($i, <$crate::token::RArrow as $crate::synom::Synom>::parse) };
653 ($i:expr, <-) => { call!($i, <$crate::token::LArrow as $crate::synom::Synom>::parse) };
654 ($i:expr, %) => { call!($i, <$crate::token::Rem as $crate::synom::Synom>::parse) };
655 ($i:expr, %=) => { call!($i, <$crate::token::RemEq as $crate::synom::Synom>::parse) };
David Tolnay17624152018-03-31 18:11:40 +0200656 ($i:expr, =>) => { call!($i, <$crate::token::FatArrow as $crate::synom::Synom>::parse) };
David Tolnay32954ef2017-12-26 22:43:16 -0500657 ($i:expr, ;) => { call!($i, <$crate::token::Semi as $crate::synom::Synom>::parse) };
658 ($i:expr, <<) => { call!($i, <$crate::token::Shl as $crate::synom::Synom>::parse) };
659 ($i:expr, <<=) => { call!($i, <$crate::token::ShlEq as $crate::synom::Synom>::parse) };
660 ($i:expr, >>) => { call!($i, <$crate::token::Shr as $crate::synom::Synom>::parse) };
661 ($i:expr, >>=) => { call!($i, <$crate::token::ShrEq as $crate::synom::Synom>::parse) };
662 ($i:expr, *) => { call!($i, <$crate::token::Star as $crate::synom::Synom>::parse) };
663 ($i:expr, -) => { call!($i, <$crate::token::Sub as $crate::synom::Synom>::parse) };
664 ($i:expr, -=) => { call!($i, <$crate::token::SubEq as $crate::synom::Synom>::parse) };
665 ($i:expr, _) => { call!($i, <$crate::token::Underscore as $crate::synom::Synom>::parse) };
David Tolnayf8db7ba2017-11-11 22:52:16 -0800666}
667
David Tolnayf005f962018-01-06 21:19:41 -0800668/// Parse a single Rust keyword token.
669///
670/// See the [token module] documentation for details and examples.
671///
672/// [token module]: token/index.html
David Tolnay461d98e2018-01-07 11:07:19 -0800673///
674/// *This macro is available if Syn is built with the `"parsing"` feature.*
David Tolnay0fbe3282017-12-26 21:46:16 -0500675#[cfg(feature = "parsing")]
David Tolnayf8db7ba2017-11-11 22:52:16 -0800676#[macro_export]
David Tolnaya5ed2fe2018-04-29 12:23:34 -0700677#[cfg_attr(rustfmt, rustfmt_skip)]
David Tolnayf8db7ba2017-11-11 22:52:16 -0800678macro_rules! keyword {
David Tolnay32954ef2017-12-26 22:43:16 -0500679 ($i:expr, as) => { call!($i, <$crate::token::As as $crate::synom::Synom>::parse) };
680 ($i:expr, auto) => { call!($i, <$crate::token::Auto as $crate::synom::Synom>::parse) };
681 ($i:expr, box) => { call!($i, <$crate::token::Box as $crate::synom::Synom>::parse) };
682 ($i:expr, break) => { call!($i, <$crate::token::Break as $crate::synom::Synom>::parse) };
683 ($i:expr, Self) => { call!($i, <$crate::token::CapSelf as $crate::synom::Synom>::parse) };
684 ($i:expr, catch) => { call!($i, <$crate::token::Catch as $crate::synom::Synom>::parse) };
685 ($i:expr, const) => { call!($i, <$crate::token::Const as $crate::synom::Synom>::parse) };
686 ($i:expr, continue) => { call!($i, <$crate::token::Continue as $crate::synom::Synom>::parse) };
687 ($i:expr, crate) => { call!($i, <$crate::token::Crate as $crate::synom::Synom>::parse) };
688 ($i:expr, default) => { call!($i, <$crate::token::Default as $crate::synom::Synom>::parse) };
689 ($i:expr, do) => { call!($i, <$crate::token::Do as $crate::synom::Synom>::parse) };
690 ($i:expr, dyn) => { call!($i, <$crate::token::Dyn as $crate::synom::Synom>::parse) };
691 ($i:expr, else) => { call!($i, <$crate::token::Else as $crate::synom::Synom>::parse) };
692 ($i:expr, enum) => { call!($i, <$crate::token::Enum as $crate::synom::Synom>::parse) };
693 ($i:expr, extern) => { call!($i, <$crate::token::Extern as $crate::synom::Synom>::parse) };
694 ($i:expr, fn) => { call!($i, <$crate::token::Fn as $crate::synom::Synom>::parse) };
695 ($i:expr, for) => { call!($i, <$crate::token::For as $crate::synom::Synom>::parse) };
696 ($i:expr, if) => { call!($i, <$crate::token::If as $crate::synom::Synom>::parse) };
697 ($i:expr, impl) => { call!($i, <$crate::token::Impl as $crate::synom::Synom>::parse) };
698 ($i:expr, in) => { call!($i, <$crate::token::In as $crate::synom::Synom>::parse) };
699 ($i:expr, let) => { call!($i, <$crate::token::Let as $crate::synom::Synom>::parse) };
700 ($i:expr, loop) => { call!($i, <$crate::token::Loop as $crate::synom::Synom>::parse) };
701 ($i:expr, macro) => { call!($i, <$crate::token::Macro as $crate::synom::Synom>::parse) };
702 ($i:expr, match) => { call!($i, <$crate::token::Match as $crate::synom::Synom>::parse) };
703 ($i:expr, mod) => { call!($i, <$crate::token::Mod as $crate::synom::Synom>::parse) };
704 ($i:expr, move) => { call!($i, <$crate::token::Move as $crate::synom::Synom>::parse) };
705 ($i:expr, mut) => { call!($i, <$crate::token::Mut as $crate::synom::Synom>::parse) };
706 ($i:expr, pub) => { call!($i, <$crate::token::Pub as $crate::synom::Synom>::parse) };
707 ($i:expr, ref) => { call!($i, <$crate::token::Ref as $crate::synom::Synom>::parse) };
708 ($i:expr, return) => { call!($i, <$crate::token::Return as $crate::synom::Synom>::parse) };
709 ($i:expr, self) => { call!($i, <$crate::token::Self_ as $crate::synom::Synom>::parse) };
710 ($i:expr, static) => { call!($i, <$crate::token::Static as $crate::synom::Synom>::parse) };
711 ($i:expr, struct) => { call!($i, <$crate::token::Struct as $crate::synom::Synom>::parse) };
712 ($i:expr, super) => { call!($i, <$crate::token::Super as $crate::synom::Synom>::parse) };
713 ($i:expr, trait) => { call!($i, <$crate::token::Trait as $crate::synom::Synom>::parse) };
714 ($i:expr, type) => { call!($i, <$crate::token::Type as $crate::synom::Synom>::parse) };
715 ($i:expr, union) => { call!($i, <$crate::token::Union as $crate::synom::Synom>::parse) };
716 ($i:expr, unsafe) => { call!($i, <$crate::token::Unsafe as $crate::synom::Synom>::parse) };
717 ($i:expr, use) => { call!($i, <$crate::token::Use as $crate::synom::Synom>::parse) };
718 ($i:expr, where) => { call!($i, <$crate::token::Where as $crate::synom::Synom>::parse) };
719 ($i:expr, while) => { call!($i, <$crate::token::While as $crate::synom::Synom>::parse) };
720 ($i:expr, yield) => { call!($i, <$crate::token::Yield as $crate::synom::Synom>::parse) };
David Tolnayf8db7ba2017-11-11 22:52:16 -0800721}
722
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700723#[cfg(feature = "parsing")]
724mod parsing {
David Tolnay51382052017-12-27 13:46:21 -0500725 use proc_macro2::{Delimiter, Spacing, Span};
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700726
David Tolnaydfc886b2018-01-06 08:03:09 -0800727 use buffer::Cursor;
David Tolnay203557a2017-12-27 23:59:33 -0500728 use parse_error;
729 use synom::PResult;
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700730
731 pub trait FromSpans: Sized {
732 fn from_spans(spans: &[Span]) -> Self;
733 }
734
735 impl FromSpans for [Span; 1] {
736 fn from_spans(spans: &[Span]) -> Self {
737 [spans[0]]
738 }
739 }
740
741 impl FromSpans for [Span; 2] {
742 fn from_spans(spans: &[Span]) -> Self {
743 [spans[0], spans[1]]
744 }
745 }
746
747 impl FromSpans for [Span; 3] {
748 fn from_spans(spans: &[Span]) -> Self {
749 [spans[0], spans[1], spans[2]]
750 }
751 }
752
David Tolnay2b069542018-05-09 12:59:36 -0700753 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 -0500754 where
755 T: FromSpans,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700756 {
Alex Crichton9a4dca22018-03-28 06:32:19 -0700757 let mut spans = [Span::call_site(); 3];
Alex Crichton954046c2017-05-30 21:49:42 -0700758 assert!(s.len() <= spans.len());
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700759 let chars = s.chars();
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700760
Alex Crichton954046c2017-05-30 21:49:42 -0700761 for (i, (ch, slot)) in chars.zip(&mut spans).enumerate() {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400762 match tokens.op() {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700763 Some((op, rest)) => {
764 if op.as_char() == ch {
765 if i != s.len() - 1 {
766 match op.spacing() {
767 Spacing::Joint => {}
768 _ => return parse_error(),
769 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400770 }
Alex Crichtona74a1c82018-05-16 10:20:44 -0700771 *slot = op.span();
772 tokens = rest;
773 } else {
774 return parse_error()
Michael Layzell0a1a6632017-06-02 18:07:43 -0400775 }
Alex Crichton954046c2017-05-30 21:49:42 -0700776 }
David Tolnay51382052017-12-27 13:46:21 -0500777 _ => return parse_error(),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700778 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700779 }
David Tolnay2b069542018-05-09 12:59:36 -0700780 Ok((new(T::from_spans(&spans)), tokens))
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700781 }
782
David Tolnayb57c8492018-05-05 00:32:04 -0700783 pub fn keyword<'a, T>(
784 keyword: &str,
785 tokens: Cursor<'a>,
David Tolnay2b069542018-05-09 12:59:36 -0700786 new: fn(Span) -> T,
David Tolnayb57c8492018-05-05 00:32:04 -0700787 ) -> PResult<'a, T> {
Alex Crichton9a4dca22018-03-28 06:32:19 -0700788 if let Some((term, rest)) = tokens.term() {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700789 if term.to_string() == keyword {
David Tolnay2b069542018-05-09 12:59:36 -0700790 return Ok((new(term.span()), rest));
Michael Layzell0a1a6632017-06-02 18:07:43 -0400791 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700792 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400793 parse_error()
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700794 }
795
David Tolnay51382052017-12-27 13:46:21 -0500796 pub fn delim<'a, F, R, T>(
797 delim: &str,
798 tokens: Cursor<'a>,
David Tolnay2b069542018-05-09 12:59:36 -0700799 new: fn(Span) -> T,
David Tolnay51382052017-12-27 13:46:21 -0500800 f: F,
David Tolnay8875fca2017-12-31 13:52:37 -0500801 ) -> PResult<'a, (T, R)>
David Tolnay51382052017-12-27 13:46:21 -0500802 where
803 F: FnOnce(Cursor) -> PResult<R>,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700804 {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400805 // NOTE: We should support none-delimited sequences here.
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700806 let delim = match delim {
807 "(" => Delimiter::Parenthesis,
808 "{" => Delimiter::Brace,
809 "[" => Delimiter::Bracket,
Michael Layzell93c36282017-06-04 20:43:14 -0400810 " " => Delimiter::None,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700811 _ => panic!("unknown delimiter: {}", delim),
812 };
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700813
David Tolnay65729482017-12-31 16:14:50 -0500814 if let Some((inside, span, rest)) = tokens.group(delim) {
815 match f(inside) {
David Tolnayf4aa6b42017-12-31 16:40:33 -0500816 Ok((ret, remaining)) => {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400817 if remaining.eof() {
David Tolnay2b069542018-05-09 12:59:36 -0700818 return Ok(((new(span), ret), rest));
Michael Layzell0a1a6632017-06-02 18:07:43 -0400819 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700820 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400821 Err(err) => return Err(err),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700822 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700823 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400824 parse_error()
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700825 }
826}
827
828#[cfg(feature = "printing")]
829mod printing {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700830 use proc_macro2::{Delimiter, Group, Punct, Spacing, Span, Ident, TokenStream};
831 use quote::TokenStreamExt;
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700832
Alex Crichtona74a1c82018-05-16 10:20:44 -0700833 pub fn punct(s: &str, spans: &[Span], tokens: &mut TokenStream) {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700834 assert_eq!(s.len(), spans.len());
835
836 let mut chars = s.chars();
837 let mut spans = spans.iter();
838 let ch = chars.next_back().unwrap();
839 let span = spans.next_back().unwrap();
840 for (ch, span) in chars.zip(spans) {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700841 let mut op = Punct::new(ch, Spacing::Joint);
Alex Crichton9a4dca22018-03-28 06:32:19 -0700842 op.set_span(*span);
843 tokens.append(op);
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700844 }
845
Alex Crichtona74a1c82018-05-16 10:20:44 -0700846 let mut op = Punct::new(ch, Spacing::Alone);
Alex Crichton9a4dca22018-03-28 06:32:19 -0700847 op.set_span(*span);
848 tokens.append(op);
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700849 }
850
Alex Crichtona74a1c82018-05-16 10:20:44 -0700851 pub fn keyword(s: &str, span: &Span, tokens: &mut TokenStream) {
852 tokens.append(Ident::new(s, *span));
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700853 }
854
Alex Crichtona74a1c82018-05-16 10:20:44 -0700855 pub fn delim<F>(s: &str, span: &Span, tokens: &mut TokenStream, f: F)
David Tolnay51382052017-12-27 13:46:21 -0500856 where
Alex Crichtona74a1c82018-05-16 10:20:44 -0700857 F: FnOnce(&mut TokenStream),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700858 {
David Tolnay00ab6982017-12-31 18:15:06 -0500859 let delim = match s {
860 "(" => Delimiter::Parenthesis,
861 "[" => Delimiter::Bracket,
862 "{" => Delimiter::Brace,
863 " " => Delimiter::None,
864 _ => panic!("unknown delimiter: {}", s),
865 };
Alex Crichtona74a1c82018-05-16 10:20:44 -0700866 let mut inner = TokenStream::empty();
David Tolnay00ab6982017-12-31 18:15:06 -0500867 f(&mut inner);
Alex Crichton9a4dca22018-03-28 06:32:19 -0700868 let mut g = Group::new(delim, inner.into());
869 g.set_span(*span);
870 tokens.append(g);
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700871 }
872}