mist_parser/parser/common/
mod.rs1pub mod decl;
2pub mod expr;
3pub mod statement;
4pub mod types;
5
6use crate::{
7 Rule,
8 ast::*,
9 ast_ensure, ast_expr,
10 error::{AstError, IntoErr, collect_recovered},
11 parser::consume_rule,
12};
13
14impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Identifier {
15 type Error = AstError<'a, Self>;
16
17 fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
18 ast_ensure!(pair, Rule::identifier => {
19 Ok(Identifier(pair.as_str().to_string()))
20 })
21 }
22}
23
24impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Path {
25 type Error = AstError<'a, Self>;
26
27 fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
28 match pair.as_rule() {
29 Rule::static_path => Ok(Path(collect_recovered(pair.into_inner()).get()?)),
30 _ => AstError::bug_unimplemented(pair),
31 }
32 }
33}
34
35impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Literal {
36 type Error = AstError<'a, Self>;
37
38 fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
39 let rule = pair.as_rule();
40 let mut inner = pair.clone().into_inner();
41
42 Ok(match rule {
43 Rule::primary => Self::try_from(inner.next().unwrap())?,
44 Rule::literal => Self::try_from(inner.next().unwrap())?,
45 Rule::integer => Literal::Int(pair.as_str().parse::<i64>().unwrap()),
46 Rule::float => Literal::Float(pair.as_str().parse::<f64>().unwrap()),
47 Rule::boolean => Literal::Bool(pair.as_str().parse::<bool>().unwrap()),
48 Rule::string_lit => Literal::String(inner.as_str().to_string()),
49 Rule::tuple => Literal::Tuple(collect_recovered(inner).get()?),
50 _ => return AstError::bug_unimplemented(pair),
51 })
52 }
53}
54
55impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Pattern {
56 type Error = AstError<'a, Self>;
57
58 fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
59 let rule = pair.as_rule();
60 let mut inner = pair.clone().into_inner();
61
62 match rule {
63 Rule::tuple_pattern => ast_expr!(Pattern::Tuple(collect_recovered(pair.into_inner()))),
64
65 Rule::named_tuple_pattern => ast_expr!(Pattern::NamedTuple(
66 Path::try_from(inner.next().unwrap()),
67 collect_recovered(inner),
68 )),
69
70 Rule::struct_pattern => ast_expr!(Pattern::Struct(
71 Path::try_from(inner.next().unwrap()),
72 collect_recovered(inner),
73 )),
74
75 Rule::literal => ast_expr!(Pattern::Literal(pair.try_into())),
76
77 Rule::identifier => ast_expr!(Pattern::Id(pair.try_into())),
78
79 Rule::static_path => ast_expr!(Pattern::Path(pair.try_into())),
80
81 _ => AstError::bug_unimplemented(pair),
82 }
83 }
84}
85
86impl<'a> TryFrom<&mut pest::iterators::Pairs<'a, Rule>> for Visibility {
87 type Error = AstError<'a, Self>;
88
89 fn try_from(pairs: &mut pest::iterators::Pairs<'a, Rule>) -> Result<Self, Self::Error> {
90 Ok(consume_rule(pairs, Rule::visibility)
91 .map(|pair| -> Result<Visibility, AstError<'a, Self>> {
92 if let Some(path) = pair.into_inner().next() {
93 ast_expr!(Visibility::PublicTarget(Path::try_from(path)))
94 } else {
95 Ok(Visibility::Public)
96 }
97 })
98 .transpose()?
99 .unwrap_or_else(|| Visibility::Private))
100 }
101}