From da74a7f9ca774cd8addcb00a361bb230facc3b31 Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Wed, 1 Feb 2012 00:20:31 -0700 Subject: [PATCH] Make macro arg optional in syntax, again untested. --- src/comp/syntax/ast.rs | 2 +- src/comp/syntax/ext/base.rs | 7 +++++++ src/comp/syntax/ext/concat_idents.rs | 1 + src/comp/syntax/ext/env.rs | 1 + src/comp/syntax/ext/fmt.rs | 1 + src/comp/syntax/ext/ident_to_str.rs | 1 + src/comp/syntax/ext/log_syntax.rs | 1 + src/comp/syntax/ext/simplext.rs | 4 +++- src/comp/syntax/fold.rs | 5 ++++- src/comp/syntax/parse/parser.rs | 22 ++++++++++++++-------- src/comp/syntax/print/pprust.rs | 7 +++++-- src/comp/syntax/visit.rs | 3 ++- 12 files changed, 41 insertions(+), 14 deletions(-) diff --git a/src/comp/syntax/ast.rs b/src/comp/syntax/ast.rs index 235485fe3f5..576e0ab865a 100644 --- a/src/comp/syntax/ast.rs +++ b/src/comp/syntax/ast.rs @@ -272,7 +272,7 @@ enum blk_sort { type mac = spanned; -type mac_arg = @expr; +type mac_arg = option::t<@expr>; type mac_body_ = {span: span}; type mac_body = option::t; diff --git a/src/comp/syntax/ext/base.rs b/src/comp/syntax/ext/base.rs index 824931a759b..415c6cb5d9b 100644 --- a/src/comp/syntax/ext/base.rs +++ b/src/comp/syntax/ext/base.rs @@ -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_ { diff --git a/src/comp/syntax/ext/concat_idents.rs b/src/comp/syntax/ext/concat_idents.rs index b451e449b30..538c40a9d66 100644 --- a/src/comp/syntax/ext/concat_idents.rs +++ b/src/comp/syntax/ext/concat_idents.rs @@ -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 } diff --git a/src/comp/syntax/ext/env.rs b/src/comp/syntax/ext/env.rs index 8cf299c5e67..b9b42028c0f 100644 --- a/src/comp/syntax/ext/env.rs +++ b/src/comp/syntax/ext/env.rs @@ -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 } diff --git a/src/comp/syntax/ext/fmt.rs b/src/comp/syntax/ext/fmt.rs index b2425079ed8..b7834175d2f 100644 --- a/src/comp/syntax/ext/fmt.rs +++ b/src/comp/syntax/ext/fmt.rs @@ -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 } diff --git a/src/comp/syntax/ext/ident_to_str.rs b/src/comp/syntax/ext/ident_to_str.rs index a5a2dedcbf9..9245e5aa6d4 100644 --- a/src/comp/syntax/ext/ident_to_str.rs +++ b/src/comp/syntax/ext/ident_to_str.rs @@ -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 } diff --git a/src/comp/syntax/ext/log_syntax.rs b/src/comp/syntax/ext/log_syntax.rs index 4047a1b95d3..911cf9ff2eb 100644 --- a/src/comp/syntax/ext/log_syntax.rs +++ b/src/comp/syntax/ext/log_syntax.rs @@ -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)); diff --git a/src/comp/syntax/ext/simplext.rs b/src/comp/syntax/ext/simplext.rs index b8722122b69..d8496203e3d 100644 --- a/src/comp/syntax/ext/simplext.rs +++ b/src/comp/syntax/ext/simplext.rs @@ -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); } diff --git a/src/comp/syntax/fold.rs b/src/comp/syntax/fold.rs index 1831e30e810..e3a05961bea 100644 --- a/src/comp/syntax/fold.rs +++ b/src/comp/syntax/fold.rs @@ -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)) } diff --git a/src/comp/syntax/parse/parser.rs b/src/comp/syntax/parse/parser.rs index ae00c9fbce7..291f820df18 100644 --- a/src/comp/syntax/parse/parser.rs +++ b/src/comp/syntax/parse/parser.rs @@ -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(); diff --git a/src/comp/syntax/print/pprust.rs b/src/comp/syntax/print/pprust.rs index 74ebd271b6a..59ea8216c43 100644 --- a/src/comp/syntax/print/pprust.rs +++ b/src/comp/syntax/print/pprust.rs @@ -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) { diff --git a/src/comp/syntax/visit.rs b/src/comp/syntax/visit.rs index 0601e8285d4..50a4f2cfbfa 100644 --- a/src/comp/syntax/visit.rs +++ b/src/comp/syntax/visit.rs @@ -273,7 +273,8 @@ fn visit_exprs(exprs: [@expr], e: E, v: vt) { fn visit_mac(m: mac, e: E, v: vt) { 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 { }