111/utils/code_parser.py

33 lines
1.0 KiB
Python
Raw Permalink Normal View History

2026-01-08 21:48:24 +08:00
from typing import List
def parse_code_structure(code: str) -> dict:
"""解析代码结构,返回基本信息"""
lines = code.split('\n')
return {
"total_lines": len(lines),
"non_empty_lines": len([l for l in lines if l.strip()]),
"code_lines": lines
}
def detect_language(code: str) -> str:
"""简单检测代码语言"""
code_stripped = code.strip()
if code_stripped.startswith(('def ', 'import ', 'from ', 'class ', 'if __name__')):
return 'python'
elif 'function' in code or 'const' in code or 'let' in code or '=>' in code:
return 'javascript'
elif 'public class' in code or 'private void' in code:
return 'java'
elif 'std::' in code or '#include <iostream>' in code:
return 'cpp'
return 'unknown'
def extract_code_blocks(text: str) -> List[str]:
"""从文本中提取代码块"""
import re
pattern = r'```[\w]*\n([\s\S]*?)```'
matches = re.findall(pattern, text)
return matches if matches else []