Fix issue with repeating {{ else }} blocks

Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
Marcel Müller 2026-03-09 16:02:55 +01:00
parent 462355b6f2
commit b0620a00d5
3 changed files with 63 additions and 28 deletions

View file

@ -345,6 +345,26 @@ fn parse_conditional_chain<'input>(
chain.push(if_block); chain.push(if_block);
let content = resume_after_cut(
inner_conditional_chain,
repeat_till(0.., any, parse_end).map(|((), _)| ()),
)
.parse_next(input)?;
chain.extend(content.into_iter().flatten());
Ok(TemplateAstExpr::ConditionalChain { chain })
})
.parse_next(input)
}
fn inner_conditional_chain<'input>(
input: &mut Input<'input>,
) -> Result<Vec<TemplateAstExpr<'input>>, AstError> {
let mut needs_end = false;
let mut chain = vec![];
loop { loop {
let (content, end_block): (Vec<_>, _) = repeat_till( let (content, end_block): (Vec<_>, _) = repeat_till(
0.., 0..,
@ -366,6 +386,16 @@ fn parse_conditional_chain<'input>(
false false
}; };
if !is_end && needs_end {
return Err(AstError::from_input(input).cut());
}
if let TemplateAstExpr::Block { expression, .. } = &end_block
&& let TemplateAstExpr::ElseConditional { expression: None } = &**expression
{
needs_end = true;
}
chain.push(end_block); chain.push(end_block);
if is_end { if is_end {
@ -373,9 +403,7 @@ fn parse_conditional_chain<'input>(
} }
} }
Ok(TemplateAstExpr::ConditionalChain { chain }) Ok(chain)
})
.parse_next(input)
} }
fn parse_conditional_if<'input>( fn parse_conditional_if<'input>(

View file

@ -5,9 +5,13 @@ fn check_files() {
for file in files { for file in files {
let input = std::fs::read_to_string(file.unwrap().path()).unwrap(); let input = std::fs::read_to_string(file.unwrap().path()).unwrap();
let parsed = nomo::parser::parse(input.into()).unwrap(); let Ok(parsed) = nomo::parser::parse(input.into()) else {
continue;
};
let ast = nomo::ast::parse(parsed.tokens()).unwrap(); let Ok(ast) = nomo::ast::parse(parsed.tokens()) else {
continue;
};
let _emit = nomo::emit::emit_machine(ast); let _emit = nomo::emit::emit_machine(ast);
} }

View file

@ -0,0 +1,3 @@
{{ if t }}
{{else}}{{ else }}
{{end }}