Skip to main content

oak_ini/builder/
mod.rs

1use crate::{IniParser, ast::*, language::IniLanguage, lexer::token_type::IniTokenType, parser::element_type::IniElementType};
2use oak_core::{Builder, BuilderCache, GreenNode, OakDiagnostics, OakError, Parser, RedNode, RedTree, SourceText, TextEdit, source::Source};
3
4/// INI AST builder implementation.
5pub struct IniBuilder<'config> {
6    /// The INI language configuration.
7    config: &'config IniLanguage,
8}
9
10impl<'config> IniBuilder<'config> {
11    /// Creates a new `IniBuilder` with the given configuration.
12    pub fn new(config: &'config IniLanguage) -> Self {
13        Self { config }
14    }
15}
16
17impl<'config> Builder<IniLanguage> for IniBuilder<'config> {
18    fn build<'a, S: Source + ?Sized>(&self, source: &S, edits: &[TextEdit], _cache: &'a mut impl BuilderCache<IniLanguage>) -> OakDiagnostics<IniRoot> {
19        let parser = IniParser::new(self.config);
20        let mut cache = oak_core::parser::session::ParseSession::<IniLanguage>::default();
21        let parse_result = parser.parse(source, edits, &mut cache);
22
23        match parse_result.result {
24            Ok(green_tree) => {
25                let source_text = SourceText::new(source.get_text_in((0..source.length()).into()).into_owned());
26                match self.build_root(green_tree, &source_text) {
27                    Ok(ast_root) => OakDiagnostics { result: Ok(ast_root), diagnostics: parse_result.diagnostics },
28                    Err(build_error) => {
29                        let mut diagnostics = parse_result.diagnostics;
30                        diagnostics.push(build_error.clone());
31                        OakDiagnostics { result: Err(build_error), diagnostics }
32                    }
33                }
34            }
35            Err(parse_error) => OakDiagnostics { result: Err(parse_error), diagnostics: parse_result.diagnostics },
36        }
37    }
38}
39
40impl<'config> IniBuilder<'config> {
41    /// Builds the AST root from a green tree.
42    pub(crate) fn build_root<'a>(&self, green_tree: &'a GreenNode<'a, IniLanguage>, source: &SourceText) -> Result<IniRoot, OakError> {
43        let red_root = RedNode::new(green_tree, 0);
44        let mut sections = Vec::new();
45        let mut properties = Vec::new();
46
47        for child in red_root.children() {
48            if let RedTree::Node(n) = child {
49                if n.green.kind == IniElementType::Table {
50                    sections.push(self.build_section(n, source)?);
51                }
52                else if n.green.kind == IniElementType::KeyValue {
53                    properties.push(self.build_property(n, source)?);
54                }
55            }
56        }
57        Ok(IniRoot { sections, properties })
58    }
59
60    /// Builds a section from a red node.
61    fn build_section(&self, node: RedNode<IniLanguage>, source: &SourceText) -> Result<Section, OakError> {
62        let span = node.span();
63        let mut name = String::new();
64        let mut properties = Vec::new();
65
66        for child in node.children() {
67            match child {
68                RedTree::Leaf(t) if t.kind == IniTokenType::Identifier => name = source.get_text_in(t.span.clone().into()).to_string(),
69                RedTree::Node(n) if n.green.kind == IniElementType::KeyValue => properties.push(self.build_property(n, source)?),
70                _ => {}
71            }
72        }
73        Ok(Section { name, properties, span })
74    }
75
76    /// Builds a property from a red node.
77    fn build_property(&self, node: RedNode<IniLanguage>, source: &SourceText) -> Result<Property, OakError> {
78        let span = node.span();
79        let mut key = String::new();
80        let mut value = String::new();
81
82        for child in node.children() {
83            if let RedTree::Node(n) = child {
84                if n.green.kind == IniElementType::Key {
85                    key = source.get_text_in(n.span().into()).to_string();
86                }
87                else if n.green.kind == IniElementType::Value {
88                    value = source.get_text_in(n.span().into()).to_string();
89                }
90            }
91        }
92        Ok(Property { key, value, span })
93    }
94}