mist_parser/parser/items/
mod.rs1pub 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 fields: collect_recovered(inner.next().unwrap().into_inner()),
73
74 constructor: inner.next().unwrap().try_into(),
75
76 items: collect_recovered(inner),
77 }),
78
79 Rule::enum_decl => ast_expr!(TopLevelKind::EnumDecl {
80 visibility: Visibility::try_from(&mut inner),
81
82 name: inner.next().unwrap().try_into(),
83
84 generics: consume_rule(&mut inner, Rule::generics_decl)
85 .map(GenericsDecl::try_from)
86 .transpose()
87 .map(|v| v.unwrap_or_default()),
88
89 fields: collect_recovered(inner),
90 }),
91
92 Rule::mod_package => ast_expr!(TopLevelKind::Mod(
93 Visibility::try_from(&mut inner),
94 inner.next().unwrap().try_into(),
95 )),
96
97 Rule::impl_for_decl | Rule::impl_decl => {
98 ast_expr!(TopLevelKind::ImplDecl(pair.try_into()))
99 }
100
101 Rule::trait_decl => ast_expr!(TopLevelKind::TraitDecl {
102 visibility: Visibility::try_from(&mut inner),
103
104 name: inner.next().unwrap().try_into(),
105
106 generics: consume_rule(&mut inner, Rule::generics_decl)
107 .map(GenericsDecl::try_from)
108 .transpose()
109 .map(|v| v.unwrap_or_default()),
110
111 requirements: consume_rule(&mut inner, Rule::trait_requirements)
112 .map(|pair| collect_recovered(pair.into_inner()))
113 .transpose()
114 .map(|v| v.unwrap_or_default()),
115
116 items: collect_recovered(&mut inner),
117 }),
118
119 _ => AstError::bug_unimplemented(pair),
120 }
121 }
122}