]> git.proxmox.com Git - rustc.git/blob - vendor/rustc-ap-rustc_parse/src/parser/nonterminal.rs
New upstream version 1.52.1+dfsg1
[rustc.git] / vendor / rustc-ap-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::{GateOr, 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::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),
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 token::NtBlock(self.collect_tokens(|this| this.parse_block())?)
112 }
113 NonterminalKind::Stmt => match self.parse_stmt(ForceCollect::Yes)? {
114 Some(s) => token::NtStmt(s),
115 None => {
116 return Err(self.struct_span_err(self.token.span, "expected a statement"));
117 }
118 },
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)
124 }
125 _ => unreachable!(),
126 })?)
127 }
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())?)
131 }
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) {
136 self.bump();
137 token::NtIdent(ident, is_raw)
138 } else {
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));
142 }
143 }
144 NonterminalKind::Path => {
145 token::NtPath(self.collect_tokens(|this| this.parse_path(PathStyle::Type))?)
146 }
147 NonterminalKind::Meta => {
148 token::NtMeta(P(self.collect_tokens(|this| this.parse_attr_item(false))?))
149 }
150 NonterminalKind::TT => token::NtTT(self.parse_token_tree()),
151 NonterminalKind::Vis => token::NtVis(
152 self.collect_tokens(|this| this.parse_visibility(FollowedByType::Yes))?,
153 ),
154 NonterminalKind::Lifetime => {
155 if self.check_lifetime() {
156 token::NtLifetime(self.expect_lifetime().ident)
157 } else {
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));
161 }
162 }
163 })
164 }
165 }
166
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)
171 }