From 184cc79a4c946dbf4d9de0e14e601b3140586c86 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 12 Mar 2024 20:51:48 +0100 Subject: [PATCH] test(parser): started some tests --- src/ast.c | 3 ++- src/lexer/lexer.c | 3 +++ src/parser/parser.c | 18 ++++++++++++++++-- test/lexer/lexer.c | 11 +++++++---- test/parser/parser.c | 38 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 test/parser/parser.c diff --git a/src/ast.c b/src/ast.c index a718e8e..62c2221 100644 --- a/src/ast.c +++ b/src/ast.c @@ -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; diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index 8305e86..d7aba98 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -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); diff --git a/src/parser/parser.c b/src/parser/parser.c index 5b5388c..6653883 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -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)); +} diff --git a/test/lexer/lexer.c b/test/lexer/lexer.c index 6a8e61c..4e3ca51 100644 --- a/test/lexer/lexer.c +++ b/test/lexer/lexer.c @@ -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); } diff --git a/test/parser/parser.c b/test/parser/parser.c new file mode 100644 index 0000000..cb70983 --- /dev/null +++ b/test/parser/parser.c @@ -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 } +};