]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/util/parser_testing.rs
Allow a dirty MirBuilt for make_extern and make_method_extern
[rust.git] / src / libsyntax / util / parser_testing.rs
1 use crate::ast::{self, Ident};
2 use crate::source_map::FilePathMapping;
3 use crate::parse::{ParseSess, PResult, source_file_to_stream};
4 use crate::parse::{lexer, new_parser_from_source_str};
5 use crate::parse::parser::Parser;
6 use crate::ptr::P;
7 use crate::tokenstream::TokenStream;
8
9 use std::iter::Peekable;
10 use std::path::PathBuf;
11
12 /// Map a string to tts, using a made-up filename:
13 pub fn string_to_stream(source_str: String) -> TokenStream {
14     let ps = ParseSess::new(FilePathMapping::empty());
15     source_file_to_stream(&ps, ps.source_map()
16                              .new_source_file(PathBuf::from("bogofile").into(), source_str), None)
17 }
18
19 /// Map string to parser (via tts)
20 pub fn string_to_parser<'a>(ps: &'a ParseSess, source_str: String) -> Parser<'a> {
21     new_parser_from_source_str(ps, PathBuf::from("bogofile").into(), source_str)
22 }
23
24 fn with_error_checking_parse<'a, T, F>(s: String, ps: &'a ParseSess, f: F) -> T where
25     F: FnOnce(&mut Parser<'a>) -> PResult<'a, T>,
26 {
27     let mut p = string_to_parser(&ps, s);
28     let x = panictry!(f(&mut p));
29     p.sess.span_diagnostic.abort_if_errors();
30     x
31 }
32
33 /// Parse a string, return a crate.
34 pub fn string_to_crate (source_str : String) -> ast::Crate {
35     let ps = ParseSess::new(FilePathMapping::empty());
36     with_error_checking_parse(source_str, &ps, |p| {
37         p.parse_crate_mod()
38     })
39 }
40
41 /// Parse a string, return an expr
42 pub fn string_to_expr (source_str : String) -> P<ast::Expr> {
43     let ps = ParseSess::new(FilePathMapping::empty());
44     with_error_checking_parse(source_str, &ps, |p| {
45         p.parse_expr()
46     })
47 }
48
49 /// Parse a string, return an item
50 pub fn string_to_item (source_str : String) -> Option<P<ast::Item>> {
51     let ps = ParseSess::new(FilePathMapping::empty());
52     with_error_checking_parse(source_str, &ps, |p| {
53         p.parse_item()
54     })
55 }
56
57 /// Parse a string, return a pat. Uses "irrefutable"... which doesn't
58 /// (currently) affect parsing.
59 pub fn string_to_pat(source_str: String) -> P<ast::Pat> {
60     let ps = ParseSess::new(FilePathMapping::empty());
61     with_error_checking_parse(source_str, &ps, |p| {
62         p.parse_pat(None)
63     })
64 }
65
66 /// Convert a vector of strings to a vector of Ident's
67 pub fn strs_to_idents(ids: Vec<&str> ) -> Vec<Ident> {
68     ids.iter().map(|u| Ident::from_str(*u)).collect()
69 }
70
71 /// Does the given string match the pattern? whitespace in the first string
72 /// may be deleted or replaced with other whitespace to match the pattern.
73 /// This function is relatively Unicode-ignorant; fortunately, the careful design
74 /// of UTF-8 mitigates this ignorance. It doesn't do NKF-normalization(?).
75 pub fn matches_codepattern(a : &str, b : &str) -> bool {
76     let mut a_iter = a.chars().peekable();
77     let mut b_iter = b.chars().peekable();
78
79     loop {
80         let (a, b) = match (a_iter.peek(), b_iter.peek()) {
81             (None, None) => return true,
82             (None, _) => return false,
83             (Some(&a), None) => {
84                 if is_pattern_whitespace(a) {
85                     break // trailing whitespace check is out of loop for borrowck
86                 } else {
87                     return false
88                 }
89             }
90             (Some(&a), Some(&b)) => (a, b)
91         };
92
93         if is_pattern_whitespace(a) && is_pattern_whitespace(b) {
94             // skip whitespace for a and b
95             scan_for_non_ws_or_end(&mut a_iter);
96             scan_for_non_ws_or_end(&mut b_iter);
97         } else if is_pattern_whitespace(a) {
98             // skip whitespace for a
99             scan_for_non_ws_or_end(&mut a_iter);
100         } else if a == b {
101             a_iter.next();
102             b_iter.next();
103         } else {
104             return false
105         }
106     }
107
108     // check if a has *only* trailing whitespace
109     a_iter.all(is_pattern_whitespace)
110 }
111
112 /// Advances the given peekable `Iterator` until it reaches a non-whitespace character
113 fn scan_for_non_ws_or_end<I: Iterator<Item= char>>(iter: &mut Peekable<I>) {
114     while lexer::is_pattern_whitespace(iter.peek().cloned()) {
115         iter.next();
116     }
117 }
118
119 pub fn is_pattern_whitespace(c: char) -> bool {
120     lexer::is_pattern_whitespace(Some(c))
121 }
122
123 #[cfg(test)]
124 mod tests {
125     use super::*;
126
127     #[test]
128     fn eqmodws() {
129         assert_eq!(matches_codepattern("",""),true);
130         assert_eq!(matches_codepattern("","a"),false);
131         assert_eq!(matches_codepattern("a",""),false);
132         assert_eq!(matches_codepattern("a","a"),true);
133         assert_eq!(matches_codepattern("a b","a   \n\t\r  b"),true);
134         assert_eq!(matches_codepattern("a b ","a   \n\t\r  b"),true);
135         assert_eq!(matches_codepattern("a b","a   \n\t\r  b "),false);
136         assert_eq!(matches_codepattern("a   b","a b"),true);
137         assert_eq!(matches_codepattern("ab","a b"),false);
138         assert_eq!(matches_codepattern("a   b","ab"),true);
139         assert_eq!(matches_codepattern(" a   b","ab"),true);
140     }
141
142     #[test]
143     fn pattern_whitespace() {
144         assert_eq!(matches_codepattern("","\x0C"), false);
145         assert_eq!(matches_codepattern("a b ","a   \u{0085}\n\t\r  b"),true);
146         assert_eq!(matches_codepattern("a b","a   \u{0085}\n\t\r  b "),false);
147     }
148
149     #[test]
150     fn non_pattern_whitespace() {
151         // These have the property 'White_Space' but not 'Pattern_White_Space'
152         assert_eq!(matches_codepattern("a b","a\u{2002}b"), false);
153         assert_eq!(matches_codepattern("a   b","a\u{2002}b"), false);
154         assert_eq!(matches_codepattern("\u{205F}a   b","ab"), false);
155         assert_eq!(matches_codepattern("a  \u{3000}b","ab"), false);
156     }
157 }