refactor: refactor identifier parsing somewhat
This commit is contained in:
@@ -42,8 +42,7 @@ use thin_vec::ThinVec;
|
||||
use tracing::debug;
|
||||
|
||||
use crate::errors::{
|
||||
DocCommentDoesNotDocumentAnything, IncorrectVisibilityRestriction, MismatchedClosingDelimiter,
|
||||
NonStringAbiLiteral,
|
||||
IncorrectVisibilityRestriction, MismatchedClosingDelimiter, NonStringAbiLiteral,
|
||||
};
|
||||
|
||||
bitflags::bitflags! {
|
||||
@@ -552,19 +551,8 @@ impl<'a> Parser<'a> {
|
||||
self.parse_ident_common(true)
|
||||
}
|
||||
|
||||
fn ident_or_err(&mut self) -> PResult<'a, (Ident, /* is_raw */ bool)> {
|
||||
self.token.ident().ok_or_else(|| match self.prev_token.kind {
|
||||
TokenKind::DocComment(..) => DocCommentDoesNotDocumentAnything {
|
||||
span: self.prev_token.span,
|
||||
missing_comma: None,
|
||||
}
|
||||
.into_diagnostic(&self.sess.span_diagnostic),
|
||||
_ => self.expected_ident_found(),
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_ident_common(&mut self, recover: bool) -> PResult<'a, Ident> {
|
||||
let (ident, is_raw) = self.ident_or_err()?;
|
||||
let (ident, is_raw) = self.ident_or_err(recover)?;
|
||||
if !is_raw && ident.is_reserved() {
|
||||
let mut err = self.expected_ident_found();
|
||||
if recover {
|
||||
@@ -577,6 +565,17 @@ impl<'a> Parser<'a> {
|
||||
Ok(ident)
|
||||
}
|
||||
|
||||
fn ident_or_err(&mut self, _recover: bool) -> PResult<'a, (Ident, /* is_raw */ bool)> {
|
||||
let result = self.token.ident().ok_or_else(|| self.expected_ident_found());
|
||||
|
||||
let (ident, is_raw) = match result {
|
||||
Ok(ident) => ident,
|
||||
Err(err) => return Err(err),
|
||||
};
|
||||
|
||||
Ok((ident, is_raw))
|
||||
}
|
||||
|
||||
/// Checks if the next token is `tok`, and returns `true` if so.
|
||||
///
|
||||
/// This method will automatically add `tok` to `expected_tokens` if `tok` is not
|
||||
|
||||
Reference in New Issue
Block a user