Abstract infix macro
Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
parent
a590839b21
commit
4f770c1f24
7 changed files with 42 additions and 35 deletions
|
|
@ -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| {
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ TemplateAst {
|
|||
root: [
|
||||
Interpolation {
|
||||
prev_whitespace_content: None,
|
||||
expression: Operation {
|
||||
expression: AccessOperation {
|
||||
op: Dot,
|
||||
lhs: ConditionalAccess(
|
||||
[Ident]"foo" (4..7),
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ TemplateAst {
|
|||
expression: FunctionCall {
|
||||
name: [Ident]"foo" (4..7),
|
||||
args: [
|
||||
Operation {
|
||||
MathOperation {
|
||||
op: Times,
|
||||
lhs: Literal {
|
||||
source: [Literal(Integer(2))]"2" (8..9),
|
||||
|
|
@ -27,7 +27,7 @@ TemplateAst {
|
|||
FunctionCall {
|
||||
name: [Ident]"bar" (15..18),
|
||||
args: [
|
||||
Operation {
|
||||
MathOperation {
|
||||
op: Plus,
|
||||
lhs: Literal {
|
||||
source: [Literal(Integer(2))]"2" (19..20),
|
||||
|
|
|
|||
|
|
@ -6,9 +6,9 @@ TemplateAst {
|
|||
root: [
|
||||
Interpolation {
|
||||
prev_whitespace_content: None,
|
||||
expression: Operation {
|
||||
expression: MathOperation {
|
||||
op: Or,
|
||||
lhs: Operation {
|
||||
lhs: MathOperation {
|
||||
op: And,
|
||||
lhs: Literal {
|
||||
source: [Literal(Bool(true))]"true" (4..8),
|
||||
|
|
@ -23,9 +23,9 @@ TemplateAst {
|
|||
},
|
||||
},
|
||||
},
|
||||
rhs: Operation {
|
||||
rhs: MathOperation {
|
||||
op: And,
|
||||
lhs: Operation {
|
||||
lhs: MathOperation {
|
||||
op: GreaterOrEqual,
|
||||
lhs: Literal {
|
||||
source: [Literal(Integer(3))]"3" (21..22),
|
||||
|
|
@ -40,7 +40,7 @@ TemplateAst {
|
|||
},
|
||||
},
|
||||
},
|
||||
rhs: Operation {
|
||||
rhs: MathOperation {
|
||||
op: Equal,
|
||||
lhs: Literal {
|
||||
source: [Literal(Integer(5))]"5" (31..32),
|
||||
|
|
|
|||
|
|
@ -6,9 +6,9 @@ TemplateAst {
|
|||
root: [
|
||||
Interpolation {
|
||||
prev_whitespace_content: None,
|
||||
expression: Operation {
|
||||
expression: MathOperation {
|
||||
op: Plus,
|
||||
lhs: Operation {
|
||||
lhs: MathOperation {
|
||||
op: Times,
|
||||
lhs: Literal {
|
||||
source: [Literal(Integer(5))]"5" (4..5),
|
||||
|
|
@ -23,7 +23,7 @@ TemplateAst {
|
|||
},
|
||||
},
|
||||
},
|
||||
rhs: Operation {
|
||||
rhs: MathOperation {
|
||||
op: Divide,
|
||||
lhs: Literal {
|
||||
source: [Literal(Integer(2))]"2" (12..13),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue