aboutsummaryrefslogtreecommitdiffstats
path: root/askama_parser/src
diff options
context:
space:
mode:
authorLibravatar René Kijewski <rene.kijewski@fu-berlin.de>2023-08-01 03:22:08 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2023-08-01 13:04:41 +0200
commit4f52dbe8be9746d1f1a2eab03e135c9003ef6dd3 (patch)
tree8e3469529aff9e0538c24e854c56f1f6e643368e /askama_parser/src
parentcd075630eab578a66afec6000ce0f266e6b281e8 (diff)
downloadaskama-4f52dbe8be9746d1f1a2eab03e135c9003ef6dd3.tar.gz
askama-4f52dbe8be9746d1f1a2eab03e135c9003ef6dd3.tar.bz2
askama-4f52dbe8be9746d1f1a2eab03e135c9003ef6dd3.zip
parser: add type for `Node::Call`
Diffstat (limited to 'askama_parser/src')
-rw-r--r--askama_parser/src/lib.rs4
-rw-r--r--askama_parser/src/node.rs56
2 files changed, 40 insertions, 20 deletions
diff --git a/askama_parser/src/lib.rs b/askama_parser/src/lib.rs
index 0f796ae..c92adbe 100644
--- a/askama_parser/src/lib.rs
+++ b/askama_parser/src/lib.rs
@@ -15,7 +15,9 @@ use nom::sequence::{delimited, pair, tuple};
use nom::{error_position, AsChar, IResult, InputTakeAtPosition};
pub use self::expr::Expr;
-pub use self::node::{Cond, CondTest, Import, Loop, Macro, Node, Target, When, Whitespace, Ws};
+pub use self::node::{
+ Call, Cond, CondTest, Import, Loop, Macro, Node, Target, When, Whitespace, Ws,
+};
mod expr;
mod node;
diff --git a/askama_parser/src/node.rs b/askama_parser/src/node.rs
index 972314e..9cdd451 100644
--- a/askama_parser/src/node.rs
+++ b/askama_parser/src/node.rs
@@ -19,7 +19,7 @@ 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>>),
+ Call(Call<'a>),
LetDecl(Ws, Target<'a>),
Let(Ws, Target<'a>, Expr<'a>),
Cond(Vec<Cond<'a>>, Ws),
@@ -69,7 +69,7 @@ impl<'a> Node<'a> {
let mut p = tuple((
|i| s.tag_block_start(i),
alt((
- Self::call,
+ map(Call::parse, Self::Call),
Self::r#let,
|i| Self::r#if(i, s),
|i| Self::r#for(i, s),
@@ -89,23 +89,6 @@ impl<'a> Node<'a> {
Ok((i, contents))
}
- fn call(i: &'a str) -> IResult<&'a str, Self> {
- let mut p = tuple((
- opt(Whitespace::parse),
- ws(keyword("call")),
- cut(tuple((
- opt(tuple((ws(identifier), ws(tag("::"))))),
- ws(identifier),
- opt(ws(Expr::arguments)),
- opt(Whitespace::parse),
- ))),
- ));
- let (i, (pws, _, (scope, name, args, nws))) = p(i)?;
- let scope = scope.map(|(scope, _)| scope);
- let args = args.unwrap_or_default();
- Ok((i, Self::Call(Ws(pws, nws), scope, name, args)))
- }
-
fn r#let(i: &'a str) -> IResult<&'a str, Self> {
let mut p = tuple((
opt(Whitespace::parse),
@@ -744,6 +727,41 @@ impl<'a> Import<'a> {
}
}
+#[derive(Debug, PartialEq)]
+pub struct Call<'a> {
+ pub ws: Ws,
+ pub scope: Option<&'a str>,
+ pub name: &'a str,
+ pub args: Vec<Expr<'a>>,
+}
+
+impl<'a> Call<'a> {
+ fn parse(i: &'a str) -> IResult<&'a str, Self> {
+ let mut p = tuple((
+ opt(Whitespace::parse),
+ ws(keyword("call")),
+ cut(tuple((
+ opt(tuple((ws(identifier), ws(tag("::"))))),
+ ws(identifier),
+ opt(ws(Expr::arguments)),
+ opt(Whitespace::parse),
+ ))),
+ ));
+ let (i, (pws, _, (scope, name, args, nws))) = p(i)?;
+ let scope = scope.map(|(scope, _)| scope);
+ let args = args.unwrap_or_default();
+ Ok((
+ i,
+ Self {
+ ws: Ws(pws, nws),
+ scope,
+ name,
+ args,
+ },
+ ))
+ }
+}
+
/// 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".