1 | extern crate rustc_ast; |
2 | extern crate rustc_expand; |
3 | extern crate rustc_parse as parse; |
4 | extern crate rustc_session; |
5 | extern crate rustc_span; |
6 | |
7 | use rustc_ast::ast; |
8 | use rustc_ast::ptr::P; |
9 | use rustc_session::parse::ParseSess; |
10 | use rustc_span::source_map::FilePathMapping; |
11 | use rustc_span::FileName; |
12 | use std::panic; |
13 | |
14 | pub 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 | |
40 | pub 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 | |