lang: replace stack-based parser with a recursive parser
This commit is contained in:
@@ -1,62 +1,9 @@
|
||||
#include "ctx.h"
|
||||
#include "node.h"
|
||||
|
||||
#include <fx/ds/string.h>
|
||||
#include <ivy/lang/lex.h>
|
||||
#include <stdio.h>
|
||||
|
||||
struct unit_package_parser_state {
|
||||
struct parser_state s_base;
|
||||
int s_prev_token;
|
||||
};
|
||||
|
||||
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 PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
||||
}
|
||||
|
||||
state->s_prev_token = IVY_SYM_DOT;
|
||||
return PARSE_RESULT(IVY_OK, 0);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
struct ivy_ast_unit_package_node *node
|
||||
= (struct ivy_ast_unit_package_node *)(state->s_base.s_node);
|
||||
|
||||
if (state->s_prev_token == IVY_TOK_IDENT) {
|
||||
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
||||
}
|
||||
|
||||
fx_queue_push_back(&node->n_ident, &tok->t_entry);
|
||||
|
||||
state->s_prev_token = IVY_TOK_IDENT;
|
||||
return PARSE_RESULT(IVY_OK, 0);
|
||||
}
|
||||
|
||||
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 PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
||||
}
|
||||
|
||||
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
||||
return PARSE_RESULT(IVY_OK, 0);
|
||||
}
|
||||
|
||||
static void to_string(struct ivy_ast_node *node, fx_string *str)
|
||||
{
|
||||
struct ivy_ast_unit_package_node *unit_package
|
||||
@@ -83,24 +30,7 @@ static void to_string(struct ivy_ast_node *node, fx_string *str)
|
||||
fx_string_append_cstr(str, ")");
|
||||
}
|
||||
|
||||
static void init_state(struct ivy_parser *ctx, struct parser_state *sp, uintptr_t arg)
|
||||
{
|
||||
struct unit_package_parser_state *state
|
||||
= (struct unit_package_parser_state *)sp;
|
||||
state->s_prev_token = IVY_KW_PACKAGE;
|
||||
}
|
||||
|
||||
struct ast_node_type unit_package_node_ops = {
|
||||
.n_to_string = to_string,
|
||||
.n_init_state = init_state,
|
||||
.n_state_size = sizeof(struct unit_package_parser_state),
|
||||
.n_node_size = sizeof(struct ivy_ast_unit_package_node),
|
||||
.n_symbol_parsers = {
|
||||
SYM_PARSER(DOT, parse_dot),
|
||||
},
|
||||
.n_token_parsers = {
|
||||
TOK_PARSER(IDENT, parse_ident),
|
||||
TOK_PARSER(LINEFEED, parse_linefeed),
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user