rustfmt_nightly/parse/macros/
lazy_static.rs

1use rustc_ast::ast;
2use rustc_ast::token;
3use rustc_ast::tokenstream::TokenStream;
4use rustc_parse::exp;
5use rustc_span::symbol;
6
7use crate::rewrite::RewriteContext;
8
9pub(crate) fn parse_lazy_static(
10    context: &RewriteContext<'_>,
11    ts: TokenStream,
12) -> Option<Vec<(ast::Visibility, symbol::Ident, Box<ast::Ty>, Box<ast::Expr>)>> {
13    let mut result = vec![];
14    let mut parser = super::build_parser(context, ts);
15    macro_rules! parse_or {
16        ($method:ident $(,)* $($arg:expr),* $(,)*) => {
17            match parser.$method($($arg,)*) {
18                Ok(val) => {
19                    if parser.psess.dcx().has_errors().is_some() {
20                        parser.psess.dcx().reset_err_count();
21                        return None;
22                    } else {
23                        val
24                    }
25                }
26                Err(err) => {
27                    err.cancel();
28                    parser.psess.dcx().reset_err_count();
29                    return None;
30                }
31            }
32        }
33    }
34    while parser.token.kind != token::Eof {
35        // Parse a `lazy_static!` item.
36        // FIXME: These `eat_*` calls should be converted to `parse_or` to avoid
37        // silently formatting malformed lazy-statics.
38        let vis = parse_or!(parse_visibility, rustc_parse::parser::FollowedByType::No);
39        let _ = parser.eat_keyword(exp!(Static));
40        let _ = parser.eat_keyword(exp!(Ref));
41        let id = parse_or!(parse_ident);
42        let _ = parser.eat(exp!(Colon));
43        let ty = parse_or!(parse_ty);
44        let _ = parser.eat(exp!(Eq));
45        let expr = parse_or!(parse_expr);
46        let _ = parser.eat(exp!(Semi));
47        result.push((vis, id, ty, expr));
48    }
49
50    Some(result)
51}