-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcst_navigation.rs
More file actions
35 lines (28 loc) · 1.06 KB
/
cst_navigation.rs
File metadata and controls
35 lines (28 loc) · 1.06 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
use vb6parse::parsers::SyntaxKind;
use vb6parse::*;
fn main() {
let code = r#"Sub Calculate()
Dim result As Double
result = 10 * 5 + 3
MsgBox result
End Sub"#;
let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", code).unpack();
let cst = cst_opt.expect("Failed to parse the input code.");
let root = cst.to_serializable().root;
println!("Total nodes in tree: {}", root.descendants().count());
// Find all Dim statements
let dims = root.find_all(SyntaxKind::DimStatement);
println!("Found {} Dim statements", dims.len());
// Find all identifiers
let identifiers = root.find_all_if(|n| n.kind() == SyntaxKind::Identifier);
println!("Found {} identifiers", identifiers.len());
for id in identifiers {
println!(" - {}", id.text());
}
// Navigate to specific nodes
if let Some(sub_stmt) = root.find(SyntaxKind::SubStatement) {
println!("\nSubroutine found:");
println!(" Text:\n'{}'", sub_stmt.text());
println!(" Children: {}", sub_stmt.child_count());
}
}