Move the panicking parse functions out of the parser

Since these functions are only used by the AST quoting syntax extensions, they should be there instead of in the parser.
This commit is contained in:
Kyle Mayes 2015-11-13 22:18:59 -05:00
parent c10569cac5
commit 44d8abcc0f
3 changed files with 53 additions and 51 deletions

View File

@ -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;
@ -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])
@ -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"],

View File

@ -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)

View File

@ -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))
}