blob: 1c59f632974618f58181ea068ebd5f9577094536 [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// Copyright 2018 Syn Developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
David Tolnayc7a5d3d2017-06-04 12:11:05 -07009extern crate proc_macro2;
Michael Layzell53fc31a2017-06-07 09:21:53 -040010extern crate syn;
David Tolnaycfa5cc02017-11-13 01:05:11 -080011extern crate syntax;
David Tolnaye9594252017-12-17 21:53:42 -080012extern crate syntax_pos;
Michael Layzell53fc31a2017-06-07 09:21:53 -040013
David Tolnaycfa5cc02017-11-13 01:05:11 -080014use self::syntax::ast;
David Tolnayeb7d79b2018-03-31 22:52:17 +020015use self::syntax::parse::{self, ParseSess};
16use self::syntax::ptr::P;
David Tolnayd1c31cc2018-08-24 14:47:15 -040017use self::syntax::source_map::FilePathMapping;
David Tolnay5d871d92018-07-31 21:43:00 -070018use self::syntax_pos::edition::Edition;
David Tolnaya25d1dc2018-07-31 21:46:19 -070019use self::syntax_pos::{hygiene, FileName};
Michael Layzell53fc31a2017-06-07 09:21:53 -040020
21use std::panic;
22
David Tolnaycfa5cc02017-11-13 01:05:11 -080023pub fn libsyntax_expr(input: &str) -> Option<P<ast::Expr>> {
Michael Layzell53fc31a2017-06-07 09:21:53 -040024 match panic::catch_unwind(|| {
David Tolnay5d871d92018-07-31 21:43:00 -070025 hygiene::set_default_edition(Edition::Edition2018);
Michael Layzell53fc31a2017-06-07 09:21:53 -040026 let sess = ParseSess::new(FilePathMapping::empty());
27 sess.span_diagnostic.set_continue_after_error(false);
David Tolnay0bb614f2018-06-14 21:41:43 -070028 let e = parse::new_parser_from_source_str(
29 &sess,
David Tolnaye9594252017-12-17 21:53:42 -080030 FileName::Custom("test_precedence".to_string()),
Michael Layzell53fc31a2017-06-07 09:21:53 -040031 input.to_string(),
David Tolnayfb84fc02018-10-02 21:01:30 -070032 )
33 .parse_expr();
David Tolnay57a28862018-07-31 21:47:10 -070034 match e {
35 Ok(expr) => Some(expr),
Michael Layzell53fc31a2017-06-07 09:21:53 -040036 Err(mut diagnostic) => {
David Tolnay57a28862018-07-31 21:47:10 -070037 diagnostic.emit();
38 None
Michael Layzell53fc31a2017-06-07 09:21:53 -040039 }
David Tolnay57a28862018-07-31 21:47:10 -070040 }
Michael Layzell53fc31a2017-06-07 09:21:53 -040041 }) {
42 Ok(Some(e)) => Some(e),
David Tolnay51382052017-12-27 13:46:21 -050043 Ok(None) => None,
Michael Layzell53fc31a2017-06-07 09:21:53 -040044 Err(_) => {
David Tolnayee97dbf2017-11-19 14:24:38 -080045 errorf!("libsyntax panicked\n");
Michael Layzell53fc31a2017-06-07 09:21:53 -040046 None
47 }
48 }
49}
50
51pub fn syn_expr(input: &str) -> Option<syn::Expr> {
David Tolnayc7a5d3d2017-06-04 12:11:05 -070052 match syn::parse_str(input) {
Michael Layzell53fc31a2017-06-07 09:21:53 -040053 Ok(e) => Some(e),
54 Err(msg) => {
55 errorf!("syn failed to parse\n{:?}\n", msg);
56 None
57 }
58 }
59}