Skip to main content

oak_vala/builder/
mod.rs

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