Skip to main content

oak_lean/builder/
mod.rs

1use crate::{ast::LeanRoot, language::LeanLanguage};
2use oak_core::{Builder, BuilderCache, GreenNode, OakDiagnostics, Parser, RedNode, TextEdit, source::Source};
3
4pub struct LeanBuilder<'config> {
5    config: &'config LeanLanguage,
6}
7
8impl<'config> LeanBuilder<'config> {
9    pub fn new(config: &'config LeanLanguage) -> Self {
10        Self { config }
11    }
12}
13
14impl<'config> Builder<LeanLanguage> for LeanBuilder<'config> {
15    fn build<'a, S: Source + ?Sized>(&self, source: &'a S, edits: &[TextEdit], cache: &'a mut impl BuilderCache<LeanLanguage>) -> OakDiagnostics<LeanRoot> {
16        let parser = crate::parser::LeanParser::new(self.config);
17        let parse_result = parser.parse(source, edits, cache);
18
19        match parse_result.result {
20            Ok(green_tree) => {
21                let ast_root = self.build_root(green_tree);
22                OakDiagnostics { result: Ok(ast_root), diagnostics: parse_result.diagnostics }
23            }
24            Err(parse_error) => OakDiagnostics { result: Err(parse_error), diagnostics: parse_result.diagnostics },
25        }
26    }
27}
28
29impl<'config> LeanBuilder<'config> {
30    pub(crate) fn build_root<'a>(&self, green_tree: &'a GreenNode<'a, LeanLanguage>) -> LeanRoot {
31        LeanRoot::new(RedNode::new(green_tree, 0).span())
32    }
33}