-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse_demo.rs
More file actions
231 lines (175 loc) · 5.29 KB
/
parse_demo.rs
File metadata and controls
231 lines (175 loc) · 5.29 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
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
//! Demonstration of the tree-sitter parser integration
use smart_diff_parser::{
ast::NodeType, language::Language, parser::Parser, tree_sitter::TreeSitterParser,
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Smart Code Diff - Tree-sitter Parser Demo");
println!("==========================================");
let parser = TreeSitterParser::new()?;
// Demo Java parsing
demo_java_parsing(&parser)?;
// Demo Python parsing
demo_python_parsing(&parser)?;
// Demo JavaScript parsing
demo_javascript_parsing(&parser)?;
// Demo C parsing
demo_c_parsing(&parser)?;
// Demo C++ parsing
demo_cpp_parsing(&parser)?;
Ok(())
}
fn demo_java_parsing(parser: &TreeSitterParser) -> Result<(), Box<dyn std::error::Error>> {
println!("\n--- Java Parsing Demo ---");
let java_code = r#"
public class Calculator {
private int value;
public Calculator() {
this.value = 0;
}
public int add(int a, int b) {
return a + b;
}
public void setValue(int value) {
this.value = value;
}
}
"#;
let result = parser.parse(java_code, Language::Java)?;
print_parse_results("Java", &result);
Ok(())
}
fn demo_python_parsing(parser: &TreeSitterParser) -> Result<(), Box<dyn std::error::Error>> {
println!("\n--- Python Parsing Demo ---");
let python_code = r#"
class Calculator:
def __init__(self):
self.value = 0
def add(self, a, b):
return a + b
def set_value(self, value):
self.value = value
def main():
calc = Calculator()
result = calc.add(10, 20)
print(f"Result: {result}")
if __name__ == "__main__":
main()
"#;
let result = parser.parse(python_code, Language::Python)?;
print_parse_results("Python", &result);
Ok(())
}
fn demo_javascript_parsing(parser: &TreeSitterParser) -> Result<(), Box<dyn std::error::Error>> {
println!("\n--- JavaScript Parsing Demo ---");
let js_code = r#"
class Calculator {
constructor() {
this.value = 0;
}
add(a, b) {
return a + b;
}
setValue(value) {
this.value = value;
}
}
function main() {
const calc = new Calculator();
const result = calc.add(10, 20);
console.log(`Result: ${result}`);
}
main();
"#;
let result = parser.parse(js_code, Language::JavaScript)?;
print_parse_results("JavaScript", &result);
Ok(())
}
fn demo_c_parsing(parser: &TreeSitterParser) -> Result<(), Box<dyn std::error::Error>> {
println!("\n--- C Parsing Demo ---");
let c_code = r#"
#include <stdio.h>
struct Calculator {
int value;
};
int add(int a, int b) {
return a + b;
}
void set_value(struct Calculator* calc, int value) {
calc->value = value;
}
int main() {
struct Calculator calc = {0};
int result = add(10, 20);
printf("Result: %d\n", result);
return 0;
}
"#;
let result = parser.parse(c_code, Language::C)?;
print_parse_results("C", &result);
Ok(())
}
fn demo_cpp_parsing(parser: &TreeSitterParser) -> Result<(), Box<dyn std::error::Error>> {
println!("\n--- C++ Parsing Demo ---");
let cpp_code = r#"
#include <iostream>
class Calculator {
private:
int value;
public:
Calculator() : value(0) {}
int add(int a, int b) {
return a + b;
}
void setValue(int value) {
this->value = value;
}
};
int main() {
Calculator calc;
int result = calc.add(10, 20);
std::cout << "Result: " << result << std::endl;
return 0;
}
"#;
let result = parser.parse(cpp_code, Language::Cpp)?;
print_parse_results("C++", &result);
Ok(())
}
fn print_parse_results(language: &str, result: &smart_diff_parser::ParseResult) {
println!("Language: {}", language);
println!("AST Root Type: {:?}", result.ast.node_type);
// Count different node types
let function_count = result.ast.find_by_type(&NodeType::Function).len();
let method_count = result.ast.find_by_type(&NodeType::Method).len();
let class_count = result.ast.find_by_type(&NodeType::Class).len();
let constructor_count = result.ast.find_by_type(&NodeType::Constructor).len();
println!("Functions found: {}", function_count);
println!("Methods found: {}", method_count);
println!("Classes found: {}", class_count);
println!("Constructors found: {}", constructor_count);
if !result.errors.is_empty() {
println!("Parse errors: {}", result.errors.len());
for error in &result.errors {
println!(" - {}", error);
}
}
if !result.warnings.is_empty() {
println!("Parse warnings: {}", result.warnings.len());
for warning in &result.warnings {
println!(" - {}", warning);
}
}
// Print some function details
let functions = result.ast.find_by_type(&NodeType::Function);
let methods = result.ast.find_by_type(&NodeType::Method);
let all_functions: Vec<_> = functions.into_iter().chain(methods).collect();
if !all_functions.is_empty() {
println!("Function details:");
for func in all_functions.iter().take(3) {
// Show first 3 functions
if let Some(name) = func.metadata.attributes.get("name") {
println!(" - {} (line {})", name, func.metadata.line);
}
}
}
}