Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser alloc dealloc #68

Merged
merged 5 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 62 additions & 7 deletions src/parser.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "ast.h"

Check notice on line 1 in src/parser.c

View workflow job for this annotation

GitHub Actions / linter

Run clang-format on src/parser.c

File src/parser.c does not conform to Custom style guidelines. (lines 1, 14, 15, 16, 27, 28, 29, 40, 42, 59, 60, 61, 78, 79, 80, 97, 98, 99, 116, 117, 121, 122, 124, 125, 126, 127, 133, 134, 135, 136, 138, 140, 141, 142, 148, 149, 150, 151, 157, 158)

Check failure on line 1 in src/parser.c

View workflow job for this annotation

GitHub Actions / linter

src/parser.c:1:10 [clang-diagnostic-error]

'ast.h' file not found
#include "lexer.h"
#include "parser.h"
#include "token.h"
Expand All @@ -11,7 +11,9 @@
*
* @return A pointer to the next token if it exists, NULL otherwise
*/
Token* peek(Parser* parser);
Token* peek(Parser* parser){
return parser != NULL ? NULL : NULL;
}


/**
Expand All @@ -22,7 +24,9 @@
*
* @return A pointer to the next token if it exists, NULL otherwise
*/
Token* next(Parser* parser);
Token* next(Parser* parser){
return parser != NULL ? NULL : NULL;
}


/**
Expand All @@ -33,7 +37,9 @@
*
* @return 0 if the next token is of the required type, -1 otherwise
*/
int expect(Parser* parser, TokenType type);
int expect(Parser* parser, TokenType type){
return parser != NULL && type ? 0 : 0;
}


/**
Expand All @@ -50,7 +56,9 @@
*
* @return The root of the AST created by parsing the `base` non-terminal
*/
ASTNode* parse_base(Parser* parser);
ASTNode* parse_base(Parser* parser){
return parser != NULL ? NULL : NULL;
}


/**
Expand All @@ -67,7 +75,9 @@
*
* @return The root of the AST created by parsing the `factor` non-terminal
*/
ASTNode* parse_factor(Parser* parser);
ASTNode* parse_factor(Parser* parser){
return parser != NULL ? NULL : NULL;
}


/**
Expand All @@ -84,7 +94,9 @@
*
* @return The root of the AST created by parsing the `term` non-terminal
*/
ASTNode* parse_term(Parser* parser);
ASTNode* parse_term(Parser* parser){
return parser != NULL ? NULL : NULL;
}


/**
Expand All @@ -101,4 +113,47 @@
*
* @return The root of the AST created by parsing the `expr` non-terminal
*/
ASTNode* parse_expr(Parser* parser);
ASTNode* parse_expr(Parser* parser){
return parser != NULL ? NULL : NULL;
}

// Create a heap allocated parser from the given Lexer
Parser* parser_create(Lexer* lexer) {
Parser* parser = malloc(sizeof(Parser));

if (parser_init(parser, lexer) < 0) {
free(parser);
return NULL;
}

return parser;
}

// Initialize the given parse using the given Lexer
int parser_init(Parser* parser, Lexer* lexer) {
if (parser == NULL) {
return -1;
}

parser->tokens = tokenize_all(lexer, &parser->n_tokens);

if (parser->tokens == NULL || parser->n_tokens <= 0) {
return -1;
}

return parser->position = 0;
}

// Release the memory used by this parser.
void parser_free(Parser* parser) {
if (parser == NULL) {
return;
}

free(parser->tokens);
}

// Create a AST by parsing the tokens in the given parser
ASTNode* parse(Parser* parser) {
return parser != NULL ? NULL : NULL;
}
8 changes: 7 additions & 1 deletion tests/test_parser.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#define FAIL_FAST

Check notice on line 1 in tests/test_parser.c

View workflow job for this annotation

GitHub Actions / linter

Run clang-format on tests/test_parser.c

File tests/test_parser.c does not conform to Custom style guidelines. (lines 14, 15, 16, 17, 18, 19, 20, 28, 29, 30, 33, 34, 35, 41, 42, 44, 46, 47, 48, 49, 50, 52, 53, 54, 59, 60, 61, 63, 64, 65, 67, 72, 73, 74, 76, 77, 79, 80, 82, 84, 85, 86, 88, 89, 91, 92, 93, 95, 96, 98, 99, 100, 101, 103, 108, 109, 110, 112, 113, 115, 116, 118, 120, 121, 122, 124, 126, 127, 129, 130, 131, 133, 134, 136, 137, 138, 139, 141, 146, 147, 148, 150, 152, 153, 154, 156, 157, 158, 159, 161, 162, 163, 165, 166, 167, 168, 170, 171, 172, 174, 182, 183, 184, 186, 187, 188, 189, 191, 193, 194, 195, 197, 198, 200, 201, 203, 204, 206, 207, 208, 209, 210, 211, 212, 213, 215, 217, 218, 220, 222, 223, 224, 226, 227, 228, 229, 231, 232, 234, 236, 237, 239, 240, 241, 258, 259, 260, 262, 263, 264, 265, 267, 269, 270, 272, 273, 274, 275, 276, 278, 279, 280, 282, 283, 285, 286, 288, 289, 291, 293, 294, 295, 296, 297, 313, 314, 315, 317, 318, 319, 321, 323, 324, 326, 327, 329, 330, 331, 333, 334, 335, 337, 338, 339, 341, 342, 343, 344, 346, 348, 349, 351, 352, 353, 355, 356, 357, 359, 360, 362, 363, 365, 366, 368, 369, 371, 372, 374, 375, 377, 389, 390, 391, 393, 394, 395, 397, 399, 400, 401, 403, 405, 406, 407, 408, 410, 411, 412, 414, 415, 416, 417, 418, 420, 422, 423, 424, 426, 428, 429, 430, 431, 433, 434, 435, 436, 438, 439, 440, 441, 442, 444, 445, 446, 458, 459, 460, 462, 464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 479, 480, 481, 482, 483, 484, 485, 486, 488, 489, 490, 491, 492, 493, 494, 495, 496, 498, 499, 500, 502, 503, 504, 506, 507, 508, 510, 511, 513, 515, 517, 518, 519, 521, 522, 523, 525, 526, 527, 529, 530, 531, 533, 534, 535, 536, 538, 539, 540, 542, 543, 545, 546, 547, 548, 550, 552, 553, 554, 556, 557, 558, 560, 561, 562, 564, 565, 567, 568, 569, 570, 572, 574, 575, 576, 578, 579, 581, 582, 583, 584, 586, 588, 589, 594, 595, 596, 597, 598, 599, 600, 601, 602, 603, 605, 608, 609, 610, 611, 612, 613)

#include "testlib/asserts.h"
#include "testlib/tests.h"
Expand All @@ -21,9 +21,9 @@


// Global vars for tests
char regex[] = "(a|b)*c";

Check warning on line 24 in tests/test_parser.c

View workflow job for this annotation

GitHub Actions / linter

tests/test_parser.c:24:6 [cppcoreguidelines-avoid-non-const-global-variables]

variable 'regex' is non-const and globally accessible, consider making it const
Lexer lexer;

Check warning on line 25 in tests/test_parser.c

View workflow job for this annotation

GitHub Actions / linter

tests/test_parser.c:25:7 [cppcoreguidelines-avoid-non-const-global-variables]

variable 'lexer' is non-const and globally accessible, consider making it const
Parser parser;

Check warning on line 26 in tests/test_parser.c

View workflow job for this annotation

GitHub Actions / linter

tests/test_parser.c:26:8 [cppcoreguidelines-avoid-non-const-global-variables]

variable 'parser' is non-const and globally accessible, consider making it const

#define CREATE_PARSER do {\
lexer_init(&lexer, regex);\
Expand Down Expand Up @@ -51,6 +51,7 @@

lexer_free(&lexer);
parser_free(parser);
free(parser);
TEST_END;
}

Expand All @@ -68,7 +69,7 @@
}

// Test peek
int test_peek(void) {

Check warning on line 72 in tests/test_parser.c

View workflow job for this annotation

GitHub Actions / linter

tests/test_parser.c:72:5 [readability-function-cognitive-complexity]

function 'test_peek' has cognitive complexity of 62 (threshold 25)
TEST_BEGIN;
CREATE_PARSER;

Expand Down Expand Up @@ -104,7 +105,7 @@
}

// Test next
int test_next(void) {

Check warning on line 108 in tests/test_parser.c

View workflow job for this annotation

GitHub Actions / linter

tests/test_parser.c:108:5 [readability-function-cognitive-complexity]

function 'test_next' has cognitive complexity of 68 (threshold 25)
TEST_BEGIN;
CREATE_PARSER;

Expand Down Expand Up @@ -142,13 +143,13 @@
}

// Test expect
int test_expect(void) {

Check warning on line 146 in tests/test_parser.c

View workflow job for this annotation

GitHub Actions / linter

tests/test_parser.c:146:5 [readability-function-cognitive-complexity]

function 'test_expect' has cognitive complexity of 44 (threshold 25)
TEST_BEGIN;
CREATE_PARSER;

TokenType expected = LPAREN;

int ret = expect(&parser, expected);

Check warning on line 152 in tests/test_parser.c

View workflow job for this annotation

GitHub Actions / linter

tests/test_parser.c:152:9 [cppcoreguidelines-init-variables]

variable 'ret' is not initialized
assert_equals_int(ret, 0);
assert_equals_int(parser.position, 0);

Expand Down Expand Up @@ -178,7 +179,7 @@
// This has two cases
// - base expands to a CHAR
// - base expands to a LPAREN expr RPAREN
int test_parse_base(void) {

Check warning on line 182 in tests/test_parser.c

View workflow job for this annotation

GitHub Actions / linter

tests/test_parser.c:182:5 [readability-function-cognitive-complexity]

function 'test_parse_base' has cognitive complexity of 58 (threshold 25)
TEST_BEGIN;
// We will use a simpler regex for this test

Expand Down Expand Up @@ -254,7 +255,7 @@
//
// Case 4 is effectively testing whether parse_base works,
// which is tested independently anyway, so we will not do that here
int test_parse_factor(void) {

Check warning on line 258 in tests/test_parser.c

View workflow job for this annotation

GitHub Actions / linter

tests/test_parser.c:258:5 [readability-function-cognitive-complexity]

function 'test_parse_factor' has cognitive complexity of 120 (threshold 25)
TEST_BEGIN;
// We will use a simpler regex for this test

Expand Down Expand Up @@ -309,7 +310,7 @@
// Case 1 is effectively testing parse_factor, which is tested independently
// but it will be replicated here because concat does not simply concat
// under some cases, like the OR token, the RPAREN token etc
int test_parse_term(void) {

Check warning on line 313 in tests/test_parser.c

View workflow job for this annotation

GitHub Actions / linter

tests/test_parser.c:313:5 [readability-function-cognitive-complexity]

function 'test_parse_term' has cognitive complexity of 174 (threshold 25)
TEST_BEGIN;
// We will use a simpler regex for this test

Expand Down Expand Up @@ -605,5 +606,10 @@


int main(int argc, char* argv[]) {
return default_main(&argv[1], argc - 1);
// Run selective tests for now
argv = (char* []){
"-r", "test_parser_create", "test_parser_init"
};
argc = 3;
return default_main(argv, argc);
}
Loading