#include "parse.h" #include #include #include #include struct import_parser_state { struct parser_state s_base; unsigned int s_prev_token; unsigned long long s_package_ident; bool s_package_ident_set; }; static enum ivy_status parse_package( struct ivy_asm_parser *ctx, struct ivy_asm_token *tok) { struct import_parser_state *state = (struct import_parser_state *)asm_parser_get_state(ctx); if (state->s_prev_token != IVY_ASM_KW_IMPORT && state->s_prev_token != IVY_ASM_TOK_LINEFEED) { return IVY_ERR_BAD_SYNTAX; } state->s_package_ident_set = false; state->s_prev_token = IVY_ASM_KW_PACKAGE; return IVY_OK; } static enum ivy_status parse_label( struct ivy_asm_parser *ctx, struct ivy_asm_token *tok) { struct import_parser_state *state = (struct import_parser_state *)asm_parser_get_state(ctx); if (state->s_prev_token != IVY_ASM_SYM_LEFT_BRACKET) { return IVY_ERR_BAD_SYNTAX; } if (state->s_package_ident_set) { return IVY_ERR_BAD_SYNTAX; } if (strcmp(tok->t_str, "ident") != 0) { return IVY_ERR_BAD_SYNTAX; } state->s_prev_token = IVY_ASM_TOK_LABEL; return IVY_OK; } static enum ivy_status parse_int( struct ivy_asm_parser *ctx, struct ivy_asm_token *tok) { struct import_parser_state *state = (struct import_parser_state *)asm_parser_get_state(ctx); if (state->s_prev_token != IVY_ASM_TOK_LABEL) { return IVY_ERR_BAD_SYNTAX; } if (state->s_package_ident_set) { return IVY_ERR_BAD_SYNTAX; } state->s_prev_token = IVY_ASM_TOK_INT; state->s_package_ident = tok->t_int.uv; state->s_package_ident_set = true; return IVY_OK; } static enum ivy_status parse_left_bracket( struct ivy_asm_parser *ctx, struct ivy_asm_token *tok) { struct import_parser_state *state = (struct import_parser_state *)asm_parser_get_state(ctx); if (state->s_prev_token != IVY_ASM_KW_PACKAGE) { return IVY_ERR_BAD_SYNTAX; } if (state->s_package_ident_set) { return IVY_ERR_BAD_SYNTAX; } state->s_prev_token = IVY_ASM_SYM_LEFT_BRACKET; return IVY_OK; } static enum ivy_status parse_right_bracket( struct ivy_asm_parser *ctx, struct ivy_asm_token *tok) { struct import_parser_state *state = (struct import_parser_state *)asm_parser_get_state(ctx); if (state->s_prev_token != IVY_ASM_TOK_INT) { return IVY_ERR_BAD_SYNTAX; } if (!state->s_package_ident_set) { return IVY_ERR_BAD_SYNTAX; } ivy_assembler_attrib_table attrib = { [IVY_ASM_ATTRIB_IDENT] = state->s_package_ident, }; ivy_assembler_put_xval(ctx->p_assembler, IVY_ASM_XVAL_PACKAGE_REF, attrib); state->s_prev_token = IVY_ASM_SYM_RIGHT_BRACKET; state->s_package_ident_set = false; return IVY_OK; } static enum ivy_status parse_end( struct ivy_asm_parser *ctx, struct ivy_asm_token *tok) { struct import_parser_state *state = (struct import_parser_state *)asm_parser_get_state(ctx); if (state->s_prev_token != IVY_ASM_KW_IMPORT && state->s_prev_token != IVY_ASM_SYM_RIGHT_BRACKET) { return IVY_ERR_BAD_SYNTAX; } asm_parser_pop_state(ctx, NULL); return IVY_OK; } static void init_state(struct ivy_asm_parser *ctx, struct parser_state *p) { ivy_assembler_begin_scope(ctx->p_assembler, IVY_ASM_SCOPE_IMPORT, NULL); struct import_parser_state *state = (struct import_parser_state *)p; state->s_prev_token = IVY_ASM_KW_IMPORT; } static void finish_state(struct ivy_asm_parser *ctx, struct parser_state *state) { ivy_assembler_end_scope(ctx->p_assembler); } struct parser_state_type import_parser_state_type = { .n_init_state = init_state, .n_finish_state = finish_state, .n_state_size = sizeof(struct import_parser_state), .n_token_parsers = { TOK_PARSER(LABEL, parse_label), TOK_PARSER(INT, parse_int), }, .n_keyword_parsers = { KW_PARSER(PACKAGE, parse_package), KW_PARSER(END, parse_end), }, .n_symbol_parsers = { SYM_PARSER(LEFT_BRACKET, parse_left_bracket), SYM_PARSER(RIGHT_BRACKET, parse_right_bracket), }, };