@@ -287,15 +287,8 @@ pub fn parse(sess: &ParseSess, rdr: TtReader, ms: &[TokenTree]) -> NamedParseRes
287
287
let mut next_eis = Vec :: new ( ) ; // or proceed normally
288
288
let mut eof_eis = Vec :: new ( ) ;
289
289
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 ( ) {
299
292
// When unzipped trees end, remove them
300
293
while ei. idx >= ei. top_elts . len ( ) {
301
294
match ei. stack . pop ( ) {
@@ -317,7 +310,6 @@ pub fn parse(sess: &ParseSess, rdr: TtReader, ms: &[TokenTree]) -> NamedParseRes
317
310
// hack: a matcher sequence is repeating iff it has a
318
311
// parent (the top level is just a container)
319
312
320
-
321
313
// disregard separator, try to go up
322
314
// (remove this condition to make trailing seps ok)
323
315
if idx == len {
@@ -334,10 +326,10 @@ pub fn parse(sess: &ParseSess, rdr: TtReader, ms: &[TokenTree]) -> NamedParseRes
334
326
335
327
// Only touch the binders we have actually bound
336
328
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]
339
331
. push ( Rc :: new ( MatchedSeq ( sub, mk_sp ( ei. sp_lo ,
340
- sp . hi ) ) ) ) ;
332
+ parser . span . hi ) ) ) ) ;
341
333
}
342
334
343
335
new_pos. match_cur = ei. match_hi ;
@@ -347,25 +339,21 @@ pub fn parse(sess: &ParseSess, rdr: TtReader, ms: &[TokenTree]) -> NamedParseRes
347
339
348
340
// can we go around again?
349
341
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) ;
368
352
}
353
+ } else { // we don't need a separator
354
+ ei. match_cur = ei. match_lo ;
355
+ ei. idx = 0 ;
356
+ cur_eis. push ( ei) ;
369
357
}
370
358
} else {
371
359
eof_eis. push ( ei) ;
@@ -380,32 +368,31 @@ pub fn parse(sess: &ParseSess, rdr: TtReader, ms: &[TokenTree]) -> NamedParseRes
380
368
new_ei. idx += 1 ;
381
369
//we specifically matched zero repeats.
382
370
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) ) ) ;
384
372
}
385
373
386
374
cur_eis. push ( new_ei) ;
387
375
}
388
376
389
377
let matches: Vec < _ > = ( 0 ..ei. matches . len ( ) )
390
378
. map ( |_| Vec :: new ( ) ) . collect ( ) ;
391
- let ei_t = ei;
392
379
cur_eis. push ( Box :: new ( MatcherPos {
393
380
stack : vec ! [ ] ,
394
381
sep : seq. separator . clone ( ) ,
395
382
idx : 0 ,
396
383
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 ) ,
401
388
sp_lo : sp. lo ,
402
389
top_elts : Tt ( TokenTree :: Sequence ( sp, seq) ) ,
403
390
} ) ) ;
404
391
}
405
392
TokenTree :: Token ( _, MatchNt ( ..) ) => {
406
393
// Built-in nonterminals never start with these tokens,
407
394
// so we can eliminate them from consideration.
408
- match tok {
395
+ match parser . token {
409
396
token:: CloseDelim ( _) => { } ,
410
397
_ => bb_eis. push ( ei) ,
411
398
}
@@ -424,28 +411,25 @@ pub fn parse(sess: &ParseSess, rdr: TtReader, ms: &[TokenTree]) -> NamedParseRes
424
411
cur_eis. push ( ei) ;
425
412
}
426
413
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) ;
431
417
}
432
418
}
433
419
}
434
420
}
435
421
}
436
422
437
423
/* 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 ) {
439
425
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 < _ > > ( ) ;
444
428
return nameize ( sess, ms, & v[ ..] ) ;
445
429
} 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 ( ) ) ;
447
431
} else {
448
- return Failure ( sp , token:: Eof ) ;
432
+ return Failure ( parser . span , token:: Eof ) ;
449
433
}
450
434
} else {
451
435
if ( !bb_eis. is_empty ( ) && !next_eis. is_empty ( ) )
@@ -457,7 +441,7 @@ pub fn parse(sess: &ParseSess, rdr: TtReader, ms: &[TokenTree]) -> NamedParseRes
457
441
_ => panic ! ( )
458
442
} ) . collect :: < Vec < String > > ( ) . join ( " or " ) ;
459
443
460
- return Error ( sp , format ! (
444
+ return Error ( parser . span , format ! (
461
445
"local ambiguity: multiple parsing options: {}" ,
462
446
match next_eis. len( ) {
463
447
0 => format!( "built-in NTs {}." , nts) ,
@@ -466,7 +450,7 @@ pub fn parse(sess: &ParseSess, rdr: TtReader, ms: &[TokenTree]) -> NamedParseRes
466
450
}
467
451
) )
468
452
} else if bb_eis. is_empty ( ) && next_eis. is_empty ( ) {
469
- return Failure ( sp , tok ) ;
453
+ return Failure ( parser . span , parser . token ) ;
470
454
} else if !next_eis. is_empty ( ) {
471
455
/* Now process the next token */
472
456
while !next_eis. is_empty ( ) {
0 commit comments