Skip to content

Commit 568874b

Browse files
Cleanup macro_parser::parse, removing a few clones.
1 parent f3af8c8 commit 568874b

File tree

1 file changed

+35
-51
lines changed

1 file changed

+35
-51
lines changed

src/libsyntax/ext/tt/macro_parser.rs

Lines changed: 35 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -287,15 +287,8 @@ pub fn parse(sess: &ParseSess, rdr: TtReader, ms: &[TokenTree]) -> NamedParseRes
287287
let mut next_eis = Vec::new(); // or proceed normally
288288
let mut eof_eis = Vec::new();
289289

290-
let (sp, tok) = (parser.span, parser.token.clone());
291-
292-
/* we append new items to this while we go */
293-
loop {
294-
let mut ei = match cur_eis.pop() {
295-
None => break, /* for each Earley Item */
296-
Some(ei) => ei,
297-
};
298-
290+
// for each Earley item
291+
while let Some(mut ei) = cur_eis.pop() {
299292
// When unzipped trees end, remove them
300293
while ei.idx >= ei.top_elts.len() {
301294
match ei.stack.pop() {
@@ -317,7 +310,6 @@ pub fn parse(sess: &ParseSess, rdr: TtReader, ms: &[TokenTree]) -> NamedParseRes
317310
// hack: a matcher sequence is repeating iff it has a
318311
// parent (the top level is just a container)
319312

320-
321313
// disregard separator, try to go up
322314
// (remove this condition to make trailing seps ok)
323315
if idx == len {
@@ -334,10 +326,10 @@ pub fn parse(sess: &ParseSess, rdr: TtReader, ms: &[TokenTree]) -> NamedParseRes
334326

335327
// Only touch the binders we have actually bound
336328
for idx in ei.match_lo..ei.match_hi {
337-
let sub = (ei.matches[idx]).clone();
338-
(&mut new_pos.matches[idx])
329+
let sub = ei.matches[idx].clone();
330+
new_pos.matches[idx]
339331
.push(Rc::new(MatchedSeq(sub, mk_sp(ei.sp_lo,
340-
sp.hi))));
332+
parser.span.hi))));
341333
}
342334

343335
new_pos.match_cur = ei.match_hi;
@@ -347,25 +339,21 @@ pub fn parse(sess: &ParseSess, rdr: TtReader, ms: &[TokenTree]) -> NamedParseRes
347339

348340
// can we go around again?
349341

350-
// the *_t vars are workarounds for the lack of unary move
351-
match ei.sep {
352-
Some(ref t) if idx == len => { // we need a separator
353-
// i'm conflicted about whether this should be hygienic....
354-
// though in this case, if the separators are never legal
355-
// idents, it shouldn't matter.
356-
if token_name_eq(&tok, t) { //pass the separator
357-
let mut ei_t = ei.clone();
358-
// ei_t.match_cur = ei_t.match_lo;
359-
ei_t.idx += 1;
360-
next_eis.push(ei_t);
361-
}
362-
}
363-
_ => { // we don't need a separator
364-
let mut ei_t = ei;
365-
ei_t.match_cur = ei_t.match_lo;
366-
ei_t.idx = 0;
367-
cur_eis.push(ei_t);
342+
// Check if we need a separator
343+
if idx == len && ei.sep.is_some() {
344+
if ei.sep.as_ref().map(|ref sep| token_name_eq(&parser.token, sep))
345+
.unwrap_or(false) {
346+
// i'm conflicted about whether this should be hygienic.... though in
347+
// this case, if the separators are never legal idents, it shouldn't
348+
// matter.
349+
// ei.match_cur = ei.match_lo;
350+
ei.idx += 1;
351+
next_eis.push(ei);
368352
}
353+
} else { // we don't need a separator
354+
ei.match_cur = ei.match_lo;
355+
ei.idx = 0;
356+
cur_eis.push(ei);
369357
}
370358
} else {
371359
eof_eis.push(ei);
@@ -380,32 +368,31 @@ pub fn parse(sess: &ParseSess, rdr: TtReader, ms: &[TokenTree]) -> NamedParseRes
380368
new_ei.idx += 1;
381369
//we specifically matched zero repeats.
382370
for idx in ei.match_cur..ei.match_cur + seq.num_captures {
383-
(&mut new_ei.matches[idx]).push(Rc::new(MatchedSeq(vec![], sp)));
371+
new_ei.matches[idx].push(Rc::new(MatchedSeq(vec![], sp)));
384372
}
385373

386374
cur_eis.push(new_ei);
387375
}
388376

389377
let matches: Vec<_> = (0..ei.matches.len())
390378
.map(|_| Vec::new()).collect();
391-
let ei_t = ei;
392379
cur_eis.push(Box::new(MatcherPos {
393380
stack: vec![],
394381
sep: seq.separator.clone(),
395382
idx: 0,
396383
matches: matches,
397-
match_lo: ei_t.match_cur,
398-
match_cur: ei_t.match_cur,
399-
match_hi: ei_t.match_cur + seq.num_captures,
400-
up: Some(ei_t),
384+
match_lo: ei.match_cur,
385+
match_cur: ei.match_cur,
386+
match_hi: ei.match_cur + seq.num_captures,
387+
up: Some(ei),
401388
sp_lo: sp.lo,
402389
top_elts: Tt(TokenTree::Sequence(sp, seq)),
403390
}));
404391
}
405392
TokenTree::Token(_, MatchNt(..)) => {
406393
// Built-in nonterminals never start with these tokens,
407394
// so we can eliminate them from consideration.
408-
match tok {
395+
match parser.token {
409396
token::CloseDelim(_) => {},
410397
_ => bb_eis.push(ei),
411398
}
@@ -424,28 +411,25 @@ pub fn parse(sess: &ParseSess, rdr: TtReader, ms: &[TokenTree]) -> NamedParseRes
424411
cur_eis.push(ei);
425412
}
426413
TokenTree::Token(_, ref t) => {
427-
if token_name_eq(t,&tok) {
428-
let mut ei_t = ei.clone();
429-
ei_t.idx += 1;
430-
next_eis.push(ei_t);
414+
if token_name_eq(t, &parser.token) {
415+
ei.idx += 1;
416+
next_eis.push(ei);
431417
}
432418
}
433419
}
434420
}
435421
}
436422

437423
/* error messages here could be improved with links to orig. rules */
438-
if token_name_eq(&tok, &token::Eof) {
424+
if token_name_eq(&parser.token, &token::Eof) {
439425
if eof_eis.len() == 1 {
440-
let mut v = Vec::new();
441-
for dv in &mut (&mut eof_eis[0]).matches {
442-
v.push(dv.pop().unwrap());
443-
}
426+
let v = eof_eis[0].matches.iter_mut()
427+
.map(|dv| dv.pop().unwrap()).collect::<Vec<_>>();
444428
return nameize(sess, ms, &v[..]);
445429
} else if eof_eis.len() > 1 {
446-
return Error(sp, "ambiguity: multiple successful parses".to_string());
430+
return Error(parser.span, "ambiguity: multiple successful parses".to_string());
447431
} else {
448-
return Failure(sp, token::Eof);
432+
return Failure(parser.span, token::Eof);
449433
}
450434
} else {
451435
if (!bb_eis.is_empty() && !next_eis.is_empty())
@@ -457,7 +441,7 @@ pub fn parse(sess: &ParseSess, rdr: TtReader, ms: &[TokenTree]) -> NamedParseRes
457441
_ => panic!()
458442
}).collect::<Vec<String>>().join(" or ");
459443

460-
return Error(sp, format!(
444+
return Error(parser.span, format!(
461445
"local ambiguity: multiple parsing options: {}",
462446
match next_eis.len() {
463447
0 => format!("built-in NTs {}.", nts),
@@ -466,7 +450,7 @@ pub fn parse(sess: &ParseSess, rdr: TtReader, ms: &[TokenTree]) -> NamedParseRes
466450
}
467451
))
468452
} else if bb_eis.is_empty() && next_eis.is_empty() {
469-
return Failure(sp, tok);
453+
return Failure(parser.span, parser.token);
470454
} else if !next_eis.is_empty() {
471455
/* Now process the next token */
472456
while !next_eis.is_empty() {

0 commit comments

Comments
 (0)