add make_method method to MacResult trait
this allows macro results to be parsed as methods
This commit is contained in:
@@ -104,6 +104,9 @@ pub type IdentMacroExpanderFn =
|
|||||||
/// just into the compiler's internal macro table, for `make_def`).
|
/// just into the compiler's internal macro table, for `make_def`).
|
||||||
pub trait MacResult {
|
pub trait MacResult {
|
||||||
/// Define a new macro.
|
/// Define a new macro.
|
||||||
|
// this should go away; the idea that a macro might expand into
|
||||||
|
// either a macro definition or an expression, depending on what
|
||||||
|
// the context wants, is kind of silly.
|
||||||
fn make_def(&self) -> Option<MacroDef> {
|
fn make_def(&self) -> Option<MacroDef> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
@@ -115,6 +118,12 @@ pub trait MacResult {
|
|||||||
fn make_items(&self) -> Option<SmallVector<Gc<ast::Item>>> {
|
fn make_items(&self) -> Option<SmallVector<Gc<ast::Item>>> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create zero or more methods.
|
||||||
|
fn make_methods(&self) -> Option<SmallVector<Gc<ast::Method>>> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
/// Create a pattern.
|
/// Create a pattern.
|
||||||
fn make_pat(&self) -> Option<Gc<ast::Pat>> {
|
fn make_pat(&self) -> Option<Gc<ast::Pat>> {
|
||||||
None
|
None
|
||||||
@@ -222,6 +231,7 @@ impl DummyResult {
|
|||||||
span: sp,
|
span: sp,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MacResult for DummyResult {
|
impl MacResult for DummyResult {
|
||||||
@@ -232,6 +242,14 @@ impl MacResult for DummyResult {
|
|||||||
Some(DummyResult::raw_pat(self.span))
|
Some(DummyResult::raw_pat(self.span))
|
||||||
}
|
}
|
||||||
fn make_items(&self) -> Option<SmallVector<Gc<ast::Item>>> {
|
fn make_items(&self) -> Option<SmallVector<Gc<ast::Item>>> {
|
||||||
|
// this code needs a comment... why not always just return the Some() ?
|
||||||
|
if self.expr_only {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(SmallVector::zero())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn make_methods(&self) -> Option<SmallVector<Gc<ast::Method>>> {
|
||||||
if self.expr_only {
|
if self.expr_only {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ struct ParserAnyMacro<'a> {
|
|||||||
impl<'a> ParserAnyMacro<'a> {
|
impl<'a> ParserAnyMacro<'a> {
|
||||||
/// Make sure we don't have any tokens left to parse, so we don't
|
/// Make sure we don't have any tokens left to parse, so we don't
|
||||||
/// silently drop anything. `allow_semi` is so that "optional"
|
/// silently drop anything. `allow_semi` is so that "optional"
|
||||||
/// semilons at the end of normal expressions aren't complained
|
/// semicolons at the end of normal expressions aren't complained
|
||||||
/// about e.g. the semicolon in `macro_rules! kapow( () => {
|
/// about e.g. the semicolon in `macro_rules! kapow( () => {
|
||||||
/// fail!(); } )` doesn't get picked up by .parse_expr(), but it's
|
/// fail!(); } )` doesn't get picked up by .parse_expr(), but it's
|
||||||
/// allowed to be there.
|
/// allowed to be there.
|
||||||
@@ -73,6 +73,9 @@ impl<'a> MacResult for ParserAnyMacro<'a> {
|
|||||||
let mut ret = SmallVector::zero();
|
let mut ret = SmallVector::zero();
|
||||||
loop {
|
loop {
|
||||||
let mut parser = self.parser.borrow_mut();
|
let mut parser = self.parser.borrow_mut();
|
||||||
|
// so... do outer attributes attached to the macro invocation
|
||||||
|
// just disappear? This question applies to make_methods, as
|
||||||
|
// well.
|
||||||
match parser.parse_item_with_outer_attributes() {
|
match parser.parse_item_with_outer_attributes() {
|
||||||
Some(item) => ret.push(item),
|
Some(item) => ret.push(item),
|
||||||
None => break
|
None => break
|
||||||
@@ -81,6 +84,20 @@ impl<'a> MacResult for ParserAnyMacro<'a> {
|
|||||||
self.ensure_complete_parse(false);
|
self.ensure_complete_parse(false);
|
||||||
Some(ret)
|
Some(ret)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn make_methods(&self) -> Option<SmallVector<Gc<ast::Method>>> {
|
||||||
|
let mut ret = SmallVector::zero();
|
||||||
|
loop {
|
||||||
|
let mut parser = self.parser.borrow_mut();
|
||||||
|
match parser.token {
|
||||||
|
EOF => break,
|
||||||
|
_ => ret.push(parser.parse_method(None))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.ensure_complete_parse(false);
|
||||||
|
Some(ret)
|
||||||
|
}
|
||||||
|
|
||||||
fn make_stmt(&self) -> Option<Gc<ast::Stmt>> {
|
fn make_stmt(&self) -> Option<Gc<ast::Stmt>> {
|
||||||
let attrs = self.parser.borrow_mut().parse_outer_attributes();
|
let attrs = self.parser.borrow_mut().parse_outer_attributes();
|
||||||
let ret = self.parser.borrow_mut().parse_stmt(attrs);
|
let ret = self.parser.borrow_mut().parse_stmt(attrs);
|
||||||
|
|||||||
Reference in New Issue
Block a user