Skip to content

Commit

Permalink
An expression starting with paren ends on close.
Browse files Browse the repository at this point in the history
BREAKING CHANGE.  If an expession starts with an open parenthesis, the
expression ends on the closing parenthesis (and that outer parenthesis
is not part of the expression.

This fixes cases where an expression is immediately followed by
something that could be part of the expression.

Before this change, calling a function on the result of some
subexpression could be written as `@(a - b).abs()`.  After this
change, that should be changed to `@((a - b).abs())` unless the intent
is to have the result of (a - b) followed by the template string
`.abs()`.

Fixes #64.
  • Loading branch information
kaj committed Dec 14, 2019
1 parent caa13cb commit 54e6cb1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
12 changes: 12 additions & 0 deletions examples/simple/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,15 @@ fn test_page_two() {
\n</html>\n\n",
)
}

#[test]
fn test_some_expressions() {
assert_eq!(
r2s(|o| some_expressions(o, "name")),
"<p>name</p>\
\n<p>name.name</p>\
\n<p>4</p>\
\n<p>name.len()</p>\
\n<p>1</p>\n"
)
}
7 changes: 7 additions & 0 deletions examples/simple/templates/some_expressions.rs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@(arg: &str)

<p>@arg</p>
<p>@arg.@arg</p>
<p>@arg.len()</p>
<p>@(arg).len()</p>
<p>@((2_i8 - 3).abs())</p>@* Note extra parens needed here *@
6 changes: 6 additions & 0 deletions src/templateexpression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ pub fn template_expression(input: &[u8]) -> PResult<TemplateExpression> {
tag("@"),
tag("{"),
tag("}"),
tag("("),
terminated(alt((tag("if"), tag("for"))), tag(" ")),
value(&b""[..], tag("")),
)),
Expand Down Expand Up @@ -193,6 +194,11 @@ pub fn template_expression(input: &[u8]) -> PResult<TemplateExpression> {
body,
},
)(i),
(i, Some(b"(")) => map(terminated(expression, tag(")")), |expr| {
TemplateExpression::Expression {
expr: expr.to_string(),
}
})(i),
(i, Some(b"")) => {
map(expression, |expr| TemplateExpression::Expression {
expr: expr.to_string(),
Expand Down

0 comments on commit 54e6cb1

Please sign in to comment.