harn-hostlib 0.8.7

Opt-in code-intelligence and deterministic-tool host builtins for the Harn VM
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
//! Symbol extraction for the scanner pipeline.
//!
//! This regex extractor covers pragma sections, Dart, and the generic
//! fallback path used for languages where the scanner does not need a full
//! tree-sitter pass. The scanner keeps the same public record shape so the
//! orchestrator in [`super::run_scan`] can swap extraction engines without
//! disturbing callers.
//!
//! The output shape — `Vec<SymbolRecord>` — is the canonical scanner
//! representation used by the scanner orchestrator.

use regex::Regex;
use std::sync::OnceLock;

use crate::scanner::result::{SymbolKind, SymbolRecord};

/// Extract every symbol the regex pipeline can recognize from `content`.
///
/// `language` is the lowercase file extension; `file_path` is the
/// repo-relative POSIX path (used to build stable symbol ids).
pub fn extract_symbols(content: &str, language: &str, file_path: &str) -> Vec<SymbolRecord> {
    let mut symbols = Vec::new();
    let mut current_container: Option<String> = None;
    let mut brace_depth: i64 = 0;

    for (idx, line) in content.lines().enumerate() {
        let trimmed = line.trim();

        if let Some(pragma) = extract_pragma(trimmed, language) {
            let signature = if pragma.has_separator {
                format!("--- {} ---", pragma.name)
            } else {
                pragma.name.clone()
            };
            symbols.push(make_symbol(
                pragma.name,
                pragma.kind,
                signature,
                idx + 1,
                file_path,
                None,
            ));
        }

        if trimmed.is_empty()
            || trimmed.starts_with("//")
            || trimmed.starts_with("/*")
            || trimmed.starts_with('*')
        {
            continue;
        }

        let opens = trimmed.bytes().filter(|b| *b == b'{').count() as i64;
        let closes = trimmed.bytes().filter(|b| *b == b'}').count() as i64;
        brace_depth += opens - closes;
        if brace_depth <= 0 {
            current_container = None;
            brace_depth = 0;
        }

        let extracted = extract_from_line(trimmed, language, idx + 1, file_path);
        for mut sym in extracted {
            if matches!(
                sym.kind,
                SymbolKind::Function | SymbolKind::Method | SymbolKind::Property
            ) {
                sym.container = current_container.clone();
            }
            if sym.kind.is_type_definition() {
                current_container = Some(sym.name.clone());
            }
            symbols.push(sym);
        }
    }

    symbols
}

/// Build a `SymbolRecord` with the canonical id format.
pub(super) fn make_symbol(
    name: String,
    kind: SymbolKind,
    signature: String,
    line: usize,
    file_path: &str,
    container: Option<String>,
) -> SymbolRecord {
    SymbolRecord {
        id: format!("{file_path}:{name}:{line}"),
        name,
        kind,
        file_path: file_path.to_string(),
        line,
        signature,
        container,
        reference_count: 0,
        importance_score: 0.0,
    }
}

fn extract_from_line(
    line: &str,
    language: &str,
    line_number: usize,
    file_path: &str,
) -> Vec<SymbolRecord> {
    match language {
        "dart" => extract_dart(line, line_number, file_path),
        _ => extract_generic(line, line_number, file_path),
    }
}

// MARK: - Dart (regex fallback)
fn extract_dart(line: &str, line_number: usize, file_path: &str) -> Vec<SymbolRecord> {
    let mut out = Vec::new();
    if let Some(name) = capture(dart_class(), line) {
        out.push(make_symbol(
            name.clone(),
            SymbolKind::ClassDecl,
            format!("class {name}"),
            line_number,
            file_path,
            None,
        ));
        return out;
    }
    if let Some(name) = capture(dart_enum(), line) {
        out.push(make_symbol(
            name.clone(),
            SymbolKind::EnumDecl,
            format!("enum {name}"),
            line_number,
            file_path,
            None,
        ));
        return out;
    }
    if let Some(caps) = dart_function().captures(line) {
        let name = caps.get(1).map(|m| m.as_str()).unwrap_or_default();
        let rest = caps.get(2).map(|m| m.as_str()).unwrap_or_default();
        const RESERVED: &[&str] = &[
            "if", "while", "for", "switch", "return", "catch", "class", "import", "export",
        ];
        if !RESERVED.contains(&name)
            && name
                .chars()
                .next()
                .map(|c| c.is_ascii_lowercase())
                .unwrap_or(false)
        {
            let signature = format!("{name}({}", truncate_signature(rest, 80));
            out.push(make_symbol(
                name.to_string(),
                SymbolKind::Function,
                signature,
                line_number,
                file_path,
                None,
            ));
        }
    }
    out
}

// MARK: - Generic regex fallback
fn extract_generic(line: &str, line_number: usize, file_path: &str) -> Vec<SymbolRecord> {
    if let Some(name) = capture(generic_function(), line) {
        return vec![make_symbol(
            name,
            SymbolKind::Function,
            String::new(),
            line_number,
            file_path,
            None,
        )];
    }
    if let Some(name) = capture(generic_struct(), line) {
        return vec![make_symbol(
            name,
            SymbolKind::StructDecl,
            String::new(),
            line_number,
            file_path,
            None,
        )];
    }
    if let Some(name) = capture(generic_enum(), line) {
        return vec![make_symbol(
            name,
            SymbolKind::EnumDecl,
            String::new(),
            line_number,
            file_path,
            None,
        )];
    }
    if let Some(name) = capture(generic_protocol(), line) {
        return vec![make_symbol(
            name,
            SymbolKind::ProtocolDecl,
            String::new(),
            line_number,
            file_path,
            None,
        )];
    }
    if let Some(name) = capture(generic_class(), line) {
        return vec![make_symbol(
            name,
            SymbolKind::ClassDecl,
            String::new(),
            line_number,
            file_path,
            None,
        )];
    }
    Vec::new()
}

// MARK: - Pragma extraction

#[derive(Clone, Debug)]
struct PragmaMatch {
    name: String,
    kind: SymbolKind,
    has_separator: bool,
}

struct PragmaPattern {
    prefix: &'static str,
    kind: SymbolKind,
    has_separator: bool,
}

const SLASH_PATTERNS: &[PragmaPattern] = &[
    PragmaPattern {
        prefix: "// MARK: - ",
        kind: SymbolKind::Mark,
        has_separator: true,
    },
    PragmaPattern {
        prefix: "// MARK: -",
        kind: SymbolKind::Mark,
        has_separator: true,
    },
    PragmaPattern {
        prefix: "// MARK: ",
        kind: SymbolKind::Mark,
        has_separator: false,
    },
    PragmaPattern {
        prefix: "// MARK:",
        kind: SymbolKind::Mark,
        has_separator: false,
    },
    PragmaPattern {
        prefix: "// TODO: ",
        kind: SymbolKind::Todo,
        has_separator: false,
    },
    PragmaPattern {
        prefix: "// TODO:",
        kind: SymbolKind::Todo,
        has_separator: false,
    },
    PragmaPattern {
        prefix: "// FIXME: ",
        kind: SymbolKind::Fixme,
        has_separator: false,
    },
    PragmaPattern {
        prefix: "// FIXME:",
        kind: SymbolKind::Fixme,
        has_separator: false,
    },
];

const PRAGMA_PATTERNS: &[PragmaPattern] = &[
    PragmaPattern {
        prefix: "#pragma mark - ",
        kind: SymbolKind::Mark,
        has_separator: true,
    },
    PragmaPattern {
        prefix: "#pragma mark -",
        kind: SymbolKind::Mark,
        has_separator: true,
    },
    PragmaPattern {
        prefix: "#pragma mark ",
        kind: SymbolKind::Mark,
        has_separator: false,
    },
];

const HASH_PATTERNS: &[PragmaPattern] = &[
    PragmaPattern {
        prefix: "# MARK: - ",
        kind: SymbolKind::Mark,
        has_separator: true,
    },
    PragmaPattern {
        prefix: "# MARK: -",
        kind: SymbolKind::Mark,
        has_separator: true,
    },
    PragmaPattern {
        prefix: "# MARK: ",
        kind: SymbolKind::Mark,
        has_separator: false,
    },
    PragmaPattern {
        prefix: "# MARK:",
        kind: SymbolKind::Mark,
        has_separator: false,
    },
    PragmaPattern {
        prefix: "# TODO: ",
        kind: SymbolKind::Todo,
        has_separator: false,
    },
    PragmaPattern {
        prefix: "# TODO:",
        kind: SymbolKind::Todo,
        has_separator: false,
    },
    PragmaPattern {
        prefix: "# FIXME: ",
        kind: SymbolKind::Fixme,
        has_separator: false,
    },
    PragmaPattern {
        prefix: "# FIXME:",
        kind: SymbolKind::Fixme,
        has_separator: false,
    },
];

fn extract_pragma(line: &str, language: &str) -> Option<PragmaMatch> {
    let is_python = language == "py";
    let is_cpp = matches!(language, "cpp" | "cc" | "cxx" | "hpp" | "h" | "c" | "hxx");

    if !is_python {
        if let Some(m) = match_patterns(SLASH_PATTERNS, line) {
            return Some(m);
        }
    }
    if is_cpp {
        if let Some(m) = match_patterns(PRAGMA_PATTERNS, line) {
            return Some(m);
        }
    }
    if is_python {
        if let Some(m) = match_patterns(HASH_PATTERNS, line) {
            return Some(m);
        }
    }
    None
}

fn match_patterns(patterns: &[PragmaPattern], line: &str) -> Option<PragmaMatch> {
    for pat in patterns {
        if let Some(rest) = line.strip_prefix(pat.prefix) {
            let trimmed = rest.trim();
            let name = if trimmed.is_empty() {
                pat.kind.keyword().to_ascii_uppercase()
            } else {
                trimmed.to_string()
            };
            return Some(PragmaMatch {
                name,
                kind: pat.kind,
                has_separator: pat.has_separator,
            });
        }
    }
    None
}

// MARK: - Helpers

fn capture(regex: &Regex, line: &str) -> Option<String> {
    regex.captures(line)?.get(1).map(|m| m.as_str().to_string())
}

fn truncate_signature(rest: &str, max_len: usize) -> String {
    let mut depth = 0i32;
    let mut end = 0usize;
    for (i, ch) in rest.char_indices() {
        if ch == '(' {
            depth += 1;
        }
        if ch == ')' {
            depth -= 1;
            if depth <= 0 {
                end = i + ch.len_utf8();
                break;
            }
        }
    }
    let candidate = if end > 0 { &rest[..end] } else { rest };
    if candidate.chars().count() > max_len {
        let truncated: String = candidate.chars().take(max_len).collect();
        format!("{truncated}")
    } else {
        candidate.to_string()
    }
}

macro_rules! pattern {
    ($name:ident, $expr:expr) => {
        fn $name() -> &'static Regex {
            static R: OnceLock<Regex> = OnceLock::new();
            R.get_or_init(|| {
                Regex::new($expr).expect(concat!("invalid pattern: ", stringify!($name)))
            })
        }
    };
}

pattern!(generic_function, r"(?:function|func|def|fn)\s+(\w+)");
pattern!(generic_struct, r"(?:^|\s)struct\s+(\w+)");
pattern!(generic_enum, r"(?:^|\s)enum\s+(\w+)");
pattern!(generic_protocol, r"(?:^|\s)(?:protocol|interface)\s+(\w+)");
pattern!(generic_class, r"(?:^|\s)(?:class|trait|type)\s+(\w+)");
pattern!(dart_class, r"^(?:abstract\s+)?class\s+(\w+)");
pattern!(dart_enum, r"^enum\s+(\w+)");
pattern!(dart_function, r"^\s*(?:\w+\s+)?(\w+)\s*\((.*)");

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

    #[test]
    fn extracts_swift_class_with_method_container() {
        let src = "class Foo {\n    func bar() {}\n}\n";
        let symbols = extract_symbols(src, "swift", "Foo.swift");
        let names: Vec<_> = symbols
            .iter()
            .map(|s| (s.name.as_str(), s.kind, s.container.as_deref()))
            .collect();
        // Class on line 1, function on line 2 with container "Foo"
        assert!(names.contains(&("Foo", SymbolKind::ClassDecl, None)));
        assert!(names.contains(&("bar", SymbolKind::Function, Some("Foo"))));
    }

    #[test]
    fn extracts_pragmas_in_python() {
        let src = "# MARK: - Section\n# TODO: write me\n";
        let symbols = extract_symbols(src, "py", "x.py");
        assert!(symbols.iter().any(|s| s.kind == SymbolKind::Mark));
        assert!(symbols.iter().any(|s| s.kind == SymbolKind::Todo));
    }

    #[test]
    fn extracts_dart_function_with_signature() {
        // The regex fallback matches any `<word> <ident>(` line, so a
        // function-call inside a body shows up as another function entry.
        // Production callers de-dupe at the tree-sitter / outline layer.
        let src = "void greet(String name) {\n  return name.length;\n}\n";
        let symbols = extract_symbols(src, "dart", "main.dart");
        let fns: Vec<_> = symbols
            .iter()
            .filter(|s| s.kind == SymbolKind::Function && s.name == "greet")
            .collect();
        assert_eq!(fns.len(), 1);
        assert!(fns[0].signature.starts_with("greet("));
    }

    #[test]
    fn ids_are_stable_per_file_name_line() {
        let src = "fn one() {}\nfn two() {}\n";
        let symbols = extract_symbols(src, "rs", "lib.rs");
        let ids: Vec<_> = symbols.iter().map(|s| s.id.as_str()).collect();
        assert!(ids.contains(&"lib.rs:one:1"));
        assert!(ids.contains(&"lib.rs:two:2"));
    }
}