Skip to content

parser: add better error messages for broken closing block tags #239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions rinja_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ use std::{fmt, str};

use winnow::Parser;
use winnow::ascii::escaped;
use winnow::combinator::{alt, cut_err, delimited, fail, not, opt, preceded, repeat};
use winnow::combinator::{alt, cut_err, delimited, fail, not, opt, peek, preceded, repeat};
use winnow::error::{ErrorKind, FromExternalError};
use winnow::stream::AsChar;
use winnow::token::{any, one_of, tag, take_till0, take_till1, take_while};
use winnow::token::{any, one_of, take_till0, take_till1, take_while};

pub mod expr;
pub use expr::{Expr, Filter};
Expand Down Expand Up @@ -666,28 +666,42 @@ impl<'a> State<'a> {
ret
}

fn tag_block_start<'i>(&self, i: &'i str) -> ParseResult<'i> {
tag(self.syntax.block_start).parse_next(i)
fn tag_block_start<'i>(&self, i: &'i str) -> ParseResult<'i, ()> {
self.syntax.block_start.value(()).parse_next(i)
}

fn tag_block_end<'i>(&self, i: &'i str) -> ParseResult<'i> {
tag(self.syntax.block_end).parse_next(i)
fn tag_block_end<'i>(&self, i: &'i str) -> ParseResult<'i, ()> {
let (i, control) = alt((
self.syntax.block_end.value(None),
peek(delimited('%', alt(('-', '~', '+')).map(Some), '}')),
|_| fail(i), // rollback on partial matches in the previous line
))
.parse_next(i)?;
if let Some(control) = control {
let message = format!(
"unclosed block, you likely meant to apply whitespace control: {:?}",
format!("{control}{}", self.syntax.block_end),
);
Err(ParseErr::backtrack(ErrorContext::new(message, i).into()))
} else {
Ok((i, ()))
}
}

fn tag_comment_start<'i>(&self, i: &'i str) -> ParseResult<'i> {
tag(self.syntax.comment_start).parse_next(i)
fn tag_comment_start<'i>(&self, i: &'i str) -> ParseResult<'i, ()> {
self.syntax.comment_start.value(()).parse_next(i)
}

fn tag_comment_end<'i>(&self, i: &'i str) -> ParseResult<'i> {
tag(self.syntax.comment_end).parse_next(i)
fn tag_comment_end<'i>(&self, i: &'i str) -> ParseResult<'i, ()> {
self.syntax.comment_end.value(()).parse_next(i)
}

fn tag_expr_start<'i>(&self, i: &'i str) -> ParseResult<'i> {
tag(self.syntax.expr_start).parse_next(i)
fn tag_expr_start<'i>(&self, i: &'i str) -> ParseResult<'i, ()> {
self.syntax.expr_start.value(()).parse_next(i)
}

fn tag_expr_end<'i>(&self, i: &'i str) -> ParseResult<'i> {
tag(self.syntax.expr_end).parse_next(i)
fn tag_expr_end<'i>(&self, i: &'i str) -> ParseResult<'i, ()> {
self.syntax.expr_end.value(()).parse_next(i)
}

fn enter_loop(&self) {
Expand Down
4 changes: 2 additions & 2 deletions rinja_parser/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl<'a> Node<'a> {

let (i, closed) = cut_node(
None,
alt(((|i| s.tag_block_end(i)).value(true), ws(eof).value(false))),
alt((ws(eof).value(false), (|i| s.tag_block_end(i)).value(true))),
)
.parse_next(i)?;
match closed {
Expand Down Expand Up @@ -457,7 +457,7 @@ fn check_block_start<'a>(
s: &State<'_>,
node: &str,
expected: &str,
) -> ParseResult<'a> {
) -> ParseResult<'a, ()> {
if i.is_empty() {
return Err(winnow::error::ErrMode::Cut(ErrorContext::new(
format!("expected `{expected}` to terminate `{node}` node, found nothing"),
Expand Down
39 changes: 39 additions & 0 deletions testing/tests/ui/garbled-closing-blocks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use rinja::Template;

#[derive(Template)]
#[template(ext = "txt", source = "{% if cond %-}{% endif %}")]
struct BlockSuppress {
cond: bool,
}

#[derive(Template)]
#[template(ext = "txt", source = "{% if cond %+}{% endif %}")]
struct BlockPreserve {
cond: bool,
}

#[derive(Template)]
#[template(ext = "txt", source = "{% if cond %~}{% endif %}")]
struct BlockFold {
cond: bool,
}

#[derive(Template)]
#[template(ext = "txt", source = "{% if cond %}{% endif %-}")]
struct BlockSuppress2 {
cond: bool,
}

#[derive(Template)]
#[template(ext = "txt", source = "{% if cond %}{% endif %+}")]
struct BlockPreserve2 {
cond: bool,
}

#[derive(Template)]
#[template(ext = "txt", source = "{% if cond %}{% endif %~}")]
struct BlockFold2 {
cond: bool,
}

fn main() {}
47 changes: 47 additions & 0 deletions testing/tests/ui/garbled-closing-blocks.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
error: unclosed block, you likely meant to apply whitespace control: "-%}"
--> <source attribute>:1:11
"%-}{% endif %}"
--> tests/ui/garbled-closing-blocks.rs:4:34
|
4 | #[template(ext = "txt", source = "{% if cond %-}{% endif %}")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: unclosed block, you likely meant to apply whitespace control: "+%}"
--> <source attribute>:1:11
"%+}{% endif %}"
--> tests/ui/garbled-closing-blocks.rs:10:34
|
10 | #[template(ext = "txt", source = "{% if cond %+}{% endif %}")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: unclosed block, you likely meant to apply whitespace control: "~%}"
--> <source attribute>:1:11
"%~}{% endif %}"
--> tests/ui/garbled-closing-blocks.rs:16:34
|
16 | #[template(ext = "txt", source = "{% if cond %~}{% endif %}")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: unclosed block, you likely meant to apply whitespace control: "-%}"
--> <source attribute>:1:22
"%-}"
--> tests/ui/garbled-closing-blocks.rs:22:34
|
22 | #[template(ext = "txt", source = "{% if cond %}{% endif %-}")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: unclosed block, you likely meant to apply whitespace control: "+%}"
--> <source attribute>:1:22
"%+}"
--> tests/ui/garbled-closing-blocks.rs:28:34
|
28 | #[template(ext = "txt", source = "{% if cond %}{% endif %+}")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: unclosed block, you likely meant to apply whitespace control: "~%}"
--> <source attribute>:1:22
"%~}"
--> tests/ui/garbled-closing-blocks.rs:34:34
|
34 | #[template(ext = "txt", source = "{% if cond %}{% endif %~}")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Loading