lang: ast: implement parsing of try-catch-finally statements
This commit is contained in:
@@ -9,7 +9,8 @@ void expr_add_terminator(struct expr_parser_state *state, unsigned short tok)
|
||||
}
|
||||
}
|
||||
|
||||
void expr_copy_terminators(const struct expr_parser_state *src, struct expr_parser_state *dest)
|
||||
void expr_copy_terminators(
|
||||
const struct expr_parser_state *src, struct expr_parser_state *dest)
|
||||
{
|
||||
dest->s_nr_terminators = src->s_nr_terminators;
|
||||
|
||||
@@ -77,11 +78,14 @@ struct ast_node_type expr_node_ops = {
|
||||
.n_keyword_parsers = {
|
||||
/* statement keywords */
|
||||
KW_PARSER(FOR, stmt_parse_for),
|
||||
KW_PARSER(TRY, stmt_parse_try),
|
||||
KW_PARSER(WHILE, stmt_parse_while),
|
||||
KW_PARSER(MATCH, stmt_parse_match),
|
||||
KW_PARSER(IF, stmt_parse_if),
|
||||
KW_PARSER(THEN, stmt_parse_end),
|
||||
KW_PARSER(ELSE, stmt_parse_end),
|
||||
KW_PARSER(CATCH, stmt_parse_end),
|
||||
KW_PARSER(FINALLY, stmt_parse_end),
|
||||
KW_PARSER(END, stmt_parse_end),
|
||||
|
||||
/* operator/block keywords */
|
||||
|
||||
@@ -92,8 +92,10 @@ struct expr_parser_state {
|
||||
/* general functions */
|
||||
|
||||
extern void expr_add_terminator(struct expr_parser_state *state, unsigned short tok);
|
||||
extern void expr_copy_terminators(const struct expr_parser_state *src, struct expr_parser_state *dest);
|
||||
extern bool expr_terminates_at_token(struct expr_parser_state *state, unsigned short tok);
|
||||
extern void expr_copy_terminators(
|
||||
const struct expr_parser_state *src, struct expr_parser_state *dest);
|
||||
extern bool expr_terminates_at_token(
|
||||
struct expr_parser_state *state, unsigned short tok);
|
||||
|
||||
extern struct token_parse_result expr_finalise(
|
||||
struct ivy_parser *ctx, struct expr_parser_state *state,
|
||||
@@ -152,6 +154,8 @@ extern struct token_parse_result arith_parse_do(
|
||||
|
||||
/* statement parser callbacks */
|
||||
|
||||
extern struct token_parse_result stmt_parse_try(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok);
|
||||
extern struct token_parse_result stmt_parse_for(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok);
|
||||
extern struct token_parse_result stmt_parse_while(
|
||||
|
||||
@@ -6,6 +6,39 @@
|
||||
#include <ivy/lang/operator.h>
|
||||
#include <stdio.h>
|
||||
|
||||
struct token_parse_result stmt_parse_try(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct expr_parser_state *state
|
||||
= parser_get_state(ctx, struct expr_parser_state);
|
||||
|
||||
if (expr_terminates_at_token(state, IVY_KW_TRY)) {
|
||||
/* treat this as a statement terminator. */
|
||||
struct token_parse_result result
|
||||
= expr_finalise_and_return(ctx, state);
|
||||
result.r_flags |= PARSE_REPEAT_TOKEN;
|
||||
return result;
|
||||
}
|
||||
|
||||
if (state->s_sub_type == EXPR_SUBTYPE_KEYWORD_ARG) {
|
||||
/* try-catch cannot be used inline. */
|
||||
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
||||
}
|
||||
|
||||
state->s_prev_token = IVY_KW_TRY;
|
||||
|
||||
if (b_queue_empty(&state->s_operator_stack)
|
||||
&& b_queue_empty(&state->s_output_queue)) {
|
||||
parser_pop_state(ctx, 0);
|
||||
}
|
||||
|
||||
/* if expr is NULL, this is an if-then-else-end statement,
|
||||
* otherwise, this is an expr-if-else-expr. */
|
||||
parser_push_state(ctx, IVY_AST_TRY, 0);
|
||||
|
||||
return PARSE_RESULT(IVY_OK, 0);
|
||||
}
|
||||
|
||||
struct token_parse_result stmt_parse_for(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
@@ -14,7 +47,8 @@ struct token_parse_result stmt_parse_for(
|
||||
|
||||
if (expr_terminates_at_token(state, IVY_KW_FOR)) {
|
||||
/* treat this as a statement terminator. */
|
||||
struct token_parse_result result = expr_finalise_and_return(ctx, state);
|
||||
struct token_parse_result result
|
||||
= expr_finalise_and_return(ctx, state);
|
||||
result.r_flags |= PARSE_REPEAT_TOKEN;
|
||||
return result;
|
||||
}
|
||||
@@ -128,7 +162,8 @@ struct token_parse_result stmt_parse_if(
|
||||
|
||||
if (expr_terminates_at_token(state, IVY_KW_IF)) {
|
||||
/* treat this as a statement terminator. */
|
||||
struct token_parse_result result = expr_finalise_and_return(ctx, state);
|
||||
struct token_parse_result result
|
||||
= expr_finalise_and_return(ctx, state);
|
||||
result.r_flags |= PARSE_REPEAT_TOKEN;
|
||||
return result;
|
||||
}
|
||||
@@ -196,4 +231,4 @@ struct token_parse_result stmt_parse_end(
|
||||
struct token_parse_result result = expr_finalise_and_return(ctx, state);
|
||||
result.r_flags |= PARSE_REPEAT_TOKEN;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user