Skip to main content

code_analyze_core/languages/
fortran.rs

1// SPDX-FileCopyrightText: 2026 code-analyze-mcp contributors
2// SPDX-License-Identifier: Apache-2.0
3/// Tree-sitter query for extracting Fortran elements (functions and subroutines).
4///
5/// Module constructs are omitted: `module_statement` has no `name` field in
6/// tree-sitter-fortran 0.5.1, so `@class` captures would be counted in
7/// `analyze_directory` but produce no names in `analyze_file`. Modules will
8/// be added here once the grammar exposes a `name` field.
9pub const ELEMENT_QUERY: &str = r"
10(subroutine
11  (subroutine_statement) @function)
12
13(function
14  (function_statement) @function)
15";
16
17/// Tree-sitter query for extracting Fortran function calls.
18pub const CALL_QUERY: &str = r"
19(subroutine_call
20  (identifier) @call)
21
22(call_expression
23  (identifier) @call)
24";
25
26/// Tree-sitter query for extracting Fortran type references.
27pub const REFERENCE_QUERY: &str = r"
28(name) @type_ref
29";
30
31/// Tree-sitter query for extracting Fortran imports (USE statements).
32pub const IMPORT_QUERY: &str = r"
33(use_statement
34  (module_name) @import_path)
35";
36
37use tree_sitter::Node;
38
39/// Extract inheritance information from a Fortran node.
40/// Fortran does not have classical inheritance; return empty.
41#[must_use]
42pub fn extract_inheritance(_node: &Node, _source: &str) -> Vec<String> {
43    Vec::new()
44}
45
46#[cfg(all(test, feature = "lang-fortran"))]
47mod tests {
48    use super::*;
49    use tree_sitter::Parser;
50
51    fn parse_fortran(source: &str) -> (tree_sitter::Tree, Vec<u8>) {
52        let mut parser = Parser::new();
53        parser
54            .set_language(&tree_sitter_fortran::LANGUAGE.into())
55            .expect("failed to set Fortran language");
56        let source_bytes = source.as_bytes().to_vec();
57        let tree = parser.parse(&source_bytes, None).expect("failed to parse");
58        (tree, source_bytes)
59    }
60
61    #[test]
62    fn test_extract_inheritance_returns_empty() {
63        // Arrange
64        let source = "PROGRAM test\nEND PROGRAM test\n";
65        let (tree, _source_bytes) = parse_fortran(source);
66        let root = tree.root_node();
67
68        // Act
69        let result = extract_inheritance(&root, source);
70
71        // Assert
72        assert!(result.is_empty());
73    }
74}