2020-07-27 14:04:54 +02:00
|
|
|
use rustc_ast::ptr::P;
|
|
|
|
use rustc_ast::token::{self, Nonterminal, NonterminalKind, Token};
|
|
|
|
use rustc_ast_pretty::pprust;
|
|
|
|
use rustc_errors::PResult;
|
|
|
|
use rustc_span::symbol::{kw, Ident};
|
|
|
|
|
2020-12-28 16:57:13 -06:00
|
|
|
use crate::parser::pat::{GateOr, RecoverComma};
|
2020-07-27 14:04:54 +02:00
|
|
|
use crate::parser::{FollowedByType, Parser, PathStyle};
|
|
|
|
|
|
|
|
impl<'a> Parser<'a> {
|
|
|
|
/// Checks whether a non-terminal may begin with a particular token.
|
|
|
|
///
|
|
|
|
/// Returning `false` is a *stability guarantee* that such a matcher will *never* begin with that
|
|
|
|
/// token. Be conservative (return true) if not sure.
|
2020-12-28 16:57:13 -06:00
|
|
|
pub fn nonterminal_may_begin_with(kind: NonterminalKind, token: &Token) -> bool {
|
2020-07-27 14:04:54 +02:00
|
|
|
/// Checks whether the non-terminal may contain a single (non-keyword) identifier.
|
|
|
|
fn may_be_ident(nt: &token::Nonterminal) -> bool {
|
|
|
|
match *nt {
|
|
|
|
token::NtItem(_) | token::NtBlock(_) | token::NtVis(_) | token::NtLifetime(_) => {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match kind {
|
|
|
|
NonterminalKind::Expr => {
|
|
|
|
token.can_begin_expr()
|
|
|
|
// This exception is here for backwards compatibility.
|
|
|
|
&& !token.is_keyword(kw::Let)
|
2020-12-17 13:51:20 -08:00
|
|
|
// This exception is here for backwards compatibility.
|
|
|
|
&& !token.is_keyword(kw::Const)
|
2020-07-27 14:04:54 +02:00
|
|
|
}
|
|
|
|
NonterminalKind::Ty => token.can_begin_type(),
|
|
|
|
NonterminalKind::Ident => get_macro_ident(token).is_some(),
|
|
|
|
NonterminalKind::Literal => token.can_begin_literal_maybe_minus(),
|
|
|
|
NonterminalKind::Vis => match token.kind {
|
|
|
|
// The follow-set of :vis + "priv" keyword + interpolated
|
|
|
|
token::Comma | token::Ident(..) | token::Interpolated(..) => true,
|
|
|
|
_ => token.can_begin_type(),
|
|
|
|
},
|
|
|
|
NonterminalKind::Block => match token.kind {
|
|
|
|
token::OpenDelim(token::Brace) => true,
|
2020-10-26 21:02:48 -04:00
|
|
|
token::Interpolated(ref nt) => !matches!(**nt, token::NtItem(_)
|
2020-07-27 14:04:54 +02:00
|
|
|
| token::NtPat(_)
|
|
|
|
| token::NtTy(_)
|
|
|
|
| token::NtIdent(..)
|
|
|
|
| token::NtMeta(_)
|
|
|
|
| token::NtPath(_)
|
2020-10-26 21:02:48 -04:00
|
|
|
| token::NtVis(_)),
|
2020-07-27 14:04:54 +02:00
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
NonterminalKind::Path | NonterminalKind::Meta => match token.kind {
|
|
|
|
token::ModSep | token::Ident(..) => true,
|
|
|
|
token::Interpolated(ref nt) => match **nt {
|
|
|
|
token::NtPath(_) | token::NtMeta(_) => true,
|
|
|
|
_ => may_be_ident(&nt),
|
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
},
|
2020-12-28 16:57:13 -06:00
|
|
|
NonterminalKind::Pat2018 { .. } | NonterminalKind::Pat2021 { .. } => match token.kind {
|
2020-07-27 14:04:54 +02:00
|
|
|
token::Ident(..) | // box, ref, mut, and other identifiers (can stricten)
|
|
|
|
token::OpenDelim(token::Paren) | // tuple pattern
|
|
|
|
token::OpenDelim(token::Bracket) | // slice pattern
|
|
|
|
token::BinOp(token::And) | // reference
|
|
|
|
token::BinOp(token::Minus) | // negative literal
|
|
|
|
token::AndAnd | // double reference
|
|
|
|
token::Literal(..) | // literal
|
|
|
|
token::DotDot | // range pattern (future compat)
|
|
|
|
token::DotDotDot | // range pattern (future compat)
|
|
|
|
token::ModSep | // path
|
|
|
|
token::Lt | // path (UFCS constant)
|
|
|
|
token::BinOp(token::Shl) => true, // path (double UFCS)
|
2020-11-10 18:00:53 -06:00
|
|
|
// leading vert `|` or-pattern
|
2020-12-28 16:57:13 -06:00
|
|
|
token::BinOp(token::Or) => matches!(kind, NonterminalKind::Pat2021 {..}),
|
2020-07-27 14:04:54 +02:00
|
|
|
token::Interpolated(ref nt) => may_be_ident(nt),
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
NonterminalKind::Lifetime => match token.kind {
|
|
|
|
token::Lifetime(_) => true,
|
2020-10-26 21:02:48 -04:00
|
|
|
token::Interpolated(ref nt) => {
|
|
|
|
matches!(**nt, token::NtLifetime(_) | token::NtTT(_))
|
|
|
|
}
|
2020-07-27 14:04:54 +02:00
|
|
|
_ => false,
|
|
|
|
},
|
2020-10-26 21:02:48 -04:00
|
|
|
NonterminalKind::TT | NonterminalKind::Item | NonterminalKind::Stmt => {
|
|
|
|
!matches!(token.kind, token::CloseDelim(_))
|
|
|
|
}
|
2020-07-27 14:04:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 18:00:53 -06:00
|
|
|
/// Parse a non-terminal (e.g. MBE `:pat` or `:ident`).
|
2020-12-28 16:57:13 -06:00
|
|
|
pub fn parse_nonterminal(&mut self, kind: NonterminalKind) -> PResult<'a, Nonterminal> {
|
2020-07-27 14:04:54 +02:00
|
|
|
// Any `Nonterminal` which stores its tokens (currently `NtItem` and `NtExpr`)
|
|
|
|
// needs to have them force-captured here.
|
|
|
|
// A `macro_rules!` invocation may pass a captured item/expr to a proc-macro,
|
|
|
|
// which requires having captured tokens available. Since we cannot determine
|
|
|
|
// in advance whether or not a proc-macro will be (transitively) invoked,
|
|
|
|
// we always capture tokens for any `Nonterminal` which needs them.
|
|
|
|
Ok(match kind {
|
|
|
|
NonterminalKind::Item => match self.collect_tokens(|this| this.parse_item())? {
|
|
|
|
(Some(mut item), tokens) => {
|
|
|
|
// If we captured tokens during parsing (due to outer attributes),
|
|
|
|
// use those.
|
|
|
|
if item.tokens.is_none() {
|
2020-10-22 10:09:08 -04:00
|
|
|
item.tokens = tokens;
|
2020-07-27 14:04:54 +02:00
|
|
|
}
|
|
|
|
token::NtItem(item)
|
|
|
|
}
|
|
|
|
(None, _) => {
|
|
|
|
return Err(self.struct_span_err(self.token.span, "expected an item keyword"));
|
|
|
|
}
|
|
|
|
},
|
2020-08-21 17:52:52 -04:00
|
|
|
NonterminalKind::Block => {
|
|
|
|
let (mut block, tokens) = self.collect_tokens(|this| this.parse_block())?;
|
|
|
|
// We have have eaten an NtBlock, which could already have tokens
|
|
|
|
if block.tokens.is_none() {
|
2020-10-22 10:09:08 -04:00
|
|
|
block.tokens = tokens;
|
2020-08-21 17:52:52 -04:00
|
|
|
}
|
|
|
|
token::NtBlock(block)
|
|
|
|
}
|
2020-09-10 16:59:30 -04:00
|
|
|
NonterminalKind::Stmt => {
|
|
|
|
let (stmt, tokens) = self.collect_tokens(|this| this.parse_stmt())?;
|
|
|
|
match stmt {
|
|
|
|
Some(mut s) => {
|
2020-11-17 14:27:44 -05:00
|
|
|
if s.tokens().is_none() {
|
|
|
|
s.set_tokens(tokens);
|
2020-09-10 16:59:30 -04:00
|
|
|
}
|
|
|
|
token::NtStmt(s)
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
return Err(self.struct_span_err(self.token.span, "expected a statement"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-28 16:57:13 -06:00
|
|
|
NonterminalKind::Pat2018 { .. } | NonterminalKind::Pat2021 { .. } => {
|
|
|
|
let (mut pat, tokens) = self.collect_tokens(|this| match kind {
|
|
|
|
NonterminalKind::Pat2018 { .. } => this.parse_pat(None),
|
|
|
|
NonterminalKind::Pat2021 { .. } => {
|
2020-11-10 18:00:53 -06:00
|
|
|
this.parse_top_pat(GateOr::Yes, RecoverComma::No)
|
|
|
|
}
|
2020-12-28 16:57:13 -06:00
|
|
|
_ => unreachable!(),
|
2020-11-10 18:00:53 -06:00
|
|
|
})?;
|
2020-07-27 18:02:29 -04:00
|
|
|
// We have have eaten an NtPat, which could already have tokens
|
|
|
|
if pat.tokens.is_none() {
|
2020-10-22 10:09:08 -04:00
|
|
|
pat.tokens = tokens;
|
2020-07-27 18:02:29 -04:00
|
|
|
}
|
|
|
|
token::NtPat(pat)
|
|
|
|
}
|
2020-07-27 14:04:54 +02:00
|
|
|
NonterminalKind::Expr => {
|
|
|
|
let (mut expr, tokens) = self.collect_tokens(|this| this.parse_expr())?;
|
|
|
|
// If we captured tokens during parsing (due to outer attributes),
|
|
|
|
// use those.
|
|
|
|
if expr.tokens.is_none() {
|
2020-10-22 10:09:08 -04:00
|
|
|
expr.tokens = tokens;
|
2020-07-27 14:04:54 +02:00
|
|
|
}
|
|
|
|
token::NtExpr(expr)
|
|
|
|
}
|
2020-08-21 18:28:47 -04:00
|
|
|
NonterminalKind::Literal => {
|
|
|
|
let (mut lit, tokens) =
|
|
|
|
self.collect_tokens(|this| this.parse_literal_maybe_minus())?;
|
|
|
|
// We have have eaten a nonterminal, which could already have tokens
|
|
|
|
if lit.tokens.is_none() {
|
2020-10-22 10:09:08 -04:00
|
|
|
lit.tokens = tokens;
|
2020-08-21 18:28:47 -04:00
|
|
|
}
|
|
|
|
token::NtLiteral(lit)
|
|
|
|
}
|
2020-08-21 18:18:04 -04:00
|
|
|
NonterminalKind::Ty => {
|
|
|
|
let (mut ty, tokens) = self.collect_tokens(|this| this.parse_ty())?;
|
|
|
|
// We have an eaten an NtTy, which could already have tokens
|
|
|
|
if ty.tokens.is_none() {
|
2020-10-22 10:09:08 -04:00
|
|
|
ty.tokens = tokens;
|
2020-08-21 18:18:04 -04:00
|
|
|
}
|
|
|
|
token::NtTy(ty)
|
|
|
|
}
|
2020-07-27 14:04:54 +02:00
|
|
|
// this could be handled like a token, since it is one
|
|
|
|
NonterminalKind::Ident => {
|
|
|
|
if let Some((ident, is_raw)) = get_macro_ident(&self.token) {
|
|
|
|
self.bump();
|
|
|
|
token::NtIdent(ident, is_raw)
|
|
|
|
} else {
|
|
|
|
let token_str = pprust::token_to_string(&self.token);
|
|
|
|
let msg = &format!("expected ident, found {}", &token_str);
|
|
|
|
return Err(self.struct_span_err(self.token.span, msg));
|
|
|
|
}
|
|
|
|
}
|
2020-08-21 18:51:23 -04:00
|
|
|
NonterminalKind::Path => {
|
|
|
|
let (mut path, tokens) =
|
|
|
|
self.collect_tokens(|this| this.parse_path(PathStyle::Type))?;
|
|
|
|
// We have have eaten an NtPath, which could already have tokens
|
|
|
|
if path.tokens.is_none() {
|
2020-10-22 10:09:08 -04:00
|
|
|
path.tokens = tokens;
|
2020-08-21 18:51:23 -04:00
|
|
|
}
|
|
|
|
token::NtPath(path)
|
|
|
|
}
|
2020-08-21 18:37:34 -04:00
|
|
|
NonterminalKind::Meta => {
|
2020-09-26 19:33:42 -04:00
|
|
|
let (mut attr, tokens) = self.collect_tokens(|this| this.parse_attr_item(false))?;
|
2020-08-21 18:37:34 -04:00
|
|
|
// We may have eaten a nonterminal, which could already have tokens
|
|
|
|
if attr.tokens.is_none() {
|
2020-10-22 10:09:08 -04:00
|
|
|
attr.tokens = tokens;
|
2020-08-21 18:37:34 -04:00
|
|
|
}
|
|
|
|
token::NtMeta(P(attr))
|
|
|
|
}
|
2020-07-27 14:04:54 +02:00
|
|
|
NonterminalKind::TT => token::NtTT(self.parse_token_tree()),
|
2020-08-21 19:11:00 -04:00
|
|
|
NonterminalKind::Vis => {
|
|
|
|
let (mut vis, tokens) =
|
|
|
|
self.collect_tokens(|this| this.parse_visibility(FollowedByType::Yes))?;
|
|
|
|
// We may have etan an `NtVis`, which could already have tokens
|
|
|
|
if vis.tokens.is_none() {
|
2020-10-22 10:09:08 -04:00
|
|
|
vis.tokens = tokens;
|
2020-08-21 19:11:00 -04:00
|
|
|
}
|
|
|
|
token::NtVis(vis)
|
|
|
|
}
|
2020-07-27 14:04:54 +02:00
|
|
|
NonterminalKind::Lifetime => {
|
|
|
|
if self.check_lifetime() {
|
|
|
|
token::NtLifetime(self.expect_lifetime().ident)
|
|
|
|
} else {
|
|
|
|
let token_str = pprust::token_to_string(&self.token);
|
|
|
|
let msg = &format!("expected a lifetime, found `{}`", &token_str);
|
|
|
|
return Err(self.struct_span_err(self.token.span, msg));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The token is an identifier, but not `_`.
|
|
|
|
/// We prohibit passing `_` to macros expecting `ident` for now.
|
|
|
|
fn get_macro_ident(token: &Token) -> Option<(Ident, bool)> {
|
|
|
|
token.ident().filter(|(ident, _)| ident.name != kw::Underscore)
|
|
|
|
}
|