Skip to content

Commit 0494b07

Browse files
committed
Merge pull request #4172 from graydon/remove-old-syntax-ext
Remove old syntax ext
2 parents 0138d87 + 9a4c669 commit 0494b07

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+272
-1961
lines changed

src/librustc/middle/astencode.rs

+24-11
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,8 @@ fn decode_item_ast(par_doc: ebml::Doc) -> @ast::item {
10331033
trait fake_ext_ctxt {
10341034
fn cfg() -> ast::crate_cfg;
10351035
fn parse_sess() -> parse::parse_sess;
1036+
fn call_site() -> span;
1037+
fn ident_of(st: ~str) -> ast::ident;
10361038
}
10371039

10381040
#[cfg(test)]
@@ -1042,6 +1044,16 @@ type fake_session = parse::parse_sess;
10421044
impl fake_session: fake_ext_ctxt {
10431045
fn cfg() -> ast::crate_cfg { ~[] }
10441046
fn parse_sess() -> parse::parse_sess { self }
1047+
fn call_site() -> span {
1048+
codemap::span {
1049+
lo: codemap::BytePos(0),
1050+
hi: codemap::BytePos(0),
1051+
expn_info: None
1052+
}
1053+
}
1054+
fn ident_of(st: ~str) -> ast::ident {
1055+
self.interner.intern(@st)
1056+
}
10451057
}
10461058

10471059
#[cfg(test)]
@@ -1050,7 +1062,8 @@ fn mk_ctxt() -> fake_ext_ctxt {
10501062
}
10511063

10521064
#[cfg(test)]
1053-
fn roundtrip(in_item: @ast::item) {
1065+
fn roundtrip(in_item: Option<@ast::item>) {
1066+
let in_item = in_item.get();
10541067
let bytes = do io::with_bytes_writer |wr| {
10551068
let ebml_w = writer::Serializer(wr);
10561069
encode_item_ast(ebml_w, in_item);
@@ -1074,45 +1087,45 @@ fn roundtrip(in_item: @ast::item) {
10741087
#[test]
10751088
fn test_basic() {
10761089
let ext_cx = mk_ctxt();
1077-
roundtrip(#ast[item]{
1090+
roundtrip(quote_item!(
10781091
fn foo() {}
1079-
});
1092+
));
10801093
}
10811094

10821095
#[test]
10831096
fn test_smalltalk() {
10841097
let ext_cx = mk_ctxt();
1085-
roundtrip(#ast[item]{
1098+
roundtrip(quote_item!(
10861099
fn foo() -> int { 3 + 4 } // first smalltalk program ever executed.
1087-
});
1100+
));
10881101
}
10891102

10901103
#[test]
10911104
fn test_more() {
10921105
let ext_cx = mk_ctxt();
1093-
roundtrip(#ast[item]{
1106+
roundtrip(quote_item!(
10941107
fn foo(x: uint, y: uint) -> uint {
10951108
let z = x + y;
10961109
return z;
10971110
}
1098-
});
1111+
));
10991112
}
11001113

11011114
#[test]
11021115
fn test_simplification() {
11031116
let ext_cx = mk_ctxt();
1104-
let item_in = ast::ii_item(#ast[item] {
1117+
let item_in = ast::ii_item(quote_item!(
11051118
fn new_int_alist<B: Copy>() -> alist<int, B> {
11061119
fn eq_int(&&a: int, &&b: int) -> bool { a == b }
11071120
return {eq_fn: eq_int, mut data: ~[]};
11081121
}
1109-
});
1122+
).get());
11101123
let item_out = simplify_ast(item_in);
1111-
let item_exp = ast::ii_item(#ast[item] {
1124+
let item_exp = ast::ii_item(quote_item!(
11121125
fn new_int_alist<B: Copy>() -> alist<int, B> {
11131126
return {eq_fn: eq_int, mut data: ~[]};
11141127
}
1115-
});
1128+
).get());
11161129
match (item_out, item_exp) {
11171130
(ast::ii_item(item_out), ast::ii_item(item_exp)) => {
11181131
assert pprust::item_to_str(item_out, ext_cx.parse_sess().interner)

src/libsyntax/ast.rs

-14
Original file line numberDiff line numberDiff line change
@@ -831,24 +831,10 @@ 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_ {
845-
mac_invoc(@path, mac_arg, mac_body), // old macro-invocation
846837
mac_invoc_tt(@path,~[token_tree]), // new macro-invocation
847-
mac_ellipsis, // old pattern-match (obsolete)
848-
849-
// the span is used by the quoter/anti-quoter ...
850-
mac_aq(span /* span of quote */, @expr), // anti-quote
851-
mac_var(uint)
852838
}
853839

854840
type lit = spanned<lit_>;

src/libsyntax/ext/auto_serialize.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ priv impl ext_ctxt {
309309
fn lambda(blk: ast::blk) -> @ast::expr {
310310
let ext_cx = self;
311311
let blk_e = self.expr(blk.span, ast::expr_block(blk));
312-
#ast{ || $(blk_e) }
312+
quote_expr!( || $blk_e )
313313
}
314314

315315
fn blk(span: span, stmts: ~[@ast::stmt]) -> ast::blk {

src/libsyntax/ext/base.rs

+52-116
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,19 @@ 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, macro_defining, macro_definer,
20-
// builtin
21-
//
2218
// new-style macro! tt code:
2319
//
2420
// syntax_expander_tt, syntax_expander_tt_item, mac_result,
2521
// normal_tt, item_tt
2622
//
27-
// also note that ast::mac has way too many cases and can probably
28-
// be trimmed down substantially.
29-
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>};
23+
// also note that ast::mac used to have a bunch of extraneous cases and
24+
// is now probably a redundant AST node, can be merged with
25+
// ast::mac_invoc_tt.
3526

3627
type macro_def = {name: ~str, ext: syntax_extension};
3728

38-
// macro_definer is obsolete, remove when #old_macros go away.
39-
type macro_definer =
40-
fn@(ext_ctxt, span, ast::mac_arg, ast::mac_body) -> macro_def;
41-
4229
type item_decorator =
4330
fn@(ext_ctxt, span, ast::meta_item, ~[@ast::item]) -> ~[@ast::item];
4431

@@ -60,13 +47,7 @@ enum mac_result {
6047

6148
enum syntax_extension {
6249

63-
// normal() is obsolete, remove when #old_macros go away.
64-
normal(syntax_expander),
65-
66-
// macro_defining() is obsolete, remove when #old_macros go away.
67-
macro_defining(macro_definer),
68-
69-
// #[auto_serialize] and such. will probably survive death of #old_macros
50+
// #[auto_serialize] and such
7051
item_decorator(item_decorator),
7152

7253
// Token-tree expanders
@@ -80,37 +61,32 @@ enum syntax_extension {
8061
// A temporary hard-coded map of methods for expanding syntax extension
8162
// AST nodes into full ASTs
8263
fn syntax_expander_table() -> HashMap<~str, syntax_extension> {
83-
fn builtin(f: syntax_expander_) -> syntax_extension
84-
{normal({expander: f, span: None})}
8564
fn builtin_normal_tt(f: syntax_expander_tt_) -> syntax_extension {
8665
normal_tt({expander: f, span: None})
8766
}
8867
fn builtin_item_tt(f: syntax_expander_tt_item_) -> syntax_extension {
8968
item_tt({expander: f, span: None})
9069
}
9170
let syntax_expanders = HashMap();
92-
syntax_expanders.insert(~"macro",
93-
macro_defining(ext::simplext::add_new_extension));
9471
syntax_expanders.insert(~"macro_rules",
9572
builtin_item_tt(
9673
ext::tt::macro_rules::add_new_extension));
97-
syntax_expanders.insert(~"fmt", builtin(ext::fmt::expand_syntax_ext));
74+
syntax_expanders.insert(~"fmt",
75+
builtin_normal_tt(ext::fmt::expand_syntax_ext));
9876
syntax_expanders.insert(
9977
~"auto_serialize",
10078
item_decorator(ext::auto_serialize::expand_auto_serialize));
10179
syntax_expanders.insert(
10280
~"auto_deserialize",
10381
item_decorator(ext::auto_serialize::expand_auto_deserialize));
104-
syntax_expanders.insert(~"env", builtin(ext::env::expand_syntax_ext));
82+
syntax_expanders.insert(~"env",
83+
builtin_normal_tt(ext::env::expand_syntax_ext));
10584
syntax_expanders.insert(~"concat_idents",
106-
builtin(ext::concat_idents::expand_syntax_ext));
107-
syntax_expanders.insert(~"ident_to_str",
108-
builtin(ext::ident_to_str::expand_syntax_ext));
85+
builtin_normal_tt(
86+
ext::concat_idents::expand_syntax_ext));
10987
syntax_expanders.insert(~"log_syntax",
11088
builtin_normal_tt(
11189
ext::log_syntax::expand_syntax_ext));
112-
syntax_expanders.insert(~"ast",
113-
builtin(ext::qquote::expand_ast));
11490
syntax_expanders.insert(~"deriving_eq",
11591
item_decorator(
11692
ext::deriving::expand_deriving_eq));
@@ -133,21 +109,29 @@ fn syntax_expander_table() -> HashMap<~str, syntax_extension> {
133109
builtin_normal_tt(ext::quote::expand_quote_stmt));
134110

135111
syntax_expanders.insert(~"line",
136-
builtin(ext::source_util::expand_line));
112+
builtin_normal_tt(
113+
ext::source_util::expand_line));
137114
syntax_expanders.insert(~"col",
138-
builtin(ext::source_util::expand_col));
115+
builtin_normal_tt(
116+
ext::source_util::expand_col));
139117
syntax_expanders.insert(~"file",
140-
builtin(ext::source_util::expand_file));
118+
builtin_normal_tt(
119+
ext::source_util::expand_file));
141120
syntax_expanders.insert(~"stringify",
142-
builtin(ext::source_util::expand_stringify));
121+
builtin_normal_tt(
122+
ext::source_util::expand_stringify));
143123
syntax_expanders.insert(~"include",
144-
builtin(ext::source_util::expand_include));
124+
builtin_normal_tt(
125+
ext::source_util::expand_include));
145126
syntax_expanders.insert(~"include_str",
146-
builtin(ext::source_util::expand_include_str));
127+
builtin_normal_tt(
128+
ext::source_util::expand_include_str));
147129
syntax_expanders.insert(~"include_bin",
148-
builtin(ext::source_util::expand_include_bin));
130+
builtin_normal_tt(
131+
ext::source_util::expand_include_bin));
149132
syntax_expanders.insert(~"module_path",
150-
builtin(ext::source_util::expand_mod));
133+
builtin_normal_tt(
134+
ext::source_util::expand_mod));
151135
syntax_expanders.insert(~"proto",
152136
builtin_item_tt(ext::pipes::expand_proto));
153137
syntax_expanders.insert(
@@ -303,87 +287,39 @@ fn expr_to_ident(cx: ext_ctxt,
303287
}
304288
}
305289

306-
fn get_mac_args_no_max(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
307-
min: uint, name: ~str) -> ~[@ast::expr] {
308-
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+
}
309295
}
310296

311-
fn get_mac_args(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
312-
min: uint, max: Option<uint>, name: ~str) -> ~[@ast::expr] {
313-
match arg {
314-
Some(expr) => match expr.node {
315-
ast::expr_vec(elts, _) => {
316-
let elts_len = vec::len(elts);
317-
match max {
318-
Some(max) if ! (min <= elts_len && elts_len <= max) => {
319-
cx.span_fatal(sp,
320-
fmt!("%s! takes between %u and %u arguments.",
321-
name, min, max));
322-
}
323-
None if ! (min <= elts_len) => {
324-
cx.span_fatal(sp, fmt!("%s! needs at least %u arguments.",
325-
name, min));
326-
}
327-
_ => return elts /* we are good */
328-
}
329-
}
330-
_ => {
331-
cx.span_fatal(sp, fmt!("%s!: malformed invocation", name))
332-
}
333-
},
334-
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));
335301
}
336-
}
337302

338-
fn get_mac_body(cx: ext_ctxt, sp: span, args: ast::mac_body)
339-
-> ast::mac_body_
340-
{
341-
match (args) {
342-
Some(body) => body,
343-
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))
344307
}
345308
}
346309

347-
// Massage syntactic form of new-style arguments to internal representation
348-
// of old-style macro args, such that old-style macro can be run and invoked
349-
// using new syntax. This will be obsolete when #old_macros go away.
350-
fn tt_args_to_original_flavor(cx: ext_ctxt, sp: span, arg: ~[ast::token_tree])
351-
-> ast::mac_arg {
352-
use ast::{matcher, matcher_, match_tok, match_seq, match_nonterminal};
353-
use parse::lexer::{new_tt_reader, reader};
354-
use tt::macro_parser::{parse_or_else, matched_seq,
355-
matched_nonterminal};
356-
357-
// these spans won't matter, anyways
358-
fn ms(m: matcher_) -> matcher {
359-
{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());
360321
}
361-
let arg_nm = cx.parse_sess().interner.gensym(@~"arg");
362-
363-
let argument_gram = ~[ms(match_seq(~[
364-
ms(match_nonterminal(arg_nm, parse::token::special_idents::expr, 0u))
365-
], Some(parse::token::COMMA), true, 0u, 1u))];
366-
367-
let arg_reader = new_tt_reader(cx.parse_sess().span_diagnostic,
368-
cx.parse_sess().interner, None, arg);
369-
let args =
370-
match parse_or_else(cx.parse_sess(), cx.cfg(), arg_reader as reader,
371-
argument_gram).get(arg_nm) {
372-
@matched_seq(s, _) => {
373-
do s.map() |lf| {
374-
match *lf {
375-
@matched_nonterminal(parse::token::nt_expr(arg)) =>
376-
arg, /* whew! list of exprs, here we come! */
377-
_ => fail ~"badly-structured parse result"
378-
}
379-
}
380-
},
381-
_ => fail ~"badly-structured parse result"
382-
};
383-
384-
return Some(@{id: parse::next_node_id(cx.parse_sess()),
385-
callee_id: parse::next_node_id(cx.parse_sess()),
386-
node: ast::expr_vec(args, ast::m_imm), span: sp});
322+
es
387323
}
388324

389325
//

0 commit comments

Comments
 (0)