]> git.ipfire.org Git - pakfire.git/blame - src/libpakfire/parser/grammar.y
libpakfire: parser: Concat lines and words
[pakfire.git] / src / libpakfire / parser / grammar.y
CommitLineData
f9a691ef
MT
1/*#############################################################################
2# #
3# Pakfire - The IPFire package management system #
4# Copyright (C) 2019 Pakfire development team #
5# #
6# This program is free software: you can redistribute it and/or modify #
7# it under the terms of the GNU General Public License as published by #
8# the Free Software Foundation, either version 3 of the License, or #
9# (at your option) any later version. #
10# #
11# This program is distributed in the hope that it will be useful, #
12# but WITHOUT ANY WARRANTY; without even the implied warranty of #
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14# GNU General Public License for more details. #
15# #
16# You should have received a copy of the GNU General Public License #
17# along with this program. If not, see <http://www.gnu.org/licenses/>. #
18# #
19#############################################################################*/
20
21%{
4f2a7bce
MT
22#include <stdio.h>
23
a0258b56 24#include <pakfire/logging.h>
35ebb186
MT
25#include <pakfire/types.h>
26
f9a691ef
MT
27#define YYERROR_VERBOSE 1
28
29typedef struct yy_buffer_state* YY_BUFFER_STATE;
30extern YY_BUFFER_STATE yy_scan_bytes(const char* buffer, size_t len);
31extern void yy_delete_buffer(YY_BUFFER_STATE buffer);
32
33extern int yylex();
34extern int yyparse();
6ab14b96
MT
35
36extern int num_lines;
e98f46a7
MT
37static Pakfire pakfire;
38static void yyerror(const char* s);
6b03e625 39
4f2a7bce
MT
40static void cleanup(void);
41#define ABORT do { cleanup(); YYABORT; } while (0);
42
f9a691ef
MT
43%}
44
45%token APPEND
46%token ASSIGN
47%token DEFINE
48%token END
49%token NEWLINE
50%token TAB
f9a691ef 51%token WHITESPACE
6b03e625 52%token <string> WORD
f9a691ef 53
68f7a6f7
MT
54%type <string> line;
55%type <string> text;
28afa9c2
MT
56%type <string> variable;
57%type <string> value;
6b03e625 58%type <string> words;
28afa9c2
MT
59
60%union {
61 char* string;
62}
f9a691ef 63
28afa9c2 64%%
70425a92 65
28afa9c2
MT
66top : top empty
67 | top block
68 | empty
69 | block
70 ;
71
72empty : WHITESPACE NEWLINE
73 | NEWLINE
74 ;
75
76// Optional whitespace
77whitespace : WHITESPACE
78 | /* empty */
79 ;
80
6b03e625 81variable : WORD
28afa9c2
MT
82 {
83 $$ = $1;
84 };
85
6b03e625 86value : words
28afa9c2
MT
87 {
88 $$ = $1;
89 }
90 | /* empty */
91 {
92 $$ = NULL;
93 };
94
6b03e625
MT
95words : WORD
96 {
97 $$ = $1;
98 }
68f7a6f7 99 | words WHITESPACE WORD
4f2a7bce
MT
100 {
101 int r = asprintf(&$$, "%s %s", $1, $3);
102 if (r < 0) {
103 ERROR(pakfire, "Could not allocate memory");
104 ABORT;
105 }
106 }
de528421
MT
107 | /* empty */
108 {
109 $$ = NULL;
110 };
68f7a6f7
MT
111
112line : whitespace words NEWLINE
113 {
4f2a7bce
MT
114 // Only forward words
115 $$ = $2;
68f7a6f7
MT
116 };
117
118text : text line
4f2a7bce
MT
119 {
120 int r = asprintf(&$$, "%s\n%s", $1, $2);
121 if (r < 0) {
122 ERROR(pakfire, "Could not allocate memory");
123 ABORT;
124 }
125 }
68f7a6f7 126 | line
de528421
MT
127 | /* empty */
128 {
129 $$ = NULL;
130 };
6b03e625 131
28afa9c2
MT
132block_opening : variable NEWLINE
133 {
134 printf("BLOCK OPEN: %s\n", $1);
135 };
136
137block_closing : END NEWLINE
138 {
139 printf("BLOCK CLOSED\n");
140 }
141
142block : block_opening assignments block_closing
143 {
144 printf("BLOCK FOUND\n");
145 };
146
147assignments : assignments assignment
148 | assignments empty
68f7a6f7 149 | assignments block_assignment
28afa9c2
MT
150 | /* empty */
151 ;
152
153assignment : whitespace variable whitespace ASSIGN whitespace value whitespace NEWLINE
154 {
155 printf("ASSIGNMENT FOUND: %s = %s\n", $2, $6);
156 };
f9a691ef 157
68f7a6f7
MT
158block_assignment : whitespace DEFINE WHITESPACE variable NEWLINE text whitespace END NEWLINE
159 {
160 printf("BLOCK ASSIGNMENT: %s: %s\n", $4, $6);
161 }
162
f9a691ef
MT
163%%
164
4f2a7bce
MT
165static void cleanup(void) {
166 // Reset Pakfire pointer
167 pakfire = NULL;
168}
169
e98f46a7
MT
170int pakfire_parser_parse_metadata(Pakfire _pakfire, const char* data, size_t len) {
171 pakfire = _pakfire;
172
a0258b56
MT
173 DEBUG(pakfire, "Parsing the following data:\n%s\n", data);
174
6ab14b96
MT
175 num_lines = 1;
176
f9a691ef
MT
177 YY_BUFFER_STATE buffer = yy_scan_bytes(data, len);
178 int r = yyparse();
179 yy_delete_buffer(buffer);
180
181 return r;
182}
183
184void yyerror(const char* s) {
e98f46a7 185 ERROR(pakfire, "Error (line %d): %s\n", num_lines, s);
f9a691ef 186}