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