Skip to content

creating convenience types for make_def, make_stmt, make_methods in MacResult #19019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P<ast::Item>>
}
Expand All @@ -219,6 +219,59 @@ impl MacResult for MacItems {
}
}

/// A convenience type for macros that return a single statement
pub struct MacStmt {
stmt: P<ast::Stmt>,
}

impl MacStmt {
pub fn new(stmt: P<ast::Stmt>) -> MacStmt {
MacStmt{ stmt: stmt }
}
}

impl MacResult for MacStmt {
fn make_stmt(self: Box<MacStmt>) -> Option<P<ast::Stmt>> {
Some(self.stmt)
}
}


/// A convenience type for macros that return a macro def
pub struct MacDef {
def: Option<MacroDef>
}

impl MacDef {
pub fn new(def: MacroDef) -> MacDef {
MacDef{ def: Some(def) }
}
}

impl MacResult for MacDef {
fn make_def(&mut self) -> Option<MacroDef> {
Some(self.def.take().expect("empty MacDef"))
}
}

/// A convenience type for macros that return methods
pub struct MacMethods {
methods: SmallVector<P<ast::Method>>
}

impl MacMethods {
pub fn new(methods: SmallVector<P<ast::Method>>) -> MacMethods {
MacMethods{ methods: methods }
}
}

impl MacResult for MacMethods {
fn make_methods(self: Box<MacMethods>) -> Option<SmallVector<P<ast::Method>>> {
Some(self.methods)
}
}


/// Fill-in macro expansion result, to allow compilation to continue
/// after hitting errors.
pub struct DummyResult {
Expand Down
18 changes: 5 additions & 13 deletions src/libsyntax/ext/tt/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -129,14 +129,6 @@ impl TTMacroExpander for MacroRulesMacroExpander {
}
}

struct MacroRulesDefiner {
def: Option<MacroDef>
}
impl MacResult for MacroRulesDefiner {
fn make_def(&mut self) -> Option<MacroDef> {
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,
Expand Down Expand Up @@ -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<MacResult+'cx>
}
) as Box<MacResult+'cx>
}