parse: tweak parse_item_ for more reuse.

This commit is contained in:
Mazdak Farrokhzad 2020-02-23 11:52:57 +01:00
parent 62930d3151
commit e66a39bb65
2 changed files with 10 additions and 20 deletions

View File

@ -25,21 +25,15 @@ pub(super) type ItemInfo = (Ident, ItemKind);
impl<'a> Parser<'a> {
pub fn parse_item(&mut self) -> PResult<'a, Option<P<Item>>> {
self.parse_item_(|_| true).map(|i| i.map(P))
}
fn parse_item_(&mut self, req_name: ReqName) -> PResult<'a, Option<Item>> {
let attrs = self.parse_outer_attributes()?;
self.parse_item_(attrs, true, false)
self.parse_item_common(attrs, true, false, req_name)
}
pub(super) fn parse_item_(
&mut self,
attrs: Vec<Attribute>,
macros_allowed: bool,
attributes_allowed: bool,
) -> PResult<'a, Option<P<Item>>> {
let item = self.parse_item_common(attrs, macros_allowed, attributes_allowed, |_| true)?;
Ok(item.map(P))
}
fn parse_item_common(
pub(super) fn parse_item_common(
&mut self,
mut attrs: Vec<Attribute>,
mac_allowed: bool,
@ -653,9 +647,7 @@ impl<'a> Parser<'a> {
/// Parses associated items.
fn parse_assoc_item(&mut self, req_name: ReqName) -> PResult<'a, Option<Option<P<AssocItem>>>> {
let attrs = self.parse_outer_attributes()?;
let it = self.parse_item_common(attrs, true, false, req_name)?;
Ok(it.map(|Item { attrs, id, span, vis, ident, kind, tokens }| {
Ok(self.parse_item_(req_name)?.map(|Item { attrs, id, span, vis, ident, kind, tokens }| {
let kind = match kind {
ItemKind::Mac(a) => AssocItemKind::Macro(a),
ItemKind::Fn(a, b, c, d) => AssocItemKind::Fn(a, b, c, d),
@ -844,9 +836,7 @@ impl<'a> Parser<'a> {
pub fn parse_foreign_item(&mut self) -> PResult<'a, Option<Option<P<ForeignItem>>>> {
maybe_whole!(self, NtForeignItem, |item| Some(Some(item)));
let attrs = self.parse_outer_attributes()?;
let item = self.parse_item_common(attrs, true, false, |_| true)?;
Ok(item.map(|Item { attrs, id, span, vis, ident, kind, tokens }| {
Ok(self.parse_item_(|_| true)?.map(|Item { attrs, id, span, vis, ident, kind, tokens }| {
let kind = match kind {
ItemKind::Mac(a) => ForeignItemKind::Macro(a),
ItemKind::Fn(a, b, c, d) => ForeignItemKind::Fn(a, b, c, d),

View File

@ -81,11 +81,11 @@ impl<'a> Parser<'a> {
// FIXME: Bad copy of attrs
let old_directory_ownership =
mem::replace(&mut self.directory.ownership, DirectoryOwnership::UnownedViaBlock);
let item = self.parse_item_(attrs.clone(), false, true)?;
let item = self.parse_item_common(attrs.clone(), false, true, |_| true)?;
self.directory.ownership = old_directory_ownership;
if let Some(item) = item {
return Ok(Some(self.mk_stmt(lo.to(item.span), StmtKind::Item(item))));
return Ok(Some(self.mk_stmt(lo.to(item.span), StmtKind::Item(P(item)))));
}
// Do not attempt to parse an expression if we're done here.