Allow macro_rules!
macros to expand to expressions or items.
This commit is contained in:
parent
c946c87b6f
commit
9845a4be5a
3 changed files with 12 additions and 5 deletions
|
@ -44,6 +44,7 @@ type syntax_expander_tt_item_
|
||||||
enum mac_result {
|
enum mac_result {
|
||||||
mr_expr(@ast::expr),
|
mr_expr(@ast::expr),
|
||||||
mr_item(@ast::item),
|
mr_item(@ast::item),
|
||||||
|
mr_expr_or_item(fn@()-> @ast::expr, fn@()-> Option<@ast::item>),
|
||||||
mr_def(macro_def)
|
mr_def(macro_def)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -81,6 +81,7 @@ fn expand_expr(exts: HashMap<~str, syntax_extension>, cx: ext_ctxt,
|
||||||
Some(expr_tt({expander: exp, span: exp_sp})) => {
|
Some(expr_tt({expander: exp, span: exp_sp})) => {
|
||||||
let expanded = match exp(cx, mac.span, tts) {
|
let expanded = match exp(cx, mac.span, tts) {
|
||||||
mr_expr(e) => e,
|
mr_expr(e) => e,
|
||||||
|
mr_expr_or_item(expr_maker,_) => expr_maker(),
|
||||||
_ => cx.span_fatal(
|
_ => cx.span_fatal(
|
||||||
pth.span, fmt!("non-expr macro in expr pos: %s",
|
pth.span, fmt!("non-expr macro in expr pos: %s",
|
||||||
*extname))
|
*extname))
|
||||||
|
@ -214,6 +215,8 @@ fn expand_item_mac(exts: HashMap<~str, syntax_extension>,
|
||||||
mr_expr(_) => cx.span_fatal(pth.span,
|
mr_expr(_) => cx.span_fatal(pth.span,
|
||||||
~"expr macro in item position: " +
|
~"expr macro in item position: " +
|
||||||
*extname),
|
*extname),
|
||||||
|
mr_expr_or_item(_, item_maker) =>
|
||||||
|
option::chain(item_maker(), |i| {fld.fold_item(i)}),
|
||||||
mr_def(mdef) => {
|
mr_def(mdef) => {
|
||||||
exts.insert(mdef.name, mdef.ext);
|
exts.insert(mdef.name, mdef.ext);
|
||||||
None
|
None
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use base::{ext_ctxt, mac_result, mr_expr, mr_def, expr_tt};
|
use base::{ext_ctxt, mac_result, mr_expr_or_item, mr_def, expr_tt};
|
||||||
use codemap::span;
|
use codemap::span;
|
||||||
use ast::{ident, matcher_, matcher, match_tok,
|
use ast::{ident, matcher_, matcher, match_tok,
|
||||||
match_nonterminal, match_seq, tt_delim};
|
match_nonterminal, match_seq, tt_delim};
|
||||||
|
@ -87,10 +87,13 @@ fn add_new_extension(cx: ext_ctxt, sp: span, name: ident,
|
||||||
// rhs has holes ( `$id` and `$(...)` that need filled)
|
// rhs has holes ( `$id` and `$(...)` that need filled)
|
||||||
let trncbr = new_tt_reader(s_d, itr, Some(named_matches),
|
let trncbr = new_tt_reader(s_d, itr, Some(named_matches),
|
||||||
~[rhs]);
|
~[rhs]);
|
||||||
let p = Parser(cx.parse_sess(), cx.cfg(),
|
let p = @Parser(cx.parse_sess(), cx.cfg(),
|
||||||
trncbr as reader);
|
trncbr as reader);
|
||||||
let e = p.parse_expr();
|
|
||||||
return mr_expr(e);
|
// Let the context choose how to interpret the result.
|
||||||
|
// Weird, but useful for X-macros.
|
||||||
|
return mr_expr_or_item(|| p.parse_expr(),
|
||||||
|
|| p.parse_item(~[/* no attrs*/]));
|
||||||
}
|
}
|
||||||
failure(sp, msg) => if sp.lo >= best_fail_spot.lo {
|
failure(sp, msg) => if sp.lo >= best_fail_spot.lo {
|
||||||
best_fail_spot = sp;
|
best_fail_spot = sp;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue