(** Registry of bundled TextMate grammars for syntax highlighting. Loads grammars embedded at build time via [ocaml-crunch] and registers them with a shared {!TmLanguage.t} instance. Language lookup is by the canonical name string produced by {!Detect.detect}. The {!languages} table is the single source of truth for which grammars are shipped and what scope each language maps to. Both the registry loader and {!scope_of_lang} derive from it. *) (** Each entry is [(grammar_file, scope_name, aliases)] where [aliases] includes the canonical name and any accepted variants. *) let languages = [ ("c", "source.c", [ "c" ]); ("clojure", "source.clojure", [ "clojure" ]); ("cpp", "source.cpp", [ "cpp" ]); ("csharp", "source.cs", [ "csharp" ]); ("css", "source.css", [ "css" ]); ("dart", "source.dart", [ "dart" ]); ("diff", "source.diff", [ "diff" ]); ("dockerfile", "source.dockerfile", [ "dockerfile" ]); ("dune", "source.dune", [ "dune" ]); ("elixir", "source.elixir", [ "elixir" ]); ("erlang", "source.erlang", [ "erlang" ]); ("fortran", "source.fortran.free", [ "fortran" ]); ("go", "source.go", [ "go" ]); ("groovy", "source.groovy", [ "groovy" ]); ("haskell", "source.haskell", [ "haskell" ]); ("html", "text.html.basic", [ "html" ]); ("java", "source.java", [ "java" ]); ("javascript", "source.js", [ "javascript" ]); ("json", "source.json", [ "json" ]); ("julia", "source.julia", [ "julia" ]); ("kotlin", "source.kotlin", [ "kotlin" ]); ("lisp", "source.lisp", [ "lisp" ]); ("lua", "source.lua", [ "lua" ]); ("makefile", "source.makefile", [ "makefile" ]); ("markdown", "text.html.markdown", [ "markdown" ]); ("nix", "source.nix", [ "nix" ]); ("objective-c", "source.objc", [ "objective-c"; "objc" ]); ("ocaml", "source.ocaml", [ "ocaml" ]); ("ocamldoc", "source.ocaml.ocamldoc", [ "ocamldoc"; "mld" ]); ("opam", "source.ocaml.opam", [ "opam" ]); ("org", "source.org", [ "org" ]); ("perl", "source.perl", [ "perl" ]); ("php", "source.php", [ "php" ]); ("powershell", "source.powershell", [ "powershell" ]); ("protobuf", "source.proto", [ "protobuf"; "proto" ]); ("python", "source.python", [ "python" ]); ("r", "source.r", [ "r" ]); ("ruby", "source.ruby", [ "ruby" ]); ("rust", "source.rust", [ "rust" ]); ("scala", "source.scala", [ "scala" ]); ("shell", "source.shell", [ "bash"; "shell"; "sh" ]); ("sql", "source.sql", [ "sql" ]); ("swift", "source.swift", [ "swift" ]); ("terraform", "source.hcl.terraform", [ "terraform"; "hcl" ]); ("toml", "source.toml", [ "toml"; "ini" ]); ("typescript", "source.ts", [ "typescript" ]); ("vue", "text.html.vue", [ "vue" ]); ("xml", "text.html.basic", [ "xml" ]); ("yaml", "source.yaml", [ "yaml" ]); ("zig", "source.zig", [ "zig" ]); ] (** The shared grammar registry, lazily initialised. *) let registry : TmLanguage.t Lazy.t = lazy (let t = TmLanguage.create () in let load filename = match Grammar_data.read filename with | None -> () | Some data -> ( try let json = Yojson.Basic.from_string data in let grammar = TmLanguage.of_yojson_exn json in TmLanguage.add_grammar t grammar with _ -> ()) in List.iter (fun (name, _, _) -> load (name ^ ".json")) languages; t) (** Scope lookup table, built once from {!languages}. *) let scope_table : (string, string) Hashtbl.t = let tbl = Hashtbl.create 128 in List.iter (fun (_, scope, aliases) -> List.iter (fun alias -> Hashtbl.replace tbl alias scope) aliases) languages; tbl (** Map from the canonical language name (as returned by {!Detect.detect}) to the scopeName used by the grammar file. *) let scope_of_lang name = Hashtbl.find_opt scope_table name