Make macro arg optional in syntax, again untested.

This commit is contained in:
Kevin Atkinson 2012-02-01 00:20:31 -07:00
parent 5ea04c65c1
commit da74a7f9ca
12 changed files with 41 additions and 14 deletions

View File

@ -272,7 +272,7 @@ enum blk_sort {
type mac = spanned<mac_>;
type mac_arg = @expr;
type mac_arg = option::t<@expr>;
type mac_body_ = {span: span};
type mac_body = option::t<mac_body_>;

View File

@ -118,6 +118,13 @@ fn make_new_lit(cx: ext_ctxt, sp: codemap::span, lit: ast::lit_) ->
ret @{id: cx.next_id(), node: ast::expr_lit(sp_lit), span: sp};
}
fn get_mac_arg(cx: ext_ctxt, sp: span, arg: ast::mac_arg) -> @ast::expr {
alt (arg) {
some(expr) {expr}
none {cx.span_fatal(sp, "missing macro args")}
}
}
fn get_mac_body(cx: ext_ctxt, sp: span, args: ast::mac_body)
-> ast::mac_body_
{

View File

@ -4,6 +4,7 @@ import syntax::ast;
fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg,
_body: ast::mac_body) -> @ast::expr {
let arg = get_mac_arg(cx,sp,arg);
let args: [@ast::expr] =
alt arg.node {
ast::expr_vec(elts, _) { elts }

View File

@ -11,6 +11,7 @@ export expand_syntax_ext;
fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg,
_body: ast::mac_body) -> @ast::expr {
let arg = get_mac_arg(cx,sp,arg);
let args: [@ast::expr] =
alt arg.node {
ast::expr_vec(elts, _) { elts }

View File

@ -15,6 +15,7 @@ export expand_syntax_ext;
fn expand_syntax_ext(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
_body: ast::mac_body) -> @ast::expr {
let arg = get_mac_arg(cx,sp,arg);
let args: [@ast::expr] =
alt arg.node {
ast::expr_vec(elts, _) { elts }

View File

@ -4,6 +4,7 @@ import syntax::ast;
fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg,
_body: ast::mac_body) -> @ast::expr {
let arg = get_mac_arg(cx,sp,arg);
let args: [@ast::expr] =
alt arg.node {
ast::expr_vec(elts, _) { elts }

View File

@ -4,6 +4,7 @@ import std::io::writer_util;
fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg,
_body: ast::mac_body) -> @ast::expr {
let arg = get_mac_arg(cx,sp,arg);
cx.print_backtrace();
std::io::stdout().write_line(print::pprust::expr_to_str(arg));

View File

@ -671,6 +671,7 @@ fn p_t_s_r_actual_vector(cx: ext_ctxt, elts: [@expr], _repeat_after: bool,
fn add_new_extension(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
_body: ast::mac_body) -> base::macro_def {
let arg = get_mac_arg(cx,sp,arg);
let args: [@ast::expr] =
alt arg.node {
ast::expr_vec(elts, _) { elts }
@ -716,7 +717,7 @@ fn add_new_extension(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
}
clauses +=
[@{params: pattern_to_selectors
(cx, invoc_arg),
(cx, get_mac_arg(cx,mac.span,invoc_arg)),
body: elts[1u]}];
// FIXME: check duplicates (or just simplify
@ -757,6 +758,7 @@ fn add_new_extension(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
fn generic_extension(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
_body: ast::mac_body, clauses: [@clause]) -> @expr {
let arg = get_mac_arg(cx,sp,arg);
for c: @clause in clauses {
alt use_selectors_to_bind(c.params, arg) {
some(bindings) { ret transcribe(cx, bindings, c.body); }

View File

@ -133,7 +133,10 @@ fn fold_mac_(m: mac, fld: ast_fold) -> mac {
ret {node:
alt m.node {
mac_invoc(pth, arg, body) {
mac_invoc(fld.fold_path(pth), fld.fold_expr(arg), body)
mac_invoc(fld.fold_path(pth),
// FIXME: bind should work, but causes a crash
option::map(arg) {|arg| fld.fold_expr(arg)},
body)
}
mac_embed_type(ty) { mac_embed_type(fld.fold_ty(ty)) }
mac_embed_block(blk) { mac_embed_block(fld.fold_block(blk)) }

View File

@ -985,14 +985,20 @@ fn parse_syntax_ext_naked(p: parser, lo: uint) -> @ast::expr {
let pth = parse_path(p);
//temporary for a backwards-compatible cycle:
let sep = seq_sep(token::COMMA);
let es =
if p.token == token::LPAREN {
parse_seq(token::LPAREN, token::RPAREN, sep, parse_expr, p)
} else {
parse_seq(token::LBRACKET, token::RBRACKET, sep, parse_expr, p)
};
let hi = es.span.hi;
let e = mk_expr(p, es.span.lo, hi, ast::expr_vec(es.node, ast::imm));
let e = none;
if (p.token == token::LPAREN || p.token == token::LBRACKET) {
let es =
if p.token == token::LPAREN {
parse_seq(token::LPAREN, token::RPAREN,
sep, parse_expr, p)
} else {
parse_seq(token::LBRACKET, token::RBRACKET,
sep, parse_expr, p)
};
let hi = es.span.hi;
e = some(mk_expr(p, es.span.lo, hi,
ast::expr_vec(es.node, ast::imm)));
}
let b = none;
if p.token == token::LBRACE {
p.bump();

View File

@ -726,8 +726,11 @@ fn print_mac(s: ps, m: ast::mac) {
ast::mac_invoc(path, arg, body) {
word(s.s, "#");
print_path(s, path, false);
alt arg.node { ast::expr_vec(_, _) { } _ { word(s.s, " "); } }
print_expr(s, arg);
alt arg {
some(@{node: ast::expr_vec(_, _), _}) { }
_ { word(s.s, " "); }
}
option::may(arg, bind print_expr(s, _));
// FIXME: extension 'body'
}
ast::mac_embed_type(ty) {

View File

@ -273,7 +273,8 @@ fn visit_exprs<E>(exprs: [@expr], e: E, v: vt<E>) {
fn visit_mac<E>(m: mac, e: E, v: vt<E>) {
alt m.node {
ast::mac_invoc(pth, arg, body) { visit_expr(arg, e, v); }
ast::mac_invoc(pth, arg, body) {
option::map(arg) {|arg| visit_expr(arg, e, v)}; }
ast::mac_embed_type(ty) { v.visit_ty(ty, e, v); }
ast::mac_embed_block(blk) { v.visit_block(blk, e, v); }
ast::mac_ellipsis { }