2024-12-14 21:57:29 +00:00
|
|
|
#include "parse.h"
|
|
|
|
|
|
|
|
|
|
#include <blue/core/hash.h>
|
|
|
|
|
#include <ivy/asm/assembler.h>
|
|
|
|
|
|
|
|
|
|
enum item_type {
|
|
|
|
|
ITEM_NONE = 0,
|
|
|
|
|
ITEM_PROPERTY,
|
|
|
|
|
ITEM_VAR,
|
|
|
|
|
ITEM_MSGH,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct class_parser_state {
|
|
|
|
|
struct parser_state s_base;
|
|
|
|
|
|
|
|
|
|
unsigned int s_prev_token;
|
|
|
|
|
enum item_type s_current_item;
|
|
|
|
|
ivy_assembler_attrib_table s_current_item_attrib;
|
|
|
|
|
|
|
|
|
|
bool s_in_attrib_list;
|
|
|
|
|
struct ivy_asm_token *s_attrib_name, *s_attrib_value;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static enum ivy_status push_attrib(struct class_parser_state *state)
|
|
|
|
|
{
|
|
|
|
|
uint64_t hash = b_hash_string(state->s_attrib_name->t_str);
|
|
|
|
|
const char *s = state->s_attrib_name->t_str;
|
|
|
|
|
unsigned long long v = state->s_attrib_value->t_int.uv;
|
|
|
|
|
|
|
|
|
|
switch (hash) {
|
|
|
|
|
case HASH_IDENT:
|
|
|
|
|
if (!strcmp(s, "ident")) {
|
|
|
|
|
state->s_current_item_attrib[IVY_ASM_ATTRIB_IDENT] = v;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case HASH_INDEX:
|
|
|
|
|
if (!strcmp(s, "index")) {
|
|
|
|
|
state->s_current_item_attrib[IVY_ASM_ATTRIB_INDEX] = v;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case HASH_RECIPIENT:
|
|
|
|
|
if (!strcmp(s, "recipient")) {
|
|
|
|
|
state->s_current_item_attrib[IVY_ASM_ATTRIB_RECIPIENT] = v;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case HASH_SELECTOR:
|
|
|
|
|
if (!strcmp(s, "selector")) {
|
|
|
|
|
state->s_current_item_attrib[IVY_ASM_ATTRIB_SELECTOR] = v;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case HASH_GET:
|
|
|
|
|
if (!strcmp(s, "get")) {
|
|
|
|
|
state->s_current_item_attrib[IVY_ASM_ATTRIB_GET] = v;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case HASH_SET:
|
|
|
|
|
if (!strcmp(s, "set")) {
|
|
|
|
|
state->s_current_item_attrib[IVY_ASM_ATTRIB_SET] = v;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case HASH_PACKAGE:
|
|
|
|
|
if (!strcmp(s, "package")) {
|
|
|
|
|
state->s_current_item_attrib[IVY_ASM_ATTRIB_PACKAGE] = v;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case HASH_BLOCK:
|
|
|
|
|
if (!strcmp(s, "block")) {
|
|
|
|
|
state->s_current_item_attrib[IVY_ASM_ATTRIB_BLOCK] = v;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return IVY_ERR_BAD_SYNTAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ivy_asm_token_destroy(state->s_attrib_name);
|
|
|
|
|
ivy_asm_token_destroy(state->s_attrib_value);
|
|
|
|
|
|
|
|
|
|
state->s_attrib_name = NULL;
|
|
|
|
|
state->s_attrib_value = NULL;
|
|
|
|
|
|
|
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum ivy_status parse_property(
|
|
|
|
|
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
|
|
|
|
{
|
|
|
|
|
struct class_parser_state *state
|
|
|
|
|
= (struct class_parser_state *)asm_parser_get_state(ctx);
|
|
|
|
|
|
|
|
|
|
if (state->s_prev_token != IVY_ASM_TOK_NONE) {
|
|
|
|
|
return IVY_ERR_BAD_SYNTAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state->s_prev_token = IVY_ASM_TOK_KEYWORD;
|
|
|
|
|
state->s_current_item = ITEM_PROPERTY;
|
|
|
|
|
memset(state->s_current_item_attrib, 0x0,
|
|
|
|
|
sizeof state->s_current_item_attrib);
|
|
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum ivy_status parse_var(
|
|
|
|
|
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
|
|
|
|
{
|
|
|
|
|
struct class_parser_state *state
|
|
|
|
|
= (struct class_parser_state *)asm_parser_get_state(ctx);
|
|
|
|
|
|
|
|
|
|
if (state->s_prev_token != IVY_ASM_TOK_NONE) {
|
|
|
|
|
return IVY_ERR_BAD_SYNTAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state->s_prev_token = IVY_ASM_TOK_KEYWORD;
|
|
|
|
|
state->s_current_item = ITEM_VAR;
|
|
|
|
|
memset(state->s_current_item_attrib, 0x0,
|
|
|
|
|
sizeof state->s_current_item_attrib);
|
|
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum ivy_status parse_msgh(
|
|
|
|
|
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
|
|
|
|
{
|
|
|
|
|
struct class_parser_state *state
|
|
|
|
|
= (struct class_parser_state *)asm_parser_get_state(ctx);
|
|
|
|
|
|
|
|
|
|
if (state->s_prev_token != IVY_ASM_TOK_NONE) {
|
|
|
|
|
return IVY_ERR_BAD_SYNTAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state->s_prev_token = IVY_ASM_TOK_KEYWORD;
|
|
|
|
|
state->s_current_item = ITEM_MSGH;
|
|
|
|
|
memset(state->s_current_item_attrib, 0x0,
|
|
|
|
|
sizeof state->s_current_item_attrib);
|
|
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum ivy_status parse_left_bracket(
|
|
|
|
|
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
|
|
|
|
{
|
|
|
|
|
struct class_parser_state *state
|
|
|
|
|
= (struct class_parser_state *)asm_parser_get_state(ctx);
|
|
|
|
|
|
|
|
|
|
if (state->s_prev_token != IVY_ASM_TOK_KEYWORD) {
|
|
|
|
|
return IVY_ERR_BAD_SYNTAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (state->s_in_attrib_list) {
|
|
|
|
|
return IVY_ERR_BAD_SYNTAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state->s_in_attrib_list = true;
|
|
|
|
|
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 class_parser_state *state
|
|
|
|
|
= (struct class_parser_state *)asm_parser_get_state(ctx);
|
|
|
|
|
|
|
|
|
|
if (!state->s_in_attrib_list) {
|
|
|
|
|
return IVY_ERR_BAD_SYNTAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state->s_in_attrib_list = false;
|
|
|
|
|
state->s_prev_token = IVY_ASM_SYM_RIGHT_BRACKET;
|
|
|
|
|
|
|
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum ivy_status parse_label(
|
|
|
|
|
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
|
|
|
|
{
|
|
|
|
|
struct class_parser_state *state
|
|
|
|
|
= (struct class_parser_state *)asm_parser_get_state(ctx);
|
|
|
|
|
|
|
|
|
|
if (state->s_prev_token != IVY_ASM_SYM_LEFT_BRACKET
|
|
|
|
|
&& state->s_prev_token != IVY_ASM_TOK_INT) {
|
|
|
|
|
return IVY_ERR_BAD_SYNTAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!state->s_in_attrib_list) {
|
|
|
|
|
return IVY_ERR_BAD_SYNTAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (state->s_attrib_name) {
|
|
|
|
|
return IVY_ERR_BAD_SYNTAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state->s_prev_token = IVY_ASM_TOK_LABEL;
|
|
|
|
|
state->s_attrib_name = tok;
|
|
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum ivy_status parse_int(
|
|
|
|
|
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
|
|
|
|
{
|
|
|
|
|
struct class_parser_state *state
|
|
|
|
|
= (struct class_parser_state *)asm_parser_get_state(ctx);
|
|
|
|
|
|
|
|
|
|
if (state->s_prev_token != IVY_ASM_TOK_LABEL) {
|
|
|
|
|
return IVY_ERR_BAD_SYNTAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!state->s_in_attrib_list) {
|
|
|
|
|
return IVY_ERR_BAD_SYNTAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (state->s_attrib_value) {
|
|
|
|
|
return IVY_ERR_BAD_SYNTAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state->s_attrib_value = tok;
|
|
|
|
|
state->s_prev_token = IVY_ASM_TOK_INT;
|
|
|
|
|
return push_attrib(state);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum ivy_status parse_linefeed(
|
|
|
|
|
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
|
|
|
|
{
|
|
|
|
|
struct class_parser_state *state
|
|
|
|
|
= (struct class_parser_state *)asm_parser_get_state(ctx);
|
|
|
|
|
|
|
|
|
|
if (state->s_in_attrib_list) {
|
|
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (state->s_prev_token != IVY_ASM_TOK_KEYWORD
|
|
|
|
|
&& state->s_prev_token != IVY_ASM_SYM_RIGHT_BRACKET) {
|
|
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (state->s_current_item == 0) {
|
|
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* TODO write table entry. */
|
|
|
|
|
switch (state->s_current_item) {
|
|
|
|
|
case ITEM_PROPERTY:
|
|
|
|
|
ivy_assembler_put_xval(
|
|
|
|
|
ctx->p_assembler, IVY_ASM_XVAL_PROPERTY,
|
|
|
|
|
state->s_current_item_attrib);
|
|
|
|
|
break;
|
|
|
|
|
case ITEM_VAR:
|
|
|
|
|
ivy_assembler_put_xval(
|
|
|
|
|
ctx->p_assembler, IVY_ASM_XVAL_MEMBER_VAR,
|
|
|
|
|
state->s_current_item_attrib);
|
|
|
|
|
break;
|
|
|
|
|
case ITEM_MSGH:
|
|
|
|
|
ivy_assembler_put_xval(
|
|
|
|
|
ctx->p_assembler, IVY_ASM_XVAL_MESSAGE_HANDLER,
|
|
|
|
|
state->s_current_item_attrib);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return IVY_ERR_INTERNAL_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state->s_current_item = 0;
|
|
|
|
|
state->s_prev_token = IVY_ASM_TOK_NONE;
|
|
|
|
|
|
|
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum ivy_status parse_end(
|
|
|
|
|
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
|
|
|
|
{
|
|
|
|
|
struct class_parser_state *state
|
|
|
|
|
= (struct class_parser_state *)asm_parser_get_state(ctx);
|
|
|
|
|
|
|
|
|
|
if (state->s_in_attrib_list) {
|
|
|
|
|
return IVY_ERR_BAD_SYNTAX;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (state->s_prev_token) {
|
|
|
|
|
case IVY_ASM_KW_CLASS:
|
|
|
|
|
case IVY_ASM_SYM_RIGHT_BRACKET:
|
|
|
|
|
case IVY_ASM_TOK_NONE:
|
|
|
|
|
asm_parser_pop_state(ctx, NULL);
|
|
|
|
|
return IVY_OK;
|
|
|
|
|
default:
|
|
|
|
|
return IVY_ERR_BAD_SYNTAX;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void init_state(struct ivy_asm_parser *ctx, struct parser_state *state)
|
|
|
|
|
{
|
|
|
|
|
ivy_assembler_begin_scope(
|
|
|
|
|
ctx->p_assembler, IVY_ASM_SCOPE_CLASS, state->s_attrib);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void finish_state(struct ivy_asm_parser *ctx, struct parser_state *state)
|
|
|
|
|
{
|
|
|
|
|
ivy_assembler_end_scope(ctx->p_assembler);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct parser_state_type class_parser_state_type = {
|
|
|
|
|
.n_init_state = init_state,
|
|
|
|
|
.n_finish_state = finish_state,
|
|
|
|
|
.n_state_size = sizeof(struct class_parser_state),
|
|
|
|
|
.n_keyword_parsers = {
|
|
|
|
|
KW_PARSER(PROPERTY, parse_property),
|
|
|
|
|
KW_PARSER(VAR, parse_var),
|
|
|
|
|
KW_PARSER(MSGH, parse_msgh),
|
|
|
|
|
KW_PARSER(END, parse_end),
|
|
|
|
|
},
|
|
|
|
|
.n_symbol_parsers = {
|
|
|
|
|
SYM_PARSER(LEFT_BRACKET, parse_left_bracket),
|
|
|
|
|
SYM_PARSER(RIGHT_BRACKET, parse_right_bracket),
|
|
|
|
|
},
|
|
|
|
|
.n_token_parsers = {
|
|
|
|
|
TOK_PARSER(LABEL, parse_label),
|
|
|
|
|
TOK_PARSER(INT, parse_int),
|
|
|
|
|
TOK_PARSER(LINEFEED, parse_linefeed),
|
|
|
|
|
}
|
|
|
|
|
};
|