Skip to content

Commit da74a7f

Browse files
committed
Make macro arg optional in syntax, again untested.
1 parent 5ea04c6 commit da74a7f

File tree

12 files changed

+41
-14
lines changed

12 files changed

+41
-14
lines changed

src/comp/syntax/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ enum blk_sort {
272272

273273
type mac = spanned<mac_>;
274274

275-
type mac_arg = @expr;
275+
type mac_arg = option::t<@expr>;
276276

277277
type mac_body_ = {span: span};
278278
type mac_body = option::t<mac_body_>;

src/comp/syntax/ext/base.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ fn make_new_lit(cx: ext_ctxt, sp: codemap::span, lit: ast::lit_) ->
118118
ret @{id: cx.next_id(), node: ast::expr_lit(sp_lit), span: sp};
119119
}
120120

121+
fn get_mac_arg(cx: ext_ctxt, sp: span, arg: ast::mac_arg) -> @ast::expr {
122+
alt (arg) {
123+
some(expr) {expr}
124+
none {cx.span_fatal(sp, "missing macro args")}
125+
}
126+
}
127+
121128
fn get_mac_body(cx: ext_ctxt, sp: span, args: ast::mac_body)
122129
-> ast::mac_body_
123130
{

src/comp/syntax/ext/concat_idents.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import syntax::ast;
44

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

src/comp/syntax/ext/env.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export expand_syntax_ext;
1111

1212
fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg,
1313
_body: ast::mac_body) -> @ast::expr {
14+
let arg = get_mac_arg(cx,sp,arg);
1415
let args: [@ast::expr] =
1516
alt arg.node {
1617
ast::expr_vec(elts, _) { elts }

src/comp/syntax/ext/fmt.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export expand_syntax_ext;
1515

1616
fn expand_syntax_ext(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
1717
_body: ast::mac_body) -> @ast::expr {
18+
let arg = get_mac_arg(cx,sp,arg);
1819
let args: [@ast::expr] =
1920
alt arg.node {
2021
ast::expr_vec(elts, _) { elts }

src/comp/syntax/ext/ident_to_str.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import syntax::ast;
44

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

src/comp/syntax/ext/log_syntax.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import std::io::writer_util;
44

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

src/comp/syntax/ext/simplext.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,7 @@ fn p_t_s_r_actual_vector(cx: ext_ctxt, elts: [@expr], _repeat_after: bool,
671671

672672
fn add_new_extension(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
673673
_body: ast::mac_body) -> base::macro_def {
674+
let arg = get_mac_arg(cx,sp,arg);
674675
let args: [@ast::expr] =
675676
alt arg.node {
676677
ast::expr_vec(elts, _) { elts }
@@ -716,7 +717,7 @@ fn add_new_extension(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
716717
}
717718
clauses +=
718719
[@{params: pattern_to_selectors
719-
(cx, invoc_arg),
720+
(cx, get_mac_arg(cx,mac.span,invoc_arg)),
720721
body: elts[1u]}];
721722

722723
// FIXME: check duplicates (or just simplify
@@ -757,6 +758,7 @@ fn add_new_extension(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
757758

758759
fn generic_extension(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
759760
_body: ast::mac_body, clauses: [@clause]) -> @expr {
761+
let arg = get_mac_arg(cx,sp,arg);
760762
for c: @clause in clauses {
761763
alt use_selectors_to_bind(c.params, arg) {
762764
some(bindings) { ret transcribe(cx, bindings, c.body); }

src/comp/syntax/fold.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ fn fold_mac_(m: mac, fld: ast_fold) -> mac {
133133
ret {node:
134134
alt m.node {
135135
mac_invoc(pth, arg, body) {
136-
mac_invoc(fld.fold_path(pth), fld.fold_expr(arg), body)
136+
mac_invoc(fld.fold_path(pth),
137+
// FIXME: bind should work, but causes a crash
138+
option::map(arg) {|arg| fld.fold_expr(arg)},
139+
body)
137140
}
138141
mac_embed_type(ty) { mac_embed_type(fld.fold_ty(ty)) }
139142
mac_embed_block(blk) { mac_embed_block(fld.fold_block(blk)) }

src/comp/syntax/parse/parser.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -985,14 +985,20 @@ fn parse_syntax_ext_naked(p: parser, lo: uint) -> @ast::expr {
985985
let pth = parse_path(p);
986986
//temporary for a backwards-compatible cycle:
987987
let sep = seq_sep(token::COMMA);
988-
let es =
989-
if p.token == token::LPAREN {
990-
parse_seq(token::LPAREN, token::RPAREN, sep, parse_expr, p)
991-
} else {
992-
parse_seq(token::LBRACKET, token::RBRACKET, sep, parse_expr, p)
993-
};
994-
let hi = es.span.hi;
995-
let e = mk_expr(p, es.span.lo, hi, ast::expr_vec(es.node, ast::imm));
988+
let e = none;
989+
if (p.token == token::LPAREN || p.token == token::LBRACKET) {
990+
let es =
991+
if p.token == token::LPAREN {
992+
parse_seq(token::LPAREN, token::RPAREN,
993+
sep, parse_expr, p)
994+
} else {
995+
parse_seq(token::LBRACKET, token::RBRACKET,
996+
sep, parse_expr, p)
997+
};
998+
let hi = es.span.hi;
999+
e = some(mk_expr(p, es.span.lo, hi,
1000+
ast::expr_vec(es.node, ast::imm)));
1001+
}
9961002
let b = none;
9971003
if p.token == token::LBRACE {
9981004
p.bump();

src/comp/syntax/print/pprust.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -726,8 +726,11 @@ fn print_mac(s: ps, m: ast::mac) {
726726
ast::mac_invoc(path, arg, body) {
727727
word(s.s, "#");
728728
print_path(s, path, false);
729-
alt arg.node { ast::expr_vec(_, _) { } _ { word(s.s, " "); } }
730-
print_expr(s, arg);
729+
alt arg {
730+
some(@{node: ast::expr_vec(_, _), _}) { }
731+
_ { word(s.s, " "); }
732+
}
733+
option::may(arg, bind print_expr(s, _));
731734
// FIXME: extension 'body'
732735
}
733736
ast::mac_embed_type(ty) {

src/comp/syntax/visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ fn visit_exprs<E>(exprs: [@expr], e: E, v: vt<E>) {
273273

274274
fn visit_mac<E>(m: mac, e: E, v: vt<E>) {
275275
alt m.node {
276-
ast::mac_invoc(pth, arg, body) { visit_expr(arg, e, v); }
276+
ast::mac_invoc(pth, arg, body) {
277+
option::map(arg) {|arg| visit_expr(arg, e, v)}; }
277278
ast::mac_embed_type(ty) { v.visit_ty(ty, e, v); }
278279
ast::mac_embed_block(blk) { v.visit_block(blk, e, v); }
279280
ast::mac_ellipsis { }

0 commit comments

Comments
 (0)