oak_handlebars/builder/
mod.rs1use 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#[derive(Clone)]
12pub struct HandlebarsBuilder<'config> {
13 config: &'config HandlebarsLanguage,
14}
15
16impl<'config> HandlebarsBuilder<'config> {
17 pub fn new(config: &'config HandlebarsLanguage) -> Self {
19 Self { config }
20 }
21}
22
23impl<'config> Builder<HandlebarsLanguage> for HandlebarsBuilder<'config> {
24 fn build<'a, S: Source + ?Sized>(&self, source: &S, edits: &[TextEdit], _cache: &'a mut impl BuilderCache<HandlebarsLanguage>) -> OakDiagnostics<HandlebarsRoot> {
25 let parser = HandlebarsParser::new(self.config);
26
27 let mut parse_cache = oak_core::parser::session::ParseSession::<HandlebarsLanguage>::default();
28 let parse_result = parser.parse(source, edits, &mut parse_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> HandlebarsBuilder<'config> {
48 pub(crate) fn build_root<'a>(&self, green_tree: &'a GreenNode<'a, HandlebarsLanguage>, source: &SourceText) -> Result<HandlebarsRoot, OakError> {
49 let red_root = RedNode::new(green_tree, 0);
50 let mut nodes = Vec::new();
51
52 for child in red_root.children() {
53 if let RedTree::Node(n) = child {
54 nodes.push(self.build_node(n, source)?)
55 }
56 else if let RedTree::Leaf(t) = child {
57 if t.kind == HandlebarsTokenType::Content {
58 nodes.push(TemplateNode::Content(Content { text: text(source, t.span.clone().into()), span: t.span.clone().into() }))
59 }
60 }
61 }
62 Ok(HandlebarsRoot { nodes })
63 }
64
65 fn build_node(&self, node: RedNode<HandlebarsLanguage>, source: &SourceText) -> Result<TemplateNode, OakError> {
66 match node.green.kind {
67 HandlebarsElementType::Mustache => {
68 Ok(TemplateNode::Mustache(Mustache { expression: Expression::Path(text(source, node.span())), is_unescaped: false, span: node.span() }))
70 }
71 HandlebarsElementType::Block => Ok(TemplateNode::Block(Block { name: "todo".to_string(), params: Vec::new(), body: Vec::new(), inverse: None, span: node.span() })),
72 HandlebarsElementType::CommentNode => Ok(TemplateNode::Comment(Comment { text: text(source, node.span()), span: node.span() })),
73 _ => Ok(TemplateNode::Content(Content { text: text(source, node.span()), span: node.span() })),
74 }
75 }
76}
77
78fn text(source: &SourceText, range: Range<usize>) -> String {
79 source.get_text_in(range).to_string()
80}