kglite 0.10.21

Pure-Rust knowledge graph engine — Cypher pipeline, snapshot/working CoW transactions, columnar/mmap/disk storage backends, optional dataset loaders (SEC EDGAR, Sodir, Wikidata). PyO3 wrappers live in the sibling kglite-py crate (the Python wheel); embeddable directly from any Rust binary without PyO3 in the dep tree.
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
//! Swift language parser (minimal viable).
//!
//! Coverage in 0.9.34:
//!   - `class` / `struct` / `actor` / `enum` declarations → ClassInfo
//!     (kind tagged from `declaration_kind`).
//!   - `protocol` declarations → InterfaceInfo with kind="protocol".
//!   - Top-level and nested `func` declarations → FunctionInfo.
//!   - `import Foundation` → FileInfo.imports.
//!   - Visibility (`public` / `internal` / `fileprivate` / `private`).
//!
//! Not yet supported (follow-up scope):
//!   - `extension` blocks emit a Class entry, but don't yet synthesize
//!     IMPLEMENTS edges to the protocol(s) they conform to.
//!   - `init` / `deinit` / `subscript` / computed properties.
//!   - `@objc` / `@available` / other attributes as decorators.
//!   - `async` / `throws` flags on functions.
//!   - Enum cases as AttributeInfo.
//!
//! These all map cleanly onto the existing `FunctionInfo` / `ClassInfo`
//! shape; they didn't make this commit's budget but the parser
//! structure leaves slots for each.

use std::path::Path;
use tree_sitter::{Node, Parser, Tree};

use super::shared::{file_to_module_path, make_qualified, node_text};
use super::LanguageParser;
use crate::code_tree::models::{
    ClassInfo, FileInfo, FunctionInfo, InterfaceInfo, ParseResult, TypeRelationship,
};

pub const SWIFT_NOISE_NAMES: &[&str] = &[
    "print",
    "String",
    "Int",
    "Float",
    "Double",
    "Bool",
    "Array",
    "Dictionary",
    "Optional",
    "Range",
    "Result",
    "Error",
    "fatalError",
    "preconditionFailure",
    "assert",
    "precondition",
    "abs",
    "min",
    "max",
];

pub struct SwiftParser;

thread_local! {
    static TS_PARSER: std::cell::RefCell<Parser> = {
        let mut p = Parser::new();
        p.set_language(&tree_sitter_swift::LANGUAGE.into())
            .expect("loading tree-sitter-swift grammar");
        std::cell::RefCell::new(p)
    };
}

impl SwiftParser {
    pub fn new() -> Self {
        SwiftParser
    }

    fn parse_tree(&self, source: &[u8]) -> Option<Tree> {
        TS_PARSER.with(|p| p.borrow_mut().parse(source, None))
    }

    /// Walk top-level children of `source_file` and dispatch to per-kind
    /// extractors. Owner scope (for nested methods/types) is passed as
    /// the dotted prefix to compose qualified names.
    fn parse_block(
        block: Node,
        source: &[u8],
        module_path: &str,
        owner_prefix: &str,
        rel_path: &str,
        result: &mut ParseResult,
        file_info: &mut FileInfo,
    ) {
        let mut cursor = block.walk();
        for child in block.named_children(&mut cursor) {
            match child.kind() {
                "import_declaration" => {
                    if let Some(target) = Self::extract_import(child, source) {
                        file_info.imports.push(target);
                    }
                }
                "class_declaration" => {
                    Self::parse_class(child, source, module_path, owner_prefix, rel_path, result);
                }
                "protocol_declaration" => {
                    Self::parse_protocol(
                        child,
                        source,
                        module_path,
                        owner_prefix,
                        rel_path,
                        result,
                    );
                }
                "function_declaration" => {
                    Self::parse_function(
                        child,
                        source,
                        module_path,
                        owner_prefix,
                        rel_path,
                        result,
                        owner_prefix.is_empty(), // top-level → not a method
                    );
                }
                _ => {}
            }
        }
    }

    fn extract_import(node: Node, source: &[u8]) -> Option<String> {
        // `import Foundation` / `import struct Foo.Bar`
        // We want the module path (e.g. "Foundation" or "Foo.Bar"). The
        // last identifier-or-navigation child is the target.
        let mut cursor = node.walk();
        let mut last_target: Option<String> = None;
        for child in node.named_children(&mut cursor) {
            let kind = child.kind();
            if kind == "identifier" || kind == "navigation_expression" || kind == "user_type" {
                last_target = Some(node_text(child, source).to_string());
            }
        }
        last_target
    }

    fn extract_visibility(node: Node, source: &[u8]) -> String {
        // Look for a `modifiers` named child whose body contains a
        // `visibility_modifier` token.
        let mut cursor = node.walk();
        for child in node.named_children(&mut cursor) {
            if child.kind() == "modifiers" {
                let mut sub = child.walk();
                for m in child.named_children(&mut sub) {
                    if m.kind() == "visibility_modifier" {
                        return node_text(m, source).to_string();
                    }
                }
            }
        }
        "internal".to_string()
    }

    /// Resolve `class_declaration` → kind ("class" | "struct" | "enum" |
    /// "actor" | "extension"). The grammar exposes the keyword as the
    /// anonymous `declaration_kind` field; its `kind()` is the literal
    /// keyword token type ("struct", "class", etc.).
    fn extract_declaration_kind(node: Node, _source: &[u8]) -> String {
        node.child_by_field_name("declaration_kind")
            .map(|c| c.kind().to_string())
            .unwrap_or_else(|| "class".to_string())
    }

    fn extract_name<'a>(node: Node<'a>, source: &'a [u8]) -> Option<&'a str> {
        node.child_by_field_name("name")
            .map(|c| node_text(c, source))
    }

    fn extract_body(node: Node) -> Option<Node> {
        node.child_by_field_name("body")
    }

    fn parse_class(
        node: Node,
        source: &[u8],
        module_path: &str,
        owner_prefix: &str,
        rel_path: &str,
        result: &mut ParseResult,
    ) {
        let Some(name) = Self::extract_name(node, source) else {
            return;
        };
        let kind_kw = Self::extract_declaration_kind(node, source);
        let visibility = Self::extract_visibility(node, source);
        let line = node.start_position().row as u32 + 1;
        let end_line = node.end_position().row as u32 + 1;
        let qname = make_qualified(module_path, owner_prefix, name, '.');

        // Map declaration_kind to KGLite's class.kind enum. `extension`
        // emits a Class entry too (it adds methods to an existing type)
        // — we tag it with kind="extension" so downstream code can
        // distinguish, and the methods inside still get qualified-named
        // under the extended type's owner_prefix.
        let kind = match kind_kw.as_str() {
            "struct" => "struct",
            "enum" => "enum",
            "actor" => "actor",
            "extension" => "extension",
            _ => "class",
        };

        // `enum` flows into ClassInfo (no special EnumInfo extraction
        // here — Swift enums are heavy on associated values and would
        // need their own pass).
        result.classes.push(ClassInfo {
            qualified_name: qname.clone(),
            visibility: visibility.clone(),
            name: name.to_string(),
            kind: kind.to_string(),
            file_path: rel_path.to_string(),
            line_number: line,
            docstring: None,
            bases: Vec::new(),
            type_parameters: None,
            end_line: Some(end_line),
            metadata: Default::default(),
        });

        if let Some(body) = Self::extract_body(node) {
            let nested_prefix = qname.clone();
            // Synthesize a TypeRelationship so the builder emits
            // HAS_METHOD edges between the class and each of its
            // methods. Mirrors python.rs's `method_rel`.
            let mut method_rel = TypeRelationship {
                source_type: qname.clone(),
                target_type: None,
                relationship: "inherent".to_string(),
                methods: Vec::new(),
            };
            let methods_start = result.functions.len();

            let mut cursor = body.walk();
            for child in body.named_children(&mut cursor) {
                match child.kind() {
                    "function_declaration" => {
                        Self::parse_function(
                            child,
                            source,
                            module_path,
                            &nested_prefix,
                            rel_path,
                            result,
                            true, // body-of-class → is_method=true
                        );
                    }
                    "class_declaration" => {
                        Self::parse_class(
                            child,
                            source,
                            module_path,
                            &nested_prefix,
                            rel_path,
                            result,
                        );
                    }
                    "protocol_declaration" => {
                        Self::parse_protocol(
                            child,
                            source,
                            module_path,
                            &nested_prefix,
                            rel_path,
                            result,
                        );
                    }
                    _ => {}
                }
            }

            // Anything appended to `result.functions` since `methods_start`
            // whose qname is directly under this class (one separator past
            // `nested_prefix`) is a method of *this* class — nested
            // class methods that were also appended are skipped because
            // they live under a deeper prefix.
            let direct_prefix = format!("{nested_prefix}.");
            for fn_info in &result.functions[methods_start..] {
                let rest = match fn_info.qualified_name.strip_prefix(&direct_prefix) {
                    Some(r) => r,
                    None => continue,
                };
                if !rest.contains('.') {
                    method_rel.methods.push(fn_info.clone());
                }
            }
            if !method_rel.methods.is_empty() {
                result.type_relationships.push(method_rel);
            }
        }

        // Extension → emit a TypeRelationship if the grammar carries
        // protocol conformance info. Stub for now — left for follow-up
        // once we know the field shape.
        if kind == "extension" {
            result.type_relationships.push(TypeRelationship {
                source_type: name.to_string(),
                target_type: None,
                relationship: "inherent".to_string(),
                methods: Vec::new(),
            });
        }
    }

    fn parse_protocol(
        node: Node,
        source: &[u8],
        module_path: &str,
        owner_prefix: &str,
        rel_path: &str,
        result: &mut ParseResult,
    ) {
        let Some(name) = Self::extract_name(node, source) else {
            return;
        };
        let visibility = Self::extract_visibility(node, source);
        let line = node.start_position().row as u32 + 1;
        let end_line = node.end_position().row as u32 + 1;
        let qname = make_qualified(module_path, owner_prefix, name, '.');

        result.interfaces.push(InterfaceInfo {
            qualified_name: qname.clone(),
            visibility,
            name: name.to_string(),
            kind: "protocol".to_string(),
            file_path: rel_path.to_string(),
            line_number: line,
            docstring: None,
            type_parameters: None,
            end_line: Some(end_line),
        });

        if let Some(body) = Self::extract_body(node) {
            let nested_prefix = qname;
            let mut cursor = body.walk();
            for child in body.named_children(&mut cursor) {
                match child.kind() {
                    // protocol methods come through as either
                    // `protocol_function_declaration` (no body) or
                    // `function_declaration` (default impl). Both are
                    // emitted as FunctionInfo with is_method=true.
                    "function_declaration" | "protocol_function_declaration" => {
                        Self::parse_function(
                            child,
                            source,
                            module_path,
                            &nested_prefix,
                            rel_path,
                            result,
                            true,
                        );
                    }
                    _ => {}
                }
            }
        }
    }

    fn parse_function(
        node: Node,
        source: &[u8],
        _module_path: &str,
        owner_prefix: &str,
        rel_path: &str,
        result: &mut ParseResult,
        is_method: bool,
    ) {
        let Some(name) = Self::extract_name(node, source) else {
            return;
        };
        let visibility = Self::extract_visibility(node, source);
        let line = node.start_position().row as u32 + 1;
        let end_line = node.end_position().row as u32 + 1;
        let qname = if owner_prefix.is_empty() {
            name.to_string()
        } else {
            format!("{owner_prefix}.{name}")
        };
        let return_type = node
            .child_by_field_name("return_type")
            .map(|c| node_text(c, source).to_string());
        let signature = Self::build_signature(node, source);
        let calls = Self::extract_calls(node, source);

        result.functions.push(FunctionInfo {
            qualified_name: qname,
            visibility,
            is_async: false,
            is_method,
            signature,
            file_path: rel_path.to_string(),
            line_number: line,
            name: name.to_string(),
            docstring: None,
            return_type,
            decorators: Vec::new(),
            calls,
            references: Vec::new(),
            function_refs: Vec::new(),
            type_parameters: None,
            end_line: Some(end_line),
            parameters: Vec::new(),
            branch_count: None,
            param_count: None,
            max_nesting: None,
            is_recursive: None,
            procedure_names: Vec::new(),
            metadata: Default::default(),
        });
    }

    fn build_signature(node: Node, source: &[u8]) -> String {
        // Crude: everything before the function body. Mirrors the
        // approach Go's parser takes.
        let mut parts: Vec<&str> = Vec::new();
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            if child.kind() == "function_body" {
                break;
            }
            parts.push(node_text(child, source));
        }
        parts.join(" ")
    }

    fn extract_calls(node: Node, source: &[u8]) -> Vec<(String, u32)> {
        let mut calls: Vec<(String, u32)> = Vec::new();
        fn walk(n: Node, source: &[u8], out: &mut Vec<(String, u32)>) {
            if n.kind() == "call_expression" {
                let line = n.start_position().row as u32 + 1;
                // Function position is the first named child.
                if let Some(first) = n.named_child(0) {
                    let text = node_text(first, source);
                    // For navigation_expression `a.b()`, take the
                    // terminal segment as the bare callee name.
                    let bare = text.rsplit('.').next().unwrap_or(text).trim();
                    if !bare.is_empty() && !bare.contains(' ') && !bare.contains('(') {
                        out.push((bare.to_string(), line));
                    }
                }
            }
            let mut cursor = n.walk();
            for child in n.named_children(&mut cursor) {
                walk(child, source, out);
            }
        }
        walk(node, source, &mut calls);
        calls
    }
}

impl LanguageParser for SwiftParser {
    fn language_name(&self) -> &'static str {
        "swift"
    }
    fn file_extensions(&self) -> &'static [&'static str] {
        &["swift"]
    }
    fn noise_names(&self) -> &'static [&'static str] {
        SWIFT_NOISE_NAMES
    }

    fn parse_file(&self, filepath: &Path, src_root: &Path) -> ParseResult {
        let mut result = ParseResult::new();
        let Ok(source) = std::fs::read_to_string(filepath) else {
            return result;
        };
        let source_bytes = source.as_bytes();
        let rel_path = filepath
            .strip_prefix(src_root)
            .unwrap_or(filepath)
            .to_string_lossy()
            .to_string();
        let module_path = file_to_module_path(filepath, src_root, '.');

        let Some(tree) = self.parse_tree(source_bytes) else {
            return result;
        };

        let filename = filepath
            .file_name()
            .and_then(|o| o.to_str())
            .unwrap_or("")
            .to_string();
        let is_test = crate::code_tree::parsers::shared::is_test_path(
            &rel_path,
            &filename,
            &["Tests.swift", "Test.swift"],
        );
        let mut file_info = FileInfo {
            path: rel_path.clone(),
            filename,
            loc: source.lines().count() as u32,
            module_path: module_path.clone(),
            language: "swift".to_string(),
            submodule_declarations: Vec::new(),
            imports: Vec::new(),
            exports: Vec::new(),
            annotations: None,
            is_test,
            skip_reason: None,
        };

        Self::parse_block(
            tree.root_node(),
            source_bytes,
            &module_path,
            "",
            &rel_path,
            &mut result,
            &mut file_info,
        );

        result.files.push(file_info);
        result
    }
}