2016-07-16 17:07:47 -04:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
use super::lexer::Lexer;
|
|
|
|
use super::reader::Reader;
|
|
|
|
|
|
|
|
|
|
|
|
const FILE_HEADER: &'static str =
|
|
|
|
"// This is a generated file from the Draconic compiler.\n\
|
|
|
|
//\n\
|
|
|
|
// Do NOT edit this file.\n";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///
|
2016-07-15 15:40:16 -04:00
|
|
|
pub enum Compiler
|
|
|
|
{
|
|
|
|
}
|
2016-07-16 17:07:47 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Reads an input File and parses it.
|
|
|
|
fn read_file(input_path: &Path) -> String
|
|
|
|
{
|
|
|
|
let mut output: String;
|
|
|
|
let mut reader: Reader;
|
|
|
|
|
|
|
|
output = String::from(FILE_HEADER);
|
|
|
|
|
|
|
|
// Create a Reader from the given input file.
|
|
|
|
reader = Reader::from_file(input_path);
|
|
|
|
|
|
|
|
// Use the Lexer to scan the Reader's
|
|
|
|
// buffer into tokens.
|
|
|
|
match Lexer::scan(reader)
|
|
|
|
{
|
|
|
|
Ok(tokens) =>
|
|
|
|
{
|
|
|
|
for token in tokens
|
|
|
|
{
|
|
|
|
output.push_str(&token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(error) =>
|
|
|
|
{
|
|
|
|
error!("{}", error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
/// Compile
|
|
|
|
pub fn compile<F>(input: F, output: F)
|
|
|
|
where F: AsRef<Path>
|
|
|
|
{
|
|
|
|
let output_string: String;
|
|
|
|
|
|
|
|
// Turn the input file into a compiled String.
|
|
|
|
output_string = read_file(input.as_ref());
|
|
|
|
|
|
|
|
// Write the compiled output file.
|
|
|
|
write_file(output.as_ref(), output_string);
|
|
|
|
}
|
|
|
|
}
|