Parse conditional access

Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
Marcel Müller 2026-03-15 12:34:35 +01:00
parent beac224f5b
commit 9b87e7089f
3 changed files with 41 additions and 1 deletions

View file

@ -1,9 +1,9 @@
use std::collections::BTreeMap;
use crate::parser::TemplateAstExpr;
use crate::input::NomoInput;
use crate::lexer::TemplateToken;
use crate::lexer::TokenOperator;
use crate::parser::TemplateAstExpr;
use crate::value::NomoValue;
pub struct EmitMachine {
@ -424,6 +424,7 @@ fn emit_ast_expr(
| TemplateAstExpr::Literal { .. }
| TemplateAstExpr::FunctionCall { .. }
| TemplateAstExpr::Operation { .. }
| TemplateAstExpr::ConditionalAccess { .. }
| TemplateAstExpr::VariableAccess { .. } => eval.push(Instruction::Abort),
}
}
@ -475,6 +476,7 @@ fn emit_expr_load(
slot: emit_slot,
});
}
TemplateAstExpr::ConditionalAccess { .. } => todo!(),
TemplateAstExpr::Invalid { .. } => eval.push(Instruction::Abort),
TemplateAstExpr::StaticContent { .. } | TemplateAstExpr::Interpolation { .. } => {
unreachable!("Invalid AST here")

View file

@ -2,6 +2,7 @@ use thiserror::Error;
use winnow::Parser;
use winnow::RecoverableParser;
use winnow::combinator::Infix::Left;
use winnow::combinator::Postfix;
use winnow::combinator::alt;
use winnow::combinator::cut_err;
use winnow::combinator::delimited;
@ -249,6 +250,7 @@ pub enum TemplateAstExpr<'input> {
value_expression: Box<TemplateAstExpr<'input>>,
},
ForElse,
ConditionalAccess(TemplateToken),
VariableAccess(TemplateToken),
IfConditional {
expression: Box<TemplateAstExpr<'input>>,
@ -709,6 +711,16 @@ fn parse_expression<'input>(
Left Lesser => 15,
Left LesserOrEqual => 15,
]
}).postfix(dispatch! { surrounded(ws, parse_operator);
TokenOperator::QuestionMark => Postfix(22, |input, rhs| {
match rhs {
TemplateAstExpr::VariableAccess(access) => Ok(TemplateAstExpr::ConditionalAccess(access)),
_ => Err(AstError::from_input(input)),
}
}),
_ => fail
}),
)
.parse_next(input)
@ -1048,4 +1060,15 @@ mod tests {
insta::assert_debug_snapshot!(ast);
}
#[test]
fn check_conditional_access() {
let input = "{{= foo? }}";
let parsed = crate::lexer::parse(input.into()).unwrap();
let ast = panic_pretty(input, parse(parsed.tokens()));
insta::assert_debug_snapshot!(ast);
}
}

View file

@ -0,0 +1,15 @@
---
source: src/parser/mod.rs
expression: ast
---
TemplateAst {
root: [
Interpolation {
prev_whitespace_content: None,
expression: ConditionalAccess(
[Ident]"foo" (4..7),
),
post_whitespace_content: None,
},
],
}