Skip to content

Commit 8d79921

Browse files
committed
parser: Remove Parser::prev_token_kind
1 parent 4d1241f commit 8d79921

File tree

4 files changed

+25
-56
lines changed

4 files changed

+25
-56
lines changed

src/librustc_parse/parser/expr.rs

+12-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::pat::{GateOr, PARAM_EXPECTED};
22
use super::ty::{AllowPlus, RecoverQPath};
3-
use super::{BlockMode, Parser, PathStyle, PrevTokenKind, Restrictions, TokenType};
3+
use super::{BlockMode, Parser, PathStyle, Restrictions, TokenType};
44
use super::{SemiColonMode, SeqSep, TokenExpectType};
55
use crate::maybe_recover_from_interpolated_ty_qpath;
66

@@ -166,17 +166,10 @@ impl<'a> Parser<'a> {
166166

167167
self.expected_tokens.push(TokenType::Operator);
168168
while let Some(op) = self.check_assoc_op() {
169-
// Adjust the span for interpolated LHS to point to the `$lhs` token and not to what
170-
// it refers to. Interpolated identifiers are unwrapped early and never show up here
171-
// as `PrevTokenKind::Interpolated` so if LHS is a single identifier we always process
172-
// it as "interpolated", it doesn't change the answer for non-interpolated idents.
173-
let lhs_span = match (self.prev_token_kind, &lhs.kind) {
174-
(PrevTokenKind::Interpolated, _) => self.prev_span,
175-
(PrevTokenKind::Ident, &ExprKind::Path(None, ref path))
176-
if path.segments.len() == 1 =>
177-
{
178-
self.prev_span
179-
}
169+
// Adjust the span for interpolated LHS to point to the `$lhs` token
170+
// and not to what it refers to.
171+
let lhs_span = match self.unnormalized_prev_token().kind {
172+
TokenKind::Interpolated(..) => self.prev_span,
180173
_ => lhs.span,
181174
};
182175

@@ -535,11 +528,13 @@ impl<'a> Parser<'a> {
535528
expr: PResult<'a, P<Expr>>,
536529
) -> PResult<'a, (Span, P<Expr>)> {
537530
expr.map(|e| {
538-
if self.prev_token_kind == PrevTokenKind::Interpolated {
539-
(self.prev_span, e)
540-
} else {
541-
(e.span, e)
542-
}
531+
(
532+
match self.unnormalized_prev_token().kind {
533+
TokenKind::Interpolated(..) => self.prev_span,
534+
_ => e.span,
535+
},
536+
e,
537+
)
543538
})
544539
}
545540

src/librustc_parse/parser/mod.rs

+6-32
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,6 @@ macro_rules! maybe_recover_from_interpolated_ty_qpath {
8383
};
8484
}
8585

86-
#[derive(Debug, Clone, Copy, PartialEq)]
87-
enum PrevTokenKind {
88-
DocComment,
89-
Comma,
90-
Plus,
91-
Interpolated,
92-
Eof,
93-
Ident,
94-
BitOr,
95-
Other,
96-
}
97-
9886
#[derive(Clone)]
9987
pub struct Parser<'a> {
10088
pub sess: &'a ParseSess,
@@ -115,9 +103,6 @@ pub struct Parser<'a> {
115103
/// Preferable use is through the `unnormalized_prev_token()` getter.
116104
/// Use span from this token if you need to concatenate it with some neighbouring spans.
117105
unnormalized_prev_token: Option<Token>,
118-
/// Equivalent to `prev_token.kind` in simplified form.
119-
/// FIXME: Remove in favor of `(unnormalized_)prev_token().kind`.
120-
prev_token_kind: PrevTokenKind,
121106
/// Equivalent to `unnormalized_prev_token().span`.
122107
/// FIXME: Remove in favor of `(unnormalized_)prev_token().span`.
123108
pub prev_span: Span,
@@ -396,7 +381,6 @@ impl<'a> Parser<'a> {
396381
unnormalized_token: None,
397382
prev_token: Token::dummy(),
398383
unnormalized_prev_token: None,
399-
prev_token_kind: PrevTokenKind::Other,
400384
prev_span: DUMMY_SP,
401385
restrictions: Restrictions::empty(),
402386
recurse_into_file_modules,
@@ -523,10 +507,11 @@ impl<'a> Parser<'a> {
523507
self.bump();
524508
Ok(Ident::new(name, span))
525509
}
526-
_ => Err(if self.prev_token_kind == PrevTokenKind::DocComment {
527-
self.span_fatal_err(self.prev_span, Error::UselessDocComment)
528-
} else {
529-
self.expected_ident_found()
510+
_ => Err(match self.prev_token.kind {
511+
TokenKind::DocComment(..) => {
512+
self.span_fatal_err(self.prev_span, Error::UselessDocComment)
513+
}
514+
_ => self.expected_ident_found(),
530515
}),
531516
}
532517
}
@@ -908,7 +893,7 @@ impl<'a> Parser<'a> {
908893

909894
/// Advance the parser by one token.
910895
pub fn bump(&mut self) {
911-
if self.prev_token_kind == PrevTokenKind::Eof {
896+
if self.prev_token.kind == TokenKind::Eof {
912897
// Bumping after EOF is a bad sign, usually an infinite loop.
913898
let msg = "attempted to bump the parser past EOF (may be stuck in a loop)";
914899
self.span_bug(self.token.span, msg);
@@ -920,16 +905,6 @@ impl<'a> Parser<'a> {
920905
self.unnormalized_prev_token = self.unnormalized_token.take();
921906

922907
// Update fields derived from the previous token.
923-
self.prev_token_kind = match self.prev_token.kind {
924-
token::DocComment(..) => PrevTokenKind::DocComment,
925-
token::Comma => PrevTokenKind::Comma,
926-
token::BinOp(token::Plus) => PrevTokenKind::Plus,
927-
token::BinOp(token::Or) => PrevTokenKind::BitOr,
928-
token::Interpolated(..) => PrevTokenKind::Interpolated,
929-
token::Eof => PrevTokenKind::Eof,
930-
token::Ident(..) => PrevTokenKind::Ident,
931-
_ => PrevTokenKind::Other,
932-
};
933908
self.prev_span = self.unnormalized_prev_token().span;
934909

935910
self.expected_tokens.clear();
@@ -949,7 +924,6 @@ impl<'a> Parser<'a> {
949924
self.unnormalized_prev_token = self.unnormalized_token.take();
950925

951926
// Update fields derived from the previous token.
952-
self.prev_token_kind = PrevTokenKind::Other;
953927
self.prev_span = self.unnormalized_prev_token().span.with_hi(span.lo());
954928

955929
self.expected_tokens.clear();

src/librustc_parse/parser/stmt.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::diagnostics::Error;
22
use super::expr::LhsExpr;
33
use super::pat::GateOr;
44
use super::path::PathStyle;
5-
use super::{BlockMode, Parser, PrevTokenKind, Restrictions, SemiColonMode};
5+
use super::{BlockMode, Parser, Restrictions, SemiColonMode};
66
use crate::maybe_whole;
77
use crate::DirectoryOwnership;
88

@@ -190,7 +190,7 @@ impl<'a> Parser<'a> {
190190
/// Also error if the previous token was a doc comment.
191191
fn error_outer_attrs(&self, attrs: &[Attribute]) {
192192
if !attrs.is_empty() {
193-
if self.prev_token_kind == PrevTokenKind::DocComment {
193+
if matches!(self.prev_token.kind, TokenKind::DocComment(..)) {
194194
self.span_fatal_err(self.prev_span, Error::UselessDocComment).emit();
195195
} else if attrs.iter().any(|a| a.style == AttrStyle::Outer) {
196196
self.struct_span_err(self.token.span, "expected statement after outer attribute")

src/librustc_parse/parser/ty.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::item::ParamCfg;
2-
use super::{Parser, PathStyle, PrevTokenKind, TokenType};
2+
use super::{Parser, PathStyle, TokenType};
33

44
use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole};
55

@@ -14,7 +14,7 @@ use syntax::ast::{
1414
};
1515
use syntax::ast::{Mac, Mutability};
1616
use syntax::ptr::P;
17-
use syntax::token::{self, Token};
17+
use syntax::token::{self, Token, TokenKind};
1818

1919
/// Any `?` or `?const` modifiers that appear at the start of a bound.
2020
struct BoundModifiers {
@@ -196,7 +196,7 @@ impl<'a> Parser<'a> {
196196
let mut trailing_plus = false;
197197
let (ts, trailing) = self.parse_paren_comma_seq(|p| {
198198
let ty = p.parse_ty()?;
199-
trailing_plus = p.prev_token_kind == PrevTokenKind::Plus;
199+
trailing_plus = p.prev_token.kind == TokenKind::BinOp(token::Plus);
200200
Ok(ty)
201201
})?;
202202

@@ -317,7 +317,7 @@ impl<'a> Parser<'a> {
317317
fn parse_impl_ty(&mut self, impl_dyn_multi: &mut bool) -> PResult<'a, TyKind> {
318318
// Always parse bounds greedily for better error recovery.
319319
let bounds = self.parse_generic_bounds(None)?;
320-
*impl_dyn_multi = bounds.len() > 1 || self.prev_token_kind == PrevTokenKind::Plus;
320+
*impl_dyn_multi = bounds.len() > 1 || self.prev_token.kind == TokenKind::BinOp(token::Plus);
321321
Ok(TyKind::ImplTrait(ast::DUMMY_NODE_ID, bounds))
322322
}
323323

@@ -337,7 +337,7 @@ impl<'a> Parser<'a> {
337337
self.bump(); // `dyn`
338338
// Always parse bounds greedily for better error recovery.
339339
let bounds = self.parse_generic_bounds(None)?;
340-
*impl_dyn_multi = bounds.len() > 1 || self.prev_token_kind == PrevTokenKind::Plus;
340+
*impl_dyn_multi = bounds.len() > 1 || self.prev_token.kind == TokenKind::BinOp(token::Plus);
341341
Ok(TyKind::TraitObject(bounds, TraitObjectSyntax::Dyn))
342342
}
343343

0 commit comments

Comments
 (0)