Greatly speed up nested array parsing#702
Merged
GuillaumeGomez merged 5 commits intoaskama-rs:mainfrom Feb 17, 2026
Merged
Conversation
Kijewski
reviewed
Feb 16, 2026
askama_parser/src/expr.rs
Outdated
Comment on lines
682
to
695
| if elements.is_empty() { | ||
| return cut_error!( | ||
| "expected element expression for array repeat syntax", | ||
| sub_span | ||
| ); | ||
| } else if elements.len() != 1 { | ||
| return cut_error!( | ||
| "unexpected `;` after expression", | ||
| elements.last().unwrap().span() | ||
| ); | ||
| } | ||
| let count = terminated(Self::array_repeat_count, ']').parse_next(i)?; | ||
| return Ok(WithSpan::new( | ||
| Box::new(Self::ArrayRepeat(elements.pop().unwrap(), count)), |
Member
There was a problem hiding this comment.
let Some(elem) = elements.pop() else {
"expected element expression for array repeat syntax"
};
if !elements.is_empty() {
"unexpected `;` after expression"
}
return Ok();
Collaborator
Author
There was a problem hiding this comment.
It's nicer, good idea!
Kijewski
reviewed
Feb 16, 2026
askama_parser/src/expr.rs
Outdated
Comment on lines
681
to
691
| if ws(';').parse_next(i).is_ok() { | ||
| if elements.is_empty() { | ||
| return cut_error!( | ||
| "expected element expression for array repeat syntax", | ||
| sub_span | ||
| ); | ||
| } else if elements.len() != 1 { | ||
| return cut_error!( | ||
| "unexpected `;` after expression", | ||
| elements.last().unwrap().span() | ||
| ); |
Member
There was a problem hiding this comment.
if let Some(span) = opt(ws(';')).span().parse_next(i)? {
if .. {
return cut_error!("unexpected `;` after expression", span);
}
}
Member
|
Oh, that's much nicer than potentially parsing everything twice! Just two minor comments. |
Collaborator
Author
|
Applied comments. :) |
* inline tiny function `array_repeat_element` * make sure to return an error with a message * for errors with array repeat, point to the `;`, not `[` * re-use `any_rust_token()` to tell what we found instead of `]` * add grouping characters to `any_rust_token()`
Member
|
I was not happy with the uninformative "failed to parse template source" error messages, so I tried to better derive the error condition and position. |
Collaborator
Author
|
Can't approve your commit but it's a great improvement, thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes https://oss-fuzz.com/testcase-detail/5640096178307072.
The problem was that, we went into the two full array parsing branches and if any failed, we tried the other one, and if this failed, we returned and then we repeat. It's fine as long as the error isn't super deep nested.
This fix simply gets all repeated exprs, and then check if a
;is next and then handle errors if any. So we have only one path instead of 2. And as a nice bonus, code is shorter. :)