lang: ast: initial implementation of class parser
This commit is contained in:
148
lang/ast/class.c
Normal file
148
lang/ast/class.c
Normal file
@@ -0,0 +1,148 @@
|
||||
#include <blue/object/string.h>
|
||||
#include <ivy/lang/lex.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "ctx.h"
|
||||
#include "node.h"
|
||||
|
||||
struct class_parser_state {
|
||||
struct parser_state s_base;
|
||||
enum {
|
||||
AREA_IDENT,
|
||||
AREA_BODY,
|
||||
} s_current_area;
|
||||
int s_prev_token;
|
||||
};
|
||||
|
||||
static enum ivy_status parse_dot(struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct class_parser_state *state
|
||||
= parser_get_state(ctx, struct unit_import_parser_state);
|
||||
|
||||
if (state->s_prev_token != IVY_TOK_IDENT) {
|
||||
return IVY_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
state->s_prev_token = IVY_SYM_DOT;
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
static enum ivy_status parse_ident(struct ivy_parser* ctx, struct ivy_token* tok)
|
||||
{
|
||||
struct class_parser_state *state
|
||||
= parser_get_state(ctx, struct unit_import_parser_state);
|
||||
|
||||
struct ivy_ast_class_node *node
|
||||
= (struct ivy_ast_class_node *)(state->s_base.s_node);
|
||||
|
||||
if (state->s_current_area != AREA_IDENT) {
|
||||
return IVY_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
if (state->s_prev_token == IVY_TOK_IDENT) {
|
||||
return IVY_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
b_queue_push_back(&node->n_ident, &tok->t_entry);
|
||||
state->s_prev_token = IVY_TOK_IDENT;
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
static enum ivy_status parse_end(struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct class_parser_state *state
|
||||
= parser_get_state(ctx, struct unit_import_parser_state);
|
||||
|
||||
struct ivy_ast_class_node *node
|
||||
= (struct ivy_ast_class_node *)(state->s_base.s_node);
|
||||
|
||||
if (state->s_current_area == AREA_IDENT) {
|
||||
return IVY_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
static enum ivy_status parse_linefeed(struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct class_parser_state *state
|
||||
= parser_get_state(ctx, struct unit_import_parser_state);
|
||||
|
||||
if (state->s_current_area != AREA_IDENT) {
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
if (state->s_prev_token != IVY_TOK_IDENT) {
|
||||
return IVY_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
state->s_prev_token = IVY_TOK_LINEFEED;
|
||||
state->s_current_area = AREA_BODY;
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
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)
|
||||
{
|
||||
struct ivy_ast_class_node *unit_import = (struct ivy_ast_class_node *)node;
|
||||
b_string *str = b_string_create();
|
||||
|
||||
b_queue_iterator it = {0};
|
||||
b_queue_foreach (&it, &unit_import->n_ident) {
|
||||
struct ivy_token *tok = b_unbox(struct ivy_token, it.entry, t_entry);
|
||||
|
||||
if (b_string_get_size(str, B_STRLEN_NORMAL) > 0) {
|
||||
b_string_append_cstr(str, ".");
|
||||
}
|
||||
|
||||
b_string_append_cstr(str, tok->t_str);
|
||||
}
|
||||
|
||||
printf("%s(%s)\n", ivy_ast_node_type_to_string(node->n_type), b_string_ptr(str));
|
||||
b_string_release(str);
|
||||
}
|
||||
|
||||
static void init_state(struct parser_state *sp)
|
||||
{
|
||||
struct class_parser_state *state
|
||||
= (struct unit_import_parser_state *)sp;
|
||||
state->s_prev_token = IVY_KW_CLASS;
|
||||
state->s_current_area = AREA_IDENT;
|
||||
}
|
||||
|
||||
struct ast_node_type class_node_ops = {
|
||||
.n_add_child = add_child,
|
||||
.n_print = print,
|
||||
.n_init_state = init_state,
|
||||
.n_state_size = sizeof(struct class_parser_state),
|
||||
.n_node_size = sizeof(struct ivy_ast_class_node),
|
||||
.n_symbol_parsers = {
|
||||
[IVY_SYM_DOT] = parse_dot,
|
||||
},
|
||||
.n_token_parsers = {
|
||||
[IVY_TOK_IDENT] = parse_ident,
|
||||
[IVY_TOK_LINEFEED] = parse_linefeed,
|
||||
},
|
||||
.n_keyword_parsers = {
|
||||
[IVY_KW_END] = parse_end,
|
||||
},
|
||||
};
|
||||
@@ -7,10 +7,14 @@
|
||||
|
||||
extern struct ast_node_type unit_node_ops;
|
||||
extern struct ast_node_type unit_package_node_ops;
|
||||
extern struct ast_node_type unit_import_node_ops;
|
||||
extern struct ast_node_type class_node_ops;
|
||||
|
||||
static const struct ast_node_type *node_ops[] = {
|
||||
[IVY_AST_UNIT] = &unit_node_ops,
|
||||
[IVY_AST_UNIT_PACKAGE] = &unit_package_node_ops,
|
||||
[IVY_AST_UNIT_IMPORT] = &unit_import_node_ops,
|
||||
[IVY_AST_CLASS] = &class_node_ops,
|
||||
};
|
||||
static const size_t nr_node_ops = sizeof node_ops / sizeof node_ops[0];
|
||||
|
||||
|
||||
@@ -17,6 +17,13 @@ static enum ivy_status parse_use_keyword(
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
static enum ivy_status parse_class_keyword(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
parser_push_state(ctx, IVY_AST_CLASS);
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
static enum ivy_status add_child(
|
||||
struct ivy_ast_node *parent, struct ivy_ast_node *child)
|
||||
{
|
||||
@@ -42,6 +49,7 @@ struct ast_node_type unit_node_ops = {
|
||||
.n_node_size = sizeof(struct ivy_ast_unit_node),
|
||||
.n_keyword_parsers = {
|
||||
[IVY_KW_PACKAGE] = parse_package_keyword,
|
||||
[IVY_KW_CLASS] = parse_class_keyword,
|
||||
[IVY_KW_USE] = parse_use_keyword,
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user