Add ty_obj to ast and parser.

This commit is contained in:
Graydon Hoare 2010-12-14 17:42:12 -08:00
parent 4cddeed651
commit b1e0c60d6d
2 changed files with 39 additions and 7 deletions

View File

@ -171,6 +171,8 @@ tag lit_ {
type ty_field = rec(ident ident, @ty ty);
type ty_arg = rec(mode mode, @ty ty);
// TODO: effect
type ty_method = rec(ident ident, vec[ty_arg] inputs, @ty output);
type ty = spanned[ty_];
tag ty_ {
ty_nil;
@ -185,6 +187,7 @@ tag ty_ {
ty_tup(vec[@ty]);
ty_rec(vec[ty_field]);
ty_fn(vec[ty_arg], @ty); // TODO: effect
ty_obj(vec[ty_method]);
ty_path(path, option.t[def]);
ty_mutable(@ty);
}

View File

@ -102,7 +102,7 @@ impure fn parse_ident(parser p) -> ast.ident {
}
}
impure fn parse_ty_fn(parser p) -> ast.ty_ {
impure fn parse_ty_fn(parser p, ast.span lo) -> ast.ty_ {
impure fn parse_fn_input_ty(parser p) -> rec(ast.mode mode, @ast.ty ty) {
auto mode;
if (p.peek() == token.BINOP(token.AND)) {
@ -124,8 +124,6 @@ impure fn parse_ty_fn(parser p) -> ast.ty_ {
auto lo = p.get_span();
expect(p, token.FN);
auto f = parse_fn_input_ty; // FIXME: trans_const_lval bug
auto inputs = parse_seq[rec(ast.mode mode, @ast.ty ty)](token.LPAREN,
token.RPAREN, some(token.COMMA), f, p);
@ -141,6 +139,31 @@ impure fn parse_ty_fn(parser p) -> ast.ty_ {
ret ast.ty_fn(inputs.node, output);
}
impure fn parse_ty_obj(parser p, &mutable ast.span hi) -> ast.ty_ {
expect(p, token.OBJ);
impure fn parse_method_sig(parser p) -> ast.ty_method {
auto flo = p.get_span();
expect(p, token.FN);
auto ident = parse_ident(p);
auto f = parse_ty_fn(p, flo);
expect(p, token.SEMI);
alt (f) {
case (ast.ty_fn(?inputs, ?output)) {
ret rec(ident=ident, inputs=inputs, output=output);
}
}
fail;
}
auto f = parse_method_sig;
auto meths =
parse_seq[ast.ty_method](token.LBRACE,
token.RBRACE,
none[token.token],
f, p);
hi = meths.span;
ret ast.ty_obj(meths.node);
}
impure fn parse_ty_field(parser p) -> ast.ty_field {
auto ty = parse_ty(p);
auto id = parse_ident(p);
@ -196,7 +219,7 @@ impure fn parse_ty(parser p) -> @ast.ty {
auto elems = parse_seq[@ast.ty] (token.LPAREN,
token.RPAREN,
some(token.COMMA), f, p);
hi = p.get_span();
hi = elems.span;
t = ast.ty_tup(elems.node);
}
@ -208,19 +231,21 @@ impure fn parse_ty(parser p) -> @ast.ty {
token.RPAREN,
some(token.COMMA),
f, p);
hi = p.get_span();
hi = elems.span;
t = ast.ty_rec(elems.node);
}
case (token.MUTABLE) {
p.bump();
auto t0 = parse_ty(p);
hi = p.get_span();
hi = t0.span;
t = ast.ty_mutable(t0);
}
case (token.FN) {
t = parse_ty_fn(p);
auto flo = p.get_span();
p.bump();
t = parse_ty_fn(p, flo);
alt (t) {
case (ast.ty_fn(_, ?out)) {
hi = out.span;
@ -228,6 +253,10 @@ impure fn parse_ty(parser p) -> @ast.ty {
}
}
case (token.OBJ) {
t = parse_ty_obj(p, hi);
}
case (token.IDENT(_)) {
let ast.path pth = vec();
let bool more = true;