Skip to content

Commit

Permalink
Start parsing pub/priv on regular items
Browse files Browse the repository at this point in the history
Issue #1893
  • Loading branch information
marijnh committed May 8, 2012
1 parent df3bf7c commit b619954
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 77 deletions.
15 changes: 8 additions & 7 deletions src/librustsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ enum ret_style {
type method = {ident: ident, attrs: [attribute],
tps: [ty_param], decl: fn_decl, body: blk,
id: node_id, span: span, self_id: node_id,
privacy: privacy}; // privacy is always public, unless it's a
vis: visibility}; // always public, unless it's a
// class method

#[auto_serialize]
Expand All @@ -580,7 +580,7 @@ type variant_arg = {ty: @ty, id: node_id};

#[auto_serialize]
type variant_ = {name: ident, attrs: [attribute], args: [variant_arg],
id: node_id, disr_expr: option<@expr>};
id: node_id, disr_expr: option<@expr>, vis: visibility};

#[auto_serialize]
type variant = spanned<variant_>;
Expand Down Expand Up @@ -641,9 +641,13 @@ type attribute_ = {style: attr_style, value: meta_item};
#[auto_serialize]
type iface_ref = {path: @path, id: node_id};

#[auto_serialize]
enum visibility { public, private }

#[auto_serialize]
type item = {ident: ident, attrs: [attribute],
id: node_id, node: item_, span: span};
id: node_id, node: item_,
vis: visibility, span: span};

#[auto_serialize]
enum region_param {
Expand Down Expand Up @@ -679,16 +683,13 @@ type class_member = spanned<class_member_>;

#[auto_serialize]
enum class_member_ {
instance_var(ident, @ty, class_mutability, node_id, privacy),
instance_var(ident, @ty, class_mutability, node_id, visibility),
class_method(@method)
}

#[auto_serialize]
enum class_mutability { class_mutable, class_immutable }

#[auto_serialize]
enum privacy { priv, pub }

#[auto_serialize]
type class_ctor = spanned<class_ctor_>;

Expand Down
16 changes: 8 additions & 8 deletions src/librustsyntax/ast_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,31 +276,31 @@ pure fn class_item_ident(ci: @class_member) -> ident {
}

type ivar = {ident: ident, ty: @ty, cm: class_mutability,
id: node_id, privacy: privacy};
id: node_id, vis: visibility};

fn public_methods(ms: [@method]) -> [@method] {
vec::filter(ms, {|m| alt m.privacy {
pub { true }
vec::filter(ms, {|m| alt m.vis {
public { true }
_ { false }}})
}

fn split_class_items(cs: [@class_member]) -> ([ivar], [@method]) {
let mut vs = [], ms = [];
for cs.each {|c|
alt c.node {
instance_var(i, t, cm, id, privacy) {
vs += [{ident: i, ty: t, cm: cm, id: id, privacy: privacy}];
instance_var(i, t, cm, id, vis) {
vs += [{ident: i, ty: t, cm: cm, id: id, vis: vis}];
}
class_method(m) { ms += [m]; }
}
};
(vs, ms)
}

pure fn class_member_privacy(ci: @class_member) -> privacy {
pure fn class_member_visibility(ci: @class_member) -> visibility {
alt ci.node {
instance_var(_, _, _, _, p) { p }
class_method(m) { m.privacy }
instance_var(_, _, _, _, vis) { vis }
class_method(m) { m.vis }
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/librustsyntax/ext/auto_serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ fn mk_ser_fn(cx: ext_ctxt, span: span, name: str, tps: [ast::ty_param],
constraints: []},
ser_tps,
ser_blk),
vis: ast::public,
span: span}
}

Expand Down Expand Up @@ -744,6 +745,7 @@ fn mk_deser_fn(cx: ext_ctxt, span: span, name: str, tps: [ast::ty_param],
constraints: []},
deser_tps,
deser_blk),
vis: ast::public,
span: span}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustsyntax/ext/qquote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ fn parse_stmt(p: parser) -> @ast::stmt {
}

fn parse_item(p: parser) -> @ast::item {
alt (parser::parse_item(p, [])) {
alt parser::parse_item(p, [], ast::public) {
some(item) { item }
none { fail "parse_item: parsing an item failed"; }
}
Expand Down
6 changes: 4 additions & 2 deletions src/librustsyntax/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ fn noop_fold_item(&&i: @item, fld: ast_fold) -> @item {
attrs: vec::map(i.attrs, fold_attribute),
id: fld.new_id(i.id),
node: fld.fold_item_underscore(i.node),
vis: i.vis,
span: fld.new_span(i.span)};
}

Expand Down Expand Up @@ -323,7 +324,7 @@ fn noop_fold_method(&&m: @method, fld: ast_fold) -> @method {
id: fld.new_id(m.id),
span: fld.new_span(m.span),
self_id: fld.new_id(m.self_id),
privacy: m.privacy};
vis: m.vis};
}


Expand Down Expand Up @@ -564,7 +565,8 @@ fn noop_fold_variant(v: variant_, fld: ast_fold) -> variant_ {
ret {name: v.name,
attrs: attrs,
args: args, id: fld.new_id(v.id),
disr_expr: de};
disr_expr: de,
vis: v.vis};
}

fn noop_fold_ident(&&i: ident, _fld: ast_fold) -> ident { ret i; }
Expand Down
3 changes: 2 additions & 1 deletion src/librustsyntax/parse/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ fn eval_crate_directive(cx: ctx, cdir: @ast::crate_directive, prefix: str,

let i =
parser::mk_item(p0, cdir.span.lo, cdir.span.hi, id,
ast::item_mod(m0), mod_attrs);
ast::item_mod(m0), ast::public, mod_attrs);
// Thread defids, chpos and byte_pos through the parsers
cx.sess.chpos = p0.reader.chpos;
cx.sess.byte_pos = cx.sess.byte_pos + p0.reader.pos;
Expand All @@ -126,6 +126,7 @@ fn eval_crate_directive(cx: ctx, cdir: @ast::crate_directive, prefix: str,
attrs: attrs + a0,
id: cx.sess.next_id,
node: ast::item_mod(m0),
vis: ast::public,
span: cdir.span};
cx.sess.next_id += 1;
items += [i];
Expand Down
55 changes: 34 additions & 21 deletions src/librustsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1485,7 +1485,7 @@ fn parse_let(p: parser) -> @ast::decl {
}

/* assumes "let" token has already been consumed */
fn parse_instance_var(p:parser, pr: ast::privacy) -> @ast::class_member {
fn parse_instance_var(p:parser, pr: ast::visibility) -> @ast::class_member {
let mut is_mutbl = ast::class_immutable;
let lo = p.span.lo;
if eat_keyword(p, "mut") {
Expand Down Expand Up @@ -1527,7 +1527,7 @@ fn parse_stmt(p: parser, +first_item_attrs: [ast::attribute]) -> @ast::stmt {

let item_attrs = first_item_attrs + item_attrs;

alt parse_item(p, item_attrs) {
alt parse_item(p, item_attrs, ast::public) {
some(i) {
let mut hi = i.span.hi;
let decl = @spanned(lo, hi, ast::decl_item(i));
Expand Down Expand Up @@ -1789,11 +1789,13 @@ fn parse_fn_header(p: parser) -> {ident: ast::ident, tps: [ast::ty_param]} {
}

fn mk_item(p: parser, lo: uint, hi: uint, +ident: ast::ident,
+node: ast::item_, +attrs: [ast::attribute]) -> @ast::item {
+node: ast::item_, vis: ast::visibility,
+attrs: [ast::attribute]) -> @ast::item {
ret @{ident: ident,
attrs: attrs,
id: p.get_id(),
node: node,
vis: vis,
span: mk_sp(lo, hi)};
}

Expand All @@ -1819,7 +1821,7 @@ fn parse_method_name(p: parser) -> ast::ident {
}
}

fn parse_method(p: parser, pr: ast::privacy) -> @ast::method {
fn parse_method(p: parser, pr: ast::visibility) -> @ast::method {
let attrs = parse_outer_attributes(p);
let lo = p.span.lo, pur = parse_fn_purity(p);
let ident = parse_method_name(p);
Expand All @@ -1829,7 +1831,7 @@ fn parse_method(p: parser, pr: ast::privacy) -> @ast::method {
let attrs = attrs + inner_attrs;
@{ident: ident, attrs: attrs, tps: tps, decl: decl, body: body,
id: p.get_id(), span: mk_sp(lo, body.span.hi),
self_id: p.get_id(), privacy: pr}
self_id: p.get_id(), vis: pr}
}

fn parse_item_iface(p: parser) -> item_info {
Expand Down Expand Up @@ -1877,7 +1879,7 @@ fn parse_item_impl(p: parser) -> item_info {
let ty = parse_ty(p, false);
let mut meths = [];
expect(p, token::LBRACE);
while !eat(p, token::RBRACE) { meths += [parse_method(p, ast::pub)]; }
while !eat(p, token::RBRACE) { meths += [parse_method(p, ast::public)]; }
(ident, ast::item_impl(tps, rp, ifce, ty, meths), none)
}

Expand Down Expand Up @@ -1978,15 +1980,15 @@ fn parse_item_class(p: parser) -> item_info {
}
}

fn parse_single_class_item(p: parser, privcy: ast::privacy)
fn parse_single_class_item(p: parser, vis: ast::visibility)
-> @ast::class_member {
if eat_keyword(p, "let") {
let a_var = parse_instance_var(p, privcy);
let a_var = parse_instance_var(p, vis);
expect(p, token::SEMI);
ret a_var;
}
else {
let m = parse_method(p, privcy);
let m = parse_method(p, vis);
ret @{node: ast::class_method(m), span: m.span};
}
}
Expand Down Expand Up @@ -2014,17 +2016,23 @@ fn parse_class_item(p:parser, class_name_with_tps: @ast::path)
expect(p, token::LBRACE);
let mut results = [];
while p.token != token::RBRACE {
results += [parse_single_class_item(p, ast::priv)];
results += [parse_single_class_item(p, ast::private)];
}
p.bump();
ret members(results);
}
else {
// Probably need to parse attrs
ret members([parse_single_class_item(p, ast::pub)]);
ret members([parse_single_class_item(p, ast::public)]);
}
}

fn parse_visibility(p: parser, def: ast::visibility) -> ast::visibility {
if eat_keyword(p, "pub") { ast::public }
else if eat_keyword(p, "priv") { ast::private }
else { def }
}

fn parse_mod_items(p: parser, term: token::token,
+first_item_attrs: [ast::attribute]) -> ast::_mod {
// Shouldn't be any view items since we've already parsed an item attr
Expand All @@ -2035,7 +2043,8 @@ fn parse_mod_items(p: parser, term: token::token,
let mut attrs = parse_outer_attributes(p);
if first { attrs = first_item_attrs + attrs; first = false; }
#debug["parse_mod_items: parse_item(attrs=%?)", attrs];
alt parse_item(p, attrs) {
let vis = parse_visibility(p, ast::private);
alt parse_item(p, attrs, vis) {
some(i) { items += [i]; }
_ {
p.fatal("expected item but found '" +
Expand Down Expand Up @@ -2154,7 +2163,7 @@ fn parse_region_param(p: parser) -> ast::region_param {
}
}

fn parse_item_enum(p: parser) -> item_info {
fn parse_item_enum(p: parser, default_vis: ast::visibility) -> item_info {
let id = parse_ident(p);
let rp = parse_region_param(p);
let ty_params = parse_ty_params(p);
Expand All @@ -2171,7 +2180,8 @@ fn parse_item_enum(p: parser) -> item_info {
attrs: [],
args: [{ty: ty, id: p.get_id()}],
id: p.get_id(),
disr_expr: none});
disr_expr: none,
vis: ast::public});
ret (id, ast::item_enum([variant], ty_params, rp), none);
}
expect(p, token::LBRACE);
Expand All @@ -2181,6 +2191,7 @@ fn parse_item_enum(p: parser) -> item_info {
while p.token != token::RBRACE {
let variant_attrs = parse_outer_attributes(p);
let vlo = p.span.lo;
let vis = parse_visibility(p, default_vis);
let ident = parse_value_ident(p);
let mut args = [], disr_expr = none;
if p.token == token::LPAREN {
Expand All @@ -2198,7 +2209,7 @@ fn parse_item_enum(p: parser) -> item_info {

let vr = {name: ident, attrs: variant_attrs,
args: args, id: p.get_id(),
disr_expr: disr_expr};
disr_expr: disr_expr, vis: vis};
variants += [spanned(vlo, p.last_span.hi, vr)];

if !eat(p, token::COMMA) { break; }
Expand Down Expand Up @@ -2241,7 +2252,8 @@ fn fn_expr_lookahead(tok: token::token) -> bool {
}
}

fn parse_item(p: parser, +attrs: [ast::attribute]) -> option<@ast::item> {
fn parse_item(p: parser, +attrs: [ast::attribute], vis: ast::visibility)
-> option<@ast::item> {
let lo = p.span.lo;
let (ident, item_, extra_attrs) = if eat_keyword(p, "const") {
parse_item_const(p)
Expand All @@ -2265,7 +2277,7 @@ fn parse_item(p: parser, +attrs: [ast::attribute]) -> option<@ast::item> {
} else if eat_keyword(p, "type") {
parse_item_type(p)
} else if eat_keyword(p, "enum") {
parse_item_enum(p)
parse_item_enum(p, vis)
} else if eat_keyword(p, "iface") {
parse_item_iface(p)
} else if eat_keyword(p, "impl") {
Expand All @@ -2275,10 +2287,11 @@ fn parse_item(p: parser, +attrs: [ast::attribute]) -> option<@ast::item> {
} else if eat_keyword(p, "class") {
parse_item_class(p)
} else { ret none; };
some(mk_item(p, lo, p.last_span.hi, ident, item_, alt extra_attrs {
some(as) { attrs + as }
none { attrs }
}))
some(mk_item(p, lo, p.last_span.hi, ident, item_, vis,
alt extra_attrs {
some(as) { attrs + as }
none { attrs }
}))
}

fn parse_use(p: parser) -> ast::view_item_ {
Expand Down
2 changes: 1 addition & 1 deletion src/librustsyntax/parse/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ fn contextual_keyword_table() -> hashmap<str, ()> {
"implements",
"move",
"of",
"priv",
"priv", "pub",
"self", "send", "static",
"to",
"use",
Expand Down
6 changes: 3 additions & 3 deletions src/librustsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,9 +533,9 @@ fn print_item(s: ps, &&item: @ast::item) {
*/
hardbreak_if_not_bol(s);
maybe_print_comment(s, ci.span.lo);
let pr = ast_util::class_member_privacy(ci);
let pr = ast_util::class_member_visibility(ci);
alt pr {
ast::priv {
ast::private {
head(s, "priv");
bopen(s);
hardbreak_if_not_bol(s);
Expand All @@ -559,7 +559,7 @@ fn print_item(s: ps, &&item: @ast::item) {
}
}
alt pr {
ast::priv { bclose(s, ci.span); }
ast::private { bclose(s, ci.span); }
_ {}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/rustc/front/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ fn mk_test_module(cx: test_ctxt) -> @ast::item {
attrs: [resolve_unexported_attr],
id: cx.sess.next_node_id(),
node: item_,
vis: ast::public,
span: dummy_sp()};

#debug("Synthetic test module:\n%s\n", pprust::item_to_str(@item));
Expand Down Expand Up @@ -233,6 +234,7 @@ fn mk_tests(cx: test_ctxt) -> @ast::item {
attrs: [],
id: cx.sess.next_node_id(),
node: item_,
vis: ast::public,
span: dummy_sp()};
ret @item;
}
Expand Down Expand Up @@ -415,6 +417,7 @@ fn mk_main(cx: test_ctxt) -> @ast::item {
attrs: [],
id: cx.sess.next_node_id(),
node: item_,
vis: ast::public,
span: dummy_sp()};
ret @item;
}
Expand Down
Loading

0 comments on commit b619954

Please sign in to comment.