car-ast 0.14.0

Tree-sitter AST parsing for code-aware inference
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
use super::{extract_doc_comment, extract_signature, field_text, node_text};
use crate::types::*;

pub fn extract(tree: &tree_sitter::Tree, source: &[u8]) -> (Vec<Symbol>, Vec<Import>) {
    let root = tree.root_node();
    let mut symbols = Vec::new();
    let mut imports = Vec::new();
    let mut cursor = root.walk();

    for child in root.children(&mut cursor) {
        extract_top_level(&child, source, &mut symbols, &mut imports);
    }

    (symbols, imports)
}

fn extract_top_level(
    node: &tree_sitter::Node,
    source: &[u8],
    symbols: &mut Vec<Symbol>,
    imports: &mut Vec<Import>,
) {
    match node.kind() {
        // `function name(...) ... end`
        "function_declaration" => {
            if let Some(sym) = extract_function(node, source) {
                symbols.push(sym);
            }
        }
        // `local function name(...) ... end`
        "local_function" | "local_function_definition_statement" => {
            if let Some(sym) = extract_local_function(node, source) {
                symbols.push(sym);
            }
        }
        // `local x = ...` — could be require or a constant
        "local_variable_declaration" | "variable_declaration" => {
            extract_variable(node, source, symbols, imports);
        }
        // `x = require("mod")` as assignment
        "assignment_statement" | "assignment" => {
            extract_assignment(node, source, symbols, imports);
        }
        // Bare function calls like `require("mod")`
        "function_call" | "function_call_statement" => {
            if is_require_call(node, source) {
                let text = node_text(node, source);
                if let Some(path) = extract_require_path(text) {
                    imports.push(Import {
                        path,
                        alias: None,
                        span: Span::from_node(node),
                    });
                }
            }
        }
        // Some tree-sitter-lua versions wrap statements
        "expression_statement" => {
            let mut inner = node.walk();
            for child in node.children(&mut inner) {
                if child.kind() == "function_call" || child.kind() == "function_call_statement" {
                    if is_require_call(&child, source) {
                        let text = node_text(&child, source);
                        if let Some(path) = extract_require_path(text) {
                            imports.push(Import {
                                path,
                                alias: None,
                                span: Span::from_node(&child),
                            });
                        }
                    }
                }
            }
        }
        // Return statements at top-level (module pattern)
        "return_statement" => {}
        _ => {}
    }
}

fn extract_function(node: &tree_sitter::Node, source: &[u8]) -> Option<Symbol> {
    let name = extract_function_name(node, source)?;

    // Check if it's a method (contains ':')
    let kind = if name.contains(':') {
        SymbolKind::Method
    } else if name.contains('.') {
        SymbolKind::Function
    } else {
        SymbolKind::Function
    };

    let parent = if name.contains(':') || name.contains('.') {
        Some(name.split(&[':', '.'][..]).next().unwrap_or("").to_string())
    } else {
        None
    };

    // Get just the method/function part for the name
    let short_name = name
        .rsplit(&[':', '.'][..])
        .next()
        .unwrap_or(&name)
        .to_string();

    Some(Symbol {
        name: short_name,
        kind,
        span: Span::from_node(node),
        signature: extract_signature(node, "body", source),
        doc_comment: extract_doc_comment(node, source),
        parent,
        children: Vec::new(),
    })
}

fn extract_local_function(node: &tree_sitter::Node, source: &[u8]) -> Option<Symbol> {
    let name = extract_function_name(node, source)?;

    Some(Symbol {
        name,
        kind: SymbolKind::Function,
        span: Span::from_node(node),
        signature: extract_signature(node, "body", source),
        doc_comment: extract_doc_comment(node, source),
        parent: None,
        children: Vec::new(),
    })
}

fn extract_function_name(node: &tree_sitter::Node, source: &[u8]) -> Option<String> {
    // Try "name" field first
    if let Some(name) = field_text(node, "name", source) {
        if !name.is_empty() {
            return Some(name.to_string());
        }
    }

    // Walk children for identifier/dot_index/method_index
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        match child.kind() {
            "identifier" | "IDENTIFIER" => {
                let t = node_text(&child, source);
                if !t.is_empty() && t != "function" && t != "local" {
                    return Some(t.to_string());
                }
            }
            "dot_index_expression" | "method_index_expression" => {
                return Some(node_text(&child, source).to_string());
            }
            "function_name" | "function_name_field" => {
                return Some(node_text(&child, source).to_string());
            }
            _ => {}
        }
    }

    // Fallback: parse from text
    let text = node_text(node, source);
    extract_name_from_function_text(text)
}

fn extract_name_from_function_text(text: &str) -> Option<String> {
    let text = text.trim();
    let text = text.strip_prefix("local ").unwrap_or(text);
    let text = text.strip_prefix("function ").unwrap_or(text);
    let paren = text.find('(')?;
    let name = text[..paren].trim();
    if name.is_empty() {
        None
    } else {
        Some(name.to_string())
    }
}

fn extract_variable(
    node: &tree_sitter::Node,
    source: &[u8],
    symbols: &mut Vec<Symbol>,
    imports: &mut Vec<Import>,
) {
    let text = node_text(node, source);

    // Check for require: `local x = require("mod")`
    if text.contains("require") {
        if let Some(path) = extract_require_path(text) {
            let alias =
                extract_local_var_name(node, source).or_else(|| extract_var_name_from_text(text));
            imports.push(Import {
                path,
                alias,
                span: Span::from_node(node),
            });
            return;
        }
    }

    // Check for local function assigned to variable: `local f = function(...) end`
    if text.contains("function") && text.contains("end") {
        if let Some(name) =
            extract_local_var_name(node, source).or_else(|| extract_var_name_from_text(text))
        {
            symbols.push(Symbol {
                name,
                kind: SymbolKind::Function,
                span: Span::from_node(node),
                signature: first_line(text).to_string(),
                doc_comment: extract_doc_comment(node, source),
                parent: None,
                children: Vec::new(),
            });
            return;
        }
    }

    // Otherwise treat as a constant/variable
    if let Some(name) =
        extract_local_var_name(node, source).or_else(|| extract_var_name_from_text(text))
    {
        symbols.push(Symbol {
            name,
            kind: SymbolKind::Const,
            span: Span::from_node(node),
            signature: first_line(text).to_string(),
            doc_comment: extract_doc_comment(node, source),
            parent: None,
            children: Vec::new(),
        });
    }
}

fn extract_assignment(
    node: &tree_sitter::Node,
    source: &[u8],
    symbols: &mut Vec<Symbol>,
    imports: &mut Vec<Import>,
) {
    let text = node_text(node, source);

    if text.contains("require") {
        if let Some(path) = extract_require_path(text) {
            let alias = extract_var_name_from_text(text);
            imports.push(Import {
                path,
                alias,
                span: Span::from_node(node),
            });
            return;
        }
    }

    // Assignment of function: `M.foo = function(...) end`
    if text.contains("function") {
        if let Some(name) = extract_var_name_from_text(text) {
            symbols.push(Symbol {
                name,
                kind: SymbolKind::Function,
                span: Span::from_node(node),
                signature: first_line(text).to_string(),
                doc_comment: extract_doc_comment(node, source),
                parent: None,
                children: Vec::new(),
            });
            return;
        }
    }

    // Regular assignment as Const
    if let Some(name) = extract_var_name_from_text(text) {
        symbols.push(Symbol {
            name,
            kind: SymbolKind::Const,
            span: Span::from_node(node),
            signature: first_line(text).to_string(),
            doc_comment: extract_doc_comment(node, source),
            parent: None,
            children: Vec::new(),
        });
    }
}

fn extract_local_var_name(node: &tree_sitter::Node, source: &[u8]) -> Option<String> {
    // Try "name" field
    if let Some(name) = field_text(node, "name", source) {
        if !name.is_empty() {
            return Some(name.to_string());
        }
    }
    // Walk children for first identifier
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        if child.kind() == "identifier" || child.kind() == "variable_list" {
            let t = node_text(&child, source).trim().to_string();
            if !t.is_empty() && t != "local" {
                return Some(t);
            }
        }
    }
    None
}

fn extract_var_name_from_text(text: &str) -> Option<String> {
    let text = text.trim();
    let text = text.strip_prefix("local ").unwrap_or(text);
    // Find `=`
    let eq = text.find('=')?;
    let name = text[..eq].trim();
    if name.is_empty() {
        None
    } else {
        Some(name.to_string())
    }
}

fn extract_require_path(text: &str) -> Option<String> {
    // require("path") or require('path') or require "path"
    let start_dq = text.find("require");
    if let Some(req_pos) = start_dq {
        let after = &text[req_pos + 7..];
        // Find opening quote
        if let Some(q_start) = after.find(|c: char| c == '"' || c == '\'') {
            let quote_char = after.as_bytes()[q_start] as char;
            let inner = &after[q_start + 1..];
            if let Some(q_end) = inner.find(quote_char) {
                return Some(inner[..q_end].to_string());
            }
        }
    }
    None
}

fn is_require_call(node: &tree_sitter::Node, source: &[u8]) -> bool {
    let text = node_text(node, source);
    text.trim().starts_with("require")
}

fn first_line(text: &str) -> &str {
    text.lines().next().unwrap_or(text)
}

#[cfg(test)]
mod tests {
    use crate::{parse, Language, SymbolKind};

    #[test]
    fn test_lua_extract() {
        let source = r#"local json = require("cjson")
local utils = require('utils')

local MAX_SIZE = 1024

local function helper(x)
    return x * 2
end

function greet(name)
    print("Hello, " .. name)
end

function M:method(arg)
    self.value = arg
end

local tbl = {}
"#;
        let parsed = parse(source, Language::Lua);
        if let Some(parsed) = parsed {
            let names: Vec<&str> = parsed.symbols.iter().map(|s| s.name.as_str()).collect();
            let kinds: Vec<(&str, SymbolKind)> = parsed
                .symbols
                .iter()
                .map(|s| (s.name.as_str(), s.kind))
                .collect();
            let import_paths: Vec<&str> = parsed.imports.iter().map(|i| i.path.as_str()).collect();

            // Check imports
            assert!(
                import_paths.iter().any(|p| *p == "cjson"),
                "Should find cjson import, got: {:?}",
                import_paths
            );
            assert!(
                import_paths.iter().any(|p| *p == "utils"),
                "Should find utils import, got: {:?}",
                import_paths
            );

            // Check functions
            assert!(
                kinds
                    .iter()
                    .any(|(n, k)| *n == "helper" && *k == SymbolKind::Function),
                "Should find 'helper' function, got: {:?}",
                kinds
            );
            assert!(
                kinds
                    .iter()
                    .any(|(n, k)| *n == "greet" && *k == SymbolKind::Function),
                "Should find 'greet' function, got: {:?}",
                kinds
            );

            // Check method
            assert!(
                kinds
                    .iter()
                    .any(|(n, k)| *n == "method" && *k == SymbolKind::Method),
                "Should find 'method' method, got: {:?}",
                kinds
            );

            // Check constant
            assert!(
                kinds
                    .iter()
                    .any(|(n, k)| *n == "MAX_SIZE" && *k == SymbolKind::Const),
                "Should find 'MAX_SIZE' const, got: {:?}",
                kinds
            );

            eprintln!("Lua symbols: {:?}", names);
            eprintln!("Lua imports: {:?}", import_paths);
        } else {
            eprintln!("Lua parser not available or parse failed — skipping assertions");
        }
    }
}