Skipping the Lexer, the parser can find and replace the include statement as long as it is on a line by itself.
135 lines
2.6 KiB
Rust
135 lines
2.6 KiB
Rust
use std::fs::File;
|
|
use std::io::Write;
|
|
use std::path::Path;
|
|
|
|
use ::lexer::Lexer;
|
|
use ::parser::Parser;
|
|
use ::reader::Reader;
|
|
use ::util::Util;
|
|
|
|
|
|
|
|
const FILE_HEADER: &'static str =
|
|
"// This is a generated file from the Draconic compiler.\n\
|
|
//\n\
|
|
// Do NOT edit this file.\n";
|
|
|
|
|
|
|
|
///
|
|
pub struct Compiler
|
|
{
|
|
util: Util
|
|
}
|
|
|
|
|
|
|
|
/// Reads an input File and parses it.
|
|
pub fn read_file(util: &Util, input_path: &Path) -> String
|
|
{
|
|
let mut output: String;
|
|
let mut reader: Reader;
|
|
let mut lines: Vec<String>;
|
|
|
|
// Create the output string of the compiled file.
|
|
output = String::new();
|
|
|
|
// Create a Reader from the given input file.
|
|
reader = Reader::from_file(input_path);
|
|
|
|
Lexer::scan(&mut reader);
|
|
|
|
/*
|
|
// Parse the file and turn it into a set of compiled lines.
|
|
lines = Parser::parse(&util, &mut reader);
|
|
|
|
// Add all these lines to the final output.
|
|
for line in lines.into_iter()
|
|
{
|
|
output.push_str(&line);
|
|
}
|
|
*/
|
|
// Return the final output.
|
|
output
|
|
}
|
|
|
|
/// Writes an output File.
|
|
fn write_file(output_path: &Path, output: String)
|
|
{
|
|
// Delete the output file if it already exists.
|
|
if output_path.exists() == true
|
|
{
|
|
match ::std::fs::remove_file(output_path)
|
|
{
|
|
Ok(_) =>
|
|
{
|
|
}
|
|
|
|
Err(error) =>
|
|
{
|
|
warn!("{}", error);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Write the compiled String to a File.
|
|
match File::create(output_path)
|
|
{
|
|
Ok(mut file) =>
|
|
{
|
|
file.write_all(output.as_bytes());
|
|
}
|
|
|
|
Err(error) =>
|
|
{
|
|
error!("{}", error);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
impl Compiler
|
|
{
|
|
/// Create a new Compiler to use.
|
|
pub fn new() -> Compiler
|
|
{
|
|
Compiler
|
|
{
|
|
util: Util::new()
|
|
}
|
|
}
|
|
|
|
/// Register an include directory.
|
|
pub fn register_include_dir(&mut self, include_dir: &Path)
|
|
{
|
|
self.util.register_include_dir(include_dir);
|
|
}
|
|
|
|
/// Register a list of include directories.
|
|
pub fn register_include_dirs(&mut self, include_dirs: Vec<&Path>)
|
|
{
|
|
for dir in include_dirs.iter()
|
|
{
|
|
self.register_include_dir(dir);
|
|
}
|
|
}
|
|
|
|
/// Compile a given input file.
|
|
pub fn compile<F>(&self, input: F, output: F)
|
|
where F: AsRef<Path>
|
|
{
|
|
let mut output_string: String;
|
|
|
|
// Create the output string of the compiled file.
|
|
// Start it with the compilation header.
|
|
output_string = String::from(FILE_HEADER);
|
|
|
|
// Turn the input file into a compiled String.
|
|
output_string.push_str(&read_file(&self.util, input.as_ref()));
|
|
|
|
// Write the compiled output file.
|
|
write_file(output.as_ref(), output_string);
|
|
}
|
|
}
|