1
Fork 0

Refactor methods onto Printer struct.

No (intentional) changes to behavior. This is intended to avoid the
anti-pattern of having to import individual methods throughout code.
This commit is contained in:
Mark Simulacrum 2017-06-24 21:22:42 -06:00
parent a1f180bde3
commit bac4bb9613
6 changed files with 521 additions and 530 deletions

File diff suppressed because it is too large Load diff

View file

@ -21,7 +21,6 @@ use std::io;
use std::mem; use std::mem;
use std::usize; use std::usize;
use syntax::ast; use syntax::ast;
use syntax::print::pp;
use syntax::print::pprust::PrintState; use syntax::print::pprust::PrintState;
use util::nodemap::NodeMap; use util::nodemap::NodeMap;
use hir; use hir;
@ -157,7 +156,7 @@ impl<'a, 'tcx, O:DataFlowOperator> pprust::PpAnn for DataFlowContext<'a, 'tcx, O
ps.synth_comment( ps.synth_comment(
format!("id {}: {}{}{}{}", id, entry_str, format!("id {}: {}{}{}{}", id, entry_str,
gens_str, action_kills_str, scope_kills_str))?; gens_str, action_kills_str, scope_kills_str))?;
pp::space(&mut ps.s)?; ps.s.space()?;
} }
Ok(()) Ok(())
} }

View file

@ -30,7 +30,7 @@ use rustc_mir::util::{write_mir_pretty, write_mir_graphviz};
use syntax::ast::{self, BlockCheckMode}; use syntax::ast::{self, BlockCheckMode};
use syntax::fold::{self, Folder}; use syntax::fold::{self, Folder};
use syntax::print::{pp, pprust}; use syntax::print::{pprust};
use syntax::print::pprust::PrintState; use syntax::print::pprust::PrintState;
use syntax::ptr::P; use syntax::ptr::P;
use syntax::util::small_vector::SmallVector; use syntax::util::small_vector::SmallVector;
@ -357,24 +357,24 @@ impl<'hir> pprust::PpAnn for IdentifiedAnnotation<'hir> {
pprust::NodeName(_) => Ok(()), pprust::NodeName(_) => Ok(()),
pprust::NodeItem(item) => { pprust::NodeItem(item) => {
pp::space(&mut s.s)?; s.s.space()?;
s.synth_comment(item.id.to_string()) s.synth_comment(item.id.to_string())
} }
pprust::NodeSubItem(id) => { pprust::NodeSubItem(id) => {
pp::space(&mut s.s)?; s.s.space()?;
s.synth_comment(id.to_string()) s.synth_comment(id.to_string())
} }
pprust::NodeBlock(blk) => { pprust::NodeBlock(blk) => {
pp::space(&mut s.s)?; s.s.space()?;
s.synth_comment(format!("block {}", blk.id)) s.synth_comment(format!("block {}", blk.id))
} }
pprust::NodeExpr(expr) => { pprust::NodeExpr(expr) => {
pp::space(&mut s.s)?; s.s.space()?;
s.synth_comment(expr.id.to_string())?; s.synth_comment(expr.id.to_string())?;
s.pclose() s.pclose()
} }
pprust::NodePat(pat) => { pprust::NodePat(pat) => {
pp::space(&mut s.s)?; s.s.space()?;
s.synth_comment(format!("pat {}", pat.id)) s.synth_comment(format!("pat {}", pat.id))
} }
} }
@ -414,24 +414,24 @@ impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> {
match node { match node {
pprust_hir::NodeName(_) => Ok(()), pprust_hir::NodeName(_) => Ok(()),
pprust_hir::NodeItem(item) => { pprust_hir::NodeItem(item) => {
pp::space(&mut s.s)?; s.s.space()?;
s.synth_comment(item.id.to_string()) s.synth_comment(item.id.to_string())
} }
pprust_hir::NodeSubItem(id) => { pprust_hir::NodeSubItem(id) => {
pp::space(&mut s.s)?; s.s.space()?;
s.synth_comment(id.to_string()) s.synth_comment(id.to_string())
} }
pprust_hir::NodeBlock(blk) => { pprust_hir::NodeBlock(blk) => {
pp::space(&mut s.s)?; s.s.space()?;
s.synth_comment(format!("block {}", blk.id)) s.synth_comment(format!("block {}", blk.id))
} }
pprust_hir::NodeExpr(expr) => { pprust_hir::NodeExpr(expr) => {
pp::space(&mut s.s)?; s.s.space()?;
s.synth_comment(expr.id.to_string())?; s.synth_comment(expr.id.to_string())?;
s.pclose() s.pclose()
} }
pprust_hir::NodePat(pat) => { pprust_hir::NodePat(pat) => {
pp::space(&mut s.s)?; s.s.space()?;
s.synth_comment(format!("pat {}", pat.id)) s.synth_comment(format!("pat {}", pat.id))
} }
} }
@ -456,13 +456,13 @@ impl<'a> pprust::PpAnn for HygieneAnnotation<'a> {
fn post(&self, s: &mut pprust::State, node: pprust::AnnNode) -> io::Result<()> { fn post(&self, s: &mut pprust::State, node: pprust::AnnNode) -> io::Result<()> {
match node { match node {
pprust::NodeIdent(&ast::Ident { name, ctxt }) => { pprust::NodeIdent(&ast::Ident { name, ctxt }) => {
pp::space(&mut s.s)?; s.s.space()?;
// FIXME #16420: this doesn't display the connections // FIXME #16420: this doesn't display the connections
// between syntax contexts // between syntax contexts
s.synth_comment(format!("{}{:?}", name.as_u32(), ctxt)) s.synth_comment(format!("{}{:?}", name.as_u32(), ctxt))
} }
pprust::NodeName(&name) => { pprust::NodeName(&name) => {
pp::space(&mut s.s)?; s.s.space()?;
s.synth_comment(name.as_u32().to_string()) s.synth_comment(name.as_u32().to_string())
} }
_ => Ok(()), _ => Ok(()),
@ -514,10 +514,10 @@ impl<'a, 'tcx> pprust_hir::PpAnn for TypedAnnotation<'a, 'tcx> {
fn post(&self, s: &mut pprust_hir::State, node: pprust_hir::AnnNode) -> io::Result<()> { fn post(&self, s: &mut pprust_hir::State, node: pprust_hir::AnnNode) -> io::Result<()> {
match node { match node {
pprust_hir::NodeExpr(expr) => { pprust_hir::NodeExpr(expr) => {
pp::space(&mut s.s)?; s.s.space()?;
pp::word(&mut s.s, "as")?; s.s.word("as")?;
pp::space(&mut s.s)?; s.s.space()?;
pp::word(&mut s.s, &self.tables.get().expr_ty(expr).to_string())?; s.s.word(&self.tables.get().expr_ty(expr).to_string())?;
s.pclose() s.pclose()
} }
_ => Ok(()), _ => Ok(()),
@ -945,13 +945,13 @@ pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
for node_id in uii.all_matching_node_ids(hir_map) { for node_id in uii.all_matching_node_ids(hir_map) {
let node = hir_map.get(node_id); let node = hir_map.get(node_id);
pp_state.print_node(node)?; pp_state.print_node(node)?;
pp::space(&mut pp_state.s)?; pp_state.s.space()?;
let path = annotation.node_path(node_id) let path = annotation.node_path(node_id)
.expect("--unpretty missing node paths"); .expect("--unpretty missing node paths");
pp_state.synth_comment(path)?; pp_state.synth_comment(path)?;
pp::hardbreak(&mut pp_state.s)?; pp_state.s.hardbreak()?;
} }
pp::eof(&mut pp_state.s) pp_state.s.eof()
}) })
} }
_ => unreachable!(), _ => unreachable!(),

View file

@ -1480,10 +1480,9 @@ impl<'a> Parser<'a> {
match ty.node { match ty.node {
TyKind::Rptr(ref lifetime, ref mut_ty) => { TyKind::Rptr(ref lifetime, ref mut_ty) => {
let sum_with_parens = pprust::to_string(|s| { let sum_with_parens = pprust::to_string(|s| {
use print::pp::word;
use print::pprust::PrintState; use print::pprust::PrintState;
word(&mut s.s, "&")?; s.s.word("&")?;
s.print_opt_lifetime(lifetime)?; s.print_opt_lifetime(lifetime)?;
s.print_mutability(mut_ty.mutbl)?; s.print_mutability(mut_ty.mutbl)?;
s.popen()?; s.popen()?;
@ -2542,14 +2541,13 @@ impl<'a> Parser<'a> {
}; };
let sugg = pprust::to_string(|s| { let sugg = pprust::to_string(|s| {
use print::pprust::PrintState; use print::pprust::PrintState;
use print::pp::word;
s.popen()?; s.popen()?;
s.print_expr(&e)?; s.print_expr(&e)?;
word(&mut s.s, ".")?; s.s.word( ".")?;
s.print_usize(float.trunc() as usize)?; s.print_usize(float.trunc() as usize)?;
s.pclose()?; s.pclose()?;
word(&mut s.s, ".")?; s.s.word(".")?;
word(&mut s.s, fstr.splitn(2, ".").last().unwrap()) s.s.word(fstr.splitn(2, ".").last().unwrap())
}); });
err.span_suggestion( err.span_suggestion(
lo.to(self.prev_span), lo.to(self.prev_span),

View file

@ -262,7 +262,7 @@ pub fn mk_printer<'a>(out: Box<io::Write+'a>, linewidth: usize) -> Printer<'a> {
} }
pub struct Printer<'a> { pub struct Printer<'a> {
pub out: Box<io::Write+'a>, out: Box<io::Write+'a>,
buf_len: usize, buf_len: usize,
/// Width of lines we're constrained to /// Width of lines we're constrained to
margin: isize, margin: isize,
@ -577,75 +577,75 @@ impl<'a> Printer<'a> {
} }
} }
} }
}
// Convenience functions to talk to the printer. // Convenience functions to talk to the printer.
/// "raw box" /// "raw box"
pub fn rbox(p: &mut Printer, indent: usize, b: Breaks) -> io::Result<()> { pub fn rbox(&mut self, indent: usize, b: Breaks) -> io::Result<()> {
p.pretty_print(Token::Begin(BeginToken { self.pretty_print(Token::Begin(BeginToken {
offset: indent as isize, offset: indent as isize,
breaks: b breaks: b
})) }))
} }
/// Inconsistent breaking box /// Inconsistent breaking box
pub fn ibox(p: &mut Printer, indent: usize) -> io::Result<()> { pub fn ibox(&mut self, indent: usize) -> io::Result<()> {
rbox(p, indent, Breaks::Inconsistent) self.rbox(indent, Breaks::Inconsistent)
} }
/// Consistent breaking box /// Consistent breaking box
pub fn cbox(p: &mut Printer, indent: usize) -> io::Result<()> { pub fn cbox(&mut self, indent: usize) -> io::Result<()> {
rbox(p, indent, Breaks::Consistent) self.rbox(indent, Breaks::Consistent)
} }
pub fn break_offset(p: &mut Printer, n: usize, off: isize) -> io::Result<()> { pub fn break_offset(&mut self, n: usize, off: isize) -> io::Result<()> {
p.pretty_print(Token::Break(BreakToken { self.pretty_print(Token::Break(BreakToken {
offset: off, offset: off,
blank_space: n as isize blank_space: n as isize
})) }))
} }
pub fn end(p: &mut Printer) -> io::Result<()> { pub fn end(&mut self) -> io::Result<()> {
p.pretty_print(Token::End) self.pretty_print(Token::End)
} }
pub fn eof(p: &mut Printer) -> io::Result<()> { pub fn eof(&mut self) -> io::Result<()> {
p.pretty_print(Token::Eof) self.pretty_print(Token::Eof)
} }
pub fn word(p: &mut Printer, wrd: &str) -> io::Result<()> { pub fn word(&mut self, wrd: &str) -> io::Result<()> {
p.pretty_print(Token::String(wrd.to_string(), wrd.len() as isize)) self.pretty_print(Token::String(wrd.to_string(), wrd.len() as isize))
} }
pub fn huge_word(p: &mut Printer, wrd: &str) -> io::Result<()> { pub fn huge_word(&mut self, wrd: &str) -> io::Result<()> {
p.pretty_print(Token::String(wrd.to_string(), SIZE_INFINITY)) self.pretty_print(Token::String(wrd.to_string(), SIZE_INFINITY))
} }
pub fn zero_word(p: &mut Printer, wrd: &str) -> io::Result<()> { pub fn zero_word(&mut self, wrd: &str) -> io::Result<()> {
p.pretty_print(Token::String(wrd.to_string(), 0)) self.pretty_print(Token::String(wrd.to_string(), 0))
} }
pub fn spaces(p: &mut Printer, n: usize) -> io::Result<()> { fn spaces(&mut self, n: usize) -> io::Result<()> {
break_offset(p, n, 0) self.break_offset(n, 0)
} }
pub fn zerobreak(p: &mut Printer) -> io::Result<()> { pub fn zerobreak(&mut self) -> io::Result<()> {
spaces(p, 0) self.spaces(0)
} }
pub fn space(p: &mut Printer) -> io::Result<()> { pub fn space(&mut self) -> io::Result<()> {
spaces(p, 1) self.spaces(1)
} }
pub fn hardbreak(p: &mut Printer) -> io::Result<()> { pub fn hardbreak(&mut self) -> io::Result<()> {
spaces(p, SIZE_INFINITY as usize) self.spaces(SIZE_INFINITY as usize)
} }
pub fn hardbreak_tok_offset(off: isize) -> Token { pub fn hardbreak_tok_offset(off: isize) -> Token {
Token::Break(BreakToken {offset: off, blank_space: SIZE_INFINITY}) Token::Break(BreakToken {offset: off, blank_space: SIZE_INFINITY})
} }
pub fn hardbreak_tok() -> Token { pub fn hardbreak_tok() -> Token {
hardbreak_tok_offset(0) Self::hardbreak_tok_offset(0)
}
} }

File diff suppressed because it is too large Load diff