From 11c5c73d56406b1471b8f989eaa49a6c30aff7ad Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Tue, 14 Jun 2011 16:13:19 -0700 Subject: [PATCH] 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 --- src/comp/front/ast.rs | 17 ++++++++++++++++- src/comp/front/parser.rs | 24 ++++++++++++++++-------- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/comp/front/ast.rs b/src/comp/front/ast.rs index 9506829d1ae..4577e88457b 100644 --- a/src/comp/front/ast.rs +++ b/src/comp/front/ast.rs @@ -448,11 +448,26 @@ tag view_item_ { 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_]; tag item_ { item_const(ident, @ty, @expr, 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_ty(ident, @ty, vec[ty_param], def_id, ann); item_tag(ident, vec[variant], vec[ty_param], def_id, ann); diff --git a/src/comp/front/parser.rs b/src/comp/front/parser.rs index 4d6b29db561..b85da77709c 100644 --- a/src/comp/front/parser.rs +++ b/src/comp/front/parser.rs @@ -1912,7 +1912,7 @@ fn parse_item_const(&parser p) -> @ast::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 id = parse_ident(p); expect(p, token::LBRACE); @@ -2125,7 +2125,7 @@ tag parsed_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")) { 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] { - let vec[ast::meta_item] attrs = []; +fn parse_attributes(&parser p) -> vec[ast::attribute] { + let vec[ast::attribute] attrs = []; while (p.peek() == token::POUND) { - p.bump(); - expect(p, token::LBRACKET); - attrs += [*parse_meta_item(p)]; - expect(p, token::RBRACKET); + attrs += [parse_attribute(p)]; } 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 { auto lo = p.get_lo_pos(); auto ident = parse_ident(p);