#include "ctx.h" #include bool parse_try(struct ivy_parser *parser, struct ivy_ast_node **out) { if (!parse_keyword(parser, IVY_KW_TRY)) { parse_trace(parser, "expected `try` keyword"); return false; } struct ivy_ast_try_node *try = create_node(IVY_AST_TRY); if (!parse_expr(parser, EXPR_PARSE_STOP_KW_CONTROL, &try->n_try)) { parse_trace(parser, "invalid try block"); return false; } bool ok = true; while (1) { if (!parse_keyword(parser, IVY_KW_CATCH)) { break; } struct ivy_ast_node *catch_guard = NULL; struct ivy_ast_node *catch_block = NULL; if (!parse_expr(parser, EXPR_PARSE_STOP_LEFT_BRACKET, &catch_guard)) { parse_trace(parser, "invalid catch guard"); ok = false; break; } if (!parse_block(parser, &catch_block)) { parse_trace(parser, "invalid catch block"); ok = false; break; } struct ivy_ast_try_catch_node *catch = create_node(IVY_AST_TRY_CATCH); catch->n_pattern = catch_guard; catch->n_block = catch_block; fx_queue_push_back(&try->n_catch, &catch->n_base.n_entry); } *out = generic_node(try); return true; }