2024-12-13 12:26:01 +00:00
|
|
|
#include "parse.h"
|
2024-12-14 20:26:04 +00:00
|
|
|
#include <ivy/asm/assembler.h>
|
|
|
|
|
#include <blue/core/hash.h>
|
|
|
|
|
|
|
|
|
|
#define HASH_IDENT 0x684f633b2528fc75
|
|
|
|
|
#define HASH_INDEX 0x83cf8e8f9081468b
|
|
|
|
|
#define HASH_RECIPIENT 0x3d0abea328337078
|
|
|
|
|
#define HASH_SELECTOR 0x03b0355a1af4ac9c
|
|
|
|
|
#define HASH_GET 0xd4e26318faaa79f7
|
|
|
|
|
#define HASH_SET 0x823b87195ce20e23
|
|
|
|
|
#define HASH_PACKAGE 0x53cf3eec39cf731b
|
2024-12-13 12:26:01 +00:00
|
|
|
|
|
|
|
|
struct unit_parser_state {
|
|
|
|
|
struct parser_state s_base;
|
2024-12-14 20:26:04 +00:00
|
|
|
|
|
|
|
|
bool s_in_attrib_list;
|
|
|
|
|
unsigned int s_prev_token;
|
|
|
|
|
unsigned int s_next_scope;
|
|
|
|
|
ivy_assembler_attrib_table s_next_scope_attrib;
|
|
|
|
|
|
|
|
|
|
struct ivy_asm_token *s_attrib_name, *s_attrib_value;
|
2024-12-13 12:26:01 +00:00
|
|
|
};
|
|
|
|
|
|
2024-12-14 20:26:04 +00:00
|
|
|
static enum ivy_status push_attrib(struct unit_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_next_scope_attrib[IVY_ASM_ATTRIB_IDENT] = v;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case HASH_INDEX:
|
|
|
|
|
if (!strcmp(s, "index")) {
|
|
|
|
|
state->s_next_scope_attrib[IVY_ASM_ATTRIB_INDEX] = v;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case HASH_RECIPIENT:
|
|
|
|
|
if (!strcmp(s, "recipient")) {
|
|
|
|
|
state->s_next_scope_attrib[IVY_ASM_ATTRIB_RECIPIENT] = v;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case HASH_SELECTOR:
|
|
|
|
|
if (!strcmp(s, "selector")) {
|
|
|
|
|
state->s_next_scope_attrib[IVY_ASM_ATTRIB_SELECTOR] = v;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case HASH_GET:
|
|
|
|
|
if (!strcmp(s, "get")) {
|
|
|
|
|
state->s_next_scope_attrib[IVY_ASM_ATTRIB_GET] = v;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case HASH_SET:
|
|
|
|
|
if (!strcmp(s, "set")) {
|
|
|
|
|
state->s_next_scope_attrib[IVY_ASM_ATTRIB_SET] = v;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case HASH_PACKAGE:
|
|
|
|
|
if (!strcmp(s, "package")) {
|
|
|
|
|
state->s_next_scope_attrib[IVY_ASM_ATTRIB_PACKAGE] = 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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-13 12:26:01 +00:00
|
|
|
static enum ivy_status parse_constpool(struct ivy_asm_parser* ctx, struct ivy_asm_token* tok)
|
|
|
|
|
{
|
2024-12-14 20:26:04 +00:00
|
|
|
struct unit_parser_state *state = (struct unit_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_next_scope = ASM_PARSER_CONSTPOOL;
|
|
|
|
|
memset(state->s_next_scope_attrib, 0x0, sizeof state->s_next_scope_attrib);
|
2024-12-13 12:26:01 +00:00
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum ivy_status parse_import(
|
|
|
|
|
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
|
|
|
|
{
|
2024-12-14 20:26:04 +00:00
|
|
|
struct unit_parser_state *state
|
|
|
|
|
= (struct unit_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_next_scope = ASM_PARSER_IMPORT;
|
|
|
|
|
memset(state->s_next_scope_attrib, 0x0, sizeof state->s_next_scope_attrib);
|
2024-12-13 12:26:01 +00:00
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum ivy_status parse_block(
|
|
|
|
|
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
|
|
|
|
{
|
2024-12-14 20:26:04 +00:00
|
|
|
struct unit_parser_state *state
|
|
|
|
|
= (struct unit_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_next_scope = ASM_PARSER_BLOCK;
|
|
|
|
|
memset(state->s_next_scope_attrib, 0x0, sizeof state->s_next_scope_attrib);
|
2024-12-13 12:26:01 +00:00
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum ivy_status parse_class(
|
|
|
|
|
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
|
|
|
|
{
|
2024-12-14 20:26:04 +00:00
|
|
|
struct unit_parser_state *state
|
|
|
|
|
= (struct unit_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_next_scope = ASM_PARSER_CLASS;
|
|
|
|
|
memset(state->s_next_scope_attrib, 0x0, sizeof state->s_next_scope_attrib);
|
|
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum ivy_status parse_left_bracket(
|
|
|
|
|
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
|
|
|
|
|
{
|
|
|
|
|
struct unit_parser_state *state
|
|
|
|
|
= (struct unit_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 unit_parser_state *state
|
|
|
|
|
= (struct unit_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 unit_parser_state *state
|
|
|
|
|
= (struct unit_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 unit_parser_state *state
|
|
|
|
|
= (struct unit_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 unit_parser_state *state
|
|
|
|
|
= (struct unit_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_next_scope == 0) {
|
|
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
asm_parser_push_state(ctx, state->s_next_scope, state->s_next_scope_attrib);
|
|
|
|
|
state->s_next_scope = 0;
|
|
|
|
|
state->s_prev_token = IVY_ASM_TOK_NONE;
|
|
|
|
|
|
2024-12-13 12:26:01 +00:00
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct parser_state_type unit_parser_state_type = {
|
|
|
|
|
.n_state_size = sizeof(struct unit_parser_state),
|
|
|
|
|
.n_keyword_parsers = {
|
|
|
|
|
KW_PARSER(CONSTPOOL, parse_constpool),
|
|
|
|
|
KW_PARSER(IMPORT, parse_import),
|
|
|
|
|
KW_PARSER(CLASS, parse_class),
|
|
|
|
|
KW_PARSER(BLOCK, parse_block),
|
|
|
|
|
},
|
2024-12-14 20:26:04 +00:00
|
|
|
.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),
|
|
|
|
|
}
|
2024-12-13 12:26:01 +00:00
|
|
|
};
|