normalize-languages 0.3.1

Tree-sitter language support and dynamic grammar loading
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
//! Language support registry with extension-based lookup.

use crate::Language;
use std::collections::HashMap;
use std::path::Path;
use std::sync::{OnceLock, RwLock};

/// Global language registry.
static LANGUAGES: RwLock<Vec<&'static dyn Language>> = RwLock::new(Vec::new());
static INITIALIZED: OnceLock<()> = OnceLock::new();

/// Cached extension → language lookup table.
static EXTENSION_MAP: OnceLock<HashMap<&'static str, &'static dyn Language>> = OnceLock::new();

/// Cached grammar_name → language lookup table.
static GRAMMAR_MAP: OnceLock<HashMap<&'static str, &'static dyn Language>> = OnceLock::new();

/// Register a language in the global registry.
/// Called internally by language modules.
pub fn register(lang: &'static dyn Language) {
    LANGUAGES
        .write()
        .unwrap_or_else(|e| e.into_inner())
        .push(lang);
}

/// Initialize built-in languages (called once).
fn init_builtin() {
    INITIALIZED.get_or_init(|| {
        #[cfg(feature = "lang-python")]
        register(&crate::python::Python);
        #[cfg(feature = "lang-rust")]
        register(&crate::rust::Rust);
        #[cfg(feature = "lang-javascript")]
        register(&crate::javascript::JavaScript);
        #[cfg(feature = "lang-typescript")]
        {
            register(&crate::typescript::TypeScript);
            register(&crate::typescript::Tsx);
        }
        #[cfg(feature = "lang-go")]
        register(&crate::go::Go);
        #[cfg(feature = "lang-java")]
        register(&crate::java::Java);
        #[cfg(feature = "lang-kotlin")]
        register(&crate::kotlin::Kotlin);
        #[cfg(feature = "lang-csharp")]
        register(&crate::csharp::CSharp);
        #[cfg(feature = "lang-swift")]
        register(&crate::swift::Swift);
        #[cfg(feature = "lang-php")]
        register(&crate::php::Php);
        #[cfg(feature = "lang-dockerfile")]
        register(&crate::dockerfile::Dockerfile);
        #[cfg(feature = "lang-c")]
        register(&crate::c::C);
        #[cfg(feature = "lang-cpp")]
        register(&crate::cpp::Cpp);
        #[cfg(feature = "lang-ruby")]
        register(&crate::ruby::Ruby);
        #[cfg(feature = "lang-scala")]
        register(&crate::scala::Scala);
        #[cfg(feature = "lang-vue")]
        register(&crate::vue::Vue);
        #[cfg(feature = "lang-markdown")]
        register(&crate::markdown::Markdown);
        #[cfg(feature = "lang-json")]
        register(&crate::json::Json);
        #[cfg(feature = "lang-yaml")]
        register(&crate::yaml::Yaml);
        #[cfg(feature = "lang-toml")]
        register(&crate::toml::Toml);
        #[cfg(feature = "lang-html")]
        register(&crate::html::Html);
        #[cfg(feature = "lang-css")]
        register(&crate::css::Css);
        #[cfg(feature = "lang-bash")]
        register(&crate::bash::Bash);
        #[cfg(feature = "lang-lua")]
        register(&crate::lua::Lua);
        #[cfg(feature = "lang-zig")]
        register(&crate::zig::Zig);
        #[cfg(feature = "lang-elixir")]
        register(&crate::elixir::Elixir);
        #[cfg(feature = "lang-erlang")]
        register(&crate::erlang::Erlang);
        #[cfg(feature = "lang-dart")]
        register(&crate::dart::Dart);
        #[cfg(feature = "lang-fsharp")]
        register(&crate::fsharp::FSharp);
        #[cfg(feature = "lang-sql")]
        register(&crate::sql::Sql);
        #[cfg(feature = "lang-graphql")]
        register(&crate::graphql::GraphQL);
        #[cfg(feature = "lang-hcl")]
        register(&crate::hcl::Hcl);
        #[cfg(feature = "lang-scss")]
        register(&crate::scss::Scss);
        #[cfg(feature = "lang-svelte")]
        register(&crate::svelte::Svelte);
        #[cfg(feature = "lang-xml")]
        register(&crate::xml::Xml);
        #[cfg(feature = "lang-clojure")]
        register(&crate::clojure::Clojure);
        #[cfg(feature = "lang-haskell")]
        register(&crate::haskell::Haskell);
        #[cfg(feature = "lang-ocaml")]
        register(&crate::ocaml::OCaml);
        #[cfg(feature = "lang-nix")]
        register(&crate::nix::Nix);
        #[cfg(feature = "lang-perl")]
        register(&crate::perl::Perl);
        #[cfg(feature = "lang-r")]
        register(&crate::r::R);
        #[cfg(feature = "lang-julia")]
        register(&crate::julia::Julia);
        #[cfg(feature = "lang-elm")]
        register(&crate::elm::Elm);
        #[cfg(feature = "lang-cmake")]
        register(&crate::cmake::CMake);
        #[cfg(feature = "lang-vim")]
        register(&crate::vim::Vim);
        #[cfg(feature = "lang-awk")]
        register(&crate::awk::Awk);
        #[cfg(feature = "lang-fish")]
        register(&crate::fish::Fish);
        #[cfg(feature = "lang-jq")]
        register(&crate::jq::Jq);
        #[cfg(feature = "lang-powershell")]
        register(&crate::powershell::PowerShell);
        #[cfg(feature = "lang-zsh")]
        register(&crate::zsh::Zsh);
        #[cfg(feature = "lang-groovy")]
        register(&crate::groovy::Groovy);
        #[cfg(feature = "lang-glsl")]
        register(&crate::glsl::Glsl);
        #[cfg(feature = "lang-hlsl")]
        register(&crate::hlsl::Hlsl);
        #[cfg(feature = "lang-commonlisp")]
        register(&crate::commonlisp::CommonLisp);
        #[cfg(feature = "lang-elisp")]
        register(&crate::elisp::Elisp);
        #[cfg(feature = "lang-gleam")]
        register(&crate::gleam::Gleam);
        #[cfg(feature = "lang-ini")]
        register(&crate::ini::Ini);
        #[cfg(feature = "lang-diff")]
        register(&crate::diff::Diff);
        #[cfg(feature = "lang-dot")]
        register(&crate::dot::Dot);
        #[cfg(feature = "lang-kdl")]
        register(&crate::kdl::Kdl);
        #[cfg(feature = "lang-ada")]
        register(&crate::ada::Ada);
        #[cfg(feature = "lang-agda")]
        register(&crate::agda::Agda);
        #[cfg(feature = "lang-d")]
        register(&crate::d::D);
        #[cfg(feature = "lang-matlab")]
        register(&crate::matlab::Matlab);
        #[cfg(feature = "lang-meson")]
        register(&crate::meson::Meson);
        #[cfg(feature = "lang-nginx")]
        register(&crate::nginx::Nginx);
        #[cfg(feature = "lang-prolog")]
        register(&crate::prolog::Prolog);
        #[cfg(feature = "lang-batch")]
        register(&crate::batch::Batch);
        #[cfg(feature = "lang-asm")]
        register(&crate::asm::Asm);
        #[cfg(feature = "lang-objc")]
        register(&crate::objc::ObjC);
        #[cfg(feature = "lang-typst")]
        register(&crate::typst::Typst);
        #[cfg(feature = "lang-asciidoc")]
        register(&crate::asciidoc::AsciiDoc);
        #[cfg(feature = "lang-vb")]
        register(&crate::vb::VB);
        #[cfg(feature = "lang-idris")]
        register(&crate::idris::Idris);
        #[cfg(feature = "lang-rescript")]
        register(&crate::rescript::ReScript);
        #[cfg(feature = "lang-lean")]
        register(&crate::lean::Lean);
        #[cfg(feature = "lang-caddy")]
        register(&crate::caddy::Caddy);
        #[cfg(feature = "lang-capnp")]
        register(&crate::capnp::Capnp);
        #[cfg(feature = "lang-devicetree")]
        register(&crate::devicetree::DeviceTree);
        #[cfg(feature = "lang-jinja2")]
        register(&crate::jinja2::Jinja2);
        #[cfg(feature = "lang-ninja")]
        register(&crate::ninja::Ninja);
        #[cfg(feature = "lang-postscript")]
        register(&crate::postscript::PostScript);
        #[cfg(feature = "lang-query")]
        register(&crate::query::Query);
        // Scheme registered after Query so .scm → Scheme (not Query) in extension_map
        #[cfg(feature = "lang-scheme")]
        register(&crate::scheme::Scheme);
        #[cfg(feature = "lang-ron")]
        register(&crate::ron::Ron);
        #[cfg(feature = "lang-sparql")]
        register(&crate::sparql::Sparql);
        #[cfg(feature = "lang-sshconfig")]
        register(&crate::sshconfig::SshConfig);
        #[cfg(feature = "lang-starlark")]
        register(&crate::starlark::Starlark);
        #[cfg(feature = "lang-textproto")]
        register(&crate::textproto::TextProto);
        #[cfg(feature = "lang-thrift")]
        register(&crate::thrift::Thrift);
        #[cfg(feature = "lang-tlaplus")]
        register(&crate::tlaplus::TlaPlus);
        #[cfg(feature = "lang-uiua")]
        register(&crate::uiua::Uiua);
        #[cfg(feature = "lang-verilog")]
        register(&crate::verilog::Verilog);
        #[cfg(feature = "lang-vhdl")]
        register(&crate::vhdl::Vhdl);
        #[cfg(feature = "lang-wit")]
        register(&crate::wit::Wit);
        #[cfg(feature = "lang-x86asm")]
        register(&crate::x86asm::X86Asm);
        #[cfg(feature = "lang-yuri")]
        register(&crate::yuri::Yuri);
    });
}

fn extension_map() -> &'static HashMap<&'static str, &'static dyn Language> {
    init_builtin();
    EXTENSION_MAP.get_or_init(|| {
        let mut map = HashMap::new();
        let langs = LANGUAGES.read().unwrap_or_else(|e| e.into_inner());
        for lang in langs.iter() {
            for ext in lang.extensions() {
                map.insert(*ext, *lang);
            }
        }
        map
    })
}

fn grammar_map() -> &'static HashMap<&'static str, &'static dyn Language> {
    init_builtin();
    GRAMMAR_MAP.get_or_init(|| {
        let mut map = HashMap::new();
        let langs = LANGUAGES.read().unwrap_or_else(|e| e.into_inner());
        for lang in langs.iter() {
            map.insert(lang.grammar_name(), *lang);
        }
        map
    })
}

/// Get language support for a file extension.
///
/// Returns `None` if the extension is not recognized or the feature is not enabled.
pub fn support_for_extension(ext: &str) -> Option<&'static dyn Language> {
    extension_map()
        .get(ext)
        .or_else(|| extension_map().get(ext.to_lowercase().as_str()))
        .copied()
}

/// Get language support by grammar name.
///
/// Returns `None` if the grammar is not recognized or the feature is not enabled.
pub fn support_for_grammar(grammar: &str) -> Option<&'static dyn Language> {
    grammar_map().get(grammar).copied()
}

/// Get language support from a file path.
///
/// Returns `None` if the file has no extension, the extension is not recognized,
/// or the feature is not enabled.
pub fn support_for_path(path: &Path) -> Option<&'static dyn Language> {
    path.extension()
        .and_then(|e| e.to_str())
        .and_then(support_for_extension)
}

/// Check if a file path is a dedicated test file for its language.
///
/// Returns false for unknown file types or languages that use inline tests.
/// Matches against the language's `test_file_globs()` patterns.
pub fn is_test_path(path: &Path) -> bool {
    let lang = match support_for_path(path) {
        Some(l) => l,
        None => return false,
    };
    let globs = lang.test_file_globs();
    if globs.is_empty() {
        return false;
    }
    let mut builder = globset::GlobSetBuilder::new();
    for g in globs {
        if let Ok(glob) = globset::Glob::new(g) {
            builder.add(glob);
        }
    }
    let Ok(set) = builder.build() else {
        return false;
    };
    set.is_match(path)
}

/// Get all glob patterns that identify test files for a given language extension.
pub fn test_file_globs_for_path(path: &Path) -> &'static [&'static str] {
    support_for_path(path)
        .map(|lang| lang.test_file_globs())
        .unwrap_or(&[])
}

/// Get all supported languages.
pub fn supported_languages() -> Vec<&'static dyn Language> {
    init_builtin();
    LANGUAGES.read().unwrap_or_else(|e| e.into_inner()).clone()
}

/// Check if a path is a programming language (not a data/config format).
///
/// Returns false for data formats like JSON, YAML, TOML, Markdown, etc.
/// even though normalize-languages can parse them for syntax highlighting.
///
/// Useful for architecture analysis where only "code" files are relevant.
/// Uses `normalize_language_meta::capabilities_for()` to determine if a
/// language is executable code.
pub fn is_programming_language(path: &Path) -> bool {
    let lang = match support_for_path(path) {
        Some(l) => l,
        None => return false,
    };

    let caps = normalize_language_meta::capabilities_for(lang.name());
    caps.executable
}

/// Validate that a language's unused node kinds audit is complete and accurate.
///
/// This function checks:
/// 1. All kinds in `documented_unused` actually exist in the grammar
/// 2. All potentially useful kinds from the grammar are either used or documented
///
/// Call this from each language's `unused_node_kinds_audit` test.
pub fn validate_unused_kinds_audit(
    lang: &dyn Language,
    documented_unused: &[&str],
) -> Result<(), String> {
    use crate::GrammarLoader;
    use crate::grammar_loader::GrammarLoadError;
    use std::collections::HashSet;

    let loader = GrammarLoader::new();
    let ts_lang = match loader.get(lang.grammar_name()) {
        Ok(l) => l,
        // Grammar `.so` not present — typical in `cargo test` without
        // `cargo xtask build-grammars`. Skip the audit instead of panicking.
        // Real audit failures (loaded but mismatched) still surface.
        Err(GrammarLoadError::NotFound(_)) => return Ok(()),
        Err(e) => return Err(format!("Grammar '{}' not found: {e}", lang.grammar_name())),
    };

    // Keywords that suggest a node kind might be useful (same as cross_check_node_kinds)
    let interesting_patterns = [
        "statement",
        "expression",
        "definition",
        "declaration",
        "clause",
        "block",
        "body",
        "import",
        "export",
        "function",
        "method",
        "class",
        "struct",
        "enum",
        "interface",
        "trait",
        "module",
        "type",
        "return",
        "if",
        "else",
        "for",
        "while",
        "loop",
        "match",
        "case",
        "try",
        "catch",
        "except",
        "throw",
        "raise",
        "with",
        "async",
        "await",
        "yield",
        "lambda",
        "comprehension",
        "generator",
        "operator",
    ];

    // Collect kinds referenced in tags.scm
    let tags_kinds: HashSet<String> = {
        let mut kinds = HashSet::new();
        if let Some(tags_content) = loader.get_tags(lang.grammar_name()) {
            // Extract top-level node kind names: lines starting with "(<identifier>"
            // These are the patterns like "(function_definition ..." in the query
            for line in tags_content.lines() {
                let trimmed = line.trim_start();
                if trimmed.starts_with('(')
                    && !trimmed.starts_with(";;")
                    && !trimmed.starts_with(";")
                {
                    // Extract the first word after the opening paren
                    let inner = &trimmed[1..];
                    let kind_name: String = inner
                        .chars()
                        .take_while(|c| c.is_alphanumeric() || *c == '_' || *c == '-')
                        .collect();
                    if !kind_name.is_empty() && !kind_name.starts_with('@') {
                        kinds.insert(kind_name);
                    }
                }
            }
        }
        kinds
    };

    let documented_set: HashSet<&str> = documented_unused.iter().copied().collect();

    // Get all valid named node kinds from grammar
    let mut grammar_kinds: HashSet<&str> = HashSet::new();
    let count = ts_lang.node_kind_count();
    for id in 0..count as u16 {
        if let Some(kind) = ts_lang.node_kind_for_id(id) {
            let named = ts_lang.node_kind_is_named(id);
            if named && !kind.starts_with('_') {
                grammar_kinds.insert(kind);
            }
        }
    }

    let mut errors: Vec<String> = Vec::new();

    // Check 1: All documented unused kinds must exist in grammar
    for kind in documented_unused {
        if !grammar_kinds.contains(*kind) {
            errors.push(format!(
                "Documented kind '{}' doesn't exist in grammar",
                kind
            ));
        }
        // Also check it's not actually being used (in tags.scm)
        if tags_kinds.contains(*kind) {
            errors.push(format!(
                "Documented kind '{}' is actually used in tags.scm",
                kind
            ));
        }
    }

    // Check 2: All potentially useful grammar kinds must be used or documented
    for kind in &grammar_kinds {
        let lower = kind.to_lowercase();
        let is_interesting = interesting_patterns.iter().any(|p| lower.contains(p));

        if is_interesting && !tags_kinds.contains(*kind) && !documented_set.contains(*kind) {
            errors.push(format!(
                "Potentially useful kind '{}' is neither used nor documented",
                kind
            ));
        }
    }

    if errors.is_empty() {
        Ok(())
    } else {
        Err(format!(
            "{} validation errors:\n  - {}",
            errors.len(),
            errors.join("\n  - ")
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::GrammarLoader;

    /// Dump all valid node kinds for a grammar (useful for fixing invalid kinds).
    /// Run with: cargo test -p rhizome-normalize-languages dump_node_kinds -- --nocapture
    #[test]
    #[ignore]
    fn dump_node_kinds() {
        let loader = GrammarLoader::new();
        // Change this to the grammar you want to inspect
        let grammar_name = std::env::var("DUMP_GRAMMAR").unwrap_or_else(|_| "python".to_string());

        let ts_lang = loader.get(&grammar_name).expect("grammar not found");

        println!("\n=== Valid node kinds for '{}' ===\n", grammar_name);
        let count = ts_lang.node_kind_count();
        for id in 0..count as u16 {
            if let Some(kind) = ts_lang.node_kind_for_id(id) {
                let named = ts_lang.node_kind_is_named(id);
                if named && !kind.starts_with('_') {
                    println!("{}", kind);
                }
            }
        }
    }

    /// Validate that all node kinds returned by Language trait methods
    /// actually exist in the tree-sitter grammar.
    ///
    /// No trait methods return node kind lists any more —
    /// export detection now uses tags.scm queries exclusively.
    /// This test is intentionally empty.
    #[test]
    fn validate_node_kinds() {
        // Nothing to validate — node kind lists were removed from the Language trait.
    }

    /// Cross-check grammar node kinds against Language implementations.
    /// Finds potentially useful kinds that exist in the grammar but aren't used.
    /// Run with: cargo test -p rhizome-normalize-languages cross_check_node_kinds -- --nocapture --ignored
    #[test]
    #[ignore]
    fn cross_check_node_kinds() {
        use std::collections::HashSet;

        let loader = GrammarLoader::new();

        // Keywords that suggest a node kind might be useful
        let interesting_patterns = [
            "statement",
            "expression",
            "definition",
            "declaration",
            "clause",
            "block",
            "body",
            "import",
            "export",
            "function",
            "method",
            "class",
            "struct",
            "enum",
            "interface",
            "trait",
            "module",
            "type",
            "return",
            "if",
            "else",
            "for",
            "while",
            "loop",
            "match",
            "case",
            "try",
            "catch",
            "except",
            "throw",
            "raise",
            "with",
            "async",
            "await",
            "yield",
            "lambda",
            "comprehension",
            "generator",
            "operator",
        ];

        for lang in supported_languages() {
            let grammar_name = lang.grammar_name();
            let ts_lang = match loader.get(grammar_name).ok() {
                Some(l) => l,
                None => continue,
            };

            // Collect all kinds currently used by the language
            // public_symbol_kinds() removed — export detection uses tags.scm exclusively.
            let used_kinds: HashSet<&str> = HashSet::new();

            // Get all valid named node kinds from grammar
            let mut all_kinds: Vec<&str> = Vec::new();
            let count = ts_lang.node_kind_count();
            for id in 0..count as u16 {
                if let Some(kind) = ts_lang.node_kind_for_id(id) {
                    let named = ts_lang.node_kind_is_named(id);
                    if named && !kind.starts_with('_') {
                        all_kinds.push(kind);
                    }
                }
            }

            // Find unused but potentially interesting kinds
            let mut unused_interesting: Vec<&str> = all_kinds
                .into_iter()
                .filter(|kind| !used_kinds.contains(*kind))
                .filter(|kind| {
                    let lower = kind.to_lowercase();
                    interesting_patterns.iter().any(|p| lower.contains(p))
                })
                .collect();

            unused_interesting.sort();

            if !unused_interesting.is_empty() {
                println!(
                    "\n=== {} ({}) - {} potentially useful unused kinds ===",
                    lang.name(),
                    grammar_name,
                    unused_interesting.len()
                );
                for kind in &unused_interesting {
                    println!("  {}", kind);
                }
            }
        }
    }
}