diff options
| author | Tim Chevalier <chevalier@alum.wellesley.edu> | 2012-04-11 16:18:00 -0700 |
|---|---|---|
| committer | Tim Chevalier <chevalier@alum.wellesley.edu> | 2012-04-11 16:20:01 -0700 |
| commit | 5c12cd72f4774af8ca1c3c9c0115b4b1a72404bb (patch) | |
| tree | 6e6982510b2cb091ed01f3072841fadd18691460 /src/librustsyntax/parse | |
| parent | 9fda1578a219a8762fadddfd37c45abdd6a271a1 (diff) | |
| download | rust-5c12cd72f4774af8ca1c3c9c0115b4b1a72404bb.tar.gz rust-5c12cd72f4774af8ca1c3c9c0115b4b1a72404bb.zip | |
Allow classes to implement ifaces
Introduce syntax like:
iface animal { ... }
class cat implements animal { ... }
to allow classes to implement ifaces. Casting classes to ifaces
is *not* yet supported. ifaces that a class implements are not
yet included in metadata.
The syntax is subject to change, and may go away completely if we
decide to use duck typing to relate classes with ifaces (see
http://smallcultfollowing.com/babysteps/blog/2012/04/10/declared-vs-duckish-typing/ )
Diffstat (limited to 'src/librustsyntax/parse')
| -rw-r--r-- | src/librustsyntax/parse/parser.rs | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/src/librustsyntax/parse/parser.rs b/src/librustsyntax/parse/parser.rs index 01224845338..17a2c4e217f 100644 --- a/src/librustsyntax/parse/parser.rs +++ b/src/librustsyntax/parse/parser.rs @@ -2143,11 +2143,19 @@ fn ident_to_path_tys(p: parser, i: ast::ident, @spanned(s.lo, s.hi, p_) } +fn parse_iface_ref_list(p:parser) -> [ast::iface_ref] { + parse_seq_to_before_end(token::LBRACE, seq_sep(token::COMMA), + {|p| {path: parse_path(p), id: p.get_id()}}, p) +} + fn parse_item_class(p: parser, attrs: [ast::attribute]) -> @ast::item { let lo = p.last_span.lo; let class_name = parse_value_ident(p); let ty_params = parse_ty_params(p); let class_path = ident_to_path_tys(p, class_name, ty_params); + let ifaces : [ast::iface_ref] = if eat_word(p, "implements") + { parse_iface_ref_list(p) } + else { [] }; expect(p, token::LBRACE); let mut ms: [@ast::class_member] = []; let ctor_id = p.get_id(); @@ -2163,9 +2171,8 @@ fn parse_item_class(p: parser, attrs: [ast::attribute]) -> @ast::item { p.bump(); alt the_ctor { some((ct_d, ct_b, ct_s)) { - ret mk_item(p, lo, p.last_span.hi, - class_name, - ast::item_class(ty_params, ms, + ret mk_item(p, lo, p.last_span.hi, class_name, + ast::item_class(ty_params, ifaces, ms, {node: {id: ctor_id, self_id: p.get_id(), dec: ct_d, |
