aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / syn / tests / common / parse.rs
| 1 | extern crate rustc_ast; |
| 2 | extern crate rustc_driver; |
| 3 | extern crate rustc_expand; |
| 4 | extern crate rustc_parse; |
| 5 | extern crate rustc_session; |
| 6 | extern crate rustc_span; |
| 7 | |
| 8 | use rustc_ast::ast; |
| 9 | use rustc_parse::lexer::StripTokens; |
| 10 | use rustc_session::parse::ParseSess; |
| 11 | use rustc_span::FileName; |
| 12 | use std::panic; |
| 13 | |
| 14 | pub fn librustc_expr(input: &str) -> Option<Box<ast::Expr>> { |
| 15 | match panic::catch_unwind(|| { |
| 16 | let sess = ParseSess::new(); |
| 17 | let name = FileName::Custom("test_precedence".to_string()); |
| 18 | let mut parser = rustc_parse::new_parser_from_source_str( |
| 19 | &sess, |
| 20 | name, |
| 21 | input.to_string(), |
| 22 | StripTokens::ShebangAndFrontmatter, |
| 23 | ) |
| 24 | .unwrap(); |
| 25 | let presult = parser.parse_expr(); |
| 26 | match presult { |
| 27 | Ok(expr) => Some(expr), |
| 28 | Err(diagnostic) => { |
| 29 | diagnostic.emit(); |
| 30 | None |
| 31 | } |
| 32 | } |
| 33 | }) { |
| 34 | Ok(Some(e)) => Some(e), |
| 35 | Ok(None) => None, |
| 36 | Err(_) => { |
| 37 | errorf!("librustc panicked\n"); |
| 38 | None |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | pub fn syn_expr(input: &str) -> Option<syn::Expr> { |
| 44 | match syn::parse_str(input) { |
| 45 | Ok(e) => Some(e), |
| 46 | Err(msg) => { |
| 47 | errorf!("syn failed to parse\n{:?}\n", msg); |
| 48 | None |
| 49 | } |
| 50 | } |
| 51 | } |