aboutsummaryrefslogtreecommitdiffstats
path: root/askama_shared/src
diff options
context:
space:
mode:
authorLibravatar René Kijewski <kijewski@library.vetmed.fu-berlin.de>2021-08-25 02:00:41 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2021-08-25 19:03:19 +0200
commit35cf03cf794b515afdb24a989e07b6acd0e2fc19 (patch)
tree9d8c4e22e99390b4939f0499394c5dd54ac113f0 /askama_shared/src
parent81429636eb295ec03ef35cf77648d1626970fd4e (diff)
downloadaskama-35cf03cf794b515afdb24a989e07b6acd0e2fc19.tar.gz
askama-35cf03cf794b515afdb24a989e07b6acd0e2fc19.tar.bz2
askama-35cf03cf794b515afdb24a989e07b6acd0e2fc19.zip
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.
Diffstat (limited to '')
-rw-r--r--askama_shared/src/generator.rs6
-rw-r--r--askama_shared/src/parser.rs24
2 files changed, 27 insertions, 3 deletions
diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs
index dccda93..ec468e5 100644
--- a/askama_shared/src/generator.rs
+++ b/askama_shared/src/generator.rs
@@ -1484,6 +1484,12 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
}
self.visit_char_lit(buf, s);
}
+ Target::BoolLit(s) => {
+ if first_level {
+ buf.write("&");
+ }
+ buf.write(s);
+ }
}
}
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));
}