]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_parse/src/parser/nonterminal.rs
Rollup merge of #82487 - CDirkx:const-socketaddr, r=m-ou-se
[rust.git] / compiler / rustc_parse / src / parser / nonterminal.rs
1 use rustc_ast::ptr::P;
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};
6
7 use crate::parser::pat::RecoverComma;
8 use crate::parser::{FollowedByType, ForceCollect, Parser, PathStyle};
9
10 impl<'a> Parser<'a> {
11     /// Checks whether a non-terminal may begin with a particular token.
12     ///
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 {
18             match *nt {
19                 token::NtItem(_) | token::NtBlock(_) | token::NtVis(_) | token::NtLifetime(_) => {
20                     false
21                 }
22                 _ => true,
23             }
24         }
25
26         match kind {
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)
33             }
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(),
41             },
42             NonterminalKind::Block => match token.kind {
43                 token::OpenDelim(token::Brace) => true,
44                 token::Interpolated(ref nt) => !matches!(
45                     **nt,
46                     token::NtItem(_)
47                         | token::NtPat(_)
48                         | token::NtTy(_)
49                         | token::NtIdent(..)
50                         | token::NtMeta(_)
51                         | token::NtPath(_)
52                         | token::NtVis(_)
53                 ),
54                 _ => false,
55             },
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),
61                 },
62                 _ => false,
63             },
64             NonterminalKind::Pat2015 { .. } | 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),
80                 _ => false,
81             },
82             NonterminalKind::Lifetime => match token.kind {
83                 token::Lifetime(_) => true,
84                 token::Interpolated(ref nt) => {
85                     matches!(**nt, token::NtLifetime(_) | token::NtTT(_))
86                 }
87                 _ => false,
88             },
89             NonterminalKind::TT | NonterminalKind::Item | NonterminalKind::Stmt => {
90                 !matches!(token.kind, token::CloseDelim(_))
91             }
92         }
93     }
94
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.
103         Ok(match kind {
104             NonterminalKind::Item => match self.parse_item(ForceCollect::Yes)? {
105                 Some(item) => token::NtItem(item),
106                 None => {
107                     return Err(self.struct_span_err(self.token.span, "expected an item keyword"));
108                 }
109             },
110             NonterminalKind::Block => {
111                 // While a block *expression* may have attributes (e.g. `#[my_attr] { ... }`),
112                 // the ':block' matcher does not support them
113                 token::NtBlock(self.collect_tokens_no_attrs(|this| this.parse_block())?)
114             }
115             NonterminalKind::Stmt => match self.parse_stmt(ForceCollect::Yes)? {
116                 Some(s) => token::NtStmt(s),
117                 None => {
118                     return Err(self.struct_span_err(self.token.span, "expected a statement"));
119                 }
120             },
121             NonterminalKind::Pat2015 { .. } | NonterminalKind::Pat2021 { .. } => {
122                 token::NtPat(self.collect_tokens_no_attrs(|this| match kind {
123                     NonterminalKind::Pat2015 { .. } => this.parse_pat_no_top_alt(None),
124                     NonterminalKind::Pat2021 { .. } => {
125                         this.parse_pat_allow_top_alt(None, RecoverComma::No)
126                     }
127                     _ => unreachable!(),
128                 })?)
129             }
130
131             NonterminalKind::Expr => token::NtExpr(self.parse_expr_force_collect()?),
132             NonterminalKind::Literal => {
133                 // The `:literal` matcher does not support attributes
134                 token::NtLiteral(
135                     self.collect_tokens_no_attrs(|this| this.parse_literal_maybe_minus())?,
136                 )
137             }
138
139             NonterminalKind::Ty => {
140                 token::NtTy(self.collect_tokens_no_attrs(|this| this.parse_ty())?)
141             }
142             // this could be handled like a token, since it is one
143             NonterminalKind::Ident => {
144                 if let Some((ident, is_raw)) = get_macro_ident(&self.token) {
145                     self.bump();
146                     token::NtIdent(ident, is_raw)
147                 } else {
148                     let token_str = pprust::token_to_string(&self.token);
149                     let msg = &format!("expected ident, found {}", &token_str);
150                     return Err(self.struct_span_err(self.token.span, msg));
151                 }
152             }
153             NonterminalKind::Path => token::NtPath(
154                 self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?,
155             ),
156             NonterminalKind::Meta => {
157                 token::NtMeta(P(self.collect_tokens_no_attrs(|this| this.parse_attr_item(false))?))
158             }
159             NonterminalKind::TT => token::NtTT(self.parse_token_tree()),
160             NonterminalKind::Vis => token::NtVis(
161                 self.collect_tokens_no_attrs(|this| this.parse_visibility(FollowedByType::Yes))?,
162             ),
163             NonterminalKind::Lifetime => {
164                 if self.check_lifetime() {
165                     token::NtLifetime(self.expect_lifetime().ident)
166                 } else {
167                     let token_str = pprust::token_to_string(&self.token);
168                     let msg = &format!("expected a lifetime, found `{}`", &token_str);
169                     return Err(self.struct_span_err(self.token.span, msg));
170                 }
171             }
172         })
173     }
174 }
175
176 /// The token is an identifier, but not `_`.
177 /// We prohibit passing `_` to macros expecting `ident` for now.
178 fn get_macro_ident(token: &Token) -> Option<(Ident, bool)> {
179     token.ident().filter(|(ident, _)| ident.name != kw::Underscore)
180 }