From 825610cd64237c877f26cc882cac20704c80e046 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 17 Feb 2017 17:13:06 +0100 Subject: Use better parsing strategy for filter expressions --- askama/src/parser.rs | 48 ++++++++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/askama/src/parser.rs b/askama/src/parser.rs index b34492a..6d0e58f 100644 --- a/askama/src/parser.rs +++ b/askama/src/parser.rs @@ -85,32 +85,28 @@ named!(target_single, map!(alphanumeric, |s| Target::Name(str::from_utf8(s).unwrap()) )); -fn expr_filtered(i: &[u8]) -> IResult<&[u8], Expr> { - let (mut left, mut expr) = match expr_var(i) { - IResult::Error(err) => { return IResult::Error(err); }, - IResult::Incomplete(needed) => { return IResult::Incomplete(needed); }, - IResult::Done(left, res) => (left, res), - }; - while left[0] == b'|' { - match alphanumeric(&left[1..]) { - IResult::Error(err) => { - return IResult::Error(err); - }, - IResult::Incomplete(needed) => { - return IResult::Incomplete(needed); - }, - IResult::Done(new_left, res) => { - left = new_left; - expr = Expr::Filter(str::from_utf8(res).unwrap(), Box::new(expr)); - }, - }; - } - IResult::Done(left, expr) -} - named!(expr_single, alt!( - expr_filtered | - expr_str_lit + expr_str_lit | + expr_var +)); + +named!(filter, do_parse!( + tag_s!("|") >> + fname: alphanumeric >> + (fname) +)); + +named!(expr_filtered, do_parse!( + obj: expr_single >> + filters: many0!(filter) >> + ({ + let mut res = obj; + for f in filters { + let fname = str::from_utf8(f).unwrap(); + res = Expr::Filter(fname, Box::new(res)); + } + res + }) )); macro_rules! expr_prec_layer { @@ -127,7 +123,7 @@ macro_rules! expr_prec_layer { } } -expr_prec_layer!(expr_muldivmod, expr_single, "*", "/", "%"); +expr_prec_layer!(expr_muldivmod, expr_filtered, "*", "/", "%"); expr_prec_layer!(expr_addsub, expr_muldivmod, "+", "-"); expr_prec_layer!(expr_shifts, expr_addsub, ">>", "<<"); expr_prec_layer!(expr_band, expr_shifts, "&"); -- cgit