1 extern crate rustc_ast; 2 extern crate rustc_driver; 3 extern crate rustc_expand; 4 extern crate rustc_parse as parse; 5 extern crate rustc_session; 6 extern crate rustc_span; 7 8 use rustc_ast::ast; 9 use rustc_ast::ptr::P; 10 use rustc_session::parse::ParseSess; 11 use rustc_span::FileName; 12 use std::panic; 13 librustc_expr(input: &str) -> Option<P<ast::Expr>>14pub fn librustc_expr(input: &str) -> Option<P<ast::Expr>> { 15 match panic::catch_unwind(|| { 16 let locale_resources = rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(); 17 let sess = ParseSess::new(locale_resources); 18 let e = parse::new_parser_from_source_str( 19 &sess, 20 FileName::Custom("test_precedence".to_string()), 21 input.to_string(), 22 ) 23 .parse_expr(); 24 match e { 25 Ok(expr) => Some(expr), 26 Err(diagnostic) => { 27 diagnostic.emit(); 28 None 29 } 30 } 31 }) { 32 Ok(Some(e)) => Some(e), 33 Ok(None) => None, 34 Err(_) => { 35 errorf!("librustc panicked\n"); 36 None 37 } 38 } 39 } 40 syn_expr(input: &str) -> Option<syn::Expr>41pub fn syn_expr(input: &str) -> Option<syn::Expr> { 42 match syn::parse_str(input) { 43 Ok(e) => Some(e), 44 Err(msg) => { 45 errorf!("syn failed to parse\n{:?}\n", msg); 46 None 47 } 48 } 49 } 50