Updated closure syntax
This commit is contained in:
@@ -30,7 +30,6 @@ keyword = {
|
||||
| "false"
|
||||
| "dyn"
|
||||
| "loop"
|
||||
| "fn"
|
||||
| "unsafe"
|
||||
| "override"
|
||||
}
|
||||
@@ -200,8 +199,8 @@ attributes = {
|
||||
// DECLARATION HELPERS
|
||||
// ======================================================
|
||||
var_decl = {
|
||||
("let" ~ pattern) |
|
||||
type_expr ~ (!tuple_pattern ~ pattern)
|
||||
("let" ~ pattern)
|
||||
| type_expr ~ (!tuple_pattern ~ pattern)
|
||||
}
|
||||
|
||||
field = {
|
||||
@@ -343,7 +342,8 @@ function_decl = {
|
||||
// ======================================================
|
||||
|
||||
top_level = {
|
||||
mod_attribute | (attributes ~ (import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | declare_module | function_decl))
|
||||
mod_attribute
|
||||
| (attributes ~ (import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | declare_module | function_decl))
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
@@ -466,8 +466,8 @@ match_stmt = {
|
||||
// ------------------------------------------------------
|
||||
|
||||
closure = {
|
||||
"fn" ~ "(" ~ param_list ~ ")" ~ type_expr? ~ block
|
||||
| "fn" ~ "(" ~ param_list ~ ")" ~ type_expr? ~ "->" ~ expr
|
||||
("(" ~ param_list ~ ")" ~ "=>" ~ expr)
|
||||
| (type_expr ~ "(" ~ param_list ~ ")" ~ "=>" ~ expr)
|
||||
}
|
||||
|
||||
// ------------------------------------------------------
|
||||
|
||||
@@ -77,10 +77,10 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression {
|
||||
|
||||
Rule::closure => {
|
||||
ast_expr!(Expression::Closure {
|
||||
params: collect_recovered(inner.next().unwrap().into_inner()),
|
||||
return_type: consume_rule(&mut inner, Rule::type_expr)
|
||||
.map(TypeExpr::try_from)
|
||||
.transpose(),
|
||||
params: collect_recovered(inner.next().unwrap().into_inner()),
|
||||
body: Expression::try_from(inner.next().unwrap()).map(Box::new),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -88,7 +88,7 @@ pub void test_as_cast() {
|
||||
}
|
||||
|
||||
pub void test_closure() {
|
||||
let add = fn(a, b) -> a + b;
|
||||
let add = (a, b) => a + b;
|
||||
assert_eq!(add(2, 3), 5);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user