Skip to content

Commit fdadba5

Browse files
committed
Store TokenTree in MacroRulesMacroExpander
1 parent 8225135 commit fdadba5

File tree

1 file changed

+23
-28
lines changed

1 file changed

+23
-28
lines changed

src/libsyntax/ext/tt/macro_rules.rs

+23-28
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use codemap::{Span, DUMMY_SP};
1313
use ext::base::{ExtCtxt, MacResult, SyntaxExtension};
1414
use ext::base::{NormalTT, TTMacroExpander};
1515
use ext::tt::macro_parser::{Success, Error, Failure};
16-
use ext::tt::macro_parser::{NamedMatch, MatchedSeq, MatchedNonterminal};
16+
use ext::tt::macro_parser::{MatchedSeq, MatchedNonterminal};
1717
use ext::tt::macro_parser::parse;
1818
use parse::lexer::new_tt_reader;
1919
use parse::parser::Parser;
@@ -129,15 +129,15 @@ impl<'a> MacResult for ParserAnyMacro<'a> {
129129
struct MacroRulesMacroExpander {
130130
name: ast::Ident,
131131
imported_from: Option<ast::Ident>,
132-
lhses: Vec<Rc<NamedMatch>>,
133-
rhses: Vec<Rc<NamedMatch>>,
132+
lhses: Vec<TokenTree>,
133+
rhses: Vec<TokenTree>,
134134
}
135135

136136
impl TTMacroExpander for MacroRulesMacroExpander {
137137
fn expand<'cx>(&self,
138138
cx: &'cx mut ExtCtxt,
139139
sp: Span,
140-
arg: &[ast::TokenTree])
140+
arg: &[TokenTree])
141141
-> Box<MacResult+'cx> {
142142
generic_extension(cx,
143143
sp,
@@ -154,9 +154,9 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
154154
sp: Span,
155155
name: ast::Ident,
156156
imported_from: Option<ast::Ident>,
157-
arg: &[ast::TokenTree],
158-
lhses: &[Rc<NamedMatch>],
159-
rhses: &[Rc<NamedMatch>])
157+
arg: &[TokenTree],
158+
lhses: &[TokenTree],
159+
rhses: &[TokenTree])
160160
-> Box<MacResult+'cx> {
161161
if cx.trace_macros() {
162162
println!("{}! {{ {} }}",
@@ -169,25 +169,17 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
169169
let mut best_fail_msg = "internal error: ran no matchers".to_string();
170170

171171
for (i, lhs) in lhses.iter().enumerate() { // try each arm's matchers
172-
match **lhs {
173-
MatchedNonterminal(NtTT(ref lhs_tt)) => {
174-
let lhs_tt = match **lhs_tt {
172+
let lhs_tt = match *lhs {
175173
TokenTree::Delimited(_, ref delim) => &delim.tts[..],
176174
_ => panic!(cx.span_fatal(sp, "malformed macro lhs"))
177175
};
178176

179177
match TokenTree::parse(cx, lhs_tt, arg) {
180178
Success(named_matches) => {
181-
let rhs = match *rhses[i] {
182-
// okay, what's your transcriber?
183-
MatchedNonterminal(NtTT(ref tt)) => {
184-
match **tt {
179+
let rhs = match rhses[i] {
185180
// ignore delimiters
186181
TokenTree::Delimited(_, ref delimed) => delimed.tts.clone(),
187182
_ => panic!(cx.span_fatal(sp, "macro rhs must be delimited")),
188-
}
189-
},
190-
_ => cx.span_bug(sp, "bad thing in rhs")
191183
};
192184
// rhs has holes ( `$id` and `$(...)` that need filled)
193185
let trncbr = new_tt_reader(&cx.parse_sess().span_diagnostic,
@@ -216,9 +208,6 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
216208
panic!(cx.span_fatal(err_sp.substitute_dummy(sp), &msg[..]))
217209
}
218210
}
219-
}
220-
_ => cx.bug("non-matcher found in parsed lhses")
221-
}
222211
}
223212

224213
panic!(cx.span_fatal(best_fail_spot.substitute_dummy(sp), &best_fail_msg[..]));
@@ -284,7 +273,12 @@ pub fn compile<'cx>(cx: &'cx mut ExtCtxt,
284273

285274
// Extract the arguments:
286275
let lhses = match **argument_map.get(&lhs_nm.name).unwrap() {
287-
MatchedSeq(ref s, _) => /* FIXME (#2543) */ (*s).clone(),
276+
MatchedSeq(ref s, _) => {
277+
s.iter().map(|m| match **m {
278+
MatchedNonterminal(NtTT(ref tt)) => (**tt).clone(),
279+
_ => cx.span_bug(def.span, "wrong-structured lhs")
280+
}).collect()
281+
}
288282
_ => cx.span_bug(def.span, "wrong-structured lhs")
289283
};
290284

@@ -293,7 +287,12 @@ pub fn compile<'cx>(cx: &'cx mut ExtCtxt,
293287
}
294288

295289
let rhses = match **argument_map.get(&rhs_nm.name).unwrap() {
296-
MatchedSeq(ref s, _) => /* FIXME (#2543) */ (*s).clone(),
290+
MatchedSeq(ref s, _) => {
291+
s.iter().map(|m| match **m {
292+
MatchedNonterminal(NtTT(ref tt)) => (**tt).clone(),
293+
_ => cx.span_bug(def.span, "wrong-structured rhs")
294+
}).collect()
295+
}
297296
_ => cx.span_bug(def.span, "wrong-structured rhs")
298297
};
299298

@@ -307,11 +306,10 @@ pub fn compile<'cx>(cx: &'cx mut ExtCtxt,
307306
NormalTT(exp, Some(def.span), def.allow_internal_unstable)
308307
}
309308

310-
fn check_lhs_nt_follows(cx: &mut ExtCtxt, lhs: &NamedMatch, sp: Span) {
311-
// lhs is going to be like MatchedNonterminal(NtTT(TokenTree::Delimited(...))), where the
309+
fn check_lhs_nt_follows(cx: &mut ExtCtxt, lhs: &TokenTree, sp: Span) {
310+
// lhs is going to be like TokenTree::Delimited(...), where the
312311
// entire lhs is those tts. Or, it can be a "bare sequence", not wrapped in parens.
313312
match lhs {
314-
&MatchedNonterminal(NtTT(ref inner)) => match &**inner {
315313
&TokenTree::Delimited(_, ref tts) => {
316314
check_matcher(cx, tts.tts.iter(), &Eof);
317315
},
@@ -320,9 +318,6 @@ fn check_lhs_nt_follows(cx: &mut ExtCtxt, lhs: &NamedMatch, sp: Span) {
320318
},
321319
_ => cx.span_err(sp, "Invalid macro matcher; matchers must be contained \
322320
in balanced delimiters or a repetition indicator")
323-
},
324-
_ => cx.span_bug(sp, "wrong-structured lhs for follow check (didn't find a \
325-
MatchedNonterminal)")
326321
};
327322
// we don't abort on errors on rejection, the driver will do that for us
328323
// after parsing/expansion. we can report every error in every macro this way.

0 commit comments

Comments
 (0)