From 621a829d1d0cf5a3c63d26c2fe1db8e2887fe1ae Mon Sep 17 00:00:00 2001 From: Joscha Date: Fri, 18 Nov 2022 14:20:29 +0100 Subject: [PATCH] Parse variable assignment expressions --- src/parser.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/parser.rs b/src/parser.rs index 94677a7..62680a2 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -210,14 +210,41 @@ fn expr_var( }) } +fn expr_var_assign( + expr: impl Parser + Clone, +) -> impl Parser { + just("[") + .ignore_then(space()) + .then(expr.clone()) + .then(space()) + .then_ignore(just("]")) + .then(space()) + .then_ignore(just("=")) + .then(space()) + .then(expr) + .map_with_span( + |(((((s0, index), s1), s2), s3), value), span| Expr::VarAssign { + s0, + index: Box::new(index), + s1, + s2, + s3, + value: Box::new(value), + span, + }, + ) +} + fn expr( expr: impl Parser + Clone, ) -> impl Parser { let lit = lit(expr.clone()).map(Expr::Lit); let table_constr = table_constr(expr.clone()).map(Expr::TableConstr); + let var = expr_var(expr.clone()); let var_ident = ident().map(Expr::VarIdent); + let var_assign = expr_var_assign(expr.clone()); - lit.or(table_constr).or(expr_var(expr)).or(var_ident) + lit.or(table_constr).or(var_assign).or(var).or(var_ident) } pub fn parser() -> impl Parser {