]> git.ipfire.org Git - pakfire.git/commitdiff
parser: Add keywords that open a new parser
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 24 Feb 2021 17:58:34 +0000 (17:58 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 24 Feb 2021 17:58:34 +0000 (17:58 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/libpakfire/parser/grammar.y
src/libpakfire/parser/scanner.l
tests/data/parser/test-subparsers.txt [new file with mode: 0644]
tests/libpakfire/parser.c

index 2ace7d7fd11935b1937a04da2f47f771bbc8b9c5..9375199e5fd46c64efd3a9fe0628dfacf786b038 100644 (file)
@@ -648,7 +648,8 @@ EXTRA_DIST += \
        tests/data/beep-1.3-2.ip3.x86_64.pfm \
        \
        tests/data/parser/test-comments.txt \
-       tests/data/parser/test-declarations.txt
+       tests/data/parser/test-declarations.txt \
+       tests/data/parser/test-subparsers.txt
 
 .PHONY: clean-local-check
 clean-local-check:
index 8a951a2efbb2b356afdc6ff059b395abafccdb69..f4d3e61f973d51a9b9c6ca01e3e99fbb0cb8ed84 100644 (file)
@@ -57,6 +57,8 @@ enum operator {
        OP_EQUALS = 0,
 };
 
+static PakfireParser make_child(PakfireParser parent, const char* namespace);
+
 %}
 
 %token                                         T_INDENT
@@ -72,6 +74,11 @@ enum operator {
 %token                                         T_ASSIGN
 %token                                         T_APPEND
 
+%token <string>                                T_SUBPARSER
+
+%type <parser>                         grammar
+%type <parser>                         subparser
+
 %type <string>                         key
 %type <string>                         value
 
@@ -88,7 +95,15 @@ enum operator {
 %%
 
 grammar                                                : %empty
+                                                       {
+                                                               $$ = make_child(parser, NULL);
+                                                       }
                                                        | grammar declaration
+                                                       | grammar subparser
+                                                       {
+                                                               $$ = pakfire_parser_merge($1, $2);
+                                                               pakfire_parser_unref($2);
+                                                       }
                                                        | grammar empty
                                                        ;
 
@@ -143,6 +158,28 @@ line                                               : T_STRING T_EOL
                                                        }
                                                        ;
 
+subparser                                      : subparser_open T_INDENT grammar T_OUTDENT subparser_close
+                                                       {
+                                                               // Move back to the parent parser
+                                                               parser = pakfire_parser_get_parent(parser);
+
+                                                               // Merge block into the parent parser
+                                                               pakfire_parser_merge(parser, $3);
+
+                                                               // Free block parser
+                                                               pakfire_parser_unref($3);
+                                                       }
+                                                       ;
+
+subparser_open                         : T_SUBPARSER T_EOL
+                                                       {
+                                                               // Create a new sub-parser which opens a new namespace
+                                                               parser = make_child(parser, $1);
+                                                       }
+                                                       ;
+
+subparser_close                                : T_END;
+
 %%
 
 int pakfire_parser_parse_data(PakfireParser parent, const char* data, size_t len) {
@@ -203,3 +240,10 @@ void yyerror(PakfireParser parser, const char* s) {
 
        pakfire_unref(pakfire);
 }
+
+static PakfireParser make_child(PakfireParser parent, const char* namespace) {
+       PakfireParser parser = pakfire_parser_create_child(parent, namespace);
+       pakfire_parser_unref(parent);
+
+       return parser;
+}
index b00f525e256b8caf1751fe393d2be73a6b54e462..9779b478c96a63a9812537d4493e69d3a2c24e0b 100644 (file)
@@ -61,6 +61,8 @@ whitespace                            [ \t]+
 
 key                                            ({letter}|{underscore})(({letter}|{digit}|{underscore})*({letter}|{digit}))?
 
+keywords                               (build|dependencies|distribution|packages?|quality\-agent)
+
 %%
 
 <*>\n                                  {
@@ -116,6 +118,12 @@ key                                                ({letter}|{underscore})(({letter}|{digit}|{underscore})*({letter}|{digi
                                                        abort();
                                                }
 
+{keywords}$                            {
+                                                       yylval.string = strdup(yytext);
+
+                                                       return T_SUBPARSER;
+                                               }
+
 "="                                            {
                                                        // Read everything after this
                                                        yy_push_state(READLINE);
diff --git a/tests/data/parser/test-subparsers.txt b/tests/data/parser/test-subparsers.txt
new file mode 100644 (file)
index 0000000..824b988
--- /dev/null
@@ -0,0 +1,9 @@
+# This is a subparser
+build
+       requires = ABC
+end
+
+# This isn't but it starts with the same letters
+builder
+       This is just a string
+end
index 74f075979bbb3effbfa9e1a8eff2e0993e6abddf..8961aa8a9128288884b089efcb137ca59e46eed1 100644 (file)
@@ -106,6 +106,7 @@ static int test_parser(const struct test* t) {
 static const char* files[] = {
        "data/parser/test-comments.txt",
        "data/parser/test-declarations.txt",
+       "data/parser/test-subparsers.txt",
        NULL,
 };