diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 0c7a3cf4a6ce3..16a9f0a472529 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -202,7 +202,7 @@ impl MacResult for MacPat { Some(self.p) } } -/// A type for macros that return multiple items. +/// A convenience type for macros that return multiple items. pub struct MacItems { items: SmallVector> } @@ -219,6 +219,59 @@ impl MacResult for MacItems { } } +/// A convenience type for macros that return a single statement +pub struct MacStmt { + stmt: P, +} + +impl MacStmt { + pub fn new(stmt: P) -> MacStmt { + MacStmt{ stmt: stmt } + } +} + +impl MacResult for MacStmt { + fn make_stmt(self: Box) -> Option> { + Some(self.stmt) + } +} + + +/// A convenience type for macros that return a macro def +pub struct MacDef { + def: Option +} + +impl MacDef { + pub fn new(def: MacroDef) -> MacDef { + MacDef{ def: Some(def) } + } +} + +impl MacResult for MacDef { + fn make_def(&mut self) -> Option { + Some(self.def.take().expect("empty MacDef")) + } +} + +/// A convenience type for macros that return methods +pub struct MacMethods { + methods: SmallVector> +} + +impl MacMethods { + pub fn new(methods: SmallVector>) -> MacMethods { + MacMethods{ methods: methods } + } +} + +impl MacResult for MacMethods { + fn make_methods(self: Box) -> Option>> { + Some(self.methods) + } +} + + /// Fill-in macro expansion result, to allow compilation to continue /// after hitting errors. pub struct DummyResult { diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index 92c68b7a9c724..967747a3ac2b9 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -11,7 +11,7 @@ use ast::{Ident, TtDelimited, TtSequence, TtToken}; use ast; use codemap::{Span, DUMMY_SP}; -use ext::base::{ExtCtxt, MacResult, MacroDef}; +use ext::base::{ExtCtxt, MacDef, MacResult, MacroDef}; use ext::base::{NormalTT, TTMacroExpander}; use ext::tt::macro_parser::{Success, Error, Failure}; use ext::tt::macro_parser::{NamedMatch, MatchedSeq, MatchedNonterminal}; @@ -129,14 +129,6 @@ impl TTMacroExpander for MacroRulesMacroExpander { } } -struct MacroRulesDefiner { - def: Option -} -impl MacResult for MacroRulesDefiner { - fn make_def(&mut self) -> Option { - Some(self.def.take().expect("empty MacroRulesDefiner")) - } -} /// Given `lhses` and `rhses`, this is the new macro we create fn generic_extension<'cx>(cx: &'cx ExtCtxt, @@ -279,10 +271,10 @@ pub fn add_new_extension<'cx>(cx: &'cx mut ExtCtxt, rhses: rhses, }; - box MacroRulesDefiner { - def: Some(MacroDef { + box MacDef::new( + MacroDef { name: token::get_ident(name).to_string(), ext: NormalTT(exp, Some(sp)) - }) - } as Box + } + ) as Box }