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;
|
2013-05-07 12:34:52 -07:00
|
|
|
use parse::{new_parse_sess};
|
2013-06-04 12:34:25 -07:00
|
|
|
use parse::{ParseSess,string_to_filemap,filemap_to_tts};
|
|
|
|
|
use parse::{new_parser_from_source_str};
|
|
|
|
|
use parse::parser::Parser;
|
|
|
|
|
use parse::token;
|
2014-04-05 16:24:28 +03:00
|
|
|
use ptr::P;
|
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> {
|
2014-02-07 00:38:33 +02:00
|
|
|
let ps = new_parse_sess();
|
2014-05-07 16:33:43 -07:00
|
|
|
filemap_to_tts(&ps,
|
2014-05-25 03:17:19 -07:00
|
|
|
string_to_filemap(&ps, source_str, "bogofile".to_string()))
|
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
|
|
|
}
|
|
|
|
|
|
2014-05-22 16:57:53 -07:00
|
|
|
fn with_error_checking_parse<T>(s: String, f: |&mut Parser| -> T) -> T {
|
2014-03-17 09:55:41 +02:00
|
|
|
let ps = new_parse_sess();
|
|
|
|
|
let mut p = string_to_parser(&ps, s);
|
2013-07-25 01:03:53 -07:00
|
|
|
let x = f(&mut p);
|
|
|
|
|
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 {
|
2013-11-21 19:20:48 -08:00
|
|
|
with_error_checking_parse(source_str, |p| {
|
2013-07-25 01:03:53 -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> {
|
2013-11-21 19:20:48 -08:00
|
|
|
with_error_checking_parse(source_str, |p| {
|
2013-07-25 01:03:53 -07: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>> {
|
2013-11-21 19:20:48 -08:00
|
|
|
with_error_checking_parse(source_str, |p| {
|
2014-02-28 13:09:09 -08:00
|
|
|
p.parse_item(Vec::new())
|
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
|
2014-09-13 19:06:01 +03:00
|
|
|
pub fn string_to_stmt(source_str : String) -> P<ast::Stmt> {
|
2013-11-21 19:20:48 -08:00
|
|
|
with_error_checking_parse(source_str, |p| {
|
2014-02-28 13:09:09 -08:00
|
|
|
p.parse_stmt(Vec::new())
|
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> {
|
2014-03-09 16:54:34 +02:00
|
|
|
string_to_parser(&new_parse_sess(), source_str).parse_pat()
|
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.
|
2014-09-02 01:35:58 -04:00
|
|
|
/// this function is Unicode-ignorant; fortunately, the careful design of
|
2014-06-09 13:12:30 -07:00
|
|
|
/// UTF-8 mitigates this ignorance. In particular, this function only collapses
|
2014-09-02 01:35:58 -04:00
|
|
|
/// sequences of \n, \r, ' ', and \t, but it should otherwise tolerate Unicode
|
2014-06-09 13:12:30 -07:00
|
|
|
/// chars. Unsurprisingly, it doesn't do NKF-normalization(?).
|
2013-06-06 14:17:00 -07:00
|
|
|
pub fn matches_codepattern(a : &str, b : &str) -> bool {
|
|
|
|
|
let mut idx_a = 0;
|
|
|
|
|
let mut idx_b = 0;
|
|
|
|
|
loop {
|
2014-01-30 14:28:44 -08:00
|
|
|
if idx_a == a.len() && idx_b == b.len() {
|
2013-06-06 14:17:00 -07:00
|
|
|
return true;
|
|
|
|
|
}
|
2014-01-30 14:28:44 -08:00
|
|
|
else if idx_a == a.len() {return false;}
|
|
|
|
|
else if idx_b == b.len() {
|
2013-06-06 14:17:00 -07:00
|
|
|
// maybe the stuff left in a is all ws?
|
2014-01-30 14:28:44 -08:00
|
|
|
if is_whitespace(a.char_at(idx_a)) {
|
|
|
|
|
return scan_for_non_ws_or_end(a,idx_a) == a.len();
|
2013-06-06 14:17:00 -07:00
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// ws in both given and pattern:
|
2014-01-30 14:28:44 -08:00
|
|
|
else if is_whitespace(a.char_at(idx_a))
|
|
|
|
|
&& is_whitespace(b.char_at(idx_b)) {
|
2013-06-06 14:17:00 -07:00
|
|
|
idx_a = scan_for_non_ws_or_end(a,idx_a);
|
|
|
|
|
idx_b = scan_for_non_ws_or_end(b,idx_b);
|
|
|
|
|
}
|
|
|
|
|
// ws in given only:
|
2014-01-30 14:28:44 -08:00
|
|
|
else if is_whitespace(a.char_at(idx_a)) {
|
2013-06-06 14:17:00 -07:00
|
|
|
idx_a = scan_for_non_ws_or_end(a,idx_a);
|
|
|
|
|
}
|
|
|
|
|
// *don't* silently eat ws in expected only.
|
2014-01-30 14:28:44 -08:00
|
|
|
else if a.char_at(idx_a) == b.char_at(idx_b) {
|
2013-06-06 14:17:00 -07:00
|
|
|
idx_a += 1;
|
|
|
|
|
idx_b += 1;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// Given a string and an index, return the first uint >= idx
|
|
|
|
|
/// that is a non-ws-char or is outside of the legal range of
|
|
|
|
|
/// the string.
|
2013-06-06 14:17:00 -07:00
|
|
|
fn scan_for_non_ws_or_end(a : &str, idx: uint) -> uint {
|
|
|
|
|
let mut i = idx;
|
|
|
|
|
let len = a.len();
|
2014-01-19 19:21:14 +11:00
|
|
|
while (i < len) && (is_whitespace(a.char_at(i))) {
|
2013-06-06 14:17:00 -07:00
|
|
|
i += 1;
|
|
|
|
|
}
|
|
|
|
|
i
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// Copied from lexer.
|
2013-06-06 14:17:00 -07:00
|
|
|
pub fn is_whitespace(c: char) -> bool {
|
|
|
|
|
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod test {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|