9
9
// except according to those terms.
10
10
11
11
use ast;
12
- use parse:: { ParseSess , filemap_to_tts} ;
12
+ use parse:: { ParseSess , PResult , filemap_to_tts} ;
13
13
use parse:: new_parser_from_source_str;
14
14
use parse:: parser:: Parser ;
15
15
use parse:: token;
@@ -31,51 +31,49 @@ pub fn string_to_parser<'a>(ps: &'a ParseSess, source_str: String) -> Parser<'a>
31
31
}
32
32
33
33
fn with_error_checking_parse < T , F > ( s : String , f : F ) -> T where
34
- F : FnOnce ( & mut Parser ) -> T ,
34
+ F : FnOnce ( & mut Parser ) -> PResult < T > ,
35
35
{
36
36
let ps = ParseSess :: new ( ) ;
37
37
let mut p = string_to_parser ( & ps, s) ;
38
- let x = f ( & mut p) ;
38
+ let x = panictry ! ( f( & mut p) ) ;
39
39
p. abort_if_errors ( ) ;
40
40
x
41
41
}
42
42
43
43
/// Parse a string, return a crate.
44
44
pub fn string_to_crate ( source_str : String ) -> ast:: Crate {
45
45
with_error_checking_parse ( source_str, |p| {
46
- panictry ! ( p. parse_crate_mod( ) )
46
+ p. parse_crate_mod ( )
47
47
} )
48
48
}
49
49
50
50
/// Parse a string, return an expr
51
51
pub fn string_to_expr ( source_str : String ) -> P < ast:: Expr > {
52
52
with_error_checking_parse ( source_str, |p| {
53
- p. parse_expr ( )
53
+ p. parse_expr_nopanic ( )
54
54
} )
55
55
}
56
56
57
57
/// Parse a string, return an item
58
58
pub fn string_to_item ( source_str : String ) -> Option < P < ast:: Item > > {
59
59
with_error_checking_parse ( source_str, |p| {
60
- p. parse_item ( )
60
+ p. parse_item_nopanic ( )
61
61
} )
62
62
}
63
63
64
64
/// Parse a string, return a stmt
65
- pub fn string_to_stmt ( source_str : String ) -> P < ast:: Stmt > {
65
+ pub fn string_to_stmt ( source_str : String ) -> Option < P < ast:: Stmt > > {
66
66
with_error_checking_parse ( source_str, |p| {
67
- p. parse_stmt ( ) . unwrap ( )
67
+ p. parse_stmt_nopanic ( )
68
68
} )
69
69
}
70
70
71
71
/// Parse a string, return a pat. Uses "irrefutable"... which doesn't
72
72
/// (currently) affect parsing.
73
73
pub fn string_to_pat ( source_str : String ) -> P < ast:: Pat > {
74
- // Binding `sess` and `parser` works around dropck-injected
75
- // region-inference issues; see #25212, #22323, #22321.
76
- let sess = ParseSess :: new ( ) ;
77
- let mut parser = string_to_parser ( & sess, source_str) ;
78
- parser. parse_pat ( )
74
+ with_error_checking_parse ( source_str, |p| {
75
+ p. parse_pat_nopanic ( )
76
+ } )
79
77
}
80
78
81
79
/// Convert a vector of strings to a vector of ast::Ident's
0 commit comments