Skip to content

Move the panicking parse functions out of the parser #29825

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

Merged
merged 1 commit into from
Nov 14, 2015
Merged
Show file tree
Hide file tree
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
55 changes: 52 additions & 3 deletions src/libsyntax/ext/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use ast::{self, TokenTree};
use ast::{self, Arg, Arm, Block, Expr, Item, Pat, Path, Stmt, TokenTree, Ty};
use codemap::Span;
use ext::base::ExtCtxt;
use ext::base;
use ext::build::AstBuilder;
use parse::parser::{Parser, PathParsingMode};
use parse::token::*;
use parse::token;
use ptr::P;
Expand Down Expand Up @@ -329,6 +330,52 @@ pub mod rt {
}
}

// These panicking parsing functions are used by the quote_*!() syntax extensions,
// but shouldn't be used otherwise.
pub fn parse_expr_panic(parser: &mut Parser) -> P<Expr> {
panictry!(parser.parse_expr())
}

pub fn parse_item_panic(parser: &mut Parser) -> Option<P<Item>> {
panictry!(parser.parse_item())
}

pub fn parse_pat_panic(parser: &mut Parser) -> P<Pat> {
panictry!(parser.parse_pat())
}

pub fn parse_arm_panic(parser: &mut Parser) -> Arm {
panictry!(parser.parse_arm())
}

pub fn parse_ty_panic(parser: &mut Parser) -> P<Ty> {
panictry!(parser.parse_ty())
}

pub fn parse_stmt_panic(parser: &mut Parser) -> Option<P<Stmt>> {
panictry!(parser.parse_stmt())
}

pub fn parse_attribute_panic(parser: &mut Parser, permit_inner: bool) -> ast::Attribute {
panictry!(parser.parse_attribute(permit_inner))
}

pub fn parse_arg_panic(parser: &mut Parser) -> Arg {
panictry!(parser.parse_arg())
}

pub fn parse_block_panic(parser: &mut Parser) -> P<Block> {
panictry!(parser.parse_block())
}

pub fn parse_meta_item_panic(parser: &mut Parser) -> P<ast::MetaItem> {
panictry!(parser.parse_meta_item())
}

pub fn parse_path_panic(parser: &mut Parser, mode: PathParsingMode) -> ast::Path {
panictry!(parser.parse_path(mode))
}

pub fn expand_quote_tokens<'cx>(cx: &'cx mut ExtCtxt,
sp: Span,
tts: &[TokenTree])
Expand Down Expand Up @@ -864,8 +911,10 @@ fn expand_parse_call(cx: &ExtCtxt,
cx.expr_ident(sp, id_ext("new_parser_from_tts")),
vec!(parse_sess_call(), cfg_call(), tts_expr));

let expr = cx.expr_method_call(sp, new_parser_call, id_ext(parse_method),
arg_exprs);
let path = vec![id_ext("syntax"), id_ext("ext"), id_ext("quote"), id_ext(parse_method)];
let mut args = vec![cx.expr_mut_addr_of(sp, new_parser_call)];
args.extend(arg_exprs);
let expr = cx.expr_call_global(sp, path, args);

if parse_method == "parse_attribute" {
expand_wrapper(cx, sp, cx_expr, expr, &[&["syntax", "ext", "quote", "rt"],
Expand Down
47 changes: 0 additions & 47 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,53 +362,6 @@ impl<'a> Parser<'a> {
}
}

// Panicing fns (for now!)
// These functions are used by the quote_*!() syntax extensions, but shouldn't
// be used otherwise.
pub fn parse_expr_panic(&mut self) -> P<Expr> {
panictry!(self.parse_expr())
}

pub fn parse_item_panic(&mut self) -> Option<P<Item>> {
panictry!(self.parse_item())
}

pub fn parse_pat_panic(&mut self) -> P<Pat> {
panictry!(self.parse_pat())
}

pub fn parse_arm_panic(&mut self) -> Arm {
panictry!(self.parse_arm())
}

pub fn parse_ty_panic(&mut self) -> P<Ty> {
panictry!(self.parse_ty())
}

pub fn parse_stmt_panic(&mut self) -> Option<P<Stmt>> {
panictry!(self.parse_stmt())
}

pub fn parse_attribute_panic(&mut self, permit_inner: bool) -> ast::Attribute {
panictry!(self.parse_attribute(permit_inner))
}

pub fn parse_arg_panic(&mut self) -> Arg {
panictry!(self.parse_arg())
}

pub fn parse_block_panic(&mut self) -> P<Block> {
panictry!(self.parse_block())
}

pub fn parse_meta_item_panic(&mut self) -> P<ast::MetaItem> {
panictry!(self.parse_meta_item())
}

pub fn parse_path_panic(&mut self, mode: PathParsingMode) -> ast::Path {
panictry!(self.parse_path(mode))
}

/// Convert a token to a string using self's reader
pub fn token_to_string(token: &token::Token) -> String {
pprust::token_to_string(token)
Expand Down
2 changes: 1 addition & 1 deletion src/test/auxiliary/macro_crate_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn expand_identity(cx: &mut ExtCtxt, _span: Span, tts: &[TokenTree])
// Parse an expression and emit it unchanged.
let mut parser = parse::new_parser_from_tts(cx.parse_sess(),
cx.cfg(), tts.to_vec());
let expr = parser.parse_expr_panic();
let expr = parser.parse_expr().unwrap();
MacEager::expr(quote_expr!(&mut *cx, $expr))
}

Expand Down