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 language frontend.
5pub struct PythonFrontend<'a, S: Source + ?Sized> {
6    source: &'a S,
7}
8
9impl<'a, S: Source + ?Sized> PythonFrontend<'a, S> {
10    /// Creates a new frontend instance.
11    pub fn new(source: &'a S) -> Self {
12        Self { source }
13    }
14
15    /// Parses Python source code into an 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: Implement GreenNode to AST conversion.
26                // Currently returning an empty Program as PythonBuilder integration is pending.
27                Ok(Program { statements: vec![] })
28            }
29            Err(e) => Err(e),
30        }
31    }
32}