rinja-rs / rinja

A template rendering engine based on Jinja, generating type-safe Rust code at compile time.
https://rinja.readthedocs.io
Apache License 2.0
116 stars 10 forks source link

Template parsing issues when using double angle brackets as delimiters #128

Closed ronnodas closed 1 month ago

ronnodas commented 1 month ago

With expr delimiters << and >>, trying to use the template <<a>> and <<b>> complains about and not being a field. Here is a full example that produces the error "no field and on type &HelloTemplate":

[[syntax]]
name = "mwe"
expr_start = "<<"
expr_end = ">>"
#[derive(Template)]
#[template(source = "<<a>> and <<b>>", syntax = "mwe", ext="")]
struct HelloTemplate {
    a: u32,
    b: u32,
}

Maybe worse, using (<<a>> and) <<b>> as the template produces "failed to parse template source".

#[derive(Template)]
#[template(source = "(<<a>> and) <<b>>", syntax = "mwe", ext = "")]
struct HelloTemplate {
    a: u32,
    b: u32,
}

Everything works fine if the delimiters are changed to eg <! and !>. I have not experimented with other pairs.

The same issue also exists in askama, which I have filed as https://github.com/djc/askama/issues/1081.

Kijewski commented 1 month ago

Thank you for the bug report! As the language is currently parsed, I don't think this is fixable. The parser sees a right shift >>, so it wants to parse a binary operation. The string <<a>> and) <<b>> is tokenized like: StartExpr, Var(a), Op(">>"), Var("and"), Unexpected(")"), …

We should explicitly reject terminators that start with >>, <<, .., &&, or ||, so you and other users get a proper error message.

ronnodas commented 1 month ago

That makes sense. It's weird that it works in the python/jinja project I'm porting from.

Kijewski commented 1 month ago

They probably first look for the terminator, then try to make sense of everything captured between the delimiters. There are pros and cons to both approaches.

GuillaumeGomez commented 1 month ago

It was fixed in #129 so I think it can be closed now. It'll be part of next release.