Skip to main content

mist_parser/parser/items/
mod.rs

1pub mod attribute;
2pub mod class;
3pub mod enums;
4pub mod function;
5pub mod impl_decl;
6
7use crate::{
8    Rule,
9    ast::*,
10    ast_expr,
11    error::{AstError, IntoErr, collect_recovered},
12    parser::consume_rule,
13};
14
15impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevel {
16    type Error = AstError<'a, Self>;
17
18    fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
19        let mut inner = pair.into_inner();
20
21        let attributes = collect_recovered(inner.next().unwrap().into_inner());
22
23        ast_expr!(TopLevel(
24            inner.next().map(Spanned::try_from).unwrap(),
25            attributes,
26        ))
27    }
28}
29
30impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
31    type Error = AstError<'a, Self>;
32
33    fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
34        let rule = pair.as_rule();
35        let mut inner = pair.clone().into_inner();
36
37        match rule {
38            Rule::import => ast_expr!(TopLevelKind::Import(
39                Visibility::try_from(&mut inner),
40                Path::try_from(inner.next().unwrap()),
41            )),
42
43            Rule::function_decl => ast_expr!(TopLevelKind::FunctionDecl(pair.try_into())),
44
45            Rule::struct_decl => ast_expr!(TopLevelKind::StructDecl {
46                visibility: Visibility::try_from(&mut inner),
47
48                name: inner.next().unwrap().try_into(),
49
50                generics: consume_rule(&mut inner, Rule::generics_decl)
51                    .map(GenericsDecl::try_from)
52                    .transpose()
53                    .map(|v| v.unwrap_or_default()),
54
55                fields: inner
56                    .next()
57                    .map(|pair| collect_recovered(pair.into_inner()))
58                    .transpose()
59                    .map(|v| v.unwrap_or_default()),
60            }),
61
62            Rule::class_decl => ast_expr!(TopLevelKind::ClassDecl {
63                visibility: Visibility::try_from(&mut inner),
64
65                name: inner.next().unwrap().try_into(),
66
67                generics: consume_rule(&mut inner, Rule::generics_decl)
68                    .map(GenericsDecl::try_from)
69                    .transpose()
70                    .map(|v| v.unwrap_or_default()),
71
72                inherits: consume_rule(&mut inner, Rule::expr_path)
73                    .map(ExprPath::try_from)
74                    .transpose(),
75
76                fields: collect_recovered(inner.next().unwrap().into_inner()),
77
78                constructor: inner.next().unwrap().try_into(),
79
80                items: collect_recovered(inner),
81            }),
82
83            Rule::enum_decl => ast_expr!(TopLevelKind::EnumDecl {
84                visibility: Visibility::try_from(&mut inner),
85
86                name: inner.next().unwrap().try_into(),
87
88                generics: consume_rule(&mut inner, Rule::generics_decl)
89                    .map(GenericsDecl::try_from)
90                    .transpose()
91                    .map(|v| v.unwrap_or_default()),
92
93                fields: collect_recovered(inner),
94            }),
95
96            Rule::mod_package => ast_expr!(TopLevelKind::Mod(
97                Visibility::try_from(&mut inner),
98                inner.next().unwrap().try_into(),
99            )),
100
101            Rule::impl_for_decl | Rule::impl_decl => {
102                ast_expr!(TopLevelKind::ImplDecl(pair.try_into()))
103            }
104
105            Rule::trait_decl => ast_expr!(TopLevelKind::TraitDecl {
106                visibility: Visibility::try_from(&mut inner),
107
108                name: inner.next().unwrap().try_into(),
109
110                generics: consume_rule(&mut inner, Rule::generics_decl)
111                    .map(GenericsDecl::try_from)
112                    .transpose()
113                    .map(|v| v.unwrap_or_default()),
114
115                requirements: consume_rule(&mut inner, Rule::trait_requirements)
116                    .map(|pair| collect_recovered(pair.into_inner()))
117                    .transpose()
118                    .map(|v| v.unwrap_or_default()),
119
120                items: collect_recovered(&mut inner),
121            }),
122
123            _ => AstError::bug_unimplemented(pair),
124        }
125    }
126}