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;
18 // map a string to tts, using a made-up filename: return both the TokenTree's
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)
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);
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)
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);
42 fn with_error_checking_parse<T>(s: ~str, f: |&mut Parser| -> T) -> T {
43 let mut p = string_to_parser(s);
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| {
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)
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| {
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())
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())
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()
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))
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 {
104 if idx_a == a.len() && idx_b == b.len() {
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();
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);
123 else if is_whitespace(a.char_at(idx_a)) {
124 idx_a = scan_for_non_ws_or_end(a,idx_a);
126 // *don't* silently eat ws in expected only.
127 else if a.char_at(idx_a) == b.char_at(idx_b) {
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
140 fn scan_for_non_ws_or_end(a : &str, idx: uint) -> uint {
143 while (i < len) && (is_whitespace(a.char_at(i))) {
149 // copied from lexer.
150 pub fn is_whitespace(c: char) -> bool {
151 return c == ' ' || c == '\t' || c == '\r' || c == '\n';
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);