Skip to content

Commit e024653

Browse files
authored
Merge pull request tree-sitter#2403 from jakesarjeant/master
feat(cli): add option to select JS runtime other than node
2 parents 397ead5 + 61b7094 commit e024653

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

cli/src/generate/mod.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ use anyhow::{anyhow, Context, Result};
2121
use lazy_static::lazy_static;
2222
use regex::{Regex, RegexBuilder};
2323
use semver::Version;
24-
use std::fs;
2524
use std::io::Write;
2625
use std::path::{Path, PathBuf};
2726
use std::process::{Command, Stdio};
27+
use std::{env, fs};
2828

2929
lazy_static! {
3030
static ref JSON_COMMENT_REGEX: Regex = RegexBuilder::new("^\\s*//.*")
@@ -44,16 +44,17 @@ pub fn generate_parser_in_directory(
4444
abi_version: usize,
4545
generate_bindings: bool,
4646
report_symbol_name: Option<&str>,
47+
js_runtime: Option<&str>,
4748
) -> Result<()> {
4849
let src_path = repo_path.join("src");
4950
let header_path = src_path.join("tree_sitter");
5051

5152
// Read the grammar.json.
5253
let grammar_json = match grammar_path {
53-
Some(path) => load_grammar_file(path.as_ref())?,
54+
Some(path) => load_grammar_file(path.as_ref(), js_runtime)?,
5455
None => {
5556
let grammar_js_path = grammar_path.map_or(repo_path.join("grammar.js"), |s| s.into());
56-
load_grammar_file(&grammar_js_path)?
57+
load_grammar_file(&grammar_js_path, js_runtime)?
5758
}
5859
};
5960

@@ -156,16 +157,15 @@ fn generate_parser_for_grammar_with_opts(
156157
})
157158
}
158159

159-
pub fn load_grammar_file(grammar_path: &Path) -> Result<String> {
160+
pub fn load_grammar_file(grammar_path: &Path, js_runtime: Option<&str>) -> Result<String> {
160161
if grammar_path.is_dir() {
161162
return Err(anyhow!(
162163
"Path to a grammar file with `.js` or `.json` extension is required"
163164
));
164165
}
165166
match grammar_path.extension().and_then(|e| e.to_str()) {
166-
Some("js") => {
167-
Ok(load_js_grammar_file(grammar_path).with_context(|| "Failed to load grammar.js")?)
168-
}
167+
Some("js") => Ok(load_js_grammar_file(grammar_path, js_runtime)
168+
.with_context(|| "Failed to load grammar.js")?),
169169
Some("json") => {
170170
Ok(fs::read_to_string(grammar_path).with_context(|| "Failed to load grammar.json")?)
171171
}
@@ -176,14 +176,17 @@ pub fn load_grammar_file(grammar_path: &Path) -> Result<String> {
176176
}
177177
}
178178

179-
fn load_js_grammar_file(grammar_path: &Path) -> Result<String> {
179+
fn load_js_grammar_file(grammar_path: &Path, js_runtime: Option<&str>) -> Result<String> {
180180
let grammar_path = fs::canonicalize(grammar_path)?;
181-
let mut node_process = Command::new("node")
181+
182+
let js_runtime = js_runtime.unwrap_or("node");
183+
184+
let mut node_process = Command::new(js_runtime)
182185
.env("TREE_SITTER_GRAMMAR_PATH", grammar_path)
183186
.stdin(Stdio::piped())
184187
.stdout(Stdio::piped())
185188
.spawn()
186-
.with_context(|| "Failed to run `node`")?;
189+
.with_context(|| format!("Failed to run `{js_runtime}`"))?;
187190

188191
let mut node_stdin = node_process
189192
.stdin

cli/src/main.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ fn run() -> Result<()> {
126126
.long("report-states-for-rule")
127127
.value_name("rule-name")
128128
.takes_value(true),
129+
)
130+
.arg(
131+
Arg::with_name("js-runtime")
132+
.long("js-runtime")
133+
.takes_value(true)
134+
.value_name("executable")
135+
.env("TREE_SITTER_JS_RUNTIME")
136+
.help("Use a JavaScript runtime other than node"),
129137
),
130138
)
131139
.subcommand(
@@ -307,6 +315,7 @@ fn run() -> Result<()> {
307315
let debug_build = matches.is_present("debug-build");
308316
let build = matches.is_present("build");
309317
let libdir = matches.value_of("libdir");
318+
let js_runtime = matches.value_of("js-runtime");
310319
let report_symbol_name = matches.value_of("report-states-for-rule").or_else(|| {
311320
if matches.is_present("report-states") {
312321
Some("")
@@ -336,6 +345,7 @@ fn run() -> Result<()> {
336345
abi_version,
337346
generate_bindings,
338347
report_symbol_name,
348+
js_runtime,
339349
)?;
340350
if build {
341351
if let Some(path) = libdir {

cli/src/tests/corpus_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ fn test_feature_corpus_files() {
260260
grammar_path = test_path.join("grammar.json");
261261
}
262262
let error_message_path = test_path.join("expected_error.txt");
263-
let grammar_json = generate::load_grammar_file(&grammar_path).unwrap();
263+
let grammar_json = generate::load_grammar_file(&grammar_path, None).unwrap();
264264
let generate_result = generate::generate_parser_for_grammar(&grammar_json);
265265

266266
if error_message_path.exists() {

0 commit comments

Comments
 (0)