Skip to content

Commit 11c5c73

Browse files
committed
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
1 parent 0eefa5f commit 11c5c73

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

src/comp/front/ast.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,11 +448,26 @@ tag view_item_ {
448448

449449
type obj_def_ids = rec(def_id ty, def_id ctor);
450450

451+
452+
// Meta-data associated with an item
453+
type attribute = spanned[attribute_];
454+
455+
// Distinguishes between attributes that decorate items and attributes that
456+
// are contained as statements within items. These two cases need to be
457+
// distinguished for pretty-printing.
458+
tag attr_style {
459+
attr_outer;
460+
attr_inner;
461+
}
462+
463+
type attribute_ = rec(attr_style style,
464+
meta_item value);
465+
451466
type item = spanned[item_];
452467
tag item_ {
453468
item_const(ident, @ty, @expr, def_id, ann);
454469
item_fn(ident, _fn, vec[ty_param], def_id, ann);
455-
item_mod(ident, _mod, vec[meta_item], def_id);
470+
item_mod(ident, _mod, vec[attribute], def_id);
456471
item_native_mod(ident, native_mod, def_id);
457472
item_ty(ident, @ty, vec[ty_param], def_id, ann);
458473
item_tag(ident, vec[variant], vec[ty_param], def_id, ann);

src/comp/front/parser.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,7 +1912,7 @@ fn parse_item_const(&parser p) -> @ast::item {
19121912
ret @spanned(lo, hi, item);
19131913
}
19141914

1915-
fn parse_item_mod(&parser p, vec[ast::meta_item] attrs) -> @ast::item {
1915+
fn parse_item_mod(&parser p, vec[ast::attribute] attrs) -> @ast::item {
19161916
auto lo = p.get_last_lo_pos();
19171917
auto id = parse_ident(p);
19181918
expect(p, token::LBRACE);
@@ -2125,7 +2125,7 @@ tag parsed_item {
21252125
fn_no_item;
21262126
}
21272127

2128-
fn parse_item(&parser p, vec[ast::meta_item] attrs) -> parsed_item {
2128+
fn parse_item(&parser p, vec[ast::attribute] attrs) -> parsed_item {
21292129

21302130
if (eat_word(p, "const")) {
21312131
ret got_item(parse_item_const(p));
@@ -2156,19 +2156,27 @@ fn parse_item(&parser p, vec[ast::meta_item] attrs) -> parsed_item {
21562156
}
21572157
}
21582158

2159-
fn parse_attributes(&parser p) -> vec[ast::meta_item] {
2160-
let vec[ast::meta_item] attrs = [];
2159+
fn parse_attributes(&parser p) -> vec[ast::attribute] {
2160+
let vec[ast::attribute] attrs = [];
21612161

21622162
while (p.peek() == token::POUND) {
2163-
p.bump();
2164-
expect(p, token::LBRACKET);
2165-
attrs += [*parse_meta_item(p)];
2166-
expect(p, token::RBRACKET);
2163+
attrs += [parse_attribute(p)];
21672164
}
21682165

21692166
ret attrs;
21702167
}
21712168

2169+
fn parse_attribute(&parser p) -> ast::attribute {
2170+
auto lo = p.get_lo_pos();
2171+
expect(p, token::POUND);
2172+
expect(p, token::LBRACKET);
2173+
auto meta_item = parse_meta_item(p);
2174+
expect(p, token::RBRACKET);
2175+
auto hi = p.get_hi_pos();
2176+
ret spanned(lo, hi, rec(style = ast::attr_outer,
2177+
value = *meta_item));
2178+
}
2179+
21722180
fn parse_meta_item(&parser p) -> @ast::meta_item {
21732181
auto lo = p.get_lo_pos();
21742182
auto ident = parse_ident(p);

0 commit comments

Comments
 (0)