lang: replace stack-based parser with a recursive parser
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
#include "ctx.h"
|
||||
|
||||
#include <ivy/lang/ast.h>
|
||||
|
||||
bool parse_unit(struct ivy_parser *parser, struct ivy_ast_node **out)
|
||||
{
|
||||
struct ivy_ast_unit_node *unit
|
||||
= (struct ivy_ast_unit_node *)ivy_ast_node_create(IVY_AST_UNIT);
|
||||
if (!unit) {
|
||||
parser->p_status = IVY_ERR_NO_MEMORY;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool result = true;
|
||||
bool prev_was_expr = false;
|
||||
while (1) {
|
||||
struct ivy_ast_node *child = NULL;
|
||||
|
||||
if (parse_eof(parser)) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (parse_linefeed(parser)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (peek_keyword(parser, IVY_KW_PACKAGE)) {
|
||||
result = parse_package_id(parser, &child);
|
||||
} else if (peek_keyword(parser, IVY_KW_USE)) {
|
||||
result = parse_package_import(parser, &child);
|
||||
} else if (peek_keyword(parser, IVY_KW_CLASS)) {
|
||||
result = parse_class(parser, &child);
|
||||
} else if (peek_expr_start(parser)) {
|
||||
result = parse_expr(
|
||||
parser, EXPR_PARSE_CONSUME_TERMINATOR, &child);
|
||||
} else {
|
||||
parse_trace(parser, "unexpected token");
|
||||
result = false;
|
||||
}
|
||||
|
||||
if (!result || !child) {
|
||||
break;
|
||||
}
|
||||
|
||||
fx_queue_push_back(&unit->n_children, &child->n_entry);
|
||||
}
|
||||
|
||||
*out = generic_node(unit);
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user