1
Fork 0

Move pp::Printer helpers to direct impl

This commit is contained in:
Mark Rousskov 2019-07-09 09:26:50 -04:00
parent e91dbc5916
commit cab453250a
10 changed files with 66 additions and 55 deletions

View file

@ -1212,8 +1212,6 @@ impl<'a> print::State<'a> {
Node::Pat(a) => self.print_pat(&a), Node::Pat(a) => self.print_pat(&a),
Node::Arm(a) => self.print_arm(&a), Node::Arm(a) => self.print_arm(&a),
Node::Block(a) => { Node::Block(a) => {
use syntax::print::pprust::PrintState;
// containing cbox, will be closed by print-block at } // containing cbox, will be closed by print-block at }
self.cbox(print::indent_unit); self.cbox(print::indent_unit);
// head-ibox, will be closed by print-block after { // head-ibox, will be closed by print-block after {

View file

@ -73,6 +73,19 @@ pub struct State<'a> {
ann: &'a (dyn PpAnn + 'a), ann: &'a (dyn PpAnn + 'a),
} }
impl std::ops::Deref for State<'_> {
type Target = pp::Printer;
fn deref(&self) -> &Self::Target {
&self.s
}
}
impl std::ops::DerefMut for State<'_> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.s
}
}
impl<'a> PrintState<'a> for State<'a> { impl<'a> PrintState<'a> for State<'a> {
fn writer(&mut self) -> &mut pp::Printer { fn writer(&mut self) -> &mut pp::Printer {
&mut self.s &mut self.s

View file

@ -8,7 +8,6 @@ use rustc::cfg::CFGIndex;
use rustc::ty::TyCtxt; use rustc::ty::TyCtxt;
use std::mem; use std::mem;
use std::usize; use std::usize;
use syntax::print::pprust::PrintState;
use log::debug; use log::debug;
use rustc_data_structures::graph::implementation::OUTGOING; use rustc_data_structures::graph::implementation::OUTGOING;

View file

@ -19,7 +19,6 @@ use rustc_mir::util::{write_mir_pretty, write_mir_graphviz};
use syntax::ast; use syntax::ast;
use syntax::mut_visit::MutVisitor; use syntax::mut_visit::MutVisitor;
use syntax::print::{pprust}; use syntax::print::{pprust};
use syntax::print::pprust::PrintState;
use syntax_pos::FileName; use syntax_pos::FileName;
use graphviz as dot; use graphviz as dot;

View file

@ -162,6 +162,7 @@ pub mod visit;
pub mod print { pub mod print {
pub mod pp; pub mod pp;
pub mod pprust; pub mod pprust;
mod helpers;
} }
pub mod ext { pub mod ext {

View file

@ -611,8 +611,6 @@ 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 crate::print::pprust::PrintState;
s.s.word("&"); 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);

View file

@ -2571,7 +2571,6 @@ impl<'a> Parser<'a> {
None => continue, None => continue,
}; };
let sugg = pprust::to_string(|s| { let sugg = pprust::to_string(|s| {
use crate::print::pprust::PrintState;
s.popen(); s.popen();
s.print_expr(&e); s.print_expr(&e);
s.s.word( "."); s.s.word( ".");
@ -4588,7 +4587,7 @@ impl<'a> Parser<'a> {
stmt_span = stmt_span.with_hi(self.prev_span.hi()); stmt_span = stmt_span.with_hi(self.prev_span.hi());
} }
let sugg = pprust::to_string(|s| { let sugg = pprust::to_string(|s| {
use crate::print::pprust::{PrintState, INDENT_UNIT}; use crate::print::pprust::INDENT_UNIT;
s.ibox(INDENT_UNIT); s.ibox(INDENT_UNIT);
s.bopen(); s.bopen();
s.print_stmt(&stmt); s.print_stmt(&stmt);

View file

@ -0,0 +1,34 @@
use std::borrow::Cow;
use crate::print::pp::Printer;
impl Printer {
pub fn word_space<W: Into<Cow<'static, str>>>(&mut self, w: W) {
self.word(w);
self.space();
}
pub fn popen(&mut self) {
self.word("(");
}
pub fn pclose(&mut self) {
self.word(")");
}
pub fn hardbreak_if_not_bol(&mut self) {
if !self.is_beginning_of_line() {
self.hardbreak()
}
}
pub fn space_if_not_bol(&mut self) {
if !self.is_beginning_of_line() { self.space(); }
}
pub fn nbsp(&mut self) { self.word(" ") }
pub fn word_nbsp<S: Into<Cow<'static, str>>>(&mut self, w: S) {
self.word(w);
self.nbsp()
}
}

View file

@ -597,7 +597,7 @@ impl Printer {
// Convenience functions to talk to the printer. // Convenience functions to talk to the printer.
/// "raw box" /// "raw box"
crate fn rbox(&mut self, indent: usize, b: Breaks) { pub fn rbox(&mut self, indent: usize, b: Breaks) {
self.scan_begin(BeginToken { self.scan_begin(BeginToken {
offset: indent as isize, offset: indent as isize,
breaks: b breaks: b
@ -605,7 +605,7 @@ impl Printer {
} }
/// Inconsistent breaking box /// Inconsistent breaking box
crate fn ibox(&mut self, indent: usize) { pub fn ibox(&mut self, indent: usize) {
self.rbox(indent, Breaks::Inconsistent) self.rbox(indent, Breaks::Inconsistent)
} }
@ -621,7 +621,7 @@ impl Printer {
}) })
} }
crate fn end(&mut self) { pub fn end(&mut self) {
self.scan_end() self.scan_end()
} }

View file

@ -432,38 +432,23 @@ fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String {
format!("{}{}", to_string(|s| s.print_visibility(vis)), s) format!("{}{}", to_string(|s| s.print_visibility(vis)), s)
} }
pub trait PrintState<'a> { impl std::ops::Deref for State<'_> {
type Target = pp::Printer;
fn deref(&self) -> &Self::Target {
&self.s
}
}
impl std::ops::DerefMut for State<'_> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.s
}
}
pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefMut {
fn writer(&mut self) -> &mut pp::Printer; fn writer(&mut self) -> &mut pp::Printer;
fn comments(&mut self) -> &mut Option<Comments<'a>>; fn comments(&mut self) -> &mut Option<Comments<'a>>;
fn word_space<S: Into<Cow<'static, str>>>(&mut self, w: S) {
self.writer().word(w);
self.writer().space()
}
fn popen(&mut self) { self.writer().word("(") }
fn pclose(&mut self) { self.writer().word(")") }
fn hardbreak_if_not_bol(&mut self) {
if !self.writer().is_beginning_of_line() {
self.writer().hardbreak()
}
}
// "raw box"
fn rbox(&mut self, u: usize, b: pp::Breaks) {
self.writer().rbox(u, b)
}
fn ibox(&mut self, u: usize) {
self.writer().ibox(u);
}
fn end(&mut self) {
self.writer().end()
}
fn commasep<T, F>(&mut self, b: Breaks, elts: &[T], mut op: F) fn commasep<T, F>(&mut self, b: Breaks, elts: &[T], mut op: F)
where F: FnMut(&mut Self, &T), where F: FnMut(&mut Self, &T),
{ {
@ -728,12 +713,6 @@ pub trait PrintState<'a> {
} }
self.end(); self.end();
} }
fn space_if_not_bol(&mut self) {
if !self.writer().is_beginning_of_line() { self.writer().space(); }
}
fn nbsp(&mut self) { self.writer().word(" ") }
} }
impl<'a> PrintState<'a> for State<'a> { impl<'a> PrintState<'a> for State<'a> {
@ -747,15 +726,6 @@ impl<'a> PrintState<'a> for State<'a> {
} }
impl<'a> State<'a> { impl<'a> State<'a> {
pub fn cbox(&mut self, u: usize) {
self.s.cbox(u);
}
crate fn word_nbsp<S: Into<Cow<'static, str>>>(&mut self, w: S) {
self.s.word(w);
self.nbsp()
}
crate fn head<S: Into<Cow<'static, str>>>(&mut self, w: S) { crate fn head<S: Into<Cow<'static, str>>>(&mut self, w: S) {
let w = w.into(); let w = w.into();
// outer-box is consistent // outer-box is consistent