Skip to content

Commit 2c4b6d6

Browse files
committed
add make_method method to MacResult trait
this allows macro results to be parsed as methods
1 parent 6c8bb5a commit 2c4b6d6

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/libsyntax/ext/base.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ pub type IdentMacroExpanderFn =
104104
/// just into the compiler's internal macro table, for `make_def`).
105105
pub trait MacResult {
106106
/// Define a new macro.
107+
// this should go away; the idea that a macro might expand into
108+
// either a macro definition or an expression, depending on what
109+
// the context wants, is kind of silly.
107110
fn make_def(&self) -> Option<MacroDef> {
108111
None
109112
}
@@ -115,6 +118,12 @@ pub trait MacResult {
115118
fn make_items(&self) -> Option<SmallVector<Gc<ast::Item>>> {
116119
None
117120
}
121+
122+
/// Create zero or more methods.
123+
fn make_methods(&self) -> Option<SmallVector<Gc<ast::Method>>> {
124+
None
125+
}
126+
118127
/// Create a pattern.
119128
fn make_pat(&self) -> Option<Gc<ast::Pat>> {
120129
None
@@ -222,6 +231,7 @@ impl DummyResult {
222231
span: sp,
223232
}
224233
}
234+
225235
}
226236

227237
impl MacResult for DummyResult {
@@ -232,6 +242,14 @@ impl MacResult for DummyResult {
232242
Some(DummyResult::raw_pat(self.span))
233243
}
234244
fn make_items(&self) -> Option<SmallVector<Gc<ast::Item>>> {
245+
// this code needs a comment... why not always just return the Some() ?
246+
if self.expr_only {
247+
None
248+
} else {
249+
Some(SmallVector::zero())
250+
}
251+
}
252+
fn make_methods(&self) -> Option<SmallVector<Gc<ast::Method>>> {
235253
if self.expr_only {
236254
None
237255
} else {

src/libsyntax/ext/tt/macro_rules.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct ParserAnyMacro<'a> {
3838
impl<'a> ParserAnyMacro<'a> {
3939
/// Make sure we don't have any tokens left to parse, so we don't
4040
/// silently drop anything. `allow_semi` is so that "optional"
41-
/// semilons at the end of normal expressions aren't complained
41+
/// semicolons at the end of normal expressions aren't complained
4242
/// about e.g. the semicolon in `macro_rules! kapow( () => {
4343
/// fail!(); } )` doesn't get picked up by .parse_expr(), but it's
4444
/// allowed to be there.
@@ -73,6 +73,9 @@ impl<'a> MacResult for ParserAnyMacro<'a> {
7373
let mut ret = SmallVector::zero();
7474
loop {
7575
let mut parser = self.parser.borrow_mut();
76+
// so... do outer attributes attached to the macro invocation
77+
// just disappear? This question applies to make_methods, as
78+
// well.
7679
match parser.parse_item_with_outer_attributes() {
7780
Some(item) => ret.push(item),
7881
None => break
@@ -81,6 +84,20 @@ impl<'a> MacResult for ParserAnyMacro<'a> {
8184
self.ensure_complete_parse(false);
8285
Some(ret)
8386
}
87+
88+
fn make_methods(&self) -> Option<SmallVector<Gc<ast::Method>>> {
89+
let mut ret = SmallVector::zero();
90+
loop {
91+
let mut parser = self.parser.borrow_mut();
92+
match parser.token {
93+
EOF => break,
94+
_ => ret.push(parser.parse_method(None))
95+
}
96+
}
97+
self.ensure_complete_parse(false);
98+
Some(ret)
99+
}
100+
84101
fn make_stmt(&self) -> Option<Gc<ast::Stmt>> {
85102
let attrs = self.parser.borrow_mut().parse_outer_attributes();
86103
let ret = self.parser.borrow_mut().parse_stmt(attrs);

0 commit comments

Comments
 (0)