Skip to content

Commit e02aa72

Browse files
committed
Refactor pprust a bit.
1 parent 871e570 commit e02aa72

File tree

4 files changed

+2042
-2167
lines changed

4 files changed

+2042
-2167
lines changed

Diff for: src/librustc/driver/driver.rs

+60-41
Original file line numberDiff line numberDiff line change
@@ -595,33 +595,36 @@ pub fn compile_input(sess: Session, cfg: ast::CrateConfig, input: &Input,
595595
struct IdentifiedAnnotation;
596596

597597
impl pprust::PpAnn for IdentifiedAnnotation {
598-
fn pre(&self, node: pprust::AnnNode) -> io::IoResult<()> {
598+
fn pre(&self,
599+
s: &mut pprust::State<IdentifiedAnnotation>,
600+
node: pprust::AnnNode) -> io::IoResult<()> {
599601
match node {
600-
pprust::NodeExpr(s, _) => pprust::popen(s),
602+
pprust::NodeExpr(_) => s.popen(),
601603
_ => Ok(())
602604
}
603605
}
604-
fn post(&self, node: pprust::AnnNode) -> io::IoResult<()> {
606+
fn post(&self,
607+
s: &mut pprust::State<IdentifiedAnnotation>,
608+
node: pprust::AnnNode) -> io::IoResult<()> {
605609
match node {
606-
pprust::NodeItem(s, item) => {
610+
pprust::NodeItem(item) => {
607611
try!(pp::space(&mut s.s));
608-
try!(pprust::synth_comment(s, item.id.to_str()));
612+
s.synth_comment(item.id.to_str())
609613
}
610-
pprust::NodeBlock(s, blk) => {
614+
pprust::NodeBlock(blk) => {
611615
try!(pp::space(&mut s.s));
612-
try!(pprust::synth_comment(s, ~"block " + blk.id.to_str()));
616+
s.synth_comment(~"block " + blk.id.to_str())
613617
}
614-
pprust::NodeExpr(s, expr) => {
618+
pprust::NodeExpr(expr) => {
615619
try!(pp::space(&mut s.s));
616-
try!(pprust::synth_comment(s, expr.id.to_str()));
617-
try!(pprust::pclose(s));
620+
try!(s.synth_comment(expr.id.to_str()));
621+
s.pclose()
618622
}
619-
pprust::NodePat(s, pat) => {
623+
pprust::NodePat(pat) => {
620624
try!(pp::space(&mut s.s));
621-
try!(pprust::synth_comment(s, ~"pat " + pat.id.to_str()));
625+
s.synth_comment(~"pat " + pat.id.to_str())
622626
}
623627
}
624-
Ok(())
625628
}
626629
}
627630
@@ -630,26 +633,29 @@ struct TypedAnnotation {
630633
}
631634
632635
impl pprust::PpAnn for TypedAnnotation {
633-
fn pre(&self, node: pprust::AnnNode) -> io::IoResult<()> {
636+
fn pre(&self,
637+
s: &mut pprust::State<TypedAnnotation>,
638+
node: pprust::AnnNode) -> io::IoResult<()> {
634639
match node {
635-
pprust::NodeExpr(s, _) => pprust::popen(s),
640+
pprust::NodeExpr(_) => s.popen(),
636641
_ => Ok(())
637642
}
638643
}
639-
fn post(&self, node: pprust::AnnNode) -> io::IoResult<()> {
644+
fn post(&self,
645+
s: &mut pprust::State<TypedAnnotation>,
646+
node: pprust::AnnNode) -> io::IoResult<()> {
640647
let tcx = &self.analysis.ty_cx;
641648
match node {
642-
pprust::NodeExpr(s, expr) => {
649+
pprust::NodeExpr(expr) => {
643650
try!(pp::space(&mut s.s));
644651
try!(pp::word(&mut s.s, "as"));
645652
try!(pp::space(&mut s.s));
646653
try!(pp::word(&mut s.s,
647654
ppaux::ty_to_str(tcx, ty::expr_ty(tcx, expr))));
648-
try!(pprust::pclose(s));
655+
s.pclose()
649656
}
650-
_ => ()
657+
_ => Ok(())
651658
}
652-
Ok(())
653659
}
654660
}
655661

@@ -670,34 +676,48 @@ pub fn pretty_print_input(sess: Session,
670676
_ => (krate, None, false)
671677
};
672678

673-
let codemap = sess.codemap;
674-
let span_diagnostic = sess.span_diagnostic;
679+
let src_name = source_name(input);
680+
let src = sess.codemap().get_filemap(src_name).deref().src.as_bytes().to_owned();
681+
let mut rdr = MemReader::new(src);
675682

676-
let annotation = match ppm {
683+
match ppm {
677684
PpmIdentified | PpmExpandedIdentified => {
678-
~IdentifiedAnnotation as ~pprust::PpAnn
685+
pprust::print_crate(sess.codemap(),
686+
sess.diagnostic(),
687+
&krate,
688+
src_name,
689+
&mut rdr,
690+
~io::stdout(),
691+
&IdentifiedAnnotation,
692+
is_expanded)
679693
}
680694
PpmTyped => {
681695
let ast_map = ast_map.expect("--pretty=typed missing ast_map");
682696
let analysis = phase_3_run_analysis_passes(sess, &krate, ast_map);
683-
~TypedAnnotation {
697+
let annotation = TypedAnnotation {
684698
analysis: analysis
685-
} as ~pprust::PpAnn:
699+
};
700+
pprust::print_crate(annotation.analysis.ty_cx.sess.codemap(),
701+
annotation.analysis.ty_cx.sess.diagnostic(),
702+
&krate,
703+
src_name,
704+
&mut rdr,
705+
~io::stdout(),
706+
&annotation,
707+
is_expanded)
686708
}
687-
_ => ~pprust::NoAnn as ~pprust::PpAnn:,
688-
};
709+
_ => {
710+
pprust::print_crate(sess.codemap(),
711+
sess.diagnostic(),
712+
&krate,
713+
src_name,
714+
&mut rdr,
715+
~io::stdout(),
716+
&pprust::NoAnn,
717+
is_expanded)
718+
}
719+
}.unwrap()
689720

690-
let src = &codemap.get_filemap(source_name(input)).src;
691-
let mut rdr = MemReader::new(src.as_bytes().to_owned());
692-
let stdout = io::stdout();
693-
pprust::print_crate(codemap,
694-
span_diagnostic,
695-
&krate,
696-
source_name(input),
697-
&mut rdr,
698-
~stdout as ~io::Writer,
699-
annotation,
700-
is_expanded).unwrap();
701721
}
702722

703723
pub fn get_os(triple: &str) -> Option<abi::Os> {
@@ -778,8 +798,7 @@ pub fn host_triple() -> ~str {
778798
(env!("CFG_COMPILER")).to_owned()
779799
}
780800

781-
pub fn build_session_options(matches: &getopts::Matches)
782-
-> @session::Options {
801+
pub fn build_session_options(matches: &getopts::Matches) -> session::Options {
783802
let mut crate_types: Vec<CrateType> = Vec::new();
784803
let unparsed_crate_types = matches.opt_strs("crate-type");
785804
for unparsed_crate_type in unparsed_crate_types.iter() {

Diff for: src/librustc/middle/dataflow.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,14 @@ struct LoopScope<'a> {
8484
}
8585

8686
impl<'a, O:DataFlowOperator> pprust::PpAnn for DataFlowContext<'a, O> {
87-
fn pre(&self, node: pprust::AnnNode) -> io::IoResult<()> {
88-
let (ps, id) = match node {
89-
pprust::NodeExpr(ps, expr) => (ps, expr.id),
90-
pprust::NodeBlock(ps, blk) => (ps, blk.id),
91-
pprust::NodeItem(ps, _) => (ps, 0),
92-
pprust::NodePat(ps, pat) => (ps, pat.id)
87+
fn pre(&self,
88+
ps: &mut pprust::State<DataFlowContext<'a, O>>,
89+
node: pprust::AnnNode) -> io::IoResult<()> {
90+
let id = match node {
91+
pprust::NodeExpr(expr) => expr.id,
92+
pprust::NodeBlock(blk) => blk.id,
93+
pprust::NodeItem(_) => 0,
94+
pprust::NodePat(pat) => pat.id
9395
};
9496

9597
if self.nodeid_to_bitset.contains_key(&id) {
@@ -111,9 +113,8 @@ impl<'a, O:DataFlowOperator> pprust::PpAnn for DataFlowContext<'a, O> {
111113
~""
112114
};
113115

114-
let comment_str = format!("id {}: {}{}{}",
115-
id, entry_str, gens_str, kills_str);
116-
try!(pprust::synth_comment(ps, comment_str));
116+
try!(ps.synth_comment(format!("id {}: {}{}{}", id, entry_str,
117+
gens_str, kills_str)));
117118
try!(pp::space(&mut ps.s));
118119
}
119120
Ok(())
@@ -351,11 +352,10 @@ impl<'a, O:DataFlowOperator+Clone+'static> DataFlowContext<'a, O> {
351352
fn pretty_print_to(&self, wr: ~io::Writer,
352353
blk: &ast::Block) -> io::IoResult<()> {
353354
let mut ps = pprust::rust_printer_annotated(wr, self);
354-
try!(pprust::cbox(&mut ps, pprust::indent_unit));
355-
try!(pprust::ibox(&mut ps, 0u));
356-
try!(pprust::print_block(&mut ps, blk));
357-
try!(pp::eof(&mut ps.s));
358-
Ok(())
355+
try!(ps.cbox(pprust::indent_unit));
356+
try!(ps.ibox(0u));
357+
try!(ps.print_block(blk));
358+
pp::eof(&mut ps.s)
359359
}
360360
}
361361

Diff for: src/librustc/middle/typeck/check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ fn compare_impl_method(tcx: &ty::ctxt,
800800
format!("method `{}` has a `{}` declaration in the impl, \
801801
but not in the trait",
802802
token::get_ident(trait_m.ident),
803-
pprust::explicit_self_to_str(&impl_m.explicit_self)));
803+
pprust::explicit_self_to_str(impl_m.explicit_self)));
804804
return;
805805
}
806806
(_, &ast::SelfStatic) => {
@@ -809,7 +809,7 @@ fn compare_impl_method(tcx: &ty::ctxt,
809809
format!("method `{}` has a `{}` declaration in the trait, \
810810
but not in the impl",
811811
token::get_ident(trait_m.ident),
812-
pprust::explicit_self_to_str(&trait_m.explicit_self)));
812+
pprust::explicit_self_to_str(trait_m.explicit_self)));
813813
return;
814814
}
815815
_ => {

0 commit comments

Comments
 (0)