Skip to main content

oak_python/
frontend.rs

1use crate::{ast::Program, language::PythonLanguage, parser::PythonParser};
2use oak_core::{OakError, Parser, parser::session::ParseSession, source::Source};
3
4/// Python 语言前端
5pub struct PythonFrontend<'a, S: Source + ?Sized> {
6    source: &'a S,
7}
8
9impl<'a, S: Source + ?Sized> PythonFrontend<'a, S> {
10    /// 创建新的前端实例
11    pub fn new(source: &'a S) -> Self {
12        Self { source }
13    }
14
15    /// 解析 Python 源代码为 AST
16    pub fn parse_to_ast(&self) -> Result<Program, OakError> {
17        let config = PythonLanguage {};
18        let parser = PythonParser::new(&config);
19        let mut cache = ParseSession::<PythonLanguage>::default();
20
21        let output = parser.parse(self.source, &[], &mut cache);
22
23        match output.result {
24            Ok(_green_node) => {
25                // TODO: 实现从 GreenNode 到 AST 的转换
26                // 目前由于 PythonBuilder 尚未完成,我们暂时返回一个空的 Program
27                // 或者我们可以尝试手动构建一些基础的 AST 节点
28                Ok(Program { statements: vec![] })
29            }
30            Err(e) => Err(e),
31        }
32    }
33}