Skip to content

Commit

Permalink
implement fold traversing macros
Browse files Browse the repository at this point in the history
  • Loading branch information
jbclements committed Jun 6, 2013
1 parent 2d59eba commit eff49fc
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions src/libsyntax/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,43 @@ fn fold_arg_(a: arg, fld: @ast_fold) -> arg {
id: fld.new_id(a.id),
}
}

//used in noop_fold_expr, and possibly elsewhere in the future
fn fold_mac_(m: &mac, fld: @ast_fold) -> mac {
spanned {
node: match m.node { mac_invoc_tt(*) => copy m.node },
span: fld.new_span(m.span),
node: match m.node {
mac_invoc_tt(p,ref tts) =>
mac_invoc_tt(fld.fold_path(p),
fold_tts(*tts,fld))
},
span: fld.new_span(m.span)
}
}

fn fold_tts(tts : &[token_tree], fld: @ast_fold) -> ~[token_tree] {
do tts.map |tt| {
match *tt {
tt_tok(span, ref tok) =>
tt_tok(span,maybe_fold_ident(tok,fld)),
tt_delim(ref tts) =>
tt_delim(fold_tts(*tts,fld)),
tt_seq(span, ref pattern, ref sep, is_optional) =>
tt_seq(span,
fold_tts(*pattern,fld),
sep.map(|tok|maybe_fold_ident(tok,fld)),
is_optional),
tt_nonterminal(sp,ref ident) =>
tt_nonterminal(sp,fld.fold_ident(*ident))
}
}
}

// apply ident folder if it's an ident, otherwise leave it alone
fn maybe_fold_ident(t : &token::Token, fld: @ast_fold) -> token::Token {
match *t {
token::IDENT(id,followed_by_colons) =>
token::IDENT(fld.fold_ident(id),followed_by_colons),
_ => copy *t
}
}

Expand Down Expand Up @@ -291,7 +323,10 @@ pub fn noop_fold_item_underscore(i: &item_, fld: @ast_fold) -> item_ {
}
item_mac(ref m) => {
// FIXME #2888: we might actually want to do something here.
item_mac(copy *m)
// ... okay, we're doing something. It would probably be nicer
// to add something to the ast_fold trait, but I'll defer
// that work.
item_mac(fold_mac_(m,fld))
}
}
}
Expand Down

5 comments on commit eff49fc

@bors
Copy link
Contributor

@bors bors commented on eff49fc Jun 6, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from catamorphism
at jbclements@eff49fc

@bors
Copy link
Contributor

@bors bors commented on eff49fc Jun 6, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging jbclements/rust/fold-traverses-macros = eff49fc into auto

@bors
Copy link
Contributor

@bors bors commented on eff49fc Jun 6, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jbclements/rust/fold-traverses-macros = eff49fc merged ok, testing candidate = 6a09b6f

@bors
Copy link
Contributor

@bors bors commented on eff49fc Jun 6, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on eff49fc Jun 6, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding incoming to auto = 6a09b6f

Please sign in to comment.