use crate::ast::{self, Ident};
use crate::codemap::FilePathMapping;
use crate::parse::parser::Parser;
use crate::parse::{filemap_to_stream, PResult, ParseSess};
use crate::parse::{lexer, new_parser_from_source_str};
use crate::ptr::P;
use crate::tokenstream::TokenStream;
use std::iter::Peekable;
pub fn string_to_stream(source_str: String) -> TokenStream {
let ps = ParseSess::new(FilePathMapping::empty());
filemap_to_stream(
&ps,
ps.codemap().new_filemap("bogofile".to_string(), source_str),
)
}
pub fn string_to_parser<'a>(ps: &'a ParseSess, source_str: String) -> Parser<'a> {
new_parser_from_source_str(ps, "bogofile".to_string(), source_str)
}
fn with_error_checking_parse<'a, T, F>(s: String, ps: &'a ParseSess, f: F) -> T
where
F: FnOnce(&mut Parser<'a>) -> PResult<'a, T>,
{
let mut p = string_to_parser(&ps, s);
let x = panictry!(f(&mut p));
p.abort_if_errors();
x
}
pub fn string_to_crate(source_str: String) -> ast::Crate {
let ps = ParseSess::new(FilePathMapping::empty());
with_error_checking_parse(source_str, &ps, |p| p.parse_crate_mod())
}
pub fn string_to_expr(source_str: String) -> P<ast::Expr> {
let ps = ParseSess::new(FilePathMapping::empty());
with_error_checking_parse(source_str, &ps, |p| p.parse_expr())
}
pub fn string_to_item(source_str: String) -> Option<P<ast::Item>> {
let ps = ParseSess::new(FilePathMapping::empty());
with_error_checking_parse(source_str, &ps, |p| p.parse_item())
}
pub fn string_to_stmt(source_str: String) -> Option<ast::Stmt> {
let ps = ParseSess::new(FilePathMapping::empty());
with_error_checking_parse(source_str, &ps, |p| p.parse_stmt())
}
pub fn string_to_pat(source_str: String) -> P<ast::Pat> {
let ps = ParseSess::new(FilePathMapping::empty());
with_error_checking_parse(source_str, &ps, |p| p.parse_pat())
}
pub fn strs_to_idents(ids: Vec<&str>) -> Vec<Ident> {
ids.iter().map(|u| Ident::from_str(*u)).collect()
}
pub fn matches_codepattern(a: &str, b: &str) -> bool {
let mut a_iter = a.chars().peekable();
let mut b_iter = b.chars().peekable();
loop {
let (a, b) = match (a_iter.peek(), b_iter.peek()) {
(None, None) => return true,
(None, _) => return false,
(Some(&a), None) => {
if is_pattern_whitespace(a) {
break; } else {
return false;
}
}
(Some(&a), Some(&b)) => (a, b),
};
if is_pattern_whitespace(a) && is_pattern_whitespace(b) {
scan_for_non_ws_or_end(&mut a_iter);
scan_for_non_ws_or_end(&mut b_iter);
} else if is_pattern_whitespace(a) {
scan_for_non_ws_or_end(&mut a_iter);
} else if a == b {
a_iter.next();
b_iter.next();
} else {
return false;
}
}
a_iter.all(is_pattern_whitespace)
}
fn scan_for_non_ws_or_end<I: Iterator<Item = char>>(iter: &mut Peekable<I>) {
while lexer::is_pattern_whitespace(iter.peek().cloned()) {
iter.next();
}
}
pub fn is_pattern_whitespace(c: char) -> bool {
lexer::is_pattern_whitespace(Some(c))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn eqmodws() {
assert_eq!(matches_codepattern("", ""), true);
assert_eq!(matches_codepattern("", "a"), false);
assert_eq!(matches_codepattern("a", ""), false);
assert_eq!(matches_codepattern("a", "a"), true);
assert_eq!(matches_codepattern("a b", "a \n\t\r b"), true);
assert_eq!(matches_codepattern("a b ", "a \n\t\r b"), true);
assert_eq!(matches_codepattern("a b", "a \n\t\r b "), false);
assert_eq!(matches_codepattern("a b", "a b"), true);
assert_eq!(matches_codepattern("ab", "a b"), false);
assert_eq!(matches_codepattern("a b", "ab"), true);
assert_eq!(matches_codepattern(" a b", "ab"), true);
}
#[test]
fn pattern_whitespace() {
assert_eq!(matches_codepattern("", "\x0C"), false);
assert_eq!(matches_codepattern("a b", "a \u{0085}\n\t\r b "), false);
}
#[test]
fn non_pattern_whitespace() {
assert_eq!(matches_codepattern("a b", "a\u{2002}b"), false);
assert_eq!(matches_codepattern("a b", "a\u{2002}b"), false);
assert_eq!(matches_codepattern("\u{205F}a b", "ab"), false);
assert_eq!(matches_codepattern("a \u{3000}b", "ab"), false);
}
}