diff options
author | David Tolnay <dtolnay@gmail.com> | 2018-05-20 18:10:38 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-20 18:10:38 -0700 |
commit | 857e7e711f8217e04d64bc0eacad9e1e20b93aef (patch) | |
tree | b8569d2f164bc940ea3cca8b5a5cca0d23630275 /src | |
parent | 5efb787c53a684546a5d8e7e49b50fd271f65374 (diff) | |
parent | 55a5f3a7cf42e52f050e0b01e93d8d23bee8365d (diff) | |
download | syn-857e7e711f8217e04d64bc0eacad9e1e20b93aef.tar.gz |
Merge pull request #426 from alexcrichton/next
Update to the next version of proc-macro2
Diffstat (limited to 'src')
-rw-r--r-- | src/attr.rs | 44 | ||||
-rw-r--r-- | src/buffer.rs | 40 | ||||
-rw-r--r-- | src/data.rs | 17 | ||||
-rw-r--r-- | src/derive.rs | 5 | ||||
-rw-r--r-- | src/expr.rs | 146 | ||||
-rw-r--r-- | src/file.rs | 5 | ||||
-rw-r--r-- | src/gen/fold.rs | 15 | ||||
-rw-r--r-- | src/gen/visit.rs | 5 | ||||
-rw-r--r-- | src/gen/visit_mut.rs | 5 | ||||
-rw-r--r-- | src/generics.rs | 37 | ||||
-rw-r--r-- | src/ident.rs | 306 | ||||
-rw-r--r-- | src/item.rs | 91 | ||||
-rw-r--r-- | src/lib.rs | 7 | ||||
-rw-r--r-- | src/lifetime.rs | 53 | ||||
-rw-r--r-- | src/lit.rs | 29 | ||||
-rw-r--r-- | src/mac.rs | 5 | ||||
-rw-r--r-- | src/macros.rs | 2 | ||||
-rw-r--r-- | src/op.rs | 7 | ||||
-rw-r--r-- | src/parsers.rs | 11 | ||||
-rw-r--r-- | src/path.rs | 25 | ||||
-rw-r--r-- | src/punctuated.rs | 7 | ||||
-rw-r--r-- | src/spanned.rs | 6 | ||||
-rw-r--r-- | src/synom.rs | 28 | ||||
-rw-r--r-- | src/token.rs | 126 | ||||
-rw-r--r-- | src/tt.rs | 12 | ||||
-rw-r--r-- | src/ty.rs | 41 |
26 files changed, 442 insertions, 633 deletions
diff --git a/src/attr.rs b/src/attr.rs index b63b3029..6f50e0a3 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -107,19 +107,19 @@ impl Attribute { }; if self.tts.is_empty() { - return Some(Meta::Word(*name)); + return Some(Meta::Word(name.clone())); } let tts = self.tts.clone().into_iter().collect::<Vec<_>>(); if tts.len() == 1 { - if let Some(meta) = Attribute::extract_meta_list(*name, &tts[0]) { + if let Some(meta) = Attribute::extract_meta_list(name.clone(), &tts[0]) { return Some(meta); } } if tts.len() == 2 { - if let Some(meta) = Attribute::extract_name_value(*name, &tts[0], &tts[1]) { + if let Some(meta) = Attribute::extract_name_value(name.clone(), &tts[0], &tts[1]) { return Some(meta); } } @@ -149,13 +149,13 @@ impl Attribute { fn extract_name_value(ident: Ident, a: &TokenTree, b: &TokenTree) -> Option<Meta> { let a = match *a { - TokenTree::Op(ref o) => o, + TokenTree::Punct(ref o) => o, _ => return None, }; if a.spacing() != Spacing::Alone { return None; } - if a.op() != '=' { + if a.as_char() != '=' { return None; } @@ -167,7 +167,7 @@ impl Attribute { lit: Lit::new(l.clone()), })) } - TokenTree::Term(ref term) => match term.as_str() { + TokenTree::Ident(ref term) => match &term.to_string()[..] { v @ "true" | v @ "false" => Some(Meta::NameValue(MetaNameValue { ident: ident, eq_token: Token![=]([a.span()]), @@ -196,21 +196,20 @@ fn nested_meta_item_from_tokens(tts: &[TokenTree]) -> Option<(NestedMeta, &[Toke } } - TokenTree::Term(sym) => { - let ident = Ident::new(sym.as_str(), sym.span()); + TokenTree::Ident(ref ident) => { if tts.len() >= 3 { - if let Some(meta) = Attribute::extract_name_value(ident, &tts[1], &tts[2]) { + if let Some(meta) = Attribute::extract_name_value(ident.clone(), &tts[1], &tts[2]) { return Some((NestedMeta::Meta(meta), &tts[3..])); } } if tts.len() >= 2 { - if let Some(meta) = Attribute::extract_meta_list(ident, &tts[1]) { + if let Some(meta) = Attribute::extract_meta_list(ident.clone(), &tts[1]) { return Some((NestedMeta::Meta(meta), &tts[2..])); } } - Some((Meta::Word(ident).into(), &tts[1..])) + Some((Meta::Word(ident.clone()).into(), &tts[1..])) } _ => None, @@ -227,11 +226,11 @@ fn list_of_nested_meta_items_from_tokens( let prev_comma = if first { first = false; None - } else if let TokenTree::Op(ref op) = tts[0] { + } else if let TokenTree::Punct(ref op) = tts[0] { if op.spacing() != Spacing::Alone { return None; } - if op.op() != ',' { + if op.as_char() != ',' { return None; } let tok = Token![,]([op.span()]); @@ -336,9 +335,9 @@ impl Meta { /// `#[derive(Copy)]`, and the `path` in `#[path = "sys/windows.rs"]`. pub fn name(&self) -> Ident { match *self { - Meta::Word(ref meta) => *meta, - Meta::List(ref meta) => meta.ident, - Meta::NameValue(ref meta) => meta.ident, + Meta::Word(ref meta) => meta.clone(), + Meta::List(ref meta) => meta.ident.clone(), + Meta::NameValue(ref meta) => meta.ident.clone(), } } } @@ -397,11 +396,11 @@ pub mod parsing { use super::*; use buffer::Cursor; use parse_error; - use proc_macro2::{Literal, Op, Spacing, Span, TokenTree}; + use proc_macro2::{Literal, Punct, Spacing, Span, TokenTree}; use synom::PResult; fn eq(span: Span) -> TokenTree { - let mut op = Op::new('=', Spacing::Alone); + let mut op = Punct::new('=', Spacing::Alone); op.set_span(span); op.into() } @@ -518,10 +517,11 @@ pub mod parsing { #[cfg(feature = "printing")] mod printing { use super::*; - use quote::{ToTokens, Tokens}; + use proc_macro2::TokenStream; + use quote::ToTokens; impl ToTokens for Attribute { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.pound_token.to_tokens(tokens); if let AttrStyle::Inner(ref b) = self.style { b.to_tokens(tokens); @@ -534,7 +534,7 @@ mod printing { } impl ToTokens for MetaList { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.ident.to_tokens(tokens); self.paren_token.surround(tokens, |tokens| { self.nested.to_tokens(tokens); @@ -543,7 +543,7 @@ mod printing { } impl ToTokens for MetaNameValue { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.ident.to_tokens(tokens); self.eq_token.to_tokens(tokens); self.lit.to_tokens(tokens); diff --git a/src/buffer.rs b/src/buffer.rs index a4af7bc6..d695c770 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -129,8 +129,8 @@ #[cfg(feature = "proc-macro")] use proc_macro as pm; -use proc_macro2::{Delimiter, Literal, Span, Term, TokenStream}; -use proc_macro2::{Group, Op, TokenTree}; +use proc_macro2::{Delimiter, Literal, Span, Ident, TokenStream}; +use proc_macro2::{Group, Punct, TokenTree}; use std::marker::PhantomData; use std::ptr; @@ -143,8 +143,8 @@ use std::fmt::{self, Debug}; enum Entry { // Mimicking types from proc-macro. Group(Span, Delimiter, TokenBuffer), - Term(Term), - Op(Op), + Ident(Ident), + Punct(Punct), Literal(Literal), // End entries contain a raw pointer to the entry from the containing // token tree, or null if this is the outermost level. @@ -177,11 +177,11 @@ impl TokenBuffer { let mut seqs = Vec::new(); for tt in stream { match tt { - TokenTree::Term(sym) => { - entries.push(Entry::Term(sym)); + TokenTree::Ident(sym) => { + entries.push(Entry::Ident(sym)); } - TokenTree::Op(op) => { - entries.push(Entry::Op(op)); + TokenTree::Punct(op) => { + entries.push(Entry::Punct(op)); } TokenTree::Literal(l) => { entries.push(Entry::Literal(l)); @@ -275,8 +275,8 @@ impl<'a> Cursor<'a> { pub fn empty() -> Self { // It's safe in this situation for us to put an `Entry` object in global // storage, despite it not actually being safe to send across threads - // (`Term` is a reference into a thread-local table). This is because - // this entry never includes a `Term` object. + // (`Ident` is a reference into a thread-local table). This is because + // this entry never includes a `Ident` object. // // This wrapper struct allows us to break the rules and put a `Sync` // object in global storage. @@ -368,22 +368,22 @@ impl<'a> Cursor<'a> { None } - /// If the cursor is pointing at a `Term`, returns it along with a cursor + /// If the cursor is pointing at a `Ident`, returns it along with a cursor /// pointing at the next `TokenTree`. - pub fn term(mut self) -> Option<(Term, Cursor<'a>)> { + pub fn ident(mut self) -> Option<(Ident, Cursor<'a>)> { self.ignore_none(); match *self.entry() { - Entry::Term(term) => Some((term, unsafe { self.bump() })), + Entry::Ident(ref term) => Some((term.clone(), unsafe { self.bump() })), _ => None, } } - /// If the cursor is pointing at an `Op`, returns it along with a cursor + /// If the cursor is pointing at an `Punct`, returns it along with a cursor /// pointing at the next `TokenTree`. - pub fn op(mut self) -> Option<(Op, Cursor<'a>)> { + pub fn punct(mut self) -> Option<(Punct, Cursor<'a>)> { self.ignore_none(); match *self.entry() { - Entry::Op(op) => Some((op, unsafe { self.bump() })), + Entry::Punct(ref op) => Some((op.clone(), unsafe { self.bump() })), _ => None, } } @@ -426,8 +426,8 @@ impl<'a> Cursor<'a> { TokenTree::from(g) } Entry::Literal(ref lit) => lit.clone().into(), - Entry::Term(term) => term.into(), - Entry::Op(op) => op.into(), + Entry::Ident(ref term) => term.clone().into(), + Entry::Punct(ref op) => op.clone().into(), Entry::End(..) => { return None; } @@ -442,8 +442,8 @@ impl<'a> Cursor<'a> { match *self.entry() { Entry::Group(span, ..) => span, Entry::Literal(ref l) => l.span(), - Entry::Term(t) => t.span(), - Entry::Op(o) => o.span(), + Entry::Ident(ref t) => t.span(), + Entry::Punct(ref o) => o.span(), Entry::End(..) => Span::call_site(), } } diff --git a/src/data.rs b/src/data.rs index 31eb1d13..fbbdb707 100644 --- a/src/data.rs +++ b/src/data.rs @@ -322,10 +322,11 @@ pub mod parsing { #[cfg(feature = "printing")] mod printing { use super::*; - use quote::{ToTokens, Tokens}; + use proc_macro2::TokenStream; + use quote::{ToTokens, TokenStreamExt}; impl ToTokens for Variant { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(&self.attrs); self.ident.to_tokens(tokens); self.fields.to_tokens(tokens); @@ -337,7 +338,7 @@ mod printing { } impl ToTokens for FieldsNamed { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.brace_token.surround(tokens, |tokens| { self.named.to_tokens(tokens); }); @@ -345,7 +346,7 @@ mod printing { } impl ToTokens for FieldsUnnamed { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.paren_token.surround(tokens, |tokens| { self.unnamed.to_tokens(tokens); }); @@ -353,7 +354,7 @@ mod printing { } impl ToTokens for Field { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(&self.attrs); self.vis.to_tokens(tokens); if let Some(ref ident) = self.ident { @@ -365,19 +366,19 @@ mod printing { } impl ToTokens for VisPublic { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.pub_token.to_tokens(tokens) } } impl ToTokens for VisCrate { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.crate_token.to_tokens(tokens); } } impl ToTokens for VisRestricted { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.pub_token.to_tokens(tokens); self.paren_token.surround(tokens, |tokens| { // XXX: If we have a path which is not "self" or "super" or diff --git a/src/derive.rs b/src/derive.rs index 3ad893d8..ba60f8df 100644 --- a/src/derive.rs +++ b/src/derive.rs @@ -163,10 +163,11 @@ pub mod parsing { mod printing { use super::*; use attr::FilterAttrs; - use quote::{ToTokens, Tokens}; + use quote::ToTokens; + use proc_macro2::TokenStream; impl ToTokens for DeriveInput { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { for attr in self.attrs.outer() { attr.to_tokens(tokens); } diff --git a/src/expr.rs b/src/expr.rs index 339e29ee..ac55c98c 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -2178,7 +2178,7 @@ pub mod parsing { tuple!(syn!(Member), map!(punct!(:), Some), syn!(Expr)) | map!(syn!(Ident), |name| ( - Member::Named(name), + Member::Named(name.clone()), None, Expr::Path(ExprPath { attrs: Vec::new(), @@ -2583,7 +2583,7 @@ pub mod parsing { let mut pat: Pat = PatIdent { by_ref: by_ref, mutability: mutability, - ident: ident, + ident: ident.clone(), subpat: None, }.into(); if let Some(boxed) = boxed { @@ -2821,13 +2821,13 @@ mod printing { use super::*; #[cfg(feature = "full")] use attr::FilterAttrs; - use proc_macro2::Literal; - use quote::{ToTokens, Tokens}; + use proc_macro2::{Literal, TokenStream}; + use quote::{ToTokens, TokenStreamExt}; // If the given expression is a bare `ExprStruct`, wraps it in parenthesis // before appending it to `Tokens`. #[cfg(feature = "full")] - fn wrap_bare_struct(tokens: &mut Tokens, e: &Expr) { + fn wrap_bare_struct(tokens: &mut TokenStream, e: &Expr) { if let Expr::Struct(_) = *e { token::Paren::default().surround(tokens, |tokens| { e.to_tokens(tokens); @@ -2838,16 +2838,16 @@ mod printing { } #[cfg(feature = "full")] - fn attrs_to_tokens(attrs: &[Attribute], tokens: &mut Tokens) { + fn attrs_to_tokens(attrs: &[Attribute], tokens: &mut TokenStream) { tokens.append_all(attrs.outer()); } #[cfg(not(feature = "full"))] - fn attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut Tokens) {} + fn attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {} #[cfg(feature = "full")] impl ToTokens for ExprBox { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.box_token.to_tokens(tokens); self.expr.to_tokens(tokens); @@ -2856,7 +2856,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprInPlace { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.place.to_tokens(tokens); self.arrow_token.to_tokens(tokens); @@ -2866,7 +2866,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprArray { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.bracket_token.surround(tokens, |tokens| { self.elems.to_tokens(tokens); @@ -2875,7 +2875,7 @@ mod printing { } impl ToTokens for ExprCall { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { attrs_to_tokens(&self.attrs, tokens); self.func.to_tokens(tokens); self.paren_token.surround(tokens, |tokens| { @@ -2886,7 +2886,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprMethodCall { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.receiver.to_tokens(tokens); self.dot_token.to_tokens(tokens); @@ -2900,7 +2900,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for MethodTurbofish { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.colon2_token.to_tokens(tokens); self.lt_token.to_tokens(tokens); self.args.to_tokens(tokens); @@ -2910,7 +2910,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for GenericMethodArgument { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { match *self { GenericMethodArgument::Type(ref t) => t.to_tokens(tokens), GenericMethodArgument::Const(ref c) => c.to_tokens(tokens), @@ -2920,7 +2920,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprTuple { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.paren_token.surround(tokens, |tokens| { self.elems.to_tokens(tokens); @@ -2934,7 +2934,7 @@ mod printing { } impl ToTokens for ExprBinary { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { attrs_to_tokens(&self.attrs, tokens); self.left.to_tokens(tokens); self.op.to_tokens(tokens); @@ -2943,7 +2943,7 @@ mod printing { } impl ToTokens for ExprUnary { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { attrs_to_tokens(&self.attrs, tokens); self.op.to_tokens(tokens); self.expr.to_tokens(tokens); @@ -2951,14 +2951,14 @@ mod printing { } impl ToTokens for ExprLit { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { attrs_to_tokens(&self.attrs, tokens); self.lit.to_tokens(tokens); } } impl ToTokens for ExprCast { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { attrs_to_tokens(&self.attrs, tokens); self.expr.to_tokens(tokens); self.as_token.to_tokens(tokens); @@ -2968,7 +2968,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprType { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { attrs_to_tokens(&self.attrs, tokens); self.expr.to_tokens(tokens); self.colon_token.to_tokens(tokens); @@ -2977,7 +2977,7 @@ mod printing { } #[cfg(feature = "full")] - fn maybe_wrap_else(tokens: &mut Tokens, else_: &Option<(Token![else], Box<Expr>)>) { + fn maybe_wrap_else(tokens: &mut TokenStream, else_: &Option<(Token![else], Box<Expr>)>) { if let Some((ref else_token, ref else_)) = *else_ { else_token.to_tokens(tokens); @@ -2998,7 +2998,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprIf { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.if_token.to_tokens(tokens); wrap_bare_struct(tokens, &self.cond); @@ -3009,7 +3009,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprIfLet { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.if_token.to_tokens(tokens); self.let_token.to_tokens(tokens); @@ -3023,7 +3023,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprWhile { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.label.to_tokens(tokens); self.while_token.to_tokens(tokens); @@ -3034,7 +3034,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprWhileLet { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.label.to_tokens(tokens); self.while_token.to_tokens(tokens); @@ -3048,7 +3048,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprForLoop { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.label.to_tokens(tokens); self.for_token.to_tokens(tokens); @@ -3061,7 +3061,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprLoop { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.label.to_tokens(tokens); self.loop_token.to_tokens(tokens); @@ -3071,7 +3071,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprMatch { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.match_token.to_tokens(tokens); wrap_bare_struct(tokens, &self.expr); @@ -3091,7 +3091,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprCatch { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.do_token.to_tokens(tokens); self.catch_token.to_tokens(tokens); @@ -3101,7 +3101,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprYield { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.yield_token.to_tokens(tokens); self.expr.to_tokens(tokens); @@ -3110,7 +3110,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprClosure { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.movability.to_tokens(tokens); self.capture.to_tokens(tokens); @@ -3136,7 +3136,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprUnsafe { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.unsafe_token.to_tokens(tokens); self.block.to_tokens(tokens); @@ -3145,7 +3145,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprBlock { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.block.to_tokens(tokens); } @@ -3153,7 +3153,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprAssign { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.left.to_tokens(tokens); self.eq_token.to_tokens(tokens); @@ -3163,7 +3163,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprAssignOp { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.left.to_tokens(tokens); self.op.to_tokens(tokens); @@ -3173,7 +3173,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprField { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.base.to_tokens(tokens); self.dot_token.to_tokens(tokens); @@ -3182,16 +3182,16 @@ mod printing { } impl ToTokens for Member { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { match *self { - Member::Named(ident) => ident.to_tokens(tokens), + Member::Named(ref ident) => ident.to_tokens(tokens), Member::Unnamed(ref index) => index.to_tokens(tokens), } } } impl ToTokens for Index { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { let mut lit = Literal::i64_unsuffixed(i64::from(self.index)); lit.set_span(self.span); tokens.append(lit); @@ -3199,7 +3199,7 @@ mod printing { } impl ToTokens for ExprIndex { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { attrs_to_tokens(&self.attrs, tokens); self.expr.to_tokens(tokens); self.bracket_token.surround(tokens, |tokens| { @@ -3210,7 +3210,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprRange { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.from.to_tokens(tokens); match self.limits { @@ -3222,7 +3222,7 @@ mod printing { } impl ToTokens for ExprPath { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { attrs_to_tokens(&self.attrs, tokens); ::PathTokens(&self.qself, &self.path).to_tokens(tokens) } @@ -3230,7 +3230,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprReference { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.and_token.to_tokens(tokens); self.mutability.to_tokens(tokens); @@ -3240,7 +3240,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprBreak { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.break_token.to_tokens(tokens); self.label.to_tokens(tokens); @@ -3250,7 +3250,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprContinue { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.continue_token.to_tokens(tokens); self.label.to_tokens(tokens); @@ -3259,7 +3259,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprReturn { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.return_token.to_tokens(tokens); self.expr.to_tokens(tokens); @@ -3268,7 +3268,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprMacro { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.mac.to_tokens(tokens); } @@ -3276,7 +3276,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprStruct { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.path.to_tokens(tokens); self.brace_token.surround(tokens, |tokens| { @@ -3291,7 +3291,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprRepeat { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.bracket_token.surround(tokens, |tokens| { self.expr.to_tokens(tokens); @@ -3303,7 +3303,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprGroup { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { attrs_to_tokens(&self.attrs, tokens); self.group_token.surround(tokens, |tokens| { self.expr.to_tokens(tokens); @@ -3312,7 +3312,7 @@ mod printing { } impl ToTokens for ExprParen { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { attrs_to_tokens(&self.attrs, tokens); self.paren_token.surround(tokens, |tokens| { self.expr.to_tokens(tokens); @@ -3322,7 +3322,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for ExprTry { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.expr.to_tokens(tokens); self.question_token.to_tokens(tokens); @@ -3330,14 +3330,14 @@ mod printing { } impl ToTokens for ExprVerbatim { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.tts.to_tokens(tokens); } } #[cfg(feature = "full")] impl ToTokens for Label { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.name.to_tokens(tokens); self.colon_token.to_tokens(tokens); } @@ -3345,7 +3345,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for FieldValue { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.member.to_tokens(tokens); if let Some(ref colon_token) = self.colon_token { @@ -3357,7 +3357,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for Arm { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(&self.attrs); self.leading_vert.to_tokens(tokens); self.pats.to_tokens(tokens); @@ -3373,14 +3373,14 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for PatWild { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.underscore_token.to_tokens(tokens); } } #[cfg(feature = "full")] impl ToTokens for PatIdent { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.by_ref.to_tokens(tokens); self.mutability.to_tokens(tokens); self.ident.to_tokens(tokens); @@ -3393,7 +3393,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for PatStruct { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.path.to_tokens(tokens); self.brace_token.surround(tokens, |tokens| { self.fields.to_tokens(tokens); @@ -3408,7 +3408,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for PatTupleStruct { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.path.to_tokens(tokens); self.pat.to_tokens(tokens); } @@ -3416,14 +3416,14 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for PatPath { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { ::PathTokens(&self.qself, &self.path).to_tokens(tokens); } } #[cfg(feature = "full")] impl ToTokens for PatTuple { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.paren_token.surround(tokens, |tokens| { self.front.to_tokens(tokens); if let Some(ref dot2_token) = self.dot2_token { @@ -3445,7 +3445,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for PatBox { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.box_token.to_tokens(tokens); self.pat.to_tokens(tokens); } @@ -3453,7 +3453,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for PatRef { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.and_token.to_tokens(tokens); self.mutability.to_tokens(tokens); self.pat.to_tokens(tokens); @@ -3462,14 +3462,14 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for PatLit { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.expr.to_tokens(tokens); } } #[cfg(feature = "full")] impl ToTokens for PatRange { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.lo.to_tokens(tokens); match self.limits { RangeLimits::HalfOpen(ref t) => t.to_tokens(tokens), @@ -3481,7 +3481,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for PatSlice { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { // XXX: This is a mess, and it will be so easy to screw it up. How // do we make this correct itself better? self.bracket_token.surround(tokens, |tokens| { @@ -3516,21 +3516,21 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for PatMacro { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.mac.to_tokens(tokens); } } #[cfg(feature = "full")] impl ToTokens for PatVerbatim { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.tts.to_tokens(tokens); } } #[cfg(feature = "full")] impl ToTokens for FieldPat { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { if let Some(ref colon_token) = self.colon_token { self.member.to_tokens(tokens); colon_token.to_tokens(tokens); @@ -3541,7 +3541,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for Block { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.brace_token.surround(tokens, |tokens| { tokens.append_all(&self.stmts); }); @@ -3550,7 +3550,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for Stmt { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { match *self { Stmt::Local(ref local) => local.to_tokens(tokens), Stmt::Item(ref item) => item.to_tokens(tokens), @@ -3565,7 +3565,7 @@ mod printing { #[cfg(feature = "full")] impl ToTokens for Local { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.let_token.to_tokens(tokens); self.pats.to_tokens(tokens); diff --git a/src/file.rs b/src/file.rs index 17ea8ff2..99a311dd 100644 --- a/src/file.rs +++ b/src/file.rs @@ -111,10 +111,11 @@ pub mod parsing { mod printing { use super::*; use attr::FilterAttrs; - use quote::{ToTokens, Tokens}; + use quote::{ToTokens, TokenStreamExt}; + use proc_macro2::TokenStream; impl ToTokens for File { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.inner()); tokens.append_all(&self.items); } diff --git a/src/gen/fold.rs b/src/gen/fold.rs index b5b67b3f..2cd760db 100644 --- a/src/gen/fold.rs +++ b/src/gen/fold.rs @@ -6,6 +6,7 @@ #![allow(unreachable_code)] #![cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))] +#[cfg(any(feature = "full", feature = "derive"))] use *; #[cfg(any(feature = "full", feature = "derive"))] use token::{Brace, Bracket, Paren, Group}; @@ -430,9 +431,6 @@ macro_rules! fold_span_only { } } -fold_span_only!(fold_ident: Ident); -#[cfg(any(feature = "full", feature = "derive"))] -fold_span_only!(fold_lifetime: Lifetime); #[cfg(any(feature = "full", feature = "derive"))] fold_span_only!(fold_lit_byte: LitByte); #[cfg(any(feature = "full", feature = "derive"))] @@ -1596,6 +1594,10 @@ pub fn fold_generics<V: Fold + ?Sized>(_visitor: &mut V, _i: Generics) -> Generi where_clause: (_i . where_clause).map(|it| { _visitor.fold_where_clause(it) }), } } + +pub fn fold_ident<V: Fold + ?Sized>(_visitor: &mut V, _i: Ident) -> Ident { + _i +} # [ cfg ( feature = "full" ) ] pub fn fold_impl_item<V: Fold + ?Sized>(_visitor: &mut V, _i: ImplItem) -> ImplItem { match _i { @@ -1980,6 +1982,13 @@ pub fn fold_label<V: Fold + ?Sized>(_visitor: &mut V, _i: Label) -> Label { } } # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] +pub fn fold_lifetime<V: Fold + ?Sized>(_visitor: &mut V, _i: Lifetime) -> Lifetime { + Lifetime { + apostrophe: _i . apostrophe, + ident: _visitor.fold_ident(_i . ident), + } +} +# [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] pub fn fold_lifetime_def<V: Fold + ?Sized>(_visitor: &mut V, _i: LifetimeDef) -> LifetimeDef { LifetimeDef { attrs: FoldHelper::lift(_i . attrs, |it| { _visitor.fold_attribute(it) }), diff --git a/src/gen/visit.rs b/src/gen/visit.rs index 6a38d1d7..3591bb7e 100644 --- a/src/gen/visit.rs +++ b/src/gen/visit.rs @@ -4,6 +4,7 @@ #![cfg_attr(feature = "cargo-clippy", allow(match_same_arms))] +#[cfg(any(feature = "full", feature = "derive"))] use *; #[cfg(any(feature = "full", feature = "derive"))] use punctuated::Punctuated; @@ -1246,7 +1247,6 @@ pub fn visit_generics<'ast, V: Visit<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast } pub fn visit_ident<'ast, V: Visit<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast Ident) { - // Skipped field _i . term; } # [ cfg ( feature = "full" ) ] pub fn visit_impl_item<'ast, V: Visit<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast ImplItem) { @@ -1545,7 +1545,8 @@ pub fn visit_label<'ast, V: Visit<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast La } # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] pub fn visit_lifetime<'ast, V: Visit<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast Lifetime) { - // Skipped field _i . term; + // Skipped field _i . apostrophe; + _visitor.visit_ident(& _i . ident); } # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] pub fn visit_lifetime_def<'ast, V: Visit<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast LifetimeDef) { diff --git a/src/gen/visit_mut.rs b/src/gen/visit_mut.rs index 2a86a0c8..848e3481 100644 --- a/src/gen/visit_mut.rs +++ b/src/gen/visit_mut.rs @@ -4,6 +4,7 @@ #![cfg_attr(feature = "cargo-clippy", allow(match_same_arms))] +#[cfg(any(feature = "full", feature = "derive"))] use *; #[cfg(any(feature = "full", feature = "derive"))] use punctuated::Punctuated; @@ -1247,7 +1248,6 @@ pub fn visit_generics_mut<V: VisitMut + ?Sized>(_visitor: &mut V, _i: &mut Gener } pub fn visit_ident_mut<V: VisitMut + ?Sized>(_visitor: &mut V, _i: &mut Ident) { - // Skipped field _i . term; } # [ cfg ( feature = "full" ) ] pub fn visit_impl_item_mut<V: VisitMut + ?Sized>(_visitor: &mut V, _i: &mut ImplItem) { @@ -1546,7 +1546,8 @@ pub fn visit_label_mut<V: VisitMut + ?Sized>(_visitor: &mut V, _i: &mut Label) { } # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] pub fn visit_lifetime_mut<V: VisitMut + ?Sized>(_visitor: &mut V, _i: &mut Lifetime) { - // Skipped field _i . term; + // Skipped field _i . apostrophe; + _visitor.visit_ident_mut(& mut _i . ident); } # [ cfg ( any ( feature = "full" , feature = "derive" ) ) ] pub fn visit_lifetime_def_mut<V: VisitMut + ?Sized>(_visitor: &mut V, _i: &mut LifetimeDef) { diff --git a/src/generics.rs b/src/generics.rs index 919b5fe8..4c2858de 100644 --- a/src/generics.rs +++ b/src/generics.rs @@ -307,12 +307,14 @@ impl Generics { /// for that type. /// /// ``` + /// # extern crate proc_macro2; /// # extern crate syn; /// # #[macro_use] /// # extern crate quote; + /// # use proc_macro2::{Span, Ident}; /// # fn main() { /// # let generics: syn::Generics = Default::default(); - /// # let name = syn::Ident::from("MyType"); + /// # let name = Ident::new("MyType", Span::call_site()); /// let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); /// quote! { /// impl #impl_generics MyTrait for #name #ty_generics #where_clause { @@ -751,10 +753,11 @@ pub mod parsing { mod printing { use super::*; use attr::FilterAttrs; - use quote::{ToTokens, Tokens}; + use quote::{ToTokens, TokenStreamExt}; + use proc_macro2::TokenStream; impl ToTokens for Generics { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { if self.params.is_empty() { return; } @@ -791,7 +794,7 @@ mod printing { } impl<'a> ToTokens for ImplGenerics<'a> { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { if self.0.params.is_empty() { return; } @@ -846,7 +849,7 @@ mod printing { } impl<'a> ToTokens for TypeGenerics<'a> { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { if self.0.params.is_empty() { return; } @@ -894,7 +897,7 @@ mod printing { } impl<'a> ToTokens for Turbofish<'a> { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { if !self.0.params.is_empty() { <Token![::]>::default().to_tokens(tokens); TypeGenerics(self.0).to_tokens(tokens); @@ -903,7 +906,7 @@ mod printing { } impl ToTokens for BoundLifetimes { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.for_token.to_tokens(tokens); self.lt_token.to_tokens(tokens); self.lifetimes.to_tokens(tokens); @@ -912,7 +915,7 @@ mod printing { } impl ToTokens for LifetimeDef { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.lifetime.to_tokens(tokens); if !self.bounds.is_empty() { @@ -923,7 +926,7 @@ mod printing { } impl ToTokens for TypeParam { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.ident.to_tokens(tokens); if !self.bounds.is_empty() { @@ -938,8 +941,8 @@ mod printing { } impl ToTokens for TraitBound { - fn to_tokens(&self, tokens: &mut Tokens) { - let to_tokens = |tokens: &mut Tokens| { + fn to_tokens(&self, tokens: &mut TokenStream) { + let to_tokens = |tokens: &mut TokenStream| { self.modifier.to_tokens(tokens); self.lifetimes.to_tokens(tokens); self.path.to_tokens(tokens); @@ -952,7 +955,7 @@ mod printing { } impl ToTokens for TraitBoundModifier { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { match *self { TraitBoundModifier::None => {} TraitBoundModifier::Maybe(ref t) => t.to_tokens(tokens), @@ -961,7 +964,7 @@ mod printing { } impl ToTokens for ConstParam { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.const_token.to_tokens(tokens); self.ident.to_tokens(tokens); @@ -975,7 +978,7 @@ mod printing { } impl ToTokens for WhereClause { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { if !self.predicates.is_empty() { self.where_token.to_tokens(tokens); self.predicates.to_tokens(tokens); @@ -984,7 +987,7 @@ mod printing { } impl ToTokens for PredicateType { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.lifetimes.to_tokens(tokens); self.bounded_ty.to_tokens(tokens); self.colon_token.to_tokens(tokens); @@ -993,7 +996,7 @@ mod printing { } impl ToTokens for PredicateLifetime { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.lifetime.to_tokens(tokens); if !self.bounds.is_empty() { TokensOrDefault(&self.colon_token).to_tokens(tokens); @@ -1003,7 +1006,7 @@ mod printing { } impl ToTokens for PredicateEq { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.lhs_ty.to_tokens(tokens); self.eq_token.to_tokens(tokens); self.rhs_ty.to_tokens(tokens); diff --git a/src/ident.rs b/src/ident.rs deleted file mode 100644 index 5fddf28e..00000000 --- a/src/ident.rs +++ /dev/null @@ -1,306 +0,0 @@ -// Copyright 2018 Syn Developers -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use std::borrow::Cow; -use std::cmp::Ordering; -use std::fmt::{self, Display}; -use std::hash::{Hash, Hasher}; - -use proc_macro2::Term; -use unicode_xid::UnicodeXID; - -use proc_macro2::Span; - -/// A word of Rust code, which may be a keyword or legal variable name. -/// -/// An identifier consists of at least one Unicode code point, the first of -/// which has the XID_Start property and the rest of which have the XID_Continue -/// property. An underscore may be used as the first character as long as it is -/// not the only character. -/// -/// - The empty string is not an identifier. Use `Option<Ident>`. -/// - An underscore by itself is not an identifier. Use -/// `Token![_]` instead. -/// - A lifetime is not an identifier. Use `syn::Lifetime` instead. -/// -/// An identifier constructed with `Ident::new` is permitted to be a Rust -/// keyword, though parsing one through its [`Synom`] implementation rejects -/// Rust keywords. Use `call!(Ident::parse_any)` when parsing to match the -/// behaviour of `Ident::new`. -/// -/// [`Synom`]: synom/trait.Synom.html -/// -/// # Examples -/// -/// A new ident can be created from a string using the `Ident::from` function. -/// Idents produced by `Ident::from` are set to resolve at the procedural macro -/// *def site* by default. A different span can be provided explicitly by using -/// `Ident::new`. -/// -/// ```rust -/// extern crate syn; -/// extern crate proc_macro2; -/// -/// use syn::Ident; -/// use proc_macro2::Span; -/// -/// fn main() { -/// let def_ident = Ident::from("definitely"); -/// let call_ident = Ident::new("calligraphy", Span::call_site()); -/// -/// println!("{} {}", def_ident, call_ident); -/// } -/// ``` -/// -/// An ident can be interpolated into a token stream using the `quote!` macro. -/// -/// ```rust -/// #[macro_use] -/// extern crate quote; -/// -/// extern crate syn; -/// use syn::Ident; -/// -/// fn main() { -/// let ident = Ident::from("demo"); -/// -/// // Create a variable binding whose name is this ident. -/// let expanded = quote! { let #ident = 10; }; -/// -/// // Create a variable binding with a slightly different name. -/// let temp_ident = Ident::from(format!("new_{}", ident)); -/// let expanded = quote! { let #temp_ident = 10; }; -/// } -/// ``` -/// -/// A string representation of the ident is available through the `as_ref()` and -/// `to_string()` methods. -/// -/// ```rust -/// # use syn::Ident; -/// # let ident = Ident::from("another_identifier"); -/// # -/// // Examine the ident as a &str. -/// let ident_str = ident.as_ref(); -/// if ident_str.len() > 60 { -/// println!("Very long identifier: {}", ident_str) -/// } -/// -/// // Create a String from the ident. -/// let ident_string = ident.to_string(); -/// give_away(ident_string); -/// -/// fn give_away(s: String) { /* ... */ } -/// ``` -#[derive(Copy, Clone, Debug)] -pub struct Ident { - term: Term, -} - -impl Ident { - /// Creates an ident with the given string representation. - /// - /// # Panics - /// - /// Panics if the input string is neither a keyword nor a legal variable - /// name. - pub fn new(s: &str, span: Span) -> Self { - if s.is_empty() { - panic!("ident is not allowed to be empty; use Option<Ident>"); - } - - if s.starts_with('\'') { - panic!("ident is not allowed to be a lifetime; use syn::Lifetime"); - } - - if s.bytes().all(|digit| digit >= b'0' && digit <= b'9') { - panic!("ident cannot be a number, use syn::Index instead"); - } - - fn xid_ok(s: &str) -> bool { - let mut chars = s.chars(); - let first = chars.next().unwrap(); - if !(UnicodeXID::is_xid_start(first) || first == '_') { - return false; - } - for ch in chars { - if !UnicodeXID::is_xid_continue(ch) { - return false; - } - } - true - } - - if !xid_ok(s) { - panic!("{:?} is not a valid ident", s); - } - - Ident { - term: Term::new(s, span), - } - } - - pub fn span(&self) -> Span { - self.term.span() - } - - pub fn set_span(&mut self, span: Span) { - self.term.set_span(span); - } -} - -impl<'a> From<&'a str> for Ident { - fn from(s: &str) -> Self { - Ident::new(s, Span::call_site()) - } -} - -impl From<Token![self]> for Ident { - fn from(tok: Token![self]) -> Self { - Ident::new("self", tok.0) - } -} - -impl From<Token![Self]> for Ident { - fn from(tok: Token![Self]) -> Self { - Ident::new("Self", tok.0) - } -} - -impl From<Token![super]> for Ident { - fn from(tok: Token![super]) -> Self { - Ident::new("super", tok.0) - } -} - -impl From<Token![crate]> for Ident { - fn from(tok: Token![crate]) -> Self { - Ident::new("crate", tok.0) - } -} - -impl<'a> From<Cow<'a, str>> for Ident { - fn from(s: Cow<'a, str>) -> Self { - Ident::new(&s, Span::call_site()) - } -} - -impl From<String> for Ident { - fn from(s: String) -> Self { - Ident::new(&s, Span::call_site()) - } -} - -impl AsRef<str> for Ident { - fn as_ref(&self) -> &str { - self.term.as_str() - } -} - -impl Display for Ident { - fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - self.term.as_str().fmt(formatter) - } -} - -impl<T: ?Sized> PartialEq<T> for Ident -where - T: AsRef<str>, -{ - fn eq(&self, other: &T) -> bool { - self.as_ref() == other.as_ref() - } -} - -impl Eq for Ident {} - -impl PartialOrd for Ident { - fn partial_cmp(&self, other: &Ident) -> Option<Ordering> { - Some(self.cmp(other)) - } -} - -impl Ord for Ident { - fn cmp(&self, other: &Ident) -> Ordering { - self.as_ref().cmp(other.as_ref()) - } -} - -impl Hash for Ident { - fn hash<H: Hasher>(&self, h: &mut H) { - self.as_ref().hash(h); - } -} - -#[cfg(feature = "parsing")] -pub mod parsing { - use super::*; - use buffer::Cursor; - use parse_error; - use synom::PResult; - use synom::Synom; - - impl Synom for Ident { - fn parse(input: Cursor) -> PResult<Self> { - let (term, rest) = match input.term() { - Some(term) => term, - _ => return parse_error(), - }; - if term.as_str().starts_with('\'') { - return parse_error(); - } - match term.as_str() { - "_" - // From https://doc.rust-lang.org/grammar.html#keywords - | "abstract" | "alignof" | "as" | "become" | "box" | "break" | "const" - | "continue" | "crate" | "do" | "else" | "enum" | "extern" | "false" | "final" - | "fn" | "for" | "if" | "impl" | "in" | "let" | "loop" | "macro" | "match" - | "mod" | "move" | "mut" | "offsetof" | "override" | "priv" | "proc" | "pub" - | "pure" | "ref" | "return" | "Self" | "self" | "sizeof" | "static" | "struct" - | "super" | "trait" | "true" | "type" | "typeof" | "unsafe" | "unsized" | "use" - | "virtual" | "where" | "while" | "yield" => return parse_error(), - _ => {} - } - - Ok((Ident { term: term }, rest)) - } - - fn description() -> Option<&'static str> { - Some("identifier") - } - } - - impl Ident { - /// Parses any identifier - /// - /// This is useful when parsing a DSL which allows Rust keywords as identifiers. - pub fn parse_any(input: Cursor) -> PResult<Self> { - let (term, rest) = match input.term() { - Some(term) => term, - _ => return parse_error(), - }; - if term.as_str().starts_with('\'') { - return parse_error(); - } - - Ok((Ident { term: term }, rest)) - } - } -} - -#[cfg(feature = "printing")] -mod printing { - use super::*; - use quote::{ToTokens, Tokens}; - - impl ToTokens for Ident { - fn to_tokens(&self, tokens: &mut Tokens) { - tokens.append(self.term); - } - } -} diff --git a/src/item.rs b/src/item.rs index beb5a9e9..226e102f 100644 --- a/src/item.rs +++ b/src/item.rs @@ -1536,10 +1536,11 @@ pub mod parsing { mod printing { use super::*; use attr::FilterAttrs; - use quote::{ToTokens, Tokens}; + use quote::{ToTokens, TokenStreamExt}; + use proc_macro2::TokenStream; impl ToTokens for ItemExternCrate { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.extern_token.to_tokens(tokens); @@ -1554,7 +1555,7 @@ mod printing { } impl ToTokens for ItemUse { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.use_token.to_tokens(tokens); @@ -1565,7 +1566,7 @@ mod printing { } impl ToTokens for ItemStatic { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.static_token.to_tokens(tokens); @@ -1580,7 +1581,7 @@ mod printing { } impl ToTokens for ItemConst { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.const_token.to_tokens(tokens); @@ -1594,13 +1595,13 @@ mod printing { } impl ToTokens for ItemFn { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.constness.to_tokens(tokens); self.unsafety.to_tokens(tokens); self.abi.to_tokens(tokens); - NamedDecl(&self.decl, self.ident).to_tokens(tokens); + NamedDecl(&self.decl, &self.ident).to_tokens(tokens); self.block.brace_token.surround(tokens, |tokens| { tokens.append_all(self.attrs.inner()); tokens.append_all(&self.block.stmts); @@ -1609,7 +1610,7 @@ mod printing { } impl ToTokens for ItemMod { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.mod_token.to_tokens(tokens); @@ -1626,7 +1627,7 @@ mod printing { } impl ToTokens for ItemForeignMod { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.abi.to_tokens(tokens); self.brace_token.surround(tokens, |tokens| { @@ -1636,7 +1637,7 @@ mod printing { } impl ToTokens for ItemType { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.type_token.to_tokens(tokens); @@ -1650,7 +1651,7 @@ mod printing { } impl ToTokens for ItemEnum { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.enum_token.to_tokens(tokens); @@ -1664,7 +1665,7 @@ mod printing { } impl ToTokens for ItemStruct { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.struct_token.to_tokens(tokens); @@ -1689,7 +1690,7 @@ mod printing { } impl ToTokens for ItemUnion { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.union_token.to_tokens(tokens); @@ -1701,7 +1702,7 @@ mod printing { } impl ToTokens for ItemTrait { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.unsafety.to_tokens(tokens); @@ -1721,7 +1722,7 @@ mod printing { } impl ToTokens for ItemImpl { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.defaultness.to_tokens(tokens); self.unsafety.to_tokens(tokens); @@ -1742,7 +1743,7 @@ mod printing { } impl ToTokens for ItemMacro { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.mac.path.to_tokens(tokens); self.mac.bang_token.to_tokens(tokens); @@ -1763,7 +1764,7 @@ mod printing { } impl ToTokens for ItemMacro2 { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.macro_token.to_tokens(tokens); @@ -1778,13 +1779,13 @@ mod printing { } impl ToTokens for ItemVerbatim { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.tts.to_tokens(tokens); } } impl ToTokens for UsePath { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.ident.to_tokens(tokens); self.colon2_token.to_tokens(tokens); self.tree.to_tokens(tokens); @@ -1792,13 +1793,13 @@ mod printing { } impl ToTokens for UseName { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.ident.to_tokens(tokens); } } impl ToTokens for UseRename { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.ident.to_tokens(tokens); self.as_token.to_tokens(tokens); self.rename.to_tokens(tokens); @@ -1806,13 +1807,13 @@ mod printing { } impl ToTokens for UseGlob { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.star_token.to_tokens(tokens); } } impl ToTokens for UseGroup { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.brace_token.surround(tokens, |tokens| { self.items.to_tokens(tokens); }); @@ -1820,7 +1821,7 @@ mod printing { } impl ToTokens for TraitItemConst { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.const_token.to_tokens(tokens); self.ident.to_tokens(tokens); @@ -1835,7 +1836,7 @@ mod printing { } impl ToTokens for TraitItemMethod { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.sig.to_tokens(tokens); match self.default { @@ -1853,7 +1854,7 @@ mod printing { } impl ToTokens for TraitItemType { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.type_token.to_tokens(tokens); self.ident.to_tokens(tokens); @@ -1872,7 +1873,7 @@ mod printing { } impl ToTokens for TraitItemMacro { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.mac.to_tokens(tokens); self.semi_token.to_tokens(tokens); @@ -1880,13 +1881,13 @@ mod printing { } impl ToTokens for TraitItemVerbatim { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.tts.to_tokens(tokens); } } impl ToTokens for ImplItemConst { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.defaultness.to_tokens(tokens); @@ -1901,7 +1902,7 @@ mod printing { } impl ToTokens for ImplItemMethod { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.defaultness.to_tokens(tokens); @@ -1914,7 +1915,7 @@ mod printing { } impl ToTokens for ImplItemType { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.defaultness.to_tokens(tokens); @@ -1928,7 +1929,7 @@ mod printing { } impl ToTokens for ImplItemMacro { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.mac.to_tokens(tokens); self.semi_token.to_tokens(tokens); @@ -1936,22 +1937,22 @@ mod printing { } impl ToTokens for ImplItemVerbatim { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.tts.to_tokens(tokens); } } impl ToTokens for ForeignItemFn { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); - NamedDecl(&self.decl, self.ident).to_tokens(tokens); + NamedDecl(&self.decl, &self.ident).to_tokens(tokens); self.semi_token.to_tokens(tokens); } } impl ToTokens for ForeignItemStatic { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.static_token.to_tokens(tokens); @@ -1964,7 +1965,7 @@ mod printing { } impl ToTokens for ForeignItemType { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); self.vis.to_tokens(tokens); self.type_token.to_tokens(tokens); @@ -1974,24 +1975,24 @@ mod printing { } impl ToTokens for ForeignItemVerbatim { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.tts.to_tokens(tokens); } } impl ToTokens for MethodSig { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.constness.to_tokens(tokens); self.unsafety.to_tokens(tokens); self.abi.to_tokens(tokens); - NamedDecl(&self.decl, self.ident).to_tokens(tokens); + NamedDecl(&self.decl, &self.ident).to_tokens(tokens); } } - struct NamedDecl<'a>(&'a FnDecl, Ident); + struct NamedDecl<'a>(&'a FnDecl, &'a Ident); impl<'a> ToTokens for NamedDecl<'a> { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.0.fn_token.to_tokens(tokens); self.1.to_tokens(tokens); self.0.generics.to_tokens(tokens); @@ -2008,7 +2009,7 @@ mod printing { } impl ToTokens for ArgSelfRef { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.and_token.to_tokens(tokens); self.lifetime.to_tokens(tokens); self.mutability.to_tokens(tokens); @@ -2017,14 +2018,14 @@ mod printing { } impl ToTokens for ArgSelf { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.mutability.to_tokens(tokens); self.self_token.to_tokens(tokens); } } impl ToTokens for ArgCaptured { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.pat.to_tokens(tokens); self.colon_token.to_tokens(tokens); self.ty.to_tokens(tokens); @@ -286,6 +286,8 @@ mod macros; #[macro_use] pub mod token; +pub use proc_macro2::Ident; + #[cfg(any(feature = "full", feature = "derive"))] mod attr; #[cfg(any(feature = "full", feature = "derive"))] @@ -329,9 +331,6 @@ pub use generics::{ #[cfg(all(any(feature = "full", feature = "derive"), feature = "printing"))] pub use generics::{ImplGenerics, Turbofish, TypeGenerics}; -mod ident; -pub use ident::Ident; - #[cfg(feature = "full")] mod item; #[cfg(feature = "full")] @@ -732,7 +731,7 @@ impl<'a, T> quote::ToTokens for TokensOrDefault<'a, T> where T: quote::ToTokens + Default, { - fn to_tokens(&self, tokens: &mut quote::Tokens) { + fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) { match *self.0 { Some(ref t) => t.to_tokens(tokens), None => T::default().to_tokens(tokens), diff --git a/src/lifetime.rs b/src/lifetime.rs index 7e3f2310..bf8147b4 100644 --- a/src/lifetime.rs +++ b/src/lifetime.rs @@ -10,9 +10,11 @@ use std::cmp::Ordering; use std::fmt::{self, Display}; use std::hash::{Hash, Hasher}; -use proc_macro2::{Span, Term}; +use proc_macro2::{Span, Ident}; use unicode_xid::UnicodeXID; +use token::Apostrophe; + /// A Rust lifetime: `'a`. /// /// Lifetime names must conform to the following rules: @@ -27,9 +29,10 @@ use unicode_xid::UnicodeXID; /// *This type is available if Syn is built with the `"derive"` or `"full"` /// feature.* #[cfg_attr(feature = "extra-traits", derive(Debug))] -#[derive(Copy, Clone)] +#[derive(Clone)] pub struct Lifetime { - term: Term, + pub apostrophe: Apostrophe, + pub ident: Ident, } impl Lifetime { @@ -65,28 +68,22 @@ impl Lifetime { } Lifetime { - term: Term::new(s, span), + apostrophe: Default::default(), + ident: Ident::new(&s[1..], span), } } - - pub fn span(&self) -> Span { - self.term.span() - } - - pub fn set_span(&mut self, span: Span) { - self.term.set_span(span); - } } impl Display for Lifetime { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - self.term.as_str().fmt(formatter) + "'".fmt(formatter)?; + self.ident.fmt(formatter) } } impl PartialEq for Lifetime { fn eq(&self, other: &Lifetime) -> bool { - self.term.as_str() == other.term.as_str() + self.ident.eq(&other.ident) } } @@ -100,13 +97,13 @@ impl PartialOrd for Lifetime { impl Ord for Lifetime { fn cmp(&self, other: &Lifetime) -> Ordering { - self.term.as_str().cmp(other.term.as_str()) + self.ident.cmp(&other.ident) } } impl Hash for Lifetime { fn hash<H: Hasher>(&self, h: &mut H) { - self.term.as_str().hash(h) + self.ident.hash(h) } } @@ -120,15 +117,17 @@ pub mod parsing { impl Synom for Lifetime { fn parse(input: Cursor) -> PResult<Self> { - let (term, rest) = match input.term() { - Some(term) => term, - _ => return parse_error(), + let (apostrophe, rest) = Apostrophe::parse(input)?; + let (ident, rest) = match rest.ident() { + Some(pair) => pair, + None => return parse_error(), }; - if !term.as_str().starts_with('\'') { - return parse_error(); - } - Ok((Lifetime { term: term }, rest)) + let ret = Lifetime { + ident: ident, + apostrophe: apostrophe, + }; + Ok((ret, rest)) } fn description() -> Option<&'static str> { @@ -140,11 +139,13 @@ pub mod parsing { #[cfg(feature = "printing")] mod printing { use super::*; - use quote::{ToTokens, Tokens}; + use quote::ToTokens; + use proc_macro2::TokenStream; impl ToTokens for Lifetime { - fn to_tokens(&self, tokens: &mut Tokens) { - self.term.to_tokens(tokens); + fn to_tokens(&self, tokens: &mut TokenStream) { + self.apostrophe.to_tokens(tokens); + self.ident.to_tokens(tokens); } } } @@ -10,7 +10,7 @@ use proc_macro2::{Literal, Span}; use std::str; #[cfg(feature = "printing")] -use proc_macro2::Term; +use proc_macro2::Ident; #[cfg(feature = "parsing")] use proc_macro2::TokenStream; @@ -429,12 +429,12 @@ pub mod parsing { Ok((Lit::new(lit), rest)) } } - _ => match input.term() { + _ => match input.ident() { Some((term, rest)) => Ok(( Lit::Bool(LitBool { - value: if term.as_str() == "true" { + value: if term == "true" { true - } else if term.as_str() == "false" { + } else if term == "false" { false } else { return parse_error(); @@ -506,53 +506,54 @@ pub mod parsing { #[cfg(feature = "printing")] mod printing { use super::*; - use quote::{ToTokens, Tokens}; + use quote::{ToTokens, TokenStreamExt}; + use proc_macro2::TokenStream; impl ToTokens for LitStr { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.token.to_tokens(tokens); } } impl ToTokens for LitByteStr { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.token.to_tokens(tokens); } } impl ToTokens for LitByte { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.token.to_tokens(tokens); } } impl ToTokens for LitChar { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.token.to_tokens(tokens); } } impl ToTokens for LitInt { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.token.to_tokens(tokens); } } impl ToTokens for LitFloat { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.token.to_tokens(tokens); } } impl ToTokens for LitBool { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { let s = if self.value { "true" } else { "false" }; - tokens.append(Term::new(s, self.span)); + tokens.append(Ident::new(s, self.span)); } } impl ToTokens for LitVerbatim { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.token.to_tokens(tokens); } } @@ -93,10 +93,11 @@ pub mod parsing { #[cfg(feature = "printing")] mod printing { use super::*; - use quote::{ToTokens, Tokens}; + use quote::ToTokens; + use proc_macro2::TokenStream; impl ToTokens for Macro { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.path.to_tokens(tokens); self.bang_token.to_tokens(tokens); match self.delimiter { diff --git a/src/macros.rs b/src/macros.rs index 667c5baf..0017d502 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -133,7 +133,7 @@ macro_rules! generate_to_tokens { (($($arms:tt)*) $tokens:ident $name:ident {}) => { impl ::quote::ToTokens for $name { - fn to_tokens(&self, $tokens: &mut ::quote::Tokens) { + fn to_tokens(&self, $tokens: &mut ::proc_macro2::TokenStream) { match *self { $($arms)* } @@ -174,10 +174,11 @@ pub mod parsing { #[cfg(feature = "printing")] mod printing { use super::*; - use quote::{ToTokens, Tokens}; + use quote::ToTokens; + use proc_macro2::TokenStream; impl ToTokens for BinOp { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { match *self { BinOp::Add(ref t) => t.to_tokens(tokens), BinOp::Sub(ref t) => t.to_tokens(tokens), @@ -212,7 +213,7 @@ mod printing { } impl ToTokens for UnOp { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { match *self { UnOp::Deref(ref t) => t.to_tokens(tokens), UnOp::Not(ref t) => t.to_tokens(tokens), diff --git a/src/parsers.rs b/src/parsers.rs index 7691074c..bdd0c6a0 100644 --- a/src/parsers.rs +++ b/src/parsers.rs @@ -217,7 +217,7 @@ pub fn invoke<T, R, F: FnOnce(T) -> R>(f: F, t: T) -> R { /// #[macro_use] /// extern crate syn; /// -/// use syn::{Expr, Ident}; +/// use syn::Expr; /// /// /// Parses any expression that does not begin with a `-` minus sign. /// named!(not_negative_expr -> Expr, do_parse!( @@ -811,15 +811,16 @@ macro_rules! tuple_parser { /// ```rust /// #[macro_use] /// extern crate syn; +/// extern crate proc_macro2; /// -/// use syn::Ident; +/// use proc_macro2::{Ident, Span}; /// /// // Parse any identifier token, or the `!` token in which case the /// // identifier is treated as `"BANG"`. /// named!(ident_or_bang -> Ident, alt!( /// syn!(Ident) /// | -/// punct!(!) => { |_| "BANG".into() } +/// punct!(!) => { |_| Ident::new("BANG", Span::call_site()) } /// )); /// # /// # fn main() {} @@ -926,10 +927,10 @@ macro_rules! alt { /// extern crate syn; /// extern crate proc_macro2; /// -/// use syn::Ident; +/// use proc_macro2::Ident; +/// use proc_macro2::TokenStream; /// use syn::token::Paren; /// use syn::synom::Synom; -/// use proc_macro2::TokenStream; /// /// /// Parse a macro invocation that uses `(` `)` parentheses. /// /// diff --git a/src/path.rs b/src/path.rs index 9ad8e71a..cded4359 100644 --- a/src/path.rs +++ b/src/path.rs @@ -31,9 +31,11 @@ impl Path { /// ```rust /// extern crate syn; /// extern crate quote; +/// extern crate proc_macro2; /// /// use syn::{QSelf, Path, PathTokens}; -/// use quote::{Tokens, ToTokens}; +/// use proc_macro2::TokenStream; +/// use quote::ToTokens; /// /// struct MyNode { /// qself: Option<QSelf>, @@ -41,7 +43,7 @@ impl Path { /// } /// /// impl ToTokens for MyNode { -/// fn to_tokens(&self, tokens: &mut Tokens) { +/// fn to_tokens(&self, tokens: &mut TokenStream) { /// PathTokens(&self.qself, &self.path).to_tokens(tokens); /// } /// } @@ -416,24 +418,25 @@ pub mod parsing { #[cfg(feature = "printing")] mod printing { use super::*; - use quote::{ToTokens, Tokens}; + use quote::ToTokens; + use proc_macro2::TokenStream; impl ToTokens for Path { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.leading_colon.to_tokens(tokens); self.segments.to_tokens(tokens); } } impl ToTokens for PathSegment { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.ident.to_tokens(tokens); self.arguments.to_tokens(tokens); } } impl ToTokens for PathArguments { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { match *self { PathArguments::None => {} PathArguments::AngleBracketed(ref arguments) => { @@ -448,7 +451,7 @@ mod printing { impl ToTokens for GenericArgument { #[cfg_attr(feature = "cargo-clippy", allow(match_same_arms))] - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { match *self { GenericArgument::Lifetime(ref lt) => lt.to_tokens(tokens), GenericArgument::Type(ref ty) => ty.to_tokens(tokens), @@ -473,7 +476,7 @@ mod printing { } impl ToTokens for AngleBracketedGenericArguments { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.colon2_token.to_tokens(tokens); self.lt_token.to_tokens(tokens); @@ -516,7 +519,7 @@ mod printing { } impl ToTokens for Binding { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.ident.to_tokens(tokens); self.eq_token.to_tokens(tokens); self.ty.to_tokens(tokens); @@ -524,7 +527,7 @@ mod printing { } impl ToTokens for ParenthesizedGenericArguments { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.paren_token.surround(tokens, |tokens| { self.inputs.to_tokens(tokens); }); @@ -533,7 +536,7 @@ mod printing { } impl<'a> ToTokens for PathTokens<'a> { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { let qself = match *self.0 { Some(ref qself) => qself, None => return self.1.to_tokens(tokens), diff --git a/src/punctuated.rs b/src/punctuated.rs index 25bcfc3d..8f4144f3 100644 --- a/src/punctuated.rs +++ b/src/punctuated.rs @@ -762,14 +762,15 @@ where #[cfg(feature = "printing")] mod printing { use super::*; - use quote::{ToTokens, Tokens}; + use quote::{ToTokens, TokenStreamExt}; + use proc_macro2::TokenStream; impl<T, P> ToTokens for Punctuated<T, P> where T: ToTokens, P: ToTokens, { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.pairs()) } } @@ -779,7 +780,7 @@ mod printing { T: ToTokens, P: ToTokens, { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { match *self { Pair::Punctuated(ref a, ref b) => { a.to_tokens(tokens); diff --git a/src/spanned.rs b/src/spanned.rs index 2f96581d..da45c108 100644 --- a/src/spanned.rs +++ b/src/spanned.rs @@ -79,7 +79,7 @@ //! error appear in the correct place underlining the right type. use proc_macro2::{Span, TokenStream}; -use quote::{ToTokens, Tokens}; +use quote::ToTokens; /// A trait that can provide the `Span` of the complete contents of a syntax /// tree node. @@ -109,7 +109,7 @@ where { #[cfg(procmacro2_semver_exempt)] fn span(&self) -> Span { - let mut tokens = Tokens::new(); + let mut tokens = TokenStream::empty(); self.to_tokens(&mut tokens); let token_stream = TokenStream::from(tokens); let mut iter = token_stream.into_iter(); @@ -129,7 +129,7 @@ where #[cfg(not(procmacro2_semver_exempt))] fn span(&self) -> Span { - let mut tokens = Tokens::new(); + let mut tokens = TokenStream::empty(); self.to_tokens(&mut tokens); let token_stream = TokenStream::from(tokens); let mut iter = token_stream.into_iter(); diff --git a/src/synom.rs b/src/synom.rs index eb4c8019..5f0bf78d 100644 --- a/src/synom.rs +++ b/src/synom.rs @@ -155,6 +155,7 @@ use proc_macro; use proc_macro2; pub use error::{PResult, ParseError}; +use error::parse_error; use buffer::{Cursor, TokenBuffer}; @@ -213,6 +214,33 @@ impl Synom for proc_macro2::TokenStream { } } +impl Synom for proc_macro2::Ident { + fn parse(input: Cursor) -> PResult<Self> { + let (term, rest) = match input.ident() { + Some(term) => term, + _ => return parse_error(), + }; + match &term.to_string()[..] { + "_" + // From https://doc.rust-lang.org/grammar.html#keywords + | "abstract" | "alignof" | "as" | "become" | "box" | "break" | "const" + | "continue" | "crate" | "do" | "else" | "enum" | "extern" | "false" | "final" + | "fn" | "for" | "if" | "impl" | "in" | "let" | "loop" | "macro" | "match" + | "mod" | "move" | "mut" | "offsetof" | "override" | "priv" | "proc" | "pub" + | "pure" | "ref" | "return" | "Self" | "self" | "sizeof" | "static" | "struct" + | "super" | "trait" | "true" | "type" | "typeof" | "unsafe" | "unsized" | "use" + | "virtual" | "where" | "while" | "yield" => return parse_error(), + _ => {} + } + + Ok((term, rest)) + } + + fn description() -> Option<&'static str> { + Some("identifier") + } +} + /// Parser that can parse Rust tokens into a particular syntax tree node. /// /// Refer to the [module documentation] for details about parsing in Syn. diff --git a/src/token.rs b/src/token.rs index 1a077ed8..40e2dce1 100644 --- a/src/token.rs +++ b/src/token.rs @@ -97,7 +97,7 @@ //! # fn main() {} //! ``` -use proc_macro2::Span; +use proc_macro2::{Span, Ident}; macro_rules! tokens { ( @@ -111,7 +111,7 @@ macro_rules! tokens { $($keyword:tt pub struct $keyword_name:ident #[$keyword_doc:meta])* } ) => ( - $(token_punct_def! { #[$punct_doc] $punct pub struct $punct_name/$len })* + $(token_punct_def! { #[$punct_doc] pub struct $punct_name/$len })* $(token_punct_parser! { $punct pub struct $punct_name })* $(token_delimiter! { #[$delimiter_doc] $delimiter pub struct $delimiter_name })* $(token_keyword! { #[$keyword_doc] $keyword pub struct $keyword_name })* @@ -119,7 +119,7 @@ macro_rules! tokens { } macro_rules! token_punct_def { - (#[$doc:meta] $s:tt pub struct $name:ident / $len:tt) => { + (#[$doc:meta] pub struct $name:ident / $len:tt) => { #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))] #[$doc] /// @@ -179,7 +179,7 @@ macro_rules! token_punct_parser { ($s:tt pub struct $name:ident) => { #[cfg(feature = "printing")] impl ::quote::ToTokens for $name { - fn to_tokens(&self, tokens: &mut ::quote::Tokens) { + fn to_tokens(&self, tokens: &mut ::proc_macro2::TokenStream) { printing::punct($s, &self.0, tokens); } } @@ -242,7 +242,7 @@ macro_rules! token_keyword { #[cfg(feature = "printing")] impl ::quote::ToTokens for $name { - fn to_tokens(&self, tokens: &mut ::quote::Tokens) { + fn to_tokens(&self, tokens: &mut ::proc_macro2::TokenStream) { printing::keyword($s, &self.0, tokens); } } @@ -306,9 +306,9 @@ macro_rules! token_delimiter { impl $name { #[cfg(feature = "printing")] - pub fn surround<F>(&self, tokens: &mut ::quote::Tokens, f: F) + pub fn surround<F>(&self, tokens: &mut ::proc_macro2::TokenStream, f: F) where - F: FnOnce(&mut ::quote::Tokens), + F: FnOnce(&mut ::proc_macro2::TokenStream), { printing::delim($s, &self.0, tokens, f); } @@ -335,22 +335,28 @@ macro_rules! token_delimiter { token_punct_def! { /// `_` - "_" pub struct Underscore/1 + pub struct Underscore/1 } #[cfg(feature = "printing")] impl ::quote::ToTokens for Underscore { - fn to_tokens(&self, tokens: &mut ::quote::Tokens) { - tokens.append(::proc_macro2::Term::new("_", self.0[0])); + fn to_tokens(&self, tokens: &mut ::proc_macro2::TokenStream) { + use quote::TokenStreamExt; + tokens.append(::proc_macro2::Ident::new("_", self.0[0])); } } #[cfg(feature = "parsing")] impl ::Synom for Underscore { fn parse(input: ::buffer::Cursor) -> ::synom::PResult<Underscore> { - match input.term() { - Some((term, rest)) if term.as_str() == "_" => Ok((Underscore([term.span()]), rest)), - Some(_) => ::parse_error(), + match input.ident() { + Some((term, rest)) => { + if term == "_" { + Ok((Underscore([term.span()]), rest)) + } else { + ::parse_error() + } + } None => parsing::punct("_", input, Underscore), } } @@ -360,6 +366,41 @@ impl ::Synom for Underscore { } } +token_punct_def! { + /// `'` + pub struct Apostrophe/1 +} + +#[cfg(feature = "printing")] +impl ::quote::ToTokens for Apostrophe { + fn to_tokens(&self, tokens: &mut ::proc_macro2::TokenStream) { + use quote::TokenStreamExt; + let mut token = ::proc_macro2::Punct::new('\'', ::proc_macro2::Spacing::Joint); + token.set_span(self.0[0]); + tokens.append(token); + } +} + +#[cfg(feature = "parsing")] +impl ::Synom for Apostrophe { + fn parse(input: ::buffer::Cursor) -> ::synom::PResult<Apostrophe> { + match input.punct() { + Some((op, rest)) => { + if op.as_char() == '\'' && op.spacing() == ::proc_macro2::Spacing::Joint { + Ok((Apostrophe([op.span()]), rest)) + } else { + ::parse_error() + } + } + None => ::parse_error() + } + } + + fn description() -> Option<&'static str> { + Some("`'`") + } +} + tokens! { punct: { "+" pub struct Add/1 /// `+` @@ -673,6 +714,21 @@ macro_rules! keyword { ($i:expr, yield) => { call!($i, <$crate::token::Yield as $crate::synom::Synom>::parse) }; } +macro_rules! ident_from_token { + ($token:ident) => { + impl From<Token![$token]> for Ident { + fn from(token: Token![$token]) -> Ident { + Ident::new(stringify!($token), token.0) + } + } + }; +} + +ident_from_token!(self); +ident_from_token!(Self); +ident_from_token!(super); +ident_from_token!(crate); + #[cfg(feature = "parsing")] mod parsing { use proc_macro2::{Delimiter, Spacing, Span}; @@ -712,16 +768,20 @@ mod parsing { let chars = s.chars(); for (i, (ch, slot)) in chars.zip(&mut spans).enumerate() { - match tokens.op() { - Some((op, rest)) if op.op() == ch => { - if i != s.len() - 1 { - match op.spacing() { - Spacing::Joint => {} - _ => return parse_error(), + match tokens.punct() { + Some((op, rest)) => { + if op.as_char() == ch { + if i != s.len() - 1 { + match op.spacing() { + Spacing::Joint => {} + _ => return parse_error(), + } } + *slot = op.span(); + tokens = rest; + } else { + return parse_error() } - *slot = op.span(); - tokens = rest; } _ => return parse_error(), } @@ -734,8 +794,8 @@ mod parsing { tokens: Cursor<'a>, new: fn(Span) -> T, ) -> PResult<'a, T> { - if let Some((term, rest)) = tokens.term() { - if term.as_str() == keyword { + if let Some((term, rest)) = tokens.ident() { + if term == keyword { return Ok((new(term.span()), rest)); } } @@ -776,10 +836,10 @@ mod parsing { #[cfg(feature = "printing")] mod printing { - use proc_macro2::{Delimiter, Group, Op, Spacing, Span, Term}; - use quote::Tokens; + use proc_macro2::{Delimiter, Group, Punct, Spacing, Span, Ident, TokenStream}; + use quote::TokenStreamExt; - pub fn punct(s: &str, spans: &[Span], tokens: &mut Tokens) { + pub fn punct(s: &str, spans: &[Span], tokens: &mut TokenStream) { assert_eq!(s.len(), spans.len()); let mut chars = s.chars(); @@ -787,23 +847,23 @@ mod printing { let ch = chars.next_back().unwrap(); let span = spans.next_back().unwrap(); for (ch, span) in chars.zip(spans) { - let mut op = Op::new(ch, Spacing::Joint); + let mut op = Punct::new(ch, Spacing::Joint); op.set_span(*span); tokens.append(op); } - let mut op = Op::new(ch, Spacing::Alone); + let mut op = Punct::new(ch, Spacing::Alone); op.set_span(*span); tokens.append(op); } - pub fn keyword(s: &str, span: &Span, tokens: &mut Tokens) { - tokens.append(Term::new(s, *span)); + pub fn keyword(s: &str, span: &Span, tokens: &mut TokenStream) { + tokens.append(Ident::new(s, *span)); } - pub fn delim<F>(s: &str, span: &Span, tokens: &mut Tokens, f: F) + pub fn delim<F>(s: &str, span: &Span, tokens: &mut TokenStream, f: F) where - F: FnOnce(&mut Tokens), + F: FnOnce(&mut TokenStream), { let delim = match s { "(" => Delimiter::Parenthesis, @@ -812,7 +872,7 @@ mod printing { " " => Delimiter::None, _ => panic!("unknown delimiter: {}", s), }; - let mut inner = Tokens::new(); + let mut inner = TokenStream::empty(); f(&mut inner); let mut g = Group::new(delim, inner.into()); g.set_span(*span); @@ -89,8 +89,8 @@ impl<'a> PartialEq for TokenTreeHelper<'a> { } s2.next().is_none() } - (&TokenTree::Op(ref o1), &TokenTree::Op(ref o2)) => { - o1.op() == o2.op() && match (o1.spacing(), o2.spacing()) { + (&TokenTree::Punct(ref o1), &TokenTree::Punct(ref o2)) => { + o1.as_char() == o2.as_char() && match (o1.spacing(), o2.spacing()) { (Spacing::Alone, Spacing::Alone) | (Spacing::Joint, Spacing::Joint) => true, _ => false, } @@ -98,7 +98,7 @@ impl<'a> PartialEq for TokenTreeHelper<'a> { (&TokenTree::Literal(ref l1), &TokenTree::Literal(ref l2)) => { l1.to_string() == l2.to_string() } - (&TokenTree::Term(ref s1), &TokenTree::Term(ref s2)) => s1.as_str() == s2.as_str(), + (&TokenTree::Ident(ref s1), &TokenTree::Ident(ref s2)) => s1 == s2, _ => false, } } @@ -124,16 +124,16 @@ impl<'a> Hash for TokenTreeHelper<'a> { } 0xffu8.hash(h); // terminator w/ a variant we don't normally hash } - TokenTree::Op(ref op) => { + TokenTree::Punct(ref op) => { 1u8.hash(h); - op.op().hash(h); + op.as_char().hash(h); match op.spacing() { Spacing::Alone => 0u8.hash(h), Spacing::Joint => 1u8.hash(h), } } TokenTree::Literal(ref lit) => (2u8, lit.to_string()).hash(h), - TokenTree::Term(ref word) => (3u8, word.as_str()).hash(h), + TokenTree::Ident(ref word) => (3u8, word).hash(h), } } } @@ -646,10 +646,11 @@ pub mod parsing { #[cfg(feature = "printing")] mod printing { use super::*; - use quote::{ToTokens, Tokens}; + use quote::ToTokens; + use proc_macro2::TokenStream; impl ToTokens for TypeSlice { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.bracket_token.surround(tokens, |tokens| { self.elem.to_tokens(tokens); }); @@ -657,7 +658,7 @@ mod printing { } impl ToTokens for TypeArray { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.bracket_token.surround(tokens, |tokens| { self.elem.to_tokens(tokens); self.semi_token.to_tokens(tokens); @@ -667,7 +668,7 @@ mod printing { } impl ToTokens for TypePtr { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.star_token.to_tokens(tokens); match self.mutability { Some(ref tok) => tok.to_tokens(tokens), @@ -680,7 +681,7 @@ mod printing { } impl ToTokens for TypeReference { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.and_token.to_tokens(tokens); self.lifetime.to_tokens(tokens); self.mutability.to_tokens(tokens); @@ -689,7 +690,7 @@ mod printing { } impl ToTokens for TypeBareFn { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.lifetimes.to_tokens(tokens); self.unsafety.to_tokens(tokens); self.abi.to_tokens(tokens); @@ -709,13 +710,13 @@ mod printing { } impl ToTokens for TypeNever { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.bang_token.to_tokens(tokens); } } impl ToTokens for TypeTuple { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.paren_token.surround(tokens, |tokens| { self.elems.to_tokens(tokens); }); @@ -723,27 +724,27 @@ mod printing { } impl ToTokens for TypePath { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { PathTokens(&self.qself, &self.path).to_tokens(tokens); } } impl ToTokens for TypeTraitObject { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.dyn_token.to_tokens(tokens); self.bounds.to_tokens(tokens); } } impl ToTokens for TypeImplTrait { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.impl_token.to_tokens(tokens); self.bounds.to_tokens(tokens); } } impl ToTokens for TypeGroup { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.group_token.surround(tokens, |tokens| { self.elem.to_tokens(tokens); }); @@ -751,7 +752,7 @@ mod printing { } impl ToTokens for TypeParen { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.paren_token.surround(tokens, |tokens| { self.elem.to_tokens(tokens); }); @@ -759,25 +760,25 @@ mod printing { } impl ToTokens for TypeInfer { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.underscore_token.to_tokens(tokens); } } impl ToTokens for TypeMacro { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.mac.to_tokens(tokens); } } impl ToTokens for TypeVerbatim { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.tts.to_tokens(tokens); } } impl ToTokens for ReturnType { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { match *self { ReturnType::Default => {} ReturnType::Type(ref arrow, ref ty) => { @@ -789,7 +790,7 @@ mod printing { } impl ToTokens for BareFnArg { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { if let Some((ref name, ref colon)) = self.name { name.to_tokens(tokens); colon.to_tokens(tokens); @@ -799,7 +800,7 @@ mod printing { } impl ToTokens for BareFnArgName { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { match *self { BareFnArgName::Named(ref t) => t.to_tokens(tokens), BareFnArgName::Wild(ref t) => t.to_tokens(tokens), @@ -808,7 +809,7 @@ mod printing { } impl ToTokens for Abi { - fn to_tokens(&self, tokens: &mut Tokens) { + fn to_tokens(&self, tokens: &mut TokenStream) { self.extern_token.to_tokens(tokens); self.name.to_tokens(tokens); } |