Skip to content

Commit f008bfd

Browse files
committed
creating convenience types for make_def, make_stmt in MacResult
Unifying with MacroDefiner
1 parent 0b7b4f0 commit f008bfd

File tree

2 files changed

+59
-14
lines changed

2 files changed

+59
-14
lines changed

src/libsyntax/ext/base.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ impl MacResult for MacPat {
202202
Some(self.p)
203203
}
204204
}
205-
/// A type for macros that return multiple items.
205+
/// A convenience type for macros that return multiple items.
206206
pub struct MacItems {
207207
items: SmallVector<P<ast::Item>>
208208
}
@@ -219,6 +219,59 @@ impl MacResult for MacItems {
219219
}
220220
}
221221

222+
/// A convenience type for macros that return a single statement
223+
pub struct MacStmt {
224+
stmt: P<ast::Stmt>,
225+
}
226+
227+
impl MacStmt {
228+
pub fn new(stmt: P<ast::Stmt>) -> MacStmt {
229+
MacStmt{ stmt: stmt }
230+
}
231+
}
232+
233+
impl MacResult for MacStmt {
234+
fn make_stmt(self: Box<MacStmt>) -> Option<P<ast::Stmt>> {
235+
Some(self.stmt)
236+
}
237+
}
238+
239+
240+
/// A convenience type for macros that return a macro def
241+
pub struct MacDef {
242+
def: Option<MacroDef>
243+
}
244+
245+
impl MacDef {
246+
pub fn new(def: MacroDef) -> MacDef {
247+
MacDef{ def: Some(def) }
248+
}
249+
}
250+
251+
impl MacResult for MacDef {
252+
fn make_def(&mut self) -> Option<MacroDef> {
253+
Some(self.def.take().expect("empty MacDef"))
254+
}
255+
}
256+
257+
/// A convenience type for macros that return methods
258+
pub struct MacMethods {
259+
methods: SmallVector<P<ast::Method>>
260+
}
261+
262+
impl MacMethods {
263+
pub fn new(methods: SmallVector<P<ast::Method>>) -> MacMethods {
264+
MacMethods{ methods: methods }
265+
}
266+
}
267+
268+
impl MacResult for MacMethods {
269+
fn make_methods(self: Box<MacMethods>) -> Option<SmallVector<P<ast::Method>>> {
270+
Some(self.methods)
271+
}
272+
}
273+
274+
222275
/// Fill-in macro expansion result, to allow compilation to continue
223276
/// after hitting errors.
224277
pub struct DummyResult {

src/libsyntax/ext/tt/macro_rules.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use ast::{Ident, TtDelimited, TtSequence, TtToken};
1212
use ast;
1313
use codemap::{Span, DUMMY_SP};
14-
use ext::base::{ExtCtxt, MacResult, MacroDef};
14+
use ext::base::{ExtCtxt, MacDef, MacResult, MacroDef};
1515
use ext::base::{NormalTT, TTMacroExpander};
1616
use ext::tt::macro_parser::{Success, Error, Failure};
1717
use ext::tt::macro_parser::{NamedMatch, MatchedSeq, MatchedNonterminal};
@@ -129,14 +129,6 @@ impl TTMacroExpander for MacroRulesMacroExpander {
129129
}
130130
}
131131

132-
struct MacroRulesDefiner {
133-
def: Option<MacroDef>
134-
}
135-
impl MacResult for MacroRulesDefiner {
136-
fn make_def(&mut self) -> Option<MacroDef> {
137-
Some(self.def.take().expect("empty MacroRulesDefiner"))
138-
}
139-
}
140132

141133
/// Given `lhses` and `rhses`, this is the new macro we create
142134
fn generic_extension<'cx>(cx: &'cx ExtCtxt,
@@ -279,10 +271,10 @@ pub fn add_new_extension<'cx>(cx: &'cx mut ExtCtxt,
279271
rhses: rhses,
280272
};
281273

282-
box MacroRulesDefiner {
283-
def: Some(MacroDef {
274+
box MacDef::new(
275+
MacroDef {
284276
name: token::get_ident(name).to_string(),
285277
ext: NormalTT(exp, Some(sp))
286-
})
287-
} as Box<MacResult+'cx>
278+
}
279+
) as Box<MacResult+'cx>
288280
}

0 commit comments

Comments
 (0)