Refactor parse_ret_ty, start parsing by-alias return specs

This commit is contained in:
Marijn Haverbeke 2011-09-14 10:46:40 +02:00
parent 6eb9738a66
commit c1c083cd66
2 changed files with 24 additions and 51 deletions

View file

@ -381,7 +381,7 @@ tag ret_style {
noreturn; // functions with return type _|_ that always noreturn; // functions with return type _|_ that always
// raise an error or exit (i.e. never return to the caller) // raise an error or exit (i.e. never return to the caller)
return_val; // everything else return_val; // everything else
return_alias; return_ref;
} }
type _fn = {decl: fn_decl, proto: proto, body: blk}; type _fn = {decl: fn_decl, proto: proto, body: blk};

View file

@ -13,8 +13,6 @@ tag restriction { UNRESTRICTED; RESTRICT_NO_CALL_EXPRS; }
tag file_type { CRATE_FILE; SOURCE_FILE; } tag file_type { CRATE_FILE; SOURCE_FILE; }
tag ty_or_bang { a_ty(@ast::ty); a_bang; }
type parse_sess = @{cm: codemap::codemap, mutable next_id: node_id}; type parse_sess = @{cm: codemap::codemap, mutable next_id: node_id};
fn next_node_id(sess: parse_sess) -> node_id { fn next_node_id(sess: parse_sess) -> node_id {
@ -281,27 +279,14 @@ fn parse_ty_fn(proto: ast::proto, p: parser) -> ast::ty_ {
let t = parse_ty(p, false); let t = parse_ty(p, false);
ret spanned(lo, t.span.hi, {mode: mode, ty: t}); ret spanned(lo, t.span.hi, {mode: mode, ty: t});
} }
let lo = p.get_lo_pos();
let inputs = let inputs =
parse_seq(token::LPAREN, token::RPAREN, some(token::COMMA), parse_seq(token::LPAREN, token::RPAREN, some(token::COMMA),
parse_fn_input_ty, p); parse_fn_input_ty, p);
// FIXME: there's no syntax for this right now anyway // FIXME: there's no syntax for this right now anyway
// auto constrs = parse_constrs(~[], p); // auto constrs = parse_constrs(~[], p);
let constrs: [@ast::constr] = []; let constrs: [@ast::constr] = [];
let output: @ast::ty; let (ret_style, ret_ty) = parse_ret_ty(p);
let cf = ast::return_val; ret ast::ty_fn(proto, inputs.node, ret_ty, ret_style, constrs);
if p.peek() == token::RARROW {
p.bump();
let tmp = parse_ret_ty(p);
alt tmp {
a_ty(t) { output = t; }
a_bang. {
output = @spanned(lo, inputs.span.hi, ast::ty_bot);
cf = ast::noreturn;
}
}
} else { output = @spanned(lo, inputs.span.hi, ast::ty_nil); }
ret ast::ty_fn(proto, inputs.node, output, cf, constrs);
} }
fn parse_proto(p: parser) -> ast::proto { fn parse_proto(p: parser) -> ast::proto {
@ -452,15 +437,21 @@ fn parse_ty_postfix(orig_t: ast::ty_, p: parser, colons_before_params: bool)
} }
} }
fn parse_ret_ty(p: parser) -> ty_or_bang { fn parse_ret_ty(p: parser) -> (ast::ret_style, @ast::ty) {
/* if eat(p, token::RARROW) { ret if eat(p, token::RARROW) {
let lo = p.get_lo_pos();
if eat(p, token::NOT) {
(ast::noreturn, @spanned(lo, p.get_last_hi_pos(), ast::ty_bot))
} else { } else {
let style = ast::return_val;
}*/ if eat(p, token::BINOP(token::AND)) {
alt p.peek() { style = ast::return_ref;
token::NOT. { p.bump(); ret a_bang; } };
_ { ret a_ty(parse_ty(p, false)); } (style, parse_ty(p, false))
}
} else {
let pos = p.get_lo_pos();
(ast::return_val, @spanned(pos, pos, ast::ty_nil))
} }
} }
@ -1760,7 +1751,6 @@ fn parse_fn_decl(p: parser, purity: ast::purity, il: ast::inlineness) ->
let inputs: ast::spanned<[ast::arg]> = let inputs: ast::spanned<[ast::arg]> =
parse_seq(token::LPAREN, token::RPAREN, some(token::COMMA), parse_arg, parse_seq(token::LPAREN, token::RPAREN, some(token::COMMA), parse_arg,
p); p);
let rslt: ty_or_bang;
// Use the args list to translate each bound variable // Use the args list to translate each bound variable
// mentioned in a constraint to an arg index. // mentioned in a constraint to an arg index.
// Seems weird to do this in the parser, but I'm not sure how else to. // Seems weird to do this in the parser, but I'm not sure how else to.
@ -1769,30 +1759,13 @@ fn parse_fn_decl(p: parser, purity: ast::purity, il: ast::inlineness) ->
p.bump(); p.bump();
constrs = parse_constrs(bind parse_ty_constr(inputs.node, _), p); constrs = parse_constrs(bind parse_ty_constr(inputs.node, _), p);
} }
if p.peek() == token::RARROW { let (ret_style, ret_ty) = parse_ret_ty(p);
p.bump();
rslt = parse_ret_ty(p);
} else {
rslt = a_ty(@spanned(inputs.span.lo, inputs.span.hi, ast::ty_nil));
}
alt rslt {
a_ty(t) {
ret {inputs: inputs.node, ret {inputs: inputs.node,
output: t, output: ret_ty,
purity: purity, purity: purity,
il: il, il: il,
cf: ast::return_val, cf: ret_style,
constraints: constrs}; constraints: constrs};
}
a_bang. {
ret {inputs: inputs.node,
output: @spanned(p.get_lo_pos(), p.get_hi_pos(), ast::ty_bot),
purity: purity,
il: il,
cf: ast::noreturn,
constraints: constrs};
}
}
} }
fn parse_fn_block_decl(p: parser) -> ast::fn_decl { fn parse_fn_block_decl(p: parser) -> ast::fn_decl {