Skip to content

Commit 5ea04c6

Browse files
committed
Add support for recognizing macro body, completely untested.
1 parent e76fdeb commit 5ea04c6

File tree

9 files changed

+43
-16
lines changed

9 files changed

+43
-16
lines changed

src/comp/syntax/ast.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,13 @@ enum blk_sort {
272272

273273
type mac = spanned<mac_>;
274274

275-
type mac_body_ = {str: str, span: span};
275+
type mac_arg = @expr;
276+
277+
type mac_body_ = {span: span};
276278
type mac_body = option::t<mac_body_>;
277279

278280
enum mac_ {
279-
mac_invoc(@path, @expr, mac_body),
281+
mac_invoc(@path, mac_arg, mac_body),
280282
mac_embed_type(@ty),
281283
mac_embed_block(blk),
282284
mac_ellipsis,

src/comp/syntax/ext/base.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import std::map::new_str_hash;
66
import codemap;
77

88
type syntax_expander =
9-
fn@(ext_ctxt, span, @ast::expr, ast::mac_body) -> @ast::expr;
9+
fn@(ext_ctxt, span, ast::mac_arg, ast::mac_body) -> @ast::expr;
1010
type macro_def = {ident: str, ext: syntax_extension};
1111
type macro_definer =
12-
fn@(ext_ctxt, span, @ast::expr, ast::mac_body) -> macro_def;
12+
fn@(ext_ctxt, span, ast::mac_arg, ast::mac_body) -> macro_def;
1313

1414
enum syntax_extension {
1515
normal(syntax_expander),
@@ -118,7 +118,14 @@ 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-
121+
fn get_mac_body(cx: ext_ctxt, sp: span, args: ast::mac_body)
122+
-> ast::mac_body_
123+
{
124+
alt (args) {
125+
some(body) {body}
126+
none {cx.span_fatal(sp, "missing macro body")}
127+
}
128+
}
122129

123130
//
124131
// Local Variables:

src/comp/syntax/ext/concat_idents.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import option;
22
import base::*;
33
import syntax::ast;
44

5-
fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, arg: @ast::expr,
5+
fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg,
66
_body: ast::mac_body) -> @ast::expr {
77
let args: [@ast::expr] =
88
alt arg.node {

src/comp/syntax/ext/env.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import std::generic_os;
99
import base::*;
1010
export expand_syntax_ext;
1111

12-
fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, arg: @ast::expr,
12+
fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg,
1313
_body: ast::mac_body) -> @ast::expr {
1414
let args: [@ast::expr] =
1515
alt arg.node {

src/comp/syntax/ext/fmt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import codemap::span;
1313
import syntax::ext::build::*;
1414
export expand_syntax_ext;
1515

16-
fn expand_syntax_ext(cx: ext_ctxt, sp: span, arg: @ast::expr,
16+
fn expand_syntax_ext(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
1717
_body: ast::mac_body) -> @ast::expr {
1818
let args: [@ast::expr] =
1919
alt arg.node {

src/comp/syntax/ext/ident_to_str.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import core::{vec, option};
22
import base::*;
33
import syntax::ast;
44

5-
fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, arg: @ast::expr,
5+
fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg,
66
_body: ast::mac_body) -> @ast::expr {
77
let args: [@ast::expr] =
88
alt arg.node {

src/comp/syntax/ext/log_syntax.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import base::*;
22
import syntax::ast;
33
import std::io::writer_util;
44

5-
fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, arg: @ast::expr,
5+
fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg,
66
_body: ast::mac_body) -> @ast::expr {
7-
87
cx.print_backtrace();
98
std::io::stdout().write_line(print::pprust::expr_to_str(arg));
109

src/comp/syntax/ext/simplext.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import std::map::{hashmap, new_str_hash};
66
import option::{some, none};
77
import driver::session::session;
88

9-
import base::{ext_ctxt, normal};
9+
import base::*;
1010

1111
import fold::*;
1212
import ast_util::respan;
@@ -669,7 +669,7 @@ fn p_t_s_r_actual_vector(cx: ext_ctxt, elts: [@expr], _repeat_after: bool,
669669
}
670670
}
671671

672-
fn add_new_extension(cx: ext_ctxt, sp: span, arg: @expr,
672+
fn add_new_extension(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
673673
_body: ast::mac_body) -> base::macro_def {
674674
let args: [@ast::expr] =
675675
alt arg.node {
@@ -715,8 +715,10 @@ fn add_new_extension(cx: ext_ctxt, sp: span, arg: @expr,
715715
}
716716
}
717717
clauses +=
718-
[@{params: pattern_to_selectors(cx, invoc_arg),
718+
[@{params: pattern_to_selectors
719+
(cx, invoc_arg),
719720
body: elts[1u]}];
721+
720722
// FIXME: check duplicates (or just simplify
721723
// the macro arg situation)
722724
}
@@ -753,7 +755,7 @@ fn add_new_extension(cx: ext_ctxt, sp: span, arg: @expr,
753755
},
754756
ext: normal(ext)};
755757

756-
fn generic_extension(cx: ext_ctxt, sp: span, arg: @expr,
758+
fn generic_extension(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
757759
_body: ast::mac_body, clauses: [@clause]) -> @expr {
758760
for c: @clause in clauses {
759761
alt use_selectors_to_bind(c.params, arg) {

src/comp/syntax/parse/parser.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,24 @@ fn parse_syntax_ext_naked(p: parser, lo: uint) -> @ast::expr {
993993
};
994994
let hi = es.span.hi;
995995
let e = mk_expr(p, es.span.lo, hi, ast::expr_vec(es.node, ast::imm));
996-
ret mk_mac_expr(p, lo, hi, ast::mac_invoc(pth, e, none));
996+
let b = none;
997+
if p.token == token::LBRACE {
998+
p.bump();
999+
let lo = p.span.lo;
1000+
let depth = 1u;
1001+
while (depth > 0u) {
1002+
alt (p.token) {
1003+
token::LBRACE {depth += 1u;}
1004+
token::RBRACE {depth -= 1u;}
1005+
token::EOF {p.fatal("unexpected EOF in macro body");}
1006+
_ {}
1007+
}
1008+
p.bump();
1009+
}
1010+
let hi = p.last_span.hi;
1011+
b = some({span: mk_sp(lo,hi)});
1012+
}
1013+
ret mk_mac_expr(p, lo, p.span.hi, ast::mac_invoc(pth, e, b));
9971014
}
9981015

9991016
fn parse_dot_or_call_expr(p: parser) -> pexpr {

0 commit comments

Comments
 (0)