-impl Lit {
- crate fn tokens(&self) -> TokenStream {
- TokenTree::Token(self.span, self.node.token()).into()
- }
-}
-
-impl LitKind {
- fn token(&self) -> Token {
- use std::ascii;
-
- match *self {
- LitKind::Str(string, ast::StrStyle::Cooked) => {
- let escaped = string.as_str().escape_default().to_string();
- Token::Literal(token::Lit::Str_(Symbol::intern(&escaped)), None)
- }
- LitKind::Str(string, ast::StrStyle::Raw(n)) => {
- Token::Literal(token::Lit::StrRaw(string, n), None)
- }
- LitKind::ByteStr(ref bytes) => {
- let string = bytes.iter().cloned().flat_map(ascii::escape_default)
- .map(Into::<char>::into).collect::<String>();
- Token::Literal(token::Lit::ByteStr(Symbol::intern(&string)), None)
- }
- LitKind::Byte(byte) => {
- let string: String = ascii::escape_default(byte).map(Into::<char>::into).collect();
- Token::Literal(token::Lit::Byte(Symbol::intern(&string)), None)
- }
- LitKind::Char(ch) => {
- let string: String = ch.escape_default().map(Into::<char>::into).collect();
- Token::Literal(token::Lit::Char(Symbol::intern(&string)), None)
- }
- LitKind::Int(n, ty) => {
- let suffix = match ty {
- ast::LitIntType::Unsigned(ty) => Some(Symbol::intern(ty.ty_to_string())),
- ast::LitIntType::Signed(ty) => Some(Symbol::intern(ty.ty_to_string())),
- ast::LitIntType::Unsuffixed => None,
- };
- Token::Literal(token::Lit::Integer(Symbol::intern(&n.to_string())), suffix)
- }
- LitKind::Float(symbol, ty) => {
- Token::Literal(token::Lit::Float(symbol), Some(Symbol::intern(ty.ty_to_string())))
- }
- LitKind::FloatUnsuffixed(symbol) => Token::Literal(token::Lit::Float(symbol), None),
- LitKind::Bool(value) => Token::Ident(Ident::with_empty_ctxt(Symbol::intern(if value {
- "true"
- } else {
- "false"
- })), false),
- LitKind::Err(val) => Token::Literal(token::Lit::Err(val), None),
- }
- }
-
- fn from_token(token: Token) -> Option<LitKind> {
- match token {
- Token::Ident(ident, false) if ident.name == "true" => Some(LitKind::Bool(true)),
- Token::Ident(ident, false) if ident.name == "false" => Some(LitKind::Bool(false)),
- Token::Interpolated(nt) => match *nt {
- token::NtExpr(ref v) | token::NtLiteral(ref v) => match v.node {
- ExprKind::Lit(ref lit) => Some(lit.node.clone()),
- _ => None,
- },
- _ => None,
- },
- Token::Literal(lit, suf) => {
- let (suffix_illegal, result) = parse::lit_token(lit, suf, None);
- if suffix_illegal && suf.is_some() {
- return None;
- }
- result
- }
- _ => None,
- }
- }
-}
-