2 use rustc_ast
::token
::{self, Delimiter, NonterminalKind, Token}
;
3 use rustc_ast
::HasTokens
;
4 use rustc_ast_pretty
::pprust
;
5 use rustc_errors
::PResult
;
6 use rustc_span
::symbol
::{kw, Ident}
;
8 use crate::parser
::pat
::{CommaRecoveryMode, RecoverColon, RecoverComma}
;
9 use crate::parser
::{FollowedByType, ForceCollect, NtOrTt, Parser, PathStyle}
;
12 /// Checks whether a non-terminal may begin with a particular token.
14 /// Returning `false` is a *stability guarantee* that such a matcher will *never* begin with
15 /// that token. Be conservative (return true) if not sure. Inlined because it has a single call
18 pub fn nonterminal_may_begin_with(kind
: NonterminalKind
, token
: &Token
) -> bool
{
19 /// Checks whether the non-terminal may contain a single (non-keyword) identifier.
20 fn may_be_ident(nt
: &token
::Nonterminal
) -> bool
{
22 token
::NtItem(_
) | token
::NtBlock(_
) | token
::NtVis(_
) | token
::NtLifetime(_
) => {
30 NonterminalKind
::Expr
=> {
31 token
.can_begin_expr()
32 // This exception is here for backwards compatibility.
33 && !token
.is_keyword(kw
::Let
)
34 // This exception is here for backwards compatibility.
35 && !token
.is_keyword(kw
::Const
)
37 NonterminalKind
::Ty
=> token
.can_begin_type(),
38 NonterminalKind
::Ident
=> get_macro_ident(token
).is_some(),
39 NonterminalKind
::Literal
=> token
.can_begin_literal_maybe_minus(),
40 NonterminalKind
::Vis
=> match token
.kind
{
41 // The follow-set of :vis + "priv" keyword + interpolated
42 token
::Comma
| token
::Ident(..) | token
::Interpolated(..) => true,
43 _
=> token
.can_begin_type(),
45 NonterminalKind
::Block
=> match token
.kind
{
46 token
::OpenDelim(Delimiter
::Brace
) => true,
47 token
::Interpolated(ref nt
) => !matches
!(
59 NonterminalKind
::Path
| NonterminalKind
::Meta
=> match token
.kind
{
60 token
::ModSep
| token
::Ident(..) => true,
61 token
::Interpolated(ref nt
) => match **nt
{
62 token
::NtPath(_
) | token
::NtMeta(_
) => true,
63 _
=> may_be_ident(&nt
),
67 NonterminalKind
::PatParam { .. }
| NonterminalKind
::PatWithOr { .. }
=> {
69 token
::Ident(..) | // box, ref, mut, and other identifiers (can stricten)
70 token
::OpenDelim(Delimiter
::Parenthesis
) | // tuple pattern
71 token
::OpenDelim(Delimiter
::Bracket
) | // slice pattern
72 token
::BinOp(token
::And
) | // reference
73 token
::BinOp(token
::Minus
) | // negative literal
74 token
::AndAnd
| // double reference
75 token
::Literal(..) | // literal
76 token
::DotDot
| // range pattern (future compat)
77 token
::DotDotDot
| // range pattern (future compat)
78 token
::ModSep
| // path
79 token
::Lt
| // path (UFCS constant)
80 token
::BinOp(token
::Shl
) => true, // path (double UFCS)
81 // leading vert `|` or-pattern
82 token
::BinOp(token
::Or
) => matches
!(kind
, NonterminalKind
::PatWithOr {..}
),
83 token
::Interpolated(ref nt
) => may_be_ident(nt
),
87 NonterminalKind
::Lifetime
=> match token
.kind
{
88 token
::Lifetime(_
) => true,
89 token
::Interpolated(ref nt
) => {
90 matches
!(**nt
, token
::NtLifetime(_
))
94 NonterminalKind
::TT
| NonterminalKind
::Item
| NonterminalKind
::Stmt
=> {
95 !matches
!(token
.kind
, token
::CloseDelim(_
))
100 /// Parse a non-terminal (e.g. MBE `:pat` or `:ident`). Inlined because there is only one call
103 pub fn parse_nonterminal(&mut self, kind
: NonterminalKind
) -> PResult
<'a
, NtOrTt
> {
104 // Any `Nonterminal` which stores its tokens (currently `NtItem` and `NtExpr`)
105 // needs to have them force-captured here.
106 // A `macro_rules!` invocation may pass a captured item/expr to a proc-macro,
107 // which requires having captured tokens available. Since we cannot determine
108 // in advance whether or not a proc-macro will be (transitively) invoked,
109 // we always capture tokens for any `Nonterminal` which needs them.
110 let mut nt
= match kind
{
111 // Note that TT is treated differently to all the others.
112 NonterminalKind
::TT
=> return Ok(NtOrTt
::Tt(self.parse_token_tree())),
113 NonterminalKind
::Item
=> match self.parse_item(ForceCollect
::Yes
)?
{
114 Some(item
) => token
::NtItem(item
),
116 return Err(self.struct_span_err(self.token
.span
, "expected an item keyword"));
119 NonterminalKind
::Block
=> {
120 // While a block *expression* may have attributes (e.g. `#[my_attr] { ... }`),
121 // the ':block' matcher does not support them
122 token
::NtBlock(self.collect_tokens_no_attrs(|this
| this
.parse_block())?
)
124 NonterminalKind
::Stmt
=> match self.parse_stmt(ForceCollect
::Yes
)?
{
125 Some(s
) => token
::NtStmt(P(s
)),
127 return Err(self.struct_span_err(self.token
.span
, "expected a statement"));
130 NonterminalKind
::PatParam { .. }
| NonterminalKind
::PatWithOr { .. }
=> {
131 token
::NtPat(self.collect_tokens_no_attrs(|this
| match kind
{
132 NonterminalKind
::PatParam { .. }
=> this
.parse_pat_no_top_alt(None
),
133 NonterminalKind
::PatWithOr { .. }
=> this
.parse_pat_allow_top_alt(
137 CommaRecoveryMode
::EitherTupleOrPipe
,
143 NonterminalKind
::Expr
=> token
::NtExpr(self.parse_expr_force_collect()?
),
144 NonterminalKind
::Literal
=> {
145 // The `:literal` matcher does not support attributes
147 self.collect_tokens_no_attrs(|this
| this
.parse_literal_maybe_minus())?
,
151 NonterminalKind
::Ty
=> token
::NtTy(
152 self.collect_tokens_no_attrs(|this
| this
.parse_no_question_mark_recover())?
,
155 // this could be handled like a token, since it is one
156 NonterminalKind
::Ident
157 if let Some((ident
, is_raw
)) = get_macro_ident(&self.token
) =>
160 token
::NtIdent(ident
, is_raw
)
162 NonterminalKind
::Ident
=> {
163 let token_str
= pprust
::token_to_string(&self.token
);
164 let msg
= &format
!("expected ident, found {}", &token_str
);
165 return Err(self.struct_span_err(self.token
.span
, msg
));
167 NonterminalKind
::Path
=> token
::NtPath(
168 P(self.collect_tokens_no_attrs(|this
| this
.parse_path(PathStyle
::Type
))?
),
170 NonterminalKind
::Meta
=> token
::NtMeta(P(self.parse_attr_item(true)?
)),
171 NonterminalKind
::Vis
=> token
::NtVis(
172 P(self.collect_tokens_no_attrs(|this
| this
.parse_visibility(FollowedByType
::Yes
))?
),
174 NonterminalKind
::Lifetime
=> {
175 if self.check_lifetime() {
176 token
::NtLifetime(self.expect_lifetime().ident
)
178 let token_str
= pprust
::token_to_string(&self.token
);
179 let msg
= &format
!("expected a lifetime, found `{}`", &token_str
);
180 return Err(self.struct_span_err(self.token
.span
, msg
));
185 // If tokens are supported at all, they should be collected.
186 if matches
!(nt
.tokens_mut(), Some(None
)) {
188 "Missing tokens for nt {:?} at {:?}: {:?}",
191 pprust
::nonterminal_to_string(&nt
)
199 /// The token is an identifier, but not `_`.
200 /// We prohibit passing `_` to macros expecting `ident` for now.
201 fn get_macro_ident(token
: &Token
) -> Option
<(Ident
, bool
)> {
202 token
.ident().filter(|(ident
, _
)| ident
.name
!= kw
::Underscore
)