@@ -21,10 +21,10 @@ use anyhow::{anyhow, Context, Result};
2121use lazy_static:: lazy_static;
2222use regex:: { Regex , RegexBuilder } ;
2323use semver:: Version ;
24- use std:: fs;
2524use std:: io:: Write ;
2625use std:: path:: { Path , PathBuf } ;
2726use std:: process:: { Command , Stdio } ;
27+ use std:: { env, fs} ;
2828
2929lazy_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
0 commit comments