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