blob: 15c2faffae547bd6557c345d40f808da0b3b53f0 [file] [log] [blame]
Michael Layzell53fc31a2017-06-07 09:21:53 -04001extern crate syn;
2extern crate syntex_syntax;
3
4use self::syntex_syntax::ast;
5use self::syntex_syntax::ptr::P;
6use self::syntex_syntax::parse::{self, ParseSess};
7use self::syntex_syntax::codemap::FilePathMapping;
8
9use std::panic;
10
11pub fn syntex_expr(input: &str) -> Option<P<ast::Expr>> {
12 match panic::catch_unwind(|| {
13 let sess = ParseSess::new(FilePathMapping::empty());
14 sess.span_diagnostic.set_continue_after_error(false);
15 let e = parse::parse_expr_from_source_str(
16 "test_precedence".to_string(),
17 input.to_string(),
18 &sess,
19 );
20 Some(match e {
21 Ok(expr) => expr,
22 Err(mut diagnostic) => {
23 diagnostic.emit();;
24 return None
25 }
26 })
27 }) {
28 Ok(Some(e)) => Some(e),
29 Ok(None) => {
30 None
31 }
32 Err(_) => {
33 errorf!("syntex paniced\n");
34 None
35 }
36 }
37}
38
39pub fn syn_expr(input: &str) -> Option<syn::Expr> {
40 match input.parse::<syn::Expr>() {
41 Ok(e) => Some(e),
42 Err(msg) => {
43 errorf!("syn failed to parse\n{:?}\n", msg);
44 None
45 }
46 }
47}