From 741dd5f43f8a6774f3dc4b24b122069e22a7b7d9 Mon Sep 17 00:00:00 2001 From: James Gilles Date: Fri, 3 May 2019 16:50:46 -0400 Subject: Simplify parser with nom's list combinators --- askama_derive/src/parser.rs | 165 ++++++++++++-------------------------------- 1 file changed, 44 insertions(+), 121 deletions(-) diff --git a/askama_derive/src/parser.rs b/askama_derive/src/parser.rs index c880202..a37a112 100644 --- a/askama_derive/src/parser.rs +++ b/askama_derive/src/parser.rs @@ -208,21 +208,16 @@ named!(expr_num_lit, map!(num_lit, |s| Expr::NumLit(s) )); -named!(expr_array_lit, do_parse!( - ws!(tag!("[")) >> - first: expr_any >> - rest: many0!(do_parse!( - ws!(tag!(",")) >> - part: expr_any >> - (part) - )) >> - ws!(tag!("]")) >> - ({ - let mut elements = vec![first]; - elements.extend(rest); - Expr::Array(elements) - }) -)); +named!(expr_array_lit, + delimited!( + ws!(tag!("[")), + map!(separated_nonempty_list!( + ws!(tag!(",")), + expr_any + ), |arr| Expr::Array(arr)), + ws!(tag!("]")) + ) +); named!(variant_num_lit, map!(num_lit, |s| MatchVariant::NumLit(s) @@ -253,11 +248,8 @@ named!(expr_var, map!(identifier, named!(expr_path, do_parse!( start: call!(identifier) >> - rest: many1!(do_parse!( - tag!("::") >> - part: identifier >> - (part) - )) >> + tag!("::") >> + rest: separated_nonempty_list!(tag!("::"), identifier) >> ({ let mut path = vec![start]; path.extend(rest); @@ -265,42 +257,25 @@ named!(expr_path, do_parse!( }) )); -named!(variant_path, do_parse!( - start: call!(identifier) >> - rest: many1!(do_parse!( - tag!("::") >> - part: identifier >> - (part) - )) >> - ({ - let mut path = vec![start]; - path.extend(rest); - MatchVariant::Path(path) - }) -)); +named!(variant_path, + map!( + separated_nonempty_list!(tag!("::"), identifier), + |path| MatchVariant::Path(path) + ) +); named!(target_single, map!(identifier, |s| Target::Name(s) )); -named!(target_tuple, do_parse!( - tag!("(") >> - args: opt!(do_parse!( - arg0: ws!(identifier) >> - args: many0!(do_parse!( - tag!(",") >> - argn: ws!(identifier) >> - (argn) - )) >> - ({ - let mut res = vec![arg0]; - res.extend(args); - res - }) - )) >> - opt!(ws!(tag!(","))) >> - tag!(")") >> - (Target::Tuple(args.unwrap_or_default())) +named!(target_tuple, delimited!( + tag!("("), + do_parse!( + res: separated_list!(tag!(","), ws!(identifier)) >> + opt!(ws!(tag!(","))) >> + (Target::Tuple(res)) + ), + tag!(")") )); named!(variant_name, map!(identifier, @@ -311,23 +286,10 @@ named!(param_name, map!(identifier, |s| MatchParameter::Name(s) )); -named!(arguments>, do_parse!( - tag!("(") >> - args: opt!(do_parse!( - arg0: ws!(expr_any) >> - args: many0!(do_parse!( - tag!(",") >> - argn: ws!(expr_any) >> - (argn) - )) >> - ({ - let mut res = vec![arg0]; - res.extend(args); - res - }) - )) >> - tag!(")") >> - (args.unwrap_or_default()) +named!(arguments>, delimited!( + tag!("("), + separated_list!(tag!(","), ws!(expr_any)), + tag!(")") )); named!(macro_arguments, @@ -384,23 +346,10 @@ fn nested_parenthesis(i: Input) -> Result<(Input, &str), nom::Err> { } } -named!(parameters>, do_parse!( - tag!("(") >> - vals: opt!(do_parse!( - arg0: ws!(identifier) >> - args: many0!(do_parse!( - tag!(",") >> - argn: ws!(identifier) >> - (argn) - )) >> - ({ - let mut res = vec![arg0]; - res.extend(args); - res - }) - )) >> - tag!(")") >> - (vals.unwrap_or_default()) +named!(parameters>, delimited!( + tag!("("), + separated_list!(tag!(","), ws!(identifier)), + tag!(")") )); named!(with_parameters, do_parse!( @@ -409,42 +358,16 @@ named!(with_parameters, do_parse!( (value) )); -named!(match_simple_parameters, do_parse!( - ws!(tag!("(")) >> - vals: opt!(do_parse!( - arg0: ws!(match_parameter) >> - args: many0!(do_parse!( - tag!(",") >> - argn: ws!(match_parameter) >> - (argn) - )) >> - ({ - let mut res = vec![arg0]; - res.extend(args); - res - }) - )) >> - tag!(")") >> - (MatchParameters::Simple(vals.unwrap_or_default())) -)); - -named!(match_named_parameters, do_parse!( - ws!(tag!("{")) >> - vals: opt!(do_parse!( - arg0: ws!(match_named_parameter) >> - args: many0!(do_parse!( - tag!(",") >> - argn: ws!(match_named_parameter) >> - (argn) - )) >> - ({ - let mut res = vec![arg0]; - res.extend(args); - res - }) - )) >> - tag!("}") >> - (MatchParameters::Named(vals.unwrap_or_default())) +named!(match_simple_parameters, delimited!( + ws!(tag!("(")), + map!(separated_list!(tag!(","), ws!(match_parameter)), |mps| MatchParameters::Simple(mps)), + tag!(")") +)); + +named!(match_named_parameters, delimited!( + ws!(tag!("{")), + map!(separated_list!(tag!(","), ws!(match_named_parameter)), |mps| MatchParameters::Named(mps)), + tag!("}") )); named!(expr_group, map!( -- cgit