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