Support ::crate in paths

This commit is contained in:
Vadim Petrochenkov
2017-11-04 23:56:45 +03:00
parent 33374fa9d0
commit 2e9b89ddc5
13 changed files with 150 additions and 41 deletions

View File

@@ -3916,6 +3916,10 @@ impl<'a> Parser<'a> {
self.look_ahead(1, |t| t.is_ident() && !t.is_reserved_ident())
}
fn is_crate_vis(&self) -> bool {
self.token.is_keyword(keywords::Crate) && self.look_ahead(1, |t| t != &token::ModSep)
}
fn eat_auto_trait(&mut self) -> bool {
if self.token.is_keyword(keywords::Auto)
&& self.look_ahead(1, |t| t.is_keyword(keywords::Trait))
@@ -4026,10 +4030,15 @@ impl<'a> Parser<'a> {
node: StmtKind::Item(macro_def),
span: lo.to(self.prev_span),
}
// Starts like a simple path, but not a union item.
// Starts like a simple path, but not a union item or item with `crate` visibility.
// Our goal here is to parse an arbitrary path `a::b::c` but not something that starts
// like a path (1 token), but it fact not a path.
// `union::b::c` - path, `union U { ... }` - not a path.
// `crate::b::c` - path, `crate struct S;` - not a path.
} else if self.token.is_path_start() &&
!self.token.is_qpath_start() &&
!self.is_union_item() {
!self.is_union_item() &&
!self.is_crate_vis() {
let pth = self.parse_path(PathStyle::Expr)?;
if !self.eat(&token::Not) {
@@ -5399,7 +5408,9 @@ impl<'a> Parser<'a> {
pub fn parse_visibility(&mut self, can_take_tuple: bool) -> PResult<'a, Visibility> {
maybe_whole!(self, NtVis, |x| x);
if self.eat_keyword(keywords::Crate) {
self.expected_tokens.push(TokenType::Keyword(keywords::Crate));
if self.is_crate_vis() {
self.bump(); // `crate`
return Ok(Visibility::Crate(self.prev_span, CrateSugar::JustCrate));
}