2024-12-01 13:25:36 +00:00
|
|
|
#include "block.h"
|
2024-11-26 13:08:39 +00:00
|
|
|
#include "ctx.h"
|
2024-12-01 13:25:36 +00:00
|
|
|
#include "iterate.h"
|
2024-11-26 13:08:39 +00:00
|
|
|
#include "ivy/status.h"
|
|
|
|
|
#include "node.h"
|
|
|
|
|
|
2025-11-06 10:38:32 +00:00
|
|
|
#include <blue/ds/string.h>
|
2024-11-26 13:08:39 +00:00
|
|
|
#include <ivy/lang/lex.h>
|
|
|
|
|
|
|
|
|
|
struct msgh_parser_state {
|
|
|
|
|
struct parser_state s_base;
|
|
|
|
|
bool s_oneline;
|
|
|
|
|
|
|
|
|
|
unsigned int s_i;
|
|
|
|
|
unsigned int s_prev;
|
|
|
|
|
};
|
|
|
|
|
|
2024-11-28 16:57:10 +00:00
|
|
|
static struct token_parse_result parse_linefeed(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok)
|
2024-11-26 13:08:39 +00:00
|
|
|
{
|
|
|
|
|
struct msgh_parser_state *state
|
|
|
|
|
= parser_get_state(ctx, struct msgh_parser_state);
|
|
|
|
|
|
|
|
|
|
struct ivy_ast_msgh_node *msgh
|
|
|
|
|
= (struct ivy_ast_msgh_node *)state->s_base.s_node;
|
|
|
|
|
|
2024-11-28 16:57:10 +00:00
|
|
|
if (state->s_oneline) {
|
|
|
|
|
if (msgh->n_body) {
|
|
|
|
|
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
|
|
|
|
return PARSE_RESULT(IVY_OK, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-27 22:29:29 +00:00
|
|
|
if (!msgh->n_sel) {
|
|
|
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
2024-11-26 13:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
2024-12-01 13:25:36 +00:00
|
|
|
struct block_parser_state *block_state
|
2024-12-04 16:35:19 +00:00
|
|
|
= (struct block_parser_state *)parser_push_state(
|
|
|
|
|
ctx, IVY_AST_BLOCK, 0);
|
2025-01-16 13:15:18 +00:00
|
|
|
block_add_terminator(block_state, IVY_SYM_BANG);
|
2024-11-26 13:08:39 +00:00
|
|
|
|
2024-11-28 16:57:10 +00:00
|
|
|
return PARSE_RESULT(IVY_OK, 0);
|
|
|
|
|
}
|
2024-11-28 10:26:53 +00:00
|
|
|
|
|
|
|
|
static struct token_parse_result parse_bang(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok)
|
|
|
|
|
{
|
|
|
|
|
struct msgh_parser_state *state
|
|
|
|
|
= parser_get_state(ctx, struct msgh_parser_state);
|
|
|
|
|
|
|
|
|
|
struct ivy_ast_msgh_node *msgh
|
|
|
|
|
= (struct ivy_ast_msgh_node *)state->s_base.s_node;
|
|
|
|
|
|
|
|
|
|
if (!msgh->n_sel || state->s_oneline) {
|
|
|
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
|
|
|
|
return PARSE_RESULT(IVY_OK, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-28 16:57:10 +00:00
|
|
|
static struct token_parse_result parse_pipe(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok)
|
|
|
|
|
{
|
|
|
|
|
struct msgh_parser_state *state
|
|
|
|
|
= parser_get_state(ctx, struct msgh_parser_state);
|
|
|
|
|
|
|
|
|
|
struct ivy_ast_msgh_node *msgh
|
|
|
|
|
= (struct ivy_ast_msgh_node *)state->s_base.s_node;
|
|
|
|
|
|
|
|
|
|
if (!msgh->n_sel || state->s_oneline) {
|
|
|
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state->s_oneline = true;
|
2024-12-04 16:35:19 +00:00
|
|
|
parser_push_state(ctx, IVY_AST_EXPR, 0);
|
2024-11-28 16:57:10 +00:00
|
|
|
return PARSE_RESULT(IVY_OK, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-27 12:56:10 +00:00
|
|
|
static enum ivy_status add_child(
|
2024-12-02 07:56:27 +00:00
|
|
|
struct parser_state *parent, struct ivy_ast_node *child)
|
2024-11-26 13:08:39 +00:00
|
|
|
{
|
2024-12-02 07:56:27 +00:00
|
|
|
struct ivy_ast_msgh_node *msgh = (struct ivy_ast_msgh_node *)parent->s_node;
|
2024-11-26 13:08:39 +00:00
|
|
|
|
|
|
|
|
if (child->n_type == IVY_AST_SELECTOR && !msgh->n_sel) {
|
|
|
|
|
msgh->n_sel = (struct ivy_ast_selector_node *)child;
|
2024-11-27 12:56:10 +00:00
|
|
|
return IVY_OK;
|
2024-11-26 13:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-28 16:57:10 +00:00
|
|
|
if (!msgh->n_body) {
|
2024-11-28 10:26:53 +00:00
|
|
|
msgh->n_body = child;
|
|
|
|
|
return IVY_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-27 12:56:10 +00:00
|
|
|
return IVY_OK;
|
2024-11-26 13:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
2024-12-06 09:53:09 +00:00
|
|
|
static void init_state(struct ivy_parser *ctx, struct parser_state *sp, uintptr_t arg)
|
2024-11-26 13:08:39 +00:00
|
|
|
{
|
|
|
|
|
struct msgh_parser_state *state = (struct msgh_parser_state *)sp;
|
|
|
|
|
state->s_oneline = false;
|
|
|
|
|
state->s_i = 0;
|
|
|
|
|
state->s_prev = IVY_SYM_HYPHEN;
|
|
|
|
|
|
2024-12-04 16:35:19 +00:00
|
|
|
parser_push_state(ctx, IVY_AST_SELECTOR, 0);
|
2024-11-26 13:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-28 16:57:10 +00:00
|
|
|
static void collect_children(
|
|
|
|
|
struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator)
|
|
|
|
|
{
|
|
|
|
|
struct ivy_ast_msgh_node *msgh = (struct ivy_ast_msgh_node *)node;
|
|
|
|
|
ast_node_iterator_enqueue_node(iterator, node, &msgh->n_sel->n_base);
|
2024-12-01 13:25:36 +00:00
|
|
|
ast_node_iterator_enqueue_node(iterator, node, msgh->n_body);
|
2024-11-28 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-26 13:08:39 +00:00
|
|
|
struct ast_node_type msgh_node_ops = {
|
|
|
|
|
.n_add_child = add_child,
|
2024-11-29 12:06:06 +00:00
|
|
|
.n_init_state = init_state,
|
2024-11-28 16:57:10 +00:00
|
|
|
.n_collect_children = collect_children,
|
2024-11-29 12:06:06 +00:00
|
|
|
.n_state_size = sizeof(struct msgh_parser_state),
|
|
|
|
|
.n_node_size = sizeof(struct ivy_ast_msgh_node),
|
2024-11-28 16:57:10 +00:00
|
|
|
.n_token_parsers = {
|
|
|
|
|
TOK_PARSER(LINEFEED, parse_linefeed),
|
2024-11-28 10:26:53 +00:00
|
|
|
},
|
|
|
|
|
.n_symbol_parsers = {
|
2024-11-28 10:56:43 +00:00
|
|
|
SYM_PARSER(BANG, parse_bang),
|
2024-11-28 16:57:10 +00:00
|
|
|
SYM_PARSER(PIPE, parse_pipe),
|
2024-11-28 10:26:53 +00:00
|
|
|
},
|
2024-11-26 13:08:39 +00:00
|
|
|
};
|