rustc: Introduce an attribute type to the AST

Right now the only thing that it adds to meta_item is an indication of whether
the attribute was declared inside or outside the item, but I expect it will
become more useful.

Issue #487
This commit is contained in:
Brian Anderson 2011-06-14 16:13:19 -07:00
parent 0eefa5f9d3
commit 11c5c73d56
2 changed files with 32 additions and 9 deletions

View File

@ -448,11 +448,26 @@ tag view_item_ {
type obj_def_ids = rec(def_id ty, def_id ctor); type obj_def_ids = rec(def_id ty, def_id ctor);
// Meta-data associated with an item
type attribute = spanned[attribute_];
// Distinguishes between attributes that decorate items and attributes that
// are contained as statements within items. These two cases need to be
// distinguished for pretty-printing.
tag attr_style {
attr_outer;
attr_inner;
}
type attribute_ = rec(attr_style style,
meta_item value);
type item = spanned[item_]; type item = spanned[item_];
tag item_ { tag item_ {
item_const(ident, @ty, @expr, def_id, ann); item_const(ident, @ty, @expr, def_id, ann);
item_fn(ident, _fn, vec[ty_param], def_id, ann); item_fn(ident, _fn, vec[ty_param], def_id, ann);
item_mod(ident, _mod, vec[meta_item], def_id); item_mod(ident, _mod, vec[attribute], def_id);
item_native_mod(ident, native_mod, def_id); item_native_mod(ident, native_mod, def_id);
item_ty(ident, @ty, vec[ty_param], def_id, ann); item_ty(ident, @ty, vec[ty_param], def_id, ann);
item_tag(ident, vec[variant], vec[ty_param], def_id, ann); item_tag(ident, vec[variant], vec[ty_param], def_id, ann);

View File

@ -1912,7 +1912,7 @@ fn parse_item_const(&parser p) -> @ast::item {
ret @spanned(lo, hi, item); ret @spanned(lo, hi, item);
} }
fn parse_item_mod(&parser p, vec[ast::meta_item] attrs) -> @ast::item { fn parse_item_mod(&parser p, vec[ast::attribute] attrs) -> @ast::item {
auto lo = p.get_last_lo_pos(); auto lo = p.get_last_lo_pos();
auto id = parse_ident(p); auto id = parse_ident(p);
expect(p, token::LBRACE); expect(p, token::LBRACE);
@ -2125,7 +2125,7 @@ tag parsed_item {
fn_no_item; fn_no_item;
} }
fn parse_item(&parser p, vec[ast::meta_item] attrs) -> parsed_item { fn parse_item(&parser p, vec[ast::attribute] attrs) -> parsed_item {
if (eat_word(p, "const")) { if (eat_word(p, "const")) {
ret got_item(parse_item_const(p)); ret got_item(parse_item_const(p));
@ -2156,19 +2156,27 @@ fn parse_item(&parser p, vec[ast::meta_item] attrs) -> parsed_item {
} }
} }
fn parse_attributes(&parser p) -> vec[ast::meta_item] { fn parse_attributes(&parser p) -> vec[ast::attribute] {
let vec[ast::meta_item] attrs = []; let vec[ast::attribute] attrs = [];
while (p.peek() == token::POUND) { while (p.peek() == token::POUND) {
p.bump(); attrs += [parse_attribute(p)];
expect(p, token::LBRACKET);
attrs += [*parse_meta_item(p)];
expect(p, token::RBRACKET);
} }
ret attrs; ret attrs;
} }
fn parse_attribute(&parser p) -> ast::attribute {
auto lo = p.get_lo_pos();
expect(p, token::POUND);
expect(p, token::LBRACKET);
auto meta_item = parse_meta_item(p);
expect(p, token::RBRACKET);
auto hi = p.get_hi_pos();
ret spanned(lo, hi, rec(style = ast::attr_outer,
value = *meta_item));
}
fn parse_meta_item(&parser p) -> @ast::meta_item { fn parse_meta_item(&parser p) -> @ast::meta_item {
auto lo = p.get_lo_pos(); auto lo = p.get_lo_pos();
auto ident = parse_ident(p); auto ident = parse_ident(p);