diff options
author | Tuomas Siipola <tuomas@zpl.fi> | 2020-01-03 20:36:17 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2020-01-03 19:56:18 +0100 |
commit | cef055108de6c51c1423cd6a8919730ef01f64a3 (patch) | |
tree | 1e2d2bbac73110453388b13ba781a1101803a37b /askama_derive/src | |
parent | 2446315d38882efe43a31a3749481eac9b25615d (diff) | |
download | askama-cef055108de6c51c1423cd6a8919730ef01f64a3.tar.gz askama-cef055108de6c51c1423cd6a8919730ef01f64a3.tar.bz2 askama-cef055108de6c51c1423cd6a8919730ef01f64a3.zip |
Support char literals
Diffstat (limited to '')
-rw-r--r-- | askama_derive/src/generator.rs | 8 | ||||
-rw-r--r-- | askama_derive/src/parser.rs | 32 |
2 files changed, 38 insertions, 2 deletions
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index 81f6ac9..9f60847 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -885,6 +885,7 @@ impl<'a> Generator<'a> { Expr::BoolLit(s) => self.visit_bool_lit(buf, s), Expr::NumLit(s) => self.visit_num_lit(buf, s), Expr::StrLit(s) => self.visit_str_lit(buf, s), + Expr::CharLit(s) => self.visit_char_lit(buf, s), Expr::Var(s) => self.visit_var(buf, s), Expr::Path(ref path) => self.visit_path(buf, path), Expr::Array(ref elements) => self.visit_array(buf, elements), @@ -918,6 +919,7 @@ impl<'a> Generator<'a> { expr_buf.write("&"); self.visit_str_lit(&mut expr_buf, s) } + MatchVariant::CharLit(s) => self.visit_char_lit(&mut expr_buf, s), MatchVariant::NumLit(s) => self.visit_num_lit(&mut expr_buf, s), MatchVariant::Name(s) => { expr_buf.write(s); @@ -937,6 +939,7 @@ impl<'a> Generator<'a> { let wrapped = match *param { MatchParameter::NumLit(s) => self.visit_num_lit(&mut expr_buf, s), MatchParameter::StrLit(s) => self.visit_str_lit(&mut expr_buf, s), + MatchParameter::CharLit(s) => self.visit_char_lit(&mut expr_buf, s), MatchParameter::Name(s) => { expr_buf.write(s); DisplayWrap::Unwrapped @@ -1155,6 +1158,11 @@ impl<'a> Generator<'a> { DisplayWrap::Unwrapped } + fn visit_char_lit(&mut self, buf: &mut Buffer, s: &str) -> DisplayWrap { + buf.write(&format!("'{}'", s)); + DisplayWrap::Unwrapped + } + fn visit_num_lit(&mut self, buf: &mut Buffer, s: &str) -> DisplayWrap { buf.write(s); DisplayWrap::Unwrapped diff --git a/askama_derive/src/parser.rs b/askama_derive/src/parser.rs index e483ff7..91c2d6a 100644 --- a/askama_derive/src/parser.rs +++ b/askama_derive/src/parser.rs @@ -15,6 +15,7 @@ pub enum Expr<'a> { BoolLit(&'a str), NumLit(&'a str), StrLit(&'a str), + CharLit(&'a str), Var(&'a str), Path(Vec<&'a str>), Array(Vec<Expr<'a>>), @@ -35,6 +36,7 @@ pub enum MatchVariant<'a> { Name(&'a str), NumLit(&'a str), StrLit(&'a str), + CharLit(&'a str), } #[derive(Debug)] @@ -42,6 +44,7 @@ pub enum MatchParameter<'a> { Name(&'a str), NumLit(&'a str), StrLit(&'a str), + CharLit(&'a str), } #[derive(Debug)] @@ -266,6 +269,24 @@ fn param_str_lit(i: &[u8]) -> IResult<&[u8], MatchParameter> { })(i) } +fn expr_char_lit(i: &[u8]) -> IResult<&[u8], Expr> { + map(delimited(char('\''), take_until("'"), char('\'')), |s| { + Expr::CharLit(str::from_utf8(s).unwrap()) + })(i) +} + +fn variant_char_lit(i: &[u8]) -> IResult<&[u8], MatchVariant> { + map(delimited(char('\''), is_not("'"), char('\'')), |s| { + MatchVariant::CharLit(str::from_utf8(s).unwrap()) + })(i) +} + +fn param_char_lit(i: &[u8]) -> IResult<&[u8], MatchParameter> { + map(delimited(char('\''), is_not("'"), char('\'')), |s| { + MatchParameter::CharLit(str::from_utf8(s).unwrap()) + })(i) +} + fn expr_var(i: &[u8]) -> IResult<&[u8], Expr> { map(identifier, |s| Expr::Var(s))(i) } @@ -405,6 +426,7 @@ fn expr_single(i: &[u8]) -> IResult<&[u8], Expr> { expr_bool_lit, expr_num_lit, expr_str_lit, + expr_char_lit, expr_path, expr_rust_macro, expr_array_lit, @@ -414,11 +436,17 @@ fn expr_single(i: &[u8]) -> IResult<&[u8], Expr> { } fn match_variant(i: &[u8]) -> IResult<&[u8], MatchVariant> { - alt((variant_path, variant_name, variant_num_lit, variant_str_lit))(i) + alt(( + variant_path, + variant_name, + variant_num_lit, + variant_str_lit, + variant_char_lit, + ))(i) } fn match_parameter(i: &[u8]) -> IResult<&[u8], MatchParameter> { - alt((param_name, param_num_lit, param_str_lit))(i) + alt((param_name, param_num_lit, param_str_lit, param_char_lit))(i) } fn match_named_parameter(i: &[u8]) -> IResult<&[u8], (&str, Option<MatchParameter>)> { |