Skip to main content

oak_handlebars/builder/
mod.rs

1use crate::{
2    ast::*,
3    language::HandlebarsLanguage,
4    lexer::token_type::HandlebarsTokenType,
5    parser::{HandlebarsParser, element_type::HandlebarsElementType},
6};
7use core::range::Range;
8use oak_core::{Builder, BuilderCache, GreenNode, OakDiagnostics, OakError, Parser, RedNode, RedTree, SourceText, TextEdit, source::Source};
9
10/// Handlebars 语言的 AST 构建器
11#[derive(Clone)]
12pub struct HandlebarsBuilder<'config> {
13    config: &'config HandlebarsLanguage,
14}
15
16impl<'config> HandlebarsBuilder<'config> {
17    pub fn new(config: &'config HandlebarsLanguage) -> Self {
18        Self { config }
19    }
20}
21
22impl<'config> Builder<HandlebarsLanguage> for HandlebarsBuilder<'config> {
23    fn build<'a, S: Source + ?Sized>(&self, source: &S, edits: &[TextEdit], _cache: &'a mut impl BuilderCache<HandlebarsLanguage>) -> OakDiagnostics<HandlebarsRoot> {
24        let parser = HandlebarsParser::new(self.config);
25
26        let mut parse_cache = oak_core::parser::session::ParseSession::<HandlebarsLanguage>::default();
27        let parse_result = parser.parse(source, edits, &mut parse_cache);
28
29        match parse_result.result {
30            Ok(green_tree) => {
31                let source_text = SourceText::new(source.get_text_in((0..source.length()).into()).into_owned());
32                match self.build_root(green_tree, &source_text) {
33                    Ok(ast_root) => OakDiagnostics { result: Ok(ast_root), diagnostics: parse_result.diagnostics },
34                    Err(build_error) => {
35                        let mut diagnostics = parse_result.diagnostics;
36                        diagnostics.push(build_error.clone());
37                        OakDiagnostics { result: Err(build_error), diagnostics }
38                    }
39                }
40            }
41            Err(parse_error) => OakDiagnostics { result: Err(parse_error), diagnostics: parse_result.diagnostics },
42        }
43    }
44}
45
46impl<'config> HandlebarsBuilder<'config> {
47    pub(crate) fn build_root<'a>(&self, green_tree: &'a GreenNode<'a, HandlebarsLanguage>, source: &SourceText) -> Result<HandlebarsRoot, OakError> {
48        let red_root = RedNode::new(green_tree, 0);
49        let mut nodes = Vec::new();
50
51        for child in red_root.children() {
52            if let RedTree::Node(n) = child {
53                nodes.push(self.build_node(n, source)?)
54            }
55            else if let RedTree::Leaf(t) = child {
56                if t.kind == HandlebarsTokenType::Content {
57                    nodes.push(TemplateNode::Content(Content { text: text(source, t.span.clone().into()), span: t.span.clone().into() }))
58                }
59            }
60        }
61        Ok(HandlebarsRoot { nodes })
62    }
63
64    fn build_node(&self, node: RedNode<HandlebarsLanguage>, source: &SourceText) -> Result<TemplateNode, OakError> {
65        match node.green.kind {
66            HandlebarsElementType::Mustache => {
67                // Simplified Mustache building
68                Ok(TemplateNode::Mustache(Mustache { expression: Expression::Path(text(source, node.span())), is_unescaped: false, span: node.span() }))
69            }
70            HandlebarsElementType::Block => Ok(TemplateNode::Block(Block { name: "todo".to_string(), params: Vec::new(), body: Vec::new(), inverse: None, span: node.span() })),
71            HandlebarsElementType::CommentNode => Ok(TemplateNode::Comment(Comment { text: text(source, node.span()), span: node.span() })),
72            _ => Ok(TemplateNode::Content(Content { text: text(source, node.span()), span: node.span() })),
73        }
74    }
75}
76
77fn text(source: &SourceText, range: Range<usize>) -> String {
78    source.get_text_in(range).to_string()
79}