]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/util/parser_testing.rs
libsyntax: Mechanically change `~[T]` to `Vec<T>`
[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
18 // map a string to tts, using a made-up filename: return both the TokenTree's
19 // and the ParseSess
20 pub fn string_to_tts_and_sess (source_str : ~str) -> (Vec<ast::TokenTree> , @ParseSess) {
21     let ps = new_parse_sess();
22     (filemap_to_tts(ps,string_to_filemap(ps,source_str,~"bogofile")),ps)
23 }
24
25 // map a string to tts, using a made-up filename:
26 pub fn string_to_tts(source_str : ~str) -> Vec<ast::TokenTree> {
27     let (tts,_) = string_to_tts_and_sess(source_str);
28     tts
29 }
30
31 pub fn string_to_parser_and_sess(source_str: ~str) -> (Parser,@ParseSess) {
32     let ps = new_parse_sess();
33     (new_parser_from_source_str(ps,Vec::new(),~"bogofile",source_str),ps)
34 }
35
36 // map string to parser (via tts)
37 pub fn string_to_parser(source_str: ~str) -> Parser {
38     let (p,_) = string_to_parser_and_sess(source_str);
39     p
40 }
41
42 fn with_error_checking_parse<T>(s: ~str, f: |&mut Parser| -> T) -> T {
43     let mut p = string_to_parser(s);
44     let x = f(&mut p);
45     p.abort_if_errors();
46     x
47 }
48
49 // parse a string, return a crate.
50 pub fn string_to_crate (source_str : ~str) -> ast::Crate {
51     with_error_checking_parse(source_str, |p| {
52         p.parse_crate_mod()
53     })
54 }
55
56 // parse a string, return a crate and the ParseSess
57 pub fn string_to_crate_and_sess (source_str : ~str) -> (ast::Crate,@ParseSess) {
58     let (mut p,ps) = string_to_parser_and_sess(source_str);
59     (p.parse_crate_mod(),ps)
60 }
61
62 // parse a string, return an expr
63 pub fn string_to_expr (source_str : ~str) -> @ast::Expr {
64     with_error_checking_parse(source_str, |p| {
65         p.parse_expr()
66     })
67 }
68
69 // parse a string, return an item
70 pub fn string_to_item (source_str : ~str) -> Option<@ast::Item> {
71     with_error_checking_parse(source_str, |p| {
72         p.parse_item(Vec::new())
73     })
74 }
75
76 // parse a string, return a stmt
77 pub fn string_to_stmt(source_str : ~str) -> @ast::Stmt {
78     with_error_checking_parse(source_str, |p| {
79         p.parse_stmt(Vec::new())
80     })
81 }
82
83 // parse a string, return a pat. Uses "irrefutable"... which doesn't
84 // (currently) affect parsing.
85 pub fn string_to_pat(source_str : ~str) -> @ast::Pat {
86     string_to_parser(source_str).parse_pat()
87 }
88
89 // convert a vector of strings to a vector of ast::Ident's
90 pub fn strs_to_idents(ids: Vec<&str> ) -> Vec<ast::Ident> {
91     ids.map(|u| token::str_to_ident(*u))
92 }
93
94 // does the given string match the pattern? whitespace in the first string
95 // may be deleted or replaced with other whitespace to match the pattern.
96 // this function is unicode-ignorant; fortunately, the careful design of
97 // UTF-8 mitigates this ignorance.  In particular, this function only collapses
98 // sequences of \n, \r, ' ', and \t, but it should otherwise tolerate unicode
99 // chars. Unsurprisingly, it doesn't do NKF-normalization(?).
100 pub fn matches_codepattern(a : &str, b : &str) -> bool {
101     let mut idx_a = 0;
102     let mut idx_b = 0;
103     loop {
104         if idx_a == a.len() && idx_b == b.len() {
105             return true;
106         }
107         else if idx_a == a.len() {return false;}
108         else if idx_b == b.len() {
109             // maybe the stuff left in a is all ws?
110             if is_whitespace(a.char_at(idx_a)) {
111                 return scan_for_non_ws_or_end(a,idx_a) == a.len();
112             } else {
113                 return false;
114             }
115         }
116         // ws in both given and pattern:
117         else if is_whitespace(a.char_at(idx_a))
118            && is_whitespace(b.char_at(idx_b)) {
119             idx_a = scan_for_non_ws_or_end(a,idx_a);
120             idx_b = scan_for_non_ws_or_end(b,idx_b);
121         }
122         // ws in given only:
123         else if is_whitespace(a.char_at(idx_a)) {
124             idx_a = scan_for_non_ws_or_end(a,idx_a);
125         }
126         // *don't* silently eat ws in expected only.
127         else if a.char_at(idx_a) == b.char_at(idx_b) {
128             idx_a += 1;
129             idx_b += 1;
130         }
131         else {
132             return false;
133         }
134     }
135 }
136
137 // given a string and an index, return the first uint >= idx
138 // that is a non-ws-char or is outside of the legal range of
139 // the string.
140 fn scan_for_non_ws_or_end(a : &str, idx: uint) -> uint {
141     let mut i = idx;
142     let len = a.len();
143     while (i < len) && (is_whitespace(a.char_at(i))) {
144         i += 1;
145     }
146     i
147 }
148
149 // copied from lexer.
150 pub fn is_whitespace(c: char) -> bool {
151     return c == ' ' || c == '\t' || c == '\r' || c == '\n';
152 }
153
154 #[cfg(test)]
155 mod test {
156     use super::*;
157
158     #[test] fn eqmodws() {
159         assert_eq!(matches_codepattern("",""),true);
160         assert_eq!(matches_codepattern("","a"),false);
161         assert_eq!(matches_codepattern("a",""),false);
162         assert_eq!(matches_codepattern("a","a"),true);
163         assert_eq!(matches_codepattern("a b","a   \n\t\r  b"),true);
164         assert_eq!(matches_codepattern("a b ","a   \n\t\r  b"),true);
165         assert_eq!(matches_codepattern("a b","a   \n\t\r  b "),false);
166         assert_eq!(matches_codepattern("a   b","a b"),true);
167         assert_eq!(matches_codepattern("ab","a b"),false);
168         assert_eq!(matches_codepattern("a   b","ab"),true);
169     }
170 }