Skip to main content

rustc_parse/parser/
cfg_select.rs

1use rustc_ast::token;
2use rustc_ast::tokenstream::{TokenStream, TokenTree};
3use rustc_ast::util::classify;
4use rustc_errors::PResult;
5
6use crate::exp;
7use crate::parser::{AttrWrapper, ForceCollect, Parser, Restrictions, Trailing, UsePreAttrPos};
8
9impl<'a> Parser<'a> {
10    /// Parses a `TokenTree` consisting either of `{ /* ... */ }` optionally followed by a comma
11    /// (and strip the braces and the optional comma) or an expression followed by a comma
12    /// (and strip the comma).
13    pub fn parse_delimited_token_tree(&mut self) -> PResult<'a, TokenStream> {
14        if self.token == token::OpenBrace {
15            // Strip the outer '{' and '}'.
16            match self.parse_token_tree() {
17                TokenTree::Token(..) => {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("because the current token is a \'{{\'")));
}unreachable!("because the current token is a '{{'"),
18                TokenTree::Delimited(.., tts) => {
19                    // Optionally end with a comma.
20                    let _ = self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Comma,
    token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma));
21                    return Ok(tts);
22                }
23            }
24        }
25        let expr = self.collect_tokens(None, AttrWrapper::empty(), ForceCollect::Yes, |p, _| {
26            p.parse_expr_res(Restrictions::STMT_EXPR, AttrWrapper::empty())
27                .map(|(expr, _)| (expr, Trailing::No, UsePreAttrPos::No))
28        })?;
29        if !classify::expr_is_complete(&expr)
30            && self.token != token::CloseBrace
31            && self.token != token::Eof
32        {
33            self.expect(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Comma,
    token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma))?;
34        } else {
35            let _ = self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Comma,
    token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma));
36        }
37        Ok(TokenStream::from_ast(&expr))
38    }
39}