Skip to main content

oak_apl/builder/
mod.rs

1#![doc = include_str!("readme.md")]
2
3use crate::{ast::*, language::AplLanguage, parser::AplParser};
4use oak_core::{Builder, BuilderCache, GreenNode, OakDiagnostics, Parser, SourceText, TextEdit, source::Source};
5
6/// AST builder for the APL language.
7#[derive(Clone)]
8pub struct AplBuilder<'config> {
9    /// The language configuration.
10    pub config: &'config AplLanguage,
11}
12
13impl<'config> AplBuilder<'config> {
14    /// Creates a new `AplBuilder`.
15    pub fn new(config: &'config AplLanguage) -> Self {
16        Self { config }
17    }
18
19    /// Builds the AST root node from a green tree.
20    pub fn build_root(&self, _green: &GreenNode<AplLanguage>, _source: &SourceText) -> Result<AplRoot, oak_core::OakError> {
21        // Simplified AST building logic
22        Ok(AplRoot::new(vec![]))
23    }
24}
25
26impl<'config> Builder<AplLanguage> for AplBuilder<'config> {
27    fn build<'a, S: Source + ?Sized>(&self, source: &'a S, edits: &[TextEdit], _cache: &'a mut impl BuilderCache<AplLanguage>) -> oak_core::builder::BuildOutput<AplLanguage> {
28        let parser = AplParser::new(self.config);
29        let mut cache = oak_core::parser::ParseSession::<AplLanguage>::default();
30        let parse_result = parser.parse(source, edits, &mut cache);
31
32        match parse_result.result {
33            Ok(green_tree) => {
34                let source_text = SourceText::new(source.get_text_in((0..source.length()).into()).into_owned());
35                match self.build_root(green_tree, &source_text) {
36                    Ok(ast_root) => OakDiagnostics { result: Ok(ast_root), diagnostics: parse_result.diagnostics },
37                    Err(build_error) => {
38                        let mut diagnostics = parse_result.diagnostics;
39                        diagnostics.push(build_error.clone());
40                        OakDiagnostics { result: Err(build_error), diagnostics }
41                    }
42                }
43            }
44            Err(parse_error) => OakDiagnostics { result: Err(parse_error), diagnostics: parse_result.diagnostics },
45        }
46    }
47}