From f688f8aedffcd802012b355c182dafbdf5e819f5 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Mon, 30 Sep 2019 01:57:50 +0200 Subject: [PATCH] syntax refactor `parse_self_param` (2) --- src/libsyntax/parse/parser.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 93de890b222..70a7b4b01d8 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -1264,17 +1264,22 @@ fn is_isolated_self(&self, n: usize) -> bool { && self.look_ahead(n + 1, |t| t != &token::ModSep) } + fn expect_self_ident(&mut self) -> Ident { + match self.token.kind { + // Preserve hygienic context. + token::Ident(name, _) => { + let span = self.token.span; + self.bump(); + Ident::new(name, span) + } + _ => unreachable!(), + } + } + /// Returns the parsed optional self parameter and whether a self shortcut was used. /// /// See `parse_self_param_with_attrs` to collect attributes. fn parse_self_param(&mut self) -> PResult<'a, Option> { - let expect_ident = |this: &mut Self| match this.token.kind { - // Preserve hygienic context. - token::Ident(name, _) => - { let span = this.token.span; this.bump(); Ident::new(name, span) } - _ => unreachable!() - }; - // Parse optional `self` parameter of a method. // Only a limited set of initial token sequences is considered `self` parameters; anything // else is parsed as a normal function parameter list, so some lookahead is required. @@ -1308,7 +1313,7 @@ fn parse_self_param(&mut self) -> PResult<'a, Option> { SelfKind::Region(Some(lt), Mutability::Mutable) } else { return Ok(None); - }, expect_ident(self), self.prev_span) + }, self.expect_self_ident(), self.prev_span) } token::BinOp(token::Star) => { // `*self` @@ -1333,13 +1338,13 @@ fn parse_self_param(&mut self) -> PResult<'a, Option> { SelfKind::Value(Mutability::Immutable) } else { return Ok(None); - }, expect_ident(self), self.prev_span) + }, self.expect_self_ident(), self.prev_span) } token::Ident(..) => { if self.is_isolated_self(0) { // `self` // `self: TYPE` - let eself_ident = expect_ident(self); + let eself_ident = self.expect_self_ident(); let eself_hi = self.prev_span; (if self.eat(&token::Colon) { let ty = self.parse_ty()?; @@ -1352,7 +1357,7 @@ fn parse_self_param(&mut self) -> PResult<'a, Option> { // `mut self` // `mut self: TYPE` self.bump(); - let eself_ident = expect_ident(self); + let eself_ident = self.expect_self_ident(); let eself_hi = self.prev_span; (if self.eat(&token::Colon) { let ty = self.parse_ty()?;