Add a span to the apostrophe in `Lifetime`
diff --git a/src/gen/fold.rs b/src/gen/fold.rs
index 99ec4a2..ee7120b 100644
--- a/src/gen/fold.rs
+++ b/src/gen/fold.rs
@@ -431,9 +431,6 @@
}
}
-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"))]
@@ -1985,6 +1982,13 @@
}
}
# [ 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 9e719b5..e363944 100644
--- a/src/gen/visit.rs
+++ b/src/gen/visit.rs
@@ -8,7 +8,7 @@
use *;
#[cfg(any(feature = "full", feature = "derive"))]
use punctuated::Punctuated;
-use proc_macro2::Span;
+use proc_macro2::{Span, Ident};
#[cfg(any(feature = "full", feature = "derive"))]
use gen::helper::visit::*;
@@ -1545,6 +1545,7 @@
}
# [ cfg ( any ( feature = "full" , feature = "derive" ) ) ]
pub fn visit_lifetime<'ast, V: Visit<'ast> + ?Sized>(_visitor: &mut V, _i: &'ast Lifetime) {
+ // Skipped field _i . apostrophe;
_visitor.visit_ident(& _i . ident);
}
# [ cfg ( any ( feature = "full" , feature = "derive" ) ) ]
diff --git a/src/gen/visit_mut.rs b/src/gen/visit_mut.rs
index 3bded0a..668f172 100644
--- a/src/gen/visit_mut.rs
+++ b/src/gen/visit_mut.rs
@@ -8,7 +8,7 @@
use *;
#[cfg(any(feature = "full", feature = "derive"))]
use punctuated::Punctuated;
-use proc_macro2::Span;
+use proc_macro2::{Span, Ident};
#[cfg(any(feature = "full", feature = "derive"))]
use gen::helper::visit_mut::*;
@@ -1546,6 +1546,7 @@
}
# [ cfg ( any ( feature = "full" , feature = "derive" ) ) ]
pub fn visit_lifetime_mut<V: VisitMut + ?Sized>(_visitor: &mut V, _i: &mut Lifetime) {
+ // Skipped field _i . apostrophe;
_visitor.visit_ident_mut(& mut _i . ident);
}
# [ cfg ( any ( feature = "full" , feature = "derive" ) ) ]
diff --git a/src/lifetime.rs b/src/lifetime.rs
index 6e4cce1..9a2bec4 100644
--- a/src/lifetime.rs
+++ b/src/lifetime.rs
@@ -13,6 +13,8 @@
use proc_macro2::{Span, Ident};
use unicode_xid::UnicodeXID;
+use token::Apostrophe;
+
/// A Rust lifetime: `'a`.
///
/// Lifetime names must conform to the following rules:
@@ -29,7 +31,8 @@
#[cfg_attr(feature = "extra-traits", derive(Debug))]
#[derive(Clone)]
pub struct Lifetime {
- ident: Ident,
+ pub apostrophe: Apostrophe,
+ pub ident: Ident,
}
impl Lifetime {
@@ -65,17 +68,10 @@
}
Lifetime {
+ apostrophe: Default::default(),
ident: Ident::new(&s[1..], span),
}
}
-
- pub fn span(&self) -> Span {
- self.ident.span()
- }
-
- pub fn set_span(&mut self, span: Span) {
- self.ident.set_span(span);
- }
}
impl Display for Lifetime {
@@ -114,7 +110,6 @@
#[cfg(feature = "parsing")]
pub mod parsing {
use super::*;
- use proc_macro2::Spacing;
use buffer::Cursor;
use parse_error;
use synom::PResult;
@@ -122,22 +117,17 @@
impl Synom for Lifetime {
fn parse(input: Cursor) -> PResult<Self> {
- let rest = match input.op() {
- Some((op, rest)) => {
- if op.as_char() == '\'' && op.spacing() == Spacing::Joint {
- rest
- } else {
- return parse_error()
- }
- }
- _ => return parse_error(),
- };
+ let (apostrophe, rest) = Apostrophe::parse(input)?;
let (ident, rest) = match rest.term() {
Some(pair) => pair,
None => return parse_error(),
};
- Ok((Lifetime { ident: ident }, rest))
+ let ret = Lifetime {
+ ident: ident,
+ apostrophe: apostrophe,
+ };
+ Ok((ret, rest))
}
fn description() -> Option<&'static str> {
@@ -149,12 +139,12 @@
#[cfg(feature = "printing")]
mod printing {
use super::*;
- use quote::{ToTokens, TokenStreamExt};
- use proc_macro2::{TokenStream, Punct, Spacing};
+ use quote::ToTokens;
+ use proc_macro2::TokenStream;
impl ToTokens for Lifetime {
fn to_tokens(&self, tokens: &mut TokenStream) {
- tokens.append(Punct::new('\'', Spacing::Joint));
+ self.apostrophe.to_tokens(tokens);
self.ident.to_tokens(tokens);
}
}
diff --git a/src/token.rs b/src/token.rs
index f0ed103..292e0da 100644
--- a/src/token.rs
+++ b/src/token.rs
@@ -376,6 +376,41 @@
}
}
+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.op() {
+ 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 /// `+`