From 61b70943b1caef016ae986c9169dd46dce78783e Mon Sep 17 00:00:00 2001 From: Jake Sarjeant Date: Tue, 18 Jul 2023 13:24:52 +0200 Subject: [PATCH] feat(cli): add option to select JS runtime other than node --- cli/src/generate/mod.rs | 23 +++++++++++++---------- cli/src/main.rs | 10 ++++++++++ cli/src/tests/corpus_test.rs | 2 +- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/cli/src/generate/mod.rs b/cli/src/generate/mod.rs index 4e1cac63..3c0aeda1 100644 --- a/cli/src/generate/mod.rs +++ b/cli/src/generate/mod.rs @@ -21,10 +21,10 @@ use anyhow::{anyhow, Context, Result}; use lazy_static::lazy_static; use regex::{Regex, RegexBuilder}; use semver::Version; -use std::fs; use std::io::Write; use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; +use std::{env, fs}; lazy_static! { static ref JSON_COMMENT_REGEX: Regex = RegexBuilder::new("^\\s*//.*") @@ -44,16 +44,17 @@ pub fn generate_parser_in_directory( abi_version: usize, generate_bindings: bool, report_symbol_name: Option<&str>, + js_runtime: Option<&str>, ) -> Result<()> { let src_path = repo_path.join("src"); let header_path = src_path.join("tree_sitter"); // Read the grammar.json. let grammar_json = match grammar_path { - Some(path) => load_grammar_file(path.as_ref())?, + Some(path) => load_grammar_file(path.as_ref(), js_runtime)?, None => { let grammar_js_path = grammar_path.map_or(repo_path.join("grammar.js"), |s| s.into()); - load_grammar_file(&grammar_js_path)? + load_grammar_file(&grammar_js_path, js_runtime)? } }; @@ -156,16 +157,15 @@ fn generate_parser_for_grammar_with_opts( }) } -pub fn load_grammar_file(grammar_path: &Path) -> Result { +pub fn load_grammar_file(grammar_path: &Path, js_runtime: Option<&str>) -> Result { if grammar_path.is_dir() { return Err(anyhow!( "Path to a grammar file with `.js` or `.json` extension is required" )); } match grammar_path.extension().and_then(|e| e.to_str()) { - Some("js") => { - Ok(load_js_grammar_file(grammar_path).with_context(|| "Failed to load grammar.js")?) - } + Some("js") => Ok(load_js_grammar_file(grammar_path, js_runtime) + .with_context(|| "Failed to load grammar.js")?), Some("json") => { Ok(fs::read_to_string(grammar_path).with_context(|| "Failed to load grammar.json")?) } @@ -176,14 +176,17 @@ pub fn load_grammar_file(grammar_path: &Path) -> Result { } } -fn load_js_grammar_file(grammar_path: &Path) -> Result { +fn load_js_grammar_file(grammar_path: &Path, js_runtime: Option<&str>) -> Result { let grammar_path = fs::canonicalize(grammar_path)?; - let mut node_process = Command::new("node") + + let js_runtime = js_runtime.unwrap_or("node"); + + let mut node_process = Command::new(js_runtime) .env("TREE_SITTER_GRAMMAR_PATH", grammar_path) .stdin(Stdio::piped()) .stdout(Stdio::piped()) .spawn() - .with_context(|| "Failed to run `node`")?; + .with_context(|| format!("Failed to run `{js_runtime}`"))?; let mut node_stdin = node_process .stdin diff --git a/cli/src/main.rs b/cli/src/main.rs index fe250e68..f66864ff 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -126,6 +126,14 @@ fn run() -> Result<()> { .long("report-states-for-rule") .value_name("rule-name") .takes_value(true), + ) + .arg( + Arg::with_name("js-runtime") + .long("js-runtime") + .takes_value(true) + .value_name("executable") + .env("TREE_SITTER_JS_RUNTIME") + .help("Use a JavaScript runtime other than node"), ), ) .subcommand( @@ -307,6 +315,7 @@ fn run() -> Result<()> { let debug_build = matches.is_present("debug-build"); let build = matches.is_present("build"); let libdir = matches.value_of("libdir"); + let js_runtime = matches.value_of("js-runtime"); let report_symbol_name = matches.value_of("report-states-for-rule").or_else(|| { if matches.is_present("report-states") { Some("") @@ -336,6 +345,7 @@ fn run() -> Result<()> { abi_version, generate_bindings, report_symbol_name, + js_runtime, )?; if build { if let Some(path) = libdir { diff --git a/cli/src/tests/corpus_test.rs b/cli/src/tests/corpus_test.rs index 24e72398..a193c029 100644 --- a/cli/src/tests/corpus_test.rs +++ b/cli/src/tests/corpus_test.rs @@ -260,7 +260,7 @@ fn test_feature_corpus_files() { grammar_path = test_path.join("grammar.json"); } let error_message_path = test_path.join("expected_error.txt"); - let grammar_json = generate::load_grammar_file(&grammar_path).unwrap(); + let grammar_json = generate::load_grammar_file(&grammar_path, None).unwrap(); let generate_result = generate::generate_parser_for_grammar(&grammar_json); if error_message_path.exists() {