@@ -33,6 +33,10 @@ pub struct MacroDef {
33
33
ext : SyntaxExtension
34
34
}
35
35
36
+ // No context arg for an Item Decorator macro, simply because
37
+ // adding it would require adding a ctxt field to all items.
38
+ // we could do this if it turns out to be useful.
39
+
36
40
pub type ItemDecoratorFun = @fn ( @ExtCtxt ,
37
41
Span ,
38
42
@ast:: MetaItem ,
@@ -41,15 +45,34 @@ pub type ItemDecoratorFun = @fn(@ExtCtxt,
41
45
42
46
pub type SyntaxExpanderTTFun = @fn ( @ExtCtxt ,
43
47
Span ,
44
- & [ ast:: token_tree ] )
48
+ & [ ast:: token_tree ] ,
49
+ ast:: SyntaxContext )
45
50
-> MacResult ;
46
51
47
52
pub type SyntaxExpanderTTItemFun = @fn ( @ExtCtxt ,
53
+ Span ,
54
+ ast:: Ident ,
55
+ ~[ ast:: token_tree ] ,
56
+ ast:: SyntaxContext )
57
+ -> MacResult ;
58
+
59
+ // oog... in order to make the presentation of builtin_normal_tt_no_ctxt
60
+ // and builtin_ident_tt_no_ctxt palatable, we need one-off types for
61
+ // functions that don't consume a ctxt:
62
+
63
+ pub type SyntaxExpanderTTFunNoCtxt = @fn ( @ExtCtxt ,
64
+ Span ,
65
+ & [ ast:: token_tree ] )
66
+ -> MacResult ;
67
+
68
+ pub type SyntaxExpanderTTItemFunNoCtxt = @fn ( @ExtCtxt ,
48
69
Span ,
49
70
ast:: Ident ,
50
71
~[ ast:: token_tree ] )
51
72
-> MacResult ;
52
73
74
+
75
+
53
76
pub enum MacResult {
54
77
MRExpr ( @ast:: Expr ) ,
55
78
MRItem ( @ast:: item ) ,
@@ -78,6 +101,7 @@ pub enum SyntaxExtension {
78
101
IdentTT ( SyntaxExpanderTTItemFun , Option < Span > ) ,
79
102
}
80
103
104
+
81
105
// The SyntaxEnv is the environment that's threaded through the expansion
82
106
// of macros. It contains bindings for macros, and also a special binding
83
107
// for " block" (not a legal identifier) that maps to a BlockInfo
@@ -109,12 +133,16 @@ type RenameList = ~[(ast::Ident,Name)];
109
133
// AST nodes into full ASTs
110
134
pub fn syntax_expander_table ( ) -> SyntaxEnv {
111
135
// utility function to simplify creating NormalTT syntax extensions
112
- fn builtin_normal_tt ( f : SyntaxExpanderTTFun ) -> @Transformer {
113
- @SE ( NormalTT ( f, None ) )
136
+ // that ignore their contexts
137
+ fn builtin_normal_tt_no_ctxt ( f : SyntaxExpanderTTFunNoCtxt ) -> @Transformer {
138
+ let wrapped_expander : SyntaxExpanderTTFun = |a, b, c, d|{ f ( a, b, c) } ;
139
+ @SE ( NormalTT ( wrapped_expander, None ) )
114
140
}
115
141
// utility function to simplify creating IdentTT syntax extensions
116
- fn builtin_item_tt ( f : SyntaxExpanderTTItemFun ) -> @Transformer {
117
- @SE ( IdentTT ( f, None ) )
142
+ // that ignore their contexts
143
+ fn builtin_item_tt_no_ctxt ( f : SyntaxExpanderTTItemFunNoCtxt ) -> @Transformer {
144
+ let wrapped_expander : SyntaxExpanderTTItemFun = |a, b, c, d, e|{ f ( a, b, c, d) } ;
145
+ @SE ( IdentTT ( wrapped_expander, None ) )
118
146
}
119
147
let mut syntax_expanders = HashMap :: new ( ) ;
120
148
// NB identifier starts with space, and can't conflict with legal idents
@@ -124,83 +152,83 @@ pub fn syntax_expander_table() -> SyntaxEnv {
124
152
pending_renames : @mut ~[ ]
125
153
} ) ) ;
126
154
syntax_expanders. insert ( intern ( & "macro_rules" ) ,
127
- builtin_item_tt (
155
+ builtin_item_tt_no_ctxt (
128
156
ext:: tt:: macro_rules:: add_new_extension) ) ;
129
157
syntax_expanders. insert ( intern ( & "fmt" ) ,
130
- builtin_normal_tt ( ext:: fmt:: expand_syntax_ext) ) ;
158
+ builtin_normal_tt_no_ctxt ( ext:: fmt:: expand_syntax_ext) ) ;
131
159
syntax_expanders. insert ( intern ( & "format" ) ,
132
- builtin_normal_tt ( ext:: ifmt:: expand_format) ) ;
160
+ builtin_normal_tt_no_ctxt ( ext:: ifmt:: expand_format) ) ;
133
161
syntax_expanders. insert ( intern ( & "write" ) ,
134
- builtin_normal_tt ( ext:: ifmt:: expand_write) ) ;
162
+ builtin_normal_tt_no_ctxt ( ext:: ifmt:: expand_write) ) ;
135
163
syntax_expanders. insert ( intern ( & "writeln" ) ,
136
- builtin_normal_tt ( ext:: ifmt:: expand_writeln) ) ;
164
+ builtin_normal_tt_no_ctxt ( ext:: ifmt:: expand_writeln) ) ;
137
165
syntax_expanders. insert (
138
166
intern ( & "auto_encode" ) ,
139
167
@SE ( ItemDecorator ( ext:: auto_encode:: expand_auto_encode) ) ) ;
140
168
syntax_expanders. insert (
141
169
intern ( & "auto_decode" ) ,
142
170
@SE ( ItemDecorator ( ext:: auto_encode:: expand_auto_decode) ) ) ;
143
171
syntax_expanders. insert ( intern ( & "env" ) ,
144
- builtin_normal_tt ( ext:: env:: expand_env) ) ;
172
+ builtin_normal_tt_no_ctxt ( ext:: env:: expand_env) ) ;
145
173
syntax_expanders. insert ( intern ( & "option_env" ) ,
146
- builtin_normal_tt ( ext:: env:: expand_option_env) ) ;
174
+ builtin_normal_tt_no_ctxt ( ext:: env:: expand_option_env) ) ;
147
175
syntax_expanders. insert ( intern ( "bytes" ) ,
148
- builtin_normal_tt ( ext:: bytes:: expand_syntax_ext) ) ;
176
+ builtin_normal_tt_no_ctxt ( ext:: bytes:: expand_syntax_ext) ) ;
149
177
syntax_expanders. insert ( intern ( "concat_idents" ) ,
150
- builtin_normal_tt (
178
+ builtin_normal_tt_no_ctxt (
151
179
ext:: concat_idents:: expand_syntax_ext) ) ;
152
180
syntax_expanders. insert ( intern ( & "log_syntax" ) ,
153
- builtin_normal_tt (
181
+ builtin_normal_tt_no_ctxt (
154
182
ext:: log_syntax:: expand_syntax_ext) ) ;
155
183
syntax_expanders. insert ( intern ( & "deriving" ) ,
156
184
@SE ( ItemDecorator (
157
185
ext:: deriving:: expand_meta_deriving) ) ) ;
158
186
159
187
// Quasi-quoting expanders
160
188
syntax_expanders. insert ( intern ( & "quote_tokens" ) ,
161
- builtin_normal_tt ( ext:: quote:: expand_quote_tokens) ) ;
189
+ @ SE ( NormalTT ( ext:: quote:: expand_quote_tokens, None ) ) ) ;
162
190
syntax_expanders. insert ( intern ( & "quote_expr" ) ,
163
- builtin_normal_tt ( ext:: quote:: expand_quote_expr) ) ;
191
+ @ SE ( NormalTT ( ext:: quote:: expand_quote_expr, None ) ) ) ;
164
192
syntax_expanders. insert ( intern ( & "quote_ty" ) ,
165
- builtin_normal_tt ( ext:: quote:: expand_quote_ty) ) ;
193
+ @ SE ( NormalTT ( ext:: quote:: expand_quote_ty, None ) ) ) ;
166
194
syntax_expanders. insert ( intern ( & "quote_item" ) ,
167
- builtin_normal_tt ( ext:: quote:: expand_quote_item) ) ;
195
+ @ SE ( NormalTT ( ext:: quote:: expand_quote_item, None ) ) ) ;
168
196
syntax_expanders. insert ( intern ( & "quote_pat" ) ,
169
- builtin_normal_tt ( ext:: quote:: expand_quote_pat) ) ;
197
+ @ SE ( NormalTT ( ext:: quote:: expand_quote_pat, None ) ) ) ;
170
198
syntax_expanders. insert ( intern ( & "quote_stmt" ) ,
171
- builtin_normal_tt ( ext:: quote:: expand_quote_stmt) ) ;
199
+ @ SE ( NormalTT ( ext:: quote:: expand_quote_stmt, None ) ) ) ;
172
200
173
201
syntax_expanders. insert ( intern ( & "line" ) ,
174
- builtin_normal_tt (
202
+ builtin_normal_tt_no_ctxt (
175
203
ext:: source_util:: expand_line) ) ;
176
204
syntax_expanders. insert ( intern ( & "col" ) ,
177
- builtin_normal_tt (
205
+ builtin_normal_tt_no_ctxt (
178
206
ext:: source_util:: expand_col) ) ;
179
207
syntax_expanders. insert ( intern ( & "file" ) ,
180
- builtin_normal_tt (
208
+ builtin_normal_tt_no_ctxt (
181
209
ext:: source_util:: expand_file) ) ;
182
210
syntax_expanders. insert ( intern ( & "stringify" ) ,
183
- builtin_normal_tt (
211
+ builtin_normal_tt_no_ctxt (
184
212
ext:: source_util:: expand_stringify) ) ;
185
213
syntax_expanders. insert ( intern ( & "include" ) ,
186
- builtin_normal_tt (
214
+ builtin_normal_tt_no_ctxt (
187
215
ext:: source_util:: expand_include) ) ;
188
216
syntax_expanders. insert ( intern ( & "include_str" ) ,
189
- builtin_normal_tt (
217
+ builtin_normal_tt_no_ctxt (
190
218
ext:: source_util:: expand_include_str) ) ;
191
219
syntax_expanders. insert ( intern ( & "include_bin" ) ,
192
- builtin_normal_tt (
220
+ builtin_normal_tt_no_ctxt (
193
221
ext:: source_util:: expand_include_bin) ) ;
194
222
syntax_expanders. insert ( intern ( & "module_path" ) ,
195
- builtin_normal_tt (
223
+ builtin_normal_tt_no_ctxt (
196
224
ext:: source_util:: expand_mod) ) ;
197
225
syntax_expanders. insert ( intern ( & "asm" ) ,
198
- builtin_normal_tt ( ext:: asm:: expand_asm) ) ;
226
+ builtin_normal_tt_no_ctxt ( ext:: asm:: expand_asm) ) ;
199
227
syntax_expanders. insert ( intern ( & "cfg" ) ,
200
- builtin_normal_tt ( ext:: cfg:: expand_cfg) ) ;
228
+ builtin_normal_tt_no_ctxt ( ext:: cfg:: expand_cfg) ) ;
201
229
syntax_expanders. insert (
202
230
intern ( & "trace_macros" ) ,
203
- builtin_normal_tt ( ext:: trace_macros:: expand_trace_macros) ) ;
231
+ builtin_normal_tt_no_ctxt ( ext:: trace_macros:: expand_trace_macros) ) ;
204
232
MapChain :: new ( ~syntax_expanders)
205
233
}
206
234
0 commit comments