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.
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.
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;
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();
24 string_to_filemap(&ps, source_str, "bogofile".to_string()))
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,
31 "bogofile".to_string(),
35 fn with_error_checking_parse<T, F>(s: String, f: F) -> T where
36 F: FnOnce(&mut Parser) -> T,
38 let ps = new_parse_sess();
39 let mut p = string_to_parser(&ps, s);
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())
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| {
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| {
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()
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()
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()
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 {
94 if idx_a == a.len() && idx_b == b.len() {
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();
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);
113 else if is_whitespace(char_at(a, idx_a)) {
114 idx_a = scan_for_non_ws_or_end(a,idx_a);
116 // *don't* silently eat ws in expected only.
117 else if char_at(a, idx_a) == char_at(b, idx_b) {
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
130 fn scan_for_non_ws_or_end(a : &str, idx: usize) -> usize {
133 while (i < len) && (is_whitespace(char_at(a, i))) {
139 /// Copied from lexer.
140 pub fn is_whitespace(c: char) -> bool {
141 return c == ' ' || c == '\t' || c == '\r' || c == '\n';
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);