Add a cton-util filecheck sub-command.

This commit is contained in:
Jakob Stoklund Olesen
2016-09-08 16:20:06 -07:00
parent 5b22634c8d
commit c95c23dbbe
5 changed files with 60 additions and 3 deletions

8
src/tools/Cargo.lock generated
View File

@@ -5,6 +5,7 @@ dependencies = [
"cretonne 0.0.0",
"cretonne-reader 0.0.0",
"docopt 0.6.80 (registry+https://github.com/rust-lang/crates.io-index)",
"filecheck 0.0.0",
"glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.1.71 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -42,6 +43,13 @@ dependencies = [
"strsim 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "filecheck"
version = "0.0.0"
dependencies = [
"regex 0.1.71 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "glob"
version = "0.2.11"

View File

@@ -12,6 +12,7 @@ path = "main.rs"
[dependencies]
cretonne = { path = "../libcretonne" }
cretonne-reader = { path = "../libreader" }
filecheck = { path = "../libfilecheck" }
docopt = "0.6.80"
rustc-serialize = "0.3.19"
regex = "0.1.71"

View File

@@ -3,6 +3,7 @@ extern crate cretonne;
extern crate cton_reader;
extern crate docopt;
extern crate rustc_serialize;
extern crate filecheck;
use cretonne::VERSION;
use docopt::Docopt;
@@ -12,26 +13,31 @@ use std::process;
mod cat;
mod print_cfg;
mod rsfilecheck;
const USAGE: &'static str = "
Cretonne code generator utility
Usage:
cton-util cat <file>...
cton-util filecheck [-v] <file>
cton-util print-cfg <file>...
cton-util --help | --version
Options:
-h, --help print this help message
--version print the Cretonne version
-v, --verbose be more verbose
-h, --help print this help message
--version print the Cretonne version
";
#[derive(RustcDecodable, Debug)]
struct Args {
cmd_cat: bool,
cmd_filecheck: bool,
cmd_print_cfg: bool,
arg_file: Vec<String>,
flag_verbose: bool,
}
/// A command either succeeds or fails with an error message.
@@ -51,6 +57,8 @@ fn cton_util() -> CommandResult {
// Find the sub-command to execute.
if args.cmd_cat {
cat::run(args.arg_file)
} else if args.cmd_filecheck {
rsfilecheck::run(args.arg_file, args.flag_verbose)
} else if args.cmd_print_cfg {
print_cfg::run(args.arg_file)
} else {

40
src/tools/rsfilecheck.rs Normal file
View File

@@ -0,0 +1,40 @@
use CommandResult;
use filecheck::{CheckerBuilder, Checker, NO_VARIABLES};
use std::fs::File;
use std::io::{self, Read};
pub fn run(files: Vec<String>, verbose: bool) -> CommandResult {
if files.is_empty() {
return Err("No check files".to_string());
}
let checker = try!(read_checkfile(&files[0]));
if checker.is_empty() {
return Err(format!("{}: no filecheck directives found", files[0]));
}
// Print out the directives under --verbose.
if verbose {
println!("{}", checker);
}
let mut buffer = String::new();
try!(io::stdin().read_to_string(&mut buffer).map_err(|e| format!("stdin: {}", e)));
if try!(checker.check(&buffer, NO_VARIABLES).map_err(|e| e.to_string())) {
Ok(())
} else {
// TODO: We need to do better than this.
Err("Check failed".to_string())
}
}
fn read_checkfile(filename: &str) -> Result<Checker, String> {
let mut file = try!(File::open(&filename).map_err(|e| format!("{}: {}", filename, e)));
let mut buffer = String::new();
try!(file.read_to_string(&mut buffer)
.map_err(|e| format!("Couldn't read {}: {}", filename, e)));
let mut builder = CheckerBuilder::new();
try!(builder.text(&buffer).map_err(|e| format!("{}: {}", filename, e)));
Ok(builder.finish())
}

View File

@@ -40,7 +40,7 @@ else
echo "If a newer version of rustfmt is available, update this script."
fi
PKGS="cretonne cretonne-reader cretonne-tools"
PKGS="cretonne cretonne-reader cretonne-tools filecheck"
cd "$topdir/src/tools"
for PKG in $PKGS
do