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: return both the TokenTree's
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)
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);
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)
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);
44 fn with_error_checking_parse<T>(s: ~str, f: |&mut Parser| -> T) -> T {
45 let mut p = string_to_parser(s);
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| {
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)
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| {
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())
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())
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()
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))
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 {
106 if idx_a == a.len() && idx_b == b.len() {
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();
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);
125 else if is_whitespace(a.char_at(idx_a)) {
126 idx_a = scan_for_non_ws_or_end(a,idx_a);
128 // *don't* silently eat ws in expected only.
129 else if a.char_at(idx_a) == b.char_at(idx_b) {
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
142 fn scan_for_non_ws_or_end(a : &str, idx: uint) -> uint {
145 while (i < len) && (is_whitespace(a.char_at(i))) {
151 // copied from lexer.
152 pub fn is_whitespace(c: char) -> bool {
153 return c == ' ' || c == '\t' || c == '\r' || c == '\n';
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);