Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parser: Remove Parser::prev_token_kind #69034

Merged
merged 1 commit into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions src/librustc_parse/parser/expr.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -166,17 +166,10 @@ impl<'a> Parser<'a> {

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,
};

Expand Down Expand Up @@ -535,11 +528,13 @@ impl<'a> Parser<'a> {
expr: PResult<'a, P<Expr>>,
) -> PResult<'a, (Span, P<Expr>)> {
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,
)
})
}

Expand Down
38 changes: 6 additions & 32 deletions src/librustc_parse/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<Token>,
/// 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,
Expand Down Expand Up @@ -396,7 +381,6 @@ impl<'a> Parser<'a> {
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,
Expand Down Expand Up @@ -523,10 +507,11 @@ impl<'a> Parser<'a> {
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(),
}),
}
}
Expand Down Expand Up @@ -908,7 +893,7 @@ impl<'a> Parser<'a> {

/// 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);
Expand All @@ -920,16 +905,6 @@ impl<'a> Parser<'a> {
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();
Expand All @@ -949,7 +924,6 @@ impl<'a> Parser<'a> {
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();
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_parse/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::diagnostics::Error;
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;

Expand Down Expand Up @@ -190,7 +190,7 @@ impl<'a> Parser<'a> {
/// 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")
Expand Down
10 changes: 5 additions & 5 deletions src/librustc_parse/parser/ty.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -14,7 +14,7 @@ use syntax::ast::{
};
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 {
Expand Down Expand Up @@ -196,7 +196,7 @@ impl<'a> Parser<'a> {
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)
})?;

Expand Down Expand Up @@ -317,7 +317,7 @@ impl<'a> Parser<'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))
}

Expand All @@ -337,7 +337,7 @@ impl<'a> Parser<'a> {
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))
}

Expand Down