Allow passing grammar JS or JSON path to generate command
This commit is contained in:
parent
8f48240bf1
commit
def5884b59
3 changed files with 26 additions and 11 deletions
|
|
@ -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())
|
||||
|
|
|
|||
|
|
@ -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(())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue