2 use rustc_ast
::token
::{self, Nonterminal, NonterminalKind, Token}
;
3 use rustc_ast_pretty
::pprust
;
4 use rustc_errors
::PResult
;
5 use rustc_span
::symbol
::{kw, Ident}
;
7 use crate::parser
::pat
::{GateOr, RecoverComma}
;
8 use crate::parser
::{FollowedByType, ForceCollect, Parser, PathStyle}
;
11 /// Checks whether a non-terminal may begin with a particular token.
13 /// Returning `false` is a *stability guarantee* that such a matcher will *never* begin with that
14 /// token. Be conservative (return true) if not sure.
15 pub fn nonterminal_may_begin_with(kind
: NonterminalKind
, token
: &Token
) -> bool
{
16 /// Checks whether the non-terminal may contain a single (non-keyword) identifier.
17 fn may_be_ident(nt
: &token
::Nonterminal
) -> bool
{
19 token
::NtItem(_
) | token
::NtBlock(_
) | token
::NtVis(_
) | token
::NtLifetime(_
) => {
27 NonterminalKind
::Expr
=> {
28 token
.can_begin_expr()
29 // This exception is here for backwards compatibility.
30 && !token
.is_keyword(kw
::Let
)
31 // This exception is here for backwards compatibility.
32 && !token
.is_keyword(kw
::Const
)
34 NonterminalKind
::Ty
=> token
.can_begin_type(),
35 NonterminalKind
::Ident
=> get_macro_ident(token
).is_some(),
36 NonterminalKind
::Literal
=> token
.can_begin_literal_maybe_minus(),
37 NonterminalKind
::Vis
=> match token
.kind
{
38 // The follow-set of :vis + "priv" keyword + interpolated
39 token
::Comma
| token
::Ident(..) | token
::Interpolated(..) => true,
40 _
=> token
.can_begin_type(),
42 NonterminalKind
::Block
=> match token
.kind
{
43 token
::OpenDelim(token
::Brace
) => true,
44 token
::Interpolated(ref nt
) => !matches
!(
56 NonterminalKind
::Path
| NonterminalKind
::Meta
=> match token
.kind
{
57 token
::ModSep
| token
::Ident(..) => true,
58 token
::Interpolated(ref nt
) => match **nt
{
59 token
::NtPath(_
) | token
::NtMeta(_
) => true,
60 _
=> may_be_ident(&nt
),
64 NonterminalKind
::Pat2018 { .. }
| NonterminalKind
::Pat2021 { .. }
=> match token
.kind
{
65 token
::Ident(..) | // box, ref, mut, and other identifiers (can stricten)
66 token
::OpenDelim(token
::Paren
) | // tuple pattern
67 token
::OpenDelim(token
::Bracket
) | // slice pattern
68 token
::BinOp(token
::And
) | // reference
69 token
::BinOp(token
::Minus
) | // negative literal
70 token
::AndAnd
| // double reference
71 token
::Literal(..) | // literal
72 token
::DotDot
| // range pattern (future compat)
73 token
::DotDotDot
| // range pattern (future compat)
74 token
::ModSep
| // path
75 token
::Lt
| // path (UFCS constant)
76 token
::BinOp(token
::Shl
) => true, // path (double UFCS)
77 // leading vert `|` or-pattern
78 token
::BinOp(token
::Or
) => matches
!(kind
, NonterminalKind
::Pat2021 {..}
),
79 token
::Interpolated(ref nt
) => may_be_ident(nt
),
82 NonterminalKind
::Lifetime
=> match token
.kind
{
83 token
::Lifetime(_
) => true,
84 token
::Interpolated(ref nt
) => {
85 matches
!(**nt
, token
::NtLifetime(_
) | token
::NtTT(_
))
89 NonterminalKind
::TT
| NonterminalKind
::Item
| NonterminalKind
::Stmt
=> {
90 !matches
!(token
.kind
, token
::CloseDelim(_
))
95 /// Parse a non-terminal (e.g. MBE `:pat` or `:ident`).
96 pub fn parse_nonterminal(&mut self, kind
: NonterminalKind
) -> PResult
<'a
, Nonterminal
> {
97 // Any `Nonterminal` which stores its tokens (currently `NtItem` and `NtExpr`)
98 // needs to have them force-captured here.
99 // A `macro_rules!` invocation may pass a captured item/expr to a proc-macro,
100 // which requires having captured tokens available. Since we cannot determine
101 // in advance whether or not a proc-macro will be (transitively) invoked,
102 // we always capture tokens for any `Nonterminal` which needs them.
104 NonterminalKind
::Item
=> match self.parse_item(ForceCollect
::Yes
)?
{
105 Some(item
) => token
::NtItem(item
),
107 return Err(self.struct_span_err(self.token
.span
, "expected an item keyword"));
110 NonterminalKind
::Block
=> {
111 token
::NtBlock(self.collect_tokens(|this
| this
.parse_block())?
)
113 NonterminalKind
::Stmt
=> match self.parse_stmt(ForceCollect
::Yes
)?
{
114 Some(s
) => token
::NtStmt(s
),
116 return Err(self.struct_span_err(self.token
.span
, "expected a statement"));
119 NonterminalKind
::Pat2018 { .. }
| NonterminalKind
::Pat2021 { .. }
=> {
120 token
::NtPat(self.collect_tokens(|this
| match kind
{
121 NonterminalKind
::Pat2018 { .. }
=> this
.parse_pat(None
),
122 NonterminalKind
::Pat2021 { .. }
=> {
123 this
.parse_top_pat(GateOr
::Yes
, RecoverComma
::No
)
128 NonterminalKind
::Expr
=> token
::NtExpr(self.collect_tokens(|this
| this
.parse_expr())?
),
129 NonterminalKind
::Literal
=> {
130 token
::NtLiteral(self.collect_tokens(|this
| this
.parse_literal_maybe_minus())?
)
132 NonterminalKind
::Ty
=> token
::NtTy(self.collect_tokens(|this
| this
.parse_ty())?
),
133 // this could be handled like a token, since it is one
134 NonterminalKind
::Ident
=> {
135 if let Some((ident
, is_raw
)) = get_macro_ident(&self.token
) {
137 token
::NtIdent(ident
, is_raw
)
139 let token_str
= pprust
::token_to_string(&self.token
);
140 let msg
= &format
!("expected ident, found {}", &token_str
);
141 return Err(self.struct_span_err(self.token
.span
, msg
));
144 NonterminalKind
::Path
=> {
145 token
::NtPath(self.collect_tokens(|this
| this
.parse_path(PathStyle
::Type
))?
)
147 NonterminalKind
::Meta
=> {
148 token
::NtMeta(P(self.collect_tokens(|this
| this
.parse_attr_item(false))?
))
150 NonterminalKind
::TT
=> token
::NtTT(self.parse_token_tree()),
151 NonterminalKind
::Vis
=> token
::NtVis(
152 self.collect_tokens(|this
| this
.parse_visibility(FollowedByType
::Yes
))?
,
154 NonterminalKind
::Lifetime
=> {
155 if self.check_lifetime() {
156 token
::NtLifetime(self.expect_lifetime().ident
)
158 let token_str
= pprust
::token_to_string(&self.token
);
159 let msg
= &format
!("expected a lifetime, found `{}`", &token_str
);
160 return Err(self.struct_span_err(self.token
.span
, msg
));
167 /// The token is an identifier, but not `_`.
168 /// We prohibit passing `_` to macros expecting `ident` for now.
169 fn get_macro_ident(token
: &Token
) -> Option
<(Ident
, bool
)> {
170 token
.ident().filter(|(ident
, _
)| ident
.name
!= kw
::Underscore
)