mirror of
https://github.com/zaphar/ucg.git
synced 2025-07-26 19:00:28 -04:00
FEATURE: Use a command line flag to turn off strict mode.
This commit is contained in:
parent
aa183960d3
commit
17a721a600
82
src/main.rs
82
src/main.rs
@ -36,6 +36,7 @@ fn do_flags<'a, 'b>() -> clap::App<'a, 'b> {
|
|||||||
(version: crate_version!())
|
(version: crate_version!())
|
||||||
(author: crate_authors!())
|
(author: crate_authors!())
|
||||||
(about: "Universal Configuration Grammar compiler.")
|
(about: "Universal Configuration Grammar compiler.")
|
||||||
|
(@arg nostrict: --("no-strict") "Turn off strict checking.")
|
||||||
(@subcommand inspect =>
|
(@subcommand inspect =>
|
||||||
(about: "Inspect a specific symbol in a ucg file.")
|
(about: "Inspect a specific symbol in a ucg file.")
|
||||||
(@arg sym: --sym +takes_value +required "Specify a specific binding in the ucg file to output.")
|
(@arg sym: --sym +takes_value +required "Specify a specific binding in the ucg file to output.")
|
||||||
@ -74,6 +75,7 @@ fn run_converter(c: &traits::Converter, v: Rc<Val>, f: Option<&str>) -> traits::
|
|||||||
fn build_file(
|
fn build_file(
|
||||||
file: &str,
|
file: &str,
|
||||||
validate: bool,
|
validate: bool,
|
||||||
|
strict: bool,
|
||||||
cache: Rc<RefCell<Cache>>,
|
cache: Rc<RefCell<Cache>>,
|
||||||
) -> Result<build::Builder, Box<Error>> {
|
) -> Result<build::Builder, Box<Error>> {
|
||||||
let mut root = PathBuf::from(file);
|
let mut root = PathBuf::from(file);
|
||||||
@ -81,6 +83,7 @@ fn build_file(
|
|||||||
root = std::env::current_dir().unwrap().join(root);
|
root = std::env::current_dir().unwrap().join(root);
|
||||||
}
|
}
|
||||||
let mut builder = build::Builder::new(root.parent().unwrap(), cache);
|
let mut builder = build::Builder::new(root.parent().unwrap(), cache);
|
||||||
|
builder.set_strict(strict);
|
||||||
if validate {
|
if validate {
|
||||||
builder.enable_validate_mode();
|
builder.enable_validate_mode();
|
||||||
}
|
}
|
||||||
@ -91,9 +94,9 @@ fn build_file(
|
|||||||
Ok(builder)
|
Ok(builder)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn do_validate(file: &str, cache: Rc<RefCell<Cache>>) -> bool {
|
fn do_validate(file: &str, strict: bool, cache: Rc<RefCell<Cache>>) -> bool {
|
||||||
println!("Validating {}", file);
|
println!("Validating {}", file);
|
||||||
match build_file(file, true, cache) {
|
match build_file(file, true, strict, cache) {
|
||||||
Ok(b) => {
|
Ok(b) => {
|
||||||
if b.assert_collector.success {
|
if b.assert_collector.success {
|
||||||
println!("File {} Pass\n", file);
|
println!("File {} Pass\n", file);
|
||||||
@ -110,9 +113,14 @@ fn do_validate(file: &str, cache: Rc<RefCell<Cache>>) -> bool {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn do_compile(file: &str, cache: Rc<RefCell<Cache>>, registry: &ConverterRegistry) -> bool {
|
fn do_compile(
|
||||||
|
file: &str,
|
||||||
|
strict: bool,
|
||||||
|
cache: Rc<RefCell<Cache>>,
|
||||||
|
registry: &ConverterRegistry,
|
||||||
|
) -> bool {
|
||||||
println!("Building {}", file);
|
println!("Building {}", file);
|
||||||
let builder = match build_file(file, false, cache.clone()) {
|
let builder = match build_file(file, false, strict, cache.clone()) {
|
||||||
Ok(builder) => builder,
|
Ok(builder) => builder,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("{}", err);
|
eprintln!("{}", err);
|
||||||
@ -143,6 +151,7 @@ fn visit_ucg_files(
|
|||||||
path: &Path,
|
path: &Path,
|
||||||
recurse: bool,
|
recurse: bool,
|
||||||
validate: bool,
|
validate: bool,
|
||||||
|
strict: bool,
|
||||||
cache: Rc<RefCell<Cache>>,
|
cache: Rc<RefCell<Cache>>,
|
||||||
registry: &ConverterRegistry,
|
registry: &ConverterRegistry,
|
||||||
) -> Result<bool, Box<Error>> {
|
) -> Result<bool, Box<Error>> {
|
||||||
@ -163,36 +172,41 @@ fn visit_ucg_files(
|
|||||||
let next_path = next_item.path();
|
let next_path = next_item.path();
|
||||||
let path_as_string = String::from(next_path.to_string_lossy());
|
let path_as_string = String::from(next_path.to_string_lossy());
|
||||||
if next_path.is_dir() && recurse {
|
if next_path.is_dir() && recurse {
|
||||||
if let Err(e) =
|
if let Err(e) = visit_ucg_files(
|
||||||
visit_ucg_files(&next_path, recurse, validate, cache.clone(), registry)
|
&next_path,
|
||||||
{
|
recurse,
|
||||||
|
validate,
|
||||||
|
strict,
|
||||||
|
cache.clone(),
|
||||||
|
registry,
|
||||||
|
) {
|
||||||
eprintln!("{}", e);
|
eprintln!("{}", e);
|
||||||
result = false;
|
result = false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if validate && path_as_string.ends_with("_test.ucg") {
|
if validate && path_as_string.ends_with("_test.ucg") {
|
||||||
if !do_validate(&path_as_string, cache.clone()) {
|
if !do_validate(&path_as_string, strict, cache.clone()) {
|
||||||
result = false;
|
result = false;
|
||||||
summary.push_str(format!("{} - FAIL\n", path_as_string).as_str())
|
summary.push_str(format!("{} - FAIL\n", path_as_string).as_str())
|
||||||
} else {
|
} else {
|
||||||
summary.push_str(format!("{} - PASS\n", path_as_string).as_str())
|
summary.push_str(format!("{} - PASS\n", path_as_string).as_str())
|
||||||
}
|
}
|
||||||
} else if !validate && path_as_string.ends_with(".ucg") {
|
} else if !validate && path_as_string.ends_with(".ucg") {
|
||||||
if !do_compile(&path_as_string, cache.clone(), registry) {
|
if !do_compile(&path_as_string, strict, cache.clone(), registry) {
|
||||||
result = false;
|
result = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if validate && our_path.ends_with("_test.ucg") {
|
} else if validate && our_path.ends_with("_test.ucg") {
|
||||||
if !do_validate(&our_path, cache) {
|
if !do_validate(&our_path, strict, cache) {
|
||||||
result = false;
|
result = false;
|
||||||
summary.push_str(format!("{} - FAIL\n", our_path).as_str());
|
summary.push_str(format!("{} - FAIL\n", our_path).as_str());
|
||||||
} else {
|
} else {
|
||||||
summary.push_str(format!("{} - PASS\n", &our_path).as_str());
|
summary.push_str(format!("{} - PASS\n", &our_path).as_str());
|
||||||
}
|
}
|
||||||
} else if !validate {
|
} else if !validate {
|
||||||
if !do_compile(&our_path, cache, registry) {
|
if !do_compile(&our_path, strict, cache, registry) {
|
||||||
result = false;
|
result = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -207,12 +221,14 @@ fn inspect_command(
|
|||||||
matches: &clap::ArgMatches,
|
matches: &clap::ArgMatches,
|
||||||
cache: Rc<RefCell<Cache>>,
|
cache: Rc<RefCell<Cache>>,
|
||||||
registry: &ConverterRegistry,
|
registry: &ConverterRegistry,
|
||||||
|
strict: bool,
|
||||||
) {
|
) {
|
||||||
let file = matches.value_of("INPUT").unwrap();
|
let file = matches.value_of("INPUT").unwrap();
|
||||||
let sym = matches.value_of("sym");
|
let sym = matches.value_of("sym");
|
||||||
let target = matches.value_of("target").unwrap();
|
let target = matches.value_of("target").unwrap();
|
||||||
let root = PathBuf::from(file);
|
let root = PathBuf::from(file);
|
||||||
let mut builder = build::Builder::new(root.parent().unwrap(), cache);
|
let mut builder = build::Builder::new(root.parent().unwrap(), cache);
|
||||||
|
builder.set_strict(strict);
|
||||||
match registry.get_converter(target) {
|
match registry.get_converter(target) {
|
||||||
Some(converter) => {
|
Some(converter) => {
|
||||||
// TODO(jwall): We should warn if this is a test file.
|
// TODO(jwall): We should warn if this is a test file.
|
||||||
@ -249,13 +265,21 @@ fn build_command(
|
|||||||
matches: &clap::ArgMatches,
|
matches: &clap::ArgMatches,
|
||||||
cache: Rc<RefCell<Cache>>,
|
cache: Rc<RefCell<Cache>>,
|
||||||
registry: &ConverterRegistry,
|
registry: &ConverterRegistry,
|
||||||
|
strict: bool,
|
||||||
) {
|
) {
|
||||||
let files = matches.values_of("INPUT");
|
let files = matches.values_of("INPUT");
|
||||||
let recurse = matches.is_present("recurse");
|
let recurse = matches.is_present("recurse");
|
||||||
let mut ok = true;
|
let mut ok = true;
|
||||||
if files.is_none() {
|
if files.is_none() {
|
||||||
let curr_dir = std::env::current_dir().unwrap();
|
let curr_dir = std::env::current_dir().unwrap();
|
||||||
let ok = visit_ucg_files(curr_dir.as_path(), recurse, false, cache.clone(), ®istry);
|
let ok = visit_ucg_files(
|
||||||
|
curr_dir.as_path(),
|
||||||
|
recurse,
|
||||||
|
false,
|
||||||
|
strict,
|
||||||
|
cache.clone(),
|
||||||
|
®istry,
|
||||||
|
);
|
||||||
if let Ok(false) = ok {
|
if let Ok(false) = ok {
|
||||||
process::exit(1)
|
process::exit(1)
|
||||||
}
|
}
|
||||||
@ -263,7 +287,7 @@ fn build_command(
|
|||||||
}
|
}
|
||||||
for file in files.unwrap() {
|
for file in files.unwrap() {
|
||||||
let pb = PathBuf::from(file);
|
let pb = PathBuf::from(file);
|
||||||
if let Ok(false) = visit_ucg_files(&pb, recurse, false, cache.clone(), ®istry) {
|
if let Ok(false) = visit_ucg_files(&pb, recurse, false, strict, cache.clone(), ®istry) {
|
||||||
ok = false;
|
ok = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -276,12 +300,20 @@ fn test_command(
|
|||||||
matches: &clap::ArgMatches,
|
matches: &clap::ArgMatches,
|
||||||
cache: Rc<RefCell<Cache>>,
|
cache: Rc<RefCell<Cache>>,
|
||||||
registry: &ConverterRegistry,
|
registry: &ConverterRegistry,
|
||||||
|
strict: bool,
|
||||||
) {
|
) {
|
||||||
let files = matches.values_of("INPUT");
|
let files = matches.values_of("INPUT");
|
||||||
let recurse = matches.is_present("recurse");
|
let recurse = matches.is_present("recurse");
|
||||||
if files.is_none() {
|
if files.is_none() {
|
||||||
let curr_dir = std::env::current_dir().unwrap();
|
let curr_dir = std::env::current_dir().unwrap();
|
||||||
let ok = visit_ucg_files(curr_dir.as_path(), recurse, true, cache.clone(), ®istry);
|
let ok = visit_ucg_files(
|
||||||
|
curr_dir.as_path(),
|
||||||
|
recurse,
|
||||||
|
true,
|
||||||
|
strict,
|
||||||
|
cache.clone(),
|
||||||
|
®istry,
|
||||||
|
);
|
||||||
if let Ok(false) = ok {
|
if let Ok(false) = ok {
|
||||||
process::exit(1)
|
process::exit(1)
|
||||||
}
|
}
|
||||||
@ -290,9 +322,14 @@ fn test_command(
|
|||||||
for file in files.unwrap() {
|
for file in files.unwrap() {
|
||||||
let pb = PathBuf::from(file);
|
let pb = PathBuf::from(file);
|
||||||
//if pb.is_dir() {
|
//if pb.is_dir() {
|
||||||
if let Ok(false) =
|
if let Ok(false) = visit_ucg_files(
|
||||||
visit_ucg_files(pb.as_path(), recurse, true, cache.clone(), ®istry)
|
pb.as_path(),
|
||||||
{
|
recurse,
|
||||||
|
true,
|
||||||
|
strict,
|
||||||
|
cache.clone(),
|
||||||
|
®istry,
|
||||||
|
) {
|
||||||
ok = false;
|
ok = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -319,12 +356,17 @@ fn main() {
|
|||||||
let app_matches = app.clone().get_matches();
|
let app_matches = app.clone().get_matches();
|
||||||
let cache: Rc<RefCell<Cache>> = Rc::new(RefCell::new(MemoryCache::new()));
|
let cache: Rc<RefCell<Cache>> = Rc::new(RefCell::new(MemoryCache::new()));
|
||||||
let registry = ConverterRegistry::make_registry();
|
let registry = ConverterRegistry::make_registry();
|
||||||
|
let strict = if app_matches.is_present("nostrict") {
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
true
|
||||||
|
};
|
||||||
if let Some(matches) = app_matches.subcommand_matches("inspect") {
|
if let Some(matches) = app_matches.subcommand_matches("inspect") {
|
||||||
inspect_command(matches, cache, ®istry);
|
inspect_command(matches, cache, ®istry, strict);
|
||||||
} else if let Some(matches) = app_matches.subcommand_matches("build") {
|
} else if let Some(matches) = app_matches.subcommand_matches("build") {
|
||||||
build_command(matches, cache, ®istry);
|
build_command(matches, cache, ®istry, strict);
|
||||||
} else if let Some(matches) = app_matches.subcommand_matches("test") {
|
} else if let Some(matches) = app_matches.subcommand_matches("test") {
|
||||||
test_command(matches, cache, ®istry);
|
test_command(matches, cache, ®istry, strict);
|
||||||
} else if let Some(_) = app_matches.subcommand_matches("converters") {
|
} else if let Some(_) = app_matches.subcommand_matches("converters") {
|
||||||
converters_command(®istry)
|
converters_command(®istry)
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user