diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-10-04 21:08:43 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-10-04 21:08:43 +0200 |
commit | d497a31e059292f7b37b46c6f17b489e9f3623b0 (patch) | |
tree | 11adee043a386dd3e7436f2c5d5b0a4f32815b7f /askama_shared | |
parent | 6c7da56ddddac1b549445befff6023c1b481db85 (diff) | |
download | askama-d497a31e059292f7b37b46c6f17b489e9f3623b0.tar.gz askama-d497a31e059292f7b37b46c6f17b489e9f3623b0.tar.bz2 askama-d497a31e059292f7b37b46c6f17b489e9f3623b0.zip |
Handle path expressions (fixes #56)
Diffstat (limited to 'askama_shared')
-rw-r--r-- | askama_shared/src/generator.rs | 11 | ||||
-rw-r--r-- | askama_shared/src/parser.rs | 18 |
2 files changed, 29 insertions, 0 deletions
diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs index ab861dc..4588677 100644 --- a/askama_shared/src/generator.rs +++ b/askama_shared/src/generator.rs @@ -561,6 +561,7 @@ impl<'a> Generator<'a> { Expr::NumLit(s) => self.visit_num_lit(s), Expr::StrLit(s) => self.visit_str_lit(s), Expr::Var(s) => self.visit_var(s), + Expr::Path(ref path) => self.visit_path(path), Expr::Attr(ref obj, name) => self.visit_attr(obj, name), Expr::Filter(name, ref args) => self.visit_filter(name, args), Expr::BinOp(op, ref left, ref right) => @@ -671,6 +672,16 @@ impl<'a> Generator<'a> { DisplayWrap::Unwrapped } + fn visit_path(&mut self, path: &Vec<&str>) -> DisplayWrap { + for (i, part) in path.iter().enumerate() { + if i > 0 { + self.write("::"); + } + self.write(part); + } + DisplayWrap::Unwrapped + } + fn visit_var(&mut self, s: &str) -> DisplayWrap { if self.locals.contains(s) { self.write(s); diff --git a/askama_shared/src/parser.rs b/askama_shared/src/parser.rs index 083e96b..495b68f 100644 --- a/askama_shared/src/parser.rs +++ b/askama_shared/src/parser.rs @@ -6,6 +6,7 @@ pub enum Expr<'a> { NumLit(&'a str), StrLit(&'a str), Var(&'a str), + Path(Vec<&'a str>), Attr(Box<Expr<'a>>, &'a str), Filter(&'a str, Vec<Expr<'a>>), BinOp(&'a str, Box<Expr<'a>>, Box<Expr<'a>>), @@ -136,6 +137,22 @@ named!(expr_var<Expr>, map!(identifier, |s| Expr::Var(s)) ); +named!(expr_path<Expr>, do_parse!( + start: call!(identifier) >> + rest: many1!(do_parse!( + tag_s!("::") >> + part: identifier >> + (part) + )) >> + ({ + let mut path = vec![ start ]; + for part in rest.iter() { + path.push(part); + } + Expr::Path(path) + }) +)); + named!(target_single<Target>, map!(identifier, |s| Target::Name(s) )); @@ -186,6 +203,7 @@ named!(expr_group<Expr>, map!( named!(expr_single<Expr>, alt!( expr_num_lit | expr_str_lit | + expr_path | expr_var | expr_group )); |