2 use rustc_ast::token::{self, Delimiter, NonterminalKind, Token};
3 use rustc_ast::HasTokens;
4 use rustc_ast_pretty::pprust;
5 use rustc_errors::IntoDiagnostic;
6 use rustc_errors::PResult;
7 use rustc_span::symbol::{kw, Ident};
9 use crate::errors::UnexpectedNonterminal;
10 use crate::parser::pat::{CommaRecoveryMode, RecoverColon, RecoverComma};
11 use crate::parser::{FollowedByType, ForceCollect, NtOrTt, Parser, PathStyle};
14 /// Checks whether a non-terminal may begin with a particular token.
16 /// Returning `false` is a *stability guarantee* that such a matcher will *never* begin with
17 /// that token. Be conservative (return true) if not sure. Inlined because it has a single call
20 pub fn nonterminal_may_begin_with(kind: NonterminalKind, token: &Token) -> bool {
21 /// Checks whether the non-terminal may contain a single (non-keyword) identifier.
22 fn may_be_ident(nt: &token::Nonterminal) -> bool {
24 token::NtItem(_) | token::NtBlock(_) | token::NtVis(_) | token::NtLifetime(_) => {
32 NonterminalKind::Expr => {
33 token.can_begin_expr()
34 // This exception is here for backwards compatibility.
35 && !token.is_keyword(kw::Let)
36 // This exception is here for backwards compatibility.
37 && !token.is_keyword(kw::Const)
39 NonterminalKind::Ty => token.can_begin_type(),
40 NonterminalKind::Ident => get_macro_ident(token).is_some(),
41 NonterminalKind::Literal => token.can_begin_literal_maybe_minus(),
42 NonterminalKind::Vis => match token.kind {
43 // The follow-set of :vis + "priv" keyword + interpolated
44 token::Comma | token::Ident(..) | token::Interpolated(..) => true,
45 _ => token.can_begin_type(),
47 NonterminalKind::Block => match &token.kind {
48 token::OpenDelim(Delimiter::Brace) => true,
49 token::Interpolated(nt) => !matches!(
61 NonterminalKind::Path | NonterminalKind::Meta => match &token.kind {
62 token::ModSep | token::Ident(..) => true,
63 token::Interpolated(nt) => match **nt {
64 token::NtPath(_) | token::NtMeta(_) => true,
65 _ => may_be_ident(&nt),
69 NonterminalKind::PatParam { .. } | NonterminalKind::PatWithOr { .. } => {
71 token::Ident(..) | // box, ref, mut, and other identifiers (can stricten)
72 token::OpenDelim(Delimiter::Parenthesis) | // tuple pattern
73 token::OpenDelim(Delimiter::Bracket) | // slice pattern
74 token::BinOp(token::And) | // reference
75 token::BinOp(token::Minus) | // negative literal
76 token::AndAnd | // double reference
77 token::Literal(..) | // literal
78 token::DotDot | // range pattern (future compat)
79 token::DotDotDot | // range pattern (future compat)
80 token::ModSep | // path
81 token::Lt | // path (UFCS constant)
82 token::BinOp(token::Shl) => true, // path (double UFCS)
83 // leading vert `|` or-pattern
84 token::BinOp(token::Or) => matches!(kind, NonterminalKind::PatWithOr {..}),
85 token::Interpolated(nt) => may_be_ident(nt),
89 NonterminalKind::Lifetime => match &token.kind {
90 token::Lifetime(_) => true,
91 token::Interpolated(nt) => {
92 matches!(**nt, token::NtLifetime(_))
96 NonterminalKind::TT | NonterminalKind::Item | NonterminalKind::Stmt => {
97 !matches!(token.kind, token::CloseDelim(_))
102 /// Parse a non-terminal (e.g. MBE `:pat` or `:ident`). Inlined because there is only one call
105 pub fn parse_nonterminal(&mut self, kind: NonterminalKind) -> PResult<'a, NtOrTt> {
106 // Any `Nonterminal` which stores its tokens (currently `NtItem` and `NtExpr`)
107 // needs to have them force-captured here.
108 // A `macro_rules!` invocation may pass a captured item/expr to a proc-macro,
109 // which requires having captured tokens available. Since we cannot determine
110 // in advance whether or not a proc-macro will be (transitively) invoked,
111 // we always capture tokens for any `Nonterminal` which needs them.
112 let mut nt = match kind {
113 // Note that TT is treated differently to all the others.
114 NonterminalKind::TT => return Ok(NtOrTt::Tt(self.parse_token_tree())),
115 NonterminalKind::Item => match self.parse_item(ForceCollect::Yes)? {
116 Some(item) => token::NtItem(item),
118 return Err(UnexpectedNonterminal::Item(self.token.span)
119 .into_diagnostic(&self.sess.span_diagnostic));
122 NonterminalKind::Block => {
123 // While a block *expression* may have attributes (e.g. `#[my_attr] { ... }`),
124 // the ':block' matcher does not support them
125 token::NtBlock(self.collect_tokens_no_attrs(|this| this.parse_block())?)
127 NonterminalKind::Stmt => match self.parse_stmt(ForceCollect::Yes)? {
128 Some(s) => token::NtStmt(P(s)),
130 return Err(UnexpectedNonterminal::Statement(self.token.span)
131 .into_diagnostic(&self.sess.span_diagnostic));
134 NonterminalKind::PatParam { .. } | NonterminalKind::PatWithOr { .. } => {
135 token::NtPat(self.collect_tokens_no_attrs(|this| match kind {
136 NonterminalKind::PatParam { .. } => this.parse_pat_no_top_alt(None),
137 NonterminalKind::PatWithOr { .. } => this.parse_pat_allow_top_alt(
141 CommaRecoveryMode::EitherTupleOrPipe,
147 NonterminalKind::Expr => token::NtExpr(self.parse_expr_force_collect()?),
148 NonterminalKind::Literal => {
149 // The `:literal` matcher does not support attributes
151 self.collect_tokens_no_attrs(|this| this.parse_literal_maybe_minus())?,
155 NonterminalKind::Ty => token::NtTy(
156 self.collect_tokens_no_attrs(|this| this.parse_no_question_mark_recover())?,
159 // this could be handled like a token, since it is one
160 NonterminalKind::Ident
161 if let Some((ident, is_raw)) = get_macro_ident(&self.token) =>
164 token::NtIdent(ident, is_raw)
166 NonterminalKind::Ident => {
167 return Err(UnexpectedNonterminal::Ident {
168 span: self.token.span,
169 token: self.token.clone(),
170 }.into_diagnostic(&self.sess.span_diagnostic));
172 NonterminalKind::Path => token::NtPath(
173 P(self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?),
175 NonterminalKind::Meta => token::NtMeta(P(self.parse_attr_item(true)?)),
176 NonterminalKind::Vis => token::NtVis(
177 P(self.collect_tokens_no_attrs(|this| this.parse_visibility(FollowedByType::Yes))?),
179 NonterminalKind::Lifetime => {
180 if self.check_lifetime() {
181 token::NtLifetime(self.expect_lifetime().ident)
183 return Err(UnexpectedNonterminal::Lifetime {
184 span: self.token.span,
185 token: self.token.clone(),
186 }.into_diagnostic(&self.sess.span_diagnostic));
191 // If tokens are supported at all, they should be collected.
192 if matches!(nt.tokens_mut(), Some(None)) {
194 "Missing tokens for nt {:?} at {:?}: {:?}",
197 pprust::nonterminal_to_string(&nt)
205 /// The token is an identifier, but not `_`.
206 /// We prohibit passing `_` to macros expecting `ident` for now.
207 fn get_macro_ident(token: &Token) -> Option<(Ident, bool)> {
208 token.ident().filter(|(ident, _)| ident.name != kw::Underscore)