kotlin_parser/parse/ty/
mod.rs

1use crate::ast::*;
2use chumsky::prelude::*;
3
4use super::declaration::annotation::annotations_parser;
5
6pub mod function;
7pub mod simple;
8
9pub fn type_parser() -> impl Parser<char, Type, Error = Simple<char>> {
10    choice((
11        function::function_type_parser(),
12        simple::simple_type_parser(),
13    ))
14    .map(Type::from)
15    .boxed()
16}
17
18pub fn type_params_parser(
19    expr_parser: impl Parser<char, Expression, Error = Simple<char>>,
20) -> impl Parser<char, Vec<TypeParam>, Error = Simple<char>> {
21    annotations_parser(expr_parser)
22        .repeated()
23        .or_not()
24        .then(
25            text::ident()
26                .padded()
27                .then(just(':').padded().ignore_then(type_parser()).or_not()),
28        )
29        .separated_by(just(',').padded())
30        .delimited_by(just('<').padded(), just('>').padded())
31        .map(|types| {
32            types
33                .into_iter()
34                .map(|(annotations, (name, ty))| TypeParam {
35                    annotations: annotations.unwrap_or_default(),
36                    name,
37                    ty,
38                })
39                .collect()
40        })
41}
42
43pub fn class_type_params_parser(
44    expr_parser: impl Parser<char, Expression, Error = Simple<char>>,
45) -> impl Parser<char, Vec<BoundedTypeParam>, Error = Simple<char>> {
46    annotations_parser(expr_parser)
47        .repeated()
48        .or_not()
49        .then(
50            choice((
51                just("in").to(BoundKind::In),
52                just("out").to(BoundKind::Out),
53            ))
54            .or_not(),
55        )
56        .then(
57            text::ident()
58                .padded()
59                .then(just(':').padded().ignore_then(type_parser()).or_not()),
60        )
61        .separated_by(just(',').padded())
62        .delimited_by(just('<').padded(), just('>').padded())
63        .map(|types| {
64            types
65                .into_iter()
66                .map(|((annotations, bound), (name, ty))| BoundedTypeParam {
67                    annotations: annotations.unwrap_or_default(),
68                    bounds: vec![TypeBound {
69                        name,
70                        ty,
71                        kind: bound,
72                    }],
73                })
74                .collect()
75        })
76}
77
78pub fn type_bounds_parser(
79) -> impl Parser<char, Vec<TypeBound>, Error = Simple<char>> {
80    just("where")
81        .padded()
82        .ignore_then(
83            text::ident()
84                .padded()
85                .then(just(':').padded().ignore_then(type_parser()))
86                .separated_by(just(',').padded()),
87        )
88        .map(|bounds| {
89            bounds
90                .into_iter()
91                .map(|(name, ty)| TypeBound {
92                    name,
93                    ty: ty.into(),
94                    kind: None,
95                })
96                .collect()
97        })
98}