pub fn collect_tokens<R>(
        &mut self,
        f: impl FnOnce(&mut Self) -> PResult<'a, R>,
    ) -> PResult<'a, (R, TokenStream)> {
        // Record all tokens we parse when parsing this item.
        let tokens: Vec<TreeAndJoint> = self.token_cursor.cur_token.clone().into_iter().collect();
        debug!("collect_tokens: starting with {:?}", tokens);

        // We need special handling for the case where `collect_tokens` is called
        // on an opening delimeter (e.g. '('). At this point, we have already pushed
        // a new frame - however, we want to record the original `TokenTree::Delimited`,
        // for consistency with the case where we start recording one token earlier.
        // See `TokenCursor::next` to see how `cur_token` is set up.
        let prev_depth =
            if matches!(self.token_cursor.cur_token, Some((TokenTree::Delimited(..), _))) {
                if self.token_cursor.stack.is_empty() {
                    // There is nothing below us in the stack that
                    // the function could consume, so the only thing it can legally
                    // capture is the entire contents of the current frame.
                    return Ok((f(self)?, TokenStream::new(tokens)));
                }
                // We have already recorded the full `TokenTree::Delimited` when we created
                // our `tokens` vector at the start of this function. We are now inside
                // a new frame corresponding to the `TokenTree::Delimited` we already recoreded.
                // We don't want to record any of the tokens inside this frame, since they
                // will be duplicates of the tokens nested inside the `TokenTree::Delimited`.
                // Therefore, we set our recording depth to the *previous* frame. This allows
                // us to record a sequence like: `(foo).bar()`: the `(foo)` will be recored
                // as our initial `cur_token`, while the `.bar()` will be recored after we
                // pop the `(foo)` frame.
                self.token_cursor.stack.len() - 1
            } else {
                self.token_cursor.stack.len()
            };
        let prev_collecting =
            self.token_cursor.collecting.replace(Collecting { buf: tokens, depth: prev_depth });

        let ret = f(self);

        let mut collected_tokens = if let Some(collecting) = self.token_cursor.collecting.take() {
            collecting.buf
        } else {
            let msg = "our vector went away?";
            debug!("collect_tokens: {}", msg);
            self.sess.span_diagnostic.delay_span_bug(self.token.span, &msg);
            // This can happen due to a bad interaction of two unrelated recovery mechanisms
            // with mismatched delimiters *and* recovery lookahead on the likely typo
            // `pub ident(` (#62895, different but similar to the case above).
            return Ok((ret?, TokenStream::default()));
        };

        debug!("collect_tokens: got raw tokens {:?}", collected_tokens);

        // If we're not at EOF our current token wasn't actually consumed by
        // `f`, but it'll still be in our list that we pulled out. In that case
        // put it back.
        let extra_token = if self.token != token::Eof { collected_tokens.pop() } else { None };

        if let Some(mut collecting) = prev_collecting {
            // If we were previously collecting at the same depth,
            // then the previous call to `collect_tokens` needs to see
            // the tokens we just recorded.
            //
            // If we were previously recording at an lower `depth`,
            // then the previous `collect_tokens` call already recorded
            // this entire frame in the form of a `TokenTree::Delimited`,
            // so there is nothing else for us to do.
            if collecting.depth == prev_depth {
                collecting.buf.extend(collected_tokens.iter().cloned());
                collecting.buf.extend(extra_token);
                debug!("collect_tokens: updating previous buf to {:?}", collecting);
            }
            self.token_cursor.collecting = Some(collecting)
        }

        Ok((ret?, TokenStream::new(collected_tokens)))
    }
/// Records all tokens consumed by the provided callback,
/// including the current token. These tokens are collected
/// into a `TokenStream`, and returned along with the result
/// of the callback.
///
/// Note: If your callback consumes an opening delimiter
/// (including the case where you call `collect_tokens`
/// when the current token is an opening delimeter),
/// you must also consume the corresponding closing delimiter.
///
/// That is, you can consume
/// `something ([{ }])` or `([{}])`, but not `([{}]`
///
/// This restriction shouldn't be an issue in practice,
/// since this function is used to record the tokens for
/// a parsed AST item, which always has matching delimiters.
Records all tokens consumed by the provided callback,
including the current token. These tokens are collected
into a `TokenStream`, and returned along with the result
of the callback.

If your callback consumes an opening delimiter
(including the case where you call `collect_tokens`
when the current token is an opening delimeter),
you must also consume the corresponding closing delimiter.



This restriction shouldn't be an issue in practice,
since this function is used to record the tokens for
a parsed AST item, which always has matching delimiters.
Records all tokens consumed by the provided callback including the current token . These tokens are collected into a ` TokenStream ` and returned along with the result of the callback . If your callback consumes an opening delimiter ( including the case where you call ` collect_tokens ` when the current token is an opening delimeter ) you must also consume the corresponding closing delimiter . This restriction shouldn ' t be an issue in practice since this function is used to record the tokens for a parsed AST item which always has matching delimiters .
pub fn collect_tokens<R>(
        &mut self,
        f: impl FnOnce(&mut Self) -> PResult<'a, R>,
    ) -> PResult<'a, (R, TokenStream)> {
        // Record all tokens we parse when parsing this item.
        let tokens: Vec<TreeAndJoint> = self.token_cursor.cur_token.clone().into_iter().collect();
        debug!("collect_tokens: starting with {:?}", tokens);
        // We need special handling for the case where `collect_tokens` is called
        // on an opening delimeter (e.g. '('). At this point, we have already pushed
        // a new frame - however, we want to record the original `TokenTree::Delimited`,
        // for consistency with the case where we start recording one token earlier.
        // See `TokenCursor::next` to see how `cur_token` is set up.
        let prev_depth =
            if matches!(self.token_cursor.cur_token, Some((TokenTree::Delimited(..), _))) {
                if self.token_cursor.stack.is_empty() {
                    // There is nothing below us in the stack that
                    // the function could consume, so the only thing it can legally
                    // capture is the entire contents of the current frame.
                    return Ok((f(self)?, TokenStream::new(tokens)));
                }
                // We have already recorded the full `TokenTree::Delimited` when we created
                // our `tokens` vector at the start of this function. We are now inside
                // a new frame corresponding to the `TokenTree::Delimited` we already recoreded.
                // We don't want to record any of the tokens inside this frame, since they
                // will be duplicates of the tokens nested inside the `TokenTree::Delimited`.
                // Therefore, we set our recording depth to the *previous* frame. This allows
                // us to record a sequence like: `(foo).bar()`: the `(foo)` will be recored
                // as our initial `cur_token`, while the `.bar()` will be recored after we
                // pop the `(foo)` frame.
                self.token_cursor.stack.len() - 1
            } else {
                self.token_cursor.stack.len()
            };
        let prev_collecting =
            self.token_cursor.collecting.replace(Collecting { buf: tokens, depth: prev_depth });
        let ret = f(self);
        let mut collected_tokens = if let Some(collecting) = self.token_cursor.collecting.take() {
            collecting.buf
        } else {
            let msg = "our vector went away?";
            debug!("collect_tokens: {}", msg);
            self.sess.span_diagnostic.delay_span_bug(self.token.span, &msg);
            // This can happen due to a bad interaction of two unrelated recovery mechanisms
            // with mismatched delimiters *and* recovery lookahead on the likely typo
            // `pub ident(` (#62895, different but similar to the case above).
            return Ok((ret?, TokenStream::default()));
        };
        debug!("collect_tokens: got raw tokens {:?}", collected_tokens);
        // If we're not at EOF our current token wasn't actually consumed by
        // `f`, but it'll still be in our list that we pulled out. In that case
        // put it back.
        let extra_token = if self.token != token::Eof { collected_tokens.pop() } else { None };
        if let Some(mut collecting) = prev_collecting {
            // If we were previously collecting at the same depth,
            // then the previous call to `collect_tokens` needs to see
            // the tokens we just recorded.
            //
            // If we were previously recording at an lower `depth`,
            // then the previous `collect_tokens` call already recorded
            // this entire frame in the form of a `TokenTree::Delimited`,
            // so there is nothing else for us to do.
            if collecting.depth == prev_depth {
                collecting.buf.extend(collected_tokens.iter().cloned());
                collecting.buf.extend(extra_token);
                debug!("collect_tokens: updating previous buf to {:?}", collecting);
            }
            self.token_cursor.collecting = Some(collecting)
        }
        Ok((ret?, TokenStream::new(collected_tokens)))
    }
pub fn collect_tokens < R > ( & mut self , f : impl FnOnce ( & mut Self ) -> PResult < ' a , R > , ) -> PResult < ' a , ( R , TokenStream ) > { // Record all tokens we parse when parsing this item. let tokens : Vec < TreeAndJoint > = self . token_cursor . cur_token . clone ( ) . into_iter ( ) . collect ( ) ; debug ! ( " " tokens ) ; // We need special handling for the case where `collect_tokens` is called // on an opening delimeter (e.g. '('). At this point, we have already pushed // a new frame - however, we want to record the original `TokenTree::Delimited`, // for consistency with the case where we start recording one token earlier. // See `TokenCursor::next` to see how `cur_token` is set up. let prev_depth = if matches ! ( self token_cursor cur_token Some ( ( TokenTree Delimited ( ) ) ) ) { if self . token_cursor . stack . is_empty ( ) { // There is nothing below us in the stack that // the function could consume, so the only thing it can legally // capture is the entire contents of the current frame. return Ok ( ( f ( self ) ? , TokenStream :: new ( tokens ) ) ) ; } // We have already recorded the full `TokenTree::Delimited` when we created // our `tokens` vector at the start of this function. We are now inside // a new frame corresponding to the `TokenTree::Delimited` we already recoreded. // We don't want to record any of the tokens inside this frame, since they // will be duplicates of the tokens nested inside the `TokenTree::Delimited`. // Therefore, we set our recording depth to the *previous* frame. This allows // us to record a sequence like: `(foo).bar()`: the `(foo)` will be recored // as our initial `cur_token`, while the `.bar()` will be recored after we // pop the `(foo)` frame. self . token_cursor . stack . len ( ) - 1 } else { self . token_cursor . stack . len ( ) } ; let prev_collecting = self . token_cursor . collecting . replace ( Collecting { buf : tokens , depth : prev_depth } ) ; let ret = f ( self ) ; let mut collected_tokens = if let Some ( collecting ) = self . token_cursor . collecting . take ( ) { collecting . buf } else { let msg = " " ; debug ! ( " " msg ) ; self . sess . span_diagnostic . delay_span_bug ( self . token . span , & msg ) ; // This can happen due to a bad interaction of two unrelated recovery mechanisms // with mismatched delimiters *and* recovery lookahead on the likely typo // `pub ident(` (#62895, different but similar to the case above). return Ok ( ( ret ? , TokenStream :: default ( ) ) ) ; } ; debug ! ( " " collected_tokens ) ; // If we're not at EOF our current token wasn't actually consumed by // `f`, but it'll still be in our list that we pulled out. In that case // put it back. let extra_token = if self . token != token :: Eof { collected_tokens . pop ( ) } else { None } ; if let Some ( mut collecting ) = prev_collecting { // If we were previously collecting at the same depth, // then the previous call to `collect_tokens` needs to see // the tokens we just recorded. // // If we were previously recording at an lower `depth`, // then the previous `collect_tokens` call already recorded // this entire frame in the form of a `TokenTree::Delimited`, // so there is nothing else for us to do. if collecting . depth == prev_depth { collecting . buf . extend ( collected_tokens . iter ( ) . cloned ( ) ) ; collecting . buf . extend ( extra_token ) ; debug ! ( " " collecting ) ; } self . token_cursor . collecting = Some ( collecting ) } Ok ( ( ret ? , TokenStream :: new ( collected_tokens ) ) ) }
Records all tokens consumed by the provided callback,
including the current token.
Records all tokens consumed by the provided callback including the current token .
&mut self
f
impl FnOnce(&mut Self) -> PResult<'a, R>
&mut self
f
impl FnOnce(&mut Self) -> PResult<'a, R>