Files
ivy/lang/ast/selector.c

191 lines
5.0 KiB
C

#include "ctx.h"
#include "ivy/status.h"
#include "node.h"
#include <blue/object/string.h>
#include <ivy/lang/lex.h>
struct selector_parser_state {
struct parser_state s_base;
unsigned int s_prev;
bool s_complete;
};
#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);
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;
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;
state->s_complete = true;
return PARSE_RESULT(IVY_OK, 0);
}
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
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 PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
b_queue_push_back(&sel->n_arg_labels, &tok->t_entry);
state->s_prev = IVY_TOK_LABEL;
state->s_complete = false;
return PARSE_RESULT(IVY_OK, 0);
}
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);
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 PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
if (state->s_prev != IVY_TOK_IDENT) {
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
state->s_prev = IVY_TOK_LINEFEED;
return PARSE_RESULT(IVY_OK, 0);
}
static struct token_parse_result parse_left_paren(
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 || !b_queue_empty(&sel->n_arg_labels)) {
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
state->s_prev = IVY_SYM_LEFT_PAREN;
return PARSE_RESULT(IVY_OK, 0);
}
static struct token_parse_result parse_right_paren(
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 || b_queue_empty(&sel->n_arg_labels)) {
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
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 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);
struct ivy_ast_selector_node *sel
= (struct ivy_ast_selector_node *)state->s_base.s_node;
if (!b_queue_empty(&sel->n_arg_labels)
&& state->s_prev != IVY_SYM_RIGHT_PAREN) {
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
if (state->s_prev != IVY_TOK_IDENT) {
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
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 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,
[IVY_SYM_RIGHT_PAREN] = parse_right_paren,
[IVY_SYM_PIPE] = parse_pipe,
},
};