From 27bc69806a6bb0f96cce0ab91ef9192f52936cf7 Mon Sep 17 00:00:00 2001 From: Joscha Date: Fri, 18 Nov 2022 14:12:10 +0100 Subject: [PATCH] Parse variable expressions --- src/parser.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/parser.rs b/src/parser.rs index 3fd16f6..cfcded6 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -194,13 +194,29 @@ fn table_constr( }) } +fn expr_var( + expr: impl Parser + Clone, +) -> impl Parser { + just("[") + .ignore_then(space()) + .then(expr) + .then(space()) + .then_ignore(just("]")) + .map_with_span(|((s0, index), s1), span| Expr::Var { + s0, + index: Box::new(index), + s1, + 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); - lit.or(table_constr) + lit.or(table_constr).or(expr_var(expr)) } pub fn parser() -> impl Parser {