refactor: refactor identifier parsing somewhat
This commit is contained in:
parent
85123d2504
commit
c9ddb73184
5 changed files with 31 additions and 23 deletions
|
@ -336,7 +336,7 @@ parse_expected_identifier_found_reserved_keyword = expected identifier, found re
|
||||||
parse_expected_identifier_found_doc_comment = expected identifier, found doc comment
|
parse_expected_identifier_found_doc_comment = expected identifier, found doc comment
|
||||||
parse_expected_identifier = expected identifier
|
parse_expected_identifier = expected identifier
|
||||||
|
|
||||||
parse_sugg_escape_to_use_as_identifier = escape `{$ident_name}` to use it as an identifier
|
parse_sugg_escape_identifier = escape `{$ident_name}` to use it as an identifier
|
||||||
|
|
||||||
parse_sugg_remove_comma = remove this comma
|
parse_sugg_remove_comma = remove this comma
|
||||||
|
|
||||||
|
|
|
@ -888,12 +888,12 @@ pub(crate) struct InvalidMetaItem {
|
||||||
|
|
||||||
#[derive(Subdiagnostic)]
|
#[derive(Subdiagnostic)]
|
||||||
#[suggestion(
|
#[suggestion(
|
||||||
parse_sugg_escape_to_use_as_identifier,
|
parse_sugg_escape_identifier,
|
||||||
style = "verbose",
|
style = "verbose",
|
||||||
applicability = "maybe-incorrect",
|
applicability = "maybe-incorrect",
|
||||||
code = "r#"
|
code = "r#"
|
||||||
)]
|
)]
|
||||||
pub(crate) struct SuggEscapeToUseAsIdentifier {
|
pub(crate) struct SuggEscapeIdentifier {
|
||||||
#[primary_span]
|
#[primary_span]
|
||||||
pub span: Span,
|
pub span: Span,
|
||||||
pub ident_name: String,
|
pub ident_name: String,
|
||||||
|
@ -937,7 +937,7 @@ impl ExpectedIdentifierFound {
|
||||||
pub(crate) struct ExpectedIdentifier {
|
pub(crate) struct ExpectedIdentifier {
|
||||||
pub span: Span,
|
pub span: Span,
|
||||||
pub token: Token,
|
pub token: Token,
|
||||||
pub suggest_raw: Option<SuggEscapeToUseAsIdentifier>,
|
pub suggest_raw: Option<SuggEscapeIdentifier>,
|
||||||
pub suggest_remove_comma: Option<SuggRemoveComma>,
|
pub suggest_remove_comma: Option<SuggRemoveComma>,
|
||||||
pub help_cannot_start_number: Option<HelpIdentifierStartsWithNumber>,
|
pub help_cannot_start_number: Option<HelpIdentifierStartsWithNumber>,
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,14 +6,14 @@ use super::{
|
||||||
use crate::errors::{
|
use crate::errors::{
|
||||||
AmbiguousPlus, AttributeOnParamType, BadQPathStage2, BadTypePlus, BadTypePlusSub,
|
AmbiguousPlus, AttributeOnParamType, BadQPathStage2, BadTypePlus, BadTypePlusSub,
|
||||||
ComparisonOperatorsCannotBeChained, ComparisonOperatorsCannotBeChainedSugg,
|
ComparisonOperatorsCannotBeChained, ComparisonOperatorsCannotBeChainedSugg,
|
||||||
ConstGenericWithoutBraces, ConstGenericWithoutBracesSugg, DocCommentOnParamType,
|
ConstGenericWithoutBraces, ConstGenericWithoutBracesSugg, DocCommentDoesNotDocumentAnything,
|
||||||
DoubleColonInBound, ExpectedIdentifier, ExpectedSemi, ExpectedSemiSugg,
|
DocCommentOnParamType, DoubleColonInBound, ExpectedIdentifier, ExpectedSemi, ExpectedSemiSugg,
|
||||||
GenericParamsWithoutAngleBrackets, GenericParamsWithoutAngleBracketsSugg,
|
GenericParamsWithoutAngleBrackets, GenericParamsWithoutAngleBracketsSugg,
|
||||||
HelpIdentifierStartsWithNumber, InInTypo, IncorrectAwait, IncorrectSemicolon,
|
HelpIdentifierStartsWithNumber, InInTypo, IncorrectAwait, IncorrectSemicolon,
|
||||||
IncorrectUseOfAwait, ParenthesesInForHead, ParenthesesInForHeadSugg,
|
IncorrectUseOfAwait, ParenthesesInForHead, ParenthesesInForHeadSugg,
|
||||||
PatternMethodParamWithoutBody, QuestionMarkInType, QuestionMarkInTypeSugg, SelfParamNotFirst,
|
PatternMethodParamWithoutBody, QuestionMarkInType, QuestionMarkInTypeSugg, SelfParamNotFirst,
|
||||||
StructLiteralBodyWithoutPath, StructLiteralBodyWithoutPathSugg, StructLiteralNeedingParens,
|
StructLiteralBodyWithoutPath, StructLiteralBodyWithoutPathSugg, StructLiteralNeedingParens,
|
||||||
StructLiteralNeedingParensSugg, SuggEscapeToUseAsIdentifier, SuggRemoveComma,
|
StructLiteralNeedingParensSugg, SuggEscapeIdentifier, SuggRemoveComma,
|
||||||
UnexpectedConstInGenericParam, UnexpectedConstParamDeclaration,
|
UnexpectedConstInGenericParam, UnexpectedConstParamDeclaration,
|
||||||
UnexpectedConstParamDeclarationSugg, UnmatchedAngleBrackets, UseEqInstead,
|
UnexpectedConstParamDeclarationSugg, UnmatchedAngleBrackets, UseEqInstead,
|
||||||
};
|
};
|
||||||
|
@ -268,7 +268,16 @@ impl<'a> Parser<'a> {
|
||||||
self.sess.source_map().span_to_snippet(span)
|
self.sess.source_map().span_to_snippet(span)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Emits an error with suggestions if an identifier was expected but not found.
|
||||||
pub(super) fn expected_ident_found(&mut self) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
|
pub(super) fn expected_ident_found(&mut self) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
|
||||||
|
if let TokenKind::DocComment(..) = self.prev_token.kind {
|
||||||
|
return DocCommentDoesNotDocumentAnything {
|
||||||
|
span: self.prev_token.span,
|
||||||
|
missing_comma: None,
|
||||||
|
}
|
||||||
|
.into_diagnostic(&self.sess.span_diagnostic);
|
||||||
|
}
|
||||||
|
|
||||||
let valid_follow = &[
|
let valid_follow = &[
|
||||||
TokenKind::Eq,
|
TokenKind::Eq,
|
||||||
TokenKind::Colon,
|
TokenKind::Colon,
|
||||||
|
@ -286,7 +295,7 @@ impl<'a> Parser<'a> {
|
||||||
if ident.is_raw_guess()
|
if ident.is_raw_guess()
|
||||||
&& self.look_ahead(1, |t| valid_follow.contains(&t.kind)) =>
|
&& self.look_ahead(1, |t| valid_follow.contains(&t.kind)) =>
|
||||||
{
|
{
|
||||||
Some(SuggEscapeToUseAsIdentifier {
|
Some(SuggEscapeIdentifier {
|
||||||
span: ident.span.shrink_to_lo(),
|
span: ident.span.shrink_to_lo(),
|
||||||
// `Symbol::to_string()` is different from `Symbol::into_diagnostic_arg()`,
|
// `Symbol::to_string()` is different from `Symbol::into_diagnostic_arg()`,
|
||||||
// which uses `Symbol::to_ident_string()` and "helpfully" adds an implicit `r#`
|
// which uses `Symbol::to_ident_string()` and "helpfully" adds an implicit `r#`
|
||||||
|
|
|
@ -1744,7 +1744,7 @@ impl<'a> Parser<'a> {
|
||||||
/// Parses a field identifier. Specialized version of `parse_ident_common`
|
/// Parses a field identifier. Specialized version of `parse_ident_common`
|
||||||
/// for better diagnostics and suggestions.
|
/// for better diagnostics and suggestions.
|
||||||
fn parse_field_ident(&mut self, adt_ty: &str, lo: Span) -> PResult<'a, Ident> {
|
fn parse_field_ident(&mut self, adt_ty: &str, lo: Span) -> PResult<'a, Ident> {
|
||||||
let (ident, is_raw) = self.ident_or_err()?;
|
let (ident, is_raw) = self.ident_or_err(true)?;
|
||||||
if !is_raw && ident.is_reserved() {
|
if !is_raw && ident.is_reserved() {
|
||||||
let snapshot = self.create_snapshot_for_diagnostic();
|
let snapshot = self.create_snapshot_for_diagnostic();
|
||||||
let err = if self.check_fn_front_matter(false, Case::Sensitive) {
|
let err = if self.check_fn_front_matter(false, Case::Sensitive) {
|
||||||
|
|
|
@ -42,8 +42,7 @@ use thin_vec::ThinVec;
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
|
||||||
use crate::errors::{
|
use crate::errors::{
|
||||||
DocCommentDoesNotDocumentAnything, IncorrectVisibilityRestriction, MismatchedClosingDelimiter,
|
IncorrectVisibilityRestriction, MismatchedClosingDelimiter, NonStringAbiLiteral,
|
||||||
NonStringAbiLiteral,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
bitflags::bitflags! {
|
bitflags::bitflags! {
|
||||||
|
@ -552,19 +551,8 @@ impl<'a> Parser<'a> {
|
||||||
self.parse_ident_common(true)
|
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> {
|
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() {
|
if !is_raw && ident.is_reserved() {
|
||||||
let mut err = self.expected_ident_found();
|
let mut err = self.expected_ident_found();
|
||||||
if recover {
|
if recover {
|
||||||
|
@ -577,6 +565,17 @@ impl<'a> Parser<'a> {
|
||||||
Ok(ident)
|
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.
|
/// 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
|
/// This method will automatically add `tok` to `expected_tokens` if `tok` is not
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue