From 280c6605ff0d74d6de22adbfeab2f787109afc5c Mon Sep 17 00:00:00 2001 From: Jason Travis Smith Date: Sun, 31 Jul 2016 04:08:08 -0400 Subject: [PATCH] Added a Pass Through compiler. This just handles the include statement. --- Cargo.lock | 6 +++ Cargo.toml | 7 +++ src/compiler.rs | 2 +- src/lib.rs | 2 + src/pt_compiler.rs | 128 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 src/pt_compiler.rs diff --git a/Cargo.lock b/Cargo.lock index e00d68d..19d7665 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,6 +3,7 @@ name = "draconic" version = "0.1.0" dependencies = [ "scribe 0.1.0 (git+https://gitlab.com/CyberMages/scribe.git)", + "spellbook 0.1.0 (git+https://gitlab.com/CyberMages/spellbook.git)", ] [[package]] @@ -10,3 +11,8 @@ name = "scribe" version = "0.1.0" source = "git+https://gitlab.com/CyberMages/scribe.git#c655eca358577795d818bdd07cb71864c0c9b9f2" +[[package]] +name = "spellbook" +version = "0.1.0" +source = "git+https://gitlab.com/CyberMages/spellbook.git#0059c4a676ef2243d982ec1ad471a03fc5c79e01" + diff --git a/Cargo.toml b/Cargo.toml index 7350f86..4f8d40c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,13 @@ repository = "https://gitlab.com/CyberMages/draconic.git" documentation = "" keywords = ["draconic", "parser", "interpreter", "compiler"] +[[bin]] +name = "draconicpt" +path = "src/pt_compiler.rs" + [dependencies.scribe] git = "https://gitlab.com/CyberMages/scribe.git" + +[dependencies.spellbook] +git = "https://gitlab.com/CyberMages/spellbook.git" diff --git a/src/compiler.rs b/src/compiler.rs index 6ffcfc7..facec93 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -136,7 +136,7 @@ impl Compiler // Create the output string of the compiled file. // Start it with the compilation header. - output_string = String::from(FILE_HEADER); + output_string = String::new();//from(FILE_HEADER); // Turn the input file into a compiled String. output_string.push_str(&read_file(&self.util, input.as_ref())); diff --git a/src/lib.rs b/src/lib.rs index f300024..3b19017 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,8 @@ #[macro_use] extern crate scribe; +extern crate spellbook; + mod compiler; diff --git a/src/pt_compiler.rs b/src/pt_compiler.rs new file mode 100644 index 0000000..4dcdbba --- /dev/null +++ b/src/pt_compiler.rs @@ -0,0 +1,128 @@ +#[macro_use] +extern crate scribe; + +extern crate spellbook; + + + +mod compiler; +mod lexer; +mod parser; +mod reader; +mod util; + + + +use std::path::PathBuf; + +use spellbook::{EMPTY_STRING, ArgParser, ArgOption}; + +use ::compiler::Compiler; + + + +const PROGRAM_NAME: &'static str = "Draconic Pass Through Compiler"; +const PROGRAM_COMMAND_NAME: &'static str = "draconicpt"; +const PROGRAM_VERSION: &'static str = "0.1.0"; +const PROGRAM_SUMMARY: &'static str = + "Compiles a Draconic template to a file."; +const PROGRAM_DESCRIPTION: &'static str = + "The Draconic HTML Compiler compiles a Draconic template to a file."; + + + +/// +fn configure_arguments(parser: &mut ArgParser) +{ + let mut option: ArgOption; + + option = ArgOption::new(); + option.set_name("version"); + option.set_short_name('v'); + option.set_description("Print the programs version information."); + option.set_optional(true); + parser.add_option(option); + + option = ArgOption::new(); + option.set_name("input"); + option.set_short_name('i'); + option.set_description("The file to use as input."); + option.set_arg_description("FILENAME"); + parser.add_option(option); + + option = ArgOption::new(); + option.set_name("output"); + option.set_short_name('o'); + option.set_description("The file to use as output."); + option.set_arg_description("FILENAME"); + parser.add_option(option); + + option = ArgOption::new(); + option.set_name("include_dir"); + option.set_short_name('I'); + option.set_description("A directory to search when looking for \ + included files."); + option.set_arg_description("DIRECTORY_PATH"); + option.set_optional(true); + parser.add_option(option); +} + +/// +fn print_version() +{ + println!("Program name: {}", PROGRAM_NAME); + println!("Version: {}", PROGRAM_VERSION); + println!("{} {} {} {}", "\u{00A9}", "2016", + "CyberMages LLC", "All Rights Reserved"); +} + +/// +pub fn main() +{ + let version: bool; + let input: String; + let output: String; + let include_dir: String; + let mut parser: ArgParser; + let mut compiler: Compiler; + + // Create a new argument parser. + parser = ArgParser::new(); + parser.set_name(PROGRAM_COMMAND_NAME); + parser.set_summary(PROGRAM_SUMMARY); + parser.set_description(PROGRAM_DESCRIPTION); + + // Configure the arguments that this program takes. + configure_arguments(&mut parser); + + // Parse the arguments to this program. + parser.parse(); + + // Get the argument values. + version = parser.get_option_value_as("version", false); + input = parser.get_option_value_as("input", String::new()); + output = parser.get_option_value_as("output", String::new()); + include_dir = parser.get_option_value_as("include_dir", String::new()); + + // If the version is desired then print it instead of running the program. + if version == true + { + print_version(); + } + else + { + // Compile to an HTML file. + // Create a new compiler. + compiler = Compiler::new(); + + // Register the include directory if there is one. + if include_dir != EMPTY_STRING + { + compiler.register_include_dir(&PathBuf::from(include_dir)); + } + + // Compile the file. + println!("Compiling {} into {}.", input, output); + compiler.compile(PathBuf::from(input), PathBuf::from(output)); + } +}