asterisk 0.1.0

Universal language-agnostic AST walking and accurate call stack generation with tree-sitter
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
use crate::block::{Block, BlockType};
use crate::config::{Config, Matchers};
use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::Path;
use tree_sitter::{Language, Node, Parser};

use crate::indexer::generate_node_key;

// C FFI bindings to the tree-sitter language libraries.
extern "C" {
    fn tree_sitter_rust() -> Language;
    fn tree_sitter_python() -> Language;
    // Add more language bindings here
}

/// Parses a code file and returns a vector of `Block`s representing the code structure.
///
/// # Arguments
///
/// * `file_path` - The path of the file to parse.
/// * `module_name` - The name of the module containing the file.
/// * `config` - The `Config` instance containing language-specific settings.
///
/// # Returns
///
/// A vector of `Block`s representing the code structure of the parsed file.
pub fn parse_file(file_path: &Path, module_name: &str, config: &Config) -> Vec<Block> {
    let code = fs::read_to_string(file_path).unwrap();
    let language = tree_sitter_language(file_path);
    let mut parser = Parser::new();
    parser.set_language(language).unwrap();
    let tree = parser.parse(&code, None).unwrap();

    let mut blocks = Vec::new();
    let mut non_function_blocks = Vec::new();
    let mut imports = HashMap::new();
    let mut cursor = tree.root_node().walk();

    traverse_tree(
        &code,
        &mut cursor,
        &mut blocks,
        &mut non_function_blocks,
        language,
        None,
        module_name,
        &mut imports,
        &config,
    );

    if !non_function_blocks.is_empty() {
        let non_function_block_content = non_function_blocks.join("\n");
        blocks.push(Block::new(
            String::from("non_function_block"),
            BlockType::NonFunction,
            non_function_block_content,
            None,
            None,
        ));
    }

    blocks
}

/// Returns the appropriate tree-sitter `Language` for a given file based on its extension.
///
/// # Arguments
///
/// * `file_path` - The path of the file to get the language for.
///
/// # Returns
///
/// The tree-sitter `Language` corresponding to the file's extension.
///
/// # Panics
///
/// Panics if the file's extension is not supported.
fn tree_sitter_language(file_path: &Path) -> Language {
    let extension = file_path
        .extension()
        .and_then(|ext| ext.to_str())
        .unwrap_or("");
    match extension {
        "rs" => unsafe { tree_sitter_rust() },
        "py" => unsafe { tree_sitter_python() },
        // Add more mappings for other supported languages
        _ => panic!("Unsupported language"),
    }
}

/// Recursively traverses the AST and extracts code blocks and call information.
///
/// # Arguments
///
/// * `code` - The code string of the file being parsed.
/// * `cursor` - A mutable reference to the `TreeCursor` used to navigate the AST.
/// * `blocks` - A mutable reference to the vector of `Block`s to populate.
/// * `non_function_blocks` - A mutable reference to the vector of non-function block strings.
/// * `language` - The tree-sitter `Language` of the file being parsed.
/// * `class_name` - An optional string representing the name of the current class, if any.
/// * `module_name` - The name of the module containing the file being parsed.
/// * `imports` - A mutable reference to the map of import aliases to their full module names.
/// * `config` - The `Config` instance containing language-specific settings.
fn traverse_tree(
    code: &str,
    cursor: &mut tree_sitter::TreeCursor,
    blocks: &mut Vec<Block>,
    non_function_blocks: &mut Vec<String>,
    language: Language,
    class_name: Option<String>,
    module_name: &str,
    imports: &mut HashMap<String, String>,
    config: &Config,
) {
    let node = cursor.node();
    let kind = node.kind();

    if is_import_statement(kind, language) {
        if let Some((module, alias)) = parse_import_statement(code, node, language, config) {
            println!("Module: {}, Alias: {}", module, alias);
            imports.insert(alias, module);
        }
    } else if is_class_definition(kind, language) {
        let class_name_node = node.child_by_field_name("name");
        if let Some(class_name_node) = class_name_node {
            let extracted_class_name = class_name_node
                .utf8_text(code.as_bytes())
                .unwrap()
                .to_string();

            if cursor.goto_first_child() {
                loop {
                    traverse_tree(
                        code,
                        cursor,
                        blocks,
                        non_function_blocks,
                        language,
                        Some(extracted_class_name.clone()),
                        module_name,
                        imports,
                        config,
                    );
                    if !cursor.goto_next_sibling() {
                        break;
                    }
                }
                cursor.goto_parent();
            }
        }
    } else if is_function_node(kind, language) {
        let function_name = get_function_name(code, node, language)
            .unwrap_or_else(|| "UnnamedFunction".to_string());
        let block_type = BlockType::Function;
        let block_content = node.utf8_text(code.as_bytes()).unwrap().to_string();

        let node_key = generate_node_key(
            Path::new(module_name),
            class_name.as_deref(),
            &function_name,
        );

        let mut block = Block::new(
            node_key,
            block_type,
            block_content,
            Some(function_name.clone()),
            class_name.clone(),
        );

        block.outgoing_calls = find_calls(code, node, language, module_name, imports);

        blocks.push(block);
    } else if !node.is_named() {
        let block_content = node.utf8_text(code.as_bytes()).unwrap().to_string();
        non_function_blocks.push(block_content);
    }

    if cursor.goto_first_child() {
        loop {
            traverse_tree(
                code,
                cursor,
                blocks,
                non_function_blocks,
                language,
                class_name.clone(),
                module_name,
                imports,
                &config,
            );
            if !cursor.goto_next_sibling() {
                break;
            }
        }
        cursor.goto_parent();
    }
}

/// Finds the function calls made within a given AST node and returns their keys.
///
/// # Arguments
///
/// * `code` - The code string of the file being parsed.
/// * `root` - The AST node to search for function calls.
/// * `language` - The tree-sitter `Language` of the file being parsed.
/// * `module_name` - The name of the module containing the file being parsed.
/// * `imports` - A reference to the map of import aliases to their full module names.
///
/// # Returns
///
/// A vector of strings representing the keys of the called functions.
fn find_calls(
    code: &str,
    root: Node,
    language: Language,
    module_name: &str,
    imports: &HashMap<String, String>,
) -> Vec<String> {
    let mut calls = HashSet::new();
    let mut cursor = root.walk();

    loop {
        let node = cursor.node();

        if is_call_expression(node.kind(), language) {
            if let Some(function_name) = get_call_expression_name(code, node, language) {
                let parts: Vec<&str> = function_name.split('.').collect();

                if parts.len() > 1 {
                    // This is for method calls on an object; the part before '.' is treated as an object, not a module.
                    let object_name = parts[0];
                    let method_name = &parts[1..].join(".");

                    // If the object name matches an alias from the imports, resolve to the correct module.
                    if let Some(imported_module) = imports.get(object_name) {
                        let call_key = generate_node_key(
                            Path::new(imported_module),
                            Some(object_name),
                            method_name,
                        );
                        calls.insert(call_key);
                    } else {
                        let call_key = generate_node_key(
                            Path::new(module_name),
                            Some(object_name),
                            method_name,
                        );
                        calls.insert(call_key);
                    }
                } else {
                    // For global function calls, check if the function name matches an alias from the imports.
                    if let Some(imported_module) = imports.get(&function_name) {
                        let call_key = generate_node_key(
                            Path::new(&format!("test-code-base/{}.py", imported_module)),
                            None,
                            &function_name,
                        );
                        calls.insert(call_key);
                    } else {
                        let function_key =
                            generate_node_key(Path::new(module_name), None, &function_name);
                        calls.insert(function_key);
                    }
                }
            }
        }

        if !cursor.goto_first_child() {
            while !cursor.goto_next_sibling() {
                if !cursor.goto_parent() {
                    return calls.into_iter().collect();
                }
            }
        }
    }
}

/// Checks if an AST node represents an import statement in the given language.
///
/// # Arguments
///
/// * `kind` - The kind (type) of the AST node.
/// * `language` - The tree-sitter `Language` of the file being parsed.
///
/// # Returns
///
/// `true` if the node represents an import statement, `false` otherwise.
fn is_import_statement(kind: &str, language: Language) -> bool {
    match language {
        lang if lang == unsafe { tree_sitter_python() } => {
            kind == "import_statement" || kind == "import_from_statement"
        }
        lang if lang == unsafe { tree_sitter_rust() } => kind == "use_declaration",
        // Add more language-specific checks here
        _ => false,
    }
}

/// Filters the children of an import statement node using the provided matchers.
///
/// # Arguments
///
/// * `child` - The child node of the import statement to filter.
/// * `code` - The code string of the file being parsed.
/// * `matchers` - The `Matchers` instance containing the field names and node types to match.
///
/// # Returns
///
/// A tuple containing:
/// - An optional string representing the module name.
/// - An optional string representing the imported object name.
/// - An optional string representing the import alias.
fn filter_import_matchers(
    child: Node,
    code: &str,
    matchers: &Matchers,
) -> (Option<String>, Option<String>, Option<String>) {
    let module = child
        .child_by_field_name(&matchers.module_name.field_name)
        .map(|n| {
            if n.kind() == matchers.module_name.kind {
                return n.utf8_text(code.as_bytes()).unwrap_or_default().to_owned();
            }

            String::default()
        });

    let name = child
        .child_by_field_name(&matchers.object_name.field_name)
        .map(|n| {
            if n.kind() == matchers.object_name.kind {
                return n.utf8_text(code.as_bytes()).unwrap_or_default().to_owned();
            }

            String::default()
        });

    let alias = child
        .child_by_field_name(&matchers.alias.field_name)
        .map(|n| {
            if n.kind() == matchers.alias.kind {
                return n.utf8_text(code.as_bytes()).unwrap_or_default().to_owned();
            }

            String::default()
        });

    (module, name, alias)
}

/// Parses an import statement node and returns the imported module and alias, if any.
///
/// # Arguments
///
/// * `code` - The code string of the file being parsed.
/// * `node` - The import statement AST node to parse.
/// * `language` - The tree-sitter `Language` of the file being parsed.
/// * `config` - The `Config` instance containing language-specific settings.
///
/// # Returns
///
/// An `Option` containing a tuple of the imported module name and alias, if successfully parsed.
fn parse_import_statement(
    code: &str,
    node: Node,
    language: Language,
    config: &Config,
) -> Option<(String, String)> {
    let mut module_name = String::new();
    let mut object_name = String::new();
    let mut alias_name = String::new();

    match language {
        lang if lang == unsafe { tree_sitter_python() } => {
            let matchers = &config
                .languages
                .get("python")
                .expect("Failed to get Python matchers from config")
                .matchers;

            if node.kind() == matchers.import_statement {
                let result = filter_import_matchers(node, code, matchers);
                (module_name, object_name, alias_name) = (
                    result.0.unwrap_or(module_name),
                    result.1.unwrap_or(object_name),
                    result.2.unwrap_or(alias_name),
                );

                let mut cursor = node.walk();
                for child in node.named_children(&mut cursor) {
                    let result = filter_import_matchers(child, code, matchers);
                    (module_name, object_name, alias_name) = (
                        result.0.unwrap_or(module_name),
                        result.1.unwrap_or(object_name),
                        result.2.unwrap_or(alias_name),
                    );

                    let mut cursor2 = child.walk();
                    for child2 in child.named_children(&mut cursor2) {
                        let result = filter_import_matchers(child2, code, matchers);
                        (module_name, object_name, alias_name) = (
                            result.0.unwrap_or(module_name),
                            result.1.unwrap_or(object_name),
                            result.2.unwrap_or(alias_name),
                        );
                    }
                }

                println!(
                    "Module: {}, Object: {}, Alias: {}",
                    module_name, object_name, alias_name
                );
                return Some((module_name, object_name));
            }
            None
        }
        lang if lang == unsafe { tree_sitter_rust() } => {
            let matchers = &config
                .languages
                .get("rust")
                .expect("Failed to get Python matchers from config")
                .matchers;

            if node.kind() == matchers.import_statement {
                let result = filter_import_matchers(node, code, matchers);
                (module_name, object_name, alias_name) = (
                    result.0.unwrap_or(module_name),
                    result.1.unwrap_or(object_name),
                    result.2.unwrap_or(alias_name),
                );

                let mut cursor = node.walk();
                for child in node.named_children(&mut cursor) {
                    let result = filter_import_matchers(child, code, matchers);
                    (module_name, object_name, alias_name) = (
                        result.0.unwrap_or(module_name),
                        result.1.unwrap_or(object_name),
                        result.2.unwrap_or(alias_name),
                    );

                    let mut cursor2 = child.walk();
                    for child2 in child.named_children(&mut cursor2) {
                        let result = filter_import_matchers(child2, code, matchers);
                        (module_name, object_name, alias_name) = (
                            result.0.unwrap_or(module_name),
                            result.1.unwrap_or(object_name),
                            result.2.unwrap_or(alias_name),
                        );
                    }
                }

                println!(
                    "Module: {}, Object: {}, Alias: {}",
                    module_name, object_name, alias_name
                );
                return Some((module_name, object_name));
            }
            None
        }
        _ => None,
    }
}

/// Checks if an AST node represents a class definition in the given language.
///
/// # Arguments
///
/// * `kind` - The kind (type) of the AST node.
/// * language - The tree-sitter Language of the file being parsed.
///
/// # Returns
///
/// true if the node represents a class definition, false otherwise.
fn is_class_definition(kind: &str, language: Language) -> bool {
    match language {
        lang if lang == unsafe { tree_sitter_python() } => kind == "class_definition",
        // Add more language-specific checks here
        _ => false,
    }
}

/// Checks if an AST node represents a function definition in the given language.
///
/// # Arguments
///
/// * kind - The kind (type) of the AST node.
/// * language - The tree-sitter Language of the file being parsed.
///
/// # Returns
///
/// true if the node represents a function definition, false otherwise.
fn is_function_node(kind: &str, language: Language) -> bool {
    match language {
        lang if lang == unsafe { tree_sitter_rust() } => kind == "function_item",
        lang if lang == unsafe { tree_sitter_python() } => kind == "function_definition",
        // Add more language-specific checks here
        _ => false,
    }
}

/// Extracts the function name from a function definition AST node.
///
/// # Arguments
///
/// * code - The code string of the file being parsed.
/// * node - The function definition AST node to extract the name from.
/// * language - The tree-sitter Language of the file being parsed.
///
/// # Returns
///
/// An Option containing the function name, if successfully extracted.
fn get_function_name(code: &str, node: Node, language: Language) -> Option<String> {
    match language {
        lang if lang == unsafe { tree_sitter_rust() } => node
            .child_by_field_name("name")
            .and_then(|child| Some(child.utf8_text(code.as_bytes()).unwrap()))
            .map(|s| s.to_string()),
        lang if lang == unsafe { tree_sitter_python() } => node
            .child_by_field_name("name")
            .and_then(|child| Some(child.utf8_text(code.as_bytes()).unwrap()))
            .map(|s| s.to_string()),
        // Add more language-specific checks here
        _ => None,
    }
}

/// Checks if an AST node represents a function call expression in the given language.
///
/// # Arguments
///
/// * kind - The kind (type) of the AST node.
/// * language - The tree-sitter Language of the file being parsed.
///
/// # Returns
///
/// true if the node represents a function call expression, false otherwise.
fn is_call_expression(kind: &str, language: Language) -> bool {
    match language {
        lang if lang == unsafe { tree_sitter_rust() } => kind == "call_expression",
        lang if lang == unsafe { tree_sitter_python() } => kind == "call",
        // Add more language-specific checks here
        _ => false,
    }
}

/// Extracts the called function name from a function call expression AST node.
///
/// # Arguments
///
/// * code - The code string of the file being parsed.
/// * node - The function call expression AST node to extract the name from.
/// * language - The tree-sitter Language of the file being parsed.
///
/// # Returns
///
/// An Option containing the called function name, if successfully extracted.
fn get_call_expression_name(code: &str, node: Node, language: Language) -> Option<String> {
    match language {
        lang if lang == unsafe { tree_sitter_rust() } => node
            .child_by_field_name("function")
            .and_then(|child| Some(child.utf8_text(code.as_bytes()).unwrap()))
            .map(|s| s.to_string()),
        lang if lang == unsafe { tree_sitter_python() } => node
            .child_by_field_name("function")
            .and_then(|child| Some(child.utf8_text(code.as_bytes()).unwrap()))
            .map(|s| s.to_string()),
        // Add more language-specific checks here
        _ => None,
    }
}