Skip to content

Commit 9a4c669

Browse files
committed
syntax: remove remaining #syntaxext machinery. Close #3516.
1 parent e24ae85 commit 9a4c669

18 files changed

+150
-272
lines changed

src/libsyntax/ast.rs

-8
Original file line numberDiff line numberDiff line change
@@ -831,14 +831,6 @@ enum matcher_ {
831831

832832
type mac = spanned<mac_>;
833833

834-
type mac_arg = Option<@expr>;
835-
836-
#[auto_serialize]
837-
#[auto_deserialize]
838-
type mac_body_ = {span: span};
839-
840-
type mac_body = Option<mac_body_>;
841-
842834
#[auto_serialize]
843835
#[auto_deserialize]
844836
enum mac_ {

src/libsyntax/ext/base.rs

+49-102
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@ use parse::parser;
1313
use diagnostic::span_handler;
1414
use codemap::{CodeMap, span, ExpnInfo, ExpandedFrom};
1515
use ast_util::dummy_sp;
16+
use parse::token;
1617

17-
// obsolete old-style #macro code:
18-
//
19-
// syntax_expander, normal, builtin
20-
//
2118
// new-style macro! tt code:
2219
//
2320
// syntax_expander_tt, syntax_expander_tt_item, mac_result,
@@ -27,12 +24,6 @@ use ast_util::dummy_sp;
2724
// is now probably a redundant AST node, can be merged with
2825
// ast::mac_invoc_tt.
2926

30-
// second argument is the span to blame for general argument problems
31-
type syntax_expander_ =
32-
fn@(ext_ctxt, span, ast::mac_arg, ast::mac_body) -> @ast::expr;
33-
// second argument is the origin of the macro, if user-defined
34-
type syntax_expander = {expander: syntax_expander_, span: Option<span>};
35-
3627
type macro_def = {name: ~str, ext: syntax_extension};
3728

3829
type item_decorator =
@@ -56,10 +47,7 @@ enum mac_result {
5647

5748
enum syntax_extension {
5849

59-
// normal() is obsolete, remove when #old_macros go away.
60-
normal(syntax_expander),
61-
62-
// #[auto_serialize] and such. will probably survive death of #old_macros
50+
// #[auto_serialize] and such
6351
item_decorator(item_decorator),
6452

6553
// Token-tree expanders
@@ -73,8 +61,6 @@ enum syntax_extension {
7361
// A temporary hard-coded map of methods for expanding syntax extension
7462
// AST nodes into full ASTs
7563
fn syntax_expander_table() -> HashMap<~str, syntax_extension> {
76-
fn builtin(f: syntax_expander_) -> syntax_extension
77-
{normal({expander: f, span: None})}
7864
fn builtin_normal_tt(f: syntax_expander_tt_) -> syntax_extension {
7965
normal_tt({expander: f, span: None})
8066
}
@@ -85,18 +71,19 @@ fn syntax_expander_table() -> HashMap<~str, syntax_extension> {
8571
syntax_expanders.insert(~"macro_rules",
8672
builtin_item_tt(
8773
ext::tt::macro_rules::add_new_extension));
88-
syntax_expanders.insert(~"fmt", builtin(ext::fmt::expand_syntax_ext));
74+
syntax_expanders.insert(~"fmt",
75+
builtin_normal_tt(ext::fmt::expand_syntax_ext));
8976
syntax_expanders.insert(
9077
~"auto_serialize",
9178
item_decorator(ext::auto_serialize::expand_auto_serialize));
9279
syntax_expanders.insert(
9380
~"auto_deserialize",
9481
item_decorator(ext::auto_serialize::expand_auto_deserialize));
95-
syntax_expanders.insert(~"env", builtin(ext::env::expand_syntax_ext));
82+
syntax_expanders.insert(~"env",
83+
builtin_normal_tt(ext::env::expand_syntax_ext));
9684
syntax_expanders.insert(~"concat_idents",
97-
builtin(ext::concat_idents::expand_syntax_ext));
98-
syntax_expanders.insert(~"ident_to_str",
99-
builtin(ext::ident_to_str::expand_syntax_ext));
85+
builtin_normal_tt(
86+
ext::concat_idents::expand_syntax_ext));
10087
syntax_expanders.insert(~"log_syntax",
10188
builtin_normal_tt(
10289
ext::log_syntax::expand_syntax_ext));
@@ -122,21 +109,29 @@ fn syntax_expander_table() -> HashMap<~str, syntax_extension> {
122109
builtin_normal_tt(ext::quote::expand_quote_stmt));
123110

124111
syntax_expanders.insert(~"line",
125-
builtin(ext::source_util::expand_line));
112+
builtin_normal_tt(
113+
ext::source_util::expand_line));
126114
syntax_expanders.insert(~"col",
127-
builtin(ext::source_util::expand_col));
115+
builtin_normal_tt(
116+
ext::source_util::expand_col));
128117
syntax_expanders.insert(~"file",
129-
builtin(ext::source_util::expand_file));
118+
builtin_normal_tt(
119+
ext::source_util::expand_file));
130120
syntax_expanders.insert(~"stringify",
131-
builtin(ext::source_util::expand_stringify));
121+
builtin_normal_tt(
122+
ext::source_util::expand_stringify));
132123
syntax_expanders.insert(~"include",
133-
builtin(ext::source_util::expand_include));
124+
builtin_normal_tt(
125+
ext::source_util::expand_include));
134126
syntax_expanders.insert(~"include_str",
135-
builtin(ext::source_util::expand_include_str));
127+
builtin_normal_tt(
128+
ext::source_util::expand_include_str));
136129
syntax_expanders.insert(~"include_bin",
137-
builtin(ext::source_util::expand_include_bin));
130+
builtin_normal_tt(
131+
ext::source_util::expand_include_bin));
138132
syntax_expanders.insert(~"module_path",
139-
builtin(ext::source_util::expand_mod));
133+
builtin_normal_tt(
134+
ext::source_util::expand_mod));
140135
syntax_expanders.insert(~"proto",
141136
builtin_item_tt(ext::pipes::expand_proto));
142137
syntax_expanders.insert(
@@ -292,87 +287,39 @@ fn expr_to_ident(cx: ext_ctxt,
292287
}
293288
}
294289

295-
fn get_mac_args_no_max(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
296-
min: uint, name: ~str) -> ~[@ast::expr] {
297-
return get_mac_args(cx, sp, arg, min, None, name);
290+
fn check_zero_tts(cx: ext_ctxt, sp: span, tts: &[ast::token_tree],
291+
name: &str) {
292+
if tts.len() != 0 {
293+
cx.span_fatal(sp, fmt!("%s takes no arguments", name));
294+
}
298295
}
299296

300-
fn get_mac_args(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
301-
min: uint, max: Option<uint>, name: ~str) -> ~[@ast::expr] {
302-
match arg {
303-
Some(expr) => match expr.node {
304-
ast::expr_vec(elts, _) => {
305-
let elts_len = vec::len(elts);
306-
match max {
307-
Some(max) if ! (min <= elts_len && elts_len <= max) => {
308-
cx.span_fatal(sp,
309-
fmt!("%s! takes between %u and %u arguments.",
310-
name, min, max));
311-
}
312-
None if ! (min <= elts_len) => {
313-
cx.span_fatal(sp, fmt!("%s! needs at least %u arguments.",
314-
name, min));
315-
}
316-
_ => return elts /* we are good */
317-
}
318-
}
319-
_ => {
320-
cx.span_fatal(sp, fmt!("%s!: malformed invocation", name))
321-
}
322-
},
323-
None => cx.span_fatal(sp, fmt!("%s!: missing arguments", name))
297+
fn get_single_str_from_tts(cx: ext_ctxt, sp: span, tts: &[ast::token_tree],
298+
name: &str) -> ~str {
299+
if tts.len() != 1 {
300+
cx.span_fatal(sp, fmt!("%s takes 1 argument.", name));
324301
}
325-
}
326302

327-
fn get_mac_body(cx: ext_ctxt, sp: span, args: ast::mac_body)
328-
-> ast::mac_body_
329-
{
330-
match (args) {
331-
Some(body) => body,
332-
None => cx.span_fatal(sp, ~"missing macro body")
303+
match tts[0] {
304+
ast::tt_tok(_, token::LIT_STR(ident)) => cx.str_of(ident),
305+
_ =>
306+
cx.span_fatal(sp, fmt!("%s requires a string.", name))
333307
}
334308
}
335309

336-
// Massage syntactic form of new-style arguments to internal representation
337-
// of old-style macro args, such that old-style macro can be run and invoked
338-
// using new syntax. This will be obsolete when #old_macros go away.
339-
fn tt_args_to_original_flavor(cx: ext_ctxt, sp: span, arg: ~[ast::token_tree])
340-
-> ast::mac_arg {
341-
use ast::{matcher, matcher_, match_tok, match_seq, match_nonterminal};
342-
use parse::lexer::{new_tt_reader, reader};
343-
use tt::macro_parser::{parse_or_else, matched_seq,
344-
matched_nonterminal};
345-
346-
// these spans won't matter, anyways
347-
fn ms(m: matcher_) -> matcher {
348-
{node: m, span: dummy_sp()}
310+
fn get_exprs_from_tts(cx: ext_ctxt, tts: ~[ast::token_tree])
311+
-> ~[@ast::expr] {
312+
let p = parse::new_parser_from_tts(cx.parse_sess(),
313+
cx.cfg(),
314+
tts);
315+
let mut es = ~[];
316+
while p.token != token::EOF {
317+
if es.len() != 0 {
318+
p.eat(token::COMMA);
319+
}
320+
es.push(p.parse_expr());
349321
}
350-
let arg_nm = cx.parse_sess().interner.gensym(@~"arg");
351-
352-
let argument_gram = ~[ms(match_seq(~[
353-
ms(match_nonterminal(arg_nm, parse::token::special_idents::expr, 0u))
354-
], Some(parse::token::COMMA), true, 0u, 1u))];
355-
356-
let arg_reader = new_tt_reader(cx.parse_sess().span_diagnostic,
357-
cx.parse_sess().interner, None, arg);
358-
let args =
359-
match parse_or_else(cx.parse_sess(), cx.cfg(), arg_reader as reader,
360-
argument_gram).get(arg_nm) {
361-
@matched_seq(s, _) => {
362-
do s.map() |lf| {
363-
match *lf {
364-
@matched_nonterminal(parse::token::nt_expr(arg)) =>
365-
arg, /* whew! list of exprs, here we come! */
366-
_ => fail ~"badly-structured parse result"
367-
}
368-
}
369-
},
370-
_ => fail ~"badly-structured parse result"
371-
};
372-
373-
return Some(@{id: parse::next_node_id(cx.parse_sess()),
374-
callee_id: parse::next_node_id(cx.parse_sess()),
375-
node: ast::expr_vec(args, ast::m_imm), span: sp});
322+
es
376323
}
377324

378325
//

src/libsyntax/ext/concat_idents.rs

+24-11
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,32 @@
1010

1111
use base::*;
1212

13-
fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg,
14-
_body: ast::mac_body) -> @ast::expr {
15-
let args = get_mac_args_no_max(cx,sp,arg,1u,~"concat_idents");
13+
fn expand_syntax_ext(cx: ext_ctxt, sp: span, tts: ~[ast::token_tree])
14+
-> base::mac_result {
1615
let mut res_str = ~"";
17-
for args.each |e| {
18-
res_str += *cx.parse_sess().interner.get(
19-
expr_to_ident(cx, *e, ~"expected an ident"));
16+
for tts.eachi |i, e| {
17+
if i & 1 == 1 {
18+
match *e {
19+
ast::tt_tok(_, token::COMMA) => (),
20+
_ => cx.span_fatal(sp, ~"concat_idents! \
21+
expecting comma.")
22+
}
23+
} else {
24+
match *e {
25+
ast::tt_tok(_, token::IDENT(ident,_)) =>
26+
res_str += cx.str_of(ident),
27+
_ => cx.span_fatal(sp, ~"concat_idents! \
28+
requires ident args.")
29+
}
30+
}
2031
}
2132
let res = cx.parse_sess().interner.intern(@res_str);
2233

23-
return @{id: cx.next_id(),
24-
callee_id: cx.next_id(),
25-
node: ast::expr_path(@{span: sp, global: false, idents: ~[res],
26-
rp: None, types: ~[]}),
27-
span: sp};
34+
let e = @{id: cx.next_id(),
35+
callee_id: cx.next_id(),
36+
node: ast::expr_path(@{span: sp, global: false,
37+
idents: ~[res],
38+
rp: None, types: ~[]}),
39+
span: sp};
40+
mr_expr(e)
2841
}

src/libsyntax/ext/env.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@ use base::*;
1818
use build::mk_uniq_str;
1919
export expand_syntax_ext;
2020

21-
fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, arg: ast::mac_arg,
22-
_body: ast::mac_body) -> @ast::expr {
23-
let args = get_mac_args(cx, sp, arg, 1u, option::Some(1u), ~"env");
21+
fn expand_syntax_ext(cx: ext_ctxt, sp: span, tts: ~[ast::token_tree])
22+
-> base::mac_result {
23+
24+
let var = get_single_str_from_tts(cx, sp, tts, "env!");
2425

2526
// FIXME (#2248): if this was more thorough it would manufacture an
2627
// Option<str> rather than just an maybe-empty string.
2728

28-
let var = expr_to_str(cx, args[0], ~"env! requires a string");
29-
match os::getenv(var) {
30-
option::None => return mk_uniq_str(cx, sp, ~""),
31-
option::Some(ref s) => return mk_uniq_str(cx, sp, (*s))
32-
}
29+
let e = match os::getenv(var) {
30+
option::None => mk_uniq_str(cx, sp, ~""),
31+
option::Some(ref s) => mk_uniq_str(cx, sp, (*s))
32+
};
33+
mr_expr(e)
3334
}
3435

3536
//

src/libsyntax/ext/expand.rs

+1-34
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,6 @@ fn expand_expr(exts: HashMap<~str, syntax_extension>, cx: ext_ctxt,
6262

6363
(fully_expanded, s)
6464
}
65-
Some(normal({expander: exp, span: exp_sp})) => {
66-
cx.bt_push(ExpandedFrom({call_site: s,
67-
callie: {name: *extname, span: exp_sp}}));
68-
69-
//convert the new-style invoc for the old-style macro
70-
let arg = base::tt_args_to_original_flavor(cx, pth.span,
71-
(*tts));
72-
let expanded = exp(cx, (*mac).span, arg, None);
73-
74-
//keep going, outside-in
75-
let fully_expanded = fld.fold_expr(expanded).node;
76-
cx.bt_pop();
77-
78-
(fully_expanded, s)
79-
}
8065
_ => {
8166
cx.span_fatal(pth.span,
8267
fmt!("'%s' is not a tt-style macro",
@@ -119,8 +104,7 @@ fn expand_mod_items(exts: HashMap<~str, syntax_extension>, cx: ext_ctxt,
119104
ast::meta_list(ref n, _) => (*n)
120105
};
121106
match exts.find(mname) {
122-
None | Some(normal(_))
123-
| Some(normal_tt(_)) | Some(item_tt(*)) => items,
107+
None | Some(normal_tt(_)) | Some(item_tt(*)) => items,
124108
Some(item_decorator(dec_fn)) => {
125109
cx.bt_push(ExpandedFrom({call_site: attr.span,
126110
callie: {name: copy mname,
@@ -262,23 +246,6 @@ fn expand_stmt(exts: HashMap<~str, syntax_extension>, cx: ext_ctxt,
262246
(fully_expanded, sp)
263247
}
264248

265-
Some(normal({expander: exp, span: exp_sp})) => {
266-
cx.bt_push(ExpandedFrom({call_site: sp,
267-
callie: {name: *extname,
268-
span: exp_sp}}));
269-
//convert the new-style invoc for the old-style macro
270-
let arg = base::tt_args_to_original_flavor(cx, pth.span, tts);
271-
let exp_expr = exp(cx, mac.span, arg, None);
272-
let expanded = @{node: stmt_expr(exp_expr, cx.next_id()),
273-
span: exp_expr.span};
274-
275-
//keep going, outside-in
276-
let fully_expanded = fld.fold_stmt(expanded).node;
277-
cx.bt_pop();
278-
279-
(fully_expanded, sp)
280-
}
281-
282249
_ => {
283250
cx.span_fatal(pth.span,
284251
fmt!("'%s' is not a tt-style macro", *extname))

src/libsyntax/ext/fmt.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ use codemap::span;
2121
use ext::build::*;
2222
export expand_syntax_ext;
2323

24-
fn expand_syntax_ext(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
25-
_body: ast::mac_body) -> @ast::expr {
26-
let args = get_mac_args_no_max(cx, sp, arg, 1u, ~"fmt");
24+
fn expand_syntax_ext(cx: ext_ctxt, sp: span, tts: ~[ast::token_tree])
25+
-> base::mac_result {
26+
let args = get_exprs_from_tts(cx, copy tts);
27+
if args.len() == 0 {
28+
cx.span_fatal(sp, "fmt! takes at least 1 argument.");
29+
}
2730
let fmt =
2831
expr_to_str(cx, args[0],
2932
~"first argument to fmt! must be a string literal.");
@@ -37,7 +40,7 @@ fn expand_syntax_ext(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
3740
parse_fmt_err_(cx, fmtspan, s)
3841
};
3942
let pieces = parse_fmt_string(fmt, parse_fmt_err);
40-
return pieces_to_expr(cx, sp, pieces, args);
43+
mr_expr(pieces_to_expr(cx, sp, pieces, args))
4144
}
4245

4346
// FIXME (#2249): A lot of these functions for producing expressions can

0 commit comments

Comments
 (0)