From 44d8abcc0f372dbc7ad58f312303d2e59612dec9 Mon Sep 17 00:00:00 2001 From: Kyle Mayes Date: Fri, 13 Nov 2015 22:18:59 -0500 Subject: [PATCH] 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. --- src/libsyntax/ext/quote.rs | 55 ++++++++++++++++++++++++-- src/libsyntax/parse/parser.rs | 47 ---------------------- src/test/auxiliary/macro_crate_test.rs | 2 +- 3 files changed, 53 insertions(+), 51 deletions(-) diff --git a/src/libsyntax/ext/quote.rs b/src/libsyntax/ext/quote.rs index 772786ad232..d743a601bbb 100644 --- a/src/libsyntax/ext/quote.rs +++ b/src/libsyntax/ext/quote.rs @@ -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 { + panictry!(parser.parse_expr()) +} + +pub fn parse_item_panic(parser: &mut Parser) -> Option> { + panictry!(parser.parse_item()) +} + +pub fn parse_pat_panic(parser: &mut Parser) -> P { + 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 { + panictry!(parser.parse_ty()) +} + +pub fn parse_stmt_panic(parser: &mut Parser) -> Option> { + 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 { + panictry!(parser.parse_block()) +} + +pub fn parse_meta_item_panic(parser: &mut Parser) -> P { + 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"], diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index c82c6dcdea3..bde910838cb 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -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 { - panictry!(self.parse_expr()) - } - - pub fn parse_item_panic(&mut self) -> Option> { - panictry!(self.parse_item()) - } - - pub fn parse_pat_panic(&mut self) -> P { - panictry!(self.parse_pat()) - } - - pub fn parse_arm_panic(&mut self) -> Arm { - panictry!(self.parse_arm()) - } - - pub fn parse_ty_panic(&mut self) -> P { - panictry!(self.parse_ty()) - } - - pub fn parse_stmt_panic(&mut self) -> Option> { - 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 { - panictry!(self.parse_block()) - } - - pub fn parse_meta_item_panic(&mut self) -> P { - 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) diff --git a/src/test/auxiliary/macro_crate_test.rs b/src/test/auxiliary/macro_crate_test.rs index 0bb5c05aaf5..7ce80a5647c 100644 --- a/src/test/auxiliary/macro_crate_test.rs +++ b/src/test/auxiliary/macro_crate_test.rs @@ -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)) }