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