From def5884b59495fbe3ff199f199eee58731f5398e Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 14 Jan 2019 14:07:42 -0800 Subject: [PATCH] Allow passing grammar JS or JSON path to `generate` command --- cli/src/generate/mod.rs | 12 +++++++++++- cli/src/generate/properties.rs | 22 ++++++++++++---------- cli/src/main.rs | 3 +++ 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/cli/src/generate/mod.rs b/cli/src/generate/mod.rs index 283ab0b2..1593c0da 100644 --- a/cli/src/generate/mod.rs +++ b/cli/src/generate/mod.rs @@ -28,12 +28,14 @@ lazy_static! { pub fn generate_parser_in_directory( repo_path: &PathBuf, + grammar_path: Option<&str>, minimize: bool, state_ids_to_log: Vec, properties_only: bool, ) -> Result<()> { if !properties_only { - let grammar_json = load_js_grammar_file(&repo_path.join("grammar.js")); + let grammar_path = grammar_path.map_or(repo_path.join("grammar.js"), |s| s.into()); + let grammar_json = load_grammar_file(&grammar_path); let c_code = generate_parser_for_grammar_with_opts(&grammar_json, minimize, state_ids_to_log)?; fs::create_dir_all("src")?; @@ -77,6 +79,14 @@ fn generate_parser_for_grammar_with_opts( )) } +fn load_grammar_file(grammar_path: &PathBuf) -> String { + match grammar_path.extension().and_then(|e| e.to_str()) { + Some("js") => load_js_grammar_file(grammar_path), + Some("json") => fs::read_to_string(grammar_path).expect("Failed to read grammar file"), + _ => panic!("Unknown grammar file extension"), + } +} + fn load_js_grammar_file(grammar_path: &PathBuf) -> String { let mut node_process = Command::new("node") .stdin(Stdio::piped()) diff --git a/cli/src/generate/properties.rs b/cli/src/generate/properties.rs index cca7fef8..e1492d6f 100644 --- a/cli/src/generate/properties.rs +++ b/cli/src/generate/properties.rs @@ -424,16 +424,18 @@ pub fn generate_property_sheets(repo_path: &Path) -> Result<()> { let src_dir_path = repo_path.join("src"); let properties_dir_path = repo_path.join("properties"); - for entry in fs::read_dir(properties_dir_path)? { - let css_path = entry?.path(); - let css = fs::read_to_string(&css_path)?; - let sheet = generate_property_sheet(&css_path, &css)?; - let property_sheet_json_path = src_dir_path - .join(css_path.file_name().unwrap()) - .with_extension("json"); - let property_sheet_json_file = File::create(property_sheet_json_path)?; - let mut writer = BufWriter::new(property_sheet_json_file); - serde_json::to_writer_pretty(&mut writer, &sheet)?; + if let Ok(entries) = fs::read_dir(properties_dir_path) { + for entry in entries { + let css_path = entry?.path(); + let css = fs::read_to_string(&css_path)?; + let sheet = generate_property_sheet(&css_path, &css)?; + let property_sheet_json_path = src_dir_path + .join(css_path.file_name().unwrap()) + .with_extension("json"); + let property_sheet_json_file = File::create(property_sheet_json_path)?; + let mut writer = BufWriter::new(property_sheet_json_file); + serde_json::to_writer_pretty(&mut writer, &sheet)?; + } } Ok(()) diff --git a/cli/src/main.rs b/cli/src/main.rs index 5a830458..80a40758 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -43,6 +43,7 @@ fn run() -> error::Result<()> { .subcommand( SubCommand::with_name("generate") .about("Generate a parser") + .arg(Arg::with_name("grammar-path").index(1)) .arg(Arg::with_name("log").long("log")) .arg(Arg::with_name("properties-only").long("properties")) .arg( @@ -84,6 +85,7 @@ fn run() -> error::Result<()> { logger::init(); } + let grammar_path = matches.value_of("grammar-path"); let minimize = !matches.is_present("no-minimize"); let properties_only = matches.is_present("properties-only"); let state_ids_to_log = matches @@ -94,6 +96,7 @@ fn run() -> error::Result<()> { }); generate::generate_parser_in_directory( ¤t_dir, + grammar_path, minimize, state_ids_to_log, properties_only,