lang: ast: implement control flags returned by parser functions

This commit is contained in:
2024-11-26 21:30:40 +00:00
parent 740df312a9
commit cd89c20beb
9 changed files with 177 additions and 98 deletions

View File

@@ -14,26 +14,27 @@ struct class_parser_state {
int s_prev_token;
};
static enum ivy_status parse_dollar(struct ivy_parser *ctx, struct ivy_token *tok)
static struct token_parse_result parse_dollar(struct ivy_parser *ctx, struct ivy_token *tok)
{
struct class_parser_state *state
= parser_get_state(ctx, struct class_parser_state);
if (state->s_current_area != AREA_BODY) {
return IVY_ERR_BAD_SYNTAX;
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
parser_push_state(ctx, IVY_AST_PROPERTY);
return IVY_OK;
return PARSE_RESULT(IVY_OK, 0);
}
static enum ivy_status parse_plus(struct ivy_parser *ctx, struct ivy_token *tok)
static struct token_parse_result parse_plus(
struct ivy_parser *ctx, struct ivy_token *tok)
{
struct class_parser_state *state
= parser_get_state(ctx, struct class_parser_state);
if (state->s_current_area != AREA_BODY) {
return IVY_ERR_BAD_SYNTAX;
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
parser_push_state(ctx, IVY_AST_MSGH);
@@ -42,16 +43,17 @@ static enum ivy_status parse_plus(struct ivy_parser *ctx, struct ivy_token *tok)
= (struct ivy_ast_msgh_node *)msgh_state->s_node;
msgh->n_recipient = IVY_AST_MSGH_CLASS;
return IVY_OK;
return PARSE_RESULT(IVY_OK, 0);
}
static enum ivy_status parse_hyphen(struct ivy_parser *ctx, struct ivy_token *tok)
static struct token_parse_result parse_hyphen(
struct ivy_parser *ctx, struct ivy_token *tok)
{
struct class_parser_state *state
= parser_get_state(ctx, struct class_parser_state);
if (state->s_current_area != AREA_BODY) {
return IVY_ERR_BAD_SYNTAX;
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
parser_push_state(ctx, IVY_AST_MSGH);
@@ -60,10 +62,11 @@ static enum ivy_status parse_hyphen(struct ivy_parser *ctx, struct ivy_token *to
= (struct ivy_ast_msgh_node *)msgh_state->s_node;
msgh->n_recipient = IVY_AST_MSGH_OBJECT;
return IVY_OK;
return PARSE_RESULT(IVY_OK, 0);
}
static enum ivy_status parse_ident(struct ivy_parser *ctx, struct ivy_token *tok)
static struct token_parse_result parse_ident(
struct ivy_parser *ctx, struct ivy_token *tok)
{
struct class_parser_state *state
= parser_get_state(ctx, struct class_parser_state);
@@ -72,19 +75,20 @@ static enum ivy_status parse_ident(struct ivy_parser *ctx, struct ivy_token *tok
= (struct ivy_ast_class_node *)(state->s_base.s_node);
if (state->s_current_area != AREA_IDENT) {
return IVY_ERR_BAD_SYNTAX;
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
if (state->s_prev_token == IVY_TOK_IDENT) {
return IVY_ERR_BAD_SYNTAX;
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
node->n_ident = tok;
state->s_prev_token = IVY_TOK_IDENT;
return IVY_OK;
return PARSE_RESULT(IVY_OK, 0);
}
static enum ivy_status parse_end(struct ivy_parser *ctx, struct ivy_token *tok)
static struct token_parse_result parse_end(
struct ivy_parser *ctx, struct ivy_token *tok)
{
struct class_parser_state *state
= parser_get_state(ctx, struct class_parser_state);
@@ -93,29 +97,30 @@ static enum ivy_status parse_end(struct ivy_parser *ctx, struct ivy_token *tok)
= (struct ivy_ast_class_node *)(state->s_base.s_node);
if (state->s_current_area == AREA_IDENT) {
return IVY_ERR_BAD_SYNTAX;
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
return IVY_OK;
return PARSE_RESULT(IVY_OK, 0);
}
static enum ivy_status parse_linefeed(struct ivy_parser *ctx, struct ivy_token *tok)
static struct token_parse_result parse_linefeed(
struct ivy_parser *ctx, struct ivy_token *tok)
{
struct class_parser_state *state
= parser_get_state(ctx, struct class_parser_state);
if (state->s_current_area != AREA_IDENT) {
return IVY_OK;
return PARSE_RESULT(IVY_OK, 0);
}
if (state->s_prev_token != IVY_TOK_IDENT) {
return IVY_ERR_BAD_SYNTAX;
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
state->s_prev_token = IVY_TOK_LINEFEED;
state->s_current_area = AREA_BODY;
return IVY_OK;
return PARSE_RESULT(IVY_OK, 0);
}
static enum ivy_status add_child(

View File

@@ -1,7 +1,6 @@
#include "ctx.h"
#include "node.h"
#include "parse.h"
#include <blue/core/queue.h>
#include <ivy/lang/ast.h>
@@ -37,25 +36,36 @@ enum ivy_status ivy_parser_get_status(struct ivy_parser *parser)
enum ivy_status ivy_parser_push_token(
struct ivy_parser *parser, struct ivy_token *tok)
{
struct parser_state *state = parser_get_state_generic(parser);
if (!state) {
parser->p_status = IVY_ERR_INTERNAL_FAILURE;
return IVY_ERR_INTERNAL_FAILURE;
while (true) {
struct parser_state *state = parser_get_state_generic(parser);
if (!state) {
parser->p_status = IVY_ERR_INTERNAL_FAILURE;
break;
}
token_parse_function func = get_token_parser(state->s_node, tok);
if (func) {
struct token_parse_result result = func(parser, tok);
parser->p_status = result.r_status;
if (result.r_flags & PARSE_REPEAT_TOKEN) {
continue;
}
break;
}
if (tok->t_type == IVY_TOK_LINEFEED) {
ivy_token_destroy(tok);
parser->p_status = IVY_OK;
break;
}
parser->p_status = IVY_ERR_BAD_SYNTAX;
break;
}
token_parse_function func = get_token_parser(state->s_node, tok);
if (func) {
parser->p_status = func(parser, tok);
return parser->p_status;
}
if (tok->t_type == IVY_TOK_LINEFEED) {
ivy_token_destroy(tok);
return IVY_OK;
}
parser->p_status = IVY_ERR_BAD_SYNTAX;
return IVY_ERR_BAD_SYNTAX;
return parser->p_status;
}
struct parser_state *parser_get_state_generic(struct ivy_parser *parser)
@@ -92,12 +102,12 @@ struct parser_state *parser_push_state(
}
state->s_node = ast_node_create_with_size(type, node_type->n_node_size);
b_queue_push_back(&parser->p_state, &state->s_entry);
if (node_type->n_init_state) {
node_type->n_init_state(parser, state);
}
b_queue_push_back(&parser->p_state, &state->s_entry);
return state;
}

View File

@@ -18,7 +18,7 @@ struct msgh_parser_state {
unsigned int s_prev;
};
static enum ivy_status parse_ident(struct ivy_parser *ctx, struct ivy_token *tok)
static struct token_parse_result parse_ident(struct ivy_parser *ctx, struct ivy_token *tok)
{
struct msgh_parser_state *state
= parser_get_state(ctx, struct msgh_parser_state);
@@ -28,29 +28,29 @@ static enum ivy_status parse_ident(struct ivy_parser *ctx, struct ivy_token *tok
if (state->s_current_area == AREA_BODY) {
/* TODO expression parsing */
return IVY_ERR_NOT_SUPPORTED;
return PARSE_RESULT(IVY_ERR_NOT_SUPPORTED, 0);
}
if (state->s_prev == IVY_SYM_HYPHEN) {
/* message name */
return IVY_OK;
return PARSE_RESULT(IVY_OK, 0);
}
return IVY_OK;
return PARSE_RESULT(IVY_OK, 0);
}
static enum ivy_status add_child(
static struct token_parse_result add_child(
struct ivy_ast_node *parent, struct ivy_ast_node *child)
{
struct ivy_ast_msgh_node *msgh = (struct ivy_ast_msgh_node *)parent;
if (child->n_type == IVY_AST_SELECTOR && !msgh->n_sel) {
msgh->n_sel = (struct ivy_ast_selector_node *)child;
return IVY_OK;
return PARSE_RESULT(IVY_OK, 0);
}
b_queue_push_back(&msgh->n_body, &child->n_entry);
return IVY_OK;
return PARSE_RESULT(IVY_OK, 0);
}
static void init_state(struct ivy_parser *ctx, struct parser_state *sp)

View File

@@ -9,12 +9,16 @@ 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;
extern struct ast_node_type msgh_node_ops;
extern struct ast_node_type selector_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,
[IVY_AST_MSGH] = &msgh_node_ops,
[IVY_AST_SELECTOR] = &selector_node_ops,
};
static const size_t nr_node_ops = sizeof node_ops / sizeof node_ops[0];

View File

@@ -9,7 +9,19 @@
struct parser_state;
typedef enum ivy_status (*token_parse_function)(
#define PARSE_RESULT(status, flags) \
((struct token_parse_result) {.r_status = (status), .r_flags = (flags) })
enum token_parse_flags {
PARSE_REPEAT_TOKEN = 0x01u,
};
struct token_parse_result {
enum ivy_status r_status;
enum token_parse_flags r_flags;
};
typedef struct token_parse_result (*token_parse_function)(
struct ivy_parser *, struct ivy_token *);
struct ast_node_type {

View File

@@ -8,9 +8,19 @@
struct selector_parser_state {
struct parser_state s_base;
unsigned int s_prev;
bool s_complete;
};
static enum ivy_status parse_ident(struct ivy_parser *ctx, struct ivy_token *tok)
#define CHECK_SELECTOR_COMPLETE() \
do { \
if (state->s_complete) { \
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT); \
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN); \
} \
} while (0)
static struct token_parse_result parse_ident(
struct ivy_parser *ctx, struct ivy_token *tok)
{
struct selector_parser_state *state
= parser_get_state(ctx, struct selector_parser_state);
@@ -18,40 +28,49 @@ static enum ivy_status parse_ident(struct ivy_parser *ctx, struct ivy_token *tok
struct ivy_ast_selector_node *sel
= (struct ivy_ast_selector_node *)state->s_base.s_node;
CHECK_SELECTOR_COMPLETE();
if (state->s_prev == 0) {
/* message name */
sel->n_msg_name = tok;
state->s_prev = IVY_TOK_IDENT;
return IVY_OK;
state->s_complete = true;
return PARSE_RESULT(IVY_OK, 0);
}
if (state->s_prev == IVY_TOK_LABEL) {
/* internal parameter name */
b_queue_push_back(&sel->n_arg_names, &tok->t_entry);
state->s_prev = IVY_TOK_IDENT;
return IVY_OK;
state->s_complete = true;
return PARSE_RESULT(IVY_OK, 0);
}
return IVY_ERR_BAD_SYNTAX;
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
static enum ivy_status parse_label(struct ivy_parser *ctx, struct ivy_token *tok)
static struct token_parse_result parse_label(
struct ivy_parser *ctx, struct ivy_token *tok)
{
struct selector_parser_state *state
= parser_get_state(ctx, struct selector_parser_state);
struct ivy_ast_selector_node *sel
= (struct ivy_ast_selector_node *)state->s_base.s_node;
if (state->s_prev != IVY_TOK_IDENT && state->s_prev != IVY_SYM_LEFT_PAREN) {
return IVY_ERR_BAD_SYNTAX;
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
b_queue_push_back(&sel->n_arg_labels, &tok->t_entry);
return IVY_OK;
state->s_prev = IVY_TOK_LABEL;
state->s_complete = false;
return PARSE_RESULT(IVY_OK, 0);
}
static enum ivy_status parse_linefeed(struct ivy_parser *ctx, struct ivy_token *tok)
static struct token_parse_result parse_linefeed(
struct ivy_parser *ctx, struct ivy_token *tok)
{
struct selector_parser_state *state
= parser_get_state(ctx, struct selector_parser_state);
@@ -59,20 +78,23 @@ static enum ivy_status parse_linefeed(struct ivy_parser *ctx, struct ivy_token *
struct ivy_ast_selector_node *sel
= (struct ivy_ast_selector_node *)state->s_base.s_node;
CHECK_SELECTOR_COMPLETE();
if (!b_queue_empty(&sel->n_arg_labels)
&& state->s_prev != IVY_SYM_RIGHT_PAREN) {
return IVY_ERR_BAD_SYNTAX;
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
if (state->s_prev != IVY_TOK_IDENT) {
return IVY_ERR_BAD_SYNTAX;
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
return IVY_OK;
state->s_prev = IVY_TOK_LINEFEED;
return PARSE_RESULT(IVY_OK, 0);
}
static enum ivy_status parse_left_paren(
static struct token_parse_result parse_left_paren(
struct ivy_parser *ctx, struct ivy_token *tok)
{
struct selector_parser_state *state
@@ -82,13 +104,14 @@ static enum ivy_status parse_left_paren(
= (struct ivy_ast_selector_node *)state->s_base.s_node;
if (state->s_prev != IVY_TOK_IDENT || !b_queue_empty(&sel->n_arg_labels)) {
return IVY_ERR_BAD_SYNTAX;
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
return IVY_OK;
state->s_prev = IVY_SYM_LEFT_PAREN;
return PARSE_RESULT(IVY_OK, 0);
}
static enum ivy_status parse_right_paren(
static struct token_parse_result parse_right_paren(
struct ivy_parser *ctx, struct ivy_token *tok)
{
struct selector_parser_state *state
@@ -96,15 +119,19 @@ static enum ivy_status parse_right_paren(
struct ivy_ast_selector_node *sel
= (struct ivy_ast_selector_node *)state->s_base.s_node;
if (state->s_prev != IVY_TOK_IDENT || !b_queue_empty(&sel->n_arg_labels)) {
return IVY_ERR_BAD_SYNTAX;
if (state->s_prev != IVY_TOK_IDENT || b_queue_empty(&sel->n_arg_labels)) {
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
return IVY_OK;
state->s_prev = IVY_SYM_RIGHT_PAREN;
state->s_complete = true;
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
return PARSE_RESULT(IVY_OK, 0);
}
static enum ivy_status parse_pipe(struct ivy_parser *ctx, struct ivy_token *tok)
static struct token_parse_result parse_pipe(
struct ivy_parser *ctx, struct ivy_token *tok)
{
struct selector_parser_state *state
= parser_get_state(ctx, struct selector_parser_state);
@@ -114,30 +141,46 @@ static enum ivy_status parse_pipe(struct ivy_parser *ctx, struct ivy_token *tok)
if (!b_queue_empty(&sel->n_arg_labels)
&& state->s_prev != IVY_SYM_RIGHT_PAREN) {
return IVY_ERR_BAD_SYNTAX;
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
if (state->s_prev != IVY_TOK_IDENT) {
return IVY_ERR_BAD_SYNTAX;
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
return IVY_OK;
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
}
static struct token_parse_result parse_other(
struct ivy_parser* ctx, struct ivy_token* tok)
{
struct selector_parser_state *state
= parser_get_state(ctx, struct selector_parser_state);
struct ivy_ast_selector_node *sel
= (struct ivy_ast_selector_node *)state->s_base.s_node;
CHECK_SELECTOR_COMPLETE();
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
static void init_state(struct ivy_parser *ctx, struct parser_state *sp)
{
struct selector_parser_state *state = (struct selector_parser_state *)sp;
state->s_prev = 0;
state->s_complete = false;
}
struct ast_node_type msgh_node_ops = {
struct ast_node_type selector_node_ops = {
.n_init_state = init_state,
.n_state_size = sizeof(struct selector_parser_state),
.n_node_size = sizeof(struct ivy_ast_selector_node),
.n_token_parsers = {
[IVY_TOK_IDENT] = parse_ident,
[IVY_TOK_LABEL] = parse_label,
[IVY_TOK_NONE] = parse_other,
},
.n_symbol_parsers = {
[IVY_SYM_LEFT_PAREN] = parse_left_paren,

View File

@@ -10,20 +10,21 @@ struct unit_import_parser_state {
int s_prev_token;
};
static enum ivy_status parse_dot(struct ivy_parser *ctx, struct ivy_token *tok)
static struct token_parse_result parse_dot(struct ivy_parser *ctx, struct ivy_token *tok)
{
struct unit_import_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;
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
state->s_prev_token = IVY_SYM_DOT;
return IVY_OK;
return PARSE_RESULT(IVY_OK, 0);
}
static enum ivy_status parse_ident(struct ivy_parser *ctx, struct ivy_token *tok)
static struct token_parse_result parse_ident(
struct ivy_parser *ctx, struct ivy_token *tok)
{
struct unit_import_parser_state *state
= parser_get_state(ctx, struct unit_import_parser_state);
@@ -32,26 +33,27 @@ static enum ivy_status parse_ident(struct ivy_parser *ctx, struct ivy_token *tok
= (struct ivy_ast_unit_import_node *)(state->s_base.s_node);
if (state->s_prev_token == IVY_TOK_IDENT) {
return IVY_ERR_BAD_SYNTAX;
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
b_queue_push_back(&node->n_ident, &tok->t_entry);
state->s_prev_token = IVY_TOK_IDENT;
return IVY_OK;
return PARSE_RESULT(IVY_OK, 0);
}
static enum ivy_status parse_linefeed(struct ivy_parser *ctx, struct ivy_token *tok)
static struct token_parse_result parse_linefeed(
struct ivy_parser *ctx, struct ivy_token *tok)
{
struct unit_import_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;
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
return IVY_OK;
return PARSE_RESULT(IVY_OK, 0);
}
static enum ivy_status add_child(

View File

@@ -10,20 +10,22 @@ struct unit_package_parser_state {
int s_prev_token;
};
static enum ivy_status parse_dot(struct ivy_parser *ctx, struct ivy_token *tok)
static struct token_parse_result parse_dot(
struct ivy_parser *ctx, struct ivy_token *tok)
{
struct unit_package_parser_state *state
= parser_get_state(ctx, struct unit_package_parser_state);
if (state->s_prev_token != IVY_TOK_IDENT) {
return IVY_ERR_BAD_SYNTAX;
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
state->s_prev_token = IVY_SYM_DOT;
return IVY_OK;
return PARSE_RESULT(IVY_OK, 0);
}
static enum ivy_status parse_ident(struct ivy_parser *ctx, struct ivy_token *tok)
static struct token_parse_result parse_ident(
struct ivy_parser *ctx, struct ivy_token *tok)
{
struct unit_package_parser_state *state
= parser_get_state(ctx, struct unit_package_parser_state);
@@ -32,26 +34,27 @@ static enum ivy_status parse_ident(struct ivy_parser *ctx, struct ivy_token *tok
= (struct ivy_ast_unit_package_node *)(state->s_base.s_node);
if (state->s_prev_token == IVY_TOK_IDENT) {
return IVY_ERR_BAD_SYNTAX;
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
b_queue_push_back(&node->n_ident, &tok->t_entry);
state->s_prev_token = IVY_TOK_IDENT;
return IVY_OK;
return PARSE_RESULT(IVY_OK, 0);
}
static enum ivy_status parse_linefeed(struct ivy_parser *ctx, struct ivy_token *tok)
static struct token_parse_result parse_linefeed(
struct ivy_parser *ctx, struct ivy_token *tok)
{
struct unit_package_parser_state *state
= parser_get_state(ctx, struct unit_package_parser_state);
if (state->s_prev_token != IVY_TOK_IDENT) {
return IVY_ERR_BAD_SYNTAX;
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
return IVY_OK;
return PARSE_RESULT(IVY_OK, 0);
}
static enum ivy_status add_child(

View File

@@ -3,33 +3,33 @@
#include "node.h"
#include "iterate.h"
static enum ivy_status parse_package_keyword(
static struct token_parse_result parse_package_keyword(
struct ivy_parser *ctx, struct ivy_token *tok)
{
parser_push_state(ctx, IVY_AST_UNIT_PACKAGE);
return IVY_OK;
return PARSE_RESULT(IVY_OK, 0);
}
static enum ivy_status parse_use_keyword(
static struct token_parse_result parse_use_keyword(
struct ivy_parser *ctx, struct ivy_token *tok)
{
parser_push_state(ctx, IVY_AST_UNIT_IMPORT);
return IVY_OK;
return PARSE_RESULT(IVY_OK, 0);
}
static enum ivy_status parse_class_keyword(
static struct token_parse_result parse_class_keyword(
struct ivy_parser *ctx, struct ivy_token *tok)
{
parser_push_state(ctx, IVY_AST_CLASS);
return IVY_OK;
return PARSE_RESULT(IVY_OK, 0);
}
static enum ivy_status add_child(
static struct token_parse_result add_child(
struct ivy_ast_node *parent, struct ivy_ast_node *child)
{
struct ivy_ast_unit_node *unit = (struct ivy_ast_unit_node *)parent;
b_queue_push_back(&unit->n_children, &child->n_entry);
return IVY_OK;
return PARSE_RESULT(IVY_OK, 0);
}
static void collect_children(struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator)