deagle-parse 0.2.1

Tree-sitter based multi-language code parser for deagle
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
//! Python language parser using tree-sitter-python.

use deagle_core::{DeagleError, EdgeKind, Language, Node, NodeKind, Result};
use std::path::Path;

use crate::ParseResult;

/// Parse a Python source file and extract definitions.
pub fn parse(path: &Path, content: &str) -> Result<Vec<Node>> {
    parse_with_edges(path, content).map(|r| r.nodes)
}

/// Parse with edge extraction — returns nodes and relationship tuples.
pub fn parse_with_edges(path: &Path, content: &str) -> Result<ParseResult> {
    let mut parser = tree_sitter::Parser::new();
    let language = tree_sitter_python::LANGUAGE;
    parser.set_language(&language.into()).map_err(|e| DeagleError::Parse {
        file: path.display().to_string(),
        message: format!("Failed to set language: {}", e),
    })?;

    let tree = parser.parse(content, None).ok_or_else(|| DeagleError::Parse {
        file: path.display().to_string(),
        message: "Failed to parse file".into(),
    })?;

    let mut nodes = Vec::new();
    let file_path = path.to_string_lossy().to_string();

    // Insert file node as index 0
    nodes.push(Node {
        id: 0,
        name: path.file_name().and_then(|n| n.to_str()).unwrap_or("unknown").to_string(),
        kind: NodeKind::File,
        language: Language::Python,
        file_path: file_path.clone(),
        line_start: 1,
        line_end: content.lines().count() as u32,
        content: None,
    });

    extract_definitions(tree.root_node(), content, &file_path, &mut nodes, false);

    // Build CONTAINS edges: file (idx 0) → each top-level entity
    let mut edges = Vec::new();
    for i in 1..nodes.len() {
        edges.push((0, i, EdgeKind::Contains));
    }

    Ok(ParseResult { nodes, edges })
}

fn extract_definitions(
    node: tree_sitter::Node,
    source: &str,
    file_path: &str,
    results: &mut Vec<Node>,
    inside_class: bool,
) {
    let kind = match node.kind() {
        "function_definition" => {
            if inside_class {
                Some(NodeKind::Method)
            } else {
                Some(NodeKind::Function)
            }
        }
        "class_definition" => Some(NodeKind::Class),
        "import_statement" | "import_from_statement" => Some(NodeKind::Import),
        "global_statement" => None, // skip
        "expression_statement" => {
            // Check for top-level assignments (module-level constants)
            if !inside_class {
                if let Some(child) = node.child(0) {
                    if child.kind() == "assignment" {
                        // Only capture UPPER_CASE assignments as constants
                        if let Some(name) = extract_assignment_name(child, source) {
                            if name.chars().all(|c| c.is_uppercase() || c == '_' || c.is_ascii_digit()) && !name.is_empty() {
                                let start = node.start_position();
                                let end = node.end_position();
                                let content = node.utf8_text(source.as_bytes()).ok().map(|s| {
                                    crate::truncate_content(s, 500)
                                });
                                results.push(Node {
                                    id: 0,
                                    name,
                                    kind: NodeKind::Constant,
                                    language: Language::Python,
                                    file_path: file_path.to_string(),
                                    line_start: (start.row + 1) as u32,
                                    line_end: (end.row + 1) as u32,
                                    content,
                                });
                            }
                        }
                    }
                }
            }
            None
        }
        _ => None,
    };

    if let Some(kind) = kind {
        if let Some(name) = extract_name(node, source, kind) {
            let start = node.start_position();
            let end = node.end_position();
            let content = node.utf8_text(source.as_bytes()).ok().map(|s| {
                crate::truncate_content(s, 500)
            });

            results.push(Node {
                id: 0,
                name,
                kind,
                language: Language::Python,
                file_path: file_path.to_string(),
                line_start: (start.row + 1) as u32,
                line_end: (end.row + 1) as u32,
                content,
            });
        }

        // If this is a class, recurse into its body to find methods
        if kind == NodeKind::Class {
            if let Some(body) = node.child_by_field_name("body") {
                let mut cursor = body.walk();
                for child in body.children(&mut cursor) {
                    extract_definitions(child, source, file_path, results, true);
                }
            }
            return; // Don't double-recurse into class children
        }
    }

    // Recurse into children (but not into class bodies — handled above)
    if node.kind() != "class_definition" {
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            extract_definitions(child, source, file_path, results, inside_class);
        }
    }
}

fn extract_name(node: tree_sitter::Node, source: &str, kind: NodeKind) -> Option<String> {
    match kind {
        NodeKind::Import => {
            // Full import text
            node.utf8_text(source.as_bytes())
                .ok()
                .map(|s| s.trim().to_string())
        }
        _ => {
            // Find the 'name' field (tree-sitter-python uses field names)
            node.child_by_field_name("name")
                .and_then(|n| n.utf8_text(source.as_bytes()).ok())
                .map(|s| s.to_string())
        }
    }
}

fn extract_assignment_name(node: tree_sitter::Node, source: &str) -> Option<String> {
    // Assignment left side — could be identifier or pattern
    node.child_by_field_name("left")
        .and_then(|n| {
            if n.kind() == "identifier" {
                n.utf8_text(source.as_bytes()).ok().map(|s| s.to_string())
            } else {
                None
            }
        })
}

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

    const SAMPLE_PYTHON: &str = r#"
import os
from pathlib import Path

MAX_SIZE = 1024
DEBUG = True

class Config:
    """Configuration holder."""

    def __init__(self, name: str):
        self.name = name
        self.values = {}

    def get(self, key: str) -> str:
        return self.values.get(key, "")

    @staticmethod
    def default() -> "Config":
        return Config("default")

class Status:
    ACTIVE = "active"
    INACTIVE = "inactive"

def process(data: list) -> dict:
    result = {}
    for item in data:
        result[item] = True
    return result

def main():
    config = Config("test")
    print(config.get("key"))
"#;

    #[test]
    fn test_parse_python_finds_all_definitions() {
        let path = PathBuf::from("test.py");
        let nodes = parse(&path, SAMPLE_PYTHON).unwrap();

        let kinds: Vec<_> = nodes.iter().map(|n| n.kind).collect();
        assert!(kinds.contains(&NodeKind::Import), "should find import");
        assert!(kinds.contains(&NodeKind::Constant), "should find constant");
        assert!(kinds.contains(&NodeKind::Class), "should find class");
        assert!(kinds.contains(&NodeKind::Function), "should find function");
    }

    #[test]
    fn test_parse_python_finds_methods() {
        let path = PathBuf::from("test.py");
        let nodes = parse(&path, SAMPLE_PYTHON).unwrap();

        let methods: Vec<_> = nodes.iter().filter(|n| n.kind == NodeKind::Method).collect();
        assert!(methods.len() >= 3, "should find methods (__init__, get, default), got {}", methods.len());
        assert!(methods.iter().any(|m| m.name == "__init__"));
        assert!(methods.iter().any(|m| m.name == "get"));
        assert!(methods.iter().any(|m| m.name == "default"));
    }

    #[test]
    fn test_parse_python_class_name() {
        let path = PathBuf::from("test.py");
        let nodes = parse(&path, SAMPLE_PYTHON).unwrap();

        let classes: Vec<_> = nodes.iter().filter(|n| n.kind == NodeKind::Class).collect();
        assert_eq!(classes.len(), 2);
        assert!(classes.iter().any(|c| c.name == "Config"));
        assert!(classes.iter().any(|c| c.name == "Status"));
        assert_eq!(classes[0].language, Language::Python);
    }

    #[test]
    fn test_parse_python_constants() {
        let path = PathBuf::from("test.py");
        let nodes = parse(&path, SAMPLE_PYTHON).unwrap();

        let constants: Vec<_> = nodes.iter().filter(|n| n.kind == NodeKind::Constant).collect();
        assert!(constants.iter().any(|c| c.name == "MAX_SIZE"), "should find MAX_SIZE");
        assert!(constants.iter().any(|c| c.name == "DEBUG"), "should find DEBUG");
    }

    #[test]
    fn test_parse_python_line_numbers() {
        let path = PathBuf::from("test.py");
        let nodes = parse(&path, SAMPLE_PYTHON).unwrap();

        let main_fn = nodes.iter().find(|n| n.name == "main" && n.kind == NodeKind::Function);
        assert!(main_fn.is_some(), "should find main function");
        assert!(main_fn.unwrap().line_start > 0, "line numbers should be 1-indexed");
    }

    #[test]
    fn test_parse_python_imports() {
        let path = PathBuf::from("test.py");
        let nodes = parse(&path, SAMPLE_PYTHON).unwrap();

        let imports: Vec<_> = nodes.iter().filter(|n| n.kind == NodeKind::Import).collect();
        assert_eq!(imports.len(), 2, "should find 2 import statements");
        assert!(imports.iter().any(|i| i.name.contains("os")));
        assert!(imports.iter().any(|i| i.name.contains("pathlib")));
    }

    #[test]
    fn test_parse_python_edges() {
        let path = PathBuf::from("test.py");
        let result = parse_with_edges(&path, SAMPLE_PYTHON).unwrap();

        assert!(!result.edges.is_empty(), "should have CONTAINS edges");
        // All edges should be from file node (idx 0)
        for &(from_idx, _, ref kind) in &result.edges {
            assert_eq!(from_idx, 0);
            assert_eq!(*kind, EdgeKind::Contains);
        }
    }

    #[test]
    fn test_parse_empty_python_file() {
        let path = PathBuf::from("empty.py");
        let nodes = parse(&path, "").unwrap();
        assert!(nodes.len() <= 1);
    }

    #[test]
    fn test_parse_python_decorated_function() {
        let source = r#"
import functools

def decorator(f):
    return f

@decorator
def decorated():
    pass

class MyClass:
    @staticmethod
    def static_method():
        pass

    @classmethod
    def class_method(cls):
        pass
"#;
        let path = PathBuf::from("deco.py");
        let nodes = parse(&path, source).unwrap();

        let fns: Vec<_> = nodes.iter().filter(|n| n.kind == NodeKind::Function).collect();
        assert!(fns.iter().any(|f| f.name == "decorator"));
        assert!(fns.iter().any(|f| f.name == "decorated"));

        let methods: Vec<_> = nodes.iter().filter(|n| n.kind == NodeKind::Method).collect();
        assert!(methods.iter().any(|m| m.name == "static_method"));
        assert!(methods.iter().any(|m| m.name == "class_method"));
    }

    #[test]
    fn test_parse_python_nested_class() {
        let source = r#"
class Outer:
    class Inner:
        def inner_method(self):
            pass

    def outer_method(self):
        pass
"#;
        let path = PathBuf::from("nested.py");
        let nodes = parse(&path, source).unwrap();

        let classes: Vec<_> = nodes.iter().filter(|n| n.kind == NodeKind::Class).collect();
        assert!(classes.iter().any(|c| c.name == "Outer"));
    }

    #[test]
    fn test_parse_python_async_function() {
        let source = r#"
import asyncio

async def fetch_data(url: str) -> dict:
    return {}

class Client:
    async def connect(self):
        pass
"#;
        let path = PathBuf::from("async.py");
        let nodes = parse(&path, source).unwrap();

        let fns: Vec<_> = nodes.iter().filter(|n| n.kind == NodeKind::Function).collect();
        assert!(fns.iter().any(|f| f.name == "fetch_data"), "should find async function");

        let methods: Vec<_> = nodes.iter().filter(|n| n.kind == NodeKind::Method).collect();
        assert!(methods.iter().any(|m| m.name == "connect"), "should find async method");
    }

    #[test]
    fn test_parse_python_lowercase_not_constant() {
        let source = r#"
MAX_SIZE = 100
lowercase_var = "not a constant"
_private = True
"#;
        let path = PathBuf::from("vars.py");
        let nodes = parse(&path, source).unwrap();

        let constants: Vec<_> = nodes.iter().filter(|n| n.kind == NodeKind::Constant).collect();
        assert!(constants.iter().any(|c| c.name == "MAX_SIZE"));
        // lowercase should NOT be captured as constant
        assert!(!constants.iter().any(|c| c.name == "lowercase_var"));
        assert!(!constants.iter().any(|c| c.name == "_private"));
    }

    #[test]
    fn test_parse_python_multiple_imports() {
        let source = r#"
import os
import sys
from typing import Dict, List, Optional
from pathlib import Path
from collections import defaultdict
"#;
        let path = PathBuf::from("imports.py");
        let nodes = parse(&path, source).unwrap();

        let imports: Vec<_> = nodes.iter().filter(|n| n.kind == NodeKind::Import).collect();
        assert_eq!(imports.len(), 5, "should find all 5 import statements");
    }
}