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>(s: String, f: |&mut Parser| -> T) -> T {
36 let ps = new_parse_sess();
37 let mut p = string_to_parser(&ps, s);
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| {
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| {
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())
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())
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()
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()
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 {
92 if idx_a == a.len() && idx_b == b.len() {
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();
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);
111 else if is_whitespace(a.char_at(idx_a)) {
112 idx_a = scan_for_non_ws_or_end(a,idx_a);
114 // *don't* silently eat ws in expected only.
115 else if a.char_at(idx_a) == b.char_at(idx_b) {
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
128 fn scan_for_non_ws_or_end(a : &str, idx: uint) -> uint {
131 while (i < len) && (is_whitespace(a.char_at(i))) {
137 // copied from lexer.
138 pub fn is_whitespace(c: char) -> bool {
139 return c == ' ' || c == '\t' || c == '\r' || c == '\n';
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);