1use std::sync::OnceLock;
7
8use crate::table::Language;
9
10#[derive(Clone, Copy, PartialEq, Eq, Debug)]
12pub enum Lang {
13 C,
14 Cpp,
15 CSharp,
16 Go,
17 Java,
18 JavaScript,
19 TypeScript,
20 Tsx,
21 Php,
22 Python,
23 Rust,
24 Lua,
25 Bash,
26 Ruby,
27 Kotlin,
28 Css,
29 Html,
30 ObjC,
31 Dart,
32 Julia,
33 Nix,
34 Dockerfile,
35 Ocaml,
36 Elixir,
37 R,
38 Svelte,
39 Solidity,
40 Groovy,
41 Make,
42 Scheme,
43 CommonLisp,
44 Pascal,
45 Gleam,
46 PowerShell,
47 CMake,
48 Erlang,
49 Hcl,
50 Vue,
51 D,
52 Elm,
53 Verilog,
54}
55
56const COUNT: usize = 41;
57
58impl Lang {
59 pub fn from_tag(tag: &str) -> Option<Lang> {
61 Some(match tag {
62 "c" => Lang::C,
63 "cpp" => Lang::Cpp,
64 "csharp" => Lang::CSharp,
65 "go" => Lang::Go,
66 "java" => Lang::Java,
67 "javascript" | "jsx" => Lang::JavaScript,
68 "typescript" => Lang::TypeScript,
69 "tsx" => Lang::Tsx,
70 "php" => Lang::Php,
71 "python" => Lang::Python,
72 "rust" => Lang::Rust,
73 "lua" => Lang::Lua,
74 "bash" | "sh" | "shell" | "zsh" => Lang::Bash,
75 "ruby" => Lang::Ruby,
76 "kotlin" => Lang::Kotlin,
77 "css" => Lang::Css,
78 "html" | "htm" => Lang::Html,
79 "objc" | "objective-c" | "objectivec" => Lang::ObjC,
80 "dart" => Lang::Dart,
81 "julia" | "jl" => Lang::Julia,
82 "nix" => Lang::Nix,
83 "dockerfile" => Lang::Dockerfile,
84 "ocaml" | "ml" => Lang::Ocaml,
85 "elixir" | "ex" | "exs" => Lang::Elixir,
86 "r" => Lang::R,
87 "svelte" => Lang::Svelte,
88 "solidity" | "sol" => Lang::Solidity,
89 "groovy" | "gradle" => Lang::Groovy,
90 "make" | "makefile" | "mk" => Lang::Make,
91 "scheme" | "scm" => Lang::Scheme,
92 "commonlisp" | "lisp" | "cl" => Lang::CommonLisp,
93 "pascal" | "pas" | "delphi" => Lang::Pascal,
94 "gleam" => Lang::Gleam,
95 "powershell" | "ps1" | "pwsh" => Lang::PowerShell,
96 "cmake" => Lang::CMake,
97 "erlang" | "erl" | "hrl" => Lang::Erlang,
98 "hcl" | "terraform" | "tf" => Lang::Hcl,
99 "vue" => Lang::Vue,
100 "d" => Lang::D,
101 "elm" => Lang::Elm,
102 "verilog" | "systemverilog" => Lang::Verilog,
103 _ => return None,
104 })
105 }
106
107 fn index(self) -> usize {
108 self as usize
109 }
110
111 fn compressed(self) -> &'static [u8] {
112 match self {
113 Lang::C => include_bytes!("../tables/c.pslg.z"),
114 Lang::Cpp => include_bytes!("../tables/cpp.pslg.z"),
115 Lang::CSharp => include_bytes!("../tables/csharp.pslg.z"),
116 Lang::Go => include_bytes!("../tables/go.pslg.z"),
117 Lang::Java => include_bytes!("../tables/java.pslg.z"),
118 Lang::JavaScript => include_bytes!("../tables/javascript.pslg.z"),
119 Lang::TypeScript => include_bytes!("../tables/typescript.pslg.z"),
120 Lang::Tsx => include_bytes!("../tables/tsx.pslg.z"),
121 Lang::Php => include_bytes!("../tables/php.pslg.z"),
122 Lang::Python => include_bytes!("../tables/python.pslg.z"),
123 Lang::Rust => include_bytes!("../tables/rust.pslg.z"),
124 Lang::Lua => include_bytes!("../tables/lua.pslg.z"),
125 Lang::Bash => include_bytes!("../tables/bash.pslg.z"),
126 Lang::Ruby => include_bytes!("../tables/ruby.pslg.z"),
127 Lang::Kotlin => include_bytes!("../tables/kotlin.pslg.z"),
128 Lang::Css => include_bytes!("../tables/css.pslg.z"),
129 Lang::Html => include_bytes!("../tables/html.pslg.z"),
130 Lang::ObjC => include_bytes!("../tables/objc.pslg.z"),
131 Lang::Dart => include_bytes!("../tables/dart.pslg.z"),
132 Lang::Julia => include_bytes!("../tables/julia.pslg.z"),
133 Lang::Nix => include_bytes!("../tables/nix.pslg.z"),
134 Lang::Dockerfile => include_bytes!("../tables/dockerfile.pslg.z"),
135 Lang::Ocaml => include_bytes!("../tables/ocaml.pslg.z"),
136 Lang::Elixir => include_bytes!("../tables/elixir.pslg.z"),
137 Lang::R => include_bytes!("../tables/r.pslg.z"),
138 Lang::Svelte => include_bytes!("../tables/svelte.pslg.z"),
139 Lang::Solidity => include_bytes!("../tables/solidity.pslg.z"),
140 Lang::Groovy => include_bytes!("../tables/groovy.pslg.z"),
141 Lang::Make => include_bytes!("../tables/make.pslg.z"),
142 Lang::Scheme => include_bytes!("../tables/scheme.pslg.z"),
143 Lang::CommonLisp => include_bytes!("../tables/commonlisp.pslg.z"),
144 Lang::Pascal => include_bytes!("../tables/pascal.pslg.z"),
145 Lang::Gleam => include_bytes!("../tables/gleam.pslg.z"),
146 Lang::PowerShell => include_bytes!("../tables/powershell.pslg.z"),
147 Lang::CMake => include_bytes!("../tables/cmake.pslg.z"),
148 Lang::Erlang => include_bytes!("../tables/erlang.pslg.z"),
149 Lang::Hcl => include_bytes!("../tables/hcl.pslg.z"),
150 Lang::Vue => include_bytes!("../tables/vue.pslg.z"),
151 Lang::D => include_bytes!("../tables/d.pslg.z"),
152 Lang::Elm => include_bytes!("../tables/elm.pslg.z"),
153 Lang::Verilog => include_bytes!("../tables/verilog.pslg.z"),
154 }
155 }
156
157 pub fn table(self) -> Option<&'static Language> {
159 static CACHE: [OnceLock<Option<Language>>; COUNT] = [const { OnceLock::new() }; COUNT];
160 CACHE[self.index()]
161 .get_or_init(|| {
162 let raw = miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(
164 self.compressed(),
165 32 << 20,
166 )
167 .ok()?;
168 Language::from_bytes(&raw).ok()
169 })
170 .as_ref()
171 }
172
173 pub(crate) fn is_c_family(self) -> bool {
175 matches!(self, Lang::C | Lang::Cpp)
176 }
177}
178
179use crate::extract::SymbolKind;
180
181#[derive(Clone, Copy, PartialEq, Eq)]
183pub(crate) enum Cont {
184 No,
185 Type,
186 Scope,
187}
188
189#[derive(Clone, Copy, PartialEq, Eq)]
191pub(crate) enum Sp {
192 GoType,
193 GoImport,
194 JsVar,
195 CDecl,
196 ExCall,
197 RAssign,
198}
199
200#[derive(Clone, Copy)]
201pub(crate) enum Rule {
202 Sym(SymbolKind, &'static str, Cont),
203 Wrap,
205 Flat,
207 Import,
208 Special(Sp),
209 Skip,
210}
211
212use Cont::{No, Scope, Type as Ty};
213use Rule::{Import, Skip, Sym, Wrap};
214use SymbolKind as K;
215
216pub(crate) fn is_leading(kind: &str) -> bool {
218 matches!(
219 kind,
220 "comment" | "line_comment" | "block_comment" | "attribute_item"
221 )
222}
223
224pub(crate) fn rule(lang: Lang, kind: &str) -> Rule {
225 match lang {
226 Lang::Python => match kind {
227 "function_definition" => Sym(K::Function, "function", No),
228 "class_definition" => Sym(K::Class, "class", Ty),
229 "decorated_definition" => Wrap,
230 "import_statement" | "import_from_statement" | "future_import_statement" => Import,
231 _ => Skip,
232 },
233 Lang::Rust => match kind {
234 "function_item" | "function_signature_item" => Sym(K::Function, "function", No),
235 "struct_item" => Sym(K::Struct, "struct", No),
236 "union_item" => Sym(K::Struct, "union", No),
237 "enum_item" => Sym(K::Enum, "enum", No),
238 "trait_item" => Sym(K::Trait, "trait", Ty),
239 "impl_item" => Sym(K::Module, "impl", Ty),
240 "mod_item" => Sym(K::Module, "mod", Scope),
241 "type_item" => Sym(K::Type, "type", No),
242 "use_declaration" => Import,
243 _ => Skip,
244 },
245 Lang::Go => match kind {
246 "function_declaration" => Sym(K::Function, "function", No),
247 "method_declaration" => Sym(K::Method, "method", No),
248 "type_declaration" => Rule::Special(Sp::GoType),
249 "import_declaration" => Rule::Special(Sp::GoImport),
250 _ => Skip,
251 },
252 Lang::Java => match kind {
253 "class_declaration" => Sym(K::Class, "class", Ty),
254 "record_declaration" => Sym(K::Class, "record", Ty),
255 "interface_declaration" => Sym(K::Interface, "interface", Ty),
256 "annotation_type_declaration" => Sym(K::Interface, "interface", Ty),
257 "enum_declaration" => Sym(K::Enum, "enum", Ty),
258 "method_declaration" => Sym(K::Method, "method", No),
259 "constructor_declaration" => Sym(K::Method, "constructor", No),
260 "enum_body_declarations" => Wrap,
261 "import_declaration" => Import,
262 _ => Skip,
263 },
264 Lang::JavaScript | Lang::TypeScript | Lang::Tsx => match kind {
265 "function_declaration" | "generator_function_declaration" => {
266 Sym(K::Function, "function", No)
267 }
268 "class_declaration" | "abstract_class_declaration" => Sym(K::Class, "class", Ty),
269 "method_definition" => Sym(K::Method, "method", No),
270 "interface_declaration" => Sym(K::Interface, "interface", No),
271 "type_alias_declaration" => Sym(K::Type, "type", No),
272 "enum_declaration" => Sym(K::Enum, "enum", No),
273 "internal_module" | "module" => Sym(K::Module, "namespace", Scope),
274 "export_statement" | "ambient_declaration" => Wrap,
275 "lexical_declaration" | "variable_declaration" => Rule::Special(Sp::JsVar),
276 "import_statement" => Import,
277 _ => Skip,
278 },
279 Lang::C | Lang::Cpp => match kind {
280 "function_definition" => Sym(K::Function, "function", No),
281 "struct_specifier" => Sym(K::Struct, "struct", Ty),
282 "class_specifier" => Sym(K::Class, "class", Ty),
283 "enum_specifier" => Sym(K::Enum, "enum", No),
284 "namespace_definition" => Sym(K::Module, "namespace", Scope),
285 "template_declaration" => Wrap,
286 "preproc_if"
287 | "preproc_ifdef"
288 | "preproc_else"
289 | "preproc_elif"
290 | "preproc_elifdef"
291 | "linkage_specification"
292 | "declaration_list" => Rule::Flat,
293 "declaration" | "type_definition" => Rule::Special(Sp::CDecl),
294 "preproc_include" => Import,
295 _ => Skip,
296 },
297 Lang::CSharp => match kind {
298 "class_declaration" => Sym(K::Class, "class", Ty),
299 "record_declaration" => Sym(K::Class, "record", Ty),
300 "struct_declaration" => Sym(K::Struct, "struct", Ty),
301 "interface_declaration" => Sym(K::Interface, "interface", Ty),
302 "enum_declaration" => Sym(K::Enum, "enum", No),
303 "namespace_declaration" => Sym(K::Module, "namespace", Scope),
304 "file_scoped_namespace_declaration" => Sym(K::Module, "namespace", No),
305 "method_declaration" => Sym(K::Method, "method", No),
306 "constructor_declaration" => Sym(K::Method, "constructor", No),
307 "using_directive" => Import,
308 _ => Skip,
309 },
310 Lang::Php => match kind {
311 "function_definition" => Sym(K::Function, "function", No),
312 "class_declaration" => Sym(K::Class, "class", Ty),
313 "interface_declaration" => Sym(K::Interface, "interface", Ty),
314 "trait_declaration" => Sym(K::Trait, "trait", Ty),
315 "enum_declaration" => Sym(K::Enum, "enum", Ty),
316 "method_declaration" => Sym(K::Method, "method", No),
317 "namespace_definition" => Sym(K::Module, "namespace", Scope),
318 "namespace_use_declaration" => Import,
319 _ => Skip,
320 },
321 Lang::Lua => match kind {
322 "function_declaration" => Sym(K::Function, "function", No),
323 _ => Skip,
324 },
325 Lang::Css | Lang::Html => Skip,
327 Lang::Kotlin => match kind {
328 "function_declaration" => Sym(K::Function, "function", No),
329 "class_declaration" => Sym(K::Class, "class", Ty),
330 "object_declaration" => Sym(K::Class, "object", Ty),
331 "type_alias" => Sym(K::Type, "typealias", No),
332 "import_list" => Rule::Flat,
333 "import_header" => Import,
334 _ => Skip,
335 },
336 Lang::Ruby => match kind {
337 "method" | "singleton_method" => Sym(K::Function, "method", No),
338 "class" => Sym(K::Class, "class", Ty),
339 "module" => Sym(K::Module, "module", Ty),
340 _ => Skip,
341 },
342 Lang::Bash => match kind {
343 "function_definition" => Sym(K::Function, "function", No),
344 _ => Skip,
345 },
346 Lang::ObjC => match kind {
347 "class_interface" | "class_implementation" => Sym(K::Class, "class", Ty),
348 "protocol_declaration" => Sym(K::Interface, "protocol", Ty),
349 "method_definition" => Sym(K::Function, "method", No),
350 "function_definition" => Sym(K::Function, "function", No),
351 "preproc_include" | "module_import" => Import,
352 _ => Skip,
353 },
354 Lang::Dart => match kind {
355 "class_definition" | "mixin_application_class" => Sym(K::Class, "class", Ty),
356 "mixin_declaration" => Sym(K::Trait, "mixin", Ty),
357 "extension_declaration" | "extension_type_declaration" => {
358 Sym(K::Module, "extension", Ty)
359 }
360 "enum_declaration" => Sym(K::Enum, "enum", Ty),
361 "type_alias" => Sym(K::Type, "typedef", No),
362 "function_signature" => Sym(K::Function, "function", No),
363 "constructor_signature"
364 | "factory_constructor_signature"
365 | "constant_constructor_signature" => Sym(K::Method, "constructor", No),
366 "method_signature" | "declaration" => Wrap,
367 "import_or_export" => Import,
368 _ => Skip,
369 },
370 Lang::Julia => match kind {
371 "function_definition" | "macro_definition" => Sym(K::Function, "function", No),
372 "struct_definition" => Sym(K::Struct, "struct", No),
373 "abstract_definition" | "primitive_definition" => Sym(K::Type, "type", No),
374 "module_definition" => Sym(K::Module, "module", Scope),
375 "import_statement" | "using_statement" => Import,
376 _ => Skip,
377 },
378 Lang::Nix => Skip,
379 Lang::Dockerfile => Skip,
380 Lang::Ocaml => match kind {
381 "value_definition" | "type_definition" | "module_definition" | "class_definition" => {
382 Wrap
383 }
384 "let_binding" => Sym(K::Function, "let", No),
385 "type_binding" => Sym(K::Type, "type", No),
386 "class_binding" => Sym(K::Class, "class", Ty),
387 "module_binding" => Sym(K::Module, "module", Scope),
388 "module_type_definition" => Sym(K::Interface, "module type", No),
389 "open_module" => Import,
390 _ => Skip,
391 },
392 Lang::Elixir => match kind {
393 "call" => Rule::Special(Sp::ExCall),
394 _ => Skip,
395 },
396 Lang::R => match kind {
397 "binary_operator" => Rule::Special(Sp::RAssign),
398 _ => Skip,
399 },
400 Lang::Svelte => Skip,
401 Lang::Solidity => match kind {
402 "contract_declaration" => Sym(K::Class, "contract", Ty),
403 "interface_declaration" => Sym(K::Interface, "interface", Ty),
404 "library_declaration" => Sym(K::Module, "library", Ty),
405 "struct_declaration" => Sym(K::Struct, "struct", No),
406 "enum_declaration" => Sym(K::Enum, "enum", No),
407 "function_definition" => Sym(K::Function, "function", No),
408 "modifier_definition" => Sym(K::Function, "modifier", No),
409 "import_directive" => Import,
410 _ => Skip,
411 },
412 Lang::Groovy => match kind {
413 "class_definition" => Sym(K::Class, "class", Ty),
414 "function_definition" | "function_declaration" => Sym(K::Function, "function", No),
415 "groovy_import" => Import,
416 _ => Skip,
417 },
418 Lang::Make => Skip,
419 Lang::Scheme => Skip,
420 Lang::CommonLisp => match kind {
421 "list_lit" => Wrap,
422 "defun" => Sym(K::Function, "defun", No),
423 _ => Skip,
424 },
425 Lang::Pascal => match kind {
426 "unit" | "program" => Sym(K::Module, "unit", Scope),
427 "defProc" => Wrap,
428 "declProc" => Sym(K::Function, "procedure", No),
429 "declUses" => Import,
430 _ => Skip,
431 },
432 Lang::Gleam => match kind {
433 "function" | "external_function" => Sym(K::Function, "fn", No),
434 "type_definition" => Sym(K::Type, "type", No),
435 "type_alias" => Sym(K::Type, "type", No),
436 "import" => Import,
437 _ => Skip,
438 },
439 Lang::PowerShell => match kind {
440 "statement_list" => Wrap,
441 "function_statement" => Sym(K::Function, "function", No),
442 "class_statement" => Sym(K::Class, "class", Ty),
443 "enum_statement" => Sym(K::Enum, "enum", No),
444 "class_method_definition" => Sym(K::Function, "method", No),
445 _ => Skip,
446 },
447 Lang::CMake => match kind {
448 "function_def" => Sym(K::Function, "function", No),
449 "macro_def" => Sym(K::Function, "macro", No),
450 _ => Skip,
451 },
452 Lang::Erlang => match kind {
453 "fun_decl" => Sym(K::Function, "function", No),
454 "module_attribute" => Sym(K::Module, "module", No),
455 "record_decl" => Sym(K::Struct, "record", No),
456 "type_alias" | "opaque" => Sym(K::Type, "type", No),
457 _ => Skip,
458 },
459 Lang::Hcl => Skip,
460 Lang::Vue => Skip,
461 Lang::D => match kind {
462 "module_def" => Wrap,
463 "class_declaration" => Sym(K::Class, "class", Ty),
464 "struct_declaration" => Sym(K::Struct, "struct", Ty),
465 "interface_declaration" => Sym(K::Interface, "interface", Ty),
466 "enum_declaration" => Sym(K::Enum, "enum", No),
467 "function_declaration" => Sym(K::Function, "function", No),
468 "import_declaration" => Import,
469 _ => Skip,
470 },
471 Lang::Verilog => match kind {
472 "module_declaration" => Sym(K::Class, "module", No),
473 "interface_declaration" => Sym(K::Interface, "interface", No),
474 "package_declaration" => Sym(K::Module, "package", No),
475 "class_declaration" => Sym(K::Class, "class", No),
476 "program_declaration" => Sym(K::Module, "program", No),
477 "function_declaration" => Sym(K::Function, "function", No),
478 "task_declaration" => Sym(K::Function, "task", No),
479 "include_compiler_directive" => Import,
480 _ => Skip,
481 },
482 Lang::Elm => match kind {
483 "module_declaration" => Sym(K::Module, "module", No),
484 "type_declaration" => Sym(K::Enum, "type", No),
485 "type_alias_declaration" => Sym(K::Type, "type alias", No),
486 "value_declaration" => Wrap,
487 "function_declaration_left" => Sym(K::Function, "function", No),
488 "import_clause" => Import,
489 _ => Skip,
490 },
491 }
492}