Skip to content
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

Improve config delimiters characters check #180

Merged
merged 2 commits into from
Oct 20, 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
46 changes: 16 additions & 30 deletions rinja_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,13 +806,13 @@ impl<'a> SyntaxBuilder<'a> {
comment_end: self.comment_end.unwrap_or(default.comment_end),
});

for (s, k) in [
(syntax.block_start, "opening block"),
(syntax.block_end, "closing block"),
(syntax.expr_start, "opening expression"),
(syntax.expr_end, "closing expression"),
(syntax.comment_start, "opening comment"),
(syntax.comment_end, "closing comment"),
for (s, k, is_closing) in [
(syntax.block_start, "opening block", false),
(syntax.block_end, "closing block", true),
(syntax.expr_start, "opening expression", false),
(syntax.expr_end, "closing expression", true),
(syntax.comment_start, "opening comment", false),
(syntax.comment_end, "closing comment", true),
] {
if s.len() < 2 {
return Err(format!(
Expand All @@ -824,6 +824,15 @@ impl<'a> SyntaxBuilder<'a> {
"delimiters may not contain white spaces. \
The {k} delimiter ({s:?}) contains white spaces",
));
} else if is_closing
&& ['(', '-', '+', '~', '.', '>', '<', '&', '|', '!']
.contains(&s.chars().next().unwrap())
{
return Err(format!(
"closing delimiters may not start with operators. \
The {k} delimiter ({s:?}) starts with operator `{}`",
s.chars().next().unwrap(),
));
}
}

Expand Down Expand Up @@ -853,29 +862,6 @@ impl<'a> SyntaxBuilder<'a> {
}
}

for (end, kind) in [
(syntax.block_end, "block"),
(syntax.expr_end, "expression"),
(syntax.comment_end, "comment"),
] {
for prefix in ["<<", ">>", "&&", "..", "||"] {
if end.starts_with(prefix) {
let msg = if end == prefix {
format!(
"a closing delimiter must not start with an operator. \
The {kind} delimiter ({end:?}) is also an operator",
)
} else {
format!(
"a closing delimiter must not start an with operator. \
The {kind} delimiter ({end:?}) starts with the {prefix:?} operator",
)
};
return Err(msg);
}
}
}

Ok(syntax)
}
}
Expand Down
4 changes: 4 additions & 0 deletions testing/operator-paren-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[[syntax]]
name = "paren"
expr_start = "<)"
expr_end = "(>"
4 changes: 4 additions & 0 deletions testing/operator-plus-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[[syntax]]
name = "plus"
expr_start = "<+"
expr_end = "+>"
8 changes: 8 additions & 0 deletions testing/tests/ui/broken-config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@ struct DelimClash;
#[template(source = "", ext = "txt", config = "delim-too-short.toml")]
struct DelimTooShort;

#[derive(Template)]
#[template(source = "<+a+> and <+b+>", config = "operator-plus-config.toml", syntax = "plus", ext = "txt")]
struct PlusOperator;

#[derive(Template)]
#[template(source = "<)a(> and <)b(>", config = "operator-paren-config.toml", syntax = "paren", ext = "txt")]
struct ParenOperator;

fn main() {}
14 changes: 14 additions & 0 deletions testing/tests/ui/broken-config.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,17 @@ error: delimiters must be at least two characters long. The opening block delimi
|
16 | #[template(source = "", ext = "txt", config = "delim-too-short.toml")]
| ^^^^^^^^^^^^^^^^^^^^^^

error: closing delimiters may not start with operators. The closing expression delimiter ("+>") starts with operator `+`
--> testing/operator-plus-config.toml
--> tests/ui/broken-config.rs:20:49
|
20 | #[template(source = "<+a+> and <+b+>", config = "operator-plus-config.toml", syntax = "plus", ext = "txt")]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

So in here, the template is perfectly valid but doesn't work. Not sure how to fix that...

| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: closing delimiters may not start with operators. The closing expression delimiter ("(>") starts with operator `(`
--> testing/operator-paren-config.toml
--> tests/ui/broken-config.rs:24:49
|
24 | #[template(source = "<)a(> and <)b(>", config = "operator-paren-config.toml", syntax = "paren", ext = "txt")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4 changes: 2 additions & 2 deletions testing/tests/ui/terminator-operator.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
error: a closing delimiter must not start with an operator. The expression delimiter (">>") is also an operator
error: closing delimiters may not start with operators. The closing expression delimiter (">>") starts with operator `>`
--> testing/issue-128.toml
--> tests/ui/terminator-operator.rs:4:49
|
4 | #[template(source = "<<a>> and <<b>>", config = "issue-128.toml", syntax = "mwe", ext="")]
| ^^^^^^^^^^^^^^^^

error: a closing delimiter must not start an with operator. The expression delimiter (">>>") starts with the ">>" operator
error: closing delimiters may not start with operators. The closing expression delimiter (">>>") starts with operator `>`
--> testing/issue-128-2.toml
--> tests/ui/terminator-operator.rs:11:49
|
Expand Down
Loading