Skip to content

Commit 94db37a

Browse files
committed
mbe: reduce panictry! uses.
1 parent 1721c96 commit 94db37a

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

src/libsyntax/ext/mbe/macro_parser.rs

+27-25
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ use TokenTreeOrTokenTreeSlice::*;
7676

7777
use crate::ast::{Ident, Name};
7878
use crate::ext::mbe::{self, TokenTree};
79-
use crate::parse::{Directory, ParseSess};
79+
use crate::parse::{Directory, ParseSess, PResult};
8080
use crate::parse::parser::{Parser, PathStyle};
8181
use crate::parse::token::{self, DocComment, Nonterminal, Token};
8282
use crate::print::pprust;
@@ -893,48 +893,50 @@ fn parse_nt(p: &mut Parser<'_>, sp: Span, name: Symbol) -> Nonterminal {
893893
}
894894
// check at the beginning and the parser checks after each bump
895895
p.process_potential_macro_variable();
896-
match name {
897-
sym::item => match panictry!(p.parse_item()) {
896+
match parse_nt_inner(p, sp, name) {
897+
Ok(nt) => nt,
898+
Err(mut err) => {
899+
err.emit();
900+
FatalError.raise();
901+
}
902+
}
903+
}
904+
905+
fn parse_nt_inner<'a>(p: &mut Parser<'a>, sp: Span, name: Symbol) -> PResult<'a, Nonterminal> {
906+
Ok(match name {
907+
sym::item => match p.parse_item()? {
898908
Some(i) => token::NtItem(i),
899-
None => {
900-
p.fatal("expected an item keyword").emit();
901-
FatalError.raise();
902-
}
909+
None => return Err(p.fatal("expected an item keyword")),
903910
},
904-
sym::block => token::NtBlock(panictry!(p.parse_block())),
905-
sym::stmt => match panictry!(p.parse_stmt()) {
911+
sym::block => token::NtBlock(p.parse_block()?),
912+
sym::stmt => match p.parse_stmt()? {
906913
Some(s) => token::NtStmt(s),
907-
None => {
908-
p.fatal("expected a statement").emit();
909-
FatalError.raise();
910-
}
914+
None => return Err(p.fatal("expected a statement")),
911915
},
912-
sym::pat => token::NtPat(panictry!(p.parse_pat(None))),
913-
sym::expr => token::NtExpr(panictry!(p.parse_expr())),
914-
sym::literal => token::NtLiteral(panictry!(p.parse_literal_maybe_minus())),
915-
sym::ty => token::NtTy(panictry!(p.parse_ty())),
916+
sym::pat => token::NtPat(p.parse_pat(None)?),
917+
sym::expr => token::NtExpr(p.parse_expr()?),
918+
sym::literal => token::NtLiteral(p.parse_literal_maybe_minus()?),
919+
sym::ty => token::NtTy(p.parse_ty()?),
916920
// this could be handled like a token, since it is one
917921
sym::ident => if let Some((name, is_raw)) = get_macro_name(&p.token) {
918922
let span = p.token.span;
919923
p.bump();
920924
token::NtIdent(Ident::new(name, span), is_raw)
921925
} else {
922926
let token_str = pprust::token_to_string(&p.token);
923-
p.fatal(&format!("expected ident, found {}", &token_str)).emit();
924-
FatalError.raise()
927+
return Err(p.fatal(&format!("expected ident, found {}", &token_str)));
925928
}
926-
sym::path => token::NtPath(panictry!(p.parse_path(PathStyle::Type))),
927-
sym::meta => token::NtMeta(panictry!(p.parse_attr_item())),
928-
sym::vis => token::NtVis(panictry!(p.parse_visibility(true))),
929+
sym::path => token::NtPath(p.parse_path(PathStyle::Type)?),
930+
sym::meta => token::NtMeta(p.parse_attr_item()?),
931+
sym::vis => token::NtVis(p.parse_visibility(true)?),
929932
sym::lifetime => if p.check_lifetime() {
930933
token::NtLifetime(p.expect_lifetime().ident)
931934
} else {
932935
let token_str = pprust::token_to_string(&p.token);
933-
p.fatal(&format!("expected a lifetime, found `{}`", &token_str)).emit();
934-
FatalError.raise();
936+
return Err(p.fatal(&format!("expected a lifetime, found `{}`", &token_str)));
935937
}
936938
// this is not supposed to happen, since it has been checked
937939
// when compiling the macro.
938940
_ => p.span_bug(sp, "invalid fragment specifier"),
939-
}
941+
})
940942
}

0 commit comments

Comments
 (0)