Skip to main content

oak_matlab/builder/
mod.rs

1use crate::{ast::*, language::MatlabLanguage, parser::MatlabParser};
2use oak_core::{Builder, BuilderCache, GreenNode, OakDiagnostics, Source, SourceText, TextEdit};
3
4/// Matlab AST builder
5#[derive(Clone)]
6pub struct MatlabBuilder<'config> {
7    config: &'config MatlabLanguage,
8}
9
10impl<'config> MatlabBuilder<'config> {
11    /// Creates a new Matlab builder
12    pub fn new(config: &'config MatlabLanguage) -> Self {
13        Self { config }
14    }
15
16    /// Builds the root node
17    pub fn build_root(&self, green_tree: &GreenNode<MatlabLanguage>, source: &SourceText) -> Result<MatlabRoot, oak_core::OakError> {
18        let red_root = oak_core::tree::RedNode::new(green_tree, 0);
19        let mut items = Vec::new();
20        for child in red_root.children() {
21            if let oak_core::tree::RedTree::Node(node) = child {
22                if let Some(item) = self.build_item(&node, source) {
23                    items.push(item)
24                }
25            }
26        }
27        Ok(MatlabRoot { items })
28    }
29
30    /// Builds a single item (function, class, or statement)
31    pub fn build_item(&self, node: &oak_core::tree::RedNode<MatlabLanguage>, source: &SourceText) -> Option<Item> {
32        use crate::MatlabElementType::*;
33        let kind = node.green.kind;
34
35        match kind {
36            FunctionDef => {
37                let mut name = "anonymous".to_string();
38                for child in node.children() {
39                    if let oak_core::RedTree::Node(n) = child {
40                        if n.green.kind == Identifier {
41                            name = source.get_text_in(n.span()).to_string();
42                            break;
43                        }
44                    }
45                }
46                Some(Item::Function(crate::ast::Function { name, inputs: Vec::new(), outputs: Vec::new(), body: Vec::new(), span: node.span() }))
47            }
48            ClassDef => {
49                let mut name = "Unknown".to_string();
50                for child in node.children() {
51                    if let oak_core::RedTree::Node(n) = child {
52                        if n.green.kind == Identifier {
53                            name = source.get_text_in(n.span()).to_string();
54                            break;
55                        }
56                    }
57                }
58                Some(Item::Class(crate::ast::Class { name, superclasses: Vec::new(), properties: Vec::new(), methods: Vec::new(), span: node.span() }))
59            }
60            Expression | Block | Statement => Some(Item::Statement(crate::ast::Statement::Expression { value: source.get_text_in(node.span()).to_string(), span: node.span() })),
61            _ => None,
62        }
63    }
64}
65
66impl<'config> Builder<MatlabLanguage> for MatlabBuilder<'config> {
67    fn build<'a, S: Source + ?Sized>(&self, source: &S, edits: &[TextEdit], _cache: &'a mut impl BuilderCache<MatlabLanguage>) -> oak_core::builder::BuildOutput<MatlabLanguage> {
68        let parser = MatlabParser::new(self.config);
69        let lexer = crate::lexer::MatlabLexer::new(&self.config);
70        let mut cache = oak_core::parser::session::ParseSession::<MatlabLanguage>::default();
71        let parse_result = oak_core::parser::parse(&parser, &lexer, source, edits, &mut cache);
72
73        match parse_result.result {
74            Ok(green_tree) => {
75                let source_text = SourceText::new(source.get_text_in((0..source.length()).into()).into_owned());
76                OakDiagnostics { result: self.build_root(&green_tree, &source_text), diagnostics: parse_result.diagnostics }
77            }
78            Err(parse_error) => OakDiagnostics { result: Err(parse_error), diagnostics: parse_result.diagnostics },
79        }
80    }
81}