syntax: Support dropping argument list from for/do
This commit is contained in:
parent
fa6a446e6c
commit
9743757113
3 changed files with 45 additions and 9 deletions
|
@ -1357,21 +1357,47 @@ class parser {
|
||||||
|
|
||||||
// `|args| { ... }` like in `do` expressions
|
// `|args| { ... }` like in `do` expressions
|
||||||
fn parse_lambda_block_expr() -> @expr {
|
fn parse_lambda_block_expr() -> @expr {
|
||||||
self.parse_lambda_expr_(|| {
|
self.parse_lambda_expr_(
|
||||||
let blk = self.parse_block();
|
|| {
|
||||||
self.mk_expr(blk.span.lo, blk.span.hi, expr_block(blk))
|
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`
|
// `|args| expr`
|
||||||
fn parse_lambda_expr() -> @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;
|
let lo = self.last_span.lo;
|
||||||
// New style lambdas `|args| expr`
|
let (decl, captures) = parse_decl();
|
||||||
let (decl, captures) = self.parse_fn_block_decl();
|
|
||||||
let body = parse_body();
|
let body = parse_body();
|
||||||
let fakeblock = {view_items: ~[], stmts: ~[], expr: some(body),
|
let fakeblock = {view_items: ~[], stmts: ~[], expr: some(body),
|
||||||
id: self.get_id(), rules: default_blk};
|
id: self.get_id(), rules: default_blk};
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
fn main() {
|
fn main() {
|
||||||
let x = do y; //! ERROR: expecting '|' but found
|
let x = do y; //! ERROR: expecting '{' but found
|
||||||
}
|
}
|
||||||
|
|
10
src/test/run-pass/do-for-no-args.rs
Normal file
10
src/test/run-pass/do-for-no-args.rs
Normal 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 { }
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue