Skip to content

Commit 6a09b6f

Browse files
committed
auto merge of #6981 : jbclements/rust/fold-traverses-macros, r=catamorphism
Fixes bug #2888 . Includes test cases r? @catamorphism
2 parents 1452797 + eff49fc commit 6a09b6f

File tree

2 files changed

+188
-3
lines changed

2 files changed

+188
-3
lines changed

src/libsyntax/fold.rs

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use core::prelude::*;
1313
use ast::*;
1414
use ast;
1515
use codemap::{span, spanned};
16+
use parse::token;
1617
use opt_vec::OptVec;
1718

1819
use core::vec;
@@ -115,11 +116,43 @@ fn fold_arg_(a: arg, fld: @ast_fold) -> arg {
115116
id: fld.new_id(a.id),
116117
}
117118
}
119+
118120
//used in noop_fold_expr, and possibly elsewhere in the future
119121
fn fold_mac_(m: &mac, fld: @ast_fold) -> mac {
120122
spanned {
121-
node: match m.node { mac_invoc_tt(*) => copy m.node },
122-
span: fld.new_span(m.span),
123+
node: match m.node {
124+
mac_invoc_tt(p,ref tts) =>
125+
mac_invoc_tt(fld.fold_path(p),
126+
fold_tts(*tts,fld))
127+
},
128+
span: fld.new_span(m.span)
129+
}
130+
}
131+
132+
fn fold_tts(tts : &[token_tree], fld: @ast_fold) -> ~[token_tree] {
133+
do tts.map |tt| {
134+
match *tt {
135+
tt_tok(span, ref tok) =>
136+
tt_tok(span,maybe_fold_ident(tok,fld)),
137+
tt_delim(ref tts) =>
138+
tt_delim(fold_tts(*tts,fld)),
139+
tt_seq(span, ref pattern, ref sep, is_optional) =>
140+
tt_seq(span,
141+
fold_tts(*pattern,fld),
142+
sep.map(|tok|maybe_fold_ident(tok,fld)),
143+
is_optional),
144+
tt_nonterminal(sp,ref ident) =>
145+
tt_nonterminal(sp,fld.fold_ident(*ident))
146+
}
147+
}
148+
}
149+
150+
// apply ident folder if it's an ident, otherwise leave it alone
151+
fn maybe_fold_ident(t : &token::Token, fld: @ast_fold) -> token::Token {
152+
match *t {
153+
token::IDENT(id,followed_by_colons) =>
154+
token::IDENT(fld.fold_ident(id),followed_by_colons),
155+
_ => copy *t
123156
}
124157
}
125158

@@ -290,7 +323,10 @@ pub fn noop_fold_item_underscore(i: &item_, fld: @ast_fold) -> item_ {
290323
}
291324
item_mac(ref m) => {
292325
// FIXME #2888: we might actually want to do something here.
293-
item_mac(copy *m)
326+
// ... okay, we're doing something. It would probably be nicer
327+
// to add something to the ast_fold trait, but I'll defer
328+
// that work.
329+
item_mac(fold_mac_(m,fld))
294330
}
295331
}
296332
}
@@ -904,3 +940,74 @@ impl AstFoldExtensions for @ast_fold {
904940
pub fn make_fold(afp: ast_fold_fns) -> @ast_fold {
905941
afp as @ast_fold
906942
}
943+
944+
#[cfg(test)]
945+
mod test {
946+
use ast;
947+
use util::parser_testing::{string_to_crate, matches_codepattern};
948+
use parse::token;
949+
use print::pprust;
950+
use super::*;
951+
952+
// taken from expand
953+
// given a function from idents to idents, produce
954+
// an ast_fold that applies that function:
955+
pub fn fun_to_ident_folder(f: @fn(ast::ident)->ast::ident) -> @ast_fold{
956+
let afp = default_ast_fold();
957+
let f_pre = @AstFoldFns{
958+
fold_ident : |id, _| f(id),
959+
.. *afp
960+
};
961+
make_fold(f_pre)
962+
}
963+
964+
// this version doesn't care about getting comments or docstrings in.
965+
fn fake_print_crate(s: @pprust::ps, crate: ast::crate) {
966+
pprust::print_mod(s, &crate.node.module, crate.node.attrs);
967+
}
968+
969+
// change every identifier to "zz"
970+
pub fn to_zz() -> @fn(ast::ident)->ast::ident {
971+
let zz_id = token::str_to_ident("zz");
972+
|id| {zz_id}
973+
}
974+
975+
// maybe add to expand.rs...
976+
macro_rules! assert_pred (
977+
($pred:expr, $predname:expr, $a:expr , $b:expr) => (
978+
{
979+
let pred_val = $pred;
980+
let a_val = $a;
981+
let b_val = $b;
982+
if !(pred_val(a_val,b_val)) {
983+
fail!("expected args satisfying %s, got %? and %?",
984+
$predname, a_val, b_val);
985+
}
986+
}
987+
)
988+
)
989+
990+
// make sure idents get transformed everywhere
991+
#[test] fn ident_transformation () {
992+
let zz_fold = fun_to_ident_folder(to_zz());
993+
let ast = string_to_crate(@~"#[a] mod b {fn c (d : e, f : g) {h!(i,j,k);l;m}}");
994+
assert_pred!(matches_codepattern,
995+
"matches_codepattern",
996+
pprust::to_str(zz_fold.fold_crate(ast),fake_print_crate,
997+
token::get_ident_interner()),
998+
~"#[a]mod zz{fn zz(zz:zz,zz:zz){zz!(zz,zz,zz);zz;zz}}");
999+
}
1000+
1001+
// even inside macro defs....
1002+
#[test] fn ident_transformation_in_defs () {
1003+
let zz_fold = fun_to_ident_folder(to_zz());
1004+
let ast = string_to_crate(@~"macro_rules! a {(b $c:expr $(d $e:token)f+
1005+
=> (g $(d $d $e)+))} ");
1006+
assert_pred!(matches_codepattern,
1007+
"matches_codepattern",
1008+
pprust::to_str(zz_fold.fold_crate(ast),fake_print_crate,
1009+
token::get_ident_interner()),
1010+
~"zz!zz((zz$zz:zz$(zz $zz:zz)zz+=>(zz$(zz$zz$zz)+)))");
1011+
}
1012+
1013+
}

src/libsyntax/util/parser_testing.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,81 @@ pub fn string_to_pat(source_str : @~str) -> @ast::pat {
6969
pub fn strs_to_idents(ids: ~[&str]) -> ~[ast::ident] {
7070
ids.map(|u| token::str_to_ident(*u))
7171
}
72+
73+
// does the given string match the pattern? whitespace in the first string
74+
// may be deleted or replaced with other whitespace to match the pattern.
75+
// this function is unicode-ignorant; fortunately, the careful design of
76+
// UTF-8 mitigates this ignorance. In particular, this function only collapses
77+
// sequences of \n, \r, ' ', and \t, but it should otherwise tolerate unicode
78+
// chars. Unsurprisingly, it doesn't do NKF-normalization(?).
79+
pub fn matches_codepattern(a : &str, b : &str) -> bool {
80+
let mut idx_a = 0;
81+
let mut idx_b = 0;
82+
loop {
83+
if (idx_a == a.len() && idx_b == b.len()) {
84+
return true;
85+
}
86+
else if (idx_a == a.len()) {return false;}
87+
else if (idx_b == b.len()) {
88+
// maybe the stuff left in a is all ws?
89+
if (is_whitespace(a.char_at(idx_a))) {
90+
return (scan_for_non_ws_or_end(a,idx_a) == a.len());
91+
} else {
92+
return false;
93+
}
94+
}
95+
// ws in both given and pattern:
96+
else if (is_whitespace(a.char_at(idx_a))
97+
&& is_whitespace(b.char_at(idx_b))) {
98+
idx_a = scan_for_non_ws_or_end(a,idx_a);
99+
idx_b = scan_for_non_ws_or_end(b,idx_b);
100+
}
101+
// ws in given only:
102+
else if (is_whitespace(a.char_at(idx_a))) {
103+
idx_a = scan_for_non_ws_or_end(a,idx_a);
104+
}
105+
// *don't* silently eat ws in expected only.
106+
else if (a.char_at(idx_a) == b.char_at(idx_b)) {
107+
idx_a += 1;
108+
idx_b += 1;
109+
}
110+
else {
111+
return false;
112+
}
113+
}
114+
}
115+
116+
// given a string and an index, return the first uint >= idx
117+
// that is a non-ws-char or is outside of the legal range of
118+
// the string.
119+
fn scan_for_non_ws_or_end(a : &str, idx: uint) -> uint {
120+
let mut i = idx;
121+
let len = a.len();
122+
while ((i < len) && (is_whitespace(a.char_at(i)))) {
123+
i += 1;
124+
}
125+
i
126+
}
127+
128+
// copied from lexer.
129+
pub fn is_whitespace(c: char) -> bool {
130+
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
131+
}
132+
133+
#[cfg(test)]
134+
mod test {
135+
use super::*;
136+
137+
#[test] fn eqmodws() {
138+
assert_eq!(matches_codepattern("",""),true);
139+
assert_eq!(matches_codepattern("","a"),false);
140+
assert_eq!(matches_codepattern("a",""),false);
141+
assert_eq!(matches_codepattern("a","a"),true);
142+
assert_eq!(matches_codepattern("a b","a \n\t\r b"),true);
143+
assert_eq!(matches_codepattern("a b ","a \n\t\r b"),true);
144+
assert_eq!(matches_codepattern("a b","a \n\t\r b "),false);
145+
assert_eq!(matches_codepattern("a b","a b"),true);
146+
assert_eq!(matches_codepattern("ab","a b"),false);
147+
assert_eq!(matches_codepattern("a b","ab"),true);
148+
}
149+
}

0 commit comments

Comments
 (0)