Skip to main content

oak_dart/builder/
mod.rs

1use crate::{ast::*, kind::DartSyntaxKind, language::DartLanguage, parser::DartParser};
2use oak_core::{Builder, BuilderCache, GreenNode, Parser, RedNode, RedTree, SourceText, TextEdit, source::Source};
3
4/// Dart 语言的 AST 构建器
5#[derive(Clone)]
6pub struct DartBuilder<'config> {
7    config: &'config DartLanguage,
8}
9
10impl<'config> DartBuilder<'config> {
11    pub fn new(config: &'config DartLanguage) -> Self {
12        Self { config }
13    }
14
15    pub(crate) fn build_root<'a>(&self, green_tree: &'a GreenNode<'a, DartLanguage>, _source: &SourceText) -> Result<DartRoot, oak_core::OakError> {
16        let red_root = RedNode::new(green_tree, 0);
17        let items = Vec::new();
18
19        for child in red_root.children() {
20            if let RedTree::Node(n) = child {
21                match n.green.kind {
22                    DartSyntaxKind::ClassDeclaration => {
23                        // TODO: Implement build_class
24                    }
25                    DartSyntaxKind::FunctionDeclaration => {
26                        // TODO: Implement build_function
27                    }
28                    _ => {}
29                }
30            }
31        }
32        Ok(DartRoot { items })
33    }
34}
35
36impl<'config> Builder<DartLanguage> for DartBuilder<'config> {
37    fn build<'a, S: Source + ?Sized>(&self, source: &S, edits: &[TextEdit], _cache: &'a mut impl BuilderCache<DartLanguage>) -> oak_core::builder::BuildOutput<DartLanguage> {
38        let parser = DartParser::new(self.config);
39        let mut parse_session = oak_core::parser::session::ParseSession::<DartLanguage>::default();
40        let parse_result = parser.parse(source, edits, &mut parse_session);
41
42        match parse_result.result {
43            Ok(green_tree) => {
44                let source_text = SourceText::new(source.get_text_in((0..source.length()).into()).into_owned());
45                match self.build_root(green_tree, &source_text) {
46                    Ok(ast_root) => oak_core::OakDiagnostics { result: Ok(ast_root), diagnostics: parse_result.diagnostics },
47                    Err(e) => oak_core::OakDiagnostics { result: Err(e), diagnostics: parse_result.diagnostics },
48                }
49            }
50            Err(e) => oak_core::OakDiagnostics { result: Err(e), diagnostics: parse_result.diagnostics },
51        }
52    }
53}