blob: 3ffe827a0d857e007a5f5df3e2556880947584f9 [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!(
David Tolnayf8106f82018-08-25 21:17:45 -040080//! attrs: many0!(Attribute::old_parse_outer) >>
David Tolnaye79ae182018-01-06 19:23:37 -080081//! 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
David Tolnay776f8e02018-08-24 22:32:10 -0400100use std;
David Tolnayd9836922018-08-25 18:05:36 -0400101#[cfg(feature = "parsing")]
102use std::cell::Cell;
David Tolnay776f8e02018-08-24 22:32:10 -0400103#[cfg(feature = "extra-traits")]
104use std::cmp;
105#[cfg(feature = "extra-traits")]
106use std::fmt::{self, Debug};
107#[cfg(feature = "extra-traits")]
108use std::hash::{Hash, Hasher};
David Tolnayd9836922018-08-25 18:05:36 -0400109#[cfg(feature = "parsing")]
110use std::rc::Rc;
David Tolnay776f8e02018-08-24 22:32:10 -0400111
David Tolnay2d84a082018-08-25 16:31:38 -0400112#[cfg(feature = "parsing")]
113use proc_macro2::Delimiter;
David Tolnay8c39ac52018-08-25 08:46:21 -0400114#[cfg(any(feature = "printing", feature = "parsing"))]
115use proc_macro2::Spacing;
David Tolnay65fb5662018-05-20 20:02:28 -0700116use proc_macro2::{Ident, Span};
Sergio Benitezd14d5362018-04-28 15:38:25 -0700117#[cfg(feature = "printing")]
David Tolnay776f8e02018-08-24 22:32:10 -0400118use proc_macro2::{Punct, TokenStream};
David Tolnay776f8e02018-08-24 22:32:10 -0400119#[cfg(feature = "printing")]
120use quote::{ToTokens, TokenStreamExt};
121
122#[cfg(feature = "parsing")]
David Tolnayad4b2472018-08-25 08:25:24 -0400123use error::Result;
124#[cfg(feature = "parsing")]
David Tolnay4fb71232018-08-25 23:14:50 -0400125use lifetime::Lifetime;
126#[cfg(feature = "parsing")]
127use lit::Lit;
128#[cfg(feature = "parsing")]
David Tolnayb6254182018-08-25 08:44:54 -0400129use lookahead;
David Tolnay776f8e02018-08-24 22:32:10 -0400130#[cfg(feature = "parsing")]
David Tolnayd9836922018-08-25 18:05:36 -0400131use parse::{Lookahead1, Parse, ParseBuffer, ParseStream};
David Tolnay776f8e02018-08-24 22:32:10 -0400132use span::IntoSpans;
133
134/// Marker trait for types that represent single tokens.
135///
136/// This trait is sealed and cannot be implemented for types outside of Syn.
137#[cfg(feature = "parsing")]
138pub trait Token: private::Sealed {
139 // Not public API.
140 #[doc(hidden)]
141 fn peek(lookahead: &Lookahead1) -> bool;
142
143 // Not public API.
144 #[doc(hidden)]
145 fn display() -> String;
Sergio Benitezd14d5362018-04-28 15:38:25 -0700146}
147
148#[cfg(feature = "parsing")]
David Tolnay776f8e02018-08-24 22:32:10 -0400149mod private {
150 pub trait Sealed {}
Sergio Benitezd14d5362018-04-28 15:38:25 -0700151}
152
David Tolnay776f8e02018-08-24 22:32:10 -0400153macro_rules! impl_token {
David Tolnay4fb71232018-08-25 23:14:50 -0400154 ($name:ident $display:expr) => {
David Tolnay776f8e02018-08-24 22:32:10 -0400155 #[cfg(feature = "parsing")]
156 impl Token for $name {
157 fn peek(lookahead: &Lookahead1) -> bool {
David Tolnayd9836922018-08-25 18:05:36 -0400158 // TODO factor out in a way that can be compiled just once
159 let scope = Span::call_site();
160 let cursor = lookahead.cursor();
161 let unexpected = Rc::new(Cell::new(None));
162 ParseBuffer::new(scope, cursor, unexpected)
163 .parse::<Self>()
164 .is_ok()
David Tolnay776f8e02018-08-24 22:32:10 -0400165 }
166
167 fn display() -> String {
David Tolnay4fb71232018-08-25 23:14:50 -0400168 $display.to_owned()
David Tolnay776f8e02018-08-24 22:32:10 -0400169 }
170 }
171
172 #[cfg(feature = "parsing")]
173 impl private::Sealed for $name {}
174 };
175}
176
David Tolnay4fb71232018-08-25 23:14:50 -0400177impl_token!(Ident "identifier");
178impl_token!(Lifetime "lifetime");
179impl_token!(Lit "literal");
180
David Tolnay776f8e02018-08-24 22:32:10 -0400181macro_rules! define_keywords {
182 ($($token:tt pub struct $name:ident #[$doc:meta])*) => {
183 $(
184 #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))]
185 #[$doc]
186 ///
187 /// Don't try to remember the name of this type -- use the [`Token!`]
188 /// macro instead.
189 ///
190 /// [`Token!`]: index.html
191 pub struct $name {
192 pub span: Span,
193 }
194
195 #[doc(hidden)]
196 #[allow(non_snake_case)]
197 pub fn $name<S: IntoSpans<[Span; 1]>>(span: S) -> $name {
198 $name {
199 span: span.into_spans()[0],
200 }
201 }
202
David Tolnay4fb71232018-08-25 23:14:50 -0400203 impl_token!($name concat!("`", $token, "`"));
David Tolnay776f8e02018-08-24 22:32:10 -0400204
205 impl std::default::Default for $name {
206 fn default() -> Self {
207 $name(Span::call_site())
208 }
209 }
210
211 #[cfg(feature = "extra-traits")]
212 impl Debug for $name {
213 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
214 f.write_str(stringify!($name))
215 }
216 }
217
218 #[cfg(feature = "extra-traits")]
219 impl cmp::Eq for $name {}
220
221 #[cfg(feature = "extra-traits")]
222 impl PartialEq for $name {
223 fn eq(&self, _other: &$name) -> bool {
224 true
225 }
226 }
227
228 #[cfg(feature = "extra-traits")]
229 impl Hash for $name {
230 fn hash<H: Hasher>(&self, _state: &mut H) {}
231 }
232
233 #[cfg(feature = "printing")]
234 impl ToTokens for $name {
235 fn to_tokens(&self, tokens: &mut TokenStream) {
236 printing::keyword($token, &self.span, tokens);
237 }
238 }
239
240 #[cfg(feature = "parsing")]
241 impl Parse for $name {
242 fn parse(input: ParseStream) -> Result<Self> {
243 parsing::keyword(input, $token).map($name)
244 }
245 }
246 )*
247 };
248}
249
250macro_rules! define_punctuation_structs {
251 ($($token:tt pub struct $name:ident/$len:tt #[$doc:meta])*) => {
252 $(
253 #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))]
254 #[$doc]
255 ///
256 /// Don't try to remember the name of this type -- use the [`Token!`]
257 /// macro instead.
258 ///
259 /// [`Token!`]: index.html
260 pub struct $name {
261 pub spans: [Span; $len],
262 }
263
264 #[doc(hidden)]
265 #[allow(non_snake_case)]
266 pub fn $name<S: IntoSpans<[Span; $len]>>(spans: S) -> $name {
267 $name {
268 spans: spans.into_spans(),
269 }
270 }
271
David Tolnay4fb71232018-08-25 23:14:50 -0400272 impl_token!($name concat!("`", $token, "`"));
David Tolnay776f8e02018-08-24 22:32:10 -0400273
274 impl std::default::Default for $name {
275 fn default() -> Self {
276 $name([Span::call_site(); $len])
277 }
278 }
279
280 #[cfg(feature = "extra-traits")]
281 impl Debug for $name {
282 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
283 f.write_str(stringify!($name))
284 }
285 }
286
287 #[cfg(feature = "extra-traits")]
288 impl cmp::Eq for $name {}
289
290 #[cfg(feature = "extra-traits")]
291 impl PartialEq for $name {
292 fn eq(&self, _other: &$name) -> bool {
293 true
294 }
295 }
296
297 #[cfg(feature = "extra-traits")]
298 impl Hash for $name {
299 fn hash<H: Hasher>(&self, _state: &mut H) {}
300 }
301 )*
302 };
303}
304
305macro_rules! define_punctuation {
306 ($($token:tt pub struct $name:ident/$len:tt #[$doc:meta])*) => {
307 $(
308 define_punctuation_structs! {
309 $token pub struct $name/$len #[$doc]
310 }
311
312 #[cfg(feature = "printing")]
313 impl ToTokens for $name {
314 fn to_tokens(&self, tokens: &mut TokenStream) {
315 printing::punct($token, &self.spans, tokens);
316 }
317 }
318
319 #[cfg(feature = "parsing")]
320 impl Parse for $name {
321 fn parse(input: ParseStream) -> Result<Self> {
322 parsing::punct(input, $token).map($name::<[Span; $len]>)
323 }
324 }
325 )*
326 };
327}
328
329macro_rules! define_delimiters {
330 ($($token:tt pub struct $name:ident #[$doc:meta])*) => {
331 $(
332 #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))]
333 #[$doc]
334 pub struct $name {
335 pub span: Span,
336 }
337
338 #[doc(hidden)]
339 #[allow(non_snake_case)]
340 pub fn $name<S: IntoSpans<[Span; 1]>>(span: S) -> $name {
341 $name {
342 span: span.into_spans()[0],
343 }
344 }
345
346 impl std::default::Default for $name {
347 fn default() -> Self {
348 $name(Span::call_site())
349 }
350 }
351
352 #[cfg(feature = "extra-traits")]
353 impl Debug for $name {
354 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
355 f.write_str(stringify!($name))
356 }
357 }
358
359 #[cfg(feature = "extra-traits")]
360 impl cmp::Eq for $name {}
361
362 #[cfg(feature = "extra-traits")]
363 impl PartialEq for $name {
364 fn eq(&self, _other: &$name) -> bool {
365 true
366 }
367 }
368
369 #[cfg(feature = "extra-traits")]
370 impl Hash for $name {
371 fn hash<H: Hasher>(&self, _state: &mut H) {}
372 }
373
374 impl $name {
375 #[cfg(feature = "printing")]
376 pub fn surround<F>(&self, tokens: &mut TokenStream, f: F)
377 where
378 F: FnOnce(&mut TokenStream),
379 {
380 printing::delim($token, &self.span, tokens, f);
381 }
382
383 #[cfg(feature = "parsing")]
384 pub fn parse<F, R>(
385 tokens: $crate::buffer::Cursor,
386 f: F,
387 ) -> $crate::synom::PResult<($name, R)>
388 where
389 F: FnOnce($crate::buffer::Cursor) -> $crate::synom::PResult<R>,
390 {
391 parsing::delim($token, tokens, $name, f)
392 }
393 }
David Tolnay2d84a082018-08-25 16:31:38 -0400394
395 #[cfg(feature = "parsing")]
396 impl private::Sealed for $name {}
David Tolnay776f8e02018-08-24 22:32:10 -0400397 )*
398 };
399}
400
401define_punctuation_structs! {
402 "'" pub struct Apostrophe/1 /// `'`
403 "_" pub struct Underscore/1 /// `_`
Alex Crichton131308c2018-05-18 14:00:24 -0700404}
405
David Tolnayb2fc7ef2018-05-20 19:54:14 -0700406// Implement Clone anyway because it is required for cloning Lifetime.
407#[cfg(not(feature = "clone-impls"))]
408impl Clone for Apostrophe {
409 fn clone(&self) -> Self {
David Tolnay7ac699c2018-08-24 14:00:58 -0400410 Apostrophe(self.spans)
David Tolnayb2fc7ef2018-05-20 19:54:14 -0700411 }
412}
413
Alex Crichton131308c2018-05-18 14:00:24 -0700414#[cfg(feature = "printing")]
David Tolnay776f8e02018-08-24 22:32:10 -0400415impl ToTokens for Apostrophe {
416 fn to_tokens(&self, tokens: &mut TokenStream) {
417 let mut token = Punct::new('\'', Spacing::Joint);
David Tolnay7ac699c2018-08-24 14:00:58 -0400418 token.set_span(self.spans[0]);
Alex Crichton131308c2018-05-18 14:00:24 -0700419 tokens.append(token);
420 }
421}
422
423#[cfg(feature = "parsing")]
David Tolnay776f8e02018-08-24 22:32:10 -0400424impl Parse for Apostrophe {
425 fn parse(input: ParseStream) -> Result<Self> {
426 input.step_cursor(|cursor| {
427 if let Some((punct, rest)) = cursor.punct() {
428 if punct.as_char() == '\'' && punct.spacing() == Spacing::Joint {
429 return Ok((Apostrophe(punct.span()), rest));
Alex Crichton131308c2018-05-18 14:00:24 -0700430 }
431 }
David Tolnay776f8e02018-08-24 22:32:10 -0400432 Err(cursor.error("expected `'`"))
433 })
Alex Crichton131308c2018-05-18 14:00:24 -0700434 }
435}
436
David Tolnay776f8e02018-08-24 22:32:10 -0400437#[cfg(feature = "printing")]
438impl ToTokens for Underscore {
439 fn to_tokens(&self, tokens: &mut TokenStream) {
440 tokens.append(Ident::new("_", self.spans[0]));
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700441 }
David Tolnay776f8e02018-08-24 22:32:10 -0400442}
443
444#[cfg(feature = "parsing")]
445impl Parse for Underscore {
446 fn parse(input: ParseStream) -> Result<Self> {
447 input.step_cursor(|cursor| {
448 if let Some((ident, rest)) = cursor.ident() {
449 if ident == "_" {
450 return Ok((Underscore(ident.span()), rest));
451 }
452 }
453 if let Some((punct, rest)) = cursor.punct() {
454 if punct.as_char() == '_' {
455 return Ok((Underscore(punct.span()), rest));
456 }
457 }
458 Err(cursor.error("expected `_`"))
459 })
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700460 }
David Tolnay776f8e02018-08-24 22:32:10 -0400461}
462
David Tolnay2d84a082018-08-25 16:31:38 -0400463#[cfg(feature = "parsing")]
464impl Token for Paren {
465 fn peek(lookahead: &Lookahead1) -> bool {
466 lookahead::is_delimiter(lookahead, Delimiter::Parenthesis)
467 }
468
469 fn display() -> String {
470 "parentheses".to_owned()
471 }
472}
473
474#[cfg(feature = "parsing")]
475impl Token for Brace {
476 fn peek(lookahead: &Lookahead1) -> bool {
477 lookahead::is_delimiter(lookahead, Delimiter::Brace)
478 }
479
480 fn display() -> String {
481 "curly braces".to_owned()
482 }
483}
484
485#[cfg(feature = "parsing")]
486impl Token for Bracket {
487 fn peek(lookahead: &Lookahead1) -> bool {
488 lookahead::is_delimiter(lookahead, Delimiter::Bracket)
489 }
490
491 fn display() -> String {
492 "square brackets".to_owned()
493 }
494}
495
David Tolnay776f8e02018-08-24 22:32:10 -0400496define_keywords! {
497 "as" pub struct As /// `as`
498 "async" pub struct Async /// `async`
499 "auto" pub struct Auto /// `auto`
500 "box" pub struct Box /// `box`
501 "break" pub struct Break /// `break`
502 "Self" pub struct CapSelf /// `Self`
503 "const" pub struct Const /// `const`
504 "continue" pub struct Continue /// `continue`
505 "crate" pub struct Crate /// `crate`
506 "default" pub struct Default /// `default`
507 "dyn" pub struct Dyn /// `dyn`
508 "else" pub struct Else /// `else`
509 "enum" pub struct Enum /// `enum`
510 "existential" pub struct Existential /// `existential`
511 "extern" pub struct Extern /// `extern`
512 "fn" pub struct Fn /// `fn`
513 "for" pub struct For /// `for`
514 "if" pub struct If /// `if`
515 "impl" pub struct Impl /// `impl`
516 "in" pub struct In /// `in`
517 "let" pub struct Let /// `let`
518 "loop" pub struct Loop /// `loop`
519 "macro" pub struct Macro /// `macro`
520 "match" pub struct Match /// `match`
521 "mod" pub struct Mod /// `mod`
522 "move" pub struct Move /// `move`
523 "mut" pub struct Mut /// `mut`
524 "pub" pub struct Pub /// `pub`
525 "ref" pub struct Ref /// `ref`
526 "return" pub struct Return /// `return`
527 "self" pub struct Self_ /// `self`
528 "static" pub struct Static /// `static`
529 "struct" pub struct Struct /// `struct`
530 "super" pub struct Super /// `super`
531 "trait" pub struct Trait /// `trait`
532 "try" pub struct Try /// `try`
533 "type" pub struct Type /// `type`
534 "union" pub struct Union /// `union`
535 "unsafe" pub struct Unsafe /// `unsafe`
536 "use" pub struct Use /// `use`
537 "where" pub struct Where /// `where`
538 "while" pub struct While /// `while`
539 "yield" pub struct Yield /// `yield`
540}
541
542define_punctuation! {
543 "+" pub struct Add/1 /// `+`
544 "+=" pub struct AddEq/2 /// `+=`
545 "&" pub struct And/1 /// `&`
546 "&&" pub struct AndAnd/2 /// `&&`
547 "&=" pub struct AndEq/2 /// `&=`
548 "@" pub struct At/1 /// `@`
549 "!" pub struct Bang/1 /// `!`
550 "^" pub struct Caret/1 /// `^`
551 "^=" pub struct CaretEq/2 /// `^=`
552 ":" pub struct Colon/1 /// `:`
553 "::" pub struct Colon2/2 /// `::`
554 "," pub struct Comma/1 /// `,`
555 "/" pub struct Div/1 /// `/`
556 "/=" pub struct DivEq/2 /// `/=`
557 "$" pub struct Dollar/1 /// `$`
558 "." pub struct Dot/1 /// `.`
559 ".." pub struct Dot2/2 /// `..`
560 "..." pub struct Dot3/3 /// `...`
561 "..=" pub struct DotDotEq/3 /// `..=`
562 "=" pub struct Eq/1 /// `=`
563 "==" pub struct EqEq/2 /// `==`
564 ">=" pub struct Ge/2 /// `>=`
565 ">" pub struct Gt/1 /// `>`
566 "<=" pub struct Le/2 /// `<=`
567 "<" pub struct Lt/1 /// `<`
568 "*=" pub struct MulEq/2 /// `*=`
569 "!=" pub struct Ne/2 /// `!=`
570 "|" pub struct Or/1 /// `|`
571 "|=" pub struct OrEq/2 /// `|=`
572 "||" pub struct OrOr/2 /// `||`
573 "#" pub struct Pound/1 /// `#`
574 "?" pub struct Question/1 /// `?`
575 "->" pub struct RArrow/2 /// `->`
576 "<-" pub struct LArrow/2 /// `<-`
577 "%" pub struct Rem/1 /// `%`
578 "%=" pub struct RemEq/2 /// `%=`
579 "=>" pub struct FatArrow/2 /// `=>`
580 ";" pub struct Semi/1 /// `;`
581 "<<" pub struct Shl/2 /// `<<`
582 "<<=" pub struct ShlEq/3 /// `<<=`
583 ">>" pub struct Shr/2 /// `>>`
584 ">>=" pub struct ShrEq/3 /// `>>=`
585 "*" pub struct Star/1 /// `*`
586 "-" pub struct Sub/1 /// `-`
587 "-=" pub struct SubEq/2 /// `-=`
588}
589
590define_delimiters! {
591 "{" pub struct Brace /// `{...}`
592 "[" pub struct Bracket /// `[...]`
593 "(" pub struct Paren /// `(...)`
594 " " pub struct Group /// None-delimited group
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700595}
596
David Tolnayf005f962018-01-06 21:19:41 -0800597/// A type-macro that expands to the name of the Rust type representation of a
598/// given token.
599///
600/// See the [token module] documentation for details and examples.
601///
602/// [token module]: token/index.html
David Tolnayf8db7ba2017-11-11 22:52:16 -0800603// Unfortunate duplication due to a rustdoc bug.
604// https://github.com/rust-lang/rust/issues/45939
605#[macro_export]
David Tolnaya5ed2fe2018-04-29 12:23:34 -0700606#[cfg_attr(rustfmt, rustfmt_skip)]
David Tolnayf8db7ba2017-11-11 22:52:16 -0800607macro_rules! Token {
David Tolnay776f8e02018-08-24 22:32:10 -0400608 (as) => { $crate::token::As };
609 (async) => { $crate::token::Async };
610 (auto) => { $crate::token::Auto };
611 (box) => { $crate::token::Box };
612 (break) => { $crate::token::Break };
613 (Self) => { $crate::token::CapSelf };
614 (const) => { $crate::token::Const };
615 (continue) => { $crate::token::Continue };
616 (crate) => { $crate::token::Crate };
617 (default) => { $crate::token::Default };
618 (dyn) => { $crate::token::Dyn };
619 (else) => { $crate::token::Else };
620 (enum) => { $crate::token::Enum };
621 (existential) => { $crate::token::Existential };
622 (extern) => { $crate::token::Extern };
623 (fn) => { $crate::token::Fn };
624 (for) => { $crate::token::For };
625 (if) => { $crate::token::If };
626 (impl) => { $crate::token::Impl };
627 (in) => { $crate::token::In };
628 (let) => { $crate::token::Let };
629 (loop) => { $crate::token::Loop };
630 (macro) => { $crate::token::Macro };
631 (match) => { $crate::token::Match };
632 (mod) => { $crate::token::Mod };
633 (move) => { $crate::token::Move };
634 (mut) => { $crate::token::Mut };
635 (pub) => { $crate::token::Pub };
636 (ref) => { $crate::token::Ref };
637 (return) => { $crate::token::Return };
638 (self) => { $crate::token::Self_ };
639 (static) => { $crate::token::Static };
640 (struct) => { $crate::token::Struct };
641 (super) => { $crate::token::Super };
642 (trait) => { $crate::token::Trait };
643 (try) => { $crate::token::Try };
644 (type) => { $crate::token::Type };
645 (union) => { $crate::token::Union };
646 (unsafe) => { $crate::token::Unsafe };
647 (use) => { $crate::token::Use };
648 (where) => { $crate::token::Where };
649 (while) => { $crate::token::While };
650 (yield) => { $crate::token::Yield };
David Tolnaybb82ef02018-08-24 20:15:45 -0400651 (+) => { $crate::token::Add };
652 (+=) => { $crate::token::AddEq };
653 (&) => { $crate::token::And };
654 (&&) => { $crate::token::AndAnd };
655 (&=) => { $crate::token::AndEq };
656 (@) => { $crate::token::At };
657 (!) => { $crate::token::Bang };
658 (^) => { $crate::token::Caret };
659 (^=) => { $crate::token::CaretEq };
660 (:) => { $crate::token::Colon };
661 (::) => { $crate::token::Colon2 };
662 (,) => { $crate::token::Comma };
663 (/) => { $crate::token::Div };
664 (/=) => { $crate::token::DivEq };
665 (.) => { $crate::token::Dot };
666 (..) => { $crate::token::Dot2 };
667 (...) => { $crate::token::Dot3 };
668 (..=) => { $crate::token::DotDotEq };
669 (=) => { $crate::token::Eq };
670 (==) => { $crate::token::EqEq };
671 (>=) => { $crate::token::Ge };
672 (>) => { $crate::token::Gt };
673 (<=) => { $crate::token::Le };
674 (<) => { $crate::token::Lt };
675 (*=) => { $crate::token::MulEq };
676 (!=) => { $crate::token::Ne };
677 (|) => { $crate::token::Or };
678 (|=) => { $crate::token::OrEq };
679 (||) => { $crate::token::OrOr };
680 (#) => { $crate::token::Pound };
681 (?) => { $crate::token::Question };
682 (->) => { $crate::token::RArrow };
683 (<-) => { $crate::token::LArrow };
684 (%) => { $crate::token::Rem };
685 (%=) => { $crate::token::RemEq };
686 (=>) => { $crate::token::FatArrow };
687 (;) => { $crate::token::Semi };
688 (<<) => { $crate::token::Shl };
689 (<<=) => { $crate::token::ShlEq };
690 (>>) => { $crate::token::Shr };
691 (>>=) => { $crate::token::ShrEq };
692 (*) => { $crate::token::Star };
693 (-) => { $crate::token::Sub };
694 (-=) => { $crate::token::SubEq };
695 (_) => { $crate::token::Underscore };
David Tolnayf8db7ba2017-11-11 22:52:16 -0800696}
697
David Tolnayf005f962018-01-06 21:19:41 -0800698/// Parse a single Rust punctuation token.
699///
700/// See the [token module] documentation for details and examples.
701///
702/// [token module]: token/index.html
David Tolnay461d98e2018-01-07 11:07:19 -0800703///
704/// *This macro is available if Syn is built with the `"parsing"` feature.*
David Tolnay0fbe3282017-12-26 21:46:16 -0500705#[cfg(feature = "parsing")]
David Tolnayf8db7ba2017-11-11 22:52:16 -0800706#[macro_export]
David Tolnaya5ed2fe2018-04-29 12:23:34 -0700707#[cfg_attr(rustfmt, rustfmt_skip)]
David Tolnayf8db7ba2017-11-11 22:52:16 -0800708macro_rules! punct {
David Tolnay32954ef2017-12-26 22:43:16 -0500709 ($i:expr, +) => { call!($i, <$crate::token::Add as $crate::synom::Synom>::parse) };
710 ($i:expr, +=) => { call!($i, <$crate::token::AddEq as $crate::synom::Synom>::parse) };
711 ($i:expr, &) => { call!($i, <$crate::token::And as $crate::synom::Synom>::parse) };
712 ($i:expr, &&) => { call!($i, <$crate::token::AndAnd as $crate::synom::Synom>::parse) };
713 ($i:expr, &=) => { call!($i, <$crate::token::AndEq as $crate::synom::Synom>::parse) };
714 ($i:expr, @) => { call!($i, <$crate::token::At as $crate::synom::Synom>::parse) };
715 ($i:expr, !) => { call!($i, <$crate::token::Bang as $crate::synom::Synom>::parse) };
716 ($i:expr, ^) => { call!($i, <$crate::token::Caret as $crate::synom::Synom>::parse) };
717 ($i:expr, ^=) => { call!($i, <$crate::token::CaretEq as $crate::synom::Synom>::parse) };
718 ($i:expr, :) => { call!($i, <$crate::token::Colon as $crate::synom::Synom>::parse) };
719 ($i:expr, ::) => { call!($i, <$crate::token::Colon2 as $crate::synom::Synom>::parse) };
720 ($i:expr, ,) => { call!($i, <$crate::token::Comma as $crate::synom::Synom>::parse) };
721 ($i:expr, /) => { call!($i, <$crate::token::Div as $crate::synom::Synom>::parse) };
722 ($i:expr, /=) => { call!($i, <$crate::token::DivEq as $crate::synom::Synom>::parse) };
723 ($i:expr, .) => { call!($i, <$crate::token::Dot as $crate::synom::Synom>::parse) };
724 ($i:expr, ..) => { call!($i, <$crate::token::Dot2 as $crate::synom::Synom>::parse) };
725 ($i:expr, ...) => { call!($i, <$crate::token::Dot3 as $crate::synom::Synom>::parse) };
726 ($i:expr, ..=) => { call!($i, <$crate::token::DotDotEq as $crate::synom::Synom>::parse) };
727 ($i:expr, =) => { call!($i, <$crate::token::Eq as $crate::synom::Synom>::parse) };
728 ($i:expr, ==) => { call!($i, <$crate::token::EqEq as $crate::synom::Synom>::parse) };
729 ($i:expr, >=) => { call!($i, <$crate::token::Ge as $crate::synom::Synom>::parse) };
730 ($i:expr, >) => { call!($i, <$crate::token::Gt as $crate::synom::Synom>::parse) };
731 ($i:expr, <=) => { call!($i, <$crate::token::Le as $crate::synom::Synom>::parse) };
732 ($i:expr, <) => { call!($i, <$crate::token::Lt as $crate::synom::Synom>::parse) };
733 ($i:expr, *=) => { call!($i, <$crate::token::MulEq as $crate::synom::Synom>::parse) };
734 ($i:expr, !=) => { call!($i, <$crate::token::Ne as $crate::synom::Synom>::parse) };
735 ($i:expr, |) => { call!($i, <$crate::token::Or as $crate::synom::Synom>::parse) };
736 ($i:expr, |=) => { call!($i, <$crate::token::OrEq as $crate::synom::Synom>::parse) };
737 ($i:expr, ||) => { call!($i, <$crate::token::OrOr as $crate::synom::Synom>::parse) };
738 ($i:expr, #) => { call!($i, <$crate::token::Pound as $crate::synom::Synom>::parse) };
739 ($i:expr, ?) => { call!($i, <$crate::token::Question as $crate::synom::Synom>::parse) };
740 ($i:expr, ->) => { call!($i, <$crate::token::RArrow as $crate::synom::Synom>::parse) };
741 ($i:expr, <-) => { call!($i, <$crate::token::LArrow as $crate::synom::Synom>::parse) };
742 ($i:expr, %) => { call!($i, <$crate::token::Rem as $crate::synom::Synom>::parse) };
743 ($i:expr, %=) => { call!($i, <$crate::token::RemEq as $crate::synom::Synom>::parse) };
David Tolnay17624152018-03-31 18:11:40 +0200744 ($i:expr, =>) => { call!($i, <$crate::token::FatArrow as $crate::synom::Synom>::parse) };
David Tolnay32954ef2017-12-26 22:43:16 -0500745 ($i:expr, ;) => { call!($i, <$crate::token::Semi as $crate::synom::Synom>::parse) };
746 ($i:expr, <<) => { call!($i, <$crate::token::Shl as $crate::synom::Synom>::parse) };
747 ($i:expr, <<=) => { call!($i, <$crate::token::ShlEq as $crate::synom::Synom>::parse) };
748 ($i:expr, >>) => { call!($i, <$crate::token::Shr as $crate::synom::Synom>::parse) };
749 ($i:expr, >>=) => { call!($i, <$crate::token::ShrEq as $crate::synom::Synom>::parse) };
750 ($i:expr, *) => { call!($i, <$crate::token::Star as $crate::synom::Synom>::parse) };
751 ($i:expr, -) => { call!($i, <$crate::token::Sub as $crate::synom::Synom>::parse) };
752 ($i:expr, -=) => { call!($i, <$crate::token::SubEq as $crate::synom::Synom>::parse) };
753 ($i:expr, _) => { call!($i, <$crate::token::Underscore as $crate::synom::Synom>::parse) };
David Tolnayf8db7ba2017-11-11 22:52:16 -0800754}
755
David Tolnayf005f962018-01-06 21:19:41 -0800756/// Parse a single Rust keyword token.
757///
758/// See the [token module] documentation for details and examples.
759///
760/// [token module]: token/index.html
David Tolnay461d98e2018-01-07 11:07:19 -0800761///
762/// *This macro is available if Syn is built with the `"parsing"` feature.*
David Tolnay0fbe3282017-12-26 21:46:16 -0500763#[cfg(feature = "parsing")]
David Tolnayf8db7ba2017-11-11 22:52:16 -0800764#[macro_export]
David Tolnaya5ed2fe2018-04-29 12:23:34 -0700765#[cfg_attr(rustfmt, rustfmt_skip)]
David Tolnayf8db7ba2017-11-11 22:52:16 -0800766macro_rules! keyword {
David Tolnaybb82ef02018-08-24 20:15:45 -0400767 ($i:expr, as) => { call!($i, <$crate::token::As as $crate::synom::Synom>::parse) };
768 ($i:expr, async) => { call!($i, <$crate::token::Async as $crate::synom::Synom>::parse) };
769 ($i:expr, auto) => { call!($i, <$crate::token::Auto as $crate::synom::Synom>::parse) };
770 ($i:expr, box) => { call!($i, <$crate::token::Box as $crate::synom::Synom>::parse) };
771 ($i:expr, break) => { call!($i, <$crate::token::Break as $crate::synom::Synom>::parse) };
772 ($i:expr, Self) => { call!($i, <$crate::token::CapSelf as $crate::synom::Synom>::parse) };
773 ($i:expr, const) => { call!($i, <$crate::token::Const as $crate::synom::Synom>::parse) };
774 ($i:expr, continue) => { call!($i, <$crate::token::Continue as $crate::synom::Synom>::parse) };
775 ($i:expr, crate) => { call!($i, <$crate::token::Crate as $crate::synom::Synom>::parse) };
776 ($i:expr, default) => { call!($i, <$crate::token::Default as $crate::synom::Synom>::parse) };
777 ($i:expr, dyn) => { call!($i, <$crate::token::Dyn as $crate::synom::Synom>::parse) };
778 ($i:expr, else) => { call!($i, <$crate::token::Else as $crate::synom::Synom>::parse) };
779 ($i:expr, enum) => { call!($i, <$crate::token::Enum as $crate::synom::Synom>::parse) };
780 ($i:expr, extern) => { call!($i, <$crate::token::Extern as $crate::synom::Synom>::parse) };
781 ($i:expr, existential) => { call!($i, <$crate::token::Existential as $crate::synom::Synom>::parse) };
782 ($i:expr, fn) => { call!($i, <$crate::token::Fn as $crate::synom::Synom>::parse) };
783 ($i:expr, for) => { call!($i, <$crate::token::For as $crate::synom::Synom>::parse) };
784 ($i:expr, if) => { call!($i, <$crate::token::If as $crate::synom::Synom>::parse) };
785 ($i:expr, impl) => { call!($i, <$crate::token::Impl as $crate::synom::Synom>::parse) };
786 ($i:expr, in) => { call!($i, <$crate::token::In as $crate::synom::Synom>::parse) };
787 ($i:expr, let) => { call!($i, <$crate::token::Let as $crate::synom::Synom>::parse) };
788 ($i:expr, loop) => { call!($i, <$crate::token::Loop as $crate::synom::Synom>::parse) };
789 ($i:expr, macro) => { call!($i, <$crate::token::Macro as $crate::synom::Synom>::parse) };
790 ($i:expr, match) => { call!($i, <$crate::token::Match as $crate::synom::Synom>::parse) };
791 ($i:expr, mod) => { call!($i, <$crate::token::Mod as $crate::synom::Synom>::parse) };
792 ($i:expr, move) => { call!($i, <$crate::token::Move as $crate::synom::Synom>::parse) };
793 ($i:expr, mut) => { call!($i, <$crate::token::Mut as $crate::synom::Synom>::parse) };
794 ($i:expr, pub) => { call!($i, <$crate::token::Pub as $crate::synom::Synom>::parse) };
795 ($i:expr, ref) => { call!($i, <$crate::token::Ref as $crate::synom::Synom>::parse) };
796 ($i:expr, return) => { call!($i, <$crate::token::Return as $crate::synom::Synom>::parse) };
797 ($i:expr, self) => { call!($i, <$crate::token::Self_ as $crate::synom::Synom>::parse) };
798 ($i:expr, static) => { call!($i, <$crate::token::Static as $crate::synom::Synom>::parse) };
799 ($i:expr, struct) => { call!($i, <$crate::token::Struct as $crate::synom::Synom>::parse) };
800 ($i:expr, super) => { call!($i, <$crate::token::Super as $crate::synom::Synom>::parse) };
801 ($i:expr, trait) => { call!($i, <$crate::token::Trait as $crate::synom::Synom>::parse) };
802 ($i:expr, try) => { call!($i, <$crate::token::Try as $crate::synom::Synom>::parse) };
803 ($i:expr, type) => { call!($i, <$crate::token::Type as $crate::synom::Synom>::parse) };
804 ($i:expr, union) => { call!($i, <$crate::token::Union as $crate::synom::Synom>::parse) };
805 ($i:expr, unsafe) => { call!($i, <$crate::token::Unsafe as $crate::synom::Synom>::parse) };
806 ($i:expr, use) => { call!($i, <$crate::token::Use as $crate::synom::Synom>::parse) };
807 ($i:expr, where) => { call!($i, <$crate::token::Where as $crate::synom::Synom>::parse) };
808 ($i:expr, while) => { call!($i, <$crate::token::While as $crate::synom::Synom>::parse) };
809 ($i:expr, yield) => { call!($i, <$crate::token::Yield as $crate::synom::Synom>::parse) };
David Tolnayf8db7ba2017-11-11 22:52:16 -0800810}
811
David Tolnaybd0bc3e2018-05-20 17:15:35 -0700812macro_rules! ident_from_token {
813 ($token:ident) => {
814 impl From<Token![$token]> for Ident {
815 fn from(token: Token![$token]) -> Ident {
David Tolnay7ac699c2018-08-24 14:00:58 -0400816 Ident::new(stringify!($token), token.span)
David Tolnaybd0bc3e2018-05-20 17:15:35 -0700817 }
818 }
819 };
820}
821
822ident_from_token!(self);
823ident_from_token!(Self);
824ident_from_token!(super);
825ident_from_token!(crate);
David Tolnay0a4d4e92018-07-21 15:31:45 -0700826ident_from_token!(extern);
David Tolnaybd0bc3e2018-05-20 17:15:35 -0700827
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700828#[cfg(feature = "parsing")]
829mod parsing {
David Tolnay51382052017-12-27 13:46:21 -0500830 use proc_macro2::{Delimiter, Spacing, Span};
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700831
David Tolnaydfc886b2018-01-06 08:03:09 -0800832 use buffer::Cursor;
David Tolnayad4b2472018-08-25 08:25:24 -0400833 use error::{Error, Result};
David Tolnayb6254182018-08-25 08:44:54 -0400834 use parse::ParseStream;
David Tolnay203557a2017-12-27 23:59:33 -0500835 use parse_error;
David Tolnay776f8e02018-08-24 22:32:10 -0400836 use span::FromSpans;
David Tolnay203557a2017-12-27 23:59:33 -0500837 use synom::PResult;
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700838
David Tolnay776f8e02018-08-24 22:32:10 -0400839 pub fn keyword(input: ParseStream, token: &str) -> Result<Span> {
840 input.step_cursor(|cursor| {
841 if let Some((ident, rest)) = cursor.ident() {
842 if ident == token {
843 return Ok((ident.span(), rest));
Alex Crichton954046c2017-05-30 21:49:42 -0700844 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700845 }
David Tolnay776f8e02018-08-24 22:32:10 -0400846 Err(cursor.error(format!("expected `{}`", token)))
847 })
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700848 }
849
David Tolnay776f8e02018-08-24 22:32:10 -0400850 pub fn punct<S: FromSpans>(input: ParseStream, token: &str) -> Result<S> {
851 input.step_cursor(|cursor| {
852 let mut cursor = *cursor;
853 let mut spans = [cursor.span(); 3];
854 assert!(token.len() <= spans.len());
855
856 for (i, ch) in token.chars().enumerate() {
857 match cursor.punct() {
858 Some((punct, rest)) => {
859 spans[i] = punct.span();
860 if punct.as_char() != ch {
861 break;
862 } else if i == token.len() - 1 {
863 return Ok((S::from_spans(&spans), rest));
864 } else if punct.spacing() != Spacing::Joint {
865 break;
866 }
867 cursor = rest;
868 }
869 None => break,
870 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400871 }
David Tolnay776f8e02018-08-24 22:32:10 -0400872
873 Err(Error::new(spans[0], format!("expected `{}`", token)))
874 })
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700875 }
876
David Tolnay51382052017-12-27 13:46:21 -0500877 pub fn delim<'a, F, R, T>(
878 delim: &str,
879 tokens: Cursor<'a>,
David Tolnay2b069542018-05-09 12:59:36 -0700880 new: fn(Span) -> T,
David Tolnay51382052017-12-27 13:46:21 -0500881 f: F,
David Tolnay8875fca2017-12-31 13:52:37 -0500882 ) -> PResult<'a, (T, R)>
David Tolnay51382052017-12-27 13:46:21 -0500883 where
884 F: FnOnce(Cursor) -> PResult<R>,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700885 {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400886 // NOTE: We should support none-delimited sequences here.
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700887 let delim = match delim {
888 "(" => Delimiter::Parenthesis,
889 "{" => Delimiter::Brace,
890 "[" => Delimiter::Bracket,
Michael Layzell93c36282017-06-04 20:43:14 -0400891 " " => Delimiter::None,
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700892 _ => panic!("unknown delimiter: {}", delim),
893 };
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700894
David Tolnay65729482017-12-31 16:14:50 -0500895 if let Some((inside, span, rest)) = tokens.group(delim) {
896 match f(inside) {
David Tolnayf4aa6b42017-12-31 16:40:33 -0500897 Ok((ret, remaining)) => {
Michael Layzell0a1a6632017-06-02 18:07:43 -0400898 if remaining.eof() {
David Tolnay2b069542018-05-09 12:59:36 -0700899 return Ok(((new(span), ret), rest));
Michael Layzell0a1a6632017-06-02 18:07:43 -0400900 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700901 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400902 Err(err) => return Err(err),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700903 }
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700904 }
Michael Layzell0a1a6632017-06-02 18:07:43 -0400905 parse_error()
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700906 }
907}
908
909#[cfg(feature = "printing")]
910mod printing {
David Tolnay65fb5662018-05-20 20:02:28 -0700911 use proc_macro2::{Delimiter, Group, Ident, Punct, Spacing, Span, TokenStream};
Alex Crichtona74a1c82018-05-16 10:20:44 -0700912 use quote::TokenStreamExt;
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700913
Alex Crichtona74a1c82018-05-16 10:20:44 -0700914 pub fn punct(s: &str, spans: &[Span], tokens: &mut TokenStream) {
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700915 assert_eq!(s.len(), spans.len());
916
917 let mut chars = s.chars();
918 let mut spans = spans.iter();
919 let ch = chars.next_back().unwrap();
920 let span = spans.next_back().unwrap();
921 for (ch, span) in chars.zip(spans) {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700922 let mut op = Punct::new(ch, Spacing::Joint);
Alex Crichton9a4dca22018-03-28 06:32:19 -0700923 op.set_span(*span);
924 tokens.append(op);
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700925 }
926
Alex Crichtona74a1c82018-05-16 10:20:44 -0700927 let mut op = Punct::new(ch, Spacing::Alone);
Alex Crichton9a4dca22018-03-28 06:32:19 -0700928 op.set_span(*span);
929 tokens.append(op);
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700930 }
931
Alex Crichtona74a1c82018-05-16 10:20:44 -0700932 pub fn keyword(s: &str, span: &Span, tokens: &mut TokenStream) {
933 tokens.append(Ident::new(s, *span));
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700934 }
935
Alex Crichtona74a1c82018-05-16 10:20:44 -0700936 pub fn delim<F>(s: &str, span: &Span, tokens: &mut TokenStream, f: F)
David Tolnay51382052017-12-27 13:46:21 -0500937 where
Alex Crichtona74a1c82018-05-16 10:20:44 -0700938 F: FnOnce(&mut TokenStream),
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700939 {
David Tolnay00ab6982017-12-31 18:15:06 -0500940 let delim = match s {
941 "(" => Delimiter::Parenthesis,
942 "[" => Delimiter::Bracket,
943 "{" => Delimiter::Brace,
944 " " => Delimiter::None,
945 _ => panic!("unknown delimiter: {}", s),
946 };
hcplaa511792018-05-29 07:13:01 +0300947 let mut inner = TokenStream::new();
David Tolnay00ab6982017-12-31 18:15:06 -0500948 f(&mut inner);
David Tolnay106db5e2018-05-20 19:56:38 -0700949 let mut g = Group::new(delim, inner);
Alex Crichton9a4dca22018-03-28 06:32:19 -0700950 g.set_span(*span);
951 tokens.append(g);
Alex Crichton7b9e02f2017-05-30 15:54:33 -0700952 }
953}