langs.rs 5.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. // Vendored lua grammar (build.rs-compiled C — the wasm's v0.4.1 revision is
  20. // not on crates.io; see grammars/lua and the lua-luau checklist).
  21. extern "C" {
  22. fn tree_sitter_lua() -> *const ();
  23. }
  24. // Vendored scala grammar (build.rs-compiled C — the wasm is master@0aca5d0a6f,
  25. // 30 states past the v0.26.0 crate; see grammars/scala and the scala checklist).
  26. extern "C" {
  27. fn tree_sitter_scala() -> *const ();
  28. }
  29. // Vendored dart grammar (build.rs-compiled C — UserNobody14 d4d8f3e; the
  30. // crates.io crate is a different-lineage fork; see grammars/dart).
  31. extern "C" {
  32. fn tree_sitter_dart() -> *const ();
  33. }
  34. /// Languages this kernel binary can extract (reported by contractInfo;
  35. /// TS-side routing policy decides what actually routes).
  36. pub const LANGUAGES: [&str; 20] = [
  37. "typescript", "tsx", "javascript", "jsx", "java", "python", "go", "c", "cpp", "rust",
  38. "csharp", "ruby", "php", "swift", "kotlin", "r", "lua", "luau", "scala", "dart",
  39. ];
  40. pub fn grammar_for(language: &str) -> Option<Language> {
  41. match language {
  42. "typescript" => Some(tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into()),
  43. "tsx" => Some(tree_sitter_typescript::LANGUAGE_TSX.into()),
  44. "javascript" | "jsx" => Some(tree_sitter_javascript::LANGUAGE.into()),
  45. "java" => Some(tree_sitter_java::LANGUAGE.into()),
  46. "python" => Some(tree_sitter_python::LANGUAGE.into()),
  47. "go" => Some(tree_sitter_go::LANGUAGE.into()),
  48. // `.metal`/`.cu`/`.cuh` map to language 'cpp' at detectLanguage, so the
  49. // dialects ride this grammar too (their blanking pre-passes stay
  50. // TS-side — the route point applies preParse before the kernel call).
  51. "c" => Some(tree_sitter_c::LANGUAGE.into()),
  52. "cpp" => Some(tree_sitter_cpp::LANGUAGE.into()),
  53. // R7b: v0.24.2, sha-matched with the vendored wasm (grammars.ts).
  54. "rust" => Some(tree_sitter_rust::LANGUAGE.into()),
  55. // R7b: 0.23.5, table-identical to the vendored ABI-15 wasm (#717;
  56. // verified against the crates.io tarball — csharp checklist header).
  57. "csharp" => Some(tree_sitter_c_sharp::LANGUAGE.into()),
  58. // R7b: v0.23.1, sha-matched with the vendored wasm (grammars.ts).
  59. // Content bump only — the tag's parser.c is still ABI 14.
  60. "ruby" => Some(tree_sitter_ruby::LANGUAGE.into()),
  61. // R7b: v0.24.2, the full HTML-interleaving variant — LANGUAGE_PHP,
  62. // NEVER LANGUAGE_PHP_ONLY (which errors on leading HTML).
  63. "php" => Some(tree_sitter_php::LANGUAGE_PHP.into()),
  64. // R7b: crate 0.7.3 — the vendored wasm is built from this crate's own
  65. // tarball src/ (table identity by construction; see grammars.ts).
  66. "swift" => Some(tree_sitter_swift::LANGUAGE.into()),
  67. // R7b: fwcd 0.3.8, vendored C compiled in build.rs (crate unusable).
  68. "kotlin" => {
  69. Some(unsafe { tree_sitter_language::LanguageFn::from_raw(tree_sitter_kotlin) }.into())
  70. }
  71. // R7b batch 4: crate =1.2.0, sha-identical to the r-lib v1.2.0 tag
  72. // the vendored wasm was built from (r checklist §Grammar prep).
  73. "r" => Some(tree_sitter_r::LANGUAGE.into()),
  74. // R7b batch 4: v0.4.1 vendored C compiled in build.rs (revision not
  75. // on crates.io — the wasm is the v0.4.1 tag, table-identical).
  76. "lua" => {
  77. Some(unsafe { tree_sitter_language::LanguageFn::from_raw(tree_sitter_lua) }.into())
  78. }
  79. // R7b batch 4: crate =1.2.0, sha-identical to the v1.2.0 tag the
  80. // vendored wasm was built from (lua-luau checklist §Grammar prep).
  81. "luau" => Some(tree_sitter_luau::LANGUAGE.into()),
  82. // R7b batch 4: master@0aca5d0a6f vendored C compiled in build.rs (the
  83. // revision is not a release — crate 0.26.0 is 30 states behind).
  84. "scala" => {
  85. Some(unsafe { tree_sitter_language::LanguageFn::from_raw(tree_sitter_scala) }.into())
  86. }
  87. // R7b batch 4: UserNobody14 d4d8f3e vendored C compiled in build.rs
  88. // (same commit as the byte-copied tree-sitter-wasms 0.1.13 artifact).
  89. "dart" => {
  90. Some(unsafe { tree_sitter_language::LanguageFn::from_raw(tree_sitter_dart) }.into())
  91. }
  92. _ => None,
  93. }
  94. }