Skip to main content

mist_parser/parser/common/
decl.rs

1use crate::{
2    Rule,
3    ast::*,
4    ast_expr,
5    error::{AstError, IntoErr, collect_recovered},
6};
7
8impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDeclStmt {
9    type Error = AstError<'a, Self>;
10
11    fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
12        match pair.as_rule() {
13            Rule::var_decl_statement => {
14                let mut inner = pair.into_inner();
15
16                ast_expr!(VarDeclStmt {
17                    decl: inner.next().unwrap().try_into(),
18                    init: inner.next().map(Expression::try_from).transpose()
19                })
20            }
21
22            _ => AstError::bug_unimplemented(pair),
23        }
24    }
25}
26
27impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FieldDeclStmt {
28    type Error = AstError<'a, Self>;
29
30    fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
31        match pair.as_rule() {
32            Rule::class_field => {
33                let mut inner = pair.into_inner();
34
35                ast_expr!(FieldDeclStmt {
36                    decl: inner.next().unwrap().try_into(),
37                    init: inner.next().map(Expression::try_from).transpose(),
38                })
39            }
40
41            _ => AstError::bug_unimplemented(pair),
42        }
43    }
44}
45
46impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDecl {
47    type Error = AstError<'a, Self>;
48
49    fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
50        match pair.as_rule() {
51            Rule::var_decl | Rule::param => {
52                let mut inner = pair.into_inner();
53
54                ast_expr!(VarDecl {
55                    name: Pattern::try_from(inner.next().unwrap()),
56                    type_: inner.next().map(TypeExpr::try_from).transpose(),
57                })
58            }
59
60            _ => AstError::bug_unimplemented(pair),
61        }
62    }
63}
64
65impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FieldDecl {
66    type Error = AstError<'a, Self>;
67
68    fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
69        match pair.as_rule() {
70            Rule::field => {
71                let mut inner = pair.into_inner();
72
73                ast_expr!(FieldDecl {
74                    visibility: Visibility::try_from(&mut inner),
75                    name: Identifier::try_from(inner.next().unwrap()),
76                    type_: TypeExpr::try_from(inner.next().unwrap()),
77                })
78            }
79
80            _ => AstError::bug_unimplemented(pair),
81        }
82    }
83}
84
85impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ParamList {
86    type Error = AstError<'a, Self>;
87
88    fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
89        Ok(ParamList(collect_recovered(pair.into_inner()).get()?))
90    }
91}