cusel/compiler/parser.c
2026-11-26 16:23:34 +02:00

52 lines
1.1 KiB
C

#include <assert.h>
#include "parser.h"
#include "vector.h"
struct parser_state {
SPAN(token) tokens;
size_t index;
VEC(ast_node) nodes;
};
static ast_node_id toplevel(struct parser_state *);
static ast_node_id
new_ast_node(struct parser_state *, struct source_location, enum ast_node_type);
static token peek_token(struct parser_state const *);
struct parse_result parse(SPAN(token) tokens) {
struct parser_state state = {
.tokens = tokens,
.index = 0,
.nodes = {},
};
auto root = toplevel(&state);
return (struct parse_result) {
.nodes = state.nodes,
.root_node = root,
};
}
static ast_node_id toplevel(struct parser_state *state) {
return new_ast_node(state, peek_token(state).at, NODE_ERROR);
}
static ast_node_id new_ast_node(
struct parser_state *state,
struct source_location at,
enum ast_node_type type
) {
auto next_index = LENGTH(state->nodes);
assert(next_index <= ast_node_id_max);
ast_node node = {.at = at, .type = type};
VEC_PUSH(state->nodes, node);
return (ast_node_id)next_index;
}
static token peek_token(struct parser_state const *state) {
return AT(state->tokens, state->index);
}