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(&ps, ps.source_map()
16 .new_source_file(PathBuf::from("bogofile").into(), source_str), None)
19 /// Map string to parser (via tts)
20 pub fn string_to_parser<'a>(ps: &'a ParseSess, source_str: String) -> Parser<'a> {
21 new_parser_from_source_str(ps, PathBuf::from("bogofile").into(), source_str)
24 fn with_error_checking_parse<'a, T, F>(s: String, ps: &'a ParseSess, f: F) -> T where
25 F: FnOnce(&mut Parser<'a>) -> PResult<'a, T>,
27 let mut p = string_to_parser(&ps, s);
28 let x = panictry!(f(&mut p));
29 p.sess.span_diagnostic.abort_if_errors();
33 /// Parse a string, return a crate.
34 pub fn string_to_crate (source_str : String) -> ast::Crate {
35 let ps = ParseSess::new(FilePathMapping::empty());
36 with_error_checking_parse(source_str, &ps, |p| {
41 /// Parse a string, return an expr
42 pub fn string_to_expr (source_str : String) -> P<ast::Expr> {
43 let ps = ParseSess::new(FilePathMapping::empty());
44 with_error_checking_parse(source_str, &ps, |p| {
49 /// Parse a string, return an item
50 pub fn string_to_item (source_str : String) -> Option<P<ast::Item>> {
51 let ps = ParseSess::new(FilePathMapping::empty());
52 with_error_checking_parse(source_str, &ps, |p| {
57 /// Parse a string, return a pat. Uses "irrefutable"... which doesn't
58 /// (currently) affect parsing.
59 pub fn string_to_pat(source_str: String) -> P<ast::Pat> {
60 let ps = ParseSess::new(FilePathMapping::empty());
61 with_error_checking_parse(source_str, &ps, |p| {
66 /// Convert a vector of strings to a vector of Ident's
67 pub fn strs_to_idents(ids: Vec<&str> ) -> Vec<Ident> {
68 ids.iter().map(|u| Ident::from_str(*u)).collect()
71 /// Does the given string match the pattern? whitespace in the first string
72 /// may be deleted or replaced with other whitespace to match the pattern.
73 /// This function is relatively Unicode-ignorant; fortunately, the careful design
74 /// of UTF-8 mitigates this ignorance. It doesn't do NKF-normalization(?).
75 pub fn matches_codepattern(a : &str, b : &str) -> bool {
76 let mut a_iter = a.chars().peekable();
77 let mut b_iter = b.chars().peekable();
80 let (a, b) = match (a_iter.peek(), b_iter.peek()) {
81 (None, None) => return true,
82 (None, _) => return false,
84 if is_pattern_whitespace(a) {
85 break // trailing whitespace check is out of loop for borrowck
90 (Some(&a), Some(&b)) => (a, b)
93 if is_pattern_whitespace(a) && is_pattern_whitespace(b) {
94 // skip whitespace for a and b
95 scan_for_non_ws_or_end(&mut a_iter);
96 scan_for_non_ws_or_end(&mut b_iter);
97 } else if is_pattern_whitespace(a) {
98 // skip whitespace for a
99 scan_for_non_ws_or_end(&mut a_iter);
108 // check if a has *only* trailing whitespace
109 a_iter.all(is_pattern_whitespace)
112 /// Advances the given peekable `Iterator` until it reaches a non-whitespace character
113 fn scan_for_non_ws_or_end<I: Iterator<Item= char>>(iter: &mut Peekable<I>) {
114 while lexer::is_pattern_whitespace(iter.peek().cloned()) {
119 pub fn is_pattern_whitespace(c: char) -> bool {
120 lexer::is_pattern_whitespace(Some(c))
129 assert_eq!(matches_codepattern("",""),true);
130 assert_eq!(matches_codepattern("","a"),false);
131 assert_eq!(matches_codepattern("a",""),false);
132 assert_eq!(matches_codepattern("a","a"),true);
133 assert_eq!(matches_codepattern("a b","a \n\t\r b"),true);
134 assert_eq!(matches_codepattern("a b ","a \n\t\r b"),true);
135 assert_eq!(matches_codepattern("a b","a \n\t\r b "),false);
136 assert_eq!(matches_codepattern("a b","a b"),true);
137 assert_eq!(matches_codepattern("ab","a b"),false);
138 assert_eq!(matches_codepattern("a b","ab"),true);
139 assert_eq!(matches_codepattern(" a b","ab"),true);
143 fn pattern_whitespace() {
144 assert_eq!(matches_codepattern("","\x0C"), false);
145 assert_eq!(matches_codepattern("a b ","a \u{0085}\n\t\r b"),true);
146 assert_eq!(matches_codepattern("a b","a \u{0085}\n\t\r b "),false);
150 fn non_pattern_whitespace() {
151 // These have the property 'White_Space' but not 'Pattern_White_Space'
152 assert_eq!(matches_codepattern("a b","a\u{2002}b"), false);
153 assert_eq!(matches_codepattern("a b","a\u{2002}b"), false);
154 assert_eq!(matches_codepattern("\u{205F}a b","ab"), false);
155 assert_eq!(matches_codepattern("a \u{3000}b","ab"), false);