diff options
| author | Eduard Burtescu <edy.burt@gmail.com> | 2015-05-14 01:44:57 +0300 |
|---|---|---|
| committer | Eduard Burtescu <edy.burt@gmail.com> | 2015-05-14 01:47:56 +0300 |
| commit | 07d4f777905a1faedaf965dac4dc37489bc625c0 (patch) | |
| tree | f398dea94809078c78eabc4c3142ff355e3d729b /src/libsyntax/codemap.rs | |
| parent | 0d50b043f748bf9a07b28a8f734327258204e26b (diff) | |
| download | rust-07d4f777905a1faedaf965dac4dc37489bc625c0.tar.gz rust-07d4f777905a1faedaf965dac4dc37489bc625c0.zip | |
syntax: abstract over the file loading mechanism.
Diffstat (limited to 'src/libsyntax/codemap.rs')
| -rw-r--r-- | src/libsyntax/codemap.rs | 48 |
1 files changed, 46 insertions, 2 deletions
diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index c692babfacc..b2a366ec5be 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -21,9 +21,11 @@ pub use self::MacroFormat::*; use std::cell::RefCell; use std::ops::{Add, Sub}; +use std::path::Path; use std::rc::Rc; -use std::fmt; +use std::{fmt, fs}; +use std::io::{self, Read}; use serialize::{Encodable, Decodable, Encoder, Decoder}; @@ -527,6 +529,29 @@ impl FileMap { } } +/// An abstraction over the fs operations used by the Parser. +pub trait FileLoader { + /// Query the existence of a file. + fn file_exists(&self, path: &Path) -> bool; + + /// Read the contents of an UTF-8 file into memory. + fn read_file(&self, path: &Path) -> io::Result<String>; +} + +/// A FileLoader that uses std::fs to load real files. +pub struct RealFileLoader; + +impl FileLoader for RealFileLoader { + fn file_exists(&self, path: &Path) -> bool { + fs::metadata(path).is_ok() + } + + fn read_file(&self, path: &Path) -> io::Result<String> { + let mut src = String::new(); + try!(try!(fs::File::open(path)).read_to_string(&mut src)); + Ok(src) + } +} // _____________________________________________________________________________ // CodeMap @@ -534,7 +559,8 @@ impl FileMap { pub struct CodeMap { pub files: RefCell<Vec<Rc<FileMap>>>, - expansions: RefCell<Vec<ExpnInfo>> + expansions: RefCell<Vec<ExpnInfo>>, + file_loader: Box<FileLoader> } impl CodeMap { @@ -542,9 +568,27 @@ impl CodeMap { CodeMap { files: RefCell::new(Vec::new()), expansions: RefCell::new(Vec::new()), + file_loader: Box::new(RealFileLoader) + } + } + + pub fn with_file_loader(file_loader: Box<FileLoader>) -> CodeMap { + CodeMap { + files: RefCell::new(Vec::new()), + expansions: RefCell::new(Vec::new()), + file_loader: file_loader } } + pub fn file_exists(&self, path: &Path) -> bool { + self.file_loader.file_exists(path) + } + + pub fn load_file(&self, path: &Path) -> io::Result<Rc<FileMap>> { + let src = try!(self.file_loader.read_file(path)); + Ok(self.new_filemap(path.to_str().unwrap().to_string(), src)) + } + pub fn new_filemap(&self, filename: FileName, mut src: String) -> Rc<FileMap> { let mut files = self.files.borrow_mut(); let start_pos = match files.last() { |
