Skip to main content

tree_sitter_cli/
init.rs

1use std::{
2    fs,
3    path::{Path, PathBuf},
4    str::{self, FromStr},
5};
6
7use anyhow::{anyhow, Context, Result};
8use crc32fast::hash as crc32;
9use heck::{ToKebabCase, ToShoutySnakeCase, ToSnakeCase, ToUpperCamelCase};
10use indoc::{formatdoc, indoc};
11use log::info;
12use rand::{thread_rng, Rng};
13use semver::Version;
14use serde::{Deserialize, Serialize};
15use serde_json::{Map, Value};
16use tree_sitter_generate::write_file;
17use tree_sitter_loader::{
18    Author, Bindings, Grammar, Links, Metadata, PathsJSON, TreeSitterJSON,
19    DEFAULT_HIGHLIGHTS_QUERY_FILE_NAME, DEFAULT_INJECTIONS_QUERY_FILE_NAME,
20    DEFAULT_LOCALS_QUERY_FILE_NAME, DEFAULT_TAGS_QUERY_FILE_NAME,
21};
22
23const CLI_VERSION: &str = env!("CARGO_PKG_VERSION");
24const CLI_VERSION_PLACEHOLDER: &str = "CLI_VERSION";
25
26const ABI_VERSION_MAX: usize = tree_sitter::LANGUAGE_VERSION;
27const ABI_VERSION_MAX_PLACEHOLDER: &str = "ABI_VERSION_MAX";
28
29const PARSER_NAME_PLACEHOLDER: &str = "PARSER_NAME";
30const CAMEL_PARSER_NAME_PLACEHOLDER: &str = "CAMEL_PARSER_NAME";
31const TITLE_PARSER_NAME_PLACEHOLDER: &str = "TITLE_PARSER_NAME";
32const UPPER_PARSER_NAME_PLACEHOLDER: &str = "UPPER_PARSER_NAME";
33const LOWER_PARSER_NAME_PLACEHOLDER: &str = "LOWER_PARSER_NAME";
34const KEBAB_PARSER_NAME_PLACEHOLDER: &str = "KEBAB_PARSER_NAME";
35const PARSER_CLASS_NAME_PLACEHOLDER: &str = "PARSER_CLASS_NAME";
36
37const PARSER_DESCRIPTION_PLACEHOLDER: &str = "PARSER_DESCRIPTION";
38const PARSER_LICENSE_PLACEHOLDER: &str = "PARSER_LICENSE";
39const PARSER_NS_PLACEHOLDER: &str = "PARSER_NS";
40const PARSER_NS_CLEANED_PLACEHOLDER: &str = "PARSER_NS_CLEANED";
41const PARSER_URL_PLACEHOLDER: &str = "PARSER_URL";
42const PARSER_URL_STRIPPED_PLACEHOLDER: &str = "PARSER_URL_STRIPPED";
43const PARSER_VERSION_PLACEHOLDER: &str = "PARSER_VERSION";
44const PARSER_FINGERPRINT_PLACEHOLDER: &str = "PARSER_FINGERPRINT";
45
46const AUTHOR_NAME_PLACEHOLDER: &str = "PARSER_AUTHOR_NAME";
47const AUTHOR_EMAIL_PLACEHOLDER: &str = "PARSER_AUTHOR_EMAIL";
48const AUTHOR_URL_PLACEHOLDER: &str = "PARSER_AUTHOR_URL";
49
50const AUTHOR_BLOCK_JS: &str = "\n  \"author\": {";
51const AUTHOR_NAME_PLACEHOLDER_JS: &str = "\n    \"name\": \"PARSER_AUTHOR_NAME\",";
52const AUTHOR_EMAIL_PLACEHOLDER_JS: &str = ",\n    \"email\": \"PARSER_AUTHOR_EMAIL\"";
53const AUTHOR_URL_PLACEHOLDER_JS: &str = ",\n    \"url\": \"PARSER_AUTHOR_URL\"";
54
55const AUTHOR_BLOCK_PY: &str = "\nauthors = [{";
56const AUTHOR_NAME_PLACEHOLDER_PY: &str = "name = \"PARSER_AUTHOR_NAME\"";
57const AUTHOR_EMAIL_PLACEHOLDER_PY: &str = ", email = \"PARSER_AUTHOR_EMAIL\"";
58
59const AUTHOR_BLOCK_RS: &str = "\nauthors = [";
60const AUTHOR_NAME_PLACEHOLDER_RS: &str = "PARSER_AUTHOR_NAME";
61const AUTHOR_EMAIL_PLACEHOLDER_RS: &str = " PARSER_AUTHOR_EMAIL";
62
63const AUTHOR_BLOCK_JAVA: &str = "\n    <developer>";
64const AUTHOR_NAME_PLACEHOLDER_JAVA: &str = "\n      <name>PARSER_AUTHOR_NAME</name>";
65const AUTHOR_EMAIL_PLACEHOLDER_JAVA: &str = "\n      <email>PARSER_AUTHOR_EMAIL</email>";
66const AUTHOR_URL_PLACEHOLDER_JAVA: &str = "\n      <url>PARSER_AUTHOR_URL</url>";
67
68const AUTHOR_BLOCK_GRAMMAR: &str = "\n * @author ";
69const AUTHOR_NAME_PLACEHOLDER_GRAMMAR: &str = "PARSER_AUTHOR_NAME";
70const AUTHOR_EMAIL_PLACEHOLDER_GRAMMAR: &str = " PARSER_AUTHOR_EMAIL";
71
72const FUNDING_URL_PLACEHOLDER: &str = "FUNDING_URL";
73
74const HIGHLIGHTS_QUERY_PATH_PLACEHOLDER: &str = "HIGHLIGHTS_QUERY_PATH";
75const INJECTIONS_QUERY_PATH_PLACEHOLDER: &str = "INJECTIONS_QUERY_PATH";
76const LOCALS_QUERY_PATH_PLACEHOLDER: &str = "LOCALS_QUERY_PATH";
77const TAGS_QUERY_PATH_PLACEHOLDER: &str = "TAGS_QUERY_PATH";
78
79const GRAMMAR_JS_TEMPLATE: &str = include_str!("./templates/grammar.js");
80const PACKAGE_JSON_TEMPLATE: &str = include_str!("./templates/package.json");
81const GITIGNORE_TEMPLATE: &str = include_str!("./templates/gitignore");
82const GITATTRIBUTES_TEMPLATE: &str = include_str!("./templates/gitattributes");
83const EDITORCONFIG_TEMPLATE: &str = include_str!("./templates/.editorconfig");
84
85const RUST_BINDING_VERSION: &str = env!("CARGO_PKG_VERSION");
86const RUST_BINDING_VERSION_PLACEHOLDER: &str = "RUST_BINDING_VERSION";
87
88const LIB_RS_TEMPLATE: &str = include_str!("./templates/lib.rs");
89const BUILD_RS_TEMPLATE: &str = include_str!("./templates/build.rs");
90const CARGO_TOML_TEMPLATE: &str = include_str!("./templates/_cargo.toml");
91
92const INDEX_JS_TEMPLATE: &str = include_str!("./templates/index.js");
93const INDEX_D_TS_TEMPLATE: &str = include_str!("./templates/index.d.ts");
94const JS_BINDING_CC_TEMPLATE: &str = include_str!("./templates/js-binding.cc");
95const BINDING_GYP_TEMPLATE: &str = include_str!("./templates/binding.gyp");
96const BINDING_TEST_JS_TEMPLATE: &str = include_str!("./templates/binding_test.js");
97
98const MAKEFILE_TEMPLATE: &str = include_str!("./templates/makefile");
99const CMAKELISTS_TXT_TEMPLATE: &str = include_str!("./templates/cmakelists.cmake");
100const PARSER_NAME_H_TEMPLATE: &str = include_str!("./templates/PARSER_NAME.h");
101const PARSER_NAME_PC_IN_TEMPLATE: &str = include_str!("./templates/PARSER_NAME.pc.in");
102
103const GO_MOD_TEMPLATE: &str = include_str!("./templates/go.mod");
104const BINDING_GO_TEMPLATE: &str = include_str!("./templates/binding.go");
105const BINDING_TEST_GO_TEMPLATE: &str = include_str!("./templates/binding_test.go");
106
107const SETUP_PY_TEMPLATE: &str = include_str!("./templates/setup.py");
108const INIT_PY_TEMPLATE: &str = include_str!("./templates/__init__.py");
109const INIT_PYI_TEMPLATE: &str = include_str!("./templates/__init__.pyi");
110const PYPROJECT_TOML_TEMPLATE: &str = include_str!("./templates/pyproject.toml");
111const PY_BINDING_C_TEMPLATE: &str = include_str!("./templates/py-binding.c");
112const TEST_BINDING_PY_TEMPLATE: &str = include_str!("./templates/test_binding.py");
113
114const PACKAGE_SWIFT_TEMPLATE: &str = include_str!("./templates/package.swift");
115const TESTS_SWIFT_TEMPLATE: &str = include_str!("./templates/tests.swift");
116
117const POM_XML_TEMPLATE: &str = include_str!("./templates/pom.xml");
118const BINDING_JAVA_TEMPLATE: &str = include_str!("./templates/binding.java");
119const TEST_JAVA_TEMPLATE: &str = include_str!("./templates/test.java");
120
121const BUILD_ZIG_TEMPLATE: &str = include_str!("./templates/build.zig");
122const BUILD_ZIG_ZON_TEMPLATE: &str = include_str!("./templates/build.zig.zon");
123const ROOT_ZIG_TEMPLATE: &str = include_str!("./templates/root.zig");
124const TEST_ZIG_TEMPLATE: &str = include_str!("./templates/test.zig");
125
126pub const TREE_SITTER_JSON_SCHEMA: &str =
127    "https://tree-sitter.github.io/tree-sitter/assets/schemas/config.schema.json";
128
129#[derive(Serialize, Deserialize, Clone)]
130pub struct JsonConfigOpts {
131    pub name: String,
132    pub camelcase: String,
133    pub title: String,
134    pub description: String,
135    #[serde(skip_serializing_if = "Option::is_none")]
136    pub repository: Option<String>,
137    #[serde(skip_serializing_if = "Option::is_none")]
138    pub funding: Option<String>,
139    pub scope: String,
140    pub file_types: Vec<String>,
141    pub version: Version,
142    pub license: String,
143    pub author: String,
144    #[serde(skip_serializing_if = "Option::is_none")]
145    pub email: Option<String>,
146    #[serde(skip_serializing_if = "Option::is_none")]
147    pub url: Option<String>,
148    pub namespace: Option<String>,
149    pub bindings: Bindings,
150}
151
152impl JsonConfigOpts {
153    #[must_use]
154    pub fn to_tree_sitter_json(self) -> TreeSitterJSON {
155        TreeSitterJSON {
156            schema: Some(TREE_SITTER_JSON_SCHEMA.to_string()),
157            grammars: vec![Grammar {
158                name: self.name.clone(),
159                camelcase: Some(self.camelcase),
160                title: Some(self.title),
161                scope: self.scope,
162                path: None,
163                external_files: PathsJSON::Empty,
164                file_types: Some(self.file_types),
165                highlights: PathsJSON::Empty,
166                injections: PathsJSON::Empty,
167                locals: PathsJSON::Empty,
168                tags: PathsJSON::Empty,
169                injection_regex: Some(format!("^{}$", self.name)),
170                first_line_regex: None,
171                content_regex: None,
172                class_name: Some(format!("TreeSitter{}", self.name.to_upper_camel_case())),
173            }],
174            metadata: Metadata {
175                version: self.version,
176                license: Some(self.license),
177                description: Some(self.description),
178                authors: Some(vec![Author {
179                    name: self.author,
180                    email: self.email,
181                    url: self.url,
182                }]),
183                links: Some(Links {
184                    repository: self.repository.unwrap_or_else(|| {
185                        format!("https://github.com/tree-sitter/tree-sitter-{}", self.name)
186                    }),
187                    funding: self.funding,
188                }),
189                namespace: self.namespace,
190            },
191            bindings: self.bindings,
192        }
193    }
194}
195
196impl Default for JsonConfigOpts {
197    fn default() -> Self {
198        Self {
199            name: String::new(),
200            camelcase: String::new(),
201            title: String::new(),
202            description: String::new(),
203            repository: None,
204            funding: None,
205            scope: String::new(),
206            file_types: vec![],
207            version: Version::from_str("0.1.0").unwrap(),
208            license: String::new(),
209            author: String::new(),
210            email: None,
211            url: None,
212            namespace: None,
213            bindings: Bindings::default(),
214        }
215    }
216}
217
218struct GenerateOpts<'a> {
219    author_name: Option<&'a str>,
220    author_email: Option<&'a str>,
221    author_url: Option<&'a str>,
222    license: Option<&'a str>,
223    description: Option<&'a str>,
224    repository: Option<&'a str>,
225    funding: Option<&'a str>,
226    version: &'a Version,
227    camel_parser_name: &'a str,
228    title_parser_name: &'a str,
229    class_name: &'a str,
230    highlights_query_path: &'a str,
231    injections_query_path: &'a str,
232    locals_query_path: &'a str,
233    tags_query_path: &'a str,
234    namespace: Option<&'a str>,
235}
236
237pub fn generate_grammar_files(
238    repo_path: &Path,
239    language_name: &str,
240    allow_update: bool,
241    opts: Option<&JsonConfigOpts>,
242) -> Result<()> {
243    let dashed_language_name = language_name.to_kebab_case();
244
245    let tree_sitter_config = missing_path_else(
246        repo_path.join("tree-sitter.json"),
247        true,
248        |path| {
249            // invariant: opts is always Some when `tree-sitter.json` doesn't exist
250            let Some(opts) = opts else { unreachable!() };
251
252            let tree_sitter_json = opts.clone().to_tree_sitter_json();
253            write_file(path, serde_json::to_string_pretty(&tree_sitter_json)?)?;
254            Ok(())
255        },
256        |path| {
257            // updating the config, if needed
258            if let Some(opts) = opts {
259                let tree_sitter_json = opts.clone().to_tree_sitter_json();
260                write_file(path, serde_json::to_string_pretty(&tree_sitter_json)?)?;
261            }
262            Ok(())
263        },
264    )?;
265
266    let tree_sitter_config = serde_json::from_str::<TreeSitterJSON>(
267        &fs::read_to_string(tree_sitter_config.as_path())
268            .with_context(|| "Failed to read tree-sitter.json")?,
269    )?;
270
271    let authors = tree_sitter_config.metadata.authors.as_ref();
272    let camel_name = tree_sitter_config.grammars[0]
273        .camelcase
274        .clone()
275        .unwrap_or_else(|| language_name.to_upper_camel_case());
276    let title_name = tree_sitter_config.grammars[0]
277        .title
278        .clone()
279        .unwrap_or_else(|| language_name.to_upper_camel_case());
280    let class_name = tree_sitter_config.grammars[0]
281        .class_name
282        .clone()
283        .unwrap_or_else(|| format!("TreeSitter{}", language_name.to_upper_camel_case()));
284
285    let default_highlights_path = Path::new("queries").join(DEFAULT_HIGHLIGHTS_QUERY_FILE_NAME);
286    let default_injections_path = Path::new("queries").join(DEFAULT_INJECTIONS_QUERY_FILE_NAME);
287    let default_locals_path = Path::new("queries").join(DEFAULT_LOCALS_QUERY_FILE_NAME);
288    let default_tags_path = Path::new("queries").join(DEFAULT_TAGS_QUERY_FILE_NAME);
289
290    let generate_opts = GenerateOpts {
291        author_name: authors
292            .map(|a| a.first().map(|a| a.name.as_str()))
293            .unwrap_or_default(),
294        author_email: authors
295            .map(|a| a.first().and_then(|a| a.email.as_deref()))
296            .unwrap_or_default(),
297        author_url: authors
298            .map(|a| a.first().and_then(|a| a.url.as_deref()))
299            .unwrap_or_default(),
300        license: tree_sitter_config.metadata.license.as_deref(),
301        description: tree_sitter_config.metadata.description.as_deref(),
302        repository: tree_sitter_config
303            .metadata
304            .links
305            .as_ref()
306            .map(|l| l.repository.as_str()),
307        funding: tree_sitter_config
308            .metadata
309            .links
310            .as_ref()
311            .and_then(|l| l.funding.as_deref()),
312        version: &tree_sitter_config.metadata.version,
313        camel_parser_name: &camel_name,
314        title_parser_name: &title_name,
315        class_name: &class_name,
316        highlights_query_path: tree_sitter_config.grammars[0]
317            .highlights
318            .to_variable_value(&default_highlights_path),
319        injections_query_path: tree_sitter_config.grammars[0]
320            .injections
321            .to_variable_value(&default_injections_path),
322        locals_query_path: tree_sitter_config.grammars[0]
323            .locals
324            .to_variable_value(&default_locals_path),
325        tags_query_path: tree_sitter_config.grammars[0]
326            .tags
327            .to_variable_value(&default_tags_path),
328        namespace: tree_sitter_config.metadata.namespace.as_deref(),
329    };
330
331    // Create package.json
332    missing_path_else(
333        repo_path.join("package.json"),
334        allow_update,
335        |path| {
336            generate_file(
337                path,
338                PACKAGE_JSON_TEMPLATE,
339                dashed_language_name.as_str(),
340                &generate_opts,
341            )
342        },
343        |path| {
344            let mut contents = fs::read_to_string(path)?
345                .replace(
346                    r#""node-addon-api": "^8.3.1""#,
347                    r#""node-addon-api": "^8.5.0""#,
348                )
349                .replace(
350                    indoc! {r#"
351                    "prebuildify": "^6.0.1",
352                    "tree-sitter-cli":"#},
353                    indoc! {r#"
354                    "prebuildify": "^6.0.1",
355                    "tree-sitter": "^0.25.0",
356                    "tree-sitter-cli":"#},
357                );
358            if !contents.contains("module") {
359                info!("Migrating package.json to ESM");
360                contents = contents.replace(
361                    r#""repository":"#,
362                    indoc! {r#"
363                    "type": "module",
364                      "repository":"#},
365                );
366            }
367            write_file(path, contents)?;
368            Ok(())
369        },
370    )?;
371
372    // Do not create a grammar.js file in a repo with multiple language configs
373    if !tree_sitter_config.has_multiple_language_configs() {
374        missing_path_else(
375            repo_path.join("grammar.js"),
376            allow_update,
377            |path| generate_file(path, GRAMMAR_JS_TEMPLATE, language_name, &generate_opts),
378            |path| {
379                let mut contents = fs::read_to_string(path)?;
380                if contents.contains("module.exports") {
381                    info!("Migrating grammars.js to ESM");
382                    contents = contents.replace("module.exports =", "export default");
383                    write_file(path, contents)?;
384                }
385
386                Ok(())
387            },
388        )?;
389    }
390
391    // Write .gitignore file
392    missing_path_else(
393        repo_path.join(".gitignore"),
394        allow_update,
395        |path| generate_file(path, GITIGNORE_TEMPLATE, language_name, &generate_opts),
396        |path| {
397            let mut contents = fs::read_to_string(path)?;
398            if !contents.contains("Zig artifacts") {
399                info!("Adding zig entries to .gitignore");
400                contents.push('\n');
401                contents.push_str(indoc! {"
402                # Zig artifacts
403                .zig-cache/
404                zig-cache/
405                zig-out/
406                "});
407            }
408            Ok(())
409        },
410    )?;
411
412    // Write .gitattributes file
413    missing_path_else(
414        repo_path.join(".gitattributes"),
415        allow_update,
416        |path| generate_file(path, GITATTRIBUTES_TEMPLATE, language_name, &generate_opts),
417        |path| {
418            let mut contents = fs::read_to_string(path)?;
419            let c_bindings_entry = "bindings/c/* ";
420            if contents.contains(c_bindings_entry) {
421                info!("Updating c bindings entry in .gitattributes");
422                contents = contents.replace(c_bindings_entry, "bindings/c/** ");
423            }
424            if !contents.contains("Zig bindings") {
425                info!("Adding zig entries to .gitattributes");
426                contents.push('\n');
427                contents.push_str(indoc! {"
428                # Zig bindings
429                build.zig linguist-generated
430                build.zig.zon linguist-generated
431                "});
432            }
433            write_file(path, contents)?;
434            Ok(())
435        },
436    )?;
437
438    // Write .editorconfig file
439    missing_path(repo_path.join(".editorconfig"), |path| {
440        generate_file(path, EDITORCONFIG_TEMPLATE, language_name, &generate_opts)
441    })?;
442
443    let bindings_dir = repo_path.join("bindings");
444
445    // Generate Rust bindings
446    if tree_sitter_config.bindings.rust {
447        missing_path(bindings_dir.join("rust"), create_dir)?.apply(|path| {
448            missing_path_else(path.join("lib.rs"), allow_update, |path| {
449                generate_file(path, LIB_RS_TEMPLATE, language_name, &generate_opts)
450            }, |path| {
451                let mut contents = fs::read_to_string(path)?;
452                if !contents.contains("#[cfg(with_highlights_query)]") {
453                    info!("Updating query constants in bindings/rust/lib.rs");
454                    let replacement = indoc! {r#"
455                        #[cfg(with_highlights_query)]
456                        /// The syntax highlighting query for this grammar.
457                        pub const HIGHLIGHTS_QUERY: &str = include_str!("../../HIGHLIGHTS_QUERY_PATH");
458
459                        #[cfg(with_injections_query)]
460                        /// The language injection query for this grammar.
461                        pub const INJECTIONS_QUERY: &str = include_str!("../../INJECTIONS_QUERY_PATH");
462
463                        #[cfg(with_locals_query)]
464                        /// The local variable query for this grammar.
465                        pub const LOCALS_QUERY: &str = include_str!("../../LOCALS_QUERY_PATH");
466
467                        #[cfg(with_tags_query)]
468                        /// The symbol tagging query for this grammar.
469                        pub const TAGS_QUERY: &str = include_str!("../../TAGS_QUERY_PATH");
470                        "#}
471                        .replace(HIGHLIGHTS_QUERY_PATH_PLACEHOLDER, &generate_opts.highlights_query_path.replace('\\', "/"))
472                        .replace(INJECTIONS_QUERY_PATH_PLACEHOLDER, &generate_opts.injections_query_path.replace('\\', "/"))
473                        .replace(LOCALS_QUERY_PATH_PLACEHOLDER, &generate_opts.locals_query_path.replace('\\', "/"))
474                        .replace(TAGS_QUERY_PATH_PLACEHOLDER, &generate_opts.tags_query_path.replace('\\', "/"));
475                    contents = contents
476                        .replace(
477                            indoc! {r#"
478                            // NOTE: uncomment these to include any queries that this grammar contains:
479
480                            // pub const HIGHLIGHTS_QUERY: &str = include_str!("../../queries/highlights.scm");
481                            // pub const INJECTIONS_QUERY: &str = include_str!("../../queries/injections.scm");
482                            // pub const LOCALS_QUERY: &str = include_str!("../../queries/locals.scm");
483                            // pub const TAGS_QUERY: &str = include_str!("../../queries/tags.scm");
484                            "#},
485                            &replacement,
486                        );
487                }
488                write_file(path, contents)?;
489                Ok(())
490            })?;
491
492            missing_path_else(
493                path.join("build.rs"),
494                allow_update,
495                |path| generate_file(path, BUILD_RS_TEMPLATE, language_name, &generate_opts),
496                |path| {
497                    let mut contents = fs::read_to_string(path)?;
498                    if !contents.contains("wasm32-unknown-unknown") {
499                        info!("Adding wasm32-unknown-unknown target to bindings/rust/build.rs");
500                        let replacement = indoc!{r#"
501                            c_config.flag("-utf-8");
502
503                            if std::env::var("TARGET").unwrap() == "wasm32-unknown-unknown" {
504                                let Ok(wasm_headers) = std::env::var("DEP_TREE_SITTER_LANGUAGE_WASM_HEADERS") else {
505                                    panic!("Environment variable DEP_TREE_SITTER_LANGUAGE_WASM_HEADERS must be set by the language crate");
506                                };
507                                let Ok(wasm_src) =
508                                    std::env::var("DEP_TREE_SITTER_LANGUAGE_WASM_SRC").map(std::path::PathBuf::from)
509                                else {
510                                    panic!("Environment variable DEP_TREE_SITTER_LANGUAGE_WASM_SRC must be set by the language crate");
511                                };
512
513                                c_config.include(&wasm_headers);
514                                c_config.files([
515                                    wasm_src.join("stdio.c"),
516                                    wasm_src.join("stdlib.c"),
517                                    wasm_src.join("string.c"),
518                                ]);
519                            }
520                        "#}
521                            .lines()
522                            .map(|line| if line.is_empty() { line.to_string() } else { format!("    {line}") })
523                            .collect::<Vec<_>>()
524                            .join("\n");
525
526                        contents = contents.replace(r#"    c_config.flag("-utf-8");"#, &replacement);
527                    }
528
529                    // Introduce configuration variables for dynamic query inclusion
530                    if !contents.contains("with_highlights_query") {
531                        info!("Adding support for dynamic query inclusion to bindings/rust/build.rs");
532                        let replaced = indoc! {r#"
533                                c_config.compile("tree-sitter-KEBAB_PARSER_NAME");
534                            }"#}
535                            .replace("KEBAB_PARSER_NAME", &language_name.to_kebab_case());
536
537                        let replacement = indoc! {r#"
538                                c_config.compile("tree-sitter-KEBAB_PARSER_NAME");
539
540                                println!("cargo:rustc-check-cfg=cfg(with_highlights_query)");
541                                if !"HIGHLIGHTS_QUERY_PATH".is_empty() && std::path::Path::new("HIGHLIGHTS_QUERY_PATH").exists() {
542                                    println!("cargo:rustc-cfg=with_highlights_query");
543                                }
544                                println!("cargo:rustc-check-cfg=cfg(with_injections_query)");
545                                if !"INJECTIONS_QUERY_PATH".is_empty() && std::path::Path::new("INJECTIONS_QUERY_PATH").exists() {
546                                    println!("cargo:rustc-cfg=with_injections_query");
547                                }
548                                println!("cargo:rustc-check-cfg=cfg(with_locals_query)");
549                                if !"LOCALS_QUERY_PATH".is_empty() && std::path::Path::new("LOCALS_QUERY_PATH").exists() {
550                                    println!("cargo:rustc-cfg=with_locals_query");
551                                }
552                                println!("cargo:rustc-check-cfg=cfg(with_tags_query)");
553                                if !"TAGS_QUERY_PATH".is_empty() && std::path::Path::new("TAGS_QUERY_PATH").exists() {
554                                    println!("cargo:rustc-cfg=with_tags_query");
555                                }
556                            }"#}
557                            .replace("KEBAB_PARSER_NAME", &language_name.to_kebab_case())
558                            .replace(HIGHLIGHTS_QUERY_PATH_PLACEHOLDER, &generate_opts.highlights_query_path.replace('\\', "/"))
559                            .replace(INJECTIONS_QUERY_PATH_PLACEHOLDER, &generate_opts.injections_query_path.replace('\\', "/"))
560                            .replace(LOCALS_QUERY_PATH_PLACEHOLDER, &generate_opts.locals_query_path.replace('\\', "/"))
561                            .replace(TAGS_QUERY_PATH_PLACEHOLDER, &generate_opts.tags_query_path.replace('\\', "/"));
562
563                        contents = contents.replace(
564                            &replaced,
565                            &replacement,
566                        );
567                    }
568
569                    write_file(path, contents)?;
570                    Ok(())
571                },
572            )?;
573
574            missing_path_else(
575                repo_path.join("Cargo.toml"),
576                allow_update,
577                |path| {
578                    generate_file(
579                        path,
580                        CARGO_TOML_TEMPLATE,
581                        dashed_language_name.as_str(),
582                        &generate_opts,
583                    )
584                },
585                |path| {
586                    let contents = fs::read_to_string(path)?;
587                    if contents.contains("\"LICENSE\"") {
588                        info!("Adding LICENSE entry to bindings/rust/Cargo.toml");
589                        write_file(path, contents.replace("\"LICENSE\"", "\"/LICENSE\""))?;
590                    }
591                    Ok(())
592                },
593            )?;
594
595            Ok(())
596        })?;
597    }
598
599    // Generate Node bindings
600    if tree_sitter_config.bindings.node {
601        missing_path(bindings_dir.join("node"), create_dir)?.apply(|path| {
602            missing_path_else(
603                path.join("index.js"),
604                allow_update,
605                |path| generate_file(path, INDEX_JS_TEMPLATE, language_name, &generate_opts),
606                |path| {
607                    let contents = fs::read_to_string(path)?;
608                    if !contents.contains("Object.defineProperty") {
609                        info!("Replacing index.js");
610                        generate_file(path, INDEX_JS_TEMPLATE, language_name, &generate_opts)?;
611                    }
612                    Ok(())
613                },
614            )?;
615
616            missing_path_else(
617                path.join("index.d.ts"),
618                allow_update,
619                |path| generate_file(path, INDEX_D_TS_TEMPLATE, language_name, &generate_opts),
620                |path| {
621                    let contents = fs::read_to_string(path)?;
622                    if !contents.contains("export default binding") {
623                        info!("Replacing index.d.ts");
624                        generate_file(path, INDEX_D_TS_TEMPLATE, language_name, &generate_opts)?;
625                    }
626                    Ok(())
627                },
628            )?;
629
630            missing_path_else(
631                path.join("binding_test.js"),
632                allow_update,
633                |path| {
634                    generate_file(
635                        path,
636                        BINDING_TEST_JS_TEMPLATE,
637                        language_name,
638                        &generate_opts,
639                    )
640                },
641                |path| {
642                    let contents = fs::read_to_string(path)?;
643                    if !contents.contains("import") {
644                        info!("Replacing binding_test.js");
645                        generate_file(
646                            path,
647                            BINDING_TEST_JS_TEMPLATE,
648                            language_name,
649                            &generate_opts,
650                        )?;
651                    }
652                    Ok(())
653                },
654            )?;
655
656            missing_path(path.join("binding.cc"), |path| {
657                generate_file(path, JS_BINDING_CC_TEMPLATE, language_name, &generate_opts)
658            })?;
659
660            missing_path_else(
661                repo_path.join("binding.gyp"),
662                allow_update,
663                |path| generate_file(path, BINDING_GYP_TEMPLATE, language_name, &generate_opts),
664                |path| {
665                    let contents = fs::read_to_string(path)?;
666                    if contents.contains("fs.exists(") {
667                        info!("Replacing `fs.exists` calls in binding.gyp");
668                        write_file(path, contents.replace("fs.exists(", "fs.existsSync("))?;
669                    }
670                    Ok(())
671                },
672            )?;
673
674            Ok(())
675        })?;
676    }
677
678    // Generate C bindings
679    if tree_sitter_config.bindings.c {
680        let kebab_case_name = language_name.to_kebab_case();
681        missing_path(bindings_dir.join("c"), create_dir)?.apply(|path| {
682            let header_name = format!("tree-sitter-{kebab_case_name}.h");
683            let old_file = &path.join(&header_name);
684            if allow_update && fs::exists(old_file).unwrap_or(false) {
685                info!("Removing bindings/c/{header_name}");
686                fs::remove_file(old_file)?;
687            }
688            missing_path(path.join("tree_sitter"), create_dir)?.apply(|include_path| {
689                missing_path(
690                    include_path.join(&header_name),
691                    |path| {
692                        generate_file(path, PARSER_NAME_H_TEMPLATE, language_name, &generate_opts)
693                    },
694                )?;
695                Ok(())
696            })?;
697
698            missing_path(
699                path.join(format!("tree-sitter-{kebab_case_name}.pc.in")),
700                |path| {
701                    generate_file(
702                        path,
703                        PARSER_NAME_PC_IN_TEMPLATE,
704                        language_name,
705                        &generate_opts,
706                    )
707                },
708            )?;
709
710            missing_path_else(
711                repo_path.join("Makefile"),
712                allow_update,
713                |path| {
714                    generate_file(path, MAKEFILE_TEMPLATE, language_name, &generate_opts)
715                },
716                |path| {
717                    let mut contents = fs::read_to_string(path)?;
718                    if !contents.contains("cd '$(DESTDIR)$(LIBDIR)' && ln -sf") {
719                        info!("Replacing Makefile");
720                        generate_file(path, MAKEFILE_TEMPLATE, language_name, &generate_opts)?;
721                    } else {
722                        let replaced = indoc! {r"
723                            $(PARSER): $(SRC_DIR)/grammar.json
724                                    $(TS) generate $^
725                            "};
726                        if contents.contains(replaced) {
727                            info!("Adding --no-parser target to Makefile");
728                            contents = contents
729                                .replace(
730                                    replaced,
731                                    indoc! {r"
732                                    $(SRC_DIR)/grammar.json: grammar.js
733                                            $(TS) generate --no-parser $^
734
735                                    $(PARSER): $(SRC_DIR)/grammar.json
736                                            $(TS) generate $^
737                                    "}
738                                );
739                        }
740                        if !contents.contains("\nDESCRIPTION :=") {
741                            if let Some(version_line) = contents.lines().find(|l| l.starts_with("VERSION := ")) {
742                                info!("Adding DESCRIPTION to Makefile");
743                                let description = generate_opts.description.map_or_else(
744                                    || format!("{} grammar for tree-sitter", generate_opts.camel_parser_name),
745                                    str::to_string,
746                                );
747                                contents = contents.replace(
748                                    version_line,
749                                    &format!("{version_line}\nDESCRIPTION := {description}"),
750                                );
751                            }
752                        }
753                        write_file(path, contents)?;
754                    }
755                    Ok(())
756                },
757            )?;
758
759            missing_path_else(
760                repo_path.join("CMakeLists.txt"),
761                allow_update,
762                |path| generate_file(path, CMAKELISTS_TXT_TEMPLATE, language_name, &generate_opts),
763                |path| {
764                    let contents = fs::read_to_string(path)?;
765                    let replaced_contents = contents
766                        .replace("add_custom_target(test", "add_custom_target(ts-test")
767                        .replace(
768                            &formatdoc! {r#"
769                            install(FILES bindings/c/tree-sitter-{language_name}.h
770                                    DESTINATION "${{CMAKE_INSTALL_INCLUDEDIR}}/tree_sitter")
771                            "#},
772                            indoc! {r#"
773                            install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bindings/c/tree_sitter"
774                                    DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
775                                    FILES_MATCHING PATTERN "*.h")
776                            "#}
777                        ).replace(
778                            &format!("target_include_directories(tree-sitter-{language_name} PRIVATE src)"),
779                            &formatdoc! {"
780                            target_include_directories(tree-sitter-{language_name}
781                                                       PRIVATE src
782                                                       INTERFACE $<BUILD_INTERFACE:${{CMAKE_CURRENT_SOURCE_DIR}}/bindings/c>
783                                                                 $<INSTALL_INTERFACE:${{CMAKE_INSTALL_INCLUDEDIR}}>)
784                            "}
785                        ).replace(
786                            indoc! {r#"
787                            add_custom_command(OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/src/parser.c"
788                                               DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/grammar.json"
789                                               COMMAND "${TREE_SITTER_CLI}" generate src/grammar.json
790                                                        --abi=${TREE_SITTER_ABI_VERSION}
791                                               WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
792                                               COMMENT "Generating parser.c")
793                            "#},
794                            indoc! {r#"
795                            add_custom_command(OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/src/grammar.json"
796                                                      "${CMAKE_CURRENT_SOURCE_DIR}/src/node-types.json"
797                                               DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/grammar.js"
798                                               COMMAND "${TREE_SITTER_CLI}" generate grammar.js --no-parser
799                                               WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
800                                               COMMENT "Generating grammar.json")
801
802                            add_custom_command(OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/src/parser.c"
803                                               BYPRODUCTS "${CMAKE_CURRENT_SOURCE_DIR}/src/tree_sitter/parser.h"
804                                                          "${CMAKE_CURRENT_SOURCE_DIR}/src/tree_sitter/alloc.h"
805                                                          "${CMAKE_CURRENT_SOURCE_DIR}/src/tree_sitter/array.h"
806                                               DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/grammar.json"
807                                               COMMAND "${TREE_SITTER_CLI}" generate src/grammar.json
808                                                        --abi=${TREE_SITTER_ABI_VERSION}
809                                               WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
810                                               COMMENT "Generating parser.c")
811                            "#}
812                        );
813                    if !replaced_contents.eq(&contents) {
814                        info!("Updating CMakeLists.txt");
815                        write_file(path, replaced_contents)?;
816                    }
817                    Ok(())
818                },
819            )?;
820
821            Ok(())
822        })?;
823    }
824
825    // Generate Go bindings
826    if tree_sitter_config.bindings.go {
827        missing_path(bindings_dir.join("go"), create_dir)?.apply(|path| {
828            missing_path(path.join("binding.go"), |path| {
829                generate_file(path, BINDING_GO_TEMPLATE, language_name, &generate_opts)
830            })?;
831
832            missing_path(path.join("binding_test.go"), |path| {
833                generate_file(
834                    path,
835                    BINDING_TEST_GO_TEMPLATE,
836                    language_name,
837                    &generate_opts,
838                )
839            })?;
840
841            missing_path(repo_path.join("go.mod"), |path| {
842                generate_file(path, GO_MOD_TEMPLATE, language_name, &generate_opts)
843            })?;
844
845            Ok(())
846        })?;
847    }
848
849    // Generate Python bindings
850    if tree_sitter_config.bindings.python {
851        missing_path(bindings_dir.join("python"), create_dir)?.apply(|path| {
852            let snake_case_grammar_name = format!("tree_sitter_{}", language_name.to_snake_case());
853            let lang_path = path.join(&snake_case_grammar_name);
854            missing_path(&lang_path, create_dir)?;
855
856            missing_path_else(
857                lang_path.join("binding.c"),
858                allow_update,
859                |path| generate_file(path, PY_BINDING_C_TEMPLATE, language_name, &generate_opts),
860                |path| {
861                    let mut contents = fs::read_to_string(path)?;
862                    if !contents.contains("PyModuleDef_Init") {
863                        info!("Updating bindings/python/{snake_case_grammar_name}/binding.c");
864                        contents = contents
865                            .replace("PyModule_Create", "PyModuleDef_Init")
866                            .replace(
867                                "static PyMethodDef methods[] = {\n",
868                                indoc! {"
869                                static struct PyModuleDef_Slot slots[] = {
870                                #ifdef Py_GIL_DISABLED
871                                    {Py_mod_gil, Py_MOD_GIL_NOT_USED},
872                                #endif
873                                    {0, NULL}
874                                };
875
876                                static PyMethodDef methods[] = {
877                                "},
878                            )
879                            .replace(
880                                indoc! {"
881                                .m_size = -1,
882                                    .m_methods = methods
883                                "},
884                                indoc! {"
885                                .m_size = 0,
886                                    .m_methods = methods,
887                                    .m_slots = slots,
888                                "},
889                            );
890                        write_file(path, contents)?;
891                    }
892                    Ok(())
893                },
894            )?;
895
896            missing_path_else(
897                lang_path.join("__init__.py"),
898                allow_update,
899                |path| {
900                    generate_file(path, INIT_PY_TEMPLATE, language_name, &generate_opts)
901                },
902                |path| {
903                    let contents = fs::read_to_string(path)?;
904                    if contents.contains("uncomment these to include any queries") {
905                        info!("Replacing __init__.py");
906                        generate_file(path, INIT_PY_TEMPLATE, language_name, &generate_opts)?;
907                    }
908                    Ok(())
909                },
910            )?;
911
912            missing_path_else(
913                lang_path.join("__init__.pyi"),
914                allow_update,
915                |path| generate_file(path, INIT_PYI_TEMPLATE, language_name, &generate_opts),
916                |path| {
917                    let mut contents = fs::read_to_string(path)?;
918                    if contents.contains("uncomment these to include any queries") {
919                        info!("Replacing __init__.pyi");
920                        generate_file(path, INIT_PYI_TEMPLATE, language_name, &generate_opts)?;
921                    } else if !contents.contains("CapsuleType") {
922                        info!("Updating __init__.pyi");
923                        contents = contents
924                            .replace(
925                                "from typing import Final",
926                                "from typing import Final\nfrom typing_extensions import CapsuleType"
927                            )
928                            .replace("-> object:", "-> CapsuleType:");
929                        write_file(path, contents)?;
930                    }
931                    Ok(())
932                },
933            )?;
934
935            missing_path(lang_path.join("py.typed"), |path| {
936                generate_file(path, "", language_name, &generate_opts) // py.typed is empty
937            })?;
938
939            missing_path(path.join("tests"), create_dir)?.apply(|path| {
940                missing_path_else(
941                    path.join("test_binding.py"),
942                    allow_update,
943                    |path| {
944                        generate_file(
945                            path,
946                            TEST_BINDING_PY_TEMPLATE,
947                            language_name,
948                            &generate_opts,
949                        )
950                    },
951                    |path| {
952                        let mut contents = fs::read_to_string(path)?;
953                        if !contents.contains("Parser(Language(") {
954                            info!("Updating Language function in bindings/python/tests/test_binding.py");
955                            contents = contents
956                                .replace("tree_sitter.Language(", "Parser(Language(")
957                                .replace(".language())\n", ".language()))\n")
958                                .replace(
959                                    "import tree_sitter\n",
960                                    "from tree_sitter import Language, Parser\n",
961                                );
962                            write_file(path, contents)?;
963                        }
964                        Ok(())
965                    },
966                )?;
967                Ok(())
968            })?;
969
970            missing_path_else(
971                repo_path.join("setup.py"),
972                allow_update,
973                |path| generate_file(path, SETUP_PY_TEMPLATE, language_name, &generate_opts),
974                |path| {
975                    let mut contents = fs::read_to_string(path)?;
976                    if !contents.contains("build_ext") {
977                        info!("Replacing setup.py");
978                        generate_file(path, SETUP_PY_TEMPLATE, language_name, &generate_opts)?;
979                    }
980                    if !contents.contains(" and not get_config_var") {
981                        info!("Updating Python free-threading support in setup.py");
982                        contents = contents.replace(
983                            r#"startswith("cp"):"#,
984                            r#"startswith("cp") and not get_config_var("Py_GIL_DISABLED"):"#
985                        );
986                        write_file(path, contents)?;
987                    }
988                    Ok(())
989                },
990            )?;
991
992            missing_path_else(
993                repo_path.join("pyproject.toml"),
994                allow_update,
995                |path| {
996                    generate_file(
997                        path,
998                        PYPROJECT_TOML_TEMPLATE,
999                        dashed_language_name.as_str(),
1000                        &generate_opts,
1001                    )
1002                },
1003                |path| {
1004                    let mut contents = fs::read_to_string(path)?;
1005                    if !contents.contains("cp310-*") {
1006                        info!("Updating dependencies in pyproject.toml");
1007                        contents = contents
1008                            .replace(r#"build = "cp39-*""#, r#"build = "cp310-*""#)
1009                            .replace(r#"python = ">=3.9""#, r#"python = ">=3.10""#)
1010                            .replace("tree-sitter~=0.22", "tree-sitter~=0.24");
1011                        write_file(path, contents)?;
1012                    }
1013                    Ok(())
1014                },
1015            )?;
1016
1017            Ok(())
1018        })?;
1019    }
1020
1021    // Generate Swift bindings
1022    if tree_sitter_config.bindings.swift {
1023        missing_path(bindings_dir.join("swift"), create_dir)?.apply(|path| {
1024            let lang_path = path.join(&class_name);
1025            missing_path(&lang_path, create_dir)?;
1026
1027            missing_path(lang_path.join(format!("{language_name}.h")), |path| {
1028                generate_file(path, PARSER_NAME_H_TEMPLATE, language_name, &generate_opts)
1029            })?;
1030
1031            missing_path(path.join(format!("{class_name}Tests")), create_dir)?.apply(|path| {
1032                missing_path(path.join(format!("{class_name}Tests.swift")), |path| {
1033                    generate_file(path, TESTS_SWIFT_TEMPLATE, language_name, &generate_opts)
1034                })?;
1035
1036                Ok(())
1037            })?;
1038
1039            missing_path_else(
1040                repo_path.join("Package.swift"),
1041                allow_update,
1042                |path| generate_file(path, PACKAGE_SWIFT_TEMPLATE, language_name, &generate_opts),
1043                |path| {
1044                    let contents = fs::read_to_string(path)?;
1045                    let replaced_contents = contents
1046                        .replace(
1047                            "https://github.com/ChimeHQ/SwiftTreeSitter",
1048                            "https://github.com/tree-sitter/swift-tree-sitter",
1049                        )
1050                        .replace("version: \"0.8.0\")", "version: \"0.9.0\")")
1051                        .replace("(url:", "(name: \"SwiftTreeSitter\", url:");
1052                    if !replaced_contents.eq(&contents) {
1053                        info!("Updating tree-sitter dependency in Package.swift");
1054                        write_file(path, replaced_contents)?;
1055                    }
1056                    Ok(())
1057                },
1058            )?;
1059
1060            Ok(())
1061        })?;
1062    }
1063
1064    // Generate Zig bindings
1065    if tree_sitter_config.bindings.zig {
1066        missing_path_else(
1067            repo_path.join("build.zig"),
1068            allow_update,
1069            |path| generate_file(path, BUILD_ZIG_TEMPLATE, language_name, &generate_opts),
1070            |path| {
1071                let contents = fs::read_to_string(path)?;
1072                if !contents.contains("b.pkg_hash.len") {
1073                    info!("Replacing build.zig");
1074                    generate_file(path, BUILD_ZIG_TEMPLATE, language_name, &generate_opts)
1075                } else {
1076                    Ok(())
1077                }
1078            },
1079        )?;
1080
1081        missing_path_else(
1082            repo_path.join("build.zig.zon"),
1083            allow_update,
1084            |path| generate_file(path, BUILD_ZIG_ZON_TEMPLATE, language_name, &generate_opts),
1085            |path| {
1086                let contents = fs::read_to_string(path)?;
1087                if !contents.contains(".name = .tree_sitter_") {
1088                    info!("Replacing build.zig.zon");
1089                    generate_file(path, BUILD_ZIG_ZON_TEMPLATE, language_name, &generate_opts)
1090                } else {
1091                    Ok(())
1092                }
1093            },
1094        )?;
1095
1096        missing_path(bindings_dir.join("zig"), create_dir)?.apply(|path| {
1097            missing_path_else(
1098                path.join("root.zig"),
1099                allow_update,
1100                |path| generate_file(path, ROOT_ZIG_TEMPLATE, language_name, &generate_opts),
1101                |path| {
1102                    let contents = fs::read_to_string(path)?;
1103                    if contents.contains("ts.Language") {
1104                        info!("Replacing root.zig");
1105                        generate_file(path, ROOT_ZIG_TEMPLATE, language_name, &generate_opts)
1106                    } else {
1107                        Ok(())
1108                    }
1109                },
1110            )?;
1111
1112            missing_path(path.join("test.zig"), |path| {
1113                generate_file(path, TEST_ZIG_TEMPLATE, language_name, &generate_opts)
1114            })?;
1115
1116            Ok(())
1117        })?;
1118    }
1119
1120    // Generate Java bindings
1121    if tree_sitter_config.bindings.java {
1122        missing_path(repo_path.join("pom.xml"), |path| {
1123            generate_file(path, POM_XML_TEMPLATE, language_name, &generate_opts)
1124        })?;
1125
1126        missing_path(bindings_dir.join("java"), create_dir)?.apply(|path| {
1127            missing_path(path.join("main"), create_dir)?.apply(|path| {
1128                let package_path = generate_opts
1129                    .namespace
1130                    .unwrap_or("io.github.treesitter")
1131                    .replace(['-', '_'], "")
1132                    .split('.')
1133                    .fold(path.to_path_buf(), |path, dir| path.join(dir))
1134                    .join("jtreesitter")
1135                    .join(language_name.to_lowercase().replace('_', ""));
1136                missing_path(package_path, create_dir)?.apply(|path| {
1137                    missing_path(path.join(format!("{class_name}.java")), |path| {
1138                        generate_file(path, BINDING_JAVA_TEMPLATE, language_name, &generate_opts)
1139                    })?;
1140
1141                    Ok(())
1142                })?;
1143
1144                Ok(())
1145            })?;
1146
1147            missing_path(path.join("test"), create_dir)?.apply(|path| {
1148                missing_path(path.join(format!("{class_name}Test.java")), |path| {
1149                    generate_file(path, TEST_JAVA_TEMPLATE, language_name, &generate_opts)
1150                })?;
1151
1152                Ok(())
1153            })?;
1154
1155            Ok(())
1156        })?;
1157    }
1158
1159    Ok(())
1160}
1161
1162pub fn get_root_path(path: &Path) -> Result<PathBuf> {
1163    let mut pathbuf = path.to_owned();
1164    let filename = path.file_name().unwrap().to_str().unwrap();
1165    let is_package_json = filename == "package.json";
1166    loop {
1167        let json = pathbuf
1168            .exists()
1169            .then(|| {
1170                let contents = fs::read_to_string(pathbuf.as_path())
1171                    .with_context(|| format!("Failed to read {filename}"))?;
1172                if is_package_json {
1173                    serde_json::from_str::<Map<String, Value>>(&contents)
1174                        .context(format!("Failed to parse {filename}"))
1175                        .map(|v| v.contains_key("tree-sitter"))
1176                } else {
1177                    serde_json::from_str::<TreeSitterJSON>(&contents)
1178                        .context(format!("Failed to parse {filename}"))
1179                        .map(|_| true)
1180                }
1181            })
1182            .transpose()?;
1183        if json == Some(true) {
1184            return Ok(pathbuf.parent().unwrap().to_path_buf());
1185        }
1186        pathbuf.pop(); // filename
1187        if !pathbuf.pop() {
1188            return Err(anyhow!(format!(
1189                concat!(
1190                    "Failed to locate a {} file,",
1191                    " please ensure you have one, and if you don't then consult the docs",
1192                ),
1193                filename
1194            )));
1195        }
1196        pathbuf.push(filename);
1197    }
1198}
1199
1200fn generate_file(
1201    path: &Path,
1202    template: &str,
1203    language_name: &str,
1204    generate_opts: &GenerateOpts,
1205) -> Result<()> {
1206    let filename = path.file_name().unwrap().to_str().unwrap();
1207
1208    let lower_parser_name = if path
1209        .extension()
1210        .is_some_and(|e| e.eq_ignore_ascii_case("java"))
1211    {
1212        language_name.to_snake_case().replace('_', "")
1213    } else {
1214        language_name.to_snake_case()
1215    };
1216
1217    let mut replacement = template
1218        .replace(
1219            CAMEL_PARSER_NAME_PLACEHOLDER,
1220            generate_opts.camel_parser_name,
1221        )
1222        .replace(
1223            TITLE_PARSER_NAME_PLACEHOLDER,
1224            generate_opts.title_parser_name,
1225        )
1226        .replace(
1227            UPPER_PARSER_NAME_PLACEHOLDER,
1228            &language_name.to_shouty_snake_case(),
1229        )
1230        .replace(
1231            KEBAB_PARSER_NAME_PLACEHOLDER,
1232            &language_name.to_kebab_case(),
1233        )
1234        .replace(LOWER_PARSER_NAME_PLACEHOLDER, &lower_parser_name)
1235        .replace(PARSER_NAME_PLACEHOLDER, language_name)
1236        .replace(CLI_VERSION_PLACEHOLDER, CLI_VERSION)
1237        .replace(RUST_BINDING_VERSION_PLACEHOLDER, RUST_BINDING_VERSION)
1238        .replace(ABI_VERSION_MAX_PLACEHOLDER, &ABI_VERSION_MAX.to_string())
1239        .replace(
1240            PARSER_VERSION_PLACEHOLDER,
1241            &generate_opts.version.to_string(),
1242        )
1243        .replace(PARSER_CLASS_NAME_PLACEHOLDER, generate_opts.class_name)
1244        .replace(
1245            HIGHLIGHTS_QUERY_PATH_PLACEHOLDER,
1246            &generate_opts.highlights_query_path.replace('\\', "/"),
1247        )
1248        .replace(
1249            INJECTIONS_QUERY_PATH_PLACEHOLDER,
1250            &generate_opts.injections_query_path.replace('\\', "/"),
1251        )
1252        .replace(
1253            LOCALS_QUERY_PATH_PLACEHOLDER,
1254            &generate_opts.locals_query_path.replace('\\', "/"),
1255        )
1256        .replace(
1257            TAGS_QUERY_PATH_PLACEHOLDER,
1258            &generate_opts.tags_query_path.replace('\\', "/"),
1259        );
1260
1261    if let Some(name) = generate_opts.author_name {
1262        replacement = replacement.replace(AUTHOR_NAME_PLACEHOLDER, name);
1263    } else {
1264        match filename {
1265            "package.json" => {
1266                replacement = replacement.replace(AUTHOR_NAME_PLACEHOLDER_JS, "");
1267            }
1268            "pyproject.toml" => {
1269                replacement = replacement.replace(AUTHOR_NAME_PLACEHOLDER_PY, "");
1270            }
1271            "grammar.js" => {
1272                replacement = replacement.replace(AUTHOR_NAME_PLACEHOLDER_GRAMMAR, "");
1273            }
1274            "Cargo.toml" => {
1275                replacement = replacement.replace(AUTHOR_NAME_PLACEHOLDER_RS, "");
1276            }
1277            "pom.xml" => {
1278                replacement = replacement.replace(AUTHOR_NAME_PLACEHOLDER_JAVA, "");
1279            }
1280            _ => {}
1281        }
1282    }
1283
1284    if let Some(email) = generate_opts.author_email {
1285        replacement = match filename {
1286            "Cargo.toml" | "grammar.js" => {
1287                replacement.replace(AUTHOR_EMAIL_PLACEHOLDER, &format!("<{email}>"))
1288            }
1289            _ => replacement.replace(AUTHOR_EMAIL_PLACEHOLDER, email),
1290        }
1291    } else {
1292        match filename {
1293            "package.json" => {
1294                replacement = replacement.replace(AUTHOR_EMAIL_PLACEHOLDER_JS, "");
1295            }
1296            "pyproject.toml" => {
1297                replacement = replacement.replace(AUTHOR_EMAIL_PLACEHOLDER_PY, "");
1298            }
1299            "grammar.js" => {
1300                replacement = replacement.replace(AUTHOR_EMAIL_PLACEHOLDER_GRAMMAR, "");
1301            }
1302            "Cargo.toml" => {
1303                replacement = replacement.replace(AUTHOR_EMAIL_PLACEHOLDER_RS, "");
1304            }
1305            "pom.xml" => {
1306                replacement = replacement.replace(AUTHOR_EMAIL_PLACEHOLDER_JAVA, "");
1307            }
1308            _ => {}
1309        }
1310    }
1311
1312    match (generate_opts.author_url, filename) {
1313        (Some(url), "package.json" | "pom.xml") => {
1314            replacement = replacement.replace(AUTHOR_URL_PLACEHOLDER, url);
1315        }
1316        (None, "package.json") => {
1317            replacement = replacement.replace(AUTHOR_URL_PLACEHOLDER_JS, "");
1318        }
1319        (None, "pom.xml") => {
1320            replacement = replacement.replace(AUTHOR_URL_PLACEHOLDER_JAVA, "");
1321        }
1322        _ => {}
1323    }
1324
1325    if generate_opts.author_name.is_none()
1326        && generate_opts.author_email.is_none()
1327        && generate_opts.author_url.is_none()
1328    {
1329        match filename {
1330            "package.json" => {
1331                if let Some(start_idx) = replacement.find(AUTHOR_BLOCK_JS) {
1332                    if let Some(end_idx) = replacement[start_idx..]
1333                        .find("},")
1334                        .map(|i| i + start_idx + 2)
1335                    {
1336                        replacement.replace_range(start_idx..end_idx, "");
1337                    }
1338                }
1339            }
1340            "pom.xml" => {
1341                if let Some(start_idx) = replacement.find(AUTHOR_BLOCK_JAVA) {
1342                    if let Some(end_idx) = replacement[start_idx..]
1343                        .find("</developer>")
1344                        .map(|i| i + start_idx + 12)
1345                    {
1346                        replacement.replace_range(start_idx..end_idx, "");
1347                    }
1348                }
1349            }
1350            _ => {}
1351        }
1352    } else if generate_opts.author_name.is_none() && generate_opts.author_email.is_none() {
1353        match filename {
1354            "pyproject.toml" => {
1355                if let Some(start_idx) = replacement.find(AUTHOR_BLOCK_PY) {
1356                    if let Some(end_idx) = replacement[start_idx..]
1357                        .find("}]")
1358                        .map(|i| i + start_idx + 2)
1359                    {
1360                        replacement.replace_range(start_idx..end_idx, "");
1361                    }
1362                }
1363            }
1364            "grammar.js" => {
1365                if let Some(start_idx) = replacement.find(AUTHOR_BLOCK_GRAMMAR) {
1366                    if let Some(end_idx) = replacement[start_idx..]
1367                        .find(" \n")
1368                        .map(|i| i + start_idx + 1)
1369                    {
1370                        replacement.replace_range(start_idx..end_idx, "");
1371                    }
1372                }
1373            }
1374            "Cargo.toml" => {
1375                if let Some(start_idx) = replacement.find(AUTHOR_BLOCK_RS) {
1376                    if let Some(end_idx) = replacement[start_idx..]
1377                        .find("\"]")
1378                        .map(|i| i + start_idx + 2)
1379                    {
1380                        replacement.replace_range(start_idx..end_idx, "");
1381                    }
1382                }
1383            }
1384            _ => {}
1385        }
1386    }
1387
1388    if let Some(license) = generate_opts.license {
1389        replacement = replacement.replace(PARSER_LICENSE_PLACEHOLDER, license);
1390    } else {
1391        replacement = replacement.replace(PARSER_LICENSE_PLACEHOLDER, "MIT");
1392    }
1393
1394    if let Some(description) = generate_opts.description {
1395        replacement = replacement.replace(PARSER_DESCRIPTION_PLACEHOLDER, description);
1396    } else {
1397        replacement = replacement.replace(
1398            PARSER_DESCRIPTION_PLACEHOLDER,
1399            &format!(
1400                "{} grammar for tree-sitter",
1401                generate_opts.camel_parser_name,
1402            ),
1403        );
1404    }
1405
1406    if let Some(repository) = generate_opts.repository {
1407        replacement = replacement
1408            .replace(
1409                PARSER_URL_STRIPPED_PLACEHOLDER,
1410                &repository.replace("https://", "").to_lowercase(),
1411            )
1412            .replace(PARSER_URL_PLACEHOLDER, &repository.to_lowercase());
1413    } else {
1414        replacement = replacement
1415            .replace(
1416                PARSER_URL_STRIPPED_PLACEHOLDER,
1417                &format!(
1418                    "github.com/tree-sitter/tree-sitter-{}",
1419                    language_name.to_lowercase()
1420                ),
1421            )
1422            .replace(
1423                PARSER_URL_PLACEHOLDER,
1424                &format!(
1425                    "https://github.com/tree-sitter/tree-sitter-{}",
1426                    language_name.to_lowercase()
1427                ),
1428            );
1429    }
1430
1431    if let Some(namespace) = generate_opts.namespace {
1432        replacement = replacement
1433            .replace(
1434                PARSER_NS_CLEANED_PLACEHOLDER,
1435                &namespace.replace(['-', '_'], ""),
1436            )
1437            .replace(PARSER_NS_PLACEHOLDER, namespace);
1438    } else {
1439        replacement = replacement
1440            .replace(PARSER_NS_CLEANED_PLACEHOLDER, "io.github.treesitter")
1441            .replace(PARSER_NS_PLACEHOLDER, "io.github.tree-sitter");
1442    }
1443
1444    if let Some(funding_url) = generate_opts.funding {
1445        match filename {
1446            "pyproject.toml" | "package.json" => {
1447                replacement = replacement.replace(FUNDING_URL_PLACEHOLDER, funding_url);
1448            }
1449            _ => {}
1450        }
1451    } else {
1452        match filename {
1453            "package.json" => {
1454                replacement = replacement.replace("  \"funding\": \"FUNDING_URL\",\n", "");
1455            }
1456            "pyproject.toml" => {
1457                replacement = replacement.replace("Funding = \"FUNDING_URL\"\n", "");
1458            }
1459            _ => {}
1460        }
1461    }
1462
1463    if filename == "build.zig.zon" {
1464        let id = thread_rng().gen_range(1u32..0xFFFF_FFFFu32);
1465        let checksum = crc32(format!("tree_sitter_{language_name}").as_bytes());
1466        replacement = replacement.replace(
1467            PARSER_FINGERPRINT_PLACEHOLDER,
1468            #[cfg(target_endian = "little")]
1469            &format!("0x{checksum:x}{id:x}"),
1470            #[cfg(target_endian = "big")]
1471            &format!("0x{id:x}{checksum:x}"),
1472        );
1473    }
1474
1475    write_file(path, replacement)?;
1476    Ok(())
1477}
1478
1479fn create_dir(path: &Path) -> Result<()> {
1480    fs::create_dir_all(path)
1481        .with_context(|| format!("Failed to create {:?}", path.to_string_lossy()))
1482}
1483
1484#[derive(PartialEq, Eq, Debug)]
1485enum PathState<P>
1486where
1487    P: AsRef<Path>,
1488{
1489    Exists(P),
1490    Missing(P),
1491}
1492
1493#[allow(dead_code)]
1494impl<P> PathState<P>
1495where
1496    P: AsRef<Path>,
1497{
1498    fn exists(&self, mut action: impl FnMut(&Path) -> Result<()>) -> Result<&Self> {
1499        if let Self::Exists(path) = self {
1500            action(path.as_ref())?;
1501        }
1502        Ok(self)
1503    }
1504
1505    fn missing(&self, mut action: impl FnMut(&Path) -> Result<()>) -> Result<&Self> {
1506        if let Self::Missing(path) = self {
1507            action(path.as_ref())?;
1508        }
1509        Ok(self)
1510    }
1511
1512    fn apply(&self, mut action: impl FnMut(&Path) -> Result<()>) -> Result<&Self> {
1513        action(self.as_path())?;
1514        Ok(self)
1515    }
1516
1517    fn apply_state(&self, mut action: impl FnMut(&Self) -> Result<()>) -> Result<&Self> {
1518        action(self)?;
1519        Ok(self)
1520    }
1521
1522    fn as_path(&self) -> &Path {
1523        match self {
1524            Self::Exists(path) | Self::Missing(path) => path.as_ref(),
1525        }
1526    }
1527}
1528
1529fn missing_path<P, F>(path: P, mut action: F) -> Result<PathState<P>>
1530where
1531    P: AsRef<Path>,
1532    F: FnMut(&Path) -> Result<()>,
1533{
1534    let path_ref = path.as_ref();
1535    if !path_ref.exists() {
1536        action(path_ref)?;
1537        Ok(PathState::Missing(path))
1538    } else {
1539        Ok(PathState::Exists(path))
1540    }
1541}
1542
1543fn missing_path_else<P, T, F>(
1544    path: P,
1545    allow_update: bool,
1546    mut action: T,
1547    mut else_action: F,
1548) -> Result<PathState<P>>
1549where
1550    P: AsRef<Path>,
1551    T: FnMut(&Path) -> Result<()>,
1552    F: FnMut(&Path) -> Result<()>,
1553{
1554    let path_ref = path.as_ref();
1555    if !path_ref.exists() {
1556        action(path_ref)?;
1557        Ok(PathState::Missing(path))
1558    } else {
1559        if allow_update {
1560            else_action(path_ref)?;
1561        }
1562        Ok(PathState::Exists(path))
1563    }
1564}