langs.rs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //! Grammar registry: codegraph `Language` string → native tree-sitter grammar.
  2. //!
  3. //! Mirrors the wasm side's `WASM_GRAMMAR_FILES` mapping (src/extraction/
  4. //! grammars.ts): `tsx` and `jsx` reuse another language's grammar exactly the
  5. //! way the wasm map does. The kernel-grammar-parity test asserts each entry is
  6. //! built from the SAME grammar revision as the vendored wasm — bump the crate
  7. //! and the wasm together.
  8. //!
  9. //! (R1 shipped a generic `.scm`-query emitter here; R2 replaced it with the
  10. //! bespoke per-language walker — see tsjs/ and the migration plan §3a — because
  11. //! extraction parity needs logic queries can't express. New languages add a
  12. //! grammar entry + a walker module.)
  13. use tree_sitter::Language;
  14. // Vendored kotlin grammar (build.rs-compiled C — no usable crate exists;
  15. // see grammars/kotlin and the kotlin checklist §Grammar prep).
  16. extern "C" {
  17. fn tree_sitter_kotlin() -> *const ();
  18. }
  19. /// Languages this kernel binary can extract (reported by contractInfo;
  20. /// TS-side routing policy decides what actually routes).
  21. pub const LANGUAGES: [&str; 16] = [
  22. "typescript", "tsx", "javascript", "jsx", "java", "python", "go", "c", "cpp", "rust",
  23. "csharp", "ruby", "php", "swift", "kotlin", "r",
  24. ];
  25. pub fn grammar_for(language: &str) -> Option<Language> {
  26. match language {
  27. "typescript" => Some(tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into()),
  28. "tsx" => Some(tree_sitter_typescript::LANGUAGE_TSX.into()),
  29. "javascript" | "jsx" => Some(tree_sitter_javascript::LANGUAGE.into()),
  30. "java" => Some(tree_sitter_java::LANGUAGE.into()),
  31. "python" => Some(tree_sitter_python::LANGUAGE.into()),
  32. "go" => Some(tree_sitter_go::LANGUAGE.into()),
  33. // `.metal`/`.cu`/`.cuh` map to language 'cpp' at detectLanguage, so the
  34. // dialects ride this grammar too (their blanking pre-passes stay
  35. // TS-side — the route point applies preParse before the kernel call).
  36. "c" => Some(tree_sitter_c::LANGUAGE.into()),
  37. "cpp" => Some(tree_sitter_cpp::LANGUAGE.into()),
  38. // R7b: v0.24.2, sha-matched with the vendored wasm (grammars.ts).
  39. "rust" => Some(tree_sitter_rust::LANGUAGE.into()),
  40. // R7b: 0.23.5, table-identical to the vendored ABI-15 wasm (#717;
  41. // verified against the crates.io tarball — csharp checklist header).
  42. "csharp" => Some(tree_sitter_c_sharp::LANGUAGE.into()),
  43. // R7b: v0.23.1, sha-matched with the vendored wasm (grammars.ts).
  44. // Content bump only — the tag's parser.c is still ABI 14.
  45. "ruby" => Some(tree_sitter_ruby::LANGUAGE.into()),
  46. // R7b: v0.24.2, the full HTML-interleaving variant — LANGUAGE_PHP,
  47. // NEVER LANGUAGE_PHP_ONLY (which errors on leading HTML).
  48. "php" => Some(tree_sitter_php::LANGUAGE_PHP.into()),
  49. // R7b: crate 0.7.3 — the vendored wasm is built from this crate's own
  50. // tarball src/ (table identity by construction; see grammars.ts).
  51. "swift" => Some(tree_sitter_swift::LANGUAGE.into()),
  52. // R7b: fwcd 0.3.8, vendored C compiled in build.rs (crate unusable).
  53. "kotlin" => {
  54. Some(unsafe { tree_sitter_language::LanguageFn::from_raw(tree_sitter_kotlin) }.into())
  55. }
  56. // R7b batch 4: crate =1.2.0, sha-identical to the r-lib v1.2.0 tag
  57. // the vendored wasm was built from (r checklist §Grammar prep).
  58. "r" => Some(tree_sitter_r::LANGUAGE.into()),
  59. _ => None,
  60. }
  61. }