allow parsing attributes on struct fields

This commit is contained in:
Erick Tryzelaar 2013-04-30 20:20:08 -07:00
parent c2e1f47955
commit 3e3e2f0025
6 changed files with 31 additions and 17 deletions

View file

@ -1158,6 +1158,7 @@ pub struct struct_field_ {
kind: struct_field_kind,
id: node_id,
ty: @Ty,
attrs: ~[attribute],
}
pub type struct_field = spanned<struct_field_>;

View file

@ -415,7 +415,8 @@ impl gen_init for protocol {
ast::struct_immutable,
ast::inherited),
id: cx.next_id(),
ty: fty
ty: fty,
attrs: ~[],
},
span: dummy_sp()
}

View file

@ -222,9 +222,12 @@ pub fn noop_fold_item(i: @item, fld: @ast_fold) -> Option<@item> {
fn noop_fold_struct_field(sf: @struct_field, fld: @ast_fold)
-> @struct_field {
let fold_attribute = |x| fold_attribute_(x, fld);
@spanned { node: ast::struct_field_ { kind: copy sf.node.kind,
id: sf.node.id,
ty: fld.fold_ty(sf.node.ty) },
ty: fld.fold_ty(sf.node.ty),
attrs: sf.node.attrs.map(|e| fold_attribute(*e)) },
span: sf.span }
}
@ -309,6 +312,7 @@ fn fold_struct_field(f: @struct_field, fld: @ast_fold) -> @struct_field {
kind: copy f.node.kind,
id: fld.new_id(f.node.id),
ty: fld.fold_ty(f.node.ty),
attrs: /* FIXME (#2543) */ copy f.node.attrs,
},
span: fld.new_span(f.span),
}
@ -757,6 +761,7 @@ impl ast_fold for AstFoldFns {
kind: copy sf.node.kind,
id: sf.node.id,
ty: (self as @ast_fold).fold_ty(sf.node.ty),
attrs: copy sf.node.attrs,
},
span: (self.new_span)(sf.span),
}

View file

@ -18,7 +18,7 @@ removed.
*/
use ast::{expr, expr_lit, lit_nil};
use ast::{expr, expr_lit, lit_nil, attribute};
use ast;
use codemap::{span, respan};
use parse::parser::Parser;
@ -282,13 +282,13 @@ pub impl Parser {
}
}
fn try_parse_obsolete_priv_section(&self) -> bool {
fn try_parse_obsolete_priv_section(&self, attrs: ~[attribute]) -> bool {
if self.is_keyword(&~"priv") && self.look_ahead(1) == token::LBRACE {
self.obsolete(copy *self.span, ObsoletePrivSection);
self.eat_keyword(&~"priv");
self.bump();
while *self.token != token::RBRACE {
self.parse_single_struct_field(ast::private);
self.parse_single_struct_field(ast::private, attrs);
}
self.bump();
true

View file

@ -2520,7 +2520,9 @@ pub impl Parser {
}
// parse a structure field
fn parse_name_and_ty(&self, pr: visibility) -> @struct_field {
fn parse_name_and_ty(&self,
pr: visibility,
attrs: ~[attribute]) -> @struct_field {
let mut is_mutbl = struct_immutable;
let lo = self.span.lo;
if self.eat_keyword(&~"mut") {
@ -2535,7 +2537,8 @@ pub impl Parser {
@spanned(lo, self.last_span.hi, ast::struct_field_ {
kind: named_field(name, is_mutbl, pr),
id: self.get_id(),
ty: ty
ty: ty,
attrs: attrs,
})
}
@ -3318,11 +3321,13 @@ pub impl Parser {
&token::RPAREN,
seq_sep_trailing_allowed(token::COMMA)
) |p| {
let attrs = self.parse_outer_attributes();
let lo = p.span.lo;
let struct_field_ = ast::struct_field_ {
kind: unnamed_field,
id: self.get_id(),
ty: p.parse_ty(false)
ty: p.parse_ty(false),
attrs: attrs,
};
@spanned(lo, p.span.hi, struct_field_)
};
@ -3359,12 +3364,14 @@ pub impl Parser {
}
// parse a structure field declaration
fn parse_single_struct_field(&self, vis: visibility) -> @struct_field {
fn parse_single_struct_field(&self,
vis: visibility,
attrs: ~[attribute]) -> @struct_field {
if self.eat_obsolete_ident("let") {
self.obsolete(*self.last_span, ObsoleteLet);
}
let a_var = self.parse_name_and_ty(vis);
let a_var = self.parse_name_and_ty(vis, attrs);
match *self.token {
token::SEMI => {
self.obsolete(copy *self.span, ObsoleteFieldTerminator);
@ -3390,26 +3397,25 @@ pub impl Parser {
// parse an element of a struct definition
fn parse_struct_decl_field(&self) -> ~[@struct_field] {
if self.try_parse_obsolete_priv_section() {
let attrs = self.parse_outer_attributes();
if self.try_parse_obsolete_priv_section(attrs) {
return ~[];
}
// Need this to parse comments on fields.
let _attrs = self.parse_outer_attributes();
if self.eat_keyword(&~"priv") {
return ~[self.parse_single_struct_field(private)]
return ~[self.parse_single_struct_field(private, attrs)]
}
if self.eat_keyword(&~"pub") {
return ~[self.parse_single_struct_field(public)];
return ~[self.parse_single_struct_field(public, attrs)];
}
if self.try_parse_obsolete_struct_ctor() {
return ~[];
}
return ~[self.parse_single_struct_field(inherited)];
return ~[self.parse_single_struct_field(inherited, attrs)];
}
// parse visiility: PUB, PRIV, or nothing

View file

@ -700,6 +700,7 @@ pub fn print_struct(s: @ps,
ast::named_field(ident, mutability, visibility) => {
hardbreak_if_not_bol(s);
maybe_print_comment(s, field.span.lo);
print_outer_attributes(s, field.node.attrs);
print_visibility(s, visibility);
if mutability == ast::struct_mutable {
word_nbsp(s, ~"mut");