Skip to content

Some function call fixes#684

Merged
Kijewski merged 1 commit intoaskama-rs:mainfrom
Kijewski:issue-682
Jan 28, 2026
Merged

Some function call fixes#684
Kijewski merged 1 commit intoaskama-rs:mainfrom
Kijewski:issue-682

Conversation

@Kijewski
Copy link
Copy Markdown
Member

@Kijewski Kijewski commented Jan 28, 2026

  • Parsing function calls is expensive. Make sure not to stack overflow.
  • We only need to to call level.nest() if we descent, but we need to call it before we descent.

I de-indented a good chunk of the code, so the diff might be more readable with hidden white-space changes.

Fixes #682.

@Kijewski Kijewski added bug Something isn't working parser Related to the parser labels Jan 28, 2026
Comment thread askama_parser/src/expr.rs
Comment on lines -31 to 37
let mut i_before = *i;
let mut level_guard = i.state.level.guard();
while let Some(((op, span), rhs)) = opt((ws(op.with_span()), inner)).parse_next(i)? {
let mut next = opt(|i: &mut _| {
// We need to make sure that we decrement the level before we enter the right-hand side.
let i_before = *i;
let op = ws(op.with_span()).parse_next(i)?;
level_guard.nest(&i_before)?;
Ok((op, inner(i)?))
});
while let Some(((op, span), rhs)) = next.parse_next(i)? {
expr = WithSpan::new(Box::new(Expr::BinOp(BinOp { op, lhs: expr, rhs })), span);
i_before = *i;
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function was a red herring. The change did not fix the problem with the function calls, but I still think the old implementation was not exactly correct.

Comment thread askama_parser/src/expr.rs
Comment on lines +282 to +292
fn comma<'a: 'l, 'l>(i: &mut InputStream<'a, 'l>) -> ParseResult<'a, ()> {
(ws(','), no_comma).void().parse_next(i)
}

fn no_comma<'a: 'l, 'l>(i: &mut InputStream<'a, 'l>) -> ParseResult<'a, ()> {
if let Some(span) = opt(','.span()).parse_next(i)? {
cut_error!("stray comma `,` in argument list", span)
} else {
Ok(())
}
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The introduction of the repeated-comma check is not necessarily part of the PR, but I think it makes for better error messages, and I was reviewing the function anyway. :)

Comment thread askama_parser/src/expr.rs Outdated

fn no_comma<'a: 'l, 'l>(i: &mut InputStream<'a, 'l>) -> ParseResult<'a, ()> {
if let Some(span) = opt(','.span()).parse_next(i)? {
cut_error!("stray comma `,` in argument list", span)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nit, not something that necessarily needs a fix but: I'd write expected an expression, found a comma in argument list.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, makes sense to tell the user what we expected instead of the comma.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following compiler example. :)

GuillaumeGomez
GuillaumeGomez previously approved these changes Jan 28, 2026
@GuillaumeGomez
Copy link
Copy Markdown
Collaborator

Apart from the nit, looks all good to me, thanks!

Comment thread askama_parser/src/expr.rs Outdated
fn no_comma<'a: 'l, 'l>(i: &mut InputStream<'a, 'l>) -> ParseResult<'a, ()> {
if let Some(span) = opt(','.span()).parse_next(i)? {
cut_error!(
"expected an expression, but found a comma in argument list",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpicking++

Suggested change
"expected an expression, but found a comma in argument list",
"expected an expression, found a comma in argument list",

* Parsing function calls is expensive. Make sure not to stack overflow.
* We only need to to call `level.nest()` if we descent, but we need to
  call it before we descent.
@GuillaumeGomez
Copy link
Copy Markdown
Collaborator

Thanks!

@Kijewski Kijewski merged commit d901581 into askama-rs:main Jan 28, 2026
50 checks passed
@Kijewski Kijewski deleted the issue-682 branch January 28, 2026 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working parser Related to the parser

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stack overflow on excessive call depth

2 participants