2024-11-25 21:31:52 +00:00
|
|
|
#include "ctx.h"
|
|
|
|
|
#include "node.h"
|
2024-11-28 22:04:22 +00:00
|
|
|
#include "iterate.h"
|
2024-11-25 21:31:52 +00:00
|
|
|
|
2024-11-25 16:50:42 +00:00
|
|
|
#include <blue/object/string.h>
|
|
|
|
|
#include <ivy/lang/lex.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
struct class_parser_state {
|
|
|
|
|
struct parser_state s_base;
|
|
|
|
|
enum {
|
|
|
|
|
AREA_IDENT,
|
|
|
|
|
AREA_BODY,
|
|
|
|
|
} s_current_area;
|
|
|
|
|
int s_prev_token;
|
|
|
|
|
};
|
|
|
|
|
|
2024-11-26 21:30:40 +00:00
|
|
|
static struct token_parse_result parse_dollar(struct ivy_parser *ctx, struct ivy_token *tok)
|
2024-11-26 13:08:39 +00:00
|
|
|
{
|
|
|
|
|
struct class_parser_state *state
|
|
|
|
|
= parser_get_state(ctx, struct class_parser_state);
|
|
|
|
|
|
|
|
|
|
if (state->s_current_area != AREA_BODY) {
|
2024-11-26 21:30:40 +00:00
|
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
2024-11-26 13:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parser_push_state(ctx, IVY_AST_PROPERTY);
|
2024-11-26 21:30:40 +00:00
|
|
|
return PARSE_RESULT(IVY_OK, 0);
|
2024-11-26 13:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-26 21:30:40 +00:00
|
|
|
static struct token_parse_result parse_plus(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok)
|
2024-11-26 13:08:39 +00:00
|
|
|
{
|
|
|
|
|
struct class_parser_state *state
|
|
|
|
|
= parser_get_state(ctx, struct class_parser_state);
|
|
|
|
|
|
|
|
|
|
if (state->s_current_area != AREA_BODY) {
|
2024-11-26 21:30:40 +00:00
|
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
2024-11-26 13:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parser_push_state(ctx, IVY_AST_MSGH);
|
|
|
|
|
struct parser_state *msgh_state = parser_get_state_generic(ctx);
|
|
|
|
|
struct ivy_ast_msgh_node *msgh
|
|
|
|
|
= (struct ivy_ast_msgh_node *)msgh_state->s_node;
|
|
|
|
|
|
2024-11-28 16:58:01 +00:00
|
|
|
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
|
2024-11-26 13:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-26 21:30:40 +00:00
|
|
|
static struct token_parse_result parse_hyphen(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok)
|
2024-11-26 13:08:39 +00:00
|
|
|
{
|
|
|
|
|
struct class_parser_state *state
|
|
|
|
|
= parser_get_state(ctx, struct class_parser_state);
|
|
|
|
|
|
|
|
|
|
if (state->s_current_area != AREA_BODY) {
|
2024-11-26 21:30:40 +00:00
|
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
2024-11-26 13:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parser_push_state(ctx, IVY_AST_MSGH);
|
|
|
|
|
struct parser_state *msgh_state = parser_get_state_generic(ctx);
|
|
|
|
|
struct ivy_ast_msgh_node *msgh
|
|
|
|
|
= (struct ivy_ast_msgh_node *)msgh_state->s_node;
|
|
|
|
|
|
2024-11-28 16:58:01 +00:00
|
|
|
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
|
2024-11-26 13:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-26 21:30:40 +00:00
|
|
|
static struct token_parse_result parse_ident(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok)
|
2024-11-25 16:50:42 +00:00
|
|
|
{
|
|
|
|
|
struct class_parser_state *state
|
2024-11-25 21:31:52 +00:00
|
|
|
= parser_get_state(ctx, struct class_parser_state);
|
2024-11-25 16:50:42 +00:00
|
|
|
|
|
|
|
|
struct ivy_ast_class_node *node
|
|
|
|
|
= (struct ivy_ast_class_node *)(state->s_base.s_node);
|
|
|
|
|
|
|
|
|
|
if (state->s_current_area != AREA_IDENT) {
|
2024-11-26 21:30:40 +00:00
|
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
2024-11-25 16:50:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (state->s_prev_token == IVY_TOK_IDENT) {
|
2024-11-26 21:30:40 +00:00
|
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
2024-11-25 16:50:42 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-25 21:31:52 +00:00
|
|
|
node->n_ident = tok;
|
2024-11-25 16:50:42 +00:00
|
|
|
state->s_prev_token = IVY_TOK_IDENT;
|
2024-11-26 21:30:40 +00:00
|
|
|
return PARSE_RESULT(IVY_OK, 0);
|
2024-11-25 16:50:42 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-26 21:30:40 +00:00
|
|
|
static struct token_parse_result parse_end(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok)
|
2024-11-25 16:50:42 +00:00
|
|
|
{
|
|
|
|
|
struct class_parser_state *state
|
2024-11-25 21:31:52 +00:00
|
|
|
= parser_get_state(ctx, struct class_parser_state);
|
2024-11-25 16:50:42 +00:00
|
|
|
|
|
|
|
|
struct ivy_ast_class_node *node
|
|
|
|
|
= (struct ivy_ast_class_node *)(state->s_base.s_node);
|
|
|
|
|
|
|
|
|
|
if (state->s_current_area == AREA_IDENT) {
|
2024-11-26 21:30:40 +00:00
|
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
2024-11-25 16:50:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
2024-11-26 21:30:40 +00:00
|
|
|
return PARSE_RESULT(IVY_OK, 0);
|
2024-11-25 16:50:42 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-26 21:30:40 +00:00
|
|
|
static struct token_parse_result parse_linefeed(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok)
|
2024-11-25 16:50:42 +00:00
|
|
|
{
|
|
|
|
|
struct class_parser_state *state
|
2024-11-25 21:31:52 +00:00
|
|
|
= parser_get_state(ctx, struct class_parser_state);
|
2024-11-25 16:50:42 +00:00
|
|
|
|
|
|
|
|
if (state->s_current_area != AREA_IDENT) {
|
2024-11-26 21:30:40 +00:00
|
|
|
return PARSE_RESULT(IVY_OK, 0);
|
2024-11-25 16:50:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (state->s_prev_token != IVY_TOK_IDENT) {
|
2024-11-26 21:30:40 +00:00
|
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
2024-11-25 16:50:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state->s_prev_token = IVY_TOK_LINEFEED;
|
|
|
|
|
state->s_current_area = AREA_BODY;
|
2024-11-26 21:30:40 +00:00
|
|
|
return PARSE_RESULT(IVY_OK, 0);
|
2024-11-25 16:50:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum ivy_status add_child(
|
|
|
|
|
struct ivy_ast_node *parent, struct ivy_ast_node *child)
|
|
|
|
|
{
|
|
|
|
|
struct ivy_ast_class_node *c = (struct ivy_ast_class_node *)parent;
|
2024-11-25 21:31:52 +00:00
|
|
|
|
2024-11-25 16:50:42 +00:00
|
|
|
switch (child->n_type) {
|
|
|
|
|
case IVY_AST_MSGH:
|
|
|
|
|
b_queue_push_back(&c->n_msg_handlers, &child->n_entry);
|
|
|
|
|
break;
|
|
|
|
|
case IVY_AST_PROPERTY:
|
|
|
|
|
b_queue_push_back(&c->n_properties, &child->n_entry);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return IVY_ERR_NOT_SUPPORTED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void print(struct ivy_ast_node *node)
|
|
|
|
|
{
|
2024-11-25 21:31:52 +00:00
|
|
|
struct ivy_ast_class_node *c = (struct ivy_ast_class_node *)node;
|
2024-11-25 16:50:42 +00:00
|
|
|
|
2024-11-25 21:31:52 +00:00
|
|
|
printf("%s(%s)\n", ivy_ast_node_type_to_string(node->n_type),
|
|
|
|
|
c->n_ident->t_str);
|
2024-11-25 16:50:42 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-26 13:08:39 +00:00
|
|
|
static void init_state(struct ivy_parser *ctx, struct parser_state *sp)
|
2024-11-25 16:50:42 +00:00
|
|
|
{
|
2024-11-25 21:31:52 +00:00
|
|
|
struct class_parser_state *state = (struct class_parser_state *)sp;
|
2024-11-25 16:50:42 +00:00
|
|
|
state->s_prev_token = IVY_KW_CLASS;
|
|
|
|
|
state->s_current_area = AREA_IDENT;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-28 16:58:01 +00:00
|
|
|
static void collect_children(
|
|
|
|
|
struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator)
|
|
|
|
|
{
|
|
|
|
|
struct ivy_ast_class_node *c = (struct ivy_ast_class_node *)node;
|
|
|
|
|
b_queue_iterator it = {0};
|
|
|
|
|
b_queue_foreach (&it, &c->n_properties) {
|
|
|
|
|
struct ivy_ast_node *child
|
|
|
|
|
= b_unbox(struct ivy_ast_node, it.entry, n_entry);
|
|
|
|
|
ast_node_iterator_enqueue_node(iterator, node, child);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b_queue_foreach (&it, &c->n_msg_handlers) {
|
|
|
|
|
struct ivy_ast_node *child
|
|
|
|
|
= b_unbox(struct ivy_ast_node, it.entry, n_entry);
|
|
|
|
|
ast_node_iterator_enqueue_node(iterator, node, child);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-25 16:50:42 +00:00
|
|
|
struct ast_node_type class_node_ops = {
|
|
|
|
|
.n_add_child = add_child,
|
|
|
|
|
.n_print = print,
|
|
|
|
|
.n_init_state = init_state,
|
2024-11-28 16:58:01 +00:00
|
|
|
.n_collect_children = collect_children,
|
2024-11-25 16:50:42 +00:00
|
|
|
.n_state_size = sizeof(struct class_parser_state),
|
|
|
|
|
.n_node_size = sizeof(struct ivy_ast_class_node),
|
2024-11-26 13:08:39 +00:00
|
|
|
.n_symbol_parsers = {
|
2024-11-28 10:56:43 +00:00
|
|
|
SYM_PARSER(DOLLAR, parse_dollar),
|
|
|
|
|
SYM_PARSER(HYPHEN, parse_hyphen),
|
|
|
|
|
SYM_PARSER(PLUS, parse_plus),
|
2024-11-26 13:08:39 +00:00
|
|
|
},
|
2024-11-25 16:50:42 +00:00
|
|
|
.n_token_parsers = {
|
2024-11-28 10:56:43 +00:00
|
|
|
TOK_PARSER(IDENT, parse_ident),
|
|
|
|
|
TOK_PARSER(LINEFEED, parse_linefeed),
|
2024-11-25 16:50:42 +00:00
|
|
|
},
|
|
|
|
|
.n_keyword_parsers = {
|
2024-11-28 10:56:43 +00:00
|
|
|
KW_PARSER(END, parse_end),
|
2024-11-25 16:50:42 +00:00
|
|
|
},
|
|
|
|
|
};
|