Skip to content

Commit

Permalink
syntax::parse: optimize file_to_filemap to read a string directly.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed May 13, 2015
1 parent 6a59d18 commit 0d50b04
Showing 1 changed file with 7 additions and 21 deletions.
28 changes: 7 additions & 21 deletions src/libsyntax/parse/mod.rs
Expand Up @@ -221,30 +221,16 @@ pub fn new_parser_from_tts<'a>(sess: &'a ParseSess,
/// Given a session and a path and an optional span (for error reporting),
/// add the path to the session's codemap and return the new filemap.
pub fn file_to_filemap(sess: &ParseSess, path: &Path, spanopt: Option<Span>)
-> Rc<FileMap> {
let err = |msg: &str| {
-> Rc<FileMap> {
let mut contents = String::new();
if let Err(e) = File::open(path).and_then(|mut f| f.read_to_string(&mut contents)) {
let msg = format!("couldn't read {:?}: {}", path.display(), e);
match spanopt {
Some(sp) => panic!(sess.span_diagnostic.span_fatal(sp, msg)),
None => sess.span_diagnostic.handler().fatal(msg),
}
};
let mut bytes = Vec::new();
match File::open(path).and_then(|mut f| f.read_to_end(&mut bytes)) {
Ok(..) => {}
Err(e) => {
err(&format!("couldn't read {:?}: {}", path.display(), e));
unreachable!();
}
};
match str::from_utf8(&bytes[..]).ok() {
Some(s) => {
sess.codemap().new_filemap(path.to_str().unwrap().to_string(), s.to_string())
}
None => {
err(&format!("{:?} is not UTF-8 encoded", path.display()));
unreachable!();
Some(sp) => panic!(sess.span_diagnostic.span_fatal(sp, &msg)),
None => sess.span_diagnostic.handler().fatal(&msg)
}
}
sess.codemap().new_filemap(path.to_str().unwrap().to_string(), contents)
}

/// Given a filemap, produce a sequence of token-trees
Expand Down

0 comments on commit 0d50b04

Please sign in to comment.