diff options
| author | Patrick Walton <[email protected]> | 2010-11-24 11:36:35 -0800 |
|---|---|---|
| committer | Patrick Walton <[email protected]> | 2010-11-24 11:36:35 -0800 |
| commit | 5d72dae1d13fdbe6275d732aac7dd9a9ad604b6f (patch) | |
| tree | c06fdafb3c451ab9771696e70b1bb780a3fcd077 /src/comp | |
| parent | rustc: Typecheck whiles and do-whiles. Add a workaround to complex.rs pending... (diff) | |
| download | rust-5d72dae1d13fdbe6275d732aac7dd9a9ad604b6f.tar.xz rust-5d72dae1d13fdbe6275d732aac7dd9a9ad604b6f.zip | |
rustc: Parse tag items. Currently segfaults in copy glue.
Diffstat (limited to 'src/comp')
| -rw-r--r-- | src/comp/front/ast.rs | 3 | ||||
| -rw-r--r-- | src/comp/front/parser.rs | 49 | ||||
| -rw-r--r-- | src/comp/middle/fold.rs | 23 |
3 files changed, 75 insertions, 0 deletions
diff --git a/src/comp/front/ast.rs b/src/comp/front/ast.rs index ddd02a03..2a590d02 100644 --- a/src/comp/front/ast.rs +++ b/src/comp/front/ast.rs @@ -153,11 +153,14 @@ type _fn = rec(vec[arg] inputs, type _mod = rec(vec[@item] items, hashmap[ident,uint] index); +type variant = rec(str name, vec[@ty] args); + type item = spanned[item_]; tag item_ { item_fn(ident, _fn, def_id, ann); item_mod(ident, _mod, def_id); item_ty(ident, @ty, def_id, ann); + item_tag(ident, vec[variant], def_id); } diff --git a/src/comp/front/parser.rs b/src/comp/front/parser.rs index 732e5b17..caff774b 100644 --- a/src/comp/front/parser.rs +++ b/src/comp/front/parser.rs @@ -1019,6 +1019,52 @@ impure fn parse_item_type(parser p) -> tup(ast.ident, @ast.item) { ret tup(id, @spanned(lo, hi, item)); } +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); + + let vec[ast.variant] variants = vec(); + expect(p, token.LBRACE); + while (p.peek() != token.RBRACE) { + auto tok = p.peek(); + alt (tok) { + case (token.IDENT(?name)) { + p.bump(); + + auto args; + alt (p.peek()) { + case (token.LPAREN) { + auto f = parse_ty; + auto tys = parse_seq[@ast.ty](token.LPAREN, + token.RPAREN, + some(token.COMMA), + f, p); + args = tys.node; + } + case (_) { + args = vec(); + } + } + + expect(p, token.SEMI); + + variants += vec(rec(name=name, args=args)); + } + case (token.RBRACE) { /* empty */ } + case (_) { + p.err("expected name of variant or '}' but found " + + token.to_str(tok)); + } + } + } + p.bump(); + + auto hi = p.get_span(); + auto item = ast.item_tag(id, variants, p.next_def_id()); + ret tup(id, @spanned(lo, hi, item)); +} + impure fn parse_item(parser p) -> tup(ast.ident, @ast.item) { alt (p.peek()) { case (token.FN) { @@ -1030,6 +1076,9 @@ impure fn parse_item(parser p) -> tup(ast.ident, @ast.item) { case (token.TYPE) { ret parse_item_type(p); } + case (token.TAG) { + ret parse_item_tag(p); + } case (?t) { p.err("expected item but found " + token.to_str(t)); } diff --git a/src/comp/middle/fold.rs b/src/comp/middle/fold.rs index 6ad65544..f32eddae 100644 --- a/src/comp/middle/fold.rs +++ b/src/comp/middle/fold.rs @@ -148,6 +148,10 @@ type ast_fold[ENV] = (fn(&ENV e, &span sp, ident ident, @ty t, def_id id, ann a) -> @item) fold_item_ty, + (fn(&ENV e, &span sp, ident ident, + vec[ast.variant] variants, + def_id id) -> @item) fold_item_tag, + // Additional nodes. (fn(&ENV e, &span sp, &ast.block_) -> block) fold_block, @@ -495,6 +499,18 @@ fn fold_item[ENV](&ENV env, ast_fold[ENV] fld, @item i) -> @item { let @ast.ty ty_ = fold_ty[ENV](env_, fld, ty); ret fld.fold_item_ty(env_, i.span, ident, ty_, id, ann); } + + case (ast.item_tag(?ident, ?variants, ?id)) { + let vec[ast.variant] new_variants = vec(); + for (ast.variant v in variants) { + let vec[@ast.ty] new_args = vec(); + for (@ast.ty t in v.args) { + new_args += vec(fold_ty[ENV](env_, fld, t)); + } + new_variants += rec(name=v.name, args=new_args); + } + ret fld.fold_item_tag(env_, i.span, ident, new_variants, id); + } } fail; @@ -723,6 +739,12 @@ fn identity_fold_item_ty[ENV](&ENV e, &span sp, ident i, ret @respan(sp, ast.item_ty(i, t, id, a)); } +fn identity_fold_item_tag[ENV](&ENV e, &span sp, ident i, + vec[ast.variant] variants, + def_id id) -> @item { + ret @respan(sp, ast.item_tag(i, variants, id)); +} + // Additional identities. @@ -832,6 +854,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_block = bind identity_fold_block[ENV](_,_,_), fold_fn = bind identity_fold_fn[ENV](_,_,_,_), |