@@ -13,32 +13,19 @@ use parse::parser;
13
13
use diagnostic:: span_handler;
14
14
use codemap:: { CodeMap , span, ExpnInfo , ExpandedFrom } ;
15
15
use ast_util:: dummy_sp;
16
+ use parse:: token;
16
17
17
- // obsolete old-style #macro code:
18
- //
19
- // syntax_expander, normal, macro_defining, macro_definer,
20
- // builtin
21
- //
22
18
// new-style macro! tt code:
23
19
//
24
20
// syntax_expander_tt, syntax_expander_tt_item, mac_result,
25
21
// normal_tt, item_tt
26
22
//
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.
35
26
36
27
type macro_def = { name : ~str , ext : syntax_extension } ;
37
28
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
-
42
29
type item_decorator =
43
30
fn @( ext_ctxt , span , ast:: meta_item , ~[ @ast:: item ] ) -> ~[ @ast:: item ] ;
44
31
@@ -60,13 +47,7 @@ enum mac_result {
60
47
61
48
enum syntax_extension {
62
49
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
70
51
item_decorator( item_decorator ) ,
71
52
72
53
// Token-tree expanders
@@ -80,37 +61,32 @@ enum syntax_extension {
80
61
// A temporary hard-coded map of methods for expanding syntax extension
81
62
// AST nodes into full ASTs
82
63
fn syntax_expander_table ( ) -> HashMap < ~str , syntax_extension > {
83
- fn builtin ( f : syntax_expander_ ) -> syntax_extension
84
- { normal ( { expander: f, span: None } ) }
85
64
fn builtin_normal_tt ( f : syntax_expander_tt_ ) -> syntax_extension {
86
65
normal_tt ( { expander: f, span: None } )
87
66
}
88
67
fn builtin_item_tt ( f : syntax_expander_tt_item_ ) -> syntax_extension {
89
68
item_tt ( { expander: f, span: None } )
90
69
}
91
70
let syntax_expanders = HashMap ( ) ;
92
- syntax_expanders. insert ( ~"macro",
93
- macro_defining ( ext:: simplext:: add_new_extension) ) ;
94
71
syntax_expanders. insert ( ~"macro_rules",
95
72
builtin_item_tt (
96
73
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) ) ;
98
76
syntax_expanders. insert (
99
77
~"auto_serialize",
100
78
item_decorator ( ext:: auto_serialize:: expand_auto_serialize) ) ;
101
79
syntax_expanders. insert (
102
80
~"auto_deserialize",
103
81
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) ) ;
105
84
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) ) ;
109
87
syntax_expanders. insert ( ~"log_syntax",
110
88
builtin_normal_tt (
111
89
ext:: log_syntax:: expand_syntax_ext) ) ;
112
- syntax_expanders. insert ( ~"ast",
113
- builtin ( ext:: qquote:: expand_ast) ) ;
114
90
syntax_expanders. insert ( ~"deriving_eq",
115
91
item_decorator (
116
92
ext:: deriving:: expand_deriving_eq) ) ;
@@ -133,21 +109,29 @@ fn syntax_expander_table() -> HashMap<~str, syntax_extension> {
133
109
builtin_normal_tt ( ext:: quote:: expand_quote_stmt) ) ;
134
110
135
111
syntax_expanders. insert ( ~"line",
136
- builtin ( ext:: source_util:: expand_line) ) ;
112
+ builtin_normal_tt (
113
+ ext:: source_util:: expand_line) ) ;
137
114
syntax_expanders. insert ( ~"col",
138
- builtin ( ext:: source_util:: expand_col) ) ;
115
+ builtin_normal_tt (
116
+ ext:: source_util:: expand_col) ) ;
139
117
syntax_expanders. insert ( ~"file",
140
- builtin ( ext:: source_util:: expand_file) ) ;
118
+ builtin_normal_tt (
119
+ ext:: source_util:: expand_file) ) ;
141
120
syntax_expanders. insert ( ~"stringify",
142
- builtin ( ext:: source_util:: expand_stringify) ) ;
121
+ builtin_normal_tt (
122
+ ext:: source_util:: expand_stringify) ) ;
143
123
syntax_expanders. insert ( ~"include",
144
- builtin ( ext:: source_util:: expand_include) ) ;
124
+ builtin_normal_tt (
125
+ ext:: source_util:: expand_include) ) ;
145
126
syntax_expanders. insert ( ~"include_str",
146
- builtin ( ext:: source_util:: expand_include_str) ) ;
127
+ builtin_normal_tt (
128
+ ext:: source_util:: expand_include_str) ) ;
147
129
syntax_expanders. insert ( ~"include_bin",
148
- builtin ( ext:: source_util:: expand_include_bin) ) ;
130
+ builtin_normal_tt (
131
+ ext:: source_util:: expand_include_bin) ) ;
149
132
syntax_expanders. insert ( ~"module_path",
150
- builtin ( ext:: source_util:: expand_mod) ) ;
133
+ builtin_normal_tt (
134
+ ext:: source_util:: expand_mod) ) ;
151
135
syntax_expanders. insert ( ~"proto",
152
136
builtin_item_tt ( ext:: pipes:: expand_proto) ) ;
153
137
syntax_expanders. insert (
@@ -303,87 +287,39 @@ fn expr_to_ident(cx: ext_ctxt,
303
287
}
304
288
}
305
289
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
+ }
309
295
}
310
296
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) ) ;
335
301
}
336
- }
337
302
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) )
344
307
}
345
308
}
346
309
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 ( ) ) ;
360
321
}
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, 0 u) )
365
- ] , Some ( parse:: token:: COMMA ) , true , 0 u, 1 u) ) ] ;
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
387
323
}
388
324
389
325
//
0 commit comments