2 use rustc_ast
::token
::{self, Nonterminal, NonterminalKind, Token}
;
3 use rustc_ast
::AstLike
;
4 use rustc_ast_pretty
::pprust
;
5 use rustc_errors
::PResult
;
6 use rustc_span
::symbol
::{kw, Ident}
;
8 use crate::parser
::pat
::{RecoverColon, RecoverComma}
;
9 use crate::parser
::{FollowedByType, ForceCollect, 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 that
15 /// token. Be conservative (return true) if not sure.
16 pub fn nonterminal_may_begin_with(kind
: NonterminalKind
, token
: &Token
) -> bool
{
17 /// Checks whether the non-terminal may contain a single (non-keyword) identifier.
18 fn may_be_ident(nt
: &token
::Nonterminal
) -> bool
{
20 token
::NtItem(_
) | token
::NtBlock(_
) | token
::NtVis(_
) | token
::NtLifetime(_
) => {
28 NonterminalKind
::Expr
=> {
29 token
.can_begin_expr()
30 // This exception is here for backwards compatibility.
31 && !token
.is_keyword(kw
::Let
)
32 // This exception is here for backwards compatibility.
33 && !token
.is_keyword(kw
::Const
)
35 NonterminalKind
::Ty
=> token
.can_begin_type(),
36 NonterminalKind
::Ident
=> get_macro_ident(token
).is_some(),
37 NonterminalKind
::Literal
=> token
.can_begin_literal_maybe_minus(),
38 NonterminalKind
::Vis
=> match token
.kind
{
39 // The follow-set of :vis + "priv" keyword + interpolated
40 token
::Comma
| token
::Ident(..) | token
::Interpolated(..) => true,
41 _
=> token
.can_begin_type(),
43 NonterminalKind
::Block
=> match token
.kind
{
44 token
::OpenDelim(token
::Brace
) => true,
45 token
::Interpolated(ref nt
) => !matches
!(
57 NonterminalKind
::Path
| NonterminalKind
::Meta
=> match token
.kind
{
58 token
::ModSep
| token
::Ident(..) => true,
59 token
::Interpolated(ref nt
) => match **nt
{
60 token
::NtPath(_
) | token
::NtMeta(_
) => true,
61 _
=> may_be_ident(&nt
),
65 NonterminalKind
::PatParam { .. }
| NonterminalKind
::PatWithOr { .. }
=> {
67 token
::Ident(..) | // box, ref, mut, and other identifiers (can stricten)
68 token
::OpenDelim(token
::Paren
) | // tuple pattern
69 token
::OpenDelim(token
::Bracket
) | // slice pattern
70 token
::BinOp(token
::And
) | // reference
71 token
::BinOp(token
::Minus
) | // negative literal
72 token
::AndAnd
| // double reference
73 token
::Literal(..) | // literal
74 token
::DotDot
| // range pattern (future compat)
75 token
::DotDotDot
| // range pattern (future compat)
76 token
::ModSep
| // path
77 token
::Lt
| // path (UFCS constant)
78 token
::BinOp(token
::Shl
) => true, // path (double UFCS)
79 // leading vert `|` or-pattern
80 token
::BinOp(token
::Or
) => matches
!(kind
, NonterminalKind
::PatWithOr {..}
),
81 token
::Interpolated(ref nt
) => may_be_ident(nt
),
85 NonterminalKind
::Lifetime
=> match token
.kind
{
86 token
::Lifetime(_
) => true,
87 token
::Interpolated(ref nt
) => {
88 matches
!(**nt
, token
::NtLifetime(_
) | token
::NtTT(_
))
92 NonterminalKind
::TT
| NonterminalKind
::Item
| NonterminalKind
::Stmt
=> {
93 !matches
!(token
.kind
, token
::CloseDelim(_
))
98 /// Parse a non-terminal (e.g. MBE `:pat` or `:ident`).
99 pub fn parse_nonterminal(&mut self, kind
: NonterminalKind
) -> PResult
<'a
, Nonterminal
> {
100 // Any `Nonterminal` which stores its tokens (currently `NtItem` and `NtExpr`)
101 // needs to have them force-captured here.
102 // A `macro_rules!` invocation may pass a captured item/expr to a proc-macro,
103 // which requires having captured tokens available. Since we cannot determine
104 // in advance whether or not a proc-macro will be (transitively) invoked,
105 // we always capture tokens for any `Nonterminal` which needs them.
106 let mut nt
= match kind
{
107 NonterminalKind
::Item
=> match self.parse_item(ForceCollect
::Yes
)?
{
108 Some(item
) => token
::NtItem(item
),
110 return Err(self.struct_span_err(self.token
.span
, "expected an item keyword"));
113 NonterminalKind
::Block
=> {
114 // While a block *expression* may have attributes (e.g. `#[my_attr] { ... }`),
115 // the ':block' matcher does not support them
116 token
::NtBlock(self.collect_tokens_no_attrs(|this
| this
.parse_block())?
)
118 NonterminalKind
::Stmt
=> match self.parse_stmt(ForceCollect
::Yes
)?
{
119 Some(s
) => token
::NtStmt(s
),
121 return Err(self.struct_span_err(self.token
.span
, "expected a statement"));
124 NonterminalKind
::PatParam { .. }
| NonterminalKind
::PatWithOr { .. }
=> {
125 token
::NtPat(self.collect_tokens_no_attrs(|this
| match kind
{
126 NonterminalKind
::PatParam { .. }
=> this
.parse_pat_no_top_alt(None
),
127 NonterminalKind
::PatWithOr { .. }
=> {
128 this
.parse_pat_allow_top_alt(None
, RecoverComma
::No
, RecoverColon
::No
)
134 NonterminalKind
::Expr
=> token
::NtExpr(self.parse_expr_force_collect()?
),
135 NonterminalKind
::Literal
=> {
136 // The `:literal` matcher does not support attributes
138 self.collect_tokens_no_attrs(|this
| this
.parse_literal_maybe_minus())?
,
142 NonterminalKind
::Ty
=> {
143 token
::NtTy(self.collect_tokens_no_attrs(|this
| this
.parse_ty())?
)
145 // this could be handled like a token, since it is one
146 NonterminalKind
::Ident
=> {
147 if let Some((ident
, is_raw
)) = get_macro_ident(&self.token
) {
149 token
::NtIdent(ident
, is_raw
)
151 let token_str
= pprust
::token_to_string(&self.token
);
152 let msg
= &format
!("expected ident, found {}", &token_str
);
153 return Err(self.struct_span_err(self.token
.span
, msg
));
156 NonterminalKind
::Path
=> token
::NtPath(
157 self.collect_tokens_no_attrs(|this
| this
.parse_path(PathStyle
::Type
))?
,
159 NonterminalKind
::Meta
=> token
::NtMeta(P(self.parse_attr_item(true)?
)),
160 NonterminalKind
::TT
=> token
::NtTT(self.parse_token_tree()),
161 NonterminalKind
::Vis
=> token
::NtVis(
162 self.collect_tokens_no_attrs(|this
| this
.parse_visibility(FollowedByType
::Yes
))?
,
164 NonterminalKind
::Lifetime
=> {
165 if self.check_lifetime() {
166 token
::NtLifetime(self.expect_lifetime().ident
)
168 let token_str
= pprust
::token_to_string(&self.token
);
169 let msg
= &format
!("expected a lifetime, found `{}`", &token_str
);
170 return Err(self.struct_span_err(self.token
.span
, msg
));
175 // If tokens are supported at all, they should be collected.
176 if matches
!(nt
.tokens_mut(), Some(None
)) {
178 "Missing tokens for nt {:?} at {:?}: {:?}",
181 pprust
::nonterminal_to_string(&nt
)
189 /// The token is an identifier, but not `_`.
190 /// We prohibit passing `_` to macros expecting `ident` for now.
191 fn get_macro_ident(token
: &Token
) -> Option
<(Ident
, bool
)> {
192 token
.ident().filter(|(ident
, _
)| ident
.name
!= kw
::Underscore
)