-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_parser.py
More file actions
292 lines (257 loc) · 11.1 KB
/
code_parser.py
File metadata and controls
292 lines (257 loc) · 11.1 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
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
"""代码解析模块 - AST解析和代码结构提取"""
import ast
import json
from typing import Dict, List, Optional, Any
import re
class CodeParser:
"""代码解析器"""
def __init__(self):
self.supported_languages = {
'python': self._parse_python,
'javascript': self._parse_javascript,
'java': self._parse_java,
'go': self._parse_go,
'cpp': self._parse_cpp
}
def parse_code(self, code: str, language: str = "python") -> Dict[str, Any]:
"""解析代码,提取结构信息"""
language = language.lower()
if language in self.supported_languages:
parser_func = self.supported_languages[language]
try:
return parser_func(code)
except Exception as e:
print(f"解析{language}代码失败: {e}")
return self._parse_generic(code, language)
else:
return self._parse_generic(code, language)
def _parse_python(self, code: str) -> Dict[str, Any]:
"""解析Python代码"""
try:
tree = ast.parse(code)
functions = []
classes = []
imports = []
variables = []
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
# 处理装饰器
decorators = []
for d in node.decorator_list:
if isinstance(d, ast.Name):
decorators.append(d.id)
elif isinstance(d, ast.Attribute):
decorators.append(f"{d.attr}")
else:
decorators.append(str(type(d).__name__))
functions.append({
"name": node.name,
"line": node.lineno,
"args": [arg.arg for arg in node.args.args],
"decorators": decorators
})
elif isinstance(node, ast.ClassDef):
methods = [n.name for n in node.body if isinstance(n, ast.FunctionDef)]
# 处理基类
bases = []
for b in node.bases:
if isinstance(b, ast.Name):
bases.append(b.id)
elif isinstance(b, ast.Attribute):
bases.append(b.attr)
else:
bases.append(str(type(b).__name__))
classes.append({
"name": node.name,
"line": node.lineno,
"methods": methods,
"bases": bases
})
elif isinstance(node, ast.Import):
for alias in node.names:
imports.append({
"module": alias.name,
"alias": alias.asname
})
elif isinstance(node, ast.ImportFrom):
imports.append({
"module": node.module or "",
"names": [alias.name for alias in node.names]
})
elif isinstance(node, ast.Assign):
for target in node.targets:
if isinstance(target, ast.Name):
variables.append({
"name": target.id,
"line": node.lineno
})
return {
"language": "python",
"functions": functions,
"classes": classes,
"imports": imports,
"variables": variables,
"function_count": len(functions),
"class_count": len(classes),
"import_count": len(imports),
"structure": {
"has_main": any(f["name"] == "__main__" or f["name"] == "main" for f in functions),
"has_classes": len(classes) > 0,
"has_functions": len(functions) > 0
}
}
except SyntaxError as e:
return {
"language": "python",
"error": f"语法错误: {str(e)}",
"functions": [],
"classes": [],
"imports": [],
"variables": []
}
def _parse_javascript(self, code: str) -> Dict[str, Any]:
"""解析JavaScript代码(简单正则匹配)"""
functions = []
classes = []
imports = []
# 匹配函数定义
function_pattern = r'(?:function\s+(\w+)|const\s+(\w+)\s*=\s*(?:\([^)]*\)\s*=>|function)|(\w+)\s*:\s*function)'
for match in re.finditer(function_pattern, code):
func_name = match.group(1) or match.group(2) or match.group(3)
if func_name:
functions.append({"name": func_name, "line": code[:match.start()].count('\n') + 1})
# 匹配类定义
class_pattern = r'class\s+(\w+)'
for match in re.finditer(class_pattern, code):
classes.append({"name": match.group(1), "line": code[:match.start()].count('\n') + 1})
# 匹配import
import_pattern = r'(?:import\s+(?:\{[^}]*\}|\w+|\*\s+as\s+\w+)\s+from\s+[\'"]([^\'"]+)[\'"]|require\s*\([\'"]([^\'"]+)[\'"]\))'
for match in re.finditer(import_pattern, code):
module = match.group(1) or match.group(2)
if module:
imports.append({"module": module})
return {
"language": "javascript",
"functions": functions,
"classes": classes,
"imports": imports,
"function_count": len(functions),
"class_count": len(classes),
"import_count": len(imports),
"structure": {
"has_classes": len(classes) > 0,
"has_functions": len(functions) > 0
}
}
def _parse_java(self, code: str) -> Dict[str, Any]:
"""解析Java代码(简单正则匹配)"""
classes = []
methods = []
imports = []
# 匹配类定义
class_pattern = r'(?:public\s+)?(?:abstract\s+)?(?:final\s+)?class\s+(\w+)'
for match in re.finditer(class_pattern, code):
classes.append({"name": match.group(1), "line": code[:match.start()].count('\n') + 1})
# 匹配方法定义
method_pattern = r'(?:public|private|protected)?\s*(?:static)?\s*\w+\s+(\w+)\s*\([^)]*\)'
for match in re.finditer(method_pattern, code):
methods.append({"name": match.group(1), "line": code[:match.start()].count('\n') + 1})
# 匹配import
import_pattern = r'import\s+(?:static\s+)?([\w.]+)'
for match in re.finditer(import_pattern, code):
imports.append({"module": match.group(1)})
return {
"language": "java",
"functions": methods,
"classes": classes,
"imports": imports,
"function_count": len(methods),
"class_count": len(classes),
"import_count": len(imports),
"structure": {
"has_classes": len(classes) > 0,
"has_functions": len(methods) > 0
}
}
def _parse_go(self, code: str) -> Dict[str, Any]:
"""解析Go代码(简单正则匹配)"""
functions = []
imports = []
# 匹配函数定义
func_pattern = r'func\s+(?:\([^)]+\)\s+)?(\w+)\s*\([^)]*\)'
for match in re.finditer(func_pattern, code):
functions.append({"name": match.group(1), "line": code[:match.start()].count('\n') + 1})
# 匹配import
import_pattern = r'import\s+(?:\(([^)]+)\)|"([^"]+)")'
for match in re.finditer(import_pattern, code):
if match.group(1):
# 多行import
for imp in match.group(1).split('\n'):
imp = imp.strip().strip('"')
if imp:
imports.append({"module": imp})
elif match.group(2):
imports.append({"module": match.group(2)})
return {
"language": "go",
"functions": functions,
"classes": [],
"imports": imports,
"function_count": len(functions),
"class_count": 0,
"import_count": len(imports),
"structure": {
"has_functions": len(functions) > 0
}
}
def _parse_cpp(self, code: str) -> Dict[str, Any]:
"""解析C++代码(简单正则匹配)"""
classes = []
functions = []
includes = []
# 匹配类定义
class_pattern = r'class\s+(\w+)'
for match in re.finditer(class_pattern, code):
classes.append({"name": match.group(1), "line": code[:match.start()].count('\n') + 1})
# 匹配函数定义
func_pattern = r'(?:\w+\s+)*(\w+)\s*\([^)]*\)\s*\{'
for match in re.finditer(func_pattern, code):
func_name = match.group(1)
if func_name not in ['if', 'for', 'while', 'switch']:
functions.append({"name": func_name, "line": code[:match.start()].count('\n') + 1})
# 匹配include
include_pattern = r'#include\s+[<"]([^>"]+)[>"]'
for match in re.finditer(include_pattern, code):
includes.append({"module": match.group(1)})
return {
"language": "cpp",
"functions": functions[:20], # 限制数量
"classes": classes,
"imports": includes,
"function_count": len(functions),
"class_count": len(classes),
"import_count": len(includes),
"structure": {
"has_classes": len(classes) > 0,
"has_functions": len(functions) > 0
}
}
def _parse_generic(self, code: str, language: str) -> Dict[str, Any]:
"""通用解析(基于代码行数和基本统计)"""
lines = code.split('\n')
non_empty_lines = [line for line in lines if line.strip()]
return {
"language": language,
"functions": [],
"classes": [],
"imports": [],
"variables": [],
"total_lines": len(lines),
"non_empty_lines": len(non_empty_lines),
"structure": {
"code_length": len(code),
"line_count": len(lines)
}
}
# 全局实例
code_parser = CodeParser()