[OCaml] Mobile-friendly clone of cgit.
1
(** Registry of bundled TextMate grammars for syntax highlighting.
2
3
Loads grammars embedded at build time via [ocaml-crunch] and registers them
4
with a shared {!TmLanguage.t} instance. Language lookup is by the canonical
5
name string produced by {!Detect.detect}.
6
7
The {!languages} table is the single source of truth for which grammars are
8
shipped and what scope each language maps to. Both the registry loader and
9
{!scope_of_lang} derive from it. *)
10
11
(** Each entry is [(grammar_file, scope_name, aliases)] where [aliases] includes
12
the canonical name and any accepted variants. *)
13
let languages =
14
[
15
("c", "source.c", [ "c" ]);
16
("clojure", "source.clojure", [ "clojure" ]);
17
("cpp", "source.cpp", [ "cpp" ]);
18
("csharp", "source.cs", [ "csharp" ]);
19
("css", "source.css", [ "css" ]);
20
("dart", "source.dart", [ "dart" ]);
21
("diff", "source.diff", [ "diff" ]);
22
("dockerfile", "source.dockerfile", [ "dockerfile" ]);
23
("dune", "source.dune", [ "dune" ]);
24
("elixir", "source.elixir", [ "elixir" ]);
25
("erlang", "source.erlang", [ "erlang" ]);
26
("fortran", "source.fortran.free", [ "fortran" ]);
27
("go", "source.go", [ "go" ]);
28
("groovy", "source.groovy", [ "groovy" ]);
29
("haskell", "source.haskell", [ "haskell" ]);
30
("html", "text.html.basic", [ "html" ]);
31
("java", "source.java", [ "java" ]);
32
("javascript", "source.js", [ "javascript" ]);
33
("json", "source.json", [ "json" ]);
34
("julia", "source.julia", [ "julia" ]);
35
("kotlin", "source.kotlin", [ "kotlin" ]);
36
("lisp", "source.lisp", [ "lisp" ]);
37
("lua", "source.lua", [ "lua" ]);
38
("makefile", "source.makefile", [ "makefile" ]);
39
("markdown", "text.html.markdown", [ "markdown" ]);
40
("nix", "source.nix", [ "nix" ]);
41
("objective-c", "source.objc", [ "objective-c"; "objc" ]);
42
("ocaml", "source.ocaml", [ "ocaml" ]);
43
("ocamldoc", "source.ocaml.ocamldoc", [ "ocamldoc"; "mld" ]);
44
("opam", "source.ocaml.opam", [ "opam" ]);
45
("org", "source.org", [ "org" ]);
46
("perl", "source.perl", [ "perl" ]);
47
("php", "source.php", [ "php" ]);
48
("powershell", "source.powershell", [ "powershell" ]);
49
("protobuf", "source.proto", [ "protobuf"; "proto" ]);
50
("python", "source.python", [ "python" ]);
51
("r", "source.r", [ "r" ]);
52
("ruby", "source.ruby", [ "ruby" ]);
53
("rust", "source.rust", [ "rust" ]);
54
("scala", "source.scala", [ "scala" ]);
55
("shell", "source.shell", [ "bash"; "shell"; "sh" ]);
56
("sql", "source.sql", [ "sql" ]);
57
("swift", "source.swift", [ "swift" ]);
58
("terraform", "source.hcl.terraform", [ "terraform"; "hcl" ]);
59
("toml", "source.toml", [ "toml"; "ini" ]);
60
("typescript", "source.ts", [ "typescript" ]);
61
("vue", "text.html.vue", [ "vue" ]);
62
("xml", "text.html.basic", [ "xml" ]);
63
("yaml", "source.yaml", [ "yaml" ]);
64
("zig", "source.zig", [ "zig" ]);
65
]
66
67
(** The shared grammar registry, lazily initialised. *)
68
let registry : TmLanguage.t Lazy.t =
69
lazy
70
(let t = TmLanguage.create () in
71
let load filename =
72
match Grammar_data.read filename with
73
| None -> ()
74
| Some data -> (
75
try
76
let json = Yojson.Basic.from_string data in
77
let grammar = TmLanguage.of_yojson_exn json in
78
TmLanguage.add_grammar t grammar
79
with _ -> ())
80
in
81
List.iter (fun (name, _, _) -> load (name ^ ".json")) languages;
82
t)
83
84
(** Scope lookup table, built once from {!languages}. *)
85
let scope_table : (string, string) Hashtbl.t =
86
let tbl = Hashtbl.create 128 in
87
List.iter
88
(fun (_, scope, aliases) ->
89
List.iter (fun alias -> Hashtbl.replace tbl alias scope) aliases)
90
languages;
91
tbl
92
93
(** Map from the canonical language name (as returned by {!Detect.detect}) to
94
the scopeName used by the grammar file. *)
95
let scope_of_lang name = Hashtbl.find_opt scope_table name
96