Skip to content

Commit eff49fc

Browse files
committed
implement fold traversing macros
1 parent 2d59eba commit eff49fc

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

src/libsyntax/fold.rs

+38-3
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,43 @@ fn fold_arg_(a: arg, fld: @ast_fold) -> arg {
116116
id: fld.new_id(a.id),
117117
}
118118
}
119+
119120
//used in noop_fold_expr, and possibly elsewhere in the future
120121
fn fold_mac_(m: &mac, fld: @ast_fold) -> mac {
121122
spanned {
122-
node: match m.node { mac_invoc_tt(*) => copy m.node },
123-
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
124156
}
125157
}
126158

@@ -291,7 +323,10 @@ pub fn noop_fold_item_underscore(i: &item_, fld: @ast_fold) -> item_ {
291323
}
292324
item_mac(ref m) => {
293325
// FIXME #2888: we might actually want to do something here.
294-
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))
295330
}
296331
}
297332
}

0 commit comments

Comments
 (0)