diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-02-17 17:13:06 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-02-17 17:13:06 +0100 |
commit | 825610cd64237c877f26cc882cac20704c80e046 (patch) | |
tree | 34dd9c886ea386ccbf009ba29387cccb920caae5 | |
parent | 288b9133e1c756e5755affc556b57de6cba3bcd4 (diff) | |
download | askama-825610cd64237c877f26cc882cac20704c80e046.tar.gz askama-825610cd64237c877f26cc882cac20704c80e046.tar.bz2 askama-825610cd64237c877f26cc882cac20704c80e046.zip |
Use better parsing strategy for filter expressions
-rw-r--r-- | askama/src/parser.rs | 48 |
1 files 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<Target>, 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<Expr>, alt!( - expr_filtered | - expr_str_lit + expr_str_lit | + expr_var +)); + +named!(filter, do_parse!( + tag_s!("|") >> + fname: alphanumeric >> + (fname) +)); + +named!(expr_filtered<Expr>, 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, "&"); |