From cfc2f67f72a2f3fc8e0a3c880952d9f75a9cfb3b Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 5 Jan 2022 20:47:47 -0800 Subject: [PATCH] Expose original token reprsentation of syn::Lit --- src/lit.rs | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/lit.rs b/src/lit.rs index 31d0c7ec63..bb61ee26a8 100644 --- a/src/lit.rs +++ b/src/lit.rs @@ -3,9 +3,7 @@ use crate::lookahead; #[cfg(feature = "parsing")] use crate::parse::{Parse, Parser}; use crate::{Error, Result}; -#[cfg(feature = "printing")] -use proc_macro2::Ident; -use proc_macro2::{Literal, Span}; +use proc_macro2::{Ident, Literal, Span}; #[cfg(feature = "parsing")] use proc_macro2::{TokenStream, TokenTree}; use std::fmt::{self, Display}; @@ -243,6 +241,10 @@ impl LitStr { pub fn suffix(&self) -> &str { &self.repr.suffix } + + pub fn token(&self) -> Literal { + self.repr.token.clone() + } } impl LitByteStr { @@ -274,6 +276,10 @@ impl LitByteStr { pub fn suffix(&self) -> &str { &self.repr.suffix } + + pub fn token(&self) -> Literal { + self.repr.token.clone() + } } impl LitByte { @@ -305,6 +311,10 @@ impl LitByte { pub fn suffix(&self) -> &str { &self.repr.suffix } + + pub fn token(&self) -> Literal { + self.repr.token.clone() + } } impl LitChar { @@ -336,6 +346,10 @@ impl LitChar { pub fn suffix(&self) -> &str { &self.repr.suffix } + + pub fn token(&self) -> Literal { + self.repr.token.clone() + } } impl LitInt { @@ -407,6 +421,10 @@ impl LitInt { pub fn set_span(&mut self, span: Span) { self.repr.token.set_span(span); } + + pub fn token(&self) -> Literal { + self.repr.token.clone() + } } impl From for LitInt { @@ -479,6 +497,10 @@ impl LitFloat { pub fn set_span(&mut self, span: Span) { self.repr.token.set_span(span); } + + pub fn token(&self) -> Literal { + self.repr.token.clone() + } } impl From for LitFloat { @@ -520,6 +542,11 @@ impl LitBool { pub fn set_span(&mut self, span: Span) { self.span = span; } + + pub fn token(&self) -> Ident { + let s = if self.value { "true" } else { "false" }; + Ident::new(s, self.span) + } } #[cfg(feature = "extra-traits")] @@ -915,8 +942,7 @@ mod printing { #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))] impl ToTokens for LitBool { fn to_tokens(&self, tokens: &mut TokenStream) { - let s = if self.value { "true" } else { "false" }; - tokens.append(Ident::new(s, self.span)); + tokens.append(self.token()); } } }