@@ -469,9 +469,13 @@ fn inner_parse_loop(
469469 else {
470470 eof_items. push ( item) ;
471471 }
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.
473477 match item. top_elts . get_tt ( idx) {
474- /* need to descend into sequence */
478+ // Need to descend into a sequence
475479 TokenTree :: Sequence ( sp, seq) => {
476480 if seq. op == quoted:: KleeneOp :: ZeroOrMore {
477481 // Examine the case where there are 0 matches of this sequence
@@ -499,18 +503,30 @@ fn inner_parse_loop(
499503 top_elts : Tt ( TokenTree :: Sequence ( sp, seq) ) ,
500504 } ) ) ;
501505 }
506+
507+ // We need to match a metavar (but the identifier is invalid)... this is an error
502508 TokenTree :: MetaVarDecl ( span, _, id) if id. name == keywords:: Invalid . name ( ) => {
503509 if sess. missing_fragment_specifiers . borrow_mut ( ) . remove ( & span) {
504510 return Error ( span, "missing fragment specifier" . to_string ( ) ) ;
505511 }
506512 }
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`.
507516 TokenTree :: MetaVarDecl ( _, _, id) => {
508517 // Built-in nonterminals never start with these tokens,
509518 // so we can eliminate them from consideration.
510519 if may_begin_with ( & * id. name . as_str ( ) , token) {
511520 bb_items. push ( item) ;
512521 }
513522 }
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.
514530 seq @ TokenTree :: Delimited ( ..) | seq @ TokenTree :: Token ( _, DocComment ( ..) ) => {
515531 let lower_elts = mem:: replace ( & mut item. top_elts , Tt ( seq) ) ;
516532 let idx = item. idx ;
@@ -521,15 +537,23 @@ fn inner_parse_loop(
521537 item. idx = 0 ;
522538 cur_items. push ( item) ;
523539 }
540+
541+ // We just matched a normal token. We can just advance the parser.
524542 TokenTree :: Token ( _, ref t) if token_name_eq ( t, token) => {
525543 item. idx += 1 ;
526544 next_items. push ( item) ;
527545 }
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.
528551 TokenTree :: Token ( ..) | TokenTree :: MetaVar ( ..) => { }
529552 }
530553 }
531554 }
532555
556+ // Yay a successful parse (so far)!
533557 Success ( ( ) )
534558}
535559
0 commit comments