aboutsummaryrefslogtreecommitdiffstats
path: root/askama_shared/src/parser.rs
diff options
context:
space:
mode:
Diffstat (limited to 'askama_shared/src/parser.rs')
-rw-r--r--askama_shared/src/parser.rs132
1 files changed, 66 insertions, 66 deletions
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))],
);
}
}