]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_parse/src/parser/nonterminal.rs
Recover from a misplaced inner doc comment
[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::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::RecoverComma;
9 use crate::parser::{FollowedByType, ForceCollect, 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(_) | token::NtTT(_))
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, 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),
109                 None => {
110                     return Err(self.struct_span_err(self.token.span, "expected an item keyword"));
111                 }
112             },
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())?)
117             }
118             NonterminalKind::Stmt => match self.parse_stmt(ForceCollect::Yes)? {
119                 Some(s) => token::NtStmt(s),
120                 None => {
121                     return Err(self.struct_span_err(self.token.span, "expected a statement"));
122                 }
123             },
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)
129                     }
130                     _ => unreachable!(),
131                 })?)
132             }
133
134             NonterminalKind::Expr => token::NtExpr(self.parse_expr_force_collect()?),
135             NonterminalKind::Literal => {
136                 // The `:literal` matcher does not support attributes
137                 token::NtLiteral(
138                     self.collect_tokens_no_attrs(|this| this.parse_literal_maybe_minus())?,
139                 )
140             }
141
142             NonterminalKind::Ty => {
143                 token::NtTy(self.collect_tokens_no_attrs(|this| this.parse_ty())?)
144             }
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) {
148                     self.bump();
149                     token::NtIdent(ident, is_raw)
150                 } else {
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));
154                 }
155             }
156             NonterminalKind::Path => token::NtPath(
157                 self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?,
158             ),
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))?,
163             ),
164             NonterminalKind::Lifetime => {
165                 if self.check_lifetime() {
166                     token::NtLifetime(self.expect_lifetime().ident)
167                 } else {
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));
171                 }
172             }
173         };
174
175         // If tokens are supported at all, they should be collected.
176         if matches!(nt.tokens_mut(), Some(None)) {
177             panic!(
178                 "Missing tokens for nt {:?} at {:?}: {:?}",
179                 nt,
180                 nt.span(),
181                 pprust::nonterminal_to_string(&nt)
182             );
183         }
184
185         Ok(nt)
186     }
187 }
188
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)
193 }