]> git.ipfire.org Git - people/ric9/pakfire.git/commitdiff
parser: Refactor creating a new parser
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Jan 2025 12:21:41 +0000 (12:21 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Jan 2025 12:21:41 +0000 (12:21 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/dist.c
src/pakfire/parser.c
src/pakfire/parser.h
src/pakfire/parser/grammar.y
tests/libpakfire/makefile.c
tests/libpakfire/parser.c
tests/parser/test.c

index 0397958c6b79445785b6fdd825bc26d6b3b09e2c..6e251ffef5ff1294cf6faed5e86bef4faba1e09b 100644 (file)
@@ -156,11 +156,10 @@ int pakfire_read_makefile(struct pakfire_parser** parser, struct pakfire* pakfir
        // Fetch context
        struct pakfire_ctx* ctx = pakfire_ctx(pakfire);
 
-       *parser = pakfire_parser_create(pakfire, NULL, NULL, PAKFIRE_PARSER_FLAGS_EXPAND_COMMANDS);
-       if (!*parser) {
-               r = 1;
+       // Create a new parser
+       r = pakfire_parser_create(parser, pakfire, NULL, NULL, PAKFIRE_PARSER_FLAGS_EXPAND_COMMANDS);
+       if (r < 0)
                goto ERROR;
-       }
 
        // Set defaults
        r = pakfire_makefile_set_defaults(pakfire, *parser, path);
index 17cc8f4072198b6c42fe285505c456de0434979d..74a3f6c6bccf04e92f165c2d77aef496603e48a1 100644 (file)
@@ -83,42 +83,6 @@ static int pakfire_parser_compile_regexes(struct pakfire_parser* parser) {
        return 0;
 }
 
-struct pakfire_parser* pakfire_parser_create(struct pakfire* pakfire,
-               struct pakfire_parser* parent, const char* namespace, int flags) {
-       struct pakfire_parser* parser = calloc(1, sizeof(*parser));
-       if (parser) {
-               parser->ctx = pakfire_ctx(pakfire);
-               parser->pakfire = pakfire_ref(pakfire);
-               parser->nrefs = 1;
-
-               // Store a reference to the parent parser if we have one
-               if (parent)
-                       parser->parent = pakfire_parser_ref(parent);
-
-               // Store flags
-               parser->flags = flags;
-
-               // Make namespace
-               pakfire_parser_set_namespace(parser, namespace);
-       }
-
-       return parser;
-}
-
-struct pakfire_parser* pakfire_parser_create_child(struct pakfire_parser* parser, const char* namespace) {
-       return pakfire_parser_create(parser->pakfire, parser, namespace, parser->flags);
-}
-
-struct pakfire_parser* pakfire_parser_ref(struct pakfire_parser* parser) {
-       ++parser->nrefs;
-
-       return parser;
-}
-
-struct pakfire* pakfire_parser_get_pakfire(struct pakfire_parser* parser) {
-       return pakfire_ref(parser->pakfire);
-}
-
 static void pakfire_parser_free_declarations(struct pakfire_parser* parser) {
        if (!parser->declarations)
                return;
@@ -156,6 +120,69 @@ static void pakfire_parser_free(struct pakfire_parser* parser) {
        free(parser);
 }
 
+int pakfire_parser_create(struct pakfire_parser** parser,
+               struct pakfire* pakfire, struct pakfire_parser* parent, const char* namespace, int flags) {
+       struct pakfire_parser* self = NULL;
+       int r;
+
+       // Allocate some memory
+       self = calloc(1, sizeof(*self));
+       if (!self)
+               return -errno;
+
+       // Store a reference to the context
+       self->ctx = pakfire_ctx(pakfire);
+
+       // Store a reference to Pakfire
+       self->pakfire = pakfire_ref(pakfire);
+
+       // Initialize the reference counter
+       self->nrefs = 1;
+
+       // Store a reference to the parent parser if we have one
+       if (parent)
+               self->parent = pakfire_parser_ref(parent);
+
+       // Store flags
+       self->flags = flags;
+
+       // Make namespace
+       r = pakfire_parser_set_namespace(self, namespace);
+       if (r < 0)
+               goto ERROR;
+
+       // Return the pointer
+       *parser = self;
+       return 0;
+
+ERROR:
+       pakfire_parser_free(self);
+
+       return r;
+}
+
+struct pakfire_parser* pakfire_parser_create_child(struct pakfire_parser* parser, const char* namespace) {
+       struct pakfire_parser* child = NULL;
+       int r;
+
+       // Create the parser
+       r = pakfire_parser_create(&child, parser->pakfire, parser, namespace, parser->flags);
+       if (r < 0)
+               return NULL;
+
+       return child;
+}
+
+struct pakfire_parser* pakfire_parser_ref(struct pakfire_parser* parser) {
+       ++parser->nrefs;
+
+       return parser;
+}
+
+struct pakfire* pakfire_parser_get_pakfire(struct pakfire_parser* parser) {
+       return pakfire_ref(parser->pakfire);
+}
+
 struct pakfire_parser* pakfire_parser_unref(struct pakfire_parser* parser) {
        if (--parser->nrefs > 0)
                return parser;
index 54a0fb18658da7e8f05f99c389c1488e19f07eda..d0cf9275a913707655dd5226a6872b996f663e63 100644 (file)
@@ -38,8 +38,8 @@ enum pakfire_parser_flags {
 
 struct pakfire_parser_error;
 
-struct pakfire_parser* pakfire_parser_create(struct pakfire* pakfire, struct pakfire_parser* parser,
-       const char* namespace, int flags);
+int pakfire_parser_create(struct pakfire_parser** parser, struct pakfire* pakfire,
+       struct pakfire_parser* parent, const char* namespace, int flags);
 struct pakfire_parser* pakfire_parser_create_child(struct pakfire_parser* parser,
        const char* namespace);
 struct pakfire_parser* pakfire_parser_ref(struct pakfire_parser* parser);
index 52a58d5a483b529473f1fb0726b67ae374999c9a..9d94539e28e050647c8f8c2b87cf9417b1dc79b0 100644 (file)
@@ -171,7 +171,9 @@ static void pakfire_parser_free_declaration(struct pakfire_parser_declaration* d
 }
 
 %initial-action {
-       *parser = pakfire_parser_create(pakfire, parent, NULL, 0);
+       int r = pakfire_parser_create(parser, pakfire, parent, NULL, 0);
+       if (r < 0)
+               ABORT;
 };
 
 %destructor { if ($$) pakfire_parser_unref($$); } <parser>
@@ -183,8 +185,8 @@ static void pakfire_parser_free_declaration(struct pakfire_parser_declaration* d
 
 grammar                                                : %empty
                                                        {
-                                                               $$ = pakfire_parser_create(pakfire, *parser, NULL, 0);
-                                                               if (!$$)
+                                                               int r = pakfire_parser_create(&$$, pakfire, *parser, NULL, 0);
+                                                               if (r < 0)
                                                                        ABORT;
 
                                                                // This becomes the new top parser
@@ -342,13 +344,14 @@ subparser                                 : subparser_name T_EOL block T_END T_EOL
                                                        {
                                                                char* key = NULL;
                                                                char* value = NULL;
+                                                               int r;
 
                                                                // Create a new parser
-                                                               $$ = pakfire_parser_create(pakfire, *parser, NULL, 0);
-                                                               if (!$$)
+                                                               r = pakfire_parser_create(&$$, pakfire, *parser, NULL, 0);
+                                                               if (r < 0)
                                                                        ABORT;
 
-                                                               int r = pakfire_string_partition($1, ":", &key, &value);
+                                                               r = pakfire_string_partition($1, ":", &key, &value);
                                                                if (r)
                                                                        ABORT;
 
index f5adc6bfcb3efe363f6832b23de140f77fd00d53..b7b7b5d63bf82ba4a2c876d8ecd9a384198a87c3 100644 (file)
@@ -70,7 +70,7 @@ static int test_parse(const struct test* t) {
                FILE* f = fopen(path, "r");
                ASSERT(f);
 
-               parser = pakfire_parser_create(t->pakfire, NULL, NULL, 0);
+               ASSERT_SUCCESS(pakfire_parser_create(&parser, t->pakfire, NULL, NULL, 0));
 
                ASSERT_SUCCESS(pakfire_parser_read(parser, f, NULL));
 
@@ -93,10 +93,10 @@ FAIL:
 }
 
 static int test_macros(const struct test* t) {
+       struct pakfire_parser* parser = NULL;
        int r = EXIT_FAILURE;
 
-       struct pakfire_parser* parser = pakfire_parser_create(t->pakfire, NULL, NULL, 0);
-       ASSERT(parser);
+       ASSERT_SUCCESS(pakfire_parser_create(&parser, t->pakfire, NULL, NULL, 0));
 
        // Load 'em all
        ASSERT_SUCCESS(load_macros(parser));
@@ -120,9 +120,8 @@ static int test_packages(const struct test* t) {
        ASSERT_SUCCESS(pakfire_repo_create(&repo, t->pakfire, "test"));
        ASSERT(repo);
 
-       parser = pakfire_parser_create(t->pakfire, NULL, NULL,
-               PAKFIRE_PARSER_FLAGS_EXPAND_COMMANDS);
-       ASSERT(parser);
+       ASSERT_SUCCESS(pakfire_parser_create(&parser, t->pakfire, NULL, NULL,
+               PAKFIRE_PARSER_FLAGS_EXPAND_COMMANDS));
 
        // Set some architecture
        pakfire_parser_set(parser, NULL, "DISTRO_ARCH", "x86_64", 0);
index ff27fe6472b13f53c8d66bec3f42a078d7a81273..618ed2ea746a7d155e761ff17c37820684747647 100644 (file)
 #include "../testsuite.h"
 
 static int test_parser(const struct test* t) {
+       struct pakfire_parser* parser = NULL;
        struct pakfire_parser* subparser = NULL;
        char* value = NULL;
        int r = EXIT_FAILURE;
 
        // Create a new parser
-       struct pakfire_parser* parser = pakfire_parser_create(t->pakfire, NULL, NULL, 0);
+       ASSERT_SUCCESS(pakfire_parser_create(&parser, t->pakfire, NULL, NULL, 0));
 
        // Retrieve a value that does not exist
        value = pakfire_parser_get(parser, NULL, "null");
@@ -116,6 +117,7 @@ static const char* files[] = {
 };
 
 static int test_parser_files(const struct test* t) {
+       struct pakfire_parser* parser = NULL;
        const char** file = files;
        char path[PATH_MAX];
        int r = EXIT_FAILURE;
@@ -124,7 +126,7 @@ static int test_parser_files(const struct test* t) {
                ASSERT_SUCCESS(pakfire_string_format(path, "%s/%s", TEST_SRC_PATH, *file));
 
                // Create a new parser
-               struct pakfire_parser* parser = pakfire_parser_create(t->pakfire, NULL, NULL, 0);
+               ASSERT_SUCCESS(pakfire_parser_create(&parser, t->pakfire, NULL, NULL, 0));
 
                FILE* f = fopen(path, "r");
                ASSERT(f);
@@ -150,10 +152,11 @@ FAIL:
 }
 
 static int test_append(const struct test* t) {
+       struct pakfire_parser* parser = NULL;
        int r = EXIT_FAILURE;
 
        // Create a new parser
-       struct pakfire_parser* parser = pakfire_parser_create(t->pakfire, NULL, NULL, 0);
+       ASSERT_SUCCESS(pakfire_parser_create(&parser, t->pakfire, NULL, NULL, 0));
 
        const char* str1 =
                "a  = 1\n"
@@ -203,9 +206,7 @@ static int test_parser_command(const struct test* t) {
        struct pakfire_parser* parser = NULL;
        int r = EXIT_FAILURE;
 
-       parser = pakfire_parser_create(t->pakfire, NULL, NULL,
-               PAKFIRE_PARSER_FLAGS_EXPAND_COMMANDS);
-       ASSERT(parser);
+       ASSERT_SUCCESS(pakfire_parser_create(&parser, t->pakfire, NULL, NULL, PAKFIRE_PARSER_FLAGS_EXPAND_COMMANDS));
 
        ASSERT_SUCCESS(pakfire_parser_set(parser, NULL, "command", command, 0));
 
index cc55aa1a60189d951278b7f18c7eb57bc2fdfb40..99aa110379bec19256354d43b0401520e0ffc21d 100644 (file)
@@ -72,9 +72,9 @@ int main(int argc, const char* argv[]) {
        }
 
        // Create a new parser
-       parser = pakfire_parser_create(pakfire, NULL, NULL, 0);
-       if (!parser) {
-               fprintf(stderr, "Could not create a parser: %m\n");
+       r = pakfire_parser_create(&parser, pakfire, NULL, NULL, 0);
+       if (r < 0) {
+               fprintf(stderr, "Could not create a parser: %s\n", strerror(-r));
                goto ERROR;
        }