-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbasic_parse.rs
More file actions
99 lines (88 loc) · 3.18 KB
/
basic_parse.rs
File metadata and controls
99 lines (88 loc) · 3.18 KB
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
// Copyright 2024-2026 Andrey Vasilevsky <anvanster@gmail.com>
// SPDX-License-Identifier: Apache-2.0
/// Basic example of parsing a single Python file
///
/// This example demonstrates:
/// - Creating a code graph
/// - Parsing a Python file
/// - Extracting entities and relationships
/// - Reporting parse results
use codegraph::CodeGraph;
use codegraph_python::{ParseError, Parser};
use std::path::Path;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create an in-memory graph
let mut graph = CodeGraph::in_memory()?;
// Create a parser with default configuration
let parser = Parser::new();
// Parse a Python file
let file_path = Path::new("tests/fixtures/simple.py");
println!("Parsing {}...", file_path.display());
match parser.parse_file(file_path, &mut graph) {
Ok(file_info) => {
println!("✓ Successfully parsed!");
println!();
println!("Results:");
println!(" Functions: {}", file_info.functions.len());
println!(" Classes: {}", file_info.classes.len());
println!(" Modules: {}", file_info.modules.len());
println!(" Traits: {}", file_info.traits.len());
println!(" Lines: {}", file_info.lines);
println!(" Parse time: {:?}", file_info.parse_time);
println!();
println!("Entities found:");
if !file_info.functions.is_empty() {
println!(" Functions:");
for func in &file_info.functions {
println!(" - {func}");
}
}
if !file_info.classes.is_empty() {
println!(" Classes:");
for class in &file_info.classes {
println!(" - {class}");
}
}
if !file_info.modules.is_empty() {
println!(" Modules:");
for module in &file_info.modules {
println!(" - {module}");
}
}
}
Err(e) => {
println!("✗ Failed to parse!");
println!();
let error_msg = match &e {
ParseError::SyntaxError {
file,
line,
column,
message,
} => {
format!("Syntax error in {file} at line {line}, column {column}: {message}")
}
ParseError::IoError { path, source } => {
format!("I/O error reading {path:?}: {source}")
}
ParseError::FileTooLarge {
path,
max_size,
actual_size,
} => {
format!(
"File {path:?} is too large: {actual_size} bytes (limit: {max_size} bytes)"
)
}
other => {
format!("Error: {other}")
}
};
println!("{error_msg}");
return Err(Box::new(e));
}
}
println!();
println!("Graph now contains {} nodes", graph.node_count());
Ok(())
}