lang: ast: re-write expression parser to support keyword messages

also adjust some parser state callbackss to better support sub-parsers returning
 results to their parents.
This commit is contained in:
2024-12-02 07:56:27 +00:00
parent 1c73e1d37b
commit 1c5b23d968
11 changed files with 605 additions and 613 deletions

View File

@@ -1,11 +1,11 @@
#include "ctx.h"
#include "node.h"
#include "block.h"
#include "ctx.h"
#include "iterate.h"
#include "node.h"
#include <blue/object/string.h>
#include <ivy/lang/lex.h>
#include <stdio.h>
static struct token_parse_result parse_end(
struct ivy_parser *ctx, struct ivy_token *tok)
@@ -21,7 +21,7 @@ static struct token_parse_result parse_end(
}
static struct token_parse_result parse_bang(
struct ivy_parser* ctx, struct ivy_token* tok)
struct ivy_parser *ctx, struct ivy_token *tok)
{
struct block_parser_state *state
= parser_get_state(ctx, struct block_parser_state);
@@ -51,9 +51,10 @@ static struct token_parse_result parse_expr_begin(
}
static enum ivy_status add_child(
struct ivy_ast_node *parent, struct ivy_ast_node *child)
struct parser_state *parent, struct ivy_ast_node *child)
{
struct ivy_ast_block_node *block = (struct ivy_ast_block_node *)parent;
struct ivy_ast_block_node *block
= (struct ivy_ast_block_node *)parent->s_node;
b_queue_push_back(&block->n_expr, &child->n_entry);
@@ -70,7 +71,7 @@ static void collect_children(
{
struct ivy_ast_block_node *block = (struct ivy_ast_block_node *)node;
b_queue_iterator it = {0};
b_queue_foreach(&it, &block->n_expr) {
b_queue_foreach (&it, &block->n_expr) {
struct ivy_ast_node *expr
= b_unbox(struct ivy_ast_node, it.entry, n_entry);
ast_node_iterator_enqueue_node(iterator, node, expr);

View File

@@ -1,6 +1,6 @@
#include "ctx.h"
#include "node.h"
#include "iterate.h"
#include "node.h"
#include <blue/object/string.h>
#include <ivy/lang/lex.h>
@@ -15,7 +15,8 @@ struct class_parser_state {
int s_prev_token;
};
static struct token_parse_result 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);
@@ -123,9 +124,9 @@ static struct token_parse_result parse_linefeed(
}
static enum ivy_status add_child(
struct ivy_ast_node *parent, struct ivy_ast_node *child)
struct parser_state *state, struct ivy_ast_node *child)
{
struct ivy_ast_class_node *c = (struct ivy_ast_class_node *)parent;
struct ivy_ast_class_node *c = (struct ivy_ast_class_node *)state->s_node;
switch (child->n_type) {
case IVY_AST_MSGH:

View File

@@ -5,9 +5,21 @@
#include <blue/core/queue.h>
#include <ivy/lang/ast.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void print_state_stack(struct ivy_parser *parser)
{
b_queue_iterator it = {0};
b_queue_foreach (&it, &parser->p_state) {
struct parser_state *state
= b_unbox(struct parser_state, it.entry, s_entry);
printf(" %s\n",
ivy_ast_node_type_to_string(state->s_node->n_type));
}
}
enum ivy_status ivy_parser_create(struct ivy_parser **parser)
{
struct ivy_parser *out = malloc(sizeof *out);
@@ -48,6 +60,9 @@ enum ivy_status ivy_parser_push_token(
struct token_parse_result result = func(parser, tok);
parser->p_status = result.r_status;
printf("states (after token)\n");
print_state_stack(parser);
if (result.r_flags & PARSE_REPEAT_TOKEN) {
continue;
}
@@ -108,6 +123,8 @@ struct parser_state *parser_push_state(
node_type->n_init_state(parser, state);
}
printf("states (after push)\n");
print_state_stack(parser);
return state;
}
@@ -122,13 +139,17 @@ void parser_pop_state(struct ivy_parser *parser, enum pop_state_flags flags)
b_queue_pop_back(&parser->p_state);
if (state && (flags & STATE_ADD_NODE_TO_PARENT)) {
ast_node_add_child(state->s_parent, state->s_node);
parser_add_child(parser, state->s_node);
}
free(state);
printf("states (after pop)\n");
print_state_stack(parser);
}
void parser_replace_current_node(struct ivy_parser *parser, struct ivy_ast_node *new_node)
void parser_replace_current_node(
struct ivy_parser *parser, struct ivy_ast_node *new_node)
{
struct parser_state *state = parser_get_state_generic(parser);
if (!state) {
@@ -139,6 +160,23 @@ void parser_replace_current_node(struct ivy_parser *parser, struct ivy_ast_node
state->s_node = new_node;
}
enum ivy_status parser_add_child(
struct ivy_parser *parser, struct ivy_ast_node *new_node)
{
struct parser_state *state = parser_get_state_generic(parser);
if (!state) {
return IVY_ERR_NOT_SUPPORTED;
}
const struct ast_node_type *node_type
= get_ast_node_type(state->s_node->n_type);
if (!node_type || !node_type->n_add_child) {
return IVY_ERR_NOT_SUPPORTED;
}
return node_type->n_add_child(state, new_node);
}
bool ivy_parser_is_node_complete(struct ivy_parser *parser)
{
return (parser->p_state.q_first == parser->p_state.q_last);

View File

@@ -30,6 +30,9 @@ extern struct parser_state *parser_push_state(
extern void parser_pop_state(struct ivy_parser *parser, enum pop_state_flags flags);
extern struct parser_state *parser_get_state_generic(struct ivy_parser *parser);
extern void parser_replace_current_node(struct ivy_parser *parser, struct ivy_ast_node *new_node);
extern void parser_replace_current_node(
struct ivy_parser *parser, struct ivy_ast_node *new_node);
extern enum ivy_status parser_add_child(
struct ivy_parser *parser, struct ivy_ast_node *new_node);
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -80,9 +80,9 @@ static struct token_parse_result parse_pipe(
}
static enum ivy_status add_child(
struct ivy_ast_node *parent, struct ivy_ast_node *child)
struct parser_state *parent, struct ivy_ast_node *child)
{
struct ivy_ast_msgh_node *msgh = (struct ivy_ast_msgh_node *)parent;
struct ivy_ast_msgh_node *msgh = (struct ivy_ast_msgh_node *)parent->s_node;
if (child->n_type == IVY_AST_SELECTOR && !msgh->n_sel) {
msgh->n_sel = (struct ivy_ast_selector_node *)child;

View File

@@ -170,24 +170,6 @@ struct ivy_ast_node *ast_node_create(enum ivy_ast_node_type type)
return node;
}
enum ivy_status ast_node_add_child(
struct ivy_ast_node *parent, struct ivy_ast_node *child)
{
const struct ast_node_type *ops = get_ast_node_type(parent->n_type);
if (!ops) {
return IVY_ERR_NOT_SUPPORTED;
}
enum ivy_status (*add_child)(struct ivy_ast_node *, struct ivy_ast_node *)
= ops->n_add_child;
if (!add_child) {
return IVY_ERR_NOT_SUPPORTED;
}
return add_child(parent, child);
}
static enum ivy_status node_print(
struct ivy_ast_node *node, struct ivy_ast_node_iterator *it)
{

View File

@@ -6,8 +6,8 @@
struct parser_state;
#define PARSE_RESULT(status, flags) \
((struct token_parse_result) {.r_status = (status), .r_flags = (flags)})
#define PARSE_RESULT(status, flags) \
((struct token_parse_result) { .r_status = (status), .r_flags = (flags) })
#define __TOK_PARSER_INDEX(x) ((x) - __IVY_TOK_INDEX_BASE)
#define __SYM_PARSER_INDEX(x) ((x) - __IVY_SYM_INDEX_BASE)
@@ -26,51 +26,51 @@ struct parser_state;
#define KW_PARSER_FALLBACK(func) [__KW_PARSER_FALLBACK_INDEX] = func
enum token_parse_flags {
PARSE_REPEAT_TOKEN = 0x01u,
PARSE_REPEAT_TOKEN = 0x01u,
};
enum token_expr_type {
TOK_EXPR_NONE = 0,
TOK_EXPR_BEGIN,
TOK_EXPR_ANY,
TOK_EXPR_NONE = 0,
TOK_EXPR_BEGIN,
TOK_EXPR_ANY,
};
struct token_parse_result {
enum ivy_status r_status;
enum token_parse_flags r_flags;
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 ivy_parser*, struct ivy_token*);
struct ast_node_type {
enum ivy_status (*n_add_child)(
struct ivy_ast_node *, struct ivy_ast_node *);
void (*n_print)(struct ivy_ast_node *);
void (*n_init_state)(struct ivy_parser *, struct parser_state *);
void (*n_collect_children)(
struct ivy_ast_node *, struct ivy_ast_node_iterator *);
enum ivy_status (*n_add_child)(
struct parser_state*, struct ivy_ast_node*);
void (*n_print)(struct ivy_ast_node*);
void (*n_init_state)(struct ivy_parser*, struct parser_state*);
void (*n_collect_children)(
struct ivy_ast_node*, struct ivy_ast_node_iterator*);
size_t n_state_size;
size_t n_node_size;
size_t n_state_size;
size_t n_node_size;
token_parse_function n_token_parsers[__TOK_PARSER_INDEX(__IVY_TOK_INDEX_LIMIT)];
token_parse_function n_keyword_parsers[__KW_PARSER_INDEX(__IVY_KW_INDEX_LIMIT)];
token_parse_function n_symbol_parsers[__SYM_PARSER_INDEX(__IVY_SYM_INDEX_LIMIT)];
token_parse_function n_token_parsers[__TOK_PARSER_INDEX(__IVY_TOK_INDEX_LIMIT)];
token_parse_function n_keyword_parsers[__KW_PARSER_INDEX(__IVY_KW_INDEX_LIMIT)];
token_parse_function n_symbol_parsers[__SYM_PARSER_INDEX(__IVY_SYM_INDEX_LIMIT)];
struct {
token_parse_function expr_begin;
token_parse_function expr_other;
token_parse_function expr_all;
} n_expr_parser;
struct {
token_parse_function expr_begin;
token_parse_function expr_other;
token_parse_function expr_all;
} n_expr_parser;
};
extern const struct ast_node_type *get_ast_node_type(enum ivy_ast_node_type type);
extern const struct ast_node_type* get_ast_node_type(enum ivy_ast_node_type type);
extern token_parse_function get_token_parser(
struct ivy_ast_node *context, struct ivy_token *tok);
extern enum token_expr_type get_token_expr_type(struct ivy_token *tok);
extern struct ivy_ast_node *ast_node_create(enum ivy_ast_node_type type);
struct ivy_ast_node* context, struct ivy_token* tok);
extern enum token_expr_type get_token_expr_type(struct ivy_token* tok);
extern struct ivy_ast_node* ast_node_create(enum ivy_ast_node_type type);
extern enum ivy_status ast_node_add_child(
struct ivy_ast_node *parent, struct ivy_ast_node *child);
struct ivy_ast_node* parent, struct ivy_ast_node* child);
#endif

View File

@@ -10,7 +10,8 @@ struct unit_import_parser_state {
int s_prev_token;
};
static struct token_parse_result 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);
@@ -56,12 +57,6 @@ static struct token_parse_result parse_linefeed(
return PARSE_RESULT(IVY_OK, 0);
}
static enum ivy_status add_child(
struct ivy_ast_node *parent, struct ivy_ast_node *child)
{
return IVY_ERR_NOT_SUPPORTED;
}
static void print(struct ivy_ast_node *node)
{
struct ivy_ast_unit_import_node *unit_import
@@ -93,7 +88,6 @@ static void init_state(struct ivy_parser *ctx, struct parser_state *sp)
}
struct ast_node_type unit_import_node_ops = {
.n_add_child = add_child,
.n_print = print,
.n_init_state = init_state,
.n_state_size = sizeof(struct unit_import_parser_state),

View File

@@ -57,12 +57,6 @@ static struct token_parse_result parse_linefeed(
return PARSE_RESULT(IVY_OK, 0);
}
static enum ivy_status add_child(
struct ivy_ast_node *parent, struct ivy_ast_node *child)
{
return IVY_ERR_NOT_SUPPORTED;
}
static void print(struct ivy_ast_node *node)
{
struct ivy_ast_unit_package_node *unit_package
@@ -94,7 +88,6 @@ static void init_state(struct ivy_parser *ctx, struct parser_state *sp)
}
struct ast_node_type unit_package_node_ops = {
.n_add_child = add_child,
.n_print = print,
.n_init_state = init_state,
.n_state_size = sizeof(struct unit_package_parser_state),
@@ -107,4 +100,4 @@ struct ast_node_type unit_package_node_ops = {
TOK_PARSER(LINEFEED, parse_linefeed),
}
};
};

View File

@@ -33,9 +33,9 @@ static struct token_parse_result parse_expr_begin(
}
static enum ivy_status add_child(
struct ivy_ast_node *parent, struct ivy_ast_node *child)
struct parser_state *parent, struct ivy_ast_node *child)
{
struct ivy_ast_unit_node *unit = (struct ivy_ast_unit_node *)parent;
struct ivy_ast_unit_node *unit = (struct ivy_ast_unit_node *)parent->s_node;
b_queue_push_back(&unit->n_children, &child->n_entry);
return IVY_OK;
}