rustc_parse: Use Token::ident where possible
This commit is contained in:
parent
2cb0b8582e
commit
925e9a2188
7 changed files with 88 additions and 94 deletions
|
|
@ -192,17 +192,19 @@ impl<'a> Parser<'a> {
|
|||
TokenKind::CloseDelim(token::DelimToken::Brace),
|
||||
TokenKind::CloseDelim(token::DelimToken::Paren),
|
||||
];
|
||||
if let token::Ident(name, false) = self.normalized_token.kind {
|
||||
if Ident::new(name, self.normalized_token.span).is_raw_guess()
|
||||
&& self.look_ahead(1, |t| valid_follow.contains(&t.kind))
|
||||
match self.token.ident() {
|
||||
Some((ident, false))
|
||||
if ident.is_raw_guess()
|
||||
&& self.look_ahead(1, |t| valid_follow.contains(&t.kind)) =>
|
||||
{
|
||||
err.span_suggestion(
|
||||
self.normalized_token.span,
|
||||
ident.span,
|
||||
"you can escape reserved keywords to use them as identifiers",
|
||||
format!("r#{}", name),
|
||||
format!("r#{}", ident.name),
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
if let Some(token_descr) = super::token_descr_opt(&self.token) {
|
||||
err.span_label(self.token.span, format!("expected identifier, found {}", token_descr));
|
||||
|
|
|
|||
|
|
@ -97,9 +97,10 @@ impl<'a> Parser<'a> {
|
|||
fn parse_expr_catch_underscore(&mut self) -> PResult<'a, P<Expr>> {
|
||||
match self.parse_expr() {
|
||||
Ok(expr) => Ok(expr),
|
||||
Err(mut err) => match self.normalized_token.kind {
|
||||
token::Ident(name, false)
|
||||
if name == kw::Underscore && self.look_ahead(1, |t| t == &token::Comma) =>
|
||||
Err(mut err) => match self.token.ident() {
|
||||
Some((ident, false))
|
||||
if ident.name == kw::Underscore
|
||||
&& self.look_ahead(1, |t| t == &token::Comma) =>
|
||||
{
|
||||
// Special-case handling of `foo(_, _, _)`
|
||||
err.emit();
|
||||
|
|
@ -331,21 +332,19 @@ impl<'a> Parser<'a> {
|
|||
///
|
||||
/// Also performs recovery for `and` / `or` which are mistaken for `&&` and `||` respectively.
|
||||
fn check_assoc_op(&self) -> Option<Spanned<AssocOp>> {
|
||||
Some(Spanned {
|
||||
node: match (AssocOp::from_token(&self.token), &self.normalized_token.kind) {
|
||||
(Some(op), _) => op,
|
||||
(None, token::Ident(sym::and, false)) => {
|
||||
self.error_bad_logical_op("and", "&&", "conjunction");
|
||||
AssocOp::LAnd
|
||||
}
|
||||
(None, token::Ident(sym::or, false)) => {
|
||||
self.error_bad_logical_op("or", "||", "disjunction");
|
||||
AssocOp::LOr
|
||||
}
|
||||
_ => return None,
|
||||
},
|
||||
span: self.normalized_token.span,
|
||||
})
|
||||
let (op, span) = match (AssocOp::from_token(&self.token), self.token.ident()) {
|
||||
(Some(op), _) => (op, self.token.span),
|
||||
(None, Some((ident, false))) if ident.name == sym::and => {
|
||||
self.error_bad_logical_op("and", "&&", "conjunction");
|
||||
(AssocOp::LAnd, ident.span)
|
||||
}
|
||||
(None, Some((ident, false))) if ident.name == sym::or => {
|
||||
self.error_bad_logical_op("or", "||", "disjunction");
|
||||
(AssocOp::LOr, ident.span)
|
||||
}
|
||||
_ => return None,
|
||||
};
|
||||
Some(source_map::respan(span, op))
|
||||
}
|
||||
|
||||
/// Error on `and` and `or` suggesting `&&` and `||` respectively.
|
||||
|
|
@ -1907,20 +1906,23 @@ impl<'a> Parser<'a> {
|
|||
|
||||
/// Use in case of error after field-looking code: `S { foo: () with a }`.
|
||||
fn find_struct_error_after_field_looking_code(&self) -> Option<Field> {
|
||||
if let token::Ident(name, _) = self.normalized_token.kind {
|
||||
if !self.token.is_reserved_ident() && self.look_ahead(1, |t| *t == token::Colon) {
|
||||
return Some(ast::Field {
|
||||
ident: Ident::new(name, self.normalized_token.span),
|
||||
match self.token.ident() {
|
||||
Some((ident, is_raw))
|
||||
if (is_raw || !ident.is_reserved())
|
||||
&& self.look_ahead(1, |t| *t == token::Colon) =>
|
||||
{
|
||||
Some(ast::Field {
|
||||
ident,
|
||||
span: self.token.span,
|
||||
expr: self.mk_expr_err(self.token.span),
|
||||
is_shorthand: false,
|
||||
attrs: AttrVec::new(),
|
||||
id: DUMMY_NODE_ID,
|
||||
is_placeholder: false,
|
||||
});
|
||||
})
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn recover_struct_comma_after_dotdot(&mut self, span: Span) {
|
||||
|
|
|
|||
|
|
@ -750,10 +750,10 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
|
||||
fn parse_ident_or_underscore(&mut self) -> PResult<'a, ast::Ident> {
|
||||
match self.normalized_token.kind {
|
||||
token::Ident(name @ kw::Underscore, false) => {
|
||||
match self.token.ident() {
|
||||
Some((ident, false)) if ident.name == kw::Underscore => {
|
||||
self.bump();
|
||||
Ok(Ident::new(name, self.normalized_prev_token.span))
|
||||
Ok(ident)
|
||||
}
|
||||
_ => self.parse_ident(),
|
||||
}
|
||||
|
|
@ -1609,15 +1609,12 @@ impl<'a> Parser<'a> {
|
|||
/// Returns the parsed optional self parameter and whether a self shortcut was used.
|
||||
fn parse_self_param(&mut self) -> PResult<'a, Option<Param>> {
|
||||
// Extract an identifier *after* having confirmed that the token is one.
|
||||
let expect_self_ident = |this: &mut Self| {
|
||||
match this.normalized_token.kind {
|
||||
// Preserve hygienic context.
|
||||
token::Ident(name, _) => {
|
||||
this.bump();
|
||||
Ident::new(name, this.normalized_prev_token.span)
|
||||
}
|
||||
_ => unreachable!(),
|
||||
let expect_self_ident = |this: &mut Self| match this.token.ident() {
|
||||
Some((ident, false)) => {
|
||||
this.bump();
|
||||
ident
|
||||
}
|
||||
_ => unreachable!(),
|
||||
};
|
||||
// Is `self` `n` tokens ahead?
|
||||
let is_isolated_self = |this: &Self, n| {
|
||||
|
|
|
|||
|
|
@ -480,9 +480,9 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
|
||||
fn parse_ident_common(&mut self, recover: bool) -> PResult<'a, ast::Ident> {
|
||||
match self.normalized_token.kind {
|
||||
token::Ident(name, _) => {
|
||||
if self.token.is_reserved_ident() {
|
||||
match self.token.ident() {
|
||||
Some((ident, is_raw)) => {
|
||||
if !is_raw && ident.is_reserved() {
|
||||
let mut err = self.expected_ident_found();
|
||||
if recover {
|
||||
err.emit();
|
||||
|
|
@ -491,7 +491,7 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
}
|
||||
self.bump();
|
||||
Ok(Ident::new(name, self.normalized_prev_token.span))
|
||||
Ok(ident)
|
||||
}
|
||||
_ => Err(match self.prev_token.kind {
|
||||
TokenKind::DocComment(..) => {
|
||||
|
|
|
|||
|
|
@ -240,10 +240,10 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
|
||||
pub(super) fn parse_path_segment_ident(&mut self) -> PResult<'a, Ident> {
|
||||
match self.normalized_token.kind {
|
||||
token::Ident(name, _) if name.is_path_segment_keyword() => {
|
||||
match self.token.ident() {
|
||||
Some((ident, false)) if ident.is_path_segment_keyword() => {
|
||||
self.bump();
|
||||
Ok(Ident::new(name, self.normalized_prev_token.span))
|
||||
Ok(ident)
|
||||
}
|
||||
_ => self.parse_ident(),
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue