arbor-core 3.1.0

AST parsing and code analysis for Arbor
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//! Lightweight fallback parser for emerging language support.
//!
//! This parser is intentionally heuristic-based (line scanner + simple token rules)
//! so Arbor can provide useful symbol indexing for languages that are not yet
//! wired to a full Tree-sitter grammar in every runtime path.

use crate::node::{CodeNode, NodeKind};

/// Extra language extensions indexed into the **code** graph via fallback parsing.
///
/// Markdown is deliberately absent. It is parseable (see [`MARKDOWN_EXTENSIONS`])
/// but headings are prose, not symbols: indexing them put a node named `fetch`
/// into the graph for a `# fetch benchmark` heading in a README, which then
/// competed with real `fetch` definitions when resolving TypeScript call sites.
/// Callers that want a document graph ask for it explicitly.
pub const FALLBACK_EXTENSIONS: &[&str] = &[
    "kt", "kts",   // Kotlin
    "swift", // Swift
    "rb",    // Ruby
    "php", "phtml", // PHP
    "sh", "bash", "zsh", // Shell
];

/// Markdown extensions, parsed into `Section` nodes for document graphs.
///
/// Not part of [`FALLBACK_EXTENSIONS`] — opt in by calling
/// [`parse_fallback_source`] with one of these directly.
pub const MARKDOWN_EXTENSIONS: &[&str] = &["md", "markdown"];

pub fn is_fallback_supported_extension(ext: &str) -> bool {
    let ext = ext.to_ascii_lowercase();
    FALLBACK_EXTENSIONS.iter().any(|e| *e == ext)
}

/// Whether this extension is parsed as prose rather than code.
pub fn is_markdown_extension(ext: &str) -> bool {
    let ext = ext.to_ascii_lowercase();
    MARKDOWN_EXTENSIONS.iter().any(|e| *e == ext)
}

pub fn parse_fallback_source(source: &str, file_path: &str, ext: &str) -> Vec<CodeNode> {
    let ext = ext.to_ascii_lowercase();
    let is_markdown = is_markdown_extension(&ext);
    let mut nodes = Vec::new();
    let mut pending_annotations: Vec<String> = Vec::new();

    for (idx, line) in source.lines().enumerate() {
        let line_no = idx as u32 + 1;
        let trimmed = line.trim_start();

        if trimmed.is_empty() {
            continue;
        }

        // Comments are never declarations. Skip them *before* parsing: the
        // shell rule looks for `()` anywhere on the line, so the comment
        // `# Compare app.fetch() between refs` used to yield a function named
        // "# Compare app.fetch". Markdown is exempt — there `#` starts a
        // heading, which is the thing we want.
        if !is_markdown && (trimmed.starts_with('#') || trimmed.starts_with("//")) {
            pending_annotations.clear();
            continue;
        }

        // Keep Hilt/Android annotations on the following declaration so entry
        // detection can see `@AndroidEntryPoint` without treating `@` names as symbols.
        if !is_markdown && trimmed.starts_with('@') {
            pending_annotations.push(trimmed.to_string());
            continue;
        }

        let candidate = match ext.as_str() {
            "md" | "markdown" => parse_markdown_line(trimmed),
            "kt" | "kts" => parse_kotlin_line(trimmed),
            "swift" => parse_swift_line(trimmed),
            "rb" => parse_ruby_line(trimmed),
            "php" | "phtml" => parse_php_line(trimmed),
            "sh" | "bash" | "zsh" => parse_shell_line(trimmed),
            _ => None,
        };

        if let Some((name, kind)) = candidate {
            let col = (line.len().saturating_sub(trimmed.len())) as u32;
            let signature = if pending_annotations.is_empty() {
                trimmed.to_string()
            } else {
                let mut signature = pending_annotations.join(" ");
                signature.push(' ');
                signature.push_str(trimmed);
                signature
            };
            pending_annotations.clear();
            let node = CodeNode::new(&name, &name, kind, file_path)
                .with_lines(line_no, line_no)
                .with_column(col)
                .with_signature(signature);
            nodes.push(node);
        } else {
            pending_annotations.clear();
        }
    }

    nodes
}

fn parse_kotlin_line(line: &str) -> Option<(String, NodeKind)> {
    let line = strip_kotlin_modifiers(line);
    if let Some(rest) = line.strip_prefix("fun ") {
        return take_ident(rest).map(|name| (name, NodeKind::Function));
    }

    if let Some(rest) = line.strip_prefix("class ") {
        return take_ident(rest).map(|name| (name, NodeKind::Class));
    }

    if let Some(rest) = line.strip_prefix("interface ") {
        return take_ident(rest).map(|name| (name, NodeKind::Interface));
    }

    if let Some(rest) = line.strip_prefix("object ") {
        return take_ident(rest).map(|name| (name, NodeKind::Class));
    }

    if let Some(rest) = line.strip_prefix("enum class ") {
        return take_ident(rest).map(|name| (name, NodeKind::Enum));
    }

    None
}

fn strip_kotlin_modifiers(line: &str) -> &str {
    let mut rest = line.trim_start();
    loop {
        let stripped = [
            "public ",
            "private ",
            "protected ",
            "internal ",
            "open ",
            "abstract ",
            "sealed ",
            "inner ",
            "data ",
            "override ",
            "lateinit ",
        ]
        .iter()
        .find_map(|prefix| rest.strip_prefix(prefix));
        match stripped {
            Some(next) => rest = next.trim_start(),
            None => return rest,
        }
    }
}

fn parse_swift_line(line: &str) -> Option<(String, NodeKind)> {
    if let Some(rest) = line.strip_prefix("func ") {
        return take_ident(rest).map(|name| (name, NodeKind::Function));
    }

    if let Some(rest) = line.strip_prefix("class ") {
        return take_ident(rest).map(|name| (name, NodeKind::Class));
    }

    if let Some(rest) = line.strip_prefix("struct ") {
        return take_ident(rest).map(|name| (name, NodeKind::Struct));
    }

    if let Some(rest) = line.strip_prefix("enum ") {
        return take_ident(rest).map(|name| (name, NodeKind::Enum));
    }

    if let Some(rest) = line.strip_prefix("protocol ") {
        return take_ident(rest).map(|name| (name, NodeKind::Interface));
    }

    if let Some(rest) = line.strip_prefix("extension ") {
        return take_ident(rest).map(|name| (name, NodeKind::Module));
    }

    None
}

fn parse_ruby_line(line: &str) -> Option<(String, NodeKind)> {
    if let Some(rest) = line.strip_prefix("def ") {
        return take_ident(rest.trim_start_matches("self.")).map(|name| (name, NodeKind::Function));
    }

    if let Some(rest) = line.strip_prefix("class ") {
        return take_ident(rest).map(|name| (name, NodeKind::Class));
    }

    if let Some(rest) = line.strip_prefix("module ") {
        return take_ident(rest).map(|name| (name, NodeKind::Module));
    }

    None
}

fn parse_php_line(line: &str) -> Option<(String, NodeKind)> {
    if let Some(rest) = line.strip_prefix("function ") {
        return take_ident(rest).map(|name| (name, NodeKind::Function));
    }

    if let Some(rest) = line.strip_prefix("class ") {
        return take_ident(rest).map(|name| (name, NodeKind::Class));
    }

    if let Some(rest) = line.strip_prefix("interface ") {
        return take_ident(rest).map(|name| (name, NodeKind::Interface));
    }

    if let Some(rest) = line.strip_prefix("trait ") {
        return take_ident(rest).map(|name| (name, NodeKind::Interface));
    }

    None
}

fn parse_shell_line(line: &str) -> Option<(String, NodeKind)> {
    if let Some(rest) = line.strip_prefix("function ") {
        return take_ident(rest).map(|name| (name, NodeKind::Function));
    }

    // foo() {
    if let Some(paren_idx) = line.find("()") {
        let name = line[..paren_idx].trim();
        // The name must be a bare shell identifier. Without this check any
        // line containing `()` anywhere — prose, a call, a pipeline — yielded
        // a "function" whose name was the whole preceding text.
        if is_shell_identifier(name) {
            return Some((name.to_string(), NodeKind::Function));
        }
    }

    None
}

/// Whether a string is a plain shell function name.
fn is_shell_identifier(s: &str) -> bool {
    !s.is_empty()
        && s.chars()
            .next()
            .is_some_and(|c| c.is_ascii_alphabetic() || c == '_')
        && s.chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
}

fn parse_markdown_line(line: &str) -> Option<(String, NodeKind)> {
    let trimmed = line.trim_start();
    if let Some(rest) = trimmed.strip_prefix("# ") {
        return take_ident(rest).map(|name| (name, NodeKind::Section));
    }
    if let Some(rest) = trimmed.strip_prefix("## ") {
        return take_ident(rest).map(|name| (name, NodeKind::Section));
    }
    if let Some(rest) = trimmed.strip_prefix("### ") {
        return take_ident(rest).map(|name| (name, NodeKind::Section));
    }
    // Support ## Heading with ID or other variants
    if trimmed.starts_with("#") && trimmed.contains(' ') {
        let name = trimmed
            .split_whitespace()
            .nth(1)
            .unwrap_or(trimmed)
            .trim_start_matches('#')
            .trim();
        if !name.is_empty() {
            return Some((name.to_string(), NodeKind::Section));
        }
    }
    None
}

fn take_ident(input: &str) -> Option<String> {
    let mut out = String::new();
    for ch in input.chars() {
        if ch.is_alphanumeric() || ch == '_' || ch == '-' {
            out.push(ch);
        } else {
            break;
        }
    }

    if out.is_empty() {
        None
    } else {
        Some(out)
    }
}

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

    #[test]
    fn fallback_supports_requested_extensions() {
        for ext in ["kt", "swift", "rb", "php", "sh"] {
            assert!(is_fallback_supported_extension(ext));
        }
        // Markdown moved out of the code-graph set — headings are prose, and
        // indexing them polluted symbol resolution. Still parseable on request.
        assert!(!is_fallback_supported_extension("md"));
        assert!(is_markdown_extension("md"));
    }

    #[test]
    fn parses_kotlin_function() {
        let source = "fun fetchUser(id: String): User = TODO()";
        let nodes = parse_fallback_source(source, "sample.kt", "kt");
        assert!(nodes.iter().any(|n| n.name == "fetchUser"));
    }

    #[test]
    fn parses_shell_function() {
        let source = "deploy_prod() { echo hi; }";
        let nodes = parse_fallback_source(source, "deploy.sh", "sh");
        assert!(nodes.iter().any(|n| n.name == "deploy_prod"));
    }

    #[test]
    fn parses_swift_source() {
        let source = r#"
class UserManager {
    func getUser() -> User {
        return User()
    }
}

struct Point {
    var x: Double
    var y: Double
}

enum Status {
    case active
    case inactive
}
"#;
        let nodes = parse_fallback_source(source, "Users.swift", "swift");
        assert!(nodes
            .iter()
            .any(|n| n.name == "UserManager" && matches!(n.kind, NodeKind::Class)));
        assert!(nodes
            .iter()
            .any(|n| n.name == "getUser" && matches!(n.kind, NodeKind::Function)));
        assert!(nodes
            .iter()
            .any(|n| n.name == "Point" && matches!(n.kind, NodeKind::Struct)));
        assert!(nodes
            .iter()
            .any(|n| n.name == "Status" && matches!(n.kind, NodeKind::Enum)));
    }

    #[test]
    fn parses_ruby_source() {
        let source = r#"
class ApplicationController
  def index
    render json: { status: "ok" }
  end

  def show
    @user = User.find(params[:id])
  end
end

module Authentication
end
"#;
        let nodes = parse_fallback_source(source, "controller.rb", "rb");
        assert!(nodes
            .iter()
            .any(|n| n.name == "ApplicationController" && matches!(n.kind, NodeKind::Class)));
        assert!(nodes
            .iter()
            .any(|n| n.name == "index" && matches!(n.kind, NodeKind::Function)));
        assert!(nodes
            .iter()
            .any(|n| n.name == "show" && matches!(n.kind, NodeKind::Function)));
        assert!(nodes
            .iter()
            .any(|n| n.name == "Authentication" && matches!(n.kind, NodeKind::Module)));
    }

    #[test]
    fn parses_php_source() {
        let source = r#"
class PaymentProcessor {
    function processPayment($amount) {
        return true;
    }
}

interface Gateway {
}

function helper() {
}
"#;
        let nodes = parse_fallback_source(source, "payment.php", "php");
        assert!(nodes
            .iter()
            .any(|n| n.name == "PaymentProcessor" && matches!(n.kind, NodeKind::Class)));
        assert!(nodes
            .iter()
            .any(|n| n.name == "processPayment" && matches!(n.kind, NodeKind::Function)));
        assert!(nodes
            .iter()
            .any(|n| n.name == "Gateway" && matches!(n.kind, NodeKind::Interface)));
        assert!(nodes
            .iter()
            .any(|n| n.name == "helper" && matches!(n.kind, NodeKind::Function)));
    }

    #[test]
    fn fallback_ignores_comments() {
        // Lines starting with // or # should not produce nodes
        let source = r#"
// class NotAClass
# def not_a_function
fun realFunction(x: Int): Int = x
"#;
        let nodes = parse_fallback_source(source, "test.kt", "kt");
        assert!(!nodes.iter().any(|n| n.name == "NotAClass"));
        assert!(!nodes.iter().any(|n| n.name == "not_a_function"));
        assert!(nodes.iter().any(|n| n.name == "realFunction"));
    }

    #[test]
    fn fallback_kotlin_class_and_data_class() {
        let source = r#"
class Repository {
}
data class UserDto(val name: String)
object Singleton
"#;
        let nodes = parse_fallback_source(source, "models.kt", "kt");
        assert!(nodes
            .iter()
            .any(|n| n.name == "Repository" && matches!(n.kind, NodeKind::Class)));
        assert!(nodes
            .iter()
            .any(|n| n.name == "UserDto" && matches!(n.kind, NodeKind::Class)));
        assert!(nodes
            .iter()
            .any(|n| n.name == "Singleton" && matches!(n.kind, NodeKind::Class)));
    }

    #[test]
    fn kotlin_annotation_stays_on_the_following_class() {
        let source = r#"
@HiltAndroidApp
open class MyApplication : Application()

@AndroidEntryPoint
class MainActivity : AppCompatActivity()
"#;
        let nodes = parse_fallback_source(source, "app/MyApplication.kt", "kt");
        let app = nodes.iter().find(|n| n.name == "MyApplication").unwrap();
        assert!(app
            .signature
            .as_deref()
            .unwrap()
            .contains("@HiltAndroidApp"));
        let activity = nodes.iter().find(|n| n.name == "MainActivity").unwrap();
        assert!(activity
            .signature
            .as_deref()
            .unwrap()
            .contains("@AndroidEntryPoint"));
    }

    #[test]
    fn fallback_empty_source_returns_empty() {
        let nodes = parse_fallback_source("", "empty.kt", "kt");
        assert!(nodes.is_empty());
    }

    #[test]
    fn fallback_unsupported_extension_returns_empty() {
        // Make sure unsupported extensions don't panic
        assert!(!is_fallback_supported_extension("xyz"));
        assert!(!is_fallback_supported_extension("rs"));
    }

    #[test]
    fn parses_shell_function_keyword() {
        let source = "function deploy_staging { echo staging; }";
        let nodes = parse_fallback_source(source, "deploy.bash", "bash");
        assert!(nodes.iter().any(|n| n.name == "deploy_staging"));
    }
}

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

    #[test]
    fn markdown_is_not_indexed_as_code() {
        // A `# fetch benchmark` heading in a README produced a graph node named
        // `fetch`, which then competed with real `fetch` definitions when
        // resolving TypeScript call sites.
        assert!(!is_fallback_supported_extension("md"));
        assert!(!is_fallback_supported_extension("markdown"));
        assert!(is_markdown_extension("md"));
    }

    #[test]
    fn markdown_still_parses_when_asked_directly() {
        let nodes = parse_fallback_source("# fetch benchmark\n## setup\n", "README.md", "md");
        assert_eq!(nodes.len(), 2);
        assert!(nodes.iter().all(|n| n.kind == NodeKind::Section));
    }

    #[test]
    fn shell_comments_are_not_functions() {
        // `# Compare app.fetch() between the working tree and a git ref`
        // matched the `()` rule and became a function named
        // "# Compare app.fetch".
        let src = "#!/bin/bash\n# Compare app.fetch() between refs\nreal_fn() {\n  echo hi\n}\n";
        let nodes = parse_fallback_source(src, "compare.sh", "sh");

        assert_eq!(nodes.len(), 1, "only the real function should be indexed");
        assert_eq!(nodes[0].name, "real_fn");
    }

    #[test]
    fn shell_names_must_be_identifiers() {
        assert!(is_shell_identifier("deploy_app"));
        assert!(is_shell_identifier("build-web"));
        assert!(!is_shell_identifier("# Compare app.fetch"));
        assert!(!is_shell_identifier("app.fetch"));
        assert!(!is_shell_identifier(""));
        assert!(!is_shell_identifier("2fast"));
    }

    #[test]
    fn prose_containing_parens_is_not_a_function() {
        let nodes = parse_fallback_source("echo \"call foo() now\"\n", "x.sh", "sh");
        assert!(
            nodes.is_empty(),
            "a string mentioning foo() is not a definition"
        );
    }

    #[test]
    fn double_slash_comments_are_skipped() {
        let nodes = parse_fallback_source("// fun notReal()\nfun real() {}\n", "a.kt", "kt");
        assert_eq!(nodes.len(), 1);
        assert_eq!(nodes[0].name, "real");
    }
}