Skip to content

Commit 7524130

Browse files
committed
Merge repeat_idx and repeat_len.
1 parent 0cc7053 commit 7524130

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

src/libsyntax/ext/tt/transcribe.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ pub fn transcribe(sp_diag: &Handler,
6969
-> Vec<TokenTree> {
7070
let mut stack = SmallVector::one(Frame::new(src));
7171
let interpolations = interp.unwrap_or_else(HashMap::new); /* just a convenience */
72-
let mut repeat_idx = Vec::new();
73-
let mut repeat_len = Vec::new();
72+
let mut repeats = Vec::new();
7473
let mut result = Vec::new();
7574
let mut result_stack = Vec::new();
7675

@@ -79,8 +78,9 @@ pub fn transcribe(sp_diag: &Handler,
7978
tree
8079
} else {
8180
if let Frame::Sequence { ref mut idx, ref sep, .. } = *stack.last_mut().unwrap() {
82-
if *repeat_idx.last().unwrap() < *repeat_len.last().unwrap() - 1 {
83-
*repeat_idx.last_mut().unwrap() += 1;
81+
let (ref mut repeat_idx, repeat_len) = *repeats.last_mut().unwrap();
82+
*repeat_idx += 1;
83+
if *repeat_idx < repeat_len {
8484
*idx = 0;
8585
if let Some(sep) = sep.clone() {
8686
// repeat same span, I guess
@@ -93,8 +93,7 @@ pub fn transcribe(sp_diag: &Handler,
9393

9494
match stack.pop().unwrap() {
9595
Frame::Sequence { .. } => {
96-
repeat_idx.pop();
97-
repeat_len.pop();
96+
repeats.pop();
9897
}
9998
Frame::Delimited { forest, span, .. } => {
10099
if result_stack.is_empty() {
@@ -116,7 +115,7 @@ pub fn transcribe(sp_diag: &Handler,
116115
// FIXME(pcwalton): Bad copy.
117116
match lockstep_iter_size(&quoted::TokenTree::Sequence(sp, seq.clone()),
118117
&interpolations,
119-
&repeat_idx) {
118+
&repeats) {
120119
LockstepIterSize::Unconstrained => {
121120
panic!(sp_diag.span_fatal(
122121
sp.clone(), /* blame macro writer */
@@ -136,8 +135,7 @@ pub fn transcribe(sp_diag: &Handler,
136135
"this must repeat at least once"));
137136
}
138137
} else {
139-
repeat_len.push(len);
140-
repeat_idx.push(0);
138+
repeats.push((0, len));
141139
stack.push(Frame::Sequence {
142140
idx: 0,
143141
sep: seq.separator.clone(),
@@ -149,7 +147,7 @@ pub fn transcribe(sp_diag: &Handler,
149147
}
150148
// FIXME #2887: think about span stuff here
151149
quoted::TokenTree::Token(sp, SubstNt(ident)) => {
152-
match lookup_cur_matched(ident, &interpolations, &repeat_idx) {
150+
match lookup_cur_matched(ident, &interpolations, &repeats) {
153151
None => result.push(TokenTree::Token(sp, SubstNt(ident))),
154152
Some(cur_matched) => if let MatchedNonterminal(ref nt) = *cur_matched {
155153
match **nt {
@@ -184,16 +182,16 @@ pub fn transcribe(sp_diag: &Handler,
184182

185183
fn lookup_cur_matched(ident: Ident,
186184
interpolations: &HashMap<Ident, Rc<NamedMatch>>,
187-
repeat_idx: &[usize])
185+
repeats: &[(usize, usize)])
188186
-> Option<Rc<NamedMatch>> {
189187
interpolations.get(&ident).map(|matched| {
190-
repeat_idx.iter().fold(matched.clone(), |ad, idx| {
188+
repeats.iter().fold(matched.clone(), |ad, &(idx, _)| {
191189
match *ad {
192190
MatchedNonterminal(_) => {
193191
// end of the line; duplicate henceforth
194192
ad.clone()
195193
}
196-
MatchedSeq(ref ads, _) => ads[*idx].clone()
194+
MatchedSeq(ref ads, _) => ads[idx].clone()
197195
}
198196
})
199197
})
@@ -230,22 +228,22 @@ impl Add for LockstepIterSize {
230228

231229
fn lockstep_iter_size(tree: &quoted::TokenTree,
232230
interpolations: &HashMap<Ident, Rc<NamedMatch>>,
233-
repeat_idx: &[usize])
231+
repeats: &[(usize, usize)])
234232
-> LockstepIterSize {
235233
use self::quoted::TokenTree;
236234
match *tree {
237235
TokenTree::Delimited(_, ref delimed) => {
238236
delimed.tts.iter().fold(LockstepIterSize::Unconstrained, |size, tt| {
239-
size + lockstep_iter_size(tt, interpolations, repeat_idx)
237+
size + lockstep_iter_size(tt, interpolations, repeats)
240238
})
241239
},
242240
TokenTree::Sequence(_, ref seq) => {
243241
seq.tts.iter().fold(LockstepIterSize::Unconstrained, |size, tt| {
244-
size + lockstep_iter_size(tt, interpolations, repeat_idx)
242+
size + lockstep_iter_size(tt, interpolations, repeats)
245243
})
246244
},
247245
TokenTree::Token(_, SubstNt(name)) | TokenTree::MetaVarDecl(_, name, _) =>
248-
match lookup_cur_matched(name, interpolations, repeat_idx) {
246+
match lookup_cur_matched(name, interpolations, repeats) {
249247
Some(matched) => match *matched {
250248
MatchedNonterminal(_) => LockstepIterSize::Unconstrained,
251249
MatchedSeq(ref ads, _) => LockstepIterSize::Constraint(ads.len(), name),

0 commit comments

Comments
 (0)