From f6d014f3f4c302a16a14a6afc036d31c8cd6605b Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 28 Jan 2019 14:23:41 -0800 Subject: [PATCH] Write tree_sitter/parser.h file in generate command --- cli/src/generate/mod.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/cli/src/generate/mod.rs b/cli/src/generate/mod.rs index 535f9d19..9e954298 100644 --- a/cli/src/generate/mod.rs +++ b/cli/src/generate/mod.rs @@ -40,7 +40,9 @@ pub fn generate_parser_in_directory( let (language_name, c_code) = generate_parser_for_grammar_with_opts(&grammar_json, minimize, state_ids_to_log)?; let repo_src_path = repo_path.join("src"); + let repo_header_path = repo_src_path.join("tree_sitter"); fs::create_dir_all(&repo_src_path)?; + fs::create_dir_all(&repo_header_path)?; fs::write(&repo_src_path.join("parser.c"), c_code) .map_err(|e| format!("Failed to write parser.c: {}", e))?; ensure_file(&repo_src_path.join("binding.cc"), || { @@ -52,6 +54,9 @@ pub fn generate_parser_in_directory( ensure_file(&repo_path.join("index.js"), || { npm_files::index_js(&language_name) })?; + ensure_file(&repo_header_path.join("parser.h"), || { + include_str!("../../../lib/include/tree_sitter/parser.h") + })?; } properties::generate_property_sheets(repo_path)?; Ok(()) @@ -96,7 +101,10 @@ fn load_grammar_file(grammar_path: &PathBuf) -> Result { match grammar_path.extension().and_then(|e| e.to_str()) { Some("js") => Ok(load_js_grammar_file(grammar_path)?), Some("json") => Ok(fs::read_to_string(grammar_path)?), - _ => Err(Error(format!("Unknown grammar file extension: {:?}", grammar_path))), + _ => Err(Error(format!( + "Unknown grammar file extension: {:?}", + grammar_path + ))), } } @@ -129,10 +137,10 @@ fn load_js_grammar_file(grammar_path: &PathBuf) -> Result { Ok(String::from_utf8(output.stdout).expect("Got invalid UTF8 from node")) } -fn ensure_file(path: &PathBuf, f: impl Fn() -> String) -> Result<()> { +fn ensure_file>(path: &PathBuf, f: impl Fn() -> T) -> Result<()> { if path.exists() { Ok(()) } else { - fs::write(path, f()).map_err(|e| Error(format!("Failed to write file {:?}: {}", path, e))) + fs::write(path, f().as_ref()).map_err(|e| Error(format!("Failed to write file {:?}: {}", path, e))) } }