#include "parse.h" #include #include #include #include struct ident_parser_state { struct parser_state s_base; unsigned int s_prev_token; b_queue s_parts; }; static void init_state(struct ivy_asm_parser *ctx, struct parser_state *s) { struct ident_parser_state *state = (struct ident_parser_state *)s; state->s_prev_token = IVY_ASM_SYM_LEFT_PAREN; } static enum ivy_status parse_ident( struct ivy_asm_parser *ctx, struct ivy_asm_token *tok) { struct ident_parser_state *state = (struct ident_parser_state *)asm_parser_get_state(ctx); if (state->s_prev_token != IVY_ASM_SYM_LEFT_PAREN && state->s_prev_token != IVY_ASM_SYM_DOT) { return IVY_ERR_BAD_SYNTAX; } b_queue_push_back(&state->s_parts, &tok->t_entry); state->s_prev_token = IVY_ASM_TOK_IDENT; return IVY_OK; } static enum ivy_status parse_dot( struct ivy_asm_parser *ctx, struct ivy_asm_token *tok) { struct ident_parser_state *state = (struct ident_parser_state *)asm_parser_get_state(ctx); if (state->s_prev_token != IVY_ASM_TOK_IDENT) { return IVY_ERR_BAD_SYNTAX; } state->s_prev_token = IVY_ASM_SYM_DOT; return IVY_OK; } static enum ivy_status parse_right_paren( struct ivy_asm_parser *ctx, struct ivy_asm_token *tok) { struct ident_parser_state *state = (struct ident_parser_state *)asm_parser_get_state(ctx); if (state->s_prev_token != IVY_ASM_TOK_IDENT) { return IVY_ERR_BAD_SYNTAX; } struct ivy_ident *ident = ivy_ident_create(); b_queue_iterator it = {0}; b_queue_iterator_begin(&state->s_parts, &it); while (b_queue_iterator_is_valid(&it)) { struct ivy_asm_token *tok = b_unbox(struct ivy_asm_token, it.entry, t_entry); b_queue_iterator_erase(&it); ivy_ident_add_part(ident, tok->t_str); ivy_asm_token_destroy(tok); } asm_parser_pop_state(ctx, ident); return IVY_OK; } struct parser_state_type ident_parser_state_type = { .n_init_state = init_state, .n_state_size = sizeof(struct ident_parser_state), .n_token_parsers = { TOK_PARSER(IDENT, parse_ident), }, .n_symbol_parsers = { SYM_PARSER(DOT, parse_dot), SYM_PARSER(RIGHT_PAREN, parse_right_paren), }, };