]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_parse/src/parser/nonterminal.rs
Rollup merge of #95473 - lqd:macro-expansion, r=petrochenkov
[rust.git] / compiler / rustc_parse / src / parser / nonterminal.rs
1 use rustc_ast::ptr::P;
2 use rustc_ast::token::{self, 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};
7
8 use crate::parser::pat::{CommaRecoveryMode, RecoverColon, RecoverComma};
9 use crate::parser::{FollowedByType, ForceCollect, NtOrTt, Parser, PathStyle};
10
11 impl<'a> Parser<'a> {
12     /// Checks whether a non-terminal may begin with a particular token.
13     ///
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 {
19             match *nt {
20                 token::NtItem(_) | token::NtBlock(_) | token::NtVis(_) | token::NtLifetime(_) => {
21                     false
22                 }
23                 _ => true,
24             }
25         }
26
27         match kind {
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)
34             }
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(),
42             },
43             NonterminalKind::Block => match token.kind {
44                 token::OpenDelim(token::Brace) => true,
45                 token::Interpolated(ref nt) => !matches!(
46                     **nt,
47                     token::NtItem(_)
48                         | token::NtPat(_)
49                         | token::NtTy(_)
50                         | token::NtIdent(..)
51                         | token::NtMeta(_)
52                         | token::NtPath(_)
53                         | token::NtVis(_)
54                 ),
55                 _ => false,
56             },
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),
62                 },
63                 _ => false,
64             },
65             NonterminalKind::PatParam { .. } | NonterminalKind::PatWithOr { .. } => {
66                 match token.kind {
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),
82                 _ => false,
83             }
84             }
85             NonterminalKind::Lifetime => match token.kind {
86                 token::Lifetime(_) => true,
87                 token::Interpolated(ref nt) => {
88                     matches!(**nt, token::NtLifetime(_))
89                 }
90                 _ => false,
91             },
92             NonterminalKind::TT | NonterminalKind::Item | NonterminalKind::Stmt => {
93                 !matches!(token.kind, token::CloseDelim(_))
94             }
95         }
96     }
97
98     /// Parse a non-terminal (e.g. MBE `:pat` or `:ident`).
99     pub fn parse_nonterminal(&mut self, kind: NonterminalKind) -> PResult<'a, NtOrTt> {
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             // Note that TT is treated differently to all the others.
108             NonterminalKind::TT => return Ok(NtOrTt::Tt(self.parse_token_tree())),
109             NonterminalKind::Item => match self.parse_item(ForceCollect::Yes)? {
110                 Some(item) => token::NtItem(item),
111                 None => {
112                     return Err(self.struct_span_err(self.token.span, "expected an item keyword"));
113                 }
114             },
115             NonterminalKind::Block => {
116                 // While a block *expression* may have attributes (e.g. `#[my_attr] { ... }`),
117                 // the ':block' matcher does not support them
118                 token::NtBlock(self.collect_tokens_no_attrs(|this| this.parse_block())?)
119             }
120             NonterminalKind::Stmt => match self.parse_stmt(ForceCollect::Yes)? {
121                 Some(s) => token::NtStmt(s),
122                 None => {
123                     return Err(self.struct_span_err(self.token.span, "expected a statement"));
124                 }
125             },
126             NonterminalKind::PatParam { .. } | NonterminalKind::PatWithOr { .. } => {
127                 token::NtPat(self.collect_tokens_no_attrs(|this| match kind {
128                     NonterminalKind::PatParam { .. } => this.parse_pat_no_top_alt(None),
129                     NonterminalKind::PatWithOr { .. } => this.parse_pat_allow_top_alt(
130                         None,
131                         RecoverComma::No,
132                         RecoverColon::No,
133                         CommaRecoveryMode::EitherTupleOrPipe,
134                     ),
135                     _ => unreachable!(),
136                 })?)
137             }
138
139             NonterminalKind::Expr => token::NtExpr(self.parse_expr_force_collect()?),
140             NonterminalKind::Literal => {
141                 // The `:literal` matcher does not support attributes
142                 token::NtLiteral(
143                     self.collect_tokens_no_attrs(|this| this.parse_literal_maybe_minus())?,
144                 )
145             }
146
147             NonterminalKind::Ty => token::NtTy(
148                 self.collect_tokens_no_attrs(|this| this.parse_no_question_mark_recover())?,
149             ),
150
151             // this could be handled like a token, since it is one
152             NonterminalKind::Ident
153                 if let Some((ident, is_raw)) = get_macro_ident(&self.token) =>
154             {
155                 self.bump();
156                 token::NtIdent(ident, is_raw)
157             }
158             NonterminalKind::Ident => {
159                 let token_str = pprust::token_to_string(&self.token);
160                 let msg = &format!("expected ident, found {}", &token_str);
161                 return Err(self.struct_span_err(self.token.span, msg));
162             }
163             NonterminalKind::Path => token::NtPath(
164                 self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?,
165             ),
166             NonterminalKind::Meta => token::NtMeta(P(self.parse_attr_item(true)?)),
167             NonterminalKind::Vis => token::NtVis(
168                 self.collect_tokens_no_attrs(|this| this.parse_visibility(FollowedByType::Yes))?,
169             ),
170             NonterminalKind::Lifetime => {
171                 if self.check_lifetime() {
172                     token::NtLifetime(self.expect_lifetime().ident)
173                 } else {
174                     let token_str = pprust::token_to_string(&self.token);
175                     let msg = &format!("expected a lifetime, found `{}`", &token_str);
176                     return Err(self.struct_span_err(self.token.span, msg));
177                 }
178             }
179         };
180
181         // If tokens are supported at all, they should be collected.
182         if matches!(nt.tokens_mut(), Some(None)) {
183             panic!(
184                 "Missing tokens for nt {:?} at {:?}: {:?}",
185                 nt,
186                 nt.span(),
187                 pprust::nonterminal_to_string(&nt)
188             );
189         }
190
191         Ok(NtOrTt::Nt(nt))
192     }
193 }
194
195 /// The token is an identifier, but not `_`.
196 /// We prohibit passing `_` to macros expecting `ident` for now.
197 fn get_macro_ident(token: &Token) -> Option<(Ident, bool)> {
198     token.ident().filter(|(ident, _)| ident.name != kw::Underscore)
199 }