Skip to main content

python_tools/
lib.rs

1//! Python Tools 库
2//! 提供 Python 语言相关的工具链
3
4#![warn(missing_docs)]
5
6use oak_core::{Builder, TextEdit, builder::BuildOutput};
7use oak_python::{PythonBuilder, PythonLanguage, ast::PythonRoot};
8
9/// Rusty Python 前端
10pub struct RustyPythonFrontend {
11    language: PythonLanguage,
12}
13
14impl Default for RustyPythonFrontend {
15    fn default() -> Self {
16        Self::new()
17    }
18}
19
20impl RustyPythonFrontend {
21    /// 创建新的前端实例
22    pub fn new() -> Self {
23        Self { language: PythonLanguage::new() }
24    }
25
26    /// 解析 Python 代码
27    pub fn parse(&self, source: &str) -> Result<PythonRoot, String> {
28        let builder = PythonBuilder::new(&self.language);
29        let edits: &[TextEdit] = &[];
30        let mut cache = oak_core::parser::ParseSession::default();
31        let output: BuildOutput<PythonLanguage> = builder.build(source, edits, &mut cache);
32        output.result.map_err(|e| e.to_string())
33    }
34}