Skip to content

Commit

Permalink
Pass Node parsing level to expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Kijewski authored and djc committed Sep 28, 2023
1 parent 238e4bb commit eef38ce
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion askama_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,5 +352,5 @@ impl Level {
Level(self.0 - 1)
}

const MAX_DEPTH: u8 = 64;
const MAX_DEPTH: u8 = 128;
}
30 changes: 15 additions & 15 deletions askama_parser/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use nom::{error_position, IResult};

use super::{
bool_lit, char_lit, identifier, is_ws, keyword, num_lit, path_or_identifier, skip_till,
str_lit, ws, Expr, Level, PathOrIdentifier, State,
str_lit, ws, Expr, PathOrIdentifier, State,
};

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -50,8 +50,8 @@ impl<'a> Node<'a> {
let mut p = delimited(
|i| s.tag_block_start(i),
alt((
map(Call::parse, Self::Call),
map(Let::parse, Self::Let),
map(|i| Call::parse(i, s), Self::Call),
map(|i| Let::parse(i, s), Self::Let),
map(|i| If::parse(i, s), Self::If),
map(|i| Loop::parse(i, s), |l| Self::Loop(Box::new(l))),
map(|i| Match::parse(i, s), Self::Match),
Expand Down Expand Up @@ -105,7 +105,7 @@ impl<'a> Node<'a> {
|i| s.tag_expr_start(i),
cut(tuple((
opt(Whitespace::parse),
ws(|i| Expr::parse(i, Level::default())),
ws(|i| Expr::parse(i, s.level.get())),
opt(Whitespace::parse),
|i| s.tag_expr_end(i),
))),
Expand Down Expand Up @@ -295,7 +295,7 @@ impl<'a> Cond<'a> {
opt(Whitespace::parse),
ws(keyword("else")),
cut(tuple((
opt(CondTest::parse),
opt(|i| CondTest::parse(i, s)),
opt(Whitespace::parse),
|i| s.tag_block_end(i),
cut(|i| Node::many(i, s)),
Expand All @@ -320,7 +320,7 @@ pub struct CondTest<'a> {
}

impl<'a> CondTest<'a> {
fn parse(i: &'a str) -> IResult<&'a str, Self> {
fn parse(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> {
let mut p = preceded(
ws(keyword("if")),
cut(tuple((
Expand All @@ -329,7 +329,7 @@ impl<'a> CondTest<'a> {
ws(Target::parse),
ws(char('=')),
)),
ws(|i| Expr::parse(i, Level::default())),
ws(|i| Expr::parse(i, s.level.get())),
))),
);
let (i, (target, expr)) = p(i)?;
Expand Down Expand Up @@ -377,7 +377,7 @@ impl<'a> Loop<'a> {

let if_cond = preceded(
ws(keyword("if")),
cut(ws(|i| Expr::parse(i, Level::default()))),
cut(ws(|i| Expr::parse(i, s.level.get()))),
);
let else_block = |i| {
let mut p = preceded(
Expand All @@ -402,7 +402,7 @@ impl<'a> Loop<'a> {
ws(Target::parse),
ws(keyword("in")),
cut(tuple((
ws(|i| Expr::parse(i, Level::default())),
ws(|i| Expr::parse(i, s.level.get())),
opt(if_cond),
opt(Whitespace::parse),
|i| s.tag_block_end(i),
Expand Down Expand Up @@ -537,14 +537,14 @@ pub struct Call<'a> {
}

impl<'a> Call<'a> {
fn parse(i: &'a str) -> IResult<&'a str, Self> {
fn parse(i: &'a str, s: &State<'_>) -> 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(|nested| Expr::arguments(nested, Level::default()))),
opt(ws(|nested| Expr::arguments(nested, s.level.get()))),
opt(Whitespace::parse),
))),
));
Expand Down Expand Up @@ -577,7 +577,7 @@ impl<'a> Match<'a> {
opt(Whitespace::parse),
ws(keyword("match")),
cut(tuple((
ws(|i| Expr::parse(i, Level::default())),
ws(|i| Expr::parse(i, s.level.get())),
opt(Whitespace::parse),
|i| s.tag_block_end(i),
cut(tuple((
Expand Down Expand Up @@ -739,15 +739,15 @@ pub struct Let<'a> {
}

impl<'a> Let<'a> {
fn parse(i: &'a str) -> IResult<&'a str, Self> {
fn parse(i: &'a str, s: &State<'_>) -> 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(|i| Expr::parse(i, Level::default())),
ws(|i| Expr::parse(i, s.level.get())),
)),
opt(Whitespace::parse),
))),
Expand Down Expand Up @@ -775,7 +775,7 @@ impl<'a> If<'a> {
fn parse(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> {
let mut p = tuple((
opt(Whitespace::parse),
CondTest::parse,
|i| CondTest::parse(i, s),
cut(tuple((
opt(Whitespace::parse),
|i| s.tag_block_end(i),
Expand Down
4 changes: 2 additions & 2 deletions testing/tests/ui/excessive_nesting.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: problems parsing template source at row 8, column 36 near:
"{%if 1%}{%if 1%}{%if 1%}{%if 1%}{%if 1%}"...
error: problems parsing template source at row 14, column 34 near:
"%}{%if 1%}{%if 1%}{%if 1%}{%if 1%}{%if 1"...
--> tests/ui/excessive_nesting.rs:3:10
|
3 | #[derive(Template)]
Expand Down

0 comments on commit eef38ce

Please sign in to comment.