Skip to content

Commit 50ce605

Browse files
committed
Parse pub(restricted)
1 parent 0833a89 commit 50ce605

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ bitflags! {
7070
const RESTRICTION_STMT_EXPR = 1 << 0,
7171
const RESTRICTION_NO_STRUCT_LITERAL = 1 << 1,
7272
const NO_NONINLINE_MOD = 1 << 2,
73+
const ALLOW_MODULE_PATHS = 1 << 3,
7374
}
7475
}
7576

@@ -560,7 +561,9 @@ impl<'a> Parser<'a> {
560561
}
561562

562563
pub fn parse_ident(&mut self) -> PResult<'a, ast::Ident> {
563-
self.check_strict_keywords();
564+
if !self.restrictions.contains(Restrictions::ALLOW_MODULE_PATHS) {
565+
self.check_strict_keywords();
566+
}
564567
self.check_reserved_keywords();
565568
match self.token {
566569
token::Ident(i, _) => {
@@ -4938,7 +4941,7 @@ impl<'a> Parser<'a> {
49384941

49394942
let mut attrs = self.parse_outer_attributes()?;
49404943
let lo = self.span.lo;
4941-
let vis = self.parse_visibility()?;
4944+
let vis = self.parse_visibility(true)?;
49424945
let defaultness = self.parse_defaultness()?;
49434946
let (name, node) = if self.eat_keyword(keywords::Type) {
49444947
let name = self.parse_ident()?;
@@ -5250,7 +5253,7 @@ impl<'a> Parser<'a> {
52505253
|p| {
52515254
let attrs = p.parse_outer_attributes()?;
52525255
let lo = p.span.lo;
5253-
let vis = p.parse_visibility()?;
5256+
let vis = p.parse_visibility(false)?;
52545257
let ty = p.parse_ty_sum()?;
52555258
Ok(StructField {
52565259
span: mk_sp(lo, p.span.hi),
@@ -5290,20 +5293,26 @@ impl<'a> Parser<'a> {
52905293

52915294
/// Parse an element of a struct definition
52925295
fn parse_struct_decl_field(&mut self) -> PResult<'a, StructField> {
5293-
52945296
let attrs = self.parse_outer_attributes()?;
5295-
5296-
if self.eat_keyword(keywords::Pub) {
5297-
return self.parse_single_struct_field(Visibility::Public, attrs);
5298-
}
5299-
5300-
return self.parse_single_struct_field(Visibility::Inherited, attrs);
5297+
let vis = self.parse_visibility(true)?;
5298+
self.parse_single_struct_field(vis, attrs)
53015299
}
53025300

5303-
/// Parse visibility: PUB or nothing
5304-
fn parse_visibility(&mut self) -> PResult<'a, Visibility> {
5305-
if self.eat_keyword(keywords::Pub) { Ok(Visibility::Public) }
5306-
else { Ok(Visibility::Inherited) }
5301+
fn parse_visibility(&mut self, allow_restricted: bool) -> PResult<'a, Visibility> {
5302+
if !self.eat_keyword(keywords::Pub) {
5303+
Ok(Visibility::Inherited)
5304+
} else if !allow_restricted || !self.eat(&token::OpenDelim(token::Paren)) {
5305+
Ok(Visibility::Public)
5306+
} else if self.eat_keyword(keywords::Crate) {
5307+
let span = self.last_span;
5308+
self.expect(&token::CloseDelim(token::Paren))?;
5309+
Ok(Visibility::Crate(span))
5310+
} else {
5311+
let path = self.with_res(Restrictions::ALLOW_MODULE_PATHS,
5312+
|this| this.parse_path(NoTypesAllowed))?;
5313+
self.expect(&token::CloseDelim(token::Paren))?;
5314+
Ok(Visibility::Restricted { path: P(path), id: ast::DUMMY_NODE_ID })
5315+
}
53075316
}
53085317

53095318
/// Parse defaultness: DEFAULT or nothing
@@ -5765,7 +5774,7 @@ impl<'a> Parser<'a> {
57655774

57665775
let lo = self.span.lo;
57675776

5768-
let visibility = self.parse_visibility()?;
5777+
let visibility = self.parse_visibility(true)?;
57695778

57705779
if self.eat_keyword(keywords::Use) {
57715780
// USE ITEM
@@ -6015,7 +6024,7 @@ impl<'a> Parser<'a> {
60156024
fn parse_foreign_item(&mut self) -> PResult<'a, Option<ForeignItem>> {
60166025
let attrs = self.parse_outer_attributes()?;
60176026
let lo = self.span.lo;
6018-
let visibility = self.parse_visibility()?;
6027+
let visibility = self.parse_visibility(true)?;
60196028

60206029
if self.check_keyword(keywords::Static) {
60216030
// FOREIGN STATIC ITEM

0 commit comments

Comments
 (0)