kotlin_parser/parse/expression/
try_expr.rs1use crate::{ast::*, parse::ty::function::param_parser};
2use chumsky::prelude::*;
3
4use super::call::block_parser;
5
6pub fn try_expr_parser(
7 stmt_parser: impl Parser<char, Statement, Error = Simple<char>> + Clone,
8 expr_parser: impl Parser<char, Expression, Error = Simple<char>>,
9) -> impl Parser<char, Expression, Error = Simple<char>> {
10 just("try")
11 .padded()
12 .ignore_then(block_parser(stmt_parser.clone()))
13 .then(catch_expr_parser(stmt_parser.clone(), expr_parser).repeated())
14 .then(finally_expr_parser(stmt_parser).or_not())
15 .map(|((body, catches), finally)| {
16 Expression::Try(TryExpression {
17 body,
18 catches,
19 finally,
20 })
21 })
22}
23
24pub fn catch_expr_parser(
25 stmt_parser: impl Parser<char, Statement, Error = Simple<char>>,
26 expr_parser: impl Parser<char, Expression, Error = Simple<char>>,
27) -> impl Parser<char, CatchExpression, Error = Simple<char>> {
28 just("catch")
29 .padded()
30 .ignore_then(param_parser(expr_parser))
31 .then(block_parser(stmt_parser))
32 .map(|(param, body)| CatchExpression { param, body })
33}
34
35pub fn finally_expr_parser(
36 stmt_parser: impl Parser<char, Statement, Error = Simple<char>>,
37) -> impl Parser<char, Block, Error = Simple<char>> {
38 just("finally")
39 .padded()
40 .ignore_then(block_parser(stmt_parser))
41}