Skip to main content

oak_d2/builder/
mod.rs

1use crate::{D2ElementType, D2Parser, D2TokenType, ast::*, language::D2Language};
2use oak_core::{
3    Builder, BuilderCache, GreenNode, OakDiagnostics, OakError, Parser, RedNode, RedTree,
4    builder::BuildOutput,
5    source::{Source, SourceText, TextEdit},
6};
7
8/// AST builder for the D2 language.
9#[derive(Clone, Copy)]
10pub struct D2Builder<'config> {
11    /// Language configuration.
12    config: &'config D2Language,
13}
14
15impl<'config> D2Builder<'config> {
16    /// Creates a new `D2Builder` with the given language configuration.
17    pub fn new(config: &'config D2Language) -> Self {
18        Self { config }
19    }
20}
21
22impl<'config> Builder<D2Language> for D2Builder<'config> {
23    /// Builds the D2 AST from the green tree.
24    fn build<'a, S: Source + ?Sized>(&self, source: &S, edits: &[TextEdit], _cache: &'a mut impl BuilderCache<D2Language>) -> BuildOutput<D2Language> {
25        let parser = D2Parser::new(self.config);
26
27        let mut cache = oak_core::parser::session::ParseSession::<D2Language>::default();
28        let parse_result = parser.parse(source, edits, &mut cache);
29
30        match parse_result.result {
31            Ok(green_tree) => {
32                let source_text = SourceText::new(source.get_text_in((0..source.length()).into()).into_owned());
33                match self.build_root(green_tree, &source_text) {
34                    Ok(ast_root) => OakDiagnostics { result: Ok(ast_root), diagnostics: parse_result.diagnostics },
35                    Err(build_error) => {
36                        let mut diagnostics = parse_result.diagnostics;
37                        diagnostics.push(build_error.clone());
38                        OakDiagnostics { result: Err(build_error), diagnostics }
39                    }
40                }
41            }
42            Err(parse_error) => OakDiagnostics { result: Err(parse_error), diagnostics: parse_result.diagnostics },
43        }
44    }
45}
46
47impl<'config> D2Builder<'config> {
48    /// Builds the AST root from the green tree.
49    pub(crate) fn build_root<'a>(&self, green_tree: &'a GreenNode<'a, D2Language>, source: &SourceText) -> Result<D2Root, OakError> {
50        let root_node = RedNode::new(green_tree, 0);
51        let mut elements = Vec::new();
52
53        for child in root_node.children() {
54            if let RedTree::Node(n) = child {
55                match n.green.kind {
56                    D2ElementType::Shape => elements.push(D2Element::Shape(self.build_shape(n, source)?)),
57                    D2ElementType::Connection => elements.push(D2Element::Connection(self.build_connection(n, source)?)),
58                    _ => {}
59                }
60            }
61        }
62
63        Ok(D2Root { elements, span: root_node.span() })
64    }
65
66    fn build_shape(&self, node: RedNode<D2Language>, source: &SourceText) -> Result<Shape, OakError> {
67        let mut id = String::new();
68        let mut label = None;
69
70        for child in node.children() {
71            match child {
72                RedTree::Leaf(t) => match t.kind {
73                    D2TokenType::Id => id = text(source, t.span.clone()),
74                    D2TokenType::Label => label = Some(text(source, t.span.clone())),
75                    _ => {}
76                },
77                _ => {}
78            }
79        }
80
81        Ok(Shape { id, label })
82    }
83
84    fn build_connection(&self, node: RedNode<D2Language>, source: &SourceText) -> Result<Connection, OakError> {
85        let mut from = String::new();
86        let mut to = String::new();
87
88        let mut ids = Vec::new();
89        for child in node.children() {
90            if let RedTree::Leaf(t) = child {
91                if t.kind == D2TokenType::Id {
92                    ids.push(text(source, t.span.clone()));
93                }
94            }
95        }
96
97        if ids.len() >= 2 {
98            from = ids[0].clone();
99            to = ids[1].clone();
100        }
101
102        Ok(Connection { from, to, span: node.span() })
103    }
104}
105
106fn text(source: &SourceText, span: core::range::Range<usize>) -> String {
107    source.get_text_in(span).to_string()
108}