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 the right-hand side of a `cfg_select!` branch,
11    /// which can be either a braced block or an expression.
12    pub fn parse_cfg_select_branch_rhs(&mut self) -> PResult<'a, TokenStream> {
13        if self.token == token::OpenBrace {
14            // Strip the outer '{' and '}'.
15            match self.parse_token_tree() {
16                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 '{{'"),
17                TokenTree::Delimited(.., tts) => {
18                    // Optionally end with a comma.
19                    let _ = self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Comma,
    token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma));
20                    return Ok(tts);
21                }
22            }
23        }
24        let attrs = AttrWrapper::empty(); // FIXME expressions with attributes can be supported here
25        let expr = self.collect_tokens(
26            None,
27            AttrWrapper::empty(),
28            ForceCollect::Yes,
29            |p, _empty_attrs| {
30                p.parse_expr_res_after_attrs(Restrictions::STMT_EXPR, attrs)
31                    .map(|(expr, _)| (expr, Trailing::No, UsePreAttrPos::No))
32            },
33        )?;
34        if !classify::expr_is_complete(&expr)
35            && self.token != token::CloseBrace
36            && self.token != token::Eof
37        {
38            self.expect(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Comma,
    token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma))?;
39        } else {
40            let _ = self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Comma,
    token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma));
41        }
42        Ok(TokenStream::from_ast(&expr))
43    }
44}