Skip to content

Commit f134261

Browse files
committed
Support Ruby-style block argument syntax
Issue #1054
1 parent ff813f8 commit f134261

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

src/comp/syntax/parse/parser.rs

+21-12
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,10 @@ fn mk_mac_expr(p: parser, lo: uint, hi: uint, m: ast::mac_) -> @ast::expr {
781781
span: ast_util::mk_sp(lo, hi)};
782782
}
783783

784+
fn is_bar(t: token::token) -> bool {
785+
alt t { token::BINOP(token::OR.) | token::OROR. { true } _ { false } }
786+
}
787+
784788
fn parse_bottom_expr(p: parser) -> @ast::expr {
785789
let lo = p.get_lo_pos();
786790
let hi = p.get_hi_pos();
@@ -815,8 +819,7 @@ fn parse_bottom_expr(p: parser) -> @ast::expr {
815819
hi = p.get_hi_pos();
816820
expect(p, token::RBRACE);
817821
ex = ast::expr_rec(fields, base);
818-
} else if p.peek() == token::BINOP(token::OR) ||
819-
p.peek() == token::OROR {
822+
} else if is_bar(p.peek()) {
820823
ret parse_fn_block_expr(p);
821824
} else {
822825
let blk = parse_block_tail(p, lo, ast::default_blk);
@@ -1061,10 +1064,8 @@ fn parse_dot_or_call_expr_with(p: parser, e: @ast::expr) -> @ast::expr {
10611064
ret e;
10621065
} else {
10631066
// Call expr.
1064-
1065-
let es =
1066-
parse_seq(token::LPAREN, token::RPAREN,
1067-
some(token::COMMA), parse_expr, p);
1067+
let es = parse_seq(token::LPAREN, token::RPAREN,
1068+
some(token::COMMA), parse_expr, p);
10681069
hi = es.span.hi;
10691070
let nd = ast::expr_call(e, es.node);
10701071
e = mk_expr(p, lo, hi, nd);
@@ -1088,6 +1089,19 @@ fn parse_dot_or_call_expr_with(p: parser, e: @ast::expr) -> @ast::expr {
10881089
t { unexpected(p, t); }
10891090
}
10901091
}
1092+
token::LBRACE. when is_bar(p.look_ahead(1u)) {
1093+
p.bump();
1094+
let blk = parse_fn_block_expr(p);
1095+
alt e.node {
1096+
ast::expr_call(f, args) {
1097+
e = @{node: ast::expr_call(f, args + [blk]) with *e};
1098+
}
1099+
_ {
1100+
e = mk_expr(p, lo, p.get_last_hi_pos(),
1101+
ast::expr_call(e, [blk]));
1102+
}
1103+
}
1104+
}
10911105
_ { ret e; }
10921106
}
10931107
}
@@ -1394,11 +1408,6 @@ fn parse_initializer(p: parser) -> option::t<ast::initializer> {
13941408
p.bump();
13951409
ret some({op: ast::init_move, expr: parse_expr(p)});
13961410
}
1397-
1398-
1399-
1400-
1401-
14021411
// Now that the the channel is the first argument to receive,
14031412
// combining it with an initializer doesn't really make sense.
14041413
// case (token::RECV) {
@@ -1792,7 +1801,7 @@ fn parse_fn_decl(p: parser, purity: ast::purity, il: ast::inlineness) ->
17921801
fn parse_fn_block_decl(p: parser) -> ast::fn_decl {
17931802
let inputs =
17941803
if p.peek() == token::OROR {
1795-
p.bump();;
1804+
p.bump();
17961805
[]
17971806
} else {
17981807
parse_seq(token::BINOP(token::OR), token::BINOP(token::OR),

0 commit comments

Comments
 (0)