Skip to content

Commit

Permalink
Expose original token reprsentation of syn::Lit
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Jan 6, 2022
1 parent 216c98d commit cfc2f67
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions src/lit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Literal> for LitInt {
Expand Down Expand Up @@ -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<Literal> for LitFloat {
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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());
}
}
}
Expand Down

0 comments on commit cfc2f67

Please sign in to comment.