From 7a6e79187ee1a86d0c71f160ac00fa9f5a290134 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 17 Feb 2017 17:15:45 +0100 Subject: Add support for integer literals --- askama/src/generator.rs | 5 +++++ askama/src/parser.rs | 6 ++++++ testing/templates/operators.html | 2 +- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/askama/src/generator.rs b/askama/src/generator.rs index e768219..a8d62af 100644 --- a/askama/src/generator.rs +++ b/askama/src/generator.rs @@ -103,6 +103,10 @@ impl<'a> Generator<'a> { self.prepare_ws(ws); } + fn visit_num_lit(&mut self, s: &str) { + self.write(s); + } + fn visit_str_lit(&mut self, s: &str) { self.write(&format!("\"{}\"", s)); } @@ -129,6 +133,7 @@ impl<'a> Generator<'a> { fn visit_expr(&mut self, expr: &Expr) { match *expr { + Expr::NumLit(s) => self.visit_num_lit(s), Expr::StrLit(s) => self.visit_str_lit(s), Expr::Var(s) => self.visit_var(s), Expr::Filter(name, ref val) => self.visit_filter(name, val), diff --git a/askama/src/parser.rs b/askama/src/parser.rs index 6d0e58f..1d34611 100644 --- a/askama/src/parser.rs +++ b/askama/src/parser.rs @@ -3,6 +3,7 @@ use std::str; #[derive(Debug)] pub enum Expr<'a> { + NumLit(&'a str), StrLit(&'a str), Var(&'a str), Filter(&'a str, Box>), @@ -72,6 +73,10 @@ fn take_content(i: &[u8]) -> IResult<&[u8], Node> { IResult::Done(&i[..0], split_ws_parts(&i[..])) } +named!(expr_num_lit, map!(nom::digit, + |s| Expr::NumLit(str::from_utf8(s).unwrap()) +)); + named!(expr_str_lit, map!( delimited!(char!('"'), is_not!("\""), char!('"')), |s| Expr::StrLit(str::from_utf8(s).unwrap()) @@ -86,6 +91,7 @@ named!(target_single, map!(alphanumeric, )); named!(expr_single, alt!( + expr_num_lit | expr_str_lit | expr_var )); diff --git a/testing/templates/operators.html b/testing/templates/operators.html index 1488b9a..4b093c5 100644 --- a/testing/templates/operators.html +++ b/testing/templates/operators.html @@ -28,6 +28,6 @@ {% if a == b && a + b == c -%} and {%- endif -%} -{% if a == c || a == b -%} +{% if a == c || a == 1 -%} or {%- endif -%} -- cgit