Add patterns to MacResult

This commit is contained in:
Keegan McAllister 2014-05-19 13:32:51 -07:00
parent 5fdd0e4b05
commit 00704ea33b
2 changed files with 35 additions and 0 deletions

View File

@ -114,6 +114,10 @@ pub trait MacResult {
fn make_items(&self) -> Option<SmallVector<@ast::Item>> {
None
}
/// Create a pattern.
fn make_pat(&self) -> Option<@ast::Pat> {
None
}
/// Create a statement.
///
@ -139,6 +143,20 @@ impl MacResult for MacExpr {
Some(self.e)
}
}
/// A convenience type for macros that return a single pattern.
pub struct MacPat {
p: @ast::Pat
}
impl MacPat {
pub fn new(p: @ast::Pat) -> Box<MacResult> {
box MacPat { p: p } as Box<MacResult>
}
}
impl MacResult for MacPat {
fn make_pat(&self) -> Option<@ast::Pat> {
Some(self.p)
}
}
/// A convenience type for macros that return a single item.
pub struct MacItem {
i: @ast::Item
@ -194,12 +212,24 @@ impl DummyResult {
span: sp,
}
}
/// A plain dummy pattern.
pub fn raw_pat(sp: Span) -> @ast::Pat {
@ast::Pat {
id: ast::DUMMY_NODE_ID,
node: ast::PatWild,
span: sp,
}
}
}
impl MacResult for DummyResult {
fn make_expr(&self) -> Option<@ast::Expr> {
Some(DummyResult::raw_expr(self.span))
}
fn make_pat(&self) -> Option<@ast::Pat> {
Some(DummyResult::raw_pat(self.span))
}
fn make_items(&self) -> Option<SmallVector<@ast::Item>> {
if self.expr_only {
None

View File

@ -63,6 +63,11 @@ impl<'a> MacResult for ParserAnyMacro<'a> {
self.ensure_complete_parse(true);
Some(ret)
}
fn make_pat(&self) -> Option<@ast::Pat> {
let ret = self.parser.borrow_mut().parse_pat();
self.ensure_complete_parse(false);
Some(ret)
}
fn make_items(&self) -> Option<SmallVector<@ast::Item>> {
let mut ret = SmallVector::zero();
loop {