Skip to content

Commit 44d8abc

Browse files
committed
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.
1 parent c10569c commit 44d8abc

File tree

3 files changed

+53
-51
lines changed

3 files changed

+53
-51
lines changed

src/libsyntax/ext/quote.rs

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use ast::{self, TokenTree};
11+
use ast::{self, Arg, Arm, Block, Expr, Item, Pat, Path, Stmt, TokenTree, Ty};
1212
use codemap::Span;
1313
use ext::base::ExtCtxt;
1414
use ext::base;
1515
use ext::build::AstBuilder;
16+
use parse::parser::{Parser, PathParsingMode};
1617
use parse::token::*;
1718
use parse::token;
1819
use ptr::P;
@@ -329,6 +330,52 @@ pub mod rt {
329330
}
330331
}
331332

333+
// These panicking parsing functions are used by the quote_*!() syntax extensions,
334+
// but shouldn't be used otherwise.
335+
pub fn parse_expr_panic(parser: &mut Parser) -> P<Expr> {
336+
panictry!(parser.parse_expr())
337+
}
338+
339+
pub fn parse_item_panic(parser: &mut Parser) -> Option<P<Item>> {
340+
panictry!(parser.parse_item())
341+
}
342+
343+
pub fn parse_pat_panic(parser: &mut Parser) -> P<Pat> {
344+
panictry!(parser.parse_pat())
345+
}
346+
347+
pub fn parse_arm_panic(parser: &mut Parser) -> Arm {
348+
panictry!(parser.parse_arm())
349+
}
350+
351+
pub fn parse_ty_panic(parser: &mut Parser) -> P<Ty> {
352+
panictry!(parser.parse_ty())
353+
}
354+
355+
pub fn parse_stmt_panic(parser: &mut Parser) -> Option<P<Stmt>> {
356+
panictry!(parser.parse_stmt())
357+
}
358+
359+
pub fn parse_attribute_panic(parser: &mut Parser, permit_inner: bool) -> ast::Attribute {
360+
panictry!(parser.parse_attribute(permit_inner))
361+
}
362+
363+
pub fn parse_arg_panic(parser: &mut Parser) -> Arg {
364+
panictry!(parser.parse_arg())
365+
}
366+
367+
pub fn parse_block_panic(parser: &mut Parser) -> P<Block> {
368+
panictry!(parser.parse_block())
369+
}
370+
371+
pub fn parse_meta_item_panic(parser: &mut Parser) -> P<ast::MetaItem> {
372+
panictry!(parser.parse_meta_item())
373+
}
374+
375+
pub fn parse_path_panic(parser: &mut Parser, mode: PathParsingMode) -> ast::Path {
376+
panictry!(parser.parse_path(mode))
377+
}
378+
332379
pub fn expand_quote_tokens<'cx>(cx: &'cx mut ExtCtxt,
333380
sp: Span,
334381
tts: &[TokenTree])
@@ -864,8 +911,10 @@ fn expand_parse_call(cx: &ExtCtxt,
864911
cx.expr_ident(sp, id_ext("new_parser_from_tts")),
865912
vec!(parse_sess_call(), cfg_call(), tts_expr));
866913

867-
let expr = cx.expr_method_call(sp, new_parser_call, id_ext(parse_method),
868-
arg_exprs);
914+
let path = vec![id_ext("syntax"), id_ext("ext"), id_ext("quote"), id_ext(parse_method)];
915+
let mut args = vec![cx.expr_mut_addr_of(sp, new_parser_call)];
916+
args.extend(arg_exprs);
917+
let expr = cx.expr_call_global(sp, path, args);
869918

870919
if parse_method == "parse_attribute" {
871920
expand_wrapper(cx, sp, cx_expr, expr, &[&["syntax", "ext", "quote", "rt"],

src/libsyntax/parse/parser.rs

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -362,53 +362,6 @@ impl<'a> Parser<'a> {
362362
}
363363
}
364364

365-
// Panicing fns (for now!)
366-
// These functions are used by the quote_*!() syntax extensions, but shouldn't
367-
// be used otherwise.
368-
pub fn parse_expr_panic(&mut self) -> P<Expr> {
369-
panictry!(self.parse_expr())
370-
}
371-
372-
pub fn parse_item_panic(&mut self) -> Option<P<Item>> {
373-
panictry!(self.parse_item())
374-
}
375-
376-
pub fn parse_pat_panic(&mut self) -> P<Pat> {
377-
panictry!(self.parse_pat())
378-
}
379-
380-
pub fn parse_arm_panic(&mut self) -> Arm {
381-
panictry!(self.parse_arm())
382-
}
383-
384-
pub fn parse_ty_panic(&mut self) -> P<Ty> {
385-
panictry!(self.parse_ty())
386-
}
387-
388-
pub fn parse_stmt_panic(&mut self) -> Option<P<Stmt>> {
389-
panictry!(self.parse_stmt())
390-
}
391-
392-
pub fn parse_attribute_panic(&mut self, permit_inner: bool) -> ast::Attribute {
393-
panictry!(self.parse_attribute(permit_inner))
394-
}
395-
396-
pub fn parse_arg_panic(&mut self) -> Arg {
397-
panictry!(self.parse_arg())
398-
}
399-
400-
pub fn parse_block_panic(&mut self) -> P<Block> {
401-
panictry!(self.parse_block())
402-
}
403-
404-
pub fn parse_meta_item_panic(&mut self) -> P<ast::MetaItem> {
405-
panictry!(self.parse_meta_item())
406-
}
407-
408-
pub fn parse_path_panic(&mut self, mode: PathParsingMode) -> ast::Path {
409-
panictry!(self.parse_path(mode))
410-
}
411-
412365
/// Convert a token to a string using self's reader
413366
pub fn token_to_string(token: &token::Token) -> String {
414367
pprust::token_to_string(token)

src/test/auxiliary/macro_crate_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn expand_identity(cx: &mut ExtCtxt, _span: Span, tts: &[TokenTree])
5454
// Parse an expression and emit it unchanged.
5555
let mut parser = parse::new_parser_from_tts(cx.parse_sess(),
5656
cx.cfg(), tts.to_vec());
57-
let expr = parser.parse_expr_panic();
57+
let expr = parser.parse_expr().unwrap();
5858
MacEager::expr(quote_expr!(&mut *cx, $expr))
5959
}
6060

0 commit comments

Comments
 (0)