@@ -469,9 +469,13 @@ fn inner_parse_loop(
469
469
else {
470
470
eof_items. push ( item) ;
471
471
}
472
- } else {
472
+ }
473
+ // We are in the middle of a matcher.
474
+ else {
475
+ // Look at what token in the matcher we are trying to match the current token (`token`)
476
+ // against. Depending on that, we may generate new items.
473
477
match item. top_elts . get_tt ( idx) {
474
- /* need to descend into sequence */
478
+ // Need to descend into a sequence
475
479
TokenTree :: Sequence ( sp, seq) => {
476
480
if seq. op == quoted:: KleeneOp :: ZeroOrMore {
477
481
// Examine the case where there are 0 matches of this sequence
@@ -499,18 +503,30 @@ fn inner_parse_loop(
499
503
top_elts : Tt ( TokenTree :: Sequence ( sp, seq) ) ,
500
504
} ) ) ;
501
505
}
506
+
507
+ // We need to match a metavar (but the identifier is invalid)... this is an error
502
508
TokenTree :: MetaVarDecl ( span, _, id) if id. name == keywords:: Invalid . name ( ) => {
503
509
if sess. missing_fragment_specifiers . borrow_mut ( ) . remove ( & span) {
504
510
return Error ( span, "missing fragment specifier" . to_string ( ) ) ;
505
511
}
506
512
}
513
+
514
+ // We need to match a metavar with a valid ident... call out to the black-box
515
+ // parser by adding an item to `bb_items`.
507
516
TokenTree :: MetaVarDecl ( _, _, id) => {
508
517
// Built-in nonterminals never start with these tokens,
509
518
// so we can eliminate them from consideration.
510
519
if may_begin_with ( & * id. name . as_str ( ) , token) {
511
520
bb_items. push ( item) ;
512
521
}
513
522
}
523
+
524
+ // We need to descend into a delimited submatcher or a doc comment. To do this, we
525
+ // push the current matcher onto a stack and push a new item containing the
526
+ // submatcher onto `cur_items`.
527
+ //
528
+ // At the beginning of the loop, if we reach the end of the delimited submatcher,
529
+ // we pop the stack to backtrack out of the descent.
514
530
seq @ TokenTree :: Delimited ( ..) | seq @ TokenTree :: Token ( _, DocComment ( ..) ) => {
515
531
let lower_elts = mem:: replace ( & mut item. top_elts , Tt ( seq) ) ;
516
532
let idx = item. idx ;
@@ -521,15 +537,23 @@ fn inner_parse_loop(
521
537
item. idx = 0 ;
522
538
cur_items. push ( item) ;
523
539
}
540
+
541
+ // We just matched a normal token. We can just advance the parser.
524
542
TokenTree :: Token ( _, ref t) if token_name_eq ( t, token) => {
525
543
item. idx += 1 ;
526
544
next_items. push ( item) ;
527
545
}
546
+
547
+ // There was another token that was not `token`... This means we can't add any
548
+ // rules. NOTE that this is not necessarily an error unless _all_ items in
549
+ // `cur_items` end up doing this. There may still be some other matchers that do
550
+ // end up working out.
528
551
TokenTree :: Token ( ..) | TokenTree :: MetaVar ( ..) => { }
529
552
}
530
553
}
531
554
}
532
555
556
+ // Yay a successful parse (so far)!
533
557
Success ( ( ) )
534
558
}
535
559
0 commit comments