From 35cf03cf794b515afdb24a989e07b6acd0e2fc19 Mon Sep 17 00:00:00 2001 From: René Kijewski Date: Wed, 25 Aug 2021 02:00:41 +0200 Subject: Parse boolean literals in assignment targets 268d825 introduced a regression that made matching against boolean literals impossible. E.g. "true" was interpreted as the variable "r#true". This PR fixes the problem. The bug was reported by @Restioson in issue #531. --- askama_shared/src/parser.rs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'askama_shared/src/parser.rs') diff --git a/askama_shared/src/parser.rs b/askama_shared/src/parser.rs index 22f9a1e..66f2ec1 100644 --- a/askama_shared/src/parser.rs +++ b/askama_shared/src/parser.rs @@ -109,6 +109,7 @@ pub enum Target<'a> { NumLit(&'a str), StrLit(&'a str), CharLit(&'a str), + BoolLit(&'a str), Path(Vec<&'a str>), } @@ -241,12 +242,20 @@ fn non_ascii(chr: u8) -> bool { (0x80..=0xFD).contains(&chr) } -fn expr_bool_lit(i: &[u8]) -> IResult<&[u8], Expr<'_>> { +fn bool_lit(i: &[u8]) -> IResult<&[u8], &str> { map(alt((tag("false"), tag("true"))), |s| { - Expr::BoolLit(str::from_utf8(s).unwrap()) + str::from_utf8(s).unwrap() })(i) } +fn expr_bool_lit(i: &[u8]) -> IResult<&[u8], Expr<'_>> { + map(bool_lit, Expr::BoolLit)(i) +} + +fn variant_bool_lit(i: &[u8]) -> IResult<&[u8], Target<'_>> { + map(bool_lit, Target::BoolLit)(i) +} + fn num_lit(i: &[u8]) -> IResult<&[u8], &str> { map(recognize(pair(digit1, opt(pair(tag("."), digit1)))), |s| { str::from_utf8(s).unwrap() @@ -362,12 +371,21 @@ fn named_target(i: &[u8]) -> IResult<&[u8], (&str, Target<'_>)> { Ok((i, (src, target.unwrap_or(Target::Name(src))))) } +fn variant_lit(i: &[u8]) -> IResult<&[u8], Target<'_>> { + alt(( + variant_str_lit, + variant_char_lit, + variant_num_lit, + variant_bool_lit, + ))(i) +} + fn target(i: &[u8]) -> IResult<&[u8], Target<'_>> { let mut opt_opening_paren = map(opt(ws(tag("("))), |o| o.is_some()); let mut opt_closing_paren = map(opt(ws(tag(")"))), |o| o.is_some()); let mut opt_opening_brace = map(opt(ws(tag("{"))), |o| o.is_some()); - let (i, lit) = opt(alt((variant_str_lit, variant_char_lit, variant_num_lit)))(i)?; + let (i, lit) = opt(variant_lit)(i)?; if let Some(lit) = lit { return Ok((i, lit)); } -- cgit