From 98e8c2ef21f6aab3dd3d4d328a1c8baf1f074ee4 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Wed, 24 Nov 2010 17:15:54 -0800 Subject: [PATCH] rustc: Parse type-parametric tags --- src/comp/front/ast.rs | 2 +- src/comp/front/parser.rs | 21 +++++++++++++-------- src/comp/middle/fold.rs | 11 +++++++---- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/comp/front/ast.rs b/src/comp/front/ast.rs index 3bc2fe167ec..800d979418b 100644 --- a/src/comp/front/ast.rs +++ b/src/comp/front/ast.rs @@ -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); } diff --git a/src/comp/front/parser.rs b/src/comp/front/parser.rs index 66529c69c15..3c640f4a862 100644 --- a/src/comp/front/parser.rs +++ b/src/comp/front/parser.rs @@ -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)); } diff --git a/src/comp/middle/fold.rs b/src/comp/middle/fold.rs index 2afbc8b9a93..8a8f65eb2db 100644 --- a/src/comp/middle/fold.rs +++ b/src/comp/middle/fold.rs @@ -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](_,_,_,_),