From cabee6391d599be867b80f81de219d9f982585f5 Mon Sep 17 00:00:00 2001 From: Paul Stansifer Date: Fri, 6 Jul 2012 14:29:50 -0700 Subject: [PATCH] Enable item macros to define macros. --- src/libsyntax/ext/base.rs | 7 ++++++- src/libsyntax/ext/expand.rs | 12 +++++++++--- src/libsyntax/ext/pipes.rs | 4 ++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 0e6cce27b2b..afc47d2ecfc 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -20,7 +20,12 @@ type syntax_expander_tt_ = fn@(ext_ctxt, span, ast::token_tree) -> @ast::expr; type syntax_expander_tt_item = {expander: syntax_expander_tt_item_, span: option}; type syntax_expander_tt_item_ - = fn@(ext_ctxt, span, ast::ident, ast::token_tree) -> @ast::item; + = fn@(ext_ctxt, span, ast::ident, ast::token_tree) -> mac_result; + +enum mac_result { + mr_item(@ast::item), + mr_def(macro_def) +} enum syntax_extension { normal(syntax_expander), diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 9cb610f486c..c6110fbe1a7 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -170,10 +170,16 @@ fn expand_item_mac(exts: hashmap, cx.bt_push(expanded_from({call_site: it.span, callie: {name: *extname, span: expand.span}})); - let it = fld.fold_item( - expand.expander(cx, it.span, it.ident, tt)); + let maybe_it = + alt expand.expander(cx, it.span, it.ident, tt) { + mr_item(it) { fld.fold_item(it) } + mr_def(mdef) { + exts.insert(*mdef.ident, mdef.ext); + none + } + }; cx.bt_pop(); - ret it + ret maybe_it } _ { cx.span_fatal(it.span, #fmt("%s is not a legal here", *extname)) } diff --git a/src/libsyntax/ext/pipes.rs b/src/libsyntax/ext/pipes.rs index 9a7bcc0e171..61c154ab15e 100644 --- a/src/libsyntax/ext/pipes.rs +++ b/src/libsyntax/ext/pipes.rs @@ -11,7 +11,7 @@ import pipes::parse_proto::proto_parser; import pipes::pipec::methods; fn expand_proto(cx: ext_ctxt, _sp: span, id: ast::ident, tt: ast::token_tree) - -> @ast::item + -> base::mac_result { let sess = cx.parse_sess(); let cfg = cx.cfg(); @@ -25,5 +25,5 @@ fn expand_proto(cx: ext_ctxt, _sp: span, id: ast::ident, tt: ast::token_tree) let proto = rust_parser.parse_proto(id); - proto.compile(cx) + base::mr_item(proto.compile(cx)) }