2020-07-27 14:04:54 +02:00
|
|
|
use rustc_ast::ptr::P;
|
2022-04-26 15:40:14 +03:00
|
|
|
use rustc_ast::token::{self, Delimiter, NonterminalKind, Token};
|
2022-05-01 20:58:24 +03:00
|
|
|
use rustc_ast::HasTokens;
|
2020-07-27 14:04:54 +02:00
|
|
|
use rustc_ast_pretty::pprust;
|
2022-10-14 23:16:25 +02:00
|
|
|
use rustc_errors::IntoDiagnostic;
|
2020-07-27 14:04:54 +02:00
|
|
|
use rustc_errors::PResult;
|
|
|
|
use rustc_span::symbol::{kw, Ident};
|
|
|
|
|
2022-10-14 23:16:25 +02:00
|
|
|
use crate::errors::UnexpectedNonterminal;
|
2022-01-12 20:43:24 +00:00
|
|
|
use crate::parser::pat::{CommaRecoveryMode, RecoverColon, RecoverComma};
|
2022-03-25 12:39:12 +11:00
|
|
|
use crate::parser::{FollowedByType, ForceCollect, NtOrTt, Parser, PathStyle};
|
2020-07-27 14:04:54 +02:00
|
|
|
|
|
|
|
impl<'a> Parser<'a> {
|
|
|
|
/// Checks whether a non-terminal may begin with a particular token.
|
|
|
|
///
|
2022-04-20 14:13:49 +10:00
|
|
|
/// Returning `false` is a *stability guarantee* that such a matcher will *never* begin with
|
|
|
|
/// that token. Be conservative (return true) if not sure. Inlined because it has a single call
|
|
|
|
/// site.
|
|
|
|
#[inline]
|
2020-12-28 16:57:13 -06:00
|
|
|
pub fn nonterminal_may_begin_with(kind: NonterminalKind, token: &Token) -> bool {
|
2020-07-27 14:04:54 +02:00
|
|
|
/// Checks whether the non-terminal may contain a single (non-keyword) identifier.
|
|
|
|
fn may_be_ident(nt: &token::Nonterminal) -> bool {
|
2023-04-15 20:49:54 +02:00
|
|
|
!matches!(
|
|
|
|
*nt,
|
|
|
|
token::NtItem(_) | token::NtBlock(_) | token::NtVis(_) | token::NtLifetime(_)
|
|
|
|
)
|
2020-07-27 14:04:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
match kind {
|
|
|
|
NonterminalKind::Expr => {
|
|
|
|
token.can_begin_expr()
|
|
|
|
// This exception is here for backwards compatibility.
|
|
|
|
&& !token.is_keyword(kw::Let)
|
2020-12-17 13:51:20 -08:00
|
|
|
// This exception is here for backwards compatibility.
|
|
|
|
&& !token.is_keyword(kw::Const)
|
2020-07-27 14:04:54 +02:00
|
|
|
}
|
|
|
|
NonterminalKind::Ty => token.can_begin_type(),
|
|
|
|
NonterminalKind::Ident => get_macro_ident(token).is_some(),
|
|
|
|
NonterminalKind::Literal => token.can_begin_literal_maybe_minus(),
|
|
|
|
NonterminalKind::Vis => match token.kind {
|
|
|
|
// The follow-set of :vis + "priv" keyword + interpolated
|
|
|
|
token::Comma | token::Ident(..) | token::Interpolated(..) => true,
|
|
|
|
_ => token.can_begin_type(),
|
|
|
|
},
|
2022-11-22 09:42:01 +00:00
|
|
|
NonterminalKind::Block => match &token.kind {
|
2022-04-26 15:40:14 +03:00
|
|
|
token::OpenDelim(Delimiter::Brace) => true,
|
2022-11-22 09:42:01 +00:00
|
|
|
token::Interpolated(nt) => !matches!(
|
2021-01-09 12:00:45 -05:00
|
|
|
**nt,
|
|
|
|
token::NtItem(_)
|
|
|
|
| token::NtPat(_)
|
|
|
|
| token::NtTy(_)
|
|
|
|
| token::NtIdent(..)
|
|
|
|
| token::NtMeta(_)
|
|
|
|
| token::NtPath(_)
|
|
|
|
| token::NtVis(_)
|
|
|
|
),
|
2020-07-27 14:04:54 +02:00
|
|
|
_ => false,
|
|
|
|
},
|
2022-11-22 09:42:01 +00:00
|
|
|
NonterminalKind::Path | NonterminalKind::Meta => match &token.kind {
|
2020-07-27 14:04:54 +02:00
|
|
|
token::ModSep | token::Ident(..) => true,
|
2022-11-22 09:42:01 +00:00
|
|
|
token::Interpolated(nt) => match **nt {
|
2020-07-27 14:04:54 +02:00
|
|
|
token::NtPath(_) | token::NtMeta(_) => true,
|
|
|
|
_ => may_be_ident(&nt),
|
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
},
|
2021-04-27 21:15:59 -05:00
|
|
|
NonterminalKind::PatParam { .. } | NonterminalKind::PatWithOr { .. } => {
|
2022-11-22 09:42:01 +00:00
|
|
|
match &token.kind {
|
2022-09-15 23:51:43 +08:00
|
|
|
token::Ident(..) | // box, ref, mut, and other identifiers (can stricten)
|
|
|
|
token::OpenDelim(Delimiter::Parenthesis) | // tuple pattern
|
|
|
|
token::OpenDelim(Delimiter::Bracket) | // slice pattern
|
|
|
|
token::BinOp(token::And) | // reference
|
|
|
|
token::BinOp(token::Minus) | // negative literal
|
|
|
|
token::AndAnd | // double reference
|
|
|
|
token::Literal(..) | // literal
|
|
|
|
token::DotDot | // range pattern (future compat)
|
|
|
|
token::DotDotDot | // range pattern (future compat)
|
|
|
|
token::ModSep | // path
|
|
|
|
token::Lt | // path (UFCS constant)
|
|
|
|
token::BinOp(token::Shl) => true, // path (double UFCS)
|
2020-11-10 18:00:53 -06:00
|
|
|
// leading vert `|` or-pattern
|
2021-04-27 21:15:59 -05:00
|
|
|
token::BinOp(token::Or) => matches!(kind, NonterminalKind::PatWithOr {..}),
|
2022-11-22 09:42:01 +00:00
|
|
|
token::Interpolated(nt) => may_be_ident(nt),
|
2020-07-27 14:04:54 +02:00
|
|
|
_ => false,
|
2021-04-14 20:34:51 -05:00
|
|
|
}
|
|
|
|
}
|
2022-11-22 09:42:01 +00:00
|
|
|
NonterminalKind::Lifetime => match &token.kind {
|
2020-07-27 14:04:54 +02:00
|
|
|
token::Lifetime(_) => true,
|
2022-11-22 09:42:01 +00:00
|
|
|
token::Interpolated(nt) => {
|
2022-03-25 12:39:12 +11:00
|
|
|
matches!(**nt, token::NtLifetime(_))
|
2020-10-26 21:02:48 -04:00
|
|
|
}
|
2020-07-27 14:04:54 +02:00
|
|
|
_ => false,
|
|
|
|
},
|
2020-10-26 21:02:48 -04:00
|
|
|
NonterminalKind::TT | NonterminalKind::Item | NonterminalKind::Stmt => {
|
|
|
|
!matches!(token.kind, token::CloseDelim(_))
|
|
|
|
}
|
2020-07-27 14:04:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 14:08:59 +10:00
|
|
|
/// Parse a non-terminal (e.g. MBE `:pat` or `:ident`). Inlined because there is only one call
|
|
|
|
/// site.
|
|
|
|
#[inline]
|
2022-03-25 12:39:12 +11:00
|
|
|
pub fn parse_nonterminal(&mut self, kind: NonterminalKind) -> PResult<'a, NtOrTt> {
|
2020-07-27 14:04:54 +02:00
|
|
|
// Any `Nonterminal` which stores its tokens (currently `NtItem` and `NtExpr`)
|
|
|
|
// needs to have them force-captured here.
|
|
|
|
// A `macro_rules!` invocation may pass a captured item/expr to a proc-macro,
|
|
|
|
// which requires having captured tokens available. Since we cannot determine
|
|
|
|
// in advance whether or not a proc-macro will be (transitively) invoked,
|
|
|
|
// we always capture tokens for any `Nonterminal` which needs them.
|
2021-05-06 16:21:40 +03:00
|
|
|
let mut nt = match kind {
|
2022-03-25 12:39:12 +11:00
|
|
|
// Note that TT is treated differently to all the others.
|
|
|
|
NonterminalKind::TT => return Ok(NtOrTt::Tt(self.parse_token_tree())),
|
2021-01-18 16:47:37 -05:00
|
|
|
NonterminalKind::Item => match self.parse_item(ForceCollect::Yes)? {
|
2021-01-13 16:28:57 -05:00
|
|
|
Some(item) => token::NtItem(item),
|
|
|
|
None => {
|
2022-10-14 23:16:25 +02:00
|
|
|
return Err(UnexpectedNonterminal::Item(self.token.span)
|
|
|
|
.into_diagnostic(&self.sess.span_diagnostic));
|
2020-07-27 14:04:54 +02:00
|
|
|
}
|
|
|
|
},
|
2020-08-21 17:52:52 -04:00
|
|
|
NonterminalKind::Block => {
|
2021-02-13 12:42:43 -05:00
|
|
|
// While a block *expression* may have attributes (e.g. `#[my_attr] { ... }`),
|
2021-01-22 13:28:08 -05:00
|
|
|
// the ':block' matcher does not support them
|
|
|
|
token::NtBlock(self.collect_tokens_no_attrs(|this| this.parse_block())?)
|
2020-08-21 17:52:52 -04:00
|
|
|
}
|
2021-01-18 16:47:37 -05:00
|
|
|
NonterminalKind::Stmt => match self.parse_stmt(ForceCollect::Yes)? {
|
2022-04-06 12:08:39 +10:00
|
|
|
Some(s) => token::NtStmt(P(s)),
|
2021-01-13 16:28:57 -05:00
|
|
|
None => {
|
2022-10-14 23:16:25 +02:00
|
|
|
return Err(UnexpectedNonterminal::Statement(self.token.span)
|
|
|
|
.into_diagnostic(&self.sess.span_diagnostic));
|
2020-09-10 16:59:30 -04:00
|
|
|
}
|
2021-01-13 16:28:57 -05:00
|
|
|
},
|
2021-04-27 21:15:59 -05:00
|
|
|
NonterminalKind::PatParam { .. } | NonterminalKind::PatWithOr { .. } => {
|
2021-01-22 13:28:08 -05:00
|
|
|
token::NtPat(self.collect_tokens_no_attrs(|this| match kind {
|
2023-08-03 00:00:56 +08:00
|
|
|
NonterminalKind::PatParam { .. } => this.parse_pat_no_top_alt(None, None),
|
2022-03-25 12:39:12 +11:00
|
|
|
NonterminalKind::PatWithOr { .. } => this.parse_pat_allow_top_alt(
|
|
|
|
None,
|
|
|
|
RecoverComma::No,
|
|
|
|
RecoverColon::No,
|
|
|
|
CommaRecoveryMode::EitherTupleOrPipe,
|
|
|
|
),
|
2020-12-28 16:57:13 -06:00
|
|
|
_ => unreachable!(),
|
2021-01-13 16:28:57 -05:00
|
|
|
})?)
|
2020-07-27 14:04:54 +02:00
|
|
|
}
|
2021-01-22 13:28:08 -05:00
|
|
|
|
2021-03-25 18:05:49 -04:00
|
|
|
NonterminalKind::Expr => token::NtExpr(self.parse_expr_force_collect()?),
|
2020-08-21 18:28:47 -04:00
|
|
|
NonterminalKind::Literal => {
|
2021-01-22 13:28:08 -05:00
|
|
|
// The `:literal` matcher does not support attributes
|
|
|
|
token::NtLiteral(
|
|
|
|
self.collect_tokens_no_attrs(|this| this.parse_literal_maybe_minus())?,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-03-25 12:39:12 +11:00
|
|
|
NonterminalKind::Ty => token::NtTy(
|
|
|
|
self.collect_tokens_no_attrs(|this| this.parse_no_question_mark_recover())?,
|
|
|
|
),
|
|
|
|
|
2020-07-27 14:04:54 +02:00
|
|
|
// this could be handled like a token, since it is one
|
2021-08-16 17:29:49 +02:00
|
|
|
NonterminalKind::Ident
|
|
|
|
if let Some((ident, is_raw)) = get_macro_ident(&self.token) =>
|
|
|
|
{
|
|
|
|
self.bump();
|
|
|
|
token::NtIdent(ident, is_raw)
|
|
|
|
}
|
2020-07-27 14:04:54 +02:00
|
|
|
NonterminalKind::Ident => {
|
2022-10-14 23:16:25 +02:00
|
|
|
return Err(UnexpectedNonterminal::Ident {
|
|
|
|
span: self.token.span,
|
|
|
|
token: self.token.clone(),
|
|
|
|
}.into_diagnostic(&self.sess.span_diagnostic));
|
2020-07-27 14:04:54 +02:00
|
|
|
}
|
2021-01-22 13:28:08 -05:00
|
|
|
NonterminalKind::Path => token::NtPath(
|
2023-08-03 00:00:56 +08:00
|
|
|
P(self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type, None))?),
|
2021-01-22 13:28:08 -05:00
|
|
|
),
|
2020-11-28 18:33:17 -05:00
|
|
|
NonterminalKind::Meta => token::NtMeta(P(self.parse_attr_item(true)?)),
|
2021-01-13 16:28:57 -05:00
|
|
|
NonterminalKind::Vis => token::NtVis(
|
2022-04-06 12:08:39 +10:00
|
|
|
P(self.collect_tokens_no_attrs(|this| this.parse_visibility(FollowedByType::Yes))?),
|
2021-01-13 16:28:57 -05:00
|
|
|
),
|
2020-07-27 14:04:54 +02:00
|
|
|
NonterminalKind::Lifetime => {
|
|
|
|
if self.check_lifetime() {
|
|
|
|
token::NtLifetime(self.expect_lifetime().ident)
|
|
|
|
} else {
|
2022-10-14 23:16:25 +02:00
|
|
|
return Err(UnexpectedNonterminal::Lifetime {
|
|
|
|
span: self.token.span,
|
|
|
|
token: self.token.clone(),
|
|
|
|
}.into_diagnostic(&self.sess.span_diagnostic));
|
2020-07-27 14:04:54 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-06 16:21:40 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// If tokens are supported at all, they should be collected.
|
|
|
|
if matches!(nt.tokens_mut(), Some(None)) {
|
|
|
|
panic!(
|
|
|
|
"Missing tokens for nt {:?} at {:?}: {:?}",
|
|
|
|
nt,
|
|
|
|
nt.span(),
|
|
|
|
pprust::nonterminal_to_string(&nt)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-25 12:39:12 +11:00
|
|
|
Ok(NtOrTt::Nt(nt))
|
2020-07-27 14:04:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The token is an identifier, but not `_`.
|
|
|
|
/// We prohibit passing `_` to macros expecting `ident` for now.
|
|
|
|
fn get_macro_ident(token: &Token) -> Option<(Ident, bool)> {
|
|
|
|
token.ident().filter(|(ident, _)| ident.name != kw::Underscore)
|
|
|
|
}
|