From d15fc3a61ff06e17cf4a560c25a73cbe9ef08def Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 19 Sep 2024 15:07:26 +0200 Subject: [PATCH 1/2] Improve config delimiters characters check --- rinja_parser/src/lib.rs | 46 ++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 30 deletions(-) diff --git a/rinja_parser/src/lib.rs b/rinja_parser/src/lib.rs index 30421d66c..a827e361d 100644 --- a/rinja_parser/src/lib.rs +++ b/rinja_parser/src/lib.rs @@ -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!( @@ -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(), + )); } } @@ -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) } } From c77cd378210960a55f0ba1d66d759d447290fdb5 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 19 Sep 2024 15:07:37 +0200 Subject: [PATCH 2/2] Add regression test for config delimiters --- testing/operator-paren-config.toml | 4 ++++ testing/operator-plus-config.toml | 4 ++++ testing/tests/ui/broken-config.rs | 8 ++++++++ testing/tests/ui/broken-config.stderr | 14 ++++++++++++++ testing/tests/ui/terminator-operator.stderr | 4 ++-- 5 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 testing/operator-paren-config.toml create mode 100644 testing/operator-plus-config.toml diff --git a/testing/operator-paren-config.toml b/testing/operator-paren-config.toml new file mode 100644 index 000000000..666c7a11e --- /dev/null +++ b/testing/operator-paren-config.toml @@ -0,0 +1,4 @@ +[[syntax]] +name = "paren" +expr_start = "<)" +expr_end = "(>" diff --git a/testing/operator-plus-config.toml b/testing/operator-plus-config.toml new file mode 100644 index 000000000..6646425cf --- /dev/null +++ b/testing/operator-plus-config.toml @@ -0,0 +1,4 @@ +[[syntax]] +name = "plus" +expr_start = "<+" +expr_end = "+>" diff --git a/testing/tests/ui/broken-config.rs b/testing/tests/ui/broken-config.rs index b41f95046..4a1525b3a 100644 --- a/testing/tests/ui/broken-config.rs +++ b/testing/tests/ui/broken-config.rs @@ -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() {} diff --git a/testing/tests/ui/broken-config.stderr b/testing/tests/ui/broken-config.stderr index 588b0c3b7..c4e6647b6 100644 --- a/testing/tests/ui/broken-config.stderr +++ b/testing/tests/ui/broken-config.stderr @@ -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")] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/testing/tests/ui/terminator-operator.stderr b/testing/tests/ui/terminator-operator.stderr index 65089f1c5..d4ae4b387 100644 --- a/testing/tests/ui/terminator-operator.stderr +++ b/testing/tests/ui/terminator-operator.stderr @@ -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 = "<> and <>", 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 |