diff options
-rw-r--r-- | askama_shared/.DS_Store | bin | 0 -> 6148 bytes | |||
-rw-r--r-- | askama_shared/src/generator.rs | 36 | ||||
-rw-r--r-- | askama_shared/src/parser.rs | 132 |
3 files changed, 84 insertions, 84 deletions
diff --git a/askama_shared/.DS_Store b/askama_shared/.DS_Store Binary files differnew file mode 100644 index 0000000..0b49530 --- /dev/null +++ b/askama_shared/.DS_Store diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs index 866fd7b..32968de 100644 --- a/askama_shared/src/generator.rs +++ b/askama_shared/src/generator.rs @@ -3,7 +3,7 @@ use crate::filters; use crate::heritage::{Context, Heritage}; use crate::input::{Source, TemplateInput}; use crate::parser::{ - parse, Cond, Expr, MatchParameter, MatchParameters, MatchVariant, Node, Target, When, WS, + parse, Cond, Expr, MatchParameter, MatchParameters, MatchVariant, Node, Target, When, Ws, }; use proc_macro2::Span; @@ -151,7 +151,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { self.handle(ctx, &ctx.nodes, buf, AstLevel::Top) }?; - self.flush_ws(WS(false, false)); + self.flush_ws(Ws(false, false)); buf.writeln("Ok(())")?; buf.writeln("}")?; @@ -451,7 +451,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { self.write_loop(ctx, buf, ws1, var, iter, body, ws2)?; } Node::BlockDef(ws1, name, _, ws2) => { - self.write_block(buf, Some(name), WS(ws1.0, ws2.1))?; + self.write_block(buf, Some(name), Ws(ws1.0, ws2.1))?; } Node::Include(ws, path) => { size_hint += self.handle_include(ctx, buf, ws, path)?; @@ -498,7 +498,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { ctx: &'a Context, buf: &mut Buffer, conds: &'a [Cond], - ws: WS, + ws: Ws, ) -> Result<usize, CompileError> { let mut flushed = 0; let mut arm_sizes = Vec::new(); @@ -559,10 +559,10 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { &mut self, ctx: &'a Context, buf: &mut Buffer, - ws1: WS, + ws1: Ws, expr: &Expr, arms: &'a [When], - ws2: WS, + ws2: Ws, ) -> Result<usize, CompileError> { self.flush_ws(ws1); let flushed = self.write_buf_writable(buf)?; @@ -648,11 +648,11 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { &mut self, ctx: &'a Context, buf: &mut Buffer, - ws1: WS, + ws1: Ws, var: &'a Target, iter: &Expr, body: &'a [Node], - ws2: WS, + ws2: Ws, ) -> Result<usize, CompileError> { self.handle_ws(ws1); self.locals.push(); @@ -704,7 +704,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { &mut self, ctx: &'a Context, buf: &mut Buffer, - ws: WS, + ws: Ws, scope: Option<&str>, name: &str, args: &[Expr], @@ -801,7 +801,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { &mut self, ctx: &'a Context, buf: &mut Buffer, - ws: WS, + ws: Ws, path: &str, ) -> Result<usize, CompileError> { self.flush_ws(ws); @@ -839,7 +839,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { fn write_let_decl( &mut self, buf: &mut Buffer, - ws: WS, + ws: Ws, var: &'a Target, ) -> Result<(), CompileError> { self.handle_ws(ws); @@ -866,7 +866,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { fn write_let( &mut self, buf: &mut Buffer, - ws: WS, + ws: Ws, var: &'a Target, val: &Expr, ) -> Result<(), CompileError> { @@ -920,7 +920,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { &mut self, buf: &mut Buffer, name: Option<&'a str>, - outer: WS, + outer: Ws, ) -> Result<usize, CompileError> { // Flush preceding whitespace according to the outer WS spec self.flush_ws(outer); @@ -981,7 +981,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { Ok(size_hint) } - fn write_expr(&mut self, ws: WS, s: &'a Expr<'a>) { + fn write_expr(&mut self, ws: Ws, s: &'a Expr<'a>) { self.handle_ws(ws); self.buf_writable.push(Writable::Expr(s)); } @@ -1084,7 +1084,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { } } - fn write_comment(&mut self, ws: WS) { + fn write_comment(&mut self, ws: Ws) { self.handle_ws(ws); } @@ -1530,7 +1530,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { // Combines `flush_ws()` and `prepare_ws()` to handle both trailing whitespace from the // preceding literal and leading whitespace from the succeeding literal. - fn handle_ws(&mut self, ws: WS) { + fn handle_ws(&mut self, ws: Ws) { self.flush_ws(ws); self.prepare_ws(ws); } @@ -1538,7 +1538,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { // If the previous literal left some trailing whitespace in `next_ws` and the // prefix whitespace suppressor from the given argument, flush that whitespace. // In either case, `next_ws` is reset to `None` (no trailing whitespace). - fn flush_ws(&mut self, ws: WS) { + fn flush_ws(&mut self, ws: Ws) { if self.next_ws.is_some() && !ws.0 { let val = self.next_ws.unwrap(); if !val.is_empty() { @@ -1551,7 +1551,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> { // Sets `skip_ws` to match the suffix whitespace suppressor from the given // argument, to determine whether to suppress leading whitespace from the // next literal. - fn prepare_ws(&mut self, ws: WS) { + fn prepare_ws(&mut self, ws: Ws) { self.skip_ws = ws.1; } } diff --git a/askama_shared/src/parser.rs b/askama_shared/src/parser.rs index 9aec56e..80f766a 100644 --- a/askama_shared/src/parser.rs +++ b/askama_shared/src/parser.rs @@ -13,20 +13,20 @@ use crate::{CompileError, Syntax}; #[derive(Debug, PartialEq)] pub enum Node<'a> { Lit(&'a str, &'a str, &'a str), - Comment(WS), - Expr(WS, Expr<'a>), - Call(WS, Option<&'a str>, &'a str, Vec<Expr<'a>>), - LetDecl(WS, Target<'a>), - Let(WS, Target<'a>, Expr<'a>), - Cond(Vec<(WS, Option<Expr<'a>>, Vec<Node<'a>>)>, WS), - Match(WS, Expr<'a>, Vec<When<'a>>, WS), - Loop(WS, Target<'a>, Expr<'a>, Vec<Node<'a>>, WS), + Comment(Ws), + Expr(Ws, Expr<'a>), + Call(Ws, Option<&'a str>, &'a str, Vec<Expr<'a>>), + LetDecl(Ws, Target<'a>), + Let(Ws, Target<'a>, Expr<'a>), + Cond(Vec<(Ws, Option<Expr<'a>>, Vec<Node<'a>>)>, Ws), + Match(Ws, Expr<'a>, Vec<When<'a>>, Ws), + Loop(Ws, Target<'a>, Expr<'a>, Vec<Node<'a>>, Ws), Extends(Expr<'a>), - BlockDef(WS, &'a str, Vec<Node<'a>>, WS), - Include(WS, &'a str), - Import(WS, &'a str, &'a str), + BlockDef(Ws, &'a str, Vec<Node<'a>>, Ws), + Include(Ws, &'a str), + Import(Ws, &'a str, &'a str), Macro(&'a str, Macro<'a>), - Raw(WS, &'a str, WS), + Raw(Ws, &'a str, Ws), } #[derive(Debug, PartialEq)] @@ -91,7 +91,7 @@ impl Expr<'_> { } pub type When<'a> = ( - WS, + Ws, Option<MatchVariant<'a>>, MatchParameters<'a>, Vec<Node<'a>>, @@ -128,10 +128,10 @@ pub enum MatchVariant<'a> { #[derive(Debug, PartialEq)] pub struct Macro<'a> { - pub ws1: WS, + pub ws1: Ws, pub args: Vec<&'a str>, pub nodes: Vec<Node<'a>>, - pub ws2: WS, + pub ws2: Ws, } #[derive(Debug, PartialEq)] @@ -141,9 +141,9 @@ pub enum Target<'a> { } #[derive(Clone, Copy, Debug, PartialEq)] -pub struct WS(pub bool, pub bool); +pub struct Ws(pub bool, pub bool); -pub type Cond<'a> = (WS, Option<Expr<'a>>, Vec<Node<'a>>); +pub type Cond<'a> = (Ws, Option<Expr<'a>>, Vec<Node<'a>>); fn ws<F, I, O, E>(mut inner: F) -> impl FnMut(I) -> IResult<I, O, E> where @@ -685,7 +685,7 @@ fn expr_node<'a>(i: &'a [u8], s: &'a Syntax<'a>) -> IResult<&'a [u8], Node<'a>> |i| tag_expr_end(i, s), )); let (i, (_, pws, expr, nws, _)) = p(i)?; - Ok((i, Node::Expr(WS(pws.is_some(), nws.is_some()), expr))) + Ok((i, Node::Expr(Ws(pws.is_some(), nws.is_some()), expr))) } fn block_call(i: &[u8]) -> IResult<&[u8], Node> { @@ -701,7 +701,7 @@ fn block_call(i: &[u8]) -> IResult<&[u8], Node> { let scope = scope.map(|(scope, _)| scope); Ok(( i, - Node::Call(WS(pws.is_some(), nws.is_some()), scope, name, args), + Node::Call(Ws(pws.is_some(), nws.is_some()), scope, name, args), )) } @@ -721,7 +721,7 @@ fn cond_block<'a>(i: &'a [u8], s: &'a Syntax<'a>) -> IResult<&'a [u8], Cond<'a>> |i| parse_template(i, s), )); let (i, (_, pws, _, cond, nws, _, block)) = p(i)?; - Ok((i, (WS(pws.is_some(), nws.is_some()), cond, block))) + Ok((i, (Ws(pws.is_some(), nws.is_some()), cond, block))) } fn block_if<'a>(i: &'a [u8], s: &'a Syntax<'a>) -> IResult<&'a [u8], Node<'a>> { @@ -739,9 +739,9 @@ fn block_if<'a>(i: &'a [u8], s: &'a Syntax<'a>) -> IResult<&'a [u8], Node<'a>> { )); let (i, (pws1, cond, nws1, _, block, elifs, _, pws2, _, nws2)) = p(i)?; - let mut res = vec![(WS(pws1.is_some(), nws1.is_some()), Some(cond), block)]; + let mut res = vec![(Ws(pws1.is_some(), nws1.is_some()), Some(cond), block)]; res.extend(elifs); - Ok((i, Node::Cond(res, WS(pws2.is_some(), nws2.is_some())))) + Ok((i, Node::Cond(res, Ws(pws2.is_some(), nws2.is_some())))) } fn match_else_block<'a>(i: &'a [u8], s: &'a Syntax<'a>) -> IResult<&'a [u8], When<'a>> { @@ -757,7 +757,7 @@ fn match_else_block<'a>(i: &'a [u8], s: &'a Syntax<'a>) -> IResult<&'a [u8], Whe Ok(( i, ( - WS(pws.is_some(), nws.is_some()), + Ws(pws.is_some(), nws.is_some()), None, MatchParameters::Simple(vec![]), block, @@ -780,7 +780,7 @@ fn when_block<'a>(i: &'a [u8], s: &'a Syntax<'a>) -> IResult<&'a [u8], When<'a>> Ok(( i, ( - WS(pws.is_some(), nws.is_some()), + Ws(pws.is_some(), nws.is_some()), Some(variant), params.unwrap_or_default(), block, @@ -830,10 +830,10 @@ fn block_match<'a>(i: &'a [u8], s: &'a Syntax<'a>) -> IResult<&'a [u8], Node<'a> Ok(( i, Node::Match( - WS(pws1.is_some(), nws1.is_some()), + Ws(pws1.is_some(), nws1.is_some()), expr, arms, - WS(pws2.is_some(), nws2.is_some()), + Ws(pws2.is_some(), nws2.is_some()), ), )) } @@ -851,9 +851,9 @@ fn block_let(i: &[u8]) -> IResult<&[u8], Node> { Ok(( i, if let Some((_, val)) = val { - Node::Let(WS(pws.is_some(), nws.is_some()), var, val) + Node::Let(Ws(pws.is_some(), nws.is_some()), var, val) } else { - Node::LetDecl(WS(pws.is_some(), nws.is_some()), var) + Node::LetDecl(Ws(pws.is_some(), nws.is_some()), var) }, )) } @@ -877,11 +877,11 @@ fn block_for<'a>(i: &'a [u8], s: &'a Syntax<'a>) -> IResult<&'a [u8], Node<'a>> Ok(( i, Node::Loop( - WS(pws1.is_some(), nws1.is_some()), + Ws(pws1.is_some(), nws1.is_some()), var, iter, block, - WS(pws2.is_some(), nws2.is_some()), + Ws(pws2.is_some(), nws2.is_some()), ), )) } @@ -914,10 +914,10 @@ fn block_block<'a>(i: &'a [u8], s: &'a Syntax<'a>) -> IResult<&'a [u8], Node<'a> Ok(( i, Node::BlockDef( - WS(pws1.is_some(), nws1.is_some()), + Ws(pws1.is_some(), nws1.is_some()), name, contents, - WS(pws2.is_some(), nws2.is_some()), + Ws(pws2.is_some(), nws2.is_some()), ), )) } @@ -933,7 +933,7 @@ fn block_include(i: &[u8]) -> IResult<&[u8], Node> { Ok(( i, Node::Include( - WS(pws.is_some(), nws.is_some()), + Ws(pws.is_some(), nws.is_some()), match name { Expr::StrLit(s) => s, _ => panic!("include path must be a string literal"), @@ -955,7 +955,7 @@ fn block_import(i: &[u8]) -> IResult<&[u8], Node> { Ok(( i, Node::Import( - WS(pws.is_some(), nws.is_some()), + Ws(pws.is_some(), nws.is_some()), match name { Expr::StrLit(s) => s, _ => panic!("import path must be a string literal"), @@ -990,10 +990,10 @@ fn block_macro<'a>(i: &'a [u8], s: &'a Syntax<'a>) -> IResult<&'a [u8], Node<'a> Node::Macro( name, Macro { - ws1: WS(pws1.is_some(), nws1.is_some()), + ws1: Ws(pws1.is_some(), nws1.is_some()), args: params, nodes: contents, - ws2: WS(pws2.is_some(), nws2.is_some()), + ws2: Ws(pws2.is_some(), nws2.is_some()), }, ), )) @@ -1017,9 +1017,9 @@ fn block_raw<'a>(i: &'a [u8], s: &'a Syntax<'a>) -> IResult<&'a [u8], Node<'a>> Ok(( i, Node::Raw( - WS(pws1.is_some(), nws1.is_some()), + Ws(pws1.is_some(), nws1.is_some()), str_contents, - WS(pws2.is_some(), nws2.is_some()), + Ws(pws2.is_some(), nws2.is_some()), ), )) } @@ -1072,7 +1072,7 @@ fn block_comment<'a>(i: &'a [u8], s: &'a Syntax<'a>) -> IResult<&'a [u8], Node<' |i| tag_comment_end(i, s), )); let (i, (_, pws, tail, _)) = p(i)?; - Ok((i, Node::Comment(WS(pws.is_some(), tail.ends_with(b"-"))))) + Ok((i, Node::Comment(Ws(pws.is_some(), tail.ends_with(b"-"))))) } fn parse_template<'a>(i: &'a [u8], s: &'a Syntax<'a>) -> IResult<&'a [u8], Vec<Node<'a>>> { @@ -1125,7 +1125,7 @@ pub fn parse<'a>(src: &'a str, syntax: &'a Syntax<'a>) -> Result<Vec<Node<'a>>, #[cfg(test)] mod tests { - use super::{Expr, Node, WS}; + use super::{Expr, Node, Ws}; use crate::Syntax; fn check_ws_split(s: &str, res: &(&str, &str, &str)) { @@ -1164,28 +1164,28 @@ mod tests { assert_eq!( super::parse("{{ strvar|e }}", &syntax).unwrap(), vec![Node::Expr( - WS(false, false), + Ws(false, false), Filter("e", vec![Var("strvar")]), )], ); assert_eq!( super::parse("{{ 2|abs }}", &syntax).unwrap(), vec![Node::Expr( - WS(false, false), + Ws(false, false), Filter("abs", vec![NumLit("2")]), )], ); assert_eq!( super::parse("{{ -2|abs }}", &syntax).unwrap(), vec![Node::Expr( - WS(false, false), + Ws(false, false), Filter("abs", vec![Unary("-", NumLit("2").into())]), )], ); assert_eq!( super::parse("{{ (1 - 2)|abs }}", &syntax).unwrap(), vec![Node::Expr( - WS(false, false), + Ws(false, false), Filter( "abs", vec![Group( @@ -1201,11 +1201,11 @@ mod tests { let syntax = Syntax::default(); assert_eq!( super::parse("{{ 2 }}", &syntax).unwrap(), - vec![Node::Expr(WS(false, false), Expr::NumLit("2"),)], + vec![Node::Expr(Ws(false, false), Expr::NumLit("2"),)], ); assert_eq!( super::parse("{{ 2.5 }}", &syntax).unwrap(), - vec![Node::Expr(WS(false, false), Expr::NumLit("2.5"),)], + vec![Node::Expr(Ws(false, false), Expr::NumLit("2.5"),)], ); } @@ -1214,7 +1214,7 @@ mod tests { assert_eq!( super::parse("{{ function(\"123\", 3) }}", &Syntax::default()).unwrap(), vec![Node::Expr( - WS(false, false), + Ws(false, false), Expr::VarCall("function", vec![Expr::StrLit("123"), Expr::NumLit("3")]), )], ); @@ -1225,7 +1225,7 @@ mod tests { assert_eq!( super::parse("{{ self::function(\"123\", 3) }}", &Syntax::default()).unwrap(), vec![Node::Expr( - WS(false, false), + Ws(false, false), Expr::PathCall( vec!["self", "function"], vec![Expr::StrLit("123"), Expr::NumLit("3")], @@ -1240,14 +1240,14 @@ mod tests { assert_eq!( super::parse("{{ std::string::String::new() }}", &syntax).unwrap(), vec![Node::Expr( - WS(false, false), + Ws(false, false), Expr::PathCall(vec!["std", "string", "String", "new"], vec![]), )], ); assert_eq!( super::parse("{{ ::std::string::String::new() }}", &syntax).unwrap(), vec![Node::Expr( - WS(false, false), + Ws(false, false), Expr::PathCall(vec!["", "std", "string", "String", "new"], vec![]), )], ); @@ -1271,7 +1271,7 @@ mod tests { assert_eq!( super::parse("{{ a + b == c }}", &syntax).unwrap(), vec![Node::Expr( - WS(false, false), + Ws(false, false), BinOp( "==", BinOp("+", Var("a").into(), Var("b").into()).into(), @@ -1282,7 +1282,7 @@ mod tests { assert_eq!( super::parse("{{ a + b * c - d / e }}", &syntax).unwrap(), vec![Node::Expr( - WS(false, false), + Ws(false, false), BinOp( "-", BinOp( @@ -1298,7 +1298,7 @@ mod tests { assert_eq!( super::parse("{{ a * (b + c) / -d }}", &syntax).unwrap(), vec![Node::Expr( - WS(false, false), + Ws(false, false), BinOp( "/", BinOp( @@ -1314,7 +1314,7 @@ mod tests { assert_eq!( super::parse("{{ a || b && c || d && e }}", &syntax).unwrap(), vec![Node::Expr( - WS(false, false), + Ws(false, false), BinOp( "||", BinOp( @@ -1336,7 +1336,7 @@ mod tests { assert_eq!( super::parse("{{ a + b + c }}", &syntax).unwrap(), vec![Node::Expr( - WS(false, false), + Ws(false, false), BinOp( "+", BinOp("+", Var("a").into(), Var("b").into()).into(), @@ -1347,7 +1347,7 @@ mod tests { assert_eq!( super::parse("{{ a * b * c }}", &syntax).unwrap(), vec![Node::Expr( - WS(false, false), + Ws(false, false), BinOp( "*", BinOp("*", Var("a").into(), Var("b").into()).into(), @@ -1358,7 +1358,7 @@ mod tests { assert_eq!( super::parse("{{ a && b && c }}", &syntax).unwrap(), vec![Node::Expr( - WS(false, false), + Ws(false, false), BinOp( "&&", BinOp("&&", Var("a").into(), Var("b").into()).into(), @@ -1369,7 +1369,7 @@ mod tests { assert_eq!( super::parse("{{ a + b - c + d }}", &syntax).unwrap(), vec![Node::Expr( - WS(false, false), + Ws(false, false), BinOp( "+", BinOp( @@ -1385,7 +1385,7 @@ mod tests { assert_eq!( super::parse("{{ a == b != c > d > e == f }}", &syntax).unwrap(), vec![Node::Expr( - WS(false, false), + Ws(false, false), BinOp( "==", BinOp( @@ -1416,32 +1416,32 @@ mod tests { assert_eq!( super::parse("{##}", s).unwrap(), - vec![Node::Comment(WS(false, false))], + vec![Node::Comment(Ws(false, false))], ); assert_eq!( super::parse("{#- #}", s).unwrap(), - vec![Node::Comment(WS(true, false))], + vec![Node::Comment(Ws(true, false))], ); assert_eq!( super::parse("{# -#}", s).unwrap(), - vec![Node::Comment(WS(false, true))], + vec![Node::Comment(Ws(false, true))], ); assert_eq!( super::parse("{#--#}", s).unwrap(), - vec![Node::Comment(WS(true, true))], + vec![Node::Comment(Ws(true, true))], ); assert_eq!( super::parse("{#- foo\n bar -#}", s).unwrap(), - vec![Node::Comment(WS(true, true))], + vec![Node::Comment(Ws(true, true))], ); assert_eq!( super::parse("{#- foo\n {#- bar\n -#} baz -#}", s).unwrap(), - vec![Node::Comment(WS(true, true))], + vec![Node::Comment(Ws(true, true))], ); assert_eq!( super::parse("{# foo {# bar #} {# {# baz #} qux #} #}", s).unwrap(), - vec![Node::Comment(WS(false, false))], + vec![Node::Comment(Ws(false, false))], ); } } |