Add first working pipeline of parse -> ast -> instr -> render

Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
Marcel Müller 2026-03-06 11:03:48 +01:00
parent f5050e369e
commit 1ea15f0e49
5 changed files with 234 additions and 6 deletions

View file

@ -24,6 +24,12 @@ pub struct TemplateAst<'input> {
root: Vec<TemplateAstExpr<'input>>,
}
impl<'input> TemplateAst<'input> {
pub fn root(&self) -> &[TemplateAstExpr<'input>] {
&self.root
}
}
#[derive(Debug, Clone)]
pub struct AstError {
pub(crate) message: Option<String>,
@ -145,6 +151,7 @@ pub enum TemplateAstExpr<'input> {
StaticContent(TemplateToken<'input>),
Interpolation {
prev_whitespace: Option<TemplateToken<'input>>,
wants_output: Option<TemplateToken<'input>>,
expression: Box<TemplateAstExpr<'input>>,
post_whitespace: Option<TemplateToken<'input>>,
},
@ -172,10 +179,11 @@ fn parse_interpolation<'input>(
)
.with_taken()
.map(|(expr, taken)| expr.unwrap_or(TemplateAstExpr::Invalid(taken)));
let (prev_whitespace, _left, (expression, _right, post_whitespace)) = (
let (prev_whitespace, _left, (wants_output, expression, _right, post_whitespace)) = (
opt(TokenKind::Whitespace),
TokenKind::LeftDelim,
cut_err((
opt(TokenKind::WantsOutput),
delimited(ignore_ws, expr_parser, ignore_ws).map(Box::new),
TokenKind::RightDelim,
opt(TokenKind::Whitespace),
@ -185,6 +193,7 @@ fn parse_interpolation<'input>(
Ok(TemplateAstExpr::Interpolation {
prev_whitespace,
wants_output,
expression,
post_whitespace,
})
@ -230,7 +239,7 @@ mod tests {
#[test]
fn check_simple_variable_interpolation() {
let input = "Hello {{ world }}";
let input = "Hello {{= world }}";
let parsed = crate::parser::parse(input).unwrap();
@ -252,6 +261,12 @@ mod tests {
source: " ",
},
),
wants_output: Some(
TemplateToken {
kind: WantsOutput,
source: "=",
},
),
expression: VariableAccess(
TemplateToken {
kind: Ident,