]> git.ipfire.org Git - pakfire.git/blame - src/libpakfire/parser/grammar.y
libpakfire: parser: Read variable assignments
[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%{
a0258b56 22#include <pakfire/logging.h>
35ebb186
MT
23#include <pakfire/types.h>
24
f9a691ef
MT
25#define YYERROR_VERBOSE 1
26
27typedef struct yy_buffer_state* YY_BUFFER_STATE;
28extern YY_BUFFER_STATE yy_scan_bytes(const char* buffer, size_t len);
29extern void yy_delete_buffer(YY_BUFFER_STATE buffer);
30
31extern int yylex();
32extern int yyparse();
6ab14b96
MT
33
34extern int num_lines;
e98f46a7
MT
35static Pakfire pakfire;
36static void yyerror(const char* s);
f9a691ef
MT
37%}
38
39%token APPEND
40%token ASSIGN
41%token DEFINE
42%token END
43%token NEWLINE
44%token TAB
28afa9c2
MT
45%token <string> VARIABLE
46%token <string> VALUE
f9a691ef
MT
47%token WHITESPACE
48
28afa9c2
MT
49%type <string> variable;
50%type <string> value;
51
52%union {
53 char* string;
54}
f9a691ef 55
28afa9c2 56%%
70425a92 57
28afa9c2
MT
58top : top empty
59 | top block
60 | empty
61 | block
62 ;
63
64empty : WHITESPACE NEWLINE
65 | NEWLINE
66 ;
67
68// Optional whitespace
69whitespace : WHITESPACE
70 | /* empty */
71 ;
72
73variable : VARIABLE
74 {
75 $$ = $1;
76 };
77
78value : VALUE
79 | variable
80 {
81 $$ = $1;
82 }
83 | /* empty */
84 {
85 $$ = NULL;
86 };
87
88block_opening : variable NEWLINE
89 {
90 printf("BLOCK OPEN: %s\n", $1);
91 };
92
93block_closing : END NEWLINE
94 {
95 printf("BLOCK CLOSED\n");
96 }
97
98block : block_opening assignments block_closing
99 {
100 printf("BLOCK FOUND\n");
101 };
102
103assignments : assignments assignment
104 | assignments empty
105 | /* empty */
106 ;
107
108assignment : whitespace variable whitespace ASSIGN whitespace value whitespace NEWLINE
109 {
110 printf("ASSIGNMENT FOUND: %s = %s\n", $2, $6);
111 };
f9a691ef
MT
112
113%%
114
e98f46a7
MT
115int pakfire_parser_parse_metadata(Pakfire _pakfire, const char* data, size_t len) {
116 pakfire = _pakfire;
117
a0258b56
MT
118 DEBUG(pakfire, "Parsing the following data:\n%s\n", data);
119
6ab14b96
MT
120 num_lines = 1;
121
f9a691ef
MT
122 YY_BUFFER_STATE buffer = yy_scan_bytes(data, len);
123 int r = yyparse();
124 yy_delete_buffer(buffer);
125
126 return r;
127}
128
129void yyerror(const char* s) {
e98f46a7 130 ERROR(pakfire, "Error (line %d): %s\n", num_lines, s);
f9a691ef 131}