Allow passing grammar JS or JSON path to generate command

This commit is contained in:
Max Brunsfeld 2019-01-14 14:07:42 -08:00
parent 8f48240bf1
commit def5884b59
3 changed files with 26 additions and 11 deletions

View file

@ -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<usize>,
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())

View file

@ -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(())

View file

@ -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(
&current_dir,
grammar_path,
minimize,
state_ids_to_log,
properties_only,