1 use crate::ast::{self, Ident};
2 use crate::source_map::FilePathMapping;
3 use crate::parse::{ParseSess, PResult, source_file_to_stream};
4 use crate::parse::{lexer, new_parser_from_source_str};
5 use crate::parse::parser::Parser;
7 use crate::tokenstream::TokenStream;
9 use std::iter::Peekable;
10 use std::path::PathBuf;
12 /// Map a string to tts, using a made-up filename:
13 pub fn string_to_stream(source_str: String) -> TokenStream {
14 let ps = ParseSess::new(FilePathMapping::empty());
15 source_file_to_stream(
17 ps.source_map().new_source_file(PathBuf::from("bogofile").into(),
22 /// Map string to parser (via tts)
23 pub fn string_to_parser<'a>(ps: &'a ParseSess, source_str: String) -> Parser<'a> {
24 new_parser_from_source_str(ps, PathBuf::from("bogofile").into(), source_str)
27 fn with_error_checking_parse<'a, T, F>(s: String, ps: &'a ParseSess, f: F) -> T where
28 F: FnOnce(&mut Parser<'a>) -> PResult<'a, T>,
30 let mut p = string_to_parser(&ps, s);
31 let x = panictry!(f(&mut p));
32 p.sess.span_diagnostic.abort_if_errors();
36 /// Parse a string, return a crate.
37 pub fn string_to_crate (source_str : String) -> ast::Crate {
38 let ps = ParseSess::new(FilePathMapping::empty());
39 with_error_checking_parse(source_str, &ps, |p| {
44 /// Parse a string, return an expr
45 pub fn string_to_expr (source_str : String) -> P<ast::Expr> {
46 let ps = ParseSess::new(FilePathMapping::empty());
47 with_error_checking_parse(source_str, &ps, |p| {
52 /// Parse a string, return an item
53 pub fn string_to_item (source_str : String) -> Option<P<ast::Item>> {
54 let ps = ParseSess::new(FilePathMapping::empty());
55 with_error_checking_parse(source_str, &ps, |p| {
60 /// Parse a string, return a pat. Uses "irrefutable"... which doesn't
61 /// (currently) affect parsing.
62 pub fn string_to_pat(source_str: String) -> P<ast::Pat> {
63 let ps = ParseSess::new(FilePathMapping::empty());
64 with_error_checking_parse(source_str, &ps, |p| {
69 /// Convert a vector of strings to a vector of Ident's
70 pub fn strs_to_idents(ids: Vec<&str> ) -> Vec<Ident> {
71 ids.iter().map(|u| Ident::from_str(*u)).collect()
74 /// Does the given string match the pattern? whitespace in the first string
75 /// may be deleted or replaced with other whitespace to match the pattern.
76 /// This function is relatively Unicode-ignorant; fortunately, the careful design
77 /// of UTF-8 mitigates this ignorance. It doesn't do NKF-normalization(?).
78 pub fn matches_codepattern(a : &str, b : &str) -> bool {
79 let mut a_iter = a.chars().peekable();
80 let mut b_iter = b.chars().peekable();
83 let (a, b) = match (a_iter.peek(), b_iter.peek()) {
84 (None, None) => return true,
85 (None, _) => return false,
87 if is_pattern_whitespace(a) {
88 break // trailing whitespace check is out of loop for borrowck
93 (Some(&a), Some(&b)) => (a, b)
96 if is_pattern_whitespace(a) && is_pattern_whitespace(b) {
97 // skip whitespace for a and b
98 scan_for_non_ws_or_end(&mut a_iter);
99 scan_for_non_ws_or_end(&mut b_iter);
100 } else if is_pattern_whitespace(a) {
101 // skip whitespace for a
102 scan_for_non_ws_or_end(&mut a_iter);
111 // check if a has *only* trailing whitespace
112 a_iter.all(is_pattern_whitespace)
115 /// Advances the given peekable `Iterator` until it reaches a non-whitespace character
116 fn scan_for_non_ws_or_end<I: Iterator<Item= char>>(iter: &mut Peekable<I>) {
117 while lexer::is_pattern_whitespace(iter.peek().cloned()) {
122 pub fn is_pattern_whitespace(c: char) -> bool {
123 lexer::is_pattern_whitespace(Some(c))
132 assert_eq!(matches_codepattern("",""),true);
133 assert_eq!(matches_codepattern("","a"),false);
134 assert_eq!(matches_codepattern("a",""),false);
135 assert_eq!(matches_codepattern("a","a"),true);
136 assert_eq!(matches_codepattern("a b","a \n\t\r b"),true);
137 assert_eq!(matches_codepattern("a b ","a \n\t\r b"),true);
138 assert_eq!(matches_codepattern("a b","a \n\t\r b "),false);
139 assert_eq!(matches_codepattern("a b","a b"),true);
140 assert_eq!(matches_codepattern("ab","a b"),false);
141 assert_eq!(matches_codepattern("a b","ab"),true);
142 assert_eq!(matches_codepattern(" a b","ab"),true);
146 fn pattern_whitespace() {
147 assert_eq!(matches_codepattern("","\x0C"), false);
148 assert_eq!(matches_codepattern("a b ","a \u{0085}\n\t\r b"),true);
149 assert_eq!(matches_codepattern("a b","a \u{0085}\n\t\r b "),false);
153 fn non_pattern_whitespace() {
154 // These have the property 'White_Space' but not 'Pattern_White_Space'
155 assert_eq!(matches_codepattern("a b","a\u{2002}b"), false);
156 assert_eq!(matches_codepattern("a b","a\u{2002}b"), false);
157 assert_eq!(matches_codepattern("\u{205F}a b","ab"), false);
158 assert_eq!(matches_codepattern("a \u{3000}b","ab"), false);