Michael Layzell | 53fc31a | 2017-06-07 09:21:53 -0400 | [diff] [blame] | 1 | extern crate syn; |
| 2 | extern crate syntex_syntax; |
| 3 | |
| 4 | use self::syntex_syntax::ast; |
| 5 | use self::syntex_syntax::ptr::P; |
| 6 | use self::syntex_syntax::parse::{self, ParseSess}; |
| 7 | use self::syntex_syntax::codemap::FilePathMapping; |
| 8 | |
| 9 | use std::panic; |
| 10 | |
| 11 | pub 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 | |
| 39 | pub 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 | } |