lang: replace stack-based parser with a recursive parser
This commit is contained in:
+9
-149
@@ -1,149 +1,24 @@
|
||||
#include "block.h"
|
||||
|
||||
#include "ctx.h"
|
||||
#include "expr/expr.h"
|
||||
#include "iterate.h"
|
||||
#include "node.h"
|
||||
|
||||
#include <fx/ds/string.h>
|
||||
#include <ivy/lang/lex.h>
|
||||
|
||||
void block_add_terminator(struct block_parser_state *state, unsigned short tok)
|
||||
{
|
||||
if (state->s_nr_terminators < BLOCK_TERMINATOR_MAX) {
|
||||
state->s_terminators[state->s_nr_terminators++] = tok;
|
||||
}
|
||||
}
|
||||
|
||||
void block_copy_terminators(
|
||||
const struct block_parser_state *src, struct block_parser_state *dest)
|
||||
{
|
||||
dest->s_nr_terminators = src->s_nr_terminators;
|
||||
|
||||
for (unsigned int i = 0; i < src->s_nr_terminators; i++) {
|
||||
dest->s_terminators[i] = src->s_terminators[i];
|
||||
}
|
||||
}
|
||||
|
||||
bool block_terminates_at_token(struct block_parser_state *state, unsigned short tok)
|
||||
{
|
||||
for (unsigned int i = 0; i < BLOCK_TERMINATOR_MAX; i++) {
|
||||
if (state->s_terminators[i] == tok) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static struct token_parse_result parse_end(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct block_parser_state *state
|
||||
= parser_get_state(ctx, struct block_parser_state);
|
||||
|
||||
struct ivy_ast_block_node *node
|
||||
= (struct ivy_ast_block_node *)(state->s_base.s_node);
|
||||
|
||||
int flags = 0;
|
||||
if (block_terminates_at_token(state, IVY_KW_END)) {
|
||||
flags = PARSE_REPEAT_TOKEN;
|
||||
}
|
||||
|
||||
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
||||
return PARSE_RESULT(IVY_OK, flags);
|
||||
}
|
||||
|
||||
static struct token_parse_result parse_else(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct block_parser_state *state
|
||||
= parser_get_state(ctx, struct block_parser_state);
|
||||
|
||||
struct ivy_ast_block_node *node
|
||||
= (struct ivy_ast_block_node *)(state->s_base.s_node);
|
||||
|
||||
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
||||
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
|
||||
}
|
||||
|
||||
static struct token_parse_result parse_keyword(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct block_parser_state *state
|
||||
= parser_get_state(ctx, struct block_parser_state);
|
||||
|
||||
struct ivy_ast_block_node *node
|
||||
= (struct ivy_ast_block_node *)(state->s_base.s_node);
|
||||
|
||||
if (block_terminates_at_token(state, tok->t_keyword)) {
|
||||
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
||||
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
|
||||
}
|
||||
|
||||
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
||||
}
|
||||
|
||||
static struct token_parse_result parse_symbol(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct block_parser_state *state
|
||||
= parser_get_state(ctx, struct block_parser_state);
|
||||
|
||||
struct ivy_ast_block_node *node
|
||||
= (struct ivy_ast_block_node *)(state->s_base.s_node);
|
||||
|
||||
if (block_terminates_at_token(state, tok->t_symbol)) {
|
||||
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
||||
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
|
||||
}
|
||||
|
||||
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
||||
}
|
||||
|
||||
static struct token_parse_result parse_global(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
parser_push_state(ctx, IVY_AST_GLOBAL, 0);
|
||||
return PARSE_RESULT(IVY_OK, 0);
|
||||
}
|
||||
|
||||
static struct token_parse_result parse_expr_begin(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct block_parser_state *state
|
||||
= parser_get_state(ctx, struct block_parser_state);
|
||||
|
||||
struct ivy_ast_block_node *node
|
||||
= (struct ivy_ast_block_node *)(state->s_base.s_node);
|
||||
|
||||
struct expr_parser_state *expr
|
||||
= (struct expr_parser_state *)parser_push_state(
|
||||
ctx, IVY_AST_EXPR, 0);
|
||||
|
||||
memcpy(expr->s_terminators, state->s_terminators,
|
||||
sizeof expr->s_terminators);
|
||||
expr->s_nr_terminators = state->s_nr_terminators;
|
||||
|
||||
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
|
||||
}
|
||||
|
||||
static enum ivy_status add_child(
|
||||
struct parser_state *parent, struct ivy_ast_node *child)
|
||||
{
|
||||
struct ivy_ast_block_node *block
|
||||
= (struct ivy_ast_block_node *)parent->s_node;
|
||||
|
||||
fx_queue_push_back(&block->n_expr, &child->n_entry);
|
||||
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
static void collect_children(
|
||||
struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator)
|
||||
{
|
||||
struct ivy_ast_block_node *block = (struct ivy_ast_block_node *)node;
|
||||
fx_queue_entry *entry = fx_queue_first(&block->n_expr);
|
||||
fx_queue_entry *entry = fx_queue_first(&block->n_arg);
|
||||
while (entry) {
|
||||
struct ivy_ast_node *expr
|
||||
= fx_unbox(struct ivy_ast_node, entry, n_entry);
|
||||
ast_node_iterator_enqueue_node(iterator, node, expr);
|
||||
entry = fx_queue_next(entry);
|
||||
}
|
||||
|
||||
entry = fx_queue_first(&block->n_expr);
|
||||
while (entry) {
|
||||
struct ivy_ast_node *expr
|
||||
= fx_unbox(struct ivy_ast_node, entry, n_entry);
|
||||
@@ -153,21 +28,6 @@ static void collect_children(
|
||||
}
|
||||
|
||||
struct ast_node_type block_node_ops = {
|
||||
.n_add_child = add_child,
|
||||
.n_collect_children = collect_children,
|
||||
.n_state_size = sizeof(struct block_parser_state),
|
||||
.n_node_size = sizeof(struct ivy_ast_block_node),
|
||||
.n_keyword_parsers = {
|
||||
KW_PARSER(GLOBAL, parse_global),
|
||||
KW_PARSER(END, parse_end),
|
||||
KW_PARSER(ELSE, parse_else),
|
||||
KW_PARSER(ELIF, parse_else), /* same behaviour as ELSE */
|
||||
KW_PARSER_FALLBACK(parse_keyword),
|
||||
},
|
||||
.n_symbol_parsers = {
|
||||
SYM_PARSER_FALLBACK(parse_symbol),
|
||||
},
|
||||
.n_expr_parser = {
|
||||
.expr_begin = parse_expr_begin,
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user