rustc: Parse type-parametric tags

This commit is contained in:
Patrick Walton 2010-11-24 17:15:54 -08:00
parent f809375b16
commit 98e8c2ef21
3 changed files with 21 additions and 13 deletions

View File

@ -171,7 +171,7 @@ tag item_ {
item_fn(ident, _fn, vec[ty_param], def_id, ann);
item_mod(ident, _mod, def_id);
item_ty(ident, @ty, def_id, ann);
item_tag(ident, vec[variant], def_id);
item_tag(ident, vec[variant], vec[ty_param], def_id);
}

View File

@ -1039,17 +1039,21 @@ impure fn parse_block(parser p) -> ast.block {
ret spanned(stmts.span, stmts.span, b);
}
impure fn parse_ty_params(parser p) -> vec[ast.ty_param] {
let vec[ast.ty_param] ty_params = vec();
if (p.peek() == token.LBRACKET) {
auto f = parse_ident; // FIXME: pass as lval directly
ty_params = parse_seq[ast.ty_param](token.LBRACKET, token.RBRACKET,
some(token.COMMA), f, p).node;
}
ret ty_params;
}
impure fn parse_item_fn(parser p) -> tup(ast.ident, @ast.item) {
auto lo = p.get_span();
expect(p, token.FN);
auto id = parse_ident(p);
let vec[ast.ty_param] ty_params = vec();
if (p.peek() == token.LBRACKET) {
auto pg = parse_ident; // FIXME: pass as lval directly
ty_params = parse_seq[ast.ty_param](token.LBRACKET, token.RBRACKET,
some(token.COMMA), pg, p).node;
}
auto ty_params = parse_ty_params(p);
auto pf = parse_arg;
let util.common.spanned[vec[ast.arg]] inputs =
@ -1120,6 +1124,7 @@ impure fn parse_item_tag(parser p) -> tup(ast.ident, @ast.item) {
auto lo = p.get_span();
expect(p, token.TAG);
auto id = parse_ident(p);
auto ty_params = parse_ty_params(p);
let vec[ast.variant] variants = vec();
expect(p, token.LBRACE);
@ -1158,7 +1163,7 @@ impure fn parse_item_tag(parser p) -> tup(ast.ident, @ast.item) {
p.bump();
auto hi = p.get_span();
auto item = ast.item_tag(id, variants, p.next_def_id());
auto item = ast.item_tag(id, variants, ty_params, p.next_def_id());
ret tup(id, @spanned(lo, hi, item));
}

View File

@ -170,6 +170,7 @@ type ast_fold[ENV] =
(fn(&ENV e, &span sp, ident ident,
vec[ast.variant] variants,
vec[ast.ty_param] ty_params,
def_id id) -> @item) fold_item_tag,
// Additional nodes.
@ -554,7 +555,7 @@ fn fold_item[ENV](&ENV env, ast_fold[ENV] fld, @item i) -> @item {
ret fld.fold_item_ty(env_, i.span, ident, ty_, id, ann);
}
case (ast.item_tag(?ident, ?variants, ?id)) {
case (ast.item_tag(?ident, ?variants, ?ty_params, ?id)) {
let vec[ast.variant] new_variants = vec();
for (ast.variant v in variants) {
let vec[@ast.ty] new_args = vec();
@ -563,7 +564,8 @@ fn fold_item[ENV](&ENV env, ast_fold[ENV] fld, @item i) -> @item {
}
new_variants += rec(name=v.name, args=new_args);
}
ret fld.fold_item_tag(env_, i.span, ident, new_variants, id);
ret fld.fold_item_tag(env_, i.span, ident, new_variants,
ty_params, id);
}
}
@ -817,8 +819,9 @@ fn identity_fold_item_ty[ENV](&ENV e, &span sp, ident i,
fn identity_fold_item_tag[ENV](&ENV e, &span sp, ident i,
vec[ast.variant] variants,
vec[ast.ty_param] ty_params,
def_id id) -> @item {
ret @respan(sp, ast.item_tag(i, variants, id));
ret @respan(sp, ast.item_tag(i, variants, ty_params, id));
}
@ -939,7 +942,7 @@ fn new_identity_fold[ENV]() -> ast_fold[ENV] {
fold_item_fn = bind identity_fold_item_fn[ENV](_,_,_,_,_,_,_),
fold_item_mod = bind identity_fold_item_mod[ENV](_,_,_,_,_),
fold_item_ty = bind identity_fold_item_ty[ENV](_,_,_,_,_,_),
fold_item_tag = bind identity_fold_item_tag[ENV](_,_,_,_,_),
fold_item_tag = bind identity_fold_item_tag[ENV](_,_,_,_,_,_),
fold_block = bind identity_fold_block[ENV](_,_,_),
fold_fn = bind identity_fold_fn[ENV](_,_,_,_),