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