2013-05-16 17:41:47 -07:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
|
|
use ast;
|
2015-10-23 20:35:44 -07:00
|
|
|
use parse::{ParseSess,PResult,filemap_to_tts};
|
2015-11-12 02:43:43 +00:00
|
|
|
use parse::{lexer, new_parser_from_source_str};
|
2013-06-04 12:34:25 -07:00
|
|
|
use parse::parser::Parser;
|
|
|
|
|
use parse::token;
|
2014-04-05 16:24:28 +03:00
|
|
|
use ptr::P;
|
2015-11-10 00:29:56 +00:00
|
|
|
use std::iter::Peekable;
|
2014-06-11 19:33:52 -07:00
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// Map a string to tts, using a made-up filename:
|
2014-05-22 16:57:53 -07:00
|
|
|
pub fn string_to_tts(source_str: String) -> Vec<ast::TokenTree> {
|
2015-05-13 23:00:17 +03:00
|
|
|
let ps = ParseSess::new();
|
2016-06-09 16:36:20 -04:00
|
|
|
filemap_to_tts(&ps, ps.codemap().new_filemap("bogofile".to_string(), None, source_str))
|
2013-05-16 17:42:08 -07:00
|
|
|
}
|
|
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// Map string to parser (via tts)
|
2014-05-22 16:57:53 -07:00
|
|
|
pub fn string_to_parser<'a>(ps: &'a ParseSess, source_str: String) -> Parser<'a> {
|
2014-05-07 16:33:43 -07:00
|
|
|
new_parser_from_source_str(ps,
|
|
|
|
|
Vec::new(),
|
2014-05-25 03:17:19 -07:00
|
|
|
"bogofile".to_string(),
|
2014-05-07 16:33:43 -07:00
|
|
|
source_str)
|
2013-05-16 17:41:47 -07:00
|
|
|
}
|
|
|
|
|
|
2015-12-21 10:00:43 +13:00
|
|
|
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>,
|
2014-12-08 13:28:32 -05:00
|
|
|
{
|
2014-03-17 09:55:41 +02:00
|
|
|
let mut p = string_to_parser(&ps, s);
|
2015-10-23 20:35:44 -07:00
|
|
|
let x = panictry!(f(&mut p));
|
2013-07-25 01:03:53 -07:00
|
|
|
p.abort_if_errors();
|
|
|
|
|
x
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// Parse a string, return a crate.
|
2014-05-22 16:57:53 -07:00
|
|
|
pub fn string_to_crate (source_str : String) -> ast::Crate {
|
2015-12-21 10:00:43 +13:00
|
|
|
let ps = ParseSess::new();
|
|
|
|
|
with_error_checking_parse(source_str, &ps, |p| {
|
2015-10-23 20:35:44 -07:00
|
|
|
p.parse_crate_mod()
|
2013-11-21 19:20:48 -08:00
|
|
|
})
|
2013-05-16 17:41:47 -07:00
|
|
|
}
|
|
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// Parse a string, return an expr
|
2014-09-13 19:06:01 +03:00
|
|
|
pub fn string_to_expr (source_str : String) -> P<ast::Expr> {
|
2015-12-21 10:00:43 +13:00
|
|
|
let ps = ParseSess::new();
|
|
|
|
|
with_error_checking_parse(source_str, &ps, |p| {
|
2015-11-10 16:08:26 -08:00
|
|
|
p.parse_expr()
|
2013-11-21 19:20:48 -08:00
|
|
|
})
|
2013-05-16 17:41:47 -07:00
|
|
|
}
|
|
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// Parse a string, return an item
|
2014-09-13 19:06:01 +03:00
|
|
|
pub fn string_to_item (source_str : String) -> Option<P<ast::Item>> {
|
2015-12-21 10:00:43 +13:00
|
|
|
let ps = ParseSess::new();
|
|
|
|
|
with_error_checking_parse(source_str, &ps, |p| {
|
2015-11-10 16:08:26 -08:00
|
|
|
p.parse_item()
|
2013-11-21 19:20:48 -08:00
|
|
|
})
|
2013-05-16 17:41:47 -07:00
|
|
|
}
|
|
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// Parse a string, return a stmt
|
2016-02-11 23:33:09 +03:00
|
|
|
pub fn string_to_stmt(source_str : String) -> Option<ast::Stmt> {
|
2015-12-21 10:00:43 +13:00
|
|
|
let ps = ParseSess::new();
|
|
|
|
|
with_error_checking_parse(source_str, &ps, |p| {
|
2015-11-10 16:08:26 -08:00
|
|
|
p.parse_stmt()
|
2013-11-21 19:20:48 -08:00
|
|
|
})
|
2013-05-16 17:41:47 -07:00
|
|
|
}
|
|
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// Parse a string, return a pat. Uses "irrefutable"... which doesn't
|
|
|
|
|
/// (currently) affect parsing.
|
2014-09-13 19:06:01 +03:00
|
|
|
pub fn string_to_pat(source_str: String) -> P<ast::Pat> {
|
2015-12-21 10:00:43 +13:00
|
|
|
let ps = ParseSess::new();
|
|
|
|
|
with_error_checking_parse(source_str, &ps, |p| {
|
2015-11-10 16:08:26 -08:00
|
|
|
p.parse_pat()
|
2015-10-23 20:35:44 -07:00
|
|
|
})
|
2013-06-04 12:34:25 -07:00
|
|
|
}
|
|
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// Convert a vector of strings to a vector of ast::Ident's
|
2014-02-28 13:09:09 -08:00
|
|
|
pub fn strs_to_idents(ids: Vec<&str> ) -> Vec<ast::Ident> {
|
2014-03-28 20:42:34 +01:00
|
|
|
ids.iter().map(|u| token::str_to_ident(*u)).collect()
|
2013-06-04 12:34:25 -07:00
|
|
|
}
|
2013-06-06 14:17:00 -07:00
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// Does the given string match the pattern? whitespace in the first string
|
|
|
|
|
/// may be deleted or replaced with other whitespace to match the pattern.
|
2015-11-10 00:29:56 +00:00
|
|
|
/// This function is relatively Unicode-ignorant; fortunately, the careful design
|
|
|
|
|
/// of UTF-8 mitigates this ignorance. It doesn't do NKF-normalization(?).
|
2013-06-06 14:17:00 -07:00
|
|
|
pub fn matches_codepattern(a : &str, b : &str) -> bool {
|
2015-11-10 00:29:56 +00:00
|
|
|
let mut a_iter = a.chars().peekable();
|
|
|
|
|
let mut b_iter = b.chars().peekable();
|
|
|
|
|
|
2013-06-06 14:17:00 -07:00
|
|
|
loop {
|
2015-11-10 00:29:56 +00:00
|
|
|
let (a, b) = match (a_iter.peek(), b_iter.peek()) {
|
|
|
|
|
(None, None) => return true,
|
|
|
|
|
(None, _) => return false,
|
2015-11-12 02:43:43 +00:00
|
|
|
(Some(&a), None) => {
|
|
|
|
|
if is_pattern_whitespace(a) {
|
2015-11-10 00:29:56 +00:00
|
|
|
break // trailing whitespace check is out of loop for borrowck
|
|
|
|
|
} else {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2013-06-06 14:17:00 -07:00
|
|
|
}
|
2015-11-10 00:29:56 +00:00
|
|
|
(Some(&a), Some(&b)) => (a, b)
|
|
|
|
|
};
|
|
|
|
|
|
2015-11-12 02:43:43 +00:00
|
|
|
if is_pattern_whitespace(a) && is_pattern_whitespace(b) {
|
2015-11-10 00:29:56 +00:00
|
|
|
// skip whitespace for a and b
|
|
|
|
|
scan_for_non_ws_or_end(&mut a_iter);
|
|
|
|
|
scan_for_non_ws_or_end(&mut b_iter);
|
2015-11-12 02:43:43 +00:00
|
|
|
} else if is_pattern_whitespace(a) {
|
2015-11-10 00:29:56 +00:00
|
|
|
// skip whitespace for a
|
|
|
|
|
scan_for_non_ws_or_end(&mut a_iter);
|
|
|
|
|
} else if a == b {
|
|
|
|
|
a_iter.next();
|
|
|
|
|
b_iter.next();
|
|
|
|
|
} else {
|
|
|
|
|
return false
|
2013-06-06 14:17:00 -07:00
|
|
|
}
|
|
|
|
|
}
|
2015-11-10 00:29:56 +00:00
|
|
|
|
|
|
|
|
// check if a has *only* trailing whitespace
|
2015-11-12 02:43:43 +00:00
|
|
|
a_iter.all(is_pattern_whitespace)
|
2013-06-06 14:17:00 -07:00
|
|
|
}
|
|
|
|
|
|
2015-11-10 00:29:56 +00:00
|
|
|
/// Advances the given peekable `Iterator` until it reaches a non-whitespace character
|
|
|
|
|
fn scan_for_non_ws_or_end<I: Iterator<Item= char>>(iter: &mut Peekable<I>) {
|
2015-11-12 02:43:43 +00:00
|
|
|
while lexer::is_pattern_whitespace(iter.peek().cloned()) {
|
2015-11-10 00:29:56 +00:00
|
|
|
iter.next();
|
2013-06-06 14:17:00 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-12 02:43:43 +00:00
|
|
|
pub fn is_pattern_whitespace(c: char) -> bool {
|
|
|
|
|
lexer::is_pattern_whitespace(Some(c))
|
2013-06-06 14:17:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2015-04-24 17:30:41 +02:00
|
|
|
mod tests {
|
2013-06-06 14:17:00 -07:00
|
|
|
use super::*;
|
|
|
|
|
|
2015-11-10 00:29:56 +00:00
|
|
|
#[test]
|
|
|
|
|
fn eqmodws() {
|
2013-06-06 14:17:00 -07:00
|
|
|
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);
|
2015-11-10 00:29:56 +00:00
|
|
|
assert_eq!(matches_codepattern(" a b","ab"),true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2015-11-12 02:43:43 +00:00
|
|
|
fn pattern_whitespace() {
|
2015-11-10 00:29:56 +00:00
|
|
|
assert_eq!(matches_codepattern("","\x0C"), false);
|
|
|
|
|
assert_eq!(matches_codepattern("a b ","a \u{0085}\n\t\r b"),true);
|
|
|
|
|
assert_eq!(matches_codepattern("a b","a \u{0085}\n\t\r b "),false);
|
2015-11-12 02:43:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn non_pattern_whitespace() {
|
|
|
|
|
// These have the property 'White_Space' but not 'Pattern_White_Space'
|
|
|
|
|
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);
|
2013-06-06 14:17:00 -07:00
|
|
|
}
|
|
|
|
|
}
|