Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

Commit

Permalink
fix(syntex): Use pprust::print_crate to write crate
Browse files Browse the repository at this point in the history
This lets syntex_syntax's print_crate keep in sync with
the rustc pretty printer, and also makes sure that things
like comments are properly synchronized.
  • Loading branch information
erickt committed Feb 17, 2016
1 parent 3b45ced commit a45aa72
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions syntex/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate syntex_syntax;

use std::fs::File;
use std::io;
use std::io::{self, Write};
use std::path::Path;

use syntex_syntax::ast;
Expand All @@ -19,7 +19,7 @@ use syntex_syntax::ext::base::ExtCtxt;
use syntex_syntax::ext::expand;
use syntex_syntax::feature_gate;
use syntex_syntax::parse::{self, token};
use syntex_syntax::print::{pp, pprust};
use syntex_syntax::print::pprust;
use syntex_syntax::ptr::P;

pub type Pass = fn(ast::Crate) -> ast::Crate;
Expand Down Expand Up @@ -149,11 +149,35 @@ impl Registry {
let krate = self.post_expansion_passes.iter()
.fold(krate, |krate, f| (f)(krate));

let dst = try!(File::create(dst));
let src_name = src.to_str().unwrap().to_string();
let src = sess.codemap()
.get_filemap(&src_name)
.src
.as_ref()
.unwrap()
.as_bytes()
.to_vec();
let mut rdr = &src[..];

let mut out = Vec::new();
let annotation = pprust::NoAnn;

{
let out: &mut io::Write = &mut out;

try!(pprust::print_crate(
sess.codemap(),
&sess.span_diagnostic,
&krate,
src_name.to_string(),
&mut rdr,
Box::new(out),
&annotation,
false)
);
}

let mut printer = pprust::rust_printer(Box::new(dst));
try!(printer.print_mod(&krate.module, &krate.attrs[..]));
try!(printer.print_remaining_comments());
pp::eof(&mut printer.s)
let mut dst = try!(File::create(dst));
dst.write_all(&out)
}
}

0 comments on commit a45aa72

Please sign in to comment.