test(parser): started some tests

main
Jef Roosens 2024-03-12 20:51:48 +01:00
parent a4a41b287e
commit 184cc79a4c
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
5 changed files with 66 additions and 7 deletions

View File

@ -17,10 +17,11 @@ mrk_err mrk_ast_node_child_append(mrk_ast_node **out, mrk_ast_node *parent) {
return mrk_err_failed_alloc;
}
parent->children.arr = children;
MRK_RES(mrk_ast_node_init(out));
children[parent->children.len] = *out;
parent->children.arr = children;
parent->children.len++;
return mrk_err_ok;

View File

@ -271,6 +271,7 @@ void mrk_lexer_lex_start_of_line(mrk_token *out, mrk_lexer *lexer) {
} else {
mrk_lexer_emit(out, lexer, mrk_token_type_newline);
}
break;
case '\t':
mrk_lexer_emit(out, lexer, mrk_token_type_indent);
break;
@ -368,6 +369,8 @@ mrk_lexer_err mrk_lexer_next(mrk_token *out, mrk_lexer *lexer) {
return mrk_lexer_err_done;
}
mrk_lexer_reset(lexer);
if (lexer->pos.line_index == 0 ||
lexer->last_emitted == mrk_token_type_indent) {
mrk_lexer_lex_start_of_line(out, lexer);

View File

@ -1,5 +1,11 @@
#include "mrk/parser_internal.h"
mrk_err mrk_parser_init(mrk_parser **out) {
MRK_CALLOC(out, 1, sizeof(mrk_parser));
return mrk_err_ok;
}
void mrk_parser_open(mrk_parser *parser, mrk_lexer *lexer) {
parser->lexer = lexer;
@ -14,7 +20,7 @@ mrk_err mrk_parser_parse(mrk_ast_node **out, mrk_parser *parser) {
mrk_ast_node *root;
MRK_RES(mrk_ast_node_init(&root));
while (!mrk_lexer_done(parser->lexer)) {
while (!mrk_parser_done(parser)) {
mrk_ast_node *child;
MRK_RES(mrk_ast_node_child_append(&child, root));
@ -36,7 +42,7 @@ mrk_err mrk_parser_parse_block(mrk_ast_node *out, mrk_parser *parser) {
}
if (!mrk_parser_done(parser)) {
mrk_parser_eat(NULL, parser, mrk_token_type_blank_line);
MRK_RES(mrk_parser_eat(NULL, parser, mrk_token_type_blank_line));
}
return mrk_err_ok;
@ -66,6 +72,9 @@ mrk_err mrk_parser_parse_header(mrk_ast_node *out, mrk_parser *parser) {
child->type = mrk_ast_node_type_space;
mrk_parser_advance(parser);
break;
case mrk_token_type_left_bracket:
mrk_parser_parse_link(child, parser);
break;
}
}
@ -87,6 +96,7 @@ mrk_err mrk_parser_parse_text(mrk_ast_node *out, mrk_parser *parser) {
mrk_token text_token;
MRK_RES(mrk_parser_eat(&text_token, parser, mrk_token_type_text));
out->type = mrk_ast_node_type_text;
// Start in input buffer
out->args[0].num = text_token.start;
// End in input buffer
@ -94,3 +104,7 @@ mrk_err mrk_parser_parse_text(mrk_ast_node *out, mrk_parser *parser) {
return mrk_err_ok;
}
mrk_err mrk_parser_parse_link(mrk_ast_node *out, mrk_parser *parser) {
MRK_RES(mrk_parser_eat(NULL, parser, mrk_token_type_left_bracket));
}

View File

@ -9,7 +9,7 @@
void test_lexer_header() {
LEXER_INIT();
const char *buf = "#### hallo";
const char *buf = "#### hallo hallo";
mrk_lexer_open(lxr, buf, 0);
mrk_token t;
@ -18,6 +18,11 @@ void test_lexer_header() {
TEST_CHECK_(t.start == 0, "t.start == %lu", t.start);
TEST_CHECK(t.end == 4);
TEST_CHECK(mrk_lexer_next(&t, lxr) == mrk_lexer_err_ok);
TEST_CHECK(t.type == mrk_token_type_text);
TEST_CHECK_(t.start == 4, "t.start == %lu", t.start);
TEST_CHECK(t.end == 16);
mrk_lexer_free(lxr);
}
@ -56,9 +61,7 @@ void test_lexer_simple1() {
TEST_CHECK(mrk_lexer_next(&t, lxr) == mrk_lexer_err_ok);
TEST_CHECK(t.type == mrk_token_type_text);
TEST_CHECK(mrk_lexer_next(&t, lxr) == mrk_lexer_err_ok);
TEST_CHECK(t.type == mrk_token_type_newline);
TEST_CHECK(mrk_lexer_next(&t, lxr) == mrk_lexer_err_ok);
TEST_CHECK(t.type == mrk_token_type_newline);
TEST_CHECK(t.type == mrk_token_type_blank_line);
TEST_CHECK(mrk_lexer_next(&t, lxr) == mrk_lexer_err_ok);
TEST_CHECK(t.type == mrk_token_type_text);
}

View File

@ -0,0 +1,38 @@
#include "test.h"
#include "mrk/lexer.h"
#include "mrk/parser.h"
#define LEXER_INIT() \
mrk_lexer *lxr; \
TEST_CHECK(mrk_lexer_init(&lxr) == mrk_err_ok)
#define PARSER_INIT() \
mrk_parser *psr; \
TEST_CHECK(mrk_parser_init(&psr) == mrk_err_ok)
#define PARSER_OPEN(buf) \
mrk_lexer *lxr; \
TEST_CHECK(mrk_lexer_init(&lxr) == mrk_err_ok); \
mrk_parser *psr; \
TEST_CHECK(mrk_parser_init(&psr) == mrk_err_ok); \
mrk_lexer_open(lxr, buf, 0); \
mrk_parser_open(psr, lxr)
void test_parse_header() {
const char *buf = "### hello world";
PARSER_OPEN(buf);
mrk_ast_node *root;
TEST_CHECK(mrk_parser_parse(&root, psr) == mrk_err_ok);
mrk_ast_node *header = root->children.arr[0];
TEST_CHECK(header->type == mrk_ast_node_type_header);
TEST_CHECK(header->children.arr[0]->type == mrk_ast_node_type_text);
}
TEST_LIST = {
{ "parser header", test_parse_header },
{ NULL, NULL }
};