Skip to content

Commit 897a0a3

Browse files
committed
auto merge of #11803 : sfackler/rust/simple-mac, r=brson
Now that procedural macros can be implemented outside of the compiler, it's more important to have a reasonable API to work with. Here are the basic changes: * Rename SyntaxExpanderTTTrait to MacroExpander, SyntaxExpanderTT to BasicMacroExpander, etc. I think "procedural macro" is the right term for these now, right? The other option would be SynExtExpander or something like that. * Stop passing the SyntaxContext to extensions. This was only ever used by macro_rules, which doesn't even use it anymore. I can't think of a context in which an external extension would need it, and removal allows the API to be significantly simpler - no more SyntaxExpanderTTItemExpanderWithoutContext wrappers to worry about.
2 parents d2b44b6 + ab5bbd3 commit 897a0a3

File tree

4 files changed

+72
-141
lines changed

4 files changed

+72
-141
lines changed

src/libsyntax/ext/base.rs

+52-82
Original file line numberDiff line numberDiff line change
@@ -38,91 +38,63 @@ pub struct MacroDef {
3838
pub type ItemDecorator =
3939
fn(&ExtCtxt, Span, @ast::MetaItem, ~[@ast::Item]) -> ~[@ast::Item];
4040

41-
pub struct SyntaxExpanderTT {
42-
expander: SyntaxExpanderTTExpander,
41+
pub struct BasicMacroExpander {
42+
expander: MacroExpanderFn,
4343
span: Option<Span>
4444
}
4545

46-
pub trait SyntaxExpanderTTTrait {
46+
pub trait MacroExpander {
4747
fn expand(&self,
4848
ecx: &mut ExtCtxt,
4949
span: Span,
50-
token_tree: &[ast::TokenTree],
51-
context: ast::SyntaxContext)
50+
token_tree: &[ast::TokenTree])
5251
-> MacResult;
5352
}
5453

55-
pub type SyntaxExpanderTTFunNoCtxt =
54+
pub type MacroExpanderFn =
5655
fn(ecx: &mut ExtCtxt, span: codemap::Span, token_tree: &[ast::TokenTree])
5756
-> MacResult;
5857

59-
enum SyntaxExpanderTTExpander {
60-
SyntaxExpanderTTExpanderWithoutContext(SyntaxExpanderTTFunNoCtxt),
61-
}
62-
63-
impl SyntaxExpanderTTTrait for SyntaxExpanderTT {
58+
impl MacroExpander for BasicMacroExpander {
6459
fn expand(&self,
6560
ecx: &mut ExtCtxt,
6661
span: Span,
67-
token_tree: &[ast::TokenTree],
68-
_: ast::SyntaxContext)
62+
token_tree: &[ast::TokenTree])
6963
-> MacResult {
70-
match self.expander {
71-
SyntaxExpanderTTExpanderWithoutContext(f) => {
72-
f(ecx, span, token_tree)
73-
}
74-
}
64+
(self.expander)(ecx, span, token_tree)
7565
}
7666
}
7767

78-
enum SyntaxExpanderTTItemExpander {
79-
SyntaxExpanderTTItemExpanderWithContext(SyntaxExpanderTTItemFun),
80-
SyntaxExpanderTTItemExpanderWithoutContext(SyntaxExpanderTTItemFunNoCtxt),
81-
}
82-
83-
pub struct SyntaxExpanderTTItem {
84-
expander: SyntaxExpanderTTItemExpander,
68+
pub struct BasicIdentMacroExpander {
69+
expander: IdentMacroExpanderFn,
8570
span: Option<Span>
8671
}
8772

88-
pub trait SyntaxExpanderTTItemTrait {
73+
pub trait IdentMacroExpander {
8974
fn expand(&self,
9075
cx: &mut ExtCtxt,
9176
sp: Span,
9277
ident: ast::Ident,
93-
token_tree: ~[ast::TokenTree],
94-
context: ast::SyntaxContext)
78+
token_tree: ~[ast::TokenTree])
9579
-> MacResult;
9680
}
9781

98-
impl SyntaxExpanderTTItemTrait for SyntaxExpanderTTItem {
82+
impl IdentMacroExpander for BasicIdentMacroExpander {
9983
fn expand(&self,
10084
cx: &mut ExtCtxt,
10185
sp: Span,
10286
ident: ast::Ident,
103-
token_tree: ~[ast::TokenTree],
104-
context: ast::SyntaxContext)
87+
token_tree: ~[ast::TokenTree])
10588
-> MacResult {
106-
match self.expander {
107-
SyntaxExpanderTTItemExpanderWithContext(fun) => {
108-
fun(cx, sp, ident, token_tree, context)
109-
}
110-
SyntaxExpanderTTItemExpanderWithoutContext(fun) => {
111-
fun(cx, sp, ident, token_tree)
112-
}
113-
}
89+
(self.expander)(cx, sp, ident, token_tree)
11490
}
11591
}
11692

117-
pub type SyntaxExpanderTTItemFun =
118-
fn(&mut ExtCtxt, Span, ast::Ident, ~[ast::TokenTree], ast::SyntaxContext)
119-
-> MacResult;
120-
121-
pub type SyntaxExpanderTTItemFunNoCtxt =
93+
pub type IdentMacroExpanderFn =
12294
fn(&mut ExtCtxt, Span, ast::Ident, ~[ast::TokenTree]) -> MacResult;
12395

12496
pub type MacroCrateRegistrationFun =
125-
extern "Rust" fn(|ast::Name, SyntaxExtension|);
97+
fn(|ast::Name, SyntaxExtension|);
12698

12799
pub trait AnyMacro {
128100
fn make_expr(&self) -> @ast::Expr;
@@ -153,7 +125,7 @@ pub enum SyntaxExtension {
153125
ItemDecorator(ItemDecorator),
154126

155127
// Token-tree expanders
156-
NormalTT(~SyntaxExpanderTTTrait:'static, Option<Span>),
128+
NormalTT(~MacroExpander:'static, Option<Span>),
157129

158130
// An IdentTT is a macro that has an
159131
// identifier in between the name of the
@@ -163,7 +135,7 @@ pub enum SyntaxExtension {
163135

164136
// perhaps macro_rules! will lose its odd special identifier argument,
165137
// and this can go away also
166-
IdentTT(~SyntaxExpanderTTItemTrait:'static, Option<Span>),
138+
IdentTT(~IdentMacroExpander:'static, Option<Span>),
167139
}
168140

169141
pub struct BlockInfo {
@@ -192,102 +164,100 @@ pub type RenameList = ~[(ast::Ident,Name)];
192164
// AST nodes into full ASTs
193165
pub fn syntax_expander_table() -> SyntaxEnv {
194166
// utility function to simplify creating NormalTT syntax extensions
195-
fn builtin_normal_tt_no_ctxt(f: SyntaxExpanderTTFunNoCtxt)
196-
-> SyntaxExtension {
197-
NormalTT(~SyntaxExpanderTT{
198-
expander: SyntaxExpanderTTExpanderWithoutContext(f),
199-
span: None,
200-
},
201-
None)
167+
fn builtin_normal_expander(f: MacroExpanderFn) -> SyntaxExtension {
168+
NormalTT(~BasicMacroExpander {
169+
expander: f,
170+
span: None,
171+
},
172+
None)
202173
}
203174

204175
let mut syntax_expanders = SyntaxEnv::new();
205176
syntax_expanders.insert(intern(&"macro_rules"),
206-
IdentTT(~SyntaxExpanderTTItem {
207-
expander: SyntaxExpanderTTItemExpanderWithContext(
208-
ext::tt::macro_rules::add_new_extension),
177+
IdentTT(~BasicIdentMacroExpander {
178+
expander: ext::tt::macro_rules::add_new_extension,
209179
span: None,
210180
},
211181
None));
212182
syntax_expanders.insert(intern(&"fmt"),
213-
builtin_normal_tt_no_ctxt(
183+
builtin_normal_expander(
214184
ext::fmt::expand_syntax_ext));
215185
syntax_expanders.insert(intern(&"format_args"),
216-
builtin_normal_tt_no_ctxt(
186+
builtin_normal_expander(
217187
ext::format::expand_args));
218188
syntax_expanders.insert(intern(&"env"),
219-
builtin_normal_tt_no_ctxt(
189+
builtin_normal_expander(
220190
ext::env::expand_env));
221191
syntax_expanders.insert(intern(&"option_env"),
222-
builtin_normal_tt_no_ctxt(
192+
builtin_normal_expander(
223193
ext::env::expand_option_env));
224194
syntax_expanders.insert(intern("bytes"),
225-
builtin_normal_tt_no_ctxt(
195+
builtin_normal_expander(
226196
ext::bytes::expand_syntax_ext));
227197
syntax_expanders.insert(intern("concat_idents"),
228-
builtin_normal_tt_no_ctxt(
198+
builtin_normal_expander(
229199
ext::concat_idents::expand_syntax_ext));
230200
syntax_expanders.insert(intern("concat"),
231-
builtin_normal_tt_no_ctxt(
201+
builtin_normal_expander(
232202
ext::concat::expand_syntax_ext));
233203
syntax_expanders.insert(intern(&"log_syntax"),
234-
builtin_normal_tt_no_ctxt(
204+
builtin_normal_expander(
235205
ext::log_syntax::expand_syntax_ext));
236206
syntax_expanders.insert(intern(&"deriving"),
237207
ItemDecorator(ext::deriving::expand_meta_deriving));
238208

239209
// Quasi-quoting expanders
240210
syntax_expanders.insert(intern(&"quote_tokens"),
241-
builtin_normal_tt_no_ctxt(
211+
builtin_normal_expander(
242212
ext::quote::expand_quote_tokens));
243213
syntax_expanders.insert(intern(&"quote_expr"),
244-
builtin_normal_tt_no_ctxt(
214+
builtin_normal_expander(
245215
ext::quote::expand_quote_expr));
246216
syntax_expanders.insert(intern(&"quote_ty"),
247-
builtin_normal_tt_no_ctxt(
217+
builtin_normal_expander(
248218
ext::quote::expand_quote_ty));
249219
syntax_expanders.insert(intern(&"quote_item"),
250-
builtin_normal_tt_no_ctxt(
220+
builtin_normal_expander(
251221
ext::quote::expand_quote_item));
252222
syntax_expanders.insert(intern(&"quote_pat"),
253-
builtin_normal_tt_no_ctxt(
223+
builtin_normal_expander(
254224
ext::quote::expand_quote_pat));
255225
syntax_expanders.insert(intern(&"quote_stmt"),
256-
builtin_normal_tt_no_ctxt(
226+
builtin_normal_expander(
257227
ext::quote::expand_quote_stmt));
258228

259229
syntax_expanders.insert(intern(&"line"),
260-
builtin_normal_tt_no_ctxt(
230+
builtin_normal_expander(
261231
ext::source_util::expand_line));
262232
syntax_expanders.insert(intern(&"col"),
263-
builtin_normal_tt_no_ctxt(
233+
builtin_normal_expander(
264234
ext::source_util::expand_col));
265235
syntax_expanders.insert(intern(&"file"),
266-
builtin_normal_tt_no_ctxt(
236+
builtin_normal_expander(
267237
ext::source_util::expand_file));
268238
syntax_expanders.insert(intern(&"stringify"),
269-
builtin_normal_tt_no_ctxt(
239+
builtin_normal_expander(
270240
ext::source_util::expand_stringify));
271241
syntax_expanders.insert(intern(&"include"),
272-
builtin_normal_tt_no_ctxt(
242+
builtin_normal_expander(
273243
ext::source_util::expand_include));
274244
syntax_expanders.insert(intern(&"include_str"),
275-
builtin_normal_tt_no_ctxt(
245+
builtin_normal_expander(
276246
ext::source_util::expand_include_str));
277247
syntax_expanders.insert(intern(&"include_bin"),
278-
builtin_normal_tt_no_ctxt(
248+
builtin_normal_expander(
279249
ext::source_util::expand_include_bin));
280250
syntax_expanders.insert(intern(&"module_path"),
281-
builtin_normal_tt_no_ctxt(
251+
builtin_normal_expander(
282252
ext::source_util::expand_mod));
283253
syntax_expanders.insert(intern(&"asm"),
284-
builtin_normal_tt_no_ctxt(
254+
builtin_normal_expander(
285255
ext::asm::expand_asm));
286256
syntax_expanders.insert(intern(&"cfg"),
287-
builtin_normal_tt_no_ctxt(
257+
builtin_normal_expander(
288258
ext::cfg::expand_cfg));
289259
syntax_expanders.insert(intern(&"trace_macros"),
290-
builtin_normal_tt_no_ctxt(
260+
builtin_normal_expander(
291261
ext::trace_macros::expand_trace_macros));
292262
syntax_expanders
293263
}

0 commit comments

Comments
 (0)