Skip to main content

oak_bash/builder/
mod.rs

1#![doc = include_str!("readme.md")]
2use crate::{ast::*, language::BashLanguage, parser::BashParser};
3use oak_core::{Builder, BuilderCache, GreenNode, OakDiagnostics, Parser, SourceText, TextEdit, source::Source};
4
5/// Bash 语言的 AST 构建器
6#[derive(Clone)]
7pub struct BashBuilder<'config> {
8    config: &'config BashLanguage,
9}
10
11impl<'config> BashBuilder<'config> {
12    pub fn new(config: &'config BashLanguage) -> Self {
13        Self { config }
14    }
15
16    pub fn build_root(&self, _green: &GreenNode<BashLanguage>, _source: &SourceText) -> Result<BashRoot, oak_core::OakError> {
17        // 简化的 AST 构建逻辑
18        Ok(BashRoot { elements: vec![] })
19    }
20}
21
22impl<'config> Builder<BashLanguage> for BashBuilder<'config> {
23    fn build<'a, S: Source + ?Sized>(&self, source: &'a S, edits: &[TextEdit], _cache: &'a mut impl BuilderCache<BashLanguage>) -> oak_core::builder::BuildOutput<BashLanguage> {
24        let parser = BashParser::new(self.config);
25        let mut cache = oak_core::parser::ParseSession::<BashLanguage>::default();
26        let parse_result = parser.parse(source, edits, &mut cache);
27
28        match parse_result.result {
29            Ok(green_tree) => {
30                let source_text = SourceText::new(source.get_text_in((0..source.length()).into()).into_owned());
31                match self.build_root(green_tree, &source_text) {
32                    Ok(ast_root) => OakDiagnostics { result: Ok(ast_root), diagnostics: parse_result.diagnostics },
33                    Err(build_error) => {
34                        let mut diagnostics = parse_result.diagnostics;
35                        diagnostics.push(build_error.clone());
36                        OakDiagnostics { result: Err(build_error), diagnostics }
37                    }
38                }
39            }
40            Err(parse_error) => OakDiagnostics { result: Err(parse_error), diagnostics: parse_result.diagnostics },
41        }
42    }
43}