aboutsummaryrefslogtreecommitdiffstats
path: root/askama_parser
diff options
context:
space:
mode:
authorLibravatar René Kijewski <rene.kijewski@fu-berlin.de>2023-08-01 03:53:15 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2023-08-01 13:04:41 +0200
commitfa3a3271ee8d2514cf96e6a7733312a958a78962 (patch)
tree7d51d73aea82d218f1807d72dc93a8448d478626 /askama_parser
parent6323b5e17198f6d91cb1d880823168a43b264ebf (diff)
downloadaskama-fa3a3271ee8d2514cf96e6a7733312a958a78962.tar.gz
askama-fa3a3271ee8d2514cf96e6a7733312a958a78962.tar.bz2
askama-fa3a3271ee8d2514cf96e6a7733312a958a78962.zip
parser: add type for `Node::Let`
Diffstat (limited to 'askama_parser')
-rw-r--r--askama_parser/src/lib.rs2
-rw-r--r--askama_parser/src/node.rs58
2 files changed, 34 insertions, 26 deletions
diff --git a/askama_parser/src/lib.rs b/askama_parser/src/lib.rs
index c0650ee..560507a 100644
--- a/askama_parser/src/lib.rs
+++ b/askama_parser/src/lib.rs
@@ -16,7 +16,7 @@ use nom::{error_position, AsChar, IResult, InputTakeAtPosition};
pub use self::expr::Expr;
pub use self::node::{
- BlockDef, Call, Cond, CondTest, Import, Lit, Loop, Macro, Match, Node, Raw, Target, When,
+ BlockDef, Call, Cond, CondTest, Import, Let, Lit, Loop, Macro, Match, Node, Raw, Target, When,
Whitespace, Ws,
};
diff --git a/askama_parser/src/node.rs b/askama_parser/src/node.rs
index ebab746..9055d99 100644
--- a/askama_parser/src/node.rs
+++ b/askama_parser/src/node.rs
@@ -20,8 +20,7 @@ pub enum Node<'a> {
Comment(Ws),
Expr(Ws, Expr<'a>),
Call(Call<'a>),
- LetDecl(Ws, Target<'a>),
- Let(Ws, Target<'a>, Expr<'a>),
+ Let(Let<'a>),
Cond(Vec<Cond<'a>>, Ws),
Match(Match<'a>),
Loop(Loop<'a>),
@@ -50,7 +49,7 @@ impl<'a> Node<'a> {
|i| s.tag_block_start(i),
alt((
map(Call::parse, Self::Call),
- Self::r#let,
+ map(Let::parse, Self::Let),
|i| Self::r#if(i, s),
|i| Self::r#for(i, s),
map(|i| Match::parse(i, s), Self::Match),
@@ -69,28 +68,6 @@ impl<'a> Node<'a> {
Ok((i, contents))
}
- fn r#let(i: &'a str) -> IResult<&'a str, Self> {
- let mut p = tuple((
- opt(Whitespace::parse),
- ws(alt((keyword("let"), keyword("set")))),
- cut(tuple((
- ws(Target::parse),
- opt(tuple((ws(char('=')), ws(Expr::parse)))),
- opt(Whitespace::parse),
- ))),
- ));
- let (i, (pws, _, (var, val, nws))) = p(i)?;
-
- Ok((
- i,
- if let Some((_, val)) = val {
- Self::Let(Ws(pws, nws), var, val)
- } else {
- Self::LetDecl(Ws(pws, nws), var)
- },
- ))
- }
-
fn r#if(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> {
let mut p = tuple((
opt(Whitespace::parse),
@@ -821,6 +798,37 @@ impl<'a> Raw<'a> {
}
}
+#[derive(Debug, PartialEq)]
+pub struct Let<'a> {
+ pub ws: Ws,
+ pub var: Target<'a>,
+ pub val: Option<Expr<'a>>,
+}
+
+impl<'a> Let<'a> {
+ fn parse(i: &'a str) -> IResult<&'a str, Self> {
+ let mut p = tuple((
+ opt(Whitespace::parse),
+ ws(alt((keyword("let"), keyword("set")))),
+ cut(tuple((
+ ws(Target::parse),
+ opt(preceded(ws(char('=')), ws(Expr::parse))),
+ opt(Whitespace::parse),
+ ))),
+ ));
+ let (i, (pws, _, (var, val, nws))) = p(i)?;
+
+ Ok((
+ i,
+ Let {
+ ws: Ws(pws, nws),
+ var,
+ val,
+ },
+ ))
+ }
+}
+
/// First field is "minus/plus sign was used on the left part of the item".
///
/// Second field is "minus/plus sign was used on the right part of the item".