Refactor away ParsePub
and make errors for unnecessary visibility qualifiers consistent
This commit is contained in:
parent
3029e0918d
commit
a904ba2dd2
1 changed files with 10 additions and 34 deletions
|
@ -99,13 +99,6 @@ pub enum BoundParsingMode {
|
||||||
Modified,
|
Modified,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `pub` should be parsed in struct fields and not parsed in variant fields
|
|
||||||
#[derive(Clone, Copy, PartialEq)]
|
|
||||||
pub enum ParsePub {
|
|
||||||
Yes,
|
|
||||||
No,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq)]
|
#[derive(Clone, Copy, PartialEq)]
|
||||||
pub enum SemiColonMode {
|
pub enum SemiColonMode {
|
||||||
Break,
|
Break,
|
||||||
|
@ -5093,20 +5086,17 @@ impl<'a> Parser<'a> {
|
||||||
VariantData::Unit(ast::DUMMY_NODE_ID)
|
VariantData::Unit(ast::DUMMY_NODE_ID)
|
||||||
} else {
|
} else {
|
||||||
// If we see: `struct Foo<T> where T: Copy { ... }`
|
// If we see: `struct Foo<T> where T: Copy { ... }`
|
||||||
VariantData::Struct(try!(self.parse_record_struct_body(ParsePub::Yes)),
|
VariantData::Struct(try!(self.parse_record_struct_body()), ast::DUMMY_NODE_ID)
|
||||||
ast::DUMMY_NODE_ID)
|
|
||||||
}
|
}
|
||||||
// No `where` so: `struct Foo<T>;`
|
// No `where` so: `struct Foo<T>;`
|
||||||
} else if self.eat(&token::Semi) {
|
} else if self.eat(&token::Semi) {
|
||||||
VariantData::Unit(ast::DUMMY_NODE_ID)
|
VariantData::Unit(ast::DUMMY_NODE_ID)
|
||||||
// Record-style struct definition
|
// Record-style struct definition
|
||||||
} else if self.token == token::OpenDelim(token::Brace) {
|
} else if self.token == token::OpenDelim(token::Brace) {
|
||||||
VariantData::Struct(try!(self.parse_record_struct_body(ParsePub::Yes)),
|
VariantData::Struct(try!(self.parse_record_struct_body()), ast::DUMMY_NODE_ID)
|
||||||
ast::DUMMY_NODE_ID)
|
|
||||||
// Tuple-style struct definition with optional where-clause.
|
// Tuple-style struct definition with optional where-clause.
|
||||||
} else if self.token == token::OpenDelim(token::Paren) {
|
} else if self.token == token::OpenDelim(token::Paren) {
|
||||||
let body = VariantData::Tuple(try!(self.parse_tuple_struct_body(ParsePub::Yes)),
|
let body = VariantData::Tuple(try!(self.parse_tuple_struct_body()), ast::DUMMY_NODE_ID);
|
||||||
ast::DUMMY_NODE_ID);
|
|
||||||
generics.where_clause = try!(self.parse_where_clause());
|
generics.where_clause = try!(self.parse_where_clause());
|
||||||
try!(self.expect(&token::Semi));
|
try!(self.expect(&token::Semi));
|
||||||
body
|
body
|
||||||
|
@ -5119,13 +5109,11 @@ impl<'a> Parser<'a> {
|
||||||
Ok((class_name, ItemKind::Struct(vdata, generics), None))
|
Ok((class_name, ItemKind::Struct(vdata, generics), None))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_record_struct_body(&mut self,
|
pub fn parse_record_struct_body(&mut self) -> PResult<'a, Vec<StructField>> {
|
||||||
parse_pub: ParsePub)
|
|
||||||
-> PResult<'a, Vec<StructField>> {
|
|
||||||
let mut fields = Vec::new();
|
let mut fields = Vec::new();
|
||||||
if self.eat(&token::OpenDelim(token::Brace)) {
|
if self.eat(&token::OpenDelim(token::Brace)) {
|
||||||
while self.token != token::CloseDelim(token::Brace) {
|
while self.token != token::CloseDelim(token::Brace) {
|
||||||
fields.push(try!(self.parse_struct_decl_field(parse_pub)));
|
fields.push(try!(self.parse_struct_decl_field()));
|
||||||
}
|
}
|
||||||
|
|
||||||
self.bump();
|
self.bump();
|
||||||
|
@ -5139,9 +5127,7 @@ impl<'a> Parser<'a> {
|
||||||
Ok(fields)
|
Ok(fields)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_tuple_struct_body(&mut self,
|
pub fn parse_tuple_struct_body(&mut self) -> PResult<'a, Vec<StructField>> {
|
||||||
parse_pub: ParsePub)
|
|
||||||
-> PResult<'a, Vec<StructField>> {
|
|
||||||
// This is the case where we find `struct Foo<T>(T) where T: Copy;`
|
// This is the case where we find `struct Foo<T>(T) where T: Copy;`
|
||||||
// Unit like structs are handled in parse_item_struct function
|
// Unit like structs are handled in parse_item_struct function
|
||||||
let fields = try!(self.parse_unspanned_seq(
|
let fields = try!(self.parse_unspanned_seq(
|
||||||
|
@ -5152,13 +5138,7 @@ impl<'a> Parser<'a> {
|
||||||
let attrs = try!(p.parse_outer_attributes());
|
let attrs = try!(p.parse_outer_attributes());
|
||||||
let lo = p.span.lo;
|
let lo = p.span.lo;
|
||||||
let struct_field_ = ast::StructField_ {
|
let struct_field_ = ast::StructField_ {
|
||||||
kind: UnnamedField (
|
kind: UnnamedField(try!(p.parse_visibility())),
|
||||||
if parse_pub == ParsePub::Yes {
|
|
||||||
try!(p.parse_visibility())
|
|
||||||
} else {
|
|
||||||
Visibility::Inherited
|
|
||||||
}
|
|
||||||
),
|
|
||||||
id: ast::DUMMY_NODE_ID,
|
id: ast::DUMMY_NODE_ID,
|
||||||
ty: try!(p.parse_ty_sum()),
|
ty: try!(p.parse_ty_sum()),
|
||||||
attrs: attrs,
|
attrs: attrs,
|
||||||
|
@ -5193,15 +5173,11 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse an element of a struct definition
|
/// Parse an element of a struct definition
|
||||||
fn parse_struct_decl_field(&mut self, parse_pub: ParsePub) -> PResult<'a, StructField> {
|
fn parse_struct_decl_field(&mut self) -> PResult<'a, StructField> {
|
||||||
|
|
||||||
let attrs = try!(self.parse_outer_attributes());
|
let attrs = try!(self.parse_outer_attributes());
|
||||||
|
|
||||||
if self.eat_keyword(keywords::Pub) {
|
if self.eat_keyword(keywords::Pub) {
|
||||||
if parse_pub == ParsePub::No {
|
|
||||||
let span = self.last_span;
|
|
||||||
self.span_err(span, "`pub` is not allowed here");
|
|
||||||
}
|
|
||||||
return self.parse_single_struct_field(Visibility::Public, attrs);
|
return self.parse_single_struct_field(Visibility::Public, attrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5567,11 +5543,11 @@ impl<'a> Parser<'a> {
|
||||||
if self.check(&token::OpenDelim(token::Brace)) {
|
if self.check(&token::OpenDelim(token::Brace)) {
|
||||||
// Parse a struct variant.
|
// Parse a struct variant.
|
||||||
all_nullary = false;
|
all_nullary = false;
|
||||||
struct_def = VariantData::Struct(try!(self.parse_record_struct_body(ParsePub::No)),
|
struct_def = VariantData::Struct(try!(self.parse_record_struct_body()),
|
||||||
ast::DUMMY_NODE_ID);
|
ast::DUMMY_NODE_ID);
|
||||||
} else if self.check(&token::OpenDelim(token::Paren)) {
|
} else if self.check(&token::OpenDelim(token::Paren)) {
|
||||||
all_nullary = false;
|
all_nullary = false;
|
||||||
struct_def = VariantData::Tuple(try!(self.parse_tuple_struct_body(ParsePub::No)),
|
struct_def = VariantData::Tuple(try!(self.parse_tuple_struct_body()),
|
||||||
ast::DUMMY_NODE_ID);
|
ast::DUMMY_NODE_ID);
|
||||||
} else if self.eat(&token::Eq) {
|
} else if self.eat(&token::Eq) {
|
||||||
disr_expr = Some(try!(self.parse_expr()));
|
disr_expr = Some(try!(self.parse_expr()));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue