Actually, just use nonterminal_may_begin_with

This commit is contained in:
Michael Goulet 2024-01-22 01:55:49 +00:00
parent f8847ff3ec
commit a0958082b3

View File

@ -1,4 +1,4 @@
use rustc_ast::token::{Delimiter, TokenKind}; use rustc_ast::token::{Delimiter, NonterminalKind, TokenKind};
use rustc_ast::tokenstream::TokenStream; use rustc_ast::tokenstream::TokenStream;
use rustc_ast::{ast, ptr}; use rustc_ast::{ast, ptr};
use rustc_parse::parser::{ForceCollect, Parser, Recovery}; use rustc_parse::parser::{ForceCollect, Parser, Recovery};
@ -24,9 +24,9 @@ fn build_parser<'a>(context: &RewriteContext<'a>, tokens: TokenStream) -> Parser
fn parse_macro_arg<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option<MacroArg> { fn parse_macro_arg<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option<MacroArg> {
macro_rules! parse_macro_arg { macro_rules! parse_macro_arg {
($macro_arg:ident, $can_begin:expr, $try_parse:expr, $then:expr) => { ($macro_arg:ident, $nt_kind:expr, $try_parse:expr, $then:expr) => {
let mut cloned_parser = (*parser).clone(); let mut cloned_parser = (*parser).clone();
if $can_begin(&mut cloned_parser) { if Parser::nonterminal_may_begin_with($nt_kind, &cloned_parser.token) {
match $try_parse(&mut cloned_parser) { match $try_parse(&mut cloned_parser) {
Ok(x) => { Ok(x) => {
if parser.sess.dcx.has_errors().is_some() { if parser.sess.dcx.has_errors().is_some() {
@ -48,27 +48,26 @@ macro_rules! parse_macro_arg {
parse_macro_arg!( parse_macro_arg!(
Expr, Expr,
|parser: &mut Parser<'b>| parser.token.can_begin_expr(), NonterminalKind::Expr,
|parser: &mut Parser<'b>| parser.parse_expr(), |parser: &mut Parser<'b>| parser.parse_expr(),
|x: ptr::P<ast::Expr>| Some(x) |x: ptr::P<ast::Expr>| Some(x)
); );
parse_macro_arg!( parse_macro_arg!(
Ty, Ty,
|parser: &mut Parser<'b>| parser.token.can_begin_type(), NonterminalKind::Ty,
|parser: &mut Parser<'b>| parser.parse_ty(), |parser: &mut Parser<'b>| parser.parse_ty(),
|x: ptr::P<ast::Ty>| Some(x) |x: ptr::P<ast::Ty>| Some(x)
); );
parse_macro_arg!( parse_macro_arg!(
Pat, Pat,
// FIXME: This isn't right NonterminalKind::PatParam { inferred: false },
|_| true,
|parser: &mut Parser<'b>| parser.parse_pat_no_top_alt(None, None), |parser: &mut Parser<'b>| parser.parse_pat_no_top_alt(None, None),
|x: ptr::P<ast::Pat>| Some(x) |x: ptr::P<ast::Pat>| Some(x)
); );
// `parse_item` returns `Option<ptr::P<ast::Item>>`. // `parse_item` returns `Option<ptr::P<ast::Item>>`.
parse_macro_arg!( parse_macro_arg!(
Item, Item,
|_| true, NonterminalKind::Item,
|parser: &mut Parser<'b>| parser.parse_item(ForceCollect::No), |parser: &mut Parser<'b>| parser.parse_item(ForceCollect::No),
|x: Option<ptr::P<ast::Item>>| x |x: Option<ptr::P<ast::Item>>| x
); );