Skip to content

Commit

Permalink
parser: make eat_macro_def redundant.
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Feb 13, 2020
1 parent c202603 commit fd64b3b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 33 deletions.
35 changes: 9 additions & 26 deletions src/librustc_parse/parser/item.rs
Expand Up @@ -86,7 +86,7 @@ impl<'a> Parser<'a> {
let vis = self.parse_visibility(FollowedByType::No)?;

if let Some((ident, kind)) = self.parse_item_kind(&mut attrs, macros_allowed, lo, &vis)? {
return Ok(Some(self.mk_item(lo.to(self.prev_span), ident, kind, vis, attrs)));
return Ok(Some(P(self.mk_item(lo, ident, kind, vis, attrs))));
}

// FAILURE TO PARSE ITEM
Expand Down Expand Up @@ -942,9 +942,7 @@ impl<'a> Parser<'a> {
}
self.unexpected()?
};

let span = lo.to(self.prev_span);
Ok(P(ast::ForeignItem { ident, attrs, kind, id: DUMMY_NODE_ID, span, vis, tokens: None }))
Ok(P(self.mk_item(lo, ident, kind, vis, attrs)))
}

/// Parses a static item from a foreign module.
Expand Down Expand Up @@ -1364,7 +1362,7 @@ impl<'a> Parser<'a> {
}

/// Is this unambiguously the start of a `macro_rules! foo` item defnition?
fn is_macro_rules_item(&mut self) -> bool {
pub(super) fn is_macro_rules_item(&mut self) -> bool {
self.check_keyword(sym::macro_rules)
&& self.look_ahead(1, |t| *t == token::Not)
&& self.look_ahead(2, |t| t.is_ident())
Expand All @@ -1385,22 +1383,6 @@ impl<'a> Parser<'a> {
Ok((ident, ItemKind::MacroDef(ast::MacroDef { body, legacy: true })))
}

pub(super) fn eat_macro_def(
&mut self,
attrs: &[Attribute],
vis: &Visibility,
lo: Span,
) -> PResult<'a, Option<P<Item>>> {
let (ident, kind) = if self.eat_keyword(kw::Macro) {
self.parse_item_decl_macro(lo)?
} else if self.is_macro_rules_item() {
self.parse_item_macro_rules(vis)?
} else {
return Ok(None);
};
Ok(Some(self.mk_item(lo.to(self.prev_span), ident, kind, vis.clone(), attrs.to_vec())))
}

fn complain_if_pub_macro(&self, vis: &VisibilityKind, sp: Span) {
match *vis {
VisibilityKind::Inherited => {}
Expand Down Expand Up @@ -1496,15 +1478,16 @@ impl<'a> Parser<'a> {
Ok(true)
}

fn mk_item(
fn mk_item<K>(
&self,
span: Span,
lo: Span,
ident: Ident,
kind: ItemKind,
kind: K,
vis: Visibility,
attrs: Vec<Attribute>,
) -> P<Item> {
P(Item { ident, attrs, id: DUMMY_NODE_ID, kind, vis, span, tokens: None })
) -> Item<K> {
let span = lo.to(self.prev_span);
Item { ident, attrs, id: DUMMY_NODE_ID, kind, vis, span, tokens: None }
}
}

Expand Down
10 changes: 3 additions & 7 deletions src/librustc_parse/parser/stmt.rs
Expand Up @@ -7,10 +7,10 @@ use crate::maybe_whole;
use crate::DirectoryOwnership;

use rustc_errors::{Applicability, PResult};
use rustc_span::source_map::{respan, BytePos, Span};
use rustc_span::source_map::{BytePos, Span};
use rustc_span::symbol::{kw, sym, Symbol};
use syntax::ast;
use syntax::ast::{AttrStyle, AttrVec, Attribute, Mac, MacStmtStyle, VisibilityKind};
use syntax::ast::{AttrStyle, AttrVec, Attribute, Mac, MacStmtStyle};
use syntax::ast::{Block, BlockCheckMode, Expr, ExprKind, Local, Stmt, StmtKind, DUMMY_NODE_ID};
use syntax::ptr::P;
use syntax::token::{self, TokenKind};
Expand Down Expand Up @@ -55,11 +55,6 @@ impl<'a> Parser<'a> {
return self.recover_stmt_local(lo, attrs.into(), msg, "let");
}

let mac_vis = respan(lo, VisibilityKind::Inherited);
if let Some(macro_def) = self.eat_macro_def(&attrs, &mac_vis, lo)? {
return Ok(Some(self.mk_stmt(lo.to(self.prev_span), StmtKind::Item(macro_def))));
}

// Starts like a simple path, being careful to avoid contextual keywords
// such as a union items, item with `crate` visibility or auto trait items.
// Our goal here is to parse an arbitrary path `a::b::c` but not something that starts
Expand All @@ -70,6 +65,7 @@ impl<'a> Parser<'a> {
&& !self.is_crate_vis() // `crate::b::c` - path, `crate struct S;` - not a path.
&& !self.is_auto_trait_item()
&& !self.is_async_fn()
&& !self.is_macro_rules_item()
{
let path = self.parse_path(PathStyle::Expr)?;

Expand Down

0 comments on commit fd64b3b

Please sign in to comment.