From 8d799218ab29793b0ac08518c77cfabc8629c001 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Mon, 10 Feb 2020 20:20:01 +0300 Subject: [PATCH] parser: Remove `Parser::prev_token_kind` --- src/librustc_parse/parser/expr.rs | 29 ++++++++++------------- src/librustc_parse/parser/mod.rs | 38 +++++-------------------------- src/librustc_parse/parser/stmt.rs | 4 ++-- src/librustc_parse/parser/ty.rs | 10 ++++---- 4 files changed, 25 insertions(+), 56 deletions(-) diff --git a/src/librustc_parse/parser/expr.rs b/src/librustc_parse/parser/expr.rs index d9832141695..c136ffaafe7 100644 --- a/src/librustc_parse/parser/expr.rs +++ b/src/librustc_parse/parser/expr.rs @@ -1,6 +1,6 @@ use super::pat::{GateOr, PARAM_EXPECTED}; use super::ty::{AllowPlus, RecoverQPath}; -use super::{BlockMode, Parser, PathStyle, PrevTokenKind, Restrictions, TokenType}; +use super::{BlockMode, Parser, PathStyle, Restrictions, TokenType}; use super::{SemiColonMode, SeqSep, TokenExpectType}; use crate::maybe_recover_from_interpolated_ty_qpath; @@ -166,17 +166,10 @@ pub(super) fn parse_assoc_expr_with( self.expected_tokens.push(TokenType::Operator); while let Some(op) = self.check_assoc_op() { - // Adjust the span for interpolated LHS to point to the `$lhs` token and not to what - // it refers to. Interpolated identifiers are unwrapped early and never show up here - // as `PrevTokenKind::Interpolated` so if LHS is a single identifier we always process - // it as "interpolated", it doesn't change the answer for non-interpolated idents. - let lhs_span = match (self.prev_token_kind, &lhs.kind) { - (PrevTokenKind::Interpolated, _) => self.prev_span, - (PrevTokenKind::Ident, &ExprKind::Path(None, ref path)) - if path.segments.len() == 1 => - { - self.prev_span - } + // Adjust the span for interpolated LHS to point to the `$lhs` token + // and not to what it refers to. + let lhs_span = match self.unnormalized_prev_token().kind { + TokenKind::Interpolated(..) => self.prev_span, _ => lhs.span, }; @@ -535,11 +528,13 @@ fn interpolated_or_expr_span( expr: PResult<'a, P>, ) -> PResult<'a, (Span, P)> { expr.map(|e| { - if self.prev_token_kind == PrevTokenKind::Interpolated { - (self.prev_span, e) - } else { - (e.span, e) - } + ( + match self.unnormalized_prev_token().kind { + TokenKind::Interpolated(..) => self.prev_span, + _ => e.span, + }, + e, + ) }) } diff --git a/src/librustc_parse/parser/mod.rs b/src/librustc_parse/parser/mod.rs index 7131eb1144e..485e348365a 100644 --- a/src/librustc_parse/parser/mod.rs +++ b/src/librustc_parse/parser/mod.rs @@ -83,18 +83,6 @@ macro_rules! maybe_recover_from_interpolated_ty_qpath { }; } -#[derive(Debug, Clone, Copy, PartialEq)] -enum PrevTokenKind { - DocComment, - Comma, - Plus, - Interpolated, - Eof, - Ident, - BitOr, - Other, -} - #[derive(Clone)] pub struct Parser<'a> { pub sess: &'a ParseSess, @@ -115,9 +103,6 @@ pub struct Parser<'a> { /// Preferable use is through the `unnormalized_prev_token()` getter. /// Use span from this token if you need to concatenate it with some neighbouring spans. unnormalized_prev_token: Option, - /// Equivalent to `prev_token.kind` in simplified form. - /// FIXME: Remove in favor of `(unnormalized_)prev_token().kind`. - prev_token_kind: PrevTokenKind, /// Equivalent to `unnormalized_prev_token().span`. /// FIXME: Remove in favor of `(unnormalized_)prev_token().span`. pub prev_span: Span, @@ -396,7 +381,6 @@ pub fn new( unnormalized_token: None, prev_token: Token::dummy(), unnormalized_prev_token: None, - prev_token_kind: PrevTokenKind::Other, prev_span: DUMMY_SP, restrictions: Restrictions::empty(), recurse_into_file_modules, @@ -523,10 +507,11 @@ fn parse_ident_common(&mut self, recover: bool) -> PResult<'a, ast::Ident> { self.bump(); Ok(Ident::new(name, span)) } - _ => Err(if self.prev_token_kind == PrevTokenKind::DocComment { - self.span_fatal_err(self.prev_span, Error::UselessDocComment) - } else { - self.expected_ident_found() + _ => Err(match self.prev_token.kind { + TokenKind::DocComment(..) => { + self.span_fatal_err(self.prev_span, Error::UselessDocComment) + } + _ => self.expected_ident_found(), }), } } @@ -908,7 +893,7 @@ fn parse_paren_comma_seq( /// Advance the parser by one token. pub fn bump(&mut self) { - if self.prev_token_kind == PrevTokenKind::Eof { + if self.prev_token.kind == TokenKind::Eof { // Bumping after EOF is a bad sign, usually an infinite loop. let msg = "attempted to bump the parser past EOF (may be stuck in a loop)"; self.span_bug(self.token.span, msg); @@ -920,16 +905,6 @@ pub fn bump(&mut self) { self.unnormalized_prev_token = self.unnormalized_token.take(); // Update fields derived from the previous token. - self.prev_token_kind = match self.prev_token.kind { - token::DocComment(..) => PrevTokenKind::DocComment, - token::Comma => PrevTokenKind::Comma, - token::BinOp(token::Plus) => PrevTokenKind::Plus, - token::BinOp(token::Or) => PrevTokenKind::BitOr, - token::Interpolated(..) => PrevTokenKind::Interpolated, - token::Eof => PrevTokenKind::Eof, - token::Ident(..) => PrevTokenKind::Ident, - _ => PrevTokenKind::Other, - }; self.prev_span = self.unnormalized_prev_token().span; self.expected_tokens.clear(); @@ -949,7 +924,6 @@ fn bump_with(&mut self, next: TokenKind, span: Span) { self.unnormalized_prev_token = self.unnormalized_token.take(); // Update fields derived from the previous token. - self.prev_token_kind = PrevTokenKind::Other; self.prev_span = self.unnormalized_prev_token().span.with_hi(span.lo()); self.expected_tokens.clear(); diff --git a/src/librustc_parse/parser/stmt.rs b/src/librustc_parse/parser/stmt.rs index f3a69729399..e11cdd5dadb 100644 --- a/src/librustc_parse/parser/stmt.rs +++ b/src/librustc_parse/parser/stmt.rs @@ -2,7 +2,7 @@ use super::expr::LhsExpr; use super::pat::GateOr; use super::path::PathStyle; -use super::{BlockMode, Parser, PrevTokenKind, Restrictions, SemiColonMode}; +use super::{BlockMode, Parser, Restrictions, SemiColonMode}; use crate::maybe_whole; use crate::DirectoryOwnership; @@ -190,7 +190,7 @@ fn parse_stmt_mac( /// Also error if the previous token was a doc comment. fn error_outer_attrs(&self, attrs: &[Attribute]) { if !attrs.is_empty() { - if self.prev_token_kind == PrevTokenKind::DocComment { + if matches!(self.prev_token.kind, TokenKind::DocComment(..)) { self.span_fatal_err(self.prev_span, Error::UselessDocComment).emit(); } else if attrs.iter().any(|a| a.style == AttrStyle::Outer) { self.struct_span_err(self.token.span, "expected statement after outer attribute") diff --git a/src/librustc_parse/parser/ty.rs b/src/librustc_parse/parser/ty.rs index 990661bf6b5..ed5c3dcbe40 100644 --- a/src/librustc_parse/parser/ty.rs +++ b/src/librustc_parse/parser/ty.rs @@ -1,5 +1,5 @@ use super::item::ParamCfg; -use super::{Parser, PathStyle, PrevTokenKind, TokenType}; +use super::{Parser, PathStyle, TokenType}; use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole}; @@ -14,7 +14,7 @@ }; use syntax::ast::{Mac, Mutability}; use syntax::ptr::P; -use syntax::token::{self, Token}; +use syntax::token::{self, Token, TokenKind}; /// Any `?` or `?const` modifiers that appear at the start of a bound. struct BoundModifiers { @@ -196,7 +196,7 @@ fn parse_ty_tuple_or_parens(&mut self, lo: Span, allow_plus: AllowPlus) -> PResu let mut trailing_plus = false; let (ts, trailing) = self.parse_paren_comma_seq(|p| { let ty = p.parse_ty()?; - trailing_plus = p.prev_token_kind == PrevTokenKind::Plus; + trailing_plus = p.prev_token.kind == TokenKind::BinOp(token::Plus); Ok(ty) })?; @@ -317,7 +317,7 @@ fn parse_ty_bare_fn(&mut self, generic_params: Vec) -> PResult<'a, fn parse_impl_ty(&mut self, impl_dyn_multi: &mut bool) -> PResult<'a, TyKind> { // Always parse bounds greedily for better error recovery. let bounds = self.parse_generic_bounds(None)?; - *impl_dyn_multi = bounds.len() > 1 || self.prev_token_kind == PrevTokenKind::Plus; + *impl_dyn_multi = bounds.len() > 1 || self.prev_token.kind == TokenKind::BinOp(token::Plus); Ok(TyKind::ImplTrait(ast::DUMMY_NODE_ID, bounds)) } @@ -337,7 +337,7 @@ fn parse_dyn_ty(&mut self, impl_dyn_multi: &mut bool) -> PResult<'a, TyKind> { self.bump(); // `dyn` // Always parse bounds greedily for better error recovery. let bounds = self.parse_generic_bounds(None)?; - *impl_dyn_multi = bounds.len() > 1 || self.prev_token_kind == PrevTokenKind::Plus; + *impl_dyn_multi = bounds.len() > 1 || self.prev_token.kind == TokenKind::BinOp(token::Plus); Ok(TyKind::TraitObject(bounds, TraitObjectSyntax::Dyn)) }