Skip to content

Commit

Permalink
Merge pull request #180 from GuillaumeGomez/config-delimiters
Browse files Browse the repository at this point in the history
Improve config delimiters characters check
  • Loading branch information
Kijewski authored Oct 20, 2024
2 parents 0ec827f + c77cd37 commit f1431a0
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 32 deletions.
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")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

0 comments on commit f1431a0

Please sign in to comment.