Abstract infix macro

Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
Marcel Müller 2026-03-15 13:48:51 +01:00
parent a590839b21
commit 4f770c1f24
7 changed files with 42 additions and 35 deletions

View file

@ -262,7 +262,12 @@ pub enum TemplateAstExpr<'input> {
expression: Option<Box<TemplateAstExpr<'input>>>,
},
Invalid(&'input [TemplateToken]),
Operation {
MathOperation {
op: TokenOperator,
lhs: Box<TemplateAstExpr<'input>>,
rhs: Box<TemplateAstExpr<'input>>,
},
AccessOperation {
op: TokenOperator,
lhs: Box<TemplateAstExpr<'input>>,
rhs: Box<TemplateAstExpr<'input>>,
@ -681,10 +686,10 @@ fn parse_expression<'input>(
input: &mut Input<'input>,
) -> Result<TemplateAstExpr<'input>, AstError> {
macro_rules! infix {
($parser:expr => [ $($side:tt $val:tt => $prec:expr),* $(,)? ]) => {
($parser:expr => [ $($side:tt $op:tt $val:tt => $prec:expr),* $(,)? ]) => {
dispatch! { surrounded(ws, parse_operator);
$(
TokenOperator::$val => $side($prec, |_, lhs, rhs| Ok(TemplateAstExpr::Operation {
TokenOperator::$val => $side($prec, |_, lhs, rhs| Ok(TemplateAstExpr::$op {
op: TokenOperator::$val,
lhs: Box::new(lhs),
rhs: Box::new(rhs)
@ -698,19 +703,19 @@ fn parse_expression<'input>(
"expression",
expression(surrounded(ws, parse_operand)).infix(infix! {
surrounded(ws, parse_operator) => [
Left Plus => 18,
Left Minus => 18,
Left Times => 20,
Left Divide => 20,
Left And => 10,
Left Or => 7,
Left Equal => 12,
Left NotEqual => 12,
Left Greater => 15,
Left GreaterOrEqual => 15,
Left Lesser => 15,
Left LesserOrEqual => 15,
Left Dot => 23,
Left MathOperation Plus => 18,
Left MathOperation Minus => 18,
Left MathOperation Times => 20,
Left MathOperation Divide => 20,
Left MathOperation And => 10,
Left MathOperation Or => 7,
Left MathOperation Equal => 12,
Left MathOperation NotEqual => 12,
Left MathOperation Greater => 15,
Left MathOperation GreaterOrEqual => 15,
Left MathOperation Lesser => 15,
Left MathOperation LesserOrEqual => 15,
Left AccessOperation Dot => 23,
]
}).postfix(dispatch! { surrounded(ws, parse_operator);
TokenOperator::QuestionMark => Postfix(22, |input, rhs| {