Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mbe: reduce panictry! uses. #65360

Merged
merged 1 commit into from
Oct 13, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 27 additions & 25 deletions src/libsyntax/ext/mbe/macro_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ use TokenTreeOrTokenTreeSlice::*;

use crate::ast::{Ident, Name};
use crate::ext::mbe::{self, TokenTree};
use crate::parse::{Directory, ParseSess};
use crate::parse::{Directory, ParseSess, PResult};
use crate::parse::parser::{Parser, PathStyle};
use crate::parse::token::{self, DocComment, Nonterminal, Token};
use crate::print::pprust;
Expand Down Expand Up @@ -893,48 +893,50 @@ fn parse_nt(p: &mut Parser<'_>, sp: Span, name: Symbol) -> Nonterminal {
}
// check at the beginning and the parser checks after each bump
p.process_potential_macro_variable();
match name {
sym::item => match panictry!(p.parse_item()) {
match parse_nt_inner(p, sp, name) {
Ok(nt) => nt,
Err(mut err) => {
err.emit();
FatalError.raise();
}
}
}

fn parse_nt_inner<'a>(p: &mut Parser<'a>, sp: Span, name: Symbol) -> PResult<'a, Nonterminal> {
Ok(match name {
sym::item => match p.parse_item()? {
Some(i) => token::NtItem(i),
None => {
p.fatal("expected an item keyword").emit();
FatalError.raise();
}
None => return Err(p.fatal("expected an item keyword")),
},
sym::block => token::NtBlock(panictry!(p.parse_block())),
sym::stmt => match panictry!(p.parse_stmt()) {
sym::block => token::NtBlock(p.parse_block()?),
sym::stmt => match p.parse_stmt()? {
Some(s) => token::NtStmt(s),
None => {
p.fatal("expected a statement").emit();
FatalError.raise();
}
None => return Err(p.fatal("expected a statement")),
},
sym::pat => token::NtPat(panictry!(p.parse_pat(None))),
sym::expr => token::NtExpr(panictry!(p.parse_expr())),
sym::literal => token::NtLiteral(panictry!(p.parse_literal_maybe_minus())),
sym::ty => token::NtTy(panictry!(p.parse_ty())),
sym::pat => token::NtPat(p.parse_pat(None)?),
sym::expr => token::NtExpr(p.parse_expr()?),
sym::literal => token::NtLiteral(p.parse_literal_maybe_minus()?),
sym::ty => token::NtTy(p.parse_ty()?),
// this could be handled like a token, since it is one
sym::ident => if let Some((name, is_raw)) = get_macro_name(&p.token) {
let span = p.token.span;
p.bump();
token::NtIdent(Ident::new(name, span), is_raw)
} else {
let token_str = pprust::token_to_string(&p.token);
p.fatal(&format!("expected ident, found {}", &token_str)).emit();
FatalError.raise()
return Err(p.fatal(&format!("expected ident, found {}", &token_str)));
}
sym::path => token::NtPath(panictry!(p.parse_path(PathStyle::Type))),
sym::meta => token::NtMeta(panictry!(p.parse_attr_item())),
sym::vis => token::NtVis(panictry!(p.parse_visibility(true))),
sym::path => token::NtPath(p.parse_path(PathStyle::Type)?),
sym::meta => token::NtMeta(p.parse_attr_item()?),
sym::vis => token::NtVis(p.parse_visibility(true)?),
sym::lifetime => if p.check_lifetime() {
token::NtLifetime(p.expect_lifetime().ident)
} else {
let token_str = pprust::token_to_string(&p.token);
p.fatal(&format!("expected a lifetime, found `{}`", &token_str)).emit();
FatalError.raise();
return Err(p.fatal(&format!("expected a lifetime, found `{}`", &token_str)));
}
// this is not supposed to happen, since it has been checked
// when compiling the macro.
_ => p.span_bug(sp, "invalid fragment specifier"),
}
})
}