]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/util/parser_testing.rs
Auto merge of #24865 - bluss:range-size, r=alexcrichton
[rust.git] / src / libsyntax / util / parser_testing.rs
1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use ast;
12 use parse::new_parse_sess;
13 use parse::{ParseSess,string_to_filemap,filemap_to_tts};
14 use parse::new_parser_from_source_str;
15 use parse::parser::Parser;
16 use parse::token;
17 use ptr::P;
18 use str::char_at;
19
20 /// Map a string to tts, using a made-up filename:
21 pub fn string_to_tts(source_str: String) -> Vec<ast::TokenTree> {
22     let ps = new_parse_sess();
23     filemap_to_tts(&ps,
24                    string_to_filemap(&ps, source_str, "bogofile".to_string()))
25 }
26
27 /// Map string to parser (via tts)
28 pub fn string_to_parser<'a>(ps: &'a ParseSess, source_str: String) -> Parser<'a> {
29     new_parser_from_source_str(ps,
30                                Vec::new(),
31                                "bogofile".to_string(),
32                                source_str)
33 }
34
35 fn with_error_checking_parse<T, F>(s: String, f: F) -> T where
36     F: FnOnce(&mut Parser) -> T,
37 {
38     let ps = new_parse_sess();
39     let mut p = string_to_parser(&ps, s);
40     let x = f(&mut p);
41     p.abort_if_errors();
42     x
43 }
44
45 /// Parse a string, return a crate.
46 pub fn string_to_crate (source_str : String) -> ast::Crate {
47     with_error_checking_parse(source_str, |p| {
48         panictry!(p.parse_crate_mod())
49     })
50 }
51
52 /// Parse a string, return an expr
53 pub fn string_to_expr (source_str : String) -> P<ast::Expr> {
54     with_error_checking_parse(source_str, |p| {
55         p.parse_expr()
56     })
57 }
58
59 /// Parse a string, return an item
60 pub fn string_to_item (source_str : String) -> Option<P<ast::Item>> {
61     with_error_checking_parse(source_str, |p| {
62         p.parse_item()
63     })
64 }
65
66 /// Parse a string, return a stmt
67 pub fn string_to_stmt(source_str : String) -> P<ast::Stmt> {
68     with_error_checking_parse(source_str, |p| {
69         p.parse_stmt().unwrap()
70     })
71 }
72
73 /// Parse a string, return a pat. Uses "irrefutable"... which doesn't
74 /// (currently) affect parsing.
75 pub fn string_to_pat(source_str: String) -> P<ast::Pat> {
76     string_to_parser(&new_parse_sess(), source_str).parse_pat()
77 }
78
79 /// Convert a vector of strings to a vector of ast::Ident's
80 pub fn strs_to_idents(ids: Vec<&str> ) -> Vec<ast::Ident> {
81     ids.iter().map(|u| token::str_to_ident(*u)).collect()
82 }
83
84 /// Does the given string match the pattern? whitespace in the first string
85 /// may be deleted or replaced with other whitespace to match the pattern.
86 /// this function is Unicode-ignorant; fortunately, the careful design of
87 /// UTF-8 mitigates this ignorance.  In particular, this function only collapses
88 /// sequences of \n, \r, ' ', and \t, but it should otherwise tolerate Unicode
89 /// chars. Unsurprisingly, it doesn't do NKF-normalization(?).
90 pub fn matches_codepattern(a : &str, b : &str) -> bool {
91     let mut idx_a = 0;
92     let mut idx_b = 0;
93     loop {
94         if idx_a == a.len() && idx_b == b.len() {
95             return true;
96         }
97         else if idx_a == a.len() {return false;}
98         else if idx_b == b.len() {
99             // maybe the stuff left in a is all ws?
100             if is_whitespace(char_at(a, idx_a)) {
101                 return scan_for_non_ws_or_end(a,idx_a) == a.len();
102             } else {
103                 return false;
104             }
105         }
106         // ws in both given and pattern:
107         else if is_whitespace(char_at(a, idx_a))
108            && is_whitespace(char_at(b, idx_b)) {
109             idx_a = scan_for_non_ws_or_end(a,idx_a);
110             idx_b = scan_for_non_ws_or_end(b,idx_b);
111         }
112         // ws in given only:
113         else if is_whitespace(char_at(a, idx_a)) {
114             idx_a = scan_for_non_ws_or_end(a,idx_a);
115         }
116         // *don't* silently eat ws in expected only.
117         else if char_at(a, idx_a) == char_at(b, idx_b) {
118             idx_a += 1;
119             idx_b += 1;
120         }
121         else {
122             return false;
123         }
124     }
125 }
126
127 /// Given a string and an index, return the first usize >= idx
128 /// that is a non-ws-char or is outside of the legal range of
129 /// the string.
130 fn scan_for_non_ws_or_end(a : &str, idx: usize) -> usize {
131     let mut i = idx;
132     let len = a.len();
133     while (i < len) && (is_whitespace(char_at(a, i))) {
134         i += 1;
135     }
136     i
137 }
138
139 /// Copied from lexer.
140 pub fn is_whitespace(c: char) -> bool {
141     return c == ' ' || c == '\t' || c == '\r' || c == '\n';
142 }
143
144 #[cfg(test)]
145 mod tests {
146     use super::*;
147
148     #[test] fn eqmodws() {
149         assert_eq!(matches_codepattern("",""),true);
150         assert_eq!(matches_codepattern("","a"),false);
151         assert_eq!(matches_codepattern("a",""),false);
152         assert_eq!(matches_codepattern("a","a"),true);
153         assert_eq!(matches_codepattern("a b","a   \n\t\r  b"),true);
154         assert_eq!(matches_codepattern("a b ","a   \n\t\r  b"),true);
155         assert_eq!(matches_codepattern("a b","a   \n\t\r  b "),false);
156         assert_eq!(matches_codepattern("a   b","a b"),true);
157         assert_eq!(matches_codepattern("ab","a b"),false);
158         assert_eq!(matches_codepattern("a   b","ab"),true);
159     }
160 }