oak_python/builder/
mod.rs1mod build_expressions;
2mod build_root;
3mod build_statements;
4mod build_utils;
5
6use crate::{language::PythonLanguage, parser::PythonParser};
7use oak_core::{Builder, BuilderCache, OakDiagnostics, Parser, SourceText, TextEdit, builder::BuildOutput, source::Source};
8
9pub struct PythonBuilder<'config> {
11 config: &'config PythonLanguage,
13}
14
15impl<'config> PythonBuilder<'config> {
16 pub fn new(config: &'config PythonLanguage) -> Self {
18 Self { config }
19 }
20}
21
22impl<'config> Builder<PythonLanguage> for PythonBuilder<'config> {
23 fn build<'a, S: Source + ?Sized>(&self, source: &S, edits: &[TextEdit], _cache: &'a mut impl BuilderCache<PythonLanguage>) -> BuildOutput<PythonLanguage> {
24 let parser = PythonParser::new(self.config);
25
26 let mut parse_cache = oak_core::parser::session::ParseSession::<PythonLanguage>::default();
27 let parse_result = parser.parse(source, edits, &mut parse_cache);
28
29 match parse_result.result {
30 Ok(green_tree) => {
31 let source_text = SourceText::new(source.get_text_in((0..source.length()).into()).into_owned());
32 match self.build_root(green_tree, &source_text) {
33 Ok(ast_root) => OakDiagnostics { result: Ok(ast_root), diagnostics: parse_result.diagnostics },
34 Err(build_error) => {
35 let mut diagnostics = parse_result.diagnostics;
36 diagnostics.push(build_error.clone());
37 OakDiagnostics { result: Err(build_error), diagnostics }
38 }
39 }
40 }
41 Err(e) => OakDiagnostics { result: Err(e), diagnostics: parse_result.diagnostics },
42 }
43 }
44}