graphify-extract 0.4.4

AST and semantic extraction engine for graphify
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
//! Per-language configuration for tree-sitter–based extraction.
//!
//! Each [`LanguageConfig`] describes the AST node types that correspond to
//! classes, functions, imports, and calls for a given language. The actual
//! tree-sitter grammar crates are not yet wired in; this module is
//! **configuration only**.

use std::collections::HashSet;

/// Metadata describing how to extract entities from a language's AST.
#[derive(Debug, Clone)]
pub struct LanguageConfig {
    /// Human-readable language name.
    pub name: &'static str,
    /// Crate that provides the tree-sitter grammar (e.g. `"tree_sitter_python"`).
    pub ts_crate: &'static str,
    /// AST node types that represent class-like definitions.
    pub class_types: HashSet<&'static str>,
    /// AST node types that represent function/method definitions.
    pub function_types: HashSet<&'static str>,
    /// AST node types that represent import statements.
    pub import_types: HashSet<&'static str>,
    /// AST node types that represent function calls.
    pub call_types: HashSet<&'static str>,
    /// Field name for the entity name within the AST node.
    pub name_field: &'static str,
    /// Field name for the body of a class/function.
    pub body_field: &'static str,
    /// Field name for the function being called in a call expression.
    pub call_function_field: &'static str,
    /// AST node types that delimit function boundaries (for scope analysis).
    pub function_boundary_types: HashSet<&'static str>,
    /// Whether the language uses parenthesised labels (e.g. Go `func (r *Recv) Name()`).
    pub function_label_parens: bool,
}

// ---------------------------------------------------------------------------
// Helper
// ---------------------------------------------------------------------------

fn hs(items: &[&'static str]) -> HashSet<&'static str> {
    items.iter().copied().collect()
}

// ---------------------------------------------------------------------------
// Language configs (matching the Python extract.py)
// ---------------------------------------------------------------------------

pub fn python_config() -> LanguageConfig {
    LanguageConfig {
        name: "python",
        ts_crate: "tree_sitter_python",
        class_types: hs(&["class_definition"]),
        function_types: hs(&["function_definition"]),
        import_types: hs(&["import_statement", "import_from_statement"]),
        call_types: hs(&["call"]),
        name_field: "name",
        body_field: "body",
        call_function_field: "function",
        function_boundary_types: hs(&["function_definition", "class_definition"]),
        function_label_parens: false,
    }
}

pub fn javascript_config() -> LanguageConfig {
    LanguageConfig {
        name: "javascript",
        ts_crate: "tree_sitter_javascript",
        class_types: hs(&["class_declaration", "class"]),
        function_types: hs(&[
            "function_declaration",
            "method_definition",
            "arrow_function",
            "function",
        ]),
        import_types: hs(&["import_statement"]),
        call_types: hs(&["call_expression"]),
        name_field: "name",
        body_field: "body",
        call_function_field: "function",
        function_boundary_types: hs(&[
            "function_declaration",
            "method_definition",
            "arrow_function",
            "class_declaration",
        ]),
        function_label_parens: false,
    }
}

pub fn typescript_config() -> LanguageConfig {
    let mut cfg = javascript_config();
    cfg.name = "typescript";
    cfg.ts_crate = "tree_sitter_typescript";
    cfg.class_types.insert("abstract_class_declaration");
    cfg.function_types.insert("function_signature");
    cfg.import_types.insert("import_statement");
    cfg
}

pub fn go_config() -> LanguageConfig {
    LanguageConfig {
        name: "go",
        ts_crate: "tree_sitter_go",
        class_types: hs(&["type_declaration", "type_spec"]),
        function_types: hs(&["function_declaration", "method_declaration"]),
        import_types: hs(&["import_declaration", "import_spec"]),
        call_types: hs(&["call_expression"]),
        name_field: "name",
        body_field: "body",
        call_function_field: "function",
        function_boundary_types: hs(&["function_declaration", "method_declaration"]),
        function_label_parens: true,
    }
}

pub fn rust_config() -> LanguageConfig {
    LanguageConfig {
        name: "rust",
        ts_crate: "tree_sitter_rust",
        class_types: hs(&["struct_item", "enum_item", "trait_item", "impl_item"]),
        function_types: hs(&["function_item"]),
        import_types: hs(&["use_declaration"]),
        call_types: hs(&["call_expression"]),
        name_field: "name",
        body_field: "body",
        call_function_field: "function",
        function_boundary_types: hs(&["function_item", "impl_item"]),
        function_label_parens: false,
    }
}

pub fn java_config() -> LanguageConfig {
    LanguageConfig {
        name: "java",
        ts_crate: "tree_sitter_java",
        class_types: hs(&[
            "class_declaration",
            "interface_declaration",
            "enum_declaration",
            "annotation_type_declaration",
        ]),
        function_types: hs(&["method_declaration", "constructor_declaration"]),
        import_types: hs(&["import_declaration"]),
        call_types: hs(&["method_invocation"]),
        name_field: "name",
        body_field: "body",
        call_function_field: "name",
        function_boundary_types: hs(&[
            "method_declaration",
            "constructor_declaration",
            "class_declaration",
        ]),
        function_label_parens: false,
    }
}

pub fn c_config() -> LanguageConfig {
    LanguageConfig {
        name: "c",
        ts_crate: "tree_sitter_c",
        class_types: hs(&["struct_specifier", "enum_specifier", "union_specifier"]),
        function_types: hs(&["function_definition"]),
        import_types: hs(&["preproc_include"]),
        call_types: hs(&["call_expression"]),
        name_field: "declarator",
        body_field: "body",
        call_function_field: "function",
        function_boundary_types: hs(&["function_definition"]),
        function_label_parens: false,
    }
}

pub fn cpp_config() -> LanguageConfig {
    let mut cfg = c_config();
    cfg.name = "cpp";
    cfg.ts_crate = "tree_sitter_cpp";
    cfg.class_types.insert("class_specifier");
    cfg.class_types.insert("namespace_definition");
    cfg.function_types.insert("function_definition");
    cfg
}

pub fn ruby_config() -> LanguageConfig {
    LanguageConfig {
        name: "ruby",
        ts_crate: "tree_sitter_ruby",
        class_types: hs(&["class", "module"]),
        function_types: hs(&["method", "singleton_method"]),
        import_types: hs(&["call"]), // require/include are calls in Ruby
        call_types: hs(&["call", "command"]),
        name_field: "name",
        body_field: "body",
        call_function_field: "method",
        function_boundary_types: hs(&["method", "singleton_method", "class"]),
        function_label_parens: false,
    }
}

pub fn csharp_config() -> LanguageConfig {
    LanguageConfig {
        name: "csharp",
        ts_crate: "tree_sitter_c_sharp",
        class_types: hs(&[
            "class_declaration",
            "interface_declaration",
            "struct_declaration",
            "enum_declaration",
        ]),
        function_types: hs(&["method_declaration", "constructor_declaration"]),
        import_types: hs(&["using_directive"]),
        call_types: hs(&["invocation_expression"]),
        name_field: "name",
        body_field: "body",
        call_function_field: "function",
        function_boundary_types: hs(&[
            "method_declaration",
            "constructor_declaration",
            "class_declaration",
        ]),
        function_label_parens: false,
    }
}

pub fn kotlin_config() -> LanguageConfig {
    LanguageConfig {
        name: "kotlin",
        ts_crate: "tree_sitter_kotlin",
        class_types: hs(&[
            "class_declaration",
            "object_declaration",
            "interface_declaration",
        ]),
        function_types: hs(&["function_declaration"]),
        import_types: hs(&["import_header"]),
        call_types: hs(&["call_expression"]),
        name_field: "name",
        body_field: "body",
        call_function_field: "function",
        function_boundary_types: hs(&["function_declaration", "class_declaration"]),
        function_label_parens: false,
    }
}

pub fn scala_config() -> LanguageConfig {
    LanguageConfig {
        name: "scala",
        ts_crate: "tree_sitter_scala",
        class_types: hs(&["class_definition", "object_definition", "trait_definition"]),
        function_types: hs(&["function_definition", "val_definition"]),
        import_types: hs(&["import_declaration"]),
        call_types: hs(&["call_expression"]),
        name_field: "name",
        body_field: "body",
        call_function_field: "function",
        function_boundary_types: hs(&["function_definition", "class_definition"]),
        function_label_parens: false,
    }
}

pub fn php_config() -> LanguageConfig {
    LanguageConfig {
        name: "php",
        ts_crate: "tree_sitter_php",
        class_types: hs(&[
            "class_declaration",
            "interface_declaration",
            "trait_declaration",
        ]),
        function_types: hs(&["function_definition", "method_declaration"]),
        import_types: hs(&["namespace_use_declaration"]),
        call_types: hs(&["function_call_expression", "method_call_expression"]),
        name_field: "name",
        body_field: "body",
        call_function_field: "function",
        function_boundary_types: hs(&[
            "function_definition",
            "method_declaration",
            "class_declaration",
        ]),
        function_label_parens: false,
    }
}

pub fn swift_config() -> LanguageConfig {
    LanguageConfig {
        name: "swift",
        ts_crate: "tree_sitter_swift",
        class_types: hs(&[
            "class_declaration",
            "struct_declaration",
            "protocol_declaration",
            "enum_declaration",
        ]),
        function_types: hs(&["function_declaration", "init_declaration"]),
        import_types: hs(&["import_declaration"]),
        call_types: hs(&["call_expression"]),
        name_field: "name",
        body_field: "body",
        call_function_field: "function",
        function_boundary_types: hs(&["function_declaration", "class_declaration"]),
        function_label_parens: false,
    }
}

pub fn lua_config() -> LanguageConfig {
    LanguageConfig {
        name: "lua",
        ts_crate: "tree_sitter_lua",
        class_types: hs(&[]),
        function_types: hs(&[
            "function_declaration",
            "local_function_declaration",
            "function_definition",
        ]),
        import_types: hs(&[]), // require() is a call
        call_types: hs(&["function_call"]),
        name_field: "name",
        body_field: "body",
        call_function_field: "name",
        function_boundary_types: hs(&["function_declaration", "local_function_declaration"]),
        function_label_parens: false,
    }
}

pub fn zig_config() -> LanguageConfig {
    LanguageConfig {
        name: "zig",
        ts_crate: "tree_sitter_zig",
        class_types: hs(&["container_declaration"]),
        function_types: hs(&["fn_proto"]),
        import_types: hs(&[]),
        call_types: hs(&["call_expr"]),
        name_field: "name",
        body_field: "body",
        call_function_field: "function",
        function_boundary_types: hs(&["fn_proto"]),
        function_label_parens: false,
    }
}

pub fn powershell_config() -> LanguageConfig {
    LanguageConfig {
        name: "powershell",
        ts_crate: "tree_sitter_powershell",
        class_types: hs(&["class_statement"]),
        function_types: hs(&["function_statement"]),
        import_types: hs(&["using_statement"]),
        call_types: hs(&["command_expression"]),
        name_field: "name",
        body_field: "body",
        call_function_field: "name",
        function_boundary_types: hs(&["function_statement"]),
        function_label_parens: false,
    }
}

pub fn elixir_config() -> LanguageConfig {
    LanguageConfig {
        name: "elixir",
        ts_crate: "tree_sitter_elixir",
        class_types: hs(&["call"]),    // defmodule is a call in Elixir
        function_types: hs(&["call"]), // def/defp are calls too
        import_types: hs(&["call"]),   // import/use/require
        call_types: hs(&["call"]),
        name_field: "target",
        body_field: "body",
        call_function_field: "target",
        function_boundary_types: hs(&["call"]),
        function_label_parens: false,
    }
}

pub fn objc_config() -> LanguageConfig {
    LanguageConfig {
        name: "objc",
        ts_crate: "tree_sitter_objc",
        class_types: hs(&[
            "class_interface",
            "class_implementation",
            "protocol_declaration",
        ]),
        function_types: hs(&["method_definition", "function_definition"]),
        import_types: hs(&["preproc_import", "preproc_include"]),
        call_types: hs(&["message_expression", "call_expression"]),
        name_field: "name",
        body_field: "body",
        call_function_field: "selector",
        function_boundary_types: hs(&["method_definition", "function_definition"]),
        function_label_parens: false,
    }
}

pub fn julia_config() -> LanguageConfig {
    LanguageConfig {
        name: "julia",
        ts_crate: "tree_sitter_julia",
        class_types: hs(&["struct_definition", "abstract_definition"]),
        function_types: hs(&["function_definition", "short_function_definition"]),
        import_types: hs(&["import_statement", "using_statement"]),
        call_types: hs(&["call_expression"]),
        name_field: "name",
        body_field: "body",
        call_function_field: "function",
        function_boundary_types: hs(&["function_definition"]),
        function_label_parens: false,
    }
}

pub fn dart_config() -> LanguageConfig {
    LanguageConfig {
        name: "dart",
        ts_crate: "tree_sitter_dart",
        class_types: hs(&[
            "class_definition",
            "enum_declaration",
            "mixin_declaration",
            "extension_declaration",
        ]),
        function_types: hs(&[
            "function_signature",
            "method_signature",
            "function_body",
            "method_definition",
        ]),
        import_types: hs(&["import_or_export"]),
        call_types: hs(&["method_invocation", "function_expression_invocation"]),
        name_field: "name",
        body_field: "body",
        call_function_field: "function",
        function_boundary_types: hs(&[
            "method_definition",
            "function_signature",
            "class_definition",
        ]),
        function_label_parens: false,
    }
}

// ---------------------------------------------------------------------------
// Lookup
// ---------------------------------------------------------------------------

/// Return the [`LanguageConfig`] for the given language name.
pub fn config_for_language(lang: &str) -> Option<LanguageConfig> {
    match lang {
        "python" => Some(python_config()),
        "javascript" => Some(javascript_config()),
        "typescript" => Some(typescript_config()),
        "go" => Some(go_config()),
        "rust" => Some(rust_config()),
        "java" => Some(java_config()),
        "c" => Some(c_config()),
        "cpp" => Some(cpp_config()),
        "ruby" => Some(ruby_config()),
        "csharp" => Some(csharp_config()),
        "kotlin" => Some(kotlin_config()),
        "scala" => Some(scala_config()),
        "php" => Some(php_config()),
        "swift" => Some(swift_config()),
        "lua" => Some(lua_config()),
        "zig" => Some(zig_config()),
        "powershell" => Some(powershell_config()),
        "elixir" => Some(elixir_config()),
        "objc" => Some(objc_config()),
        "julia" => Some(julia_config()),
        "dart" => Some(dart_config()),
        _ => None,
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn all_21_languages_have_configs() {
        let languages = [
            "python",
            "javascript",
            "typescript",
            "go",
            "rust",
            "java",
            "c",
            "cpp",
            "ruby",
            "csharp",
            "kotlin",
            "scala",
            "php",
            "swift",
            "lua",
            "zig",
            "powershell",
            "elixir",
            "objc",
            "julia",
            "dart",
        ];
        for lang in languages {
            assert!(
                config_for_language(lang).is_some(),
                "missing config for {lang}"
            );
        }
        assert_eq!(languages.len(), 21);
    }

    #[test]
    fn python_config_has_expected_types() {
        let cfg = python_config();
        assert!(cfg.class_types.contains("class_definition"));
        assert!(cfg.function_types.contains("function_definition"));
        assert!(cfg.import_types.contains("import_statement"));
        assert!(cfg.import_types.contains("import_from_statement"));
    }

    #[test]
    fn typescript_extends_javascript() {
        let ts = typescript_config();
        assert!(ts.class_types.contains("abstract_class_declaration"));
        // Inherited from JS
        assert!(ts.class_types.contains("class_declaration"));
    }

    #[test]
    fn unknown_language_returns_none() {
        assert!(config_for_language("brainfuck").is_none());
    }
}