Skip to main content

oak_bat/builder/
mod.rs

1#![doc = include_str!("readme.md")]
2use crate::{ast::*, language::BatLanguage, parser::BatParser};
3use oak_core::{Builder, BuilderCache, GreenNode, OakDiagnostics, Parser, SourceText, TextEdit, source::Source};
4
5/// Windows Batch (BAT) AST builder.
6#[derive(Clone)]
7pub struct BatBuilder<'config> {
8    config: &'config BatLanguage,
9}
10
11impl<'config> BatBuilder<'config> {
12    /// Creates a new `BatBuilder` instance with the specified language configuration.
13    ///
14    /// # Arguments
15    ///
16    /// * `config` - A reference to the `BatLanguage` configuration.
17    pub fn new(config: &'config BatLanguage) -> Self {
18        Self { config }
19    }
20
21    /// Builds a `BatRoot` AST from the parsed green tree and source text.
22    ///
23    /// # Arguments
24    ///
25    /// * `_green` - The parsed green tree from the parser.
26    /// * `_source` - The source text for reference.
27    ///
28    /// # Returns
29    ///
30    /// A `Result` containing the `BatRoot` AST or an `OakError`.
31    pub fn build_root(&self, _green: &GreenNode<BatLanguage>, _source: &SourceText) -> Result<BatRoot, oak_core::OakError> {
32        // Simplified AST construction logic.
33        Ok(BatRoot { elements: vec![] })
34    }
35}
36
37impl<'config> Builder<BatLanguage> for BatBuilder<'config> {
38    fn build<'a, S: Source + ?Sized>(&self, source: &'a S, edits: &[TextEdit], _cache: &'a mut impl BuilderCache<BatLanguage>) -> oak_core::builder::BuildOutput<BatLanguage> {
39        let parser = BatParser::new(self.config);
40        let mut cache = oak_core::parser::ParseSession::<BatLanguage>::default();
41        let parse_result = parser.parse(source, edits, &mut cache);
42
43        match parse_result.result {
44            Ok(green_tree) => {
45                let source_text = SourceText::new(source.get_text_in((0..source.length()).into()).into_owned());
46                match self.build_root(green_tree, &source_text) {
47                    Ok(ast_root) => OakDiagnostics { result: Ok(ast_root), diagnostics: parse_result.diagnostics },
48                    Err(build_error) => {
49                        let mut diagnostics = parse_result.diagnostics;
50                        diagnostics.push(build_error.clone());
51                        OakDiagnostics { result: Err(build_error), diagnostics }
52                    }
53                }
54            }
55            Err(parse_error) => OakDiagnostics { result: Err(parse_error), diagnostics: parse_result.diagnostics },
56        }
57    }
58}