Skip to main content

oak_ini/builder/
mod.rs

1use crate::{IniParser, ast::*, kind::IniSyntaxKind, language::IniLanguage};
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                match n.green.kind {
46                    IniSyntaxKind::Table => {
47                        sections.push(self.build_section(n, source)?);
48                    }
49                    IniSyntaxKind::KeyValue => {
50                        properties.push(self.build_property(n, source)?);
51                    }
52                    _ => {}
53                }
54            }
55        }
56        Ok(IniRoot { sections, properties })
57    }
58
59    fn build_section(&self, node: RedNode<IniLanguage>, source: &SourceText) -> Result<Section, OakError> {
60        let span = node.span();
61        let mut name = String::new();
62        let mut properties = Vec::new();
63
64        for child in node.children() {
65            match child {
66                RedTree::Leaf(t) if t.kind == IniSyntaxKind::Identifier => {
67                    name = source.get_text_in(t.span.clone().into()).to_string();
68                }
69                RedTree::Node(n) if n.green.kind == IniSyntaxKind::KeyValue => {
70                    properties.push(self.build_property(n, source)?);
71                }
72                _ => {}
73            }
74        }
75        Ok(Section { name, properties, span })
76    }
77
78    fn build_property(&self, node: RedNode<IniLanguage>, source: &SourceText) -> Result<Property, OakError> {
79        let span = node.span();
80        let mut key = String::new();
81        let mut value = String::new();
82
83        for child in node.children() {
84            if let RedTree::Node(n) = child {
85                match n.green.kind {
86                    IniSyntaxKind::Key => key = source.get_text_in(n.span().into()).to_string(),
87                    IniSyntaxKind::Value => value = source.get_text_in(n.span().into()).to_string(),
88                    _ => {}
89                }
90            }
91        }
92        Ok(Property { key, value, span })
93    }
94}