syntax: Support dropping argument list from for/do

This commit is contained in:
Brian Anderson 2012-07-01 22:18:41 -07:00
parent fa6a446e6c
commit 9743757113
3 changed files with 45 additions and 9 deletions

View file

@ -1357,21 +1357,47 @@ class parser {
// `|args| { ... }` like in `do` expressions
fn parse_lambda_block_expr() -> @expr {
self.parse_lambda_expr_(|| {
let blk = self.parse_block();
self.mk_expr(blk.span.lo, blk.span.hi, expr_block(blk))
})
self.parse_lambda_expr_(
|| {
alt self.token {
token::BINOP(token::OR) | token::OROR {
self.parse_fn_block_decl()
}
_ {
// No argument list - `do foo {`
({
{
inputs: ~[],
output: @{
id: self.get_id(),
node: ty_infer,
span: self.span
},
purity: impure_fn,
cf: return_val,
constraints: ~[]
}
},
@~[])
}
}
},
|| {
let blk = self.parse_block();
self.mk_expr(blk.span.lo, blk.span.hi, expr_block(blk))
})
}
// `|args| expr`
fn parse_lambda_expr() -> @expr {
self.parse_lambda_expr_(|| self.parse_expr())
self.parse_lambda_expr_(|| self.parse_fn_block_decl(),
|| self.parse_expr())
}
fn parse_lambda_expr_(parse_body: fn&() -> @expr) -> @expr {
fn parse_lambda_expr_(parse_decl: fn&() -> (fn_decl, capture_clause),
parse_body: fn&() -> @expr) -> @expr {
let lo = self.last_span.lo;
// New style lambdas `|args| expr`
let (decl, captures) = self.parse_fn_block_decl();
let (decl, captures) = parse_decl();
let body = parse_body();
let fakeblock = {view_items: ~[], stmts: ~[], expr: some(body),
id: self.get_id(), rules: default_blk};

View file

@ -1,3 +1,3 @@
fn main() {
let x = do y; //! ERROR: expecting '|' but found
let x = do y; //! ERROR: expecting '{' but found
}

View file

@ -0,0 +1,10 @@
// Testing that we can drop the || in for/do exprs
fn f(f: fn@() -> bool) { }
fn d(f: fn@()) { }
fn main() {
for f { }
do d { }
}