Skip to content

Commit

Permalink
Allow much more in parentehsis expressions.
Browse files Browse the repository at this point in the history
Basically, just handle nesting parenthesis, comments, and strings, and
let the rust compiler sort everything else out in the generated code.

Attempts to handle #25 .
  • Loading branch information
kaj committed Jun 23, 2018
1 parent 785152e commit 05ab367
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 18 deletions.
85 changes: 68 additions & 17 deletions src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,10 @@ named!(pub expression<&[u8], String>,
tag!("")),
from_utf8) >>
name: return_error!(err_str!("Expected rust expression"),
alt!(map!(rust_name, String::from) |
alt_complete!(map!(rust_name, String::from) |
map!(map_res!(digit, from_utf8), String::from) |
map!(delimited!(char!('"'),
map_res!(
escaped!(is_not!("\"\\"),
'\\', one_of!("\"\\")),
from_utf8),
char!('"')),
|text| format!("\"{}\"", text)) |
map!(delimited!(tag!("("), comma_expressions, tag!(")")),
|expr| format!("({})", expr)) |
map!(quoted_string, String::from) |
map!(expr_in_parens, String::from) |
map!(delimited!(tag!("["), comma_expressions, tag!("]")),
|expr| format!("[{}]", expr)))) >>
post: fold_many0!(
Expand All @@ -26,12 +19,11 @@ named!(pub expression<&[u8], String>,
|expr| format!(".{}", expr)) |
map!(preceded!(tag!("::"), expression),
|expr| format!("::{}", expr)) |
map!(delimited!(tag!("("), comma_expressions, tag!(")")),
|expr| format!("({})", expr)) |
map!(expr_in_parens, String::from) |
map!(delimited!(tag!("["), comma_expressions, tag!("]")),
|expr| format!("[{}]", expr)) |
map!(delimited!(tag!("!("), comma_expressions, tag!(")")),
|expr| format!("!({})", expr)) |
map!(preceded!(tag!("!"), expr_in_parens),
|expr| format!("!{}", expr)) |
map!(delimited!(tag!("!["), comma_expressions, tag!("]")),
|expr| format!("![{}]", expr))),
String::new(),
Expand All @@ -55,6 +47,45 @@ named!(
from_utf8
));

named!(
expr_in_parens<&[u8], &str>,
map_res!(recognize!(
delimited!(
tag!("("),
many0!(alt!(
value!((), is_not!("()\"/")) |
value!((), expr_in_parens) |
value!((), quoted_string) |
value!((), rust_comment) |
value!((), terminated!(tag!("/"), none_of!("*")))
)),
tag!(")")
)),
from_utf8
)
);

named!(
quoted_string<&[u8], String>,
map!(delimited!(char!('"'),
map_res!(
escaped!(is_not!("\"\\"),
'\\', one_of!("\"\\")),
from_utf8),
char!('"')),
|text| format!("\"{}\"", text)
)
);

named!(
rust_comment,
delimited!(tag!("/*"),
recognize!(many0!(alt_complete!(
is_not!("*") |
preceded!(tag!("*"), not!(tag!("/")))))),
tag!("*/"))
);

#[cfg(test)]
mod test {
use expression::expression;
Expand Down Expand Up @@ -101,6 +132,10 @@ mod test {
check_expr("\"foo\"");
}
#[test]
fn expression_str_paren() {
check_expr("(\")\")");
}
#[test]
fn expression_enum_variant() {
check_expr("MyEnum::Variant.method()");
}
Expand All @@ -120,6 +155,22 @@ mod test {
fn expression_number() {
check_expr("42");
}
#[test]
fn expression_with_comment() {
check_expr("(42 /* truly important number */)");
}
#[test]
fn expression_with_comment_a() {
check_expr("(42 /* \" */)");
}
#[test]
fn expression_with_comment_b() {
check_expr("(42 /* ) */)");
}
#[test]
fn expression_arithemtic_in_parens() {
check_expr("(2 + 3*4 - 5/2)");
}

fn check_expr(expr: &str) {
for post in &[" ", ", ", "! ", "? ", "<a>", "##", ". ", "\"", "'"] {
Expand Down Expand Up @@ -153,10 +204,10 @@ mod test {
#[test]
fn non_expression_c() {
assert_eq!(
expression_error_message(b"(+)"),
": 1:(+)\n\
expression_error_message(b"(missing end"),
": 1:(missing end\n\
: ^ Expected rust expression\n\
: 1:(+)\n\
: 1:(missing end\n\
: ^ Alt\n"
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/templateexpression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ mod test {
#[test]
fn if_let_2() {
assert_eq!(
template_expression(b"@if let Some((x,y)) = x { something }"),
template_expression(b"@if let Some((x, y)) = x { something }"),
IResult::Done(
&b""[..],
TemplateExpression::IfBlock {
Expand Down

0 comments on commit 05ab367

Please sign in to comment.