Move literal_to_string to fmt::Display

This commit is contained in:
Mark Rousskov 2019-06-26 07:23:27 -04:00
parent 0324a2b309
commit 9b0ebfa4e9
4 changed files with 32 additions and 8 deletions

View file

@ -5,7 +5,7 @@ use syntax::parse::ParseSess;
use syntax::parse::lexer::comments;
use syntax::print::pp::{self, Breaks};
use syntax::print::pp::Breaks::{Consistent, Inconsistent};
use syntax::print::pprust::{self, PrintState};
use syntax::print::pprust::PrintState;
use syntax::symbol::kw;
use syntax::util::parser::{self, AssocOp, Fixity};
use syntax_pos::{self, BytePos, FileName};
@ -1226,7 +1226,7 @@ impl<'a> State<'a> {
fn print_literal(&mut self, lit: &hir::Lit) {
self.maybe_print_comment(lit.span.lo());
self.writer().word(pprust::literal_to_string(lit.node.to_lit_token()))
self.writer().word(lit.node.to_lit_token().to_string())
}
pub fn print_expr(&mut self, expr: &hir::Expr) {

View file

@ -344,7 +344,7 @@ impl<'a> Parser<'a> {
// Pack possible quotes and prefixes from the original literal into
// the error literal's symbol so they can be pretty-printed faithfully.
let suffixless_lit = token::Lit::new(lit.kind, lit.symbol, None);
let symbol = Symbol::intern(&pprust::literal_to_string(suffixless_lit));
let symbol = Symbol::intern(&suffixless_lit.to_string());
let lit = token::Lit::new(token::Err, symbol, lit.suffix);
Lit::from_lit_token(lit, span).map_err(|_| unreachable!())
}

View file

@ -80,6 +80,34 @@ pub struct Lit {
pub suffix: Option<Symbol>,
}
impl fmt::Display for Lit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let Lit { kind, symbol, suffix } = *self;
match kind {
Byte => write!(f, "b'{}'", symbol)?,
Char => write!(f, "'{}'", symbol)?,
Str => write!(f, "\"{}\"", symbol)?,
StrRaw(n) => write!(f, "r{delim}\"{string}\"{delim}",
delim="#".repeat(n as usize),
string=symbol)?,
ByteStr => write!(f, "b\"{}\"", symbol)?,
ByteStrRaw(n) => write!(f, "br{delim}\"{string}\"{delim}",
delim="#".repeat(n as usize),
string=symbol)?,
Integer |
Float |
Bool |
Err => write!(f, "{}", symbol)?,
}
if let Some(suffix) = suffix {
write!(f, "{}", suffix)?;
}
Ok(())
}
}
impl LitKind {
/// An English article for the literal token kind.
crate fn article(self) -> &'static str {

View file

@ -426,10 +426,6 @@ pub fn attribute_to_string(attr: &ast::Attribute) -> String {
to_string(|s| s.print_attribute(attr))
}
pub fn lit_to_string(l: &ast::Lit) -> String {
to_string(|s| s.print_literal(l))
}
pub fn variant_to_string(var: &ast::Variant) -> String {
to_string(|s| s.print_variant(var))
}
@ -597,7 +593,7 @@ pub trait PrintState<'a> {
fn print_literal(&mut self, lit: &ast::Lit) {
self.maybe_print_comment(lit.span.lo());
self.writer().word(literal_to_string(lit.token))
self.writer().word(lit.token.to_string())
}
fn print_string(&mut self, st: &str,