-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.yy
379 lines (325 loc) · 8.86 KB
/
parser.yy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* $Id: parser.yy 48 2009-09-05 08:07:10Z tb $ -*- mode: c++ -*- */
/** \file parser.yy Contains the example Bison parser source */
//%code requires { /*** C/C++ Declarations ***/
%{
#include <boost/foreach.hpp>
#include <boost/variant/variant.hpp>
#include <boost/variant/get.hpp>
#include <boost/tuple/tuple.hpp>
#include <stdio.h>
#include <string>
#include <vector>
#include <map>
#include "cudf.h"
#define foreach BOOST_FOREACH
using std::string;
using std::map;
typedef boost::variant<vpkglist_t,list_vpkglist_t,Keep,std::string,int,bool> propVariant;
typedef boost::tuple<string,propVariant> propData;
typedef map<string,propVariant> pkgProps;
//}
%}
/*** yacc/bison Declarations ***/
/* Require bison 2.3 or later */
%require "2.3"
/* add debug output code to generated parser. disable this for release
* versions. */
//%debug
/* start symbol is named "start" */
%start start
/* write out a header file containing the token defines */
%defines
/* use newer C++ skeleton file */
%skeleton "lalr1.cc"
/* namespace to enclose parser in */
%name-prefix="example"
/* set the parser's class identifier */
%define "parser_class_name" "Parser"
/* keep track of the current position within the input */
%locations
%initial-action
{
// initialize the initial location object
@$.begin.filename = @$.end.filename = &driver.streamname;
};
/* The driver is passed by reference to the parser and to the scanner. This
* provides a simple but effective pure interface, not relying on global
* variables. */
%parse-param { class Driver& driver }
/* verbose error messages */
%error-verbose
/*** BEGIN EXAMPLE - Change the example grammar's tokens below ***/
%union {
bool boolVal;
int integerVal;
std::string* stringVal;
RelOp relopVal;
Keep keepVal;
class Vpkg* vpkgVal;
vpkglist_t* vpkglistVal;
list_vpkglist_t* listvpkglistVal;
propData* propVal;
pkgProps* propsVal;
propVariant* propertyVal;
}
%token END 0 "end of file"
%token EOL "end of line"
%token PREAMBLE "'preamble:'"
%token PROPERTYKW "'property:'"
%token PACKAGEKW "'package:'"
%token VERSIONKW "'version:'"
%token DEPENDSKW "'depends:'"
%token CONFLICTSKW "'conflicts:'"
%token PROVIDESKW "'provides:'"
%token INSTALLEDKW "'installed:'"
%token KEEPKW "'keep:'"
%token KEEPPACKAGE "'package'"
%token KEEPVERSION "'version'"
%token KEEPFEATURE "'feature'"
%token KEEPNONE "'none'"
%token REQUEST "'request:'"
%token UPGRADE "'upgrade:'"
%token INSTALL "'install:'"
%token REMOVE "'remove:'"
%token REQ "="
%token RNEQ "!="
%token RGT ">"
%token RGE ">="
%token RLT "<"
%token RLE "<="
%token <boolVal> BOOL "boolean ('true' or 'false')"
%token <integerVal> INTEGER "integer"
%token <stringVal> STRING "string"
%token <stringVal> PROPNAME "property name"
%token <stringVal> IDENT "identifier"
%type <relopVal> relop
%type<keepVal> keepprop
%type<vpkgVal> vpkg
%type<vpkglistVal> vpkglist vpkglist2
%type<listvpkglistVal> vpkgorlist
%type<propVal> pkgprop
%type<propsVal> pkgprops
%type<propertyVal> propval
%type <pkgVal> package universe
%destructor { delete $$; } STRING
%destructor { delete $$; } PROPNAME
%destructor { delete $$; } IDENT
%destructor { delete $$; } vpkg
%destructor { delete $$; } vpkglist
%destructor { delete $$; } vpkglist2
%destructor { delete $$; } vpkgorlist
%destructor { delete $$; } pkgprop
%{
#include "driver.h"
#include "scanner.h"
/* this "connects" the bison parser in the driver to the flex scanner class
* object. it defines the yylex() function call to pull the next token from the
* current lexer object of the driver context. */
#undef yylex
#define yylex driver.lexer->lex
%}
%% /*** Grammar Rules ***/
propinit :
/* empy */
| REQ '[' INTEGER ']'
propdef :
PROPNAME IDENT propinit
{
(void)$2;
(void)$1;
}
propdefs : /* empty */
| propdef
| propdefs ',' propdef
preamble : PREAMBLE EOL PROPERTYKW propdefs
{
//std::cerr << "recognized preamble" << std::endl;
}
relop :
REQ {$$ = ROP_EQ;}
| RNEQ {$$ = ROP_NEQ;}
| RGT {$$ = ROP_GT;}
| RGE {$$ = ROP_GE;}
| RLT {$$ = ROP_LT;}
| RLE {$$ = ROP_LE;}
vpkg :
IDENT {$$ = new Vpkg(*$1,ROP_NOP,0);}
|IDENT relop INTEGER
{
$$ = new Vpkg(*$1,$2,$3);
}
vpkglist :
vpkg
{
$$ = new vpkglist_t;
$$->push_back(*$1);
}
| vpkglist ',' vpkg
{
$$ = new vpkglist_t;
foreach(Vpkg v, *$1) {
$$->push_back(v);
}
$$->push_back(*$3);
}
vpkglist2 :
vpkg
{
$$ = new vpkglist_t;
$$->push_back(*$1);
}
| vpkglist2 '|' vpkg
{
$$ = new vpkglist_t;
foreach(Vpkg& v, *$1) {
$$->push_back(v);
}
$$->push_back(*$3);
}
vpkgorlist :
/* empty */
{
$$ = new list_vpkglist_t;
}
| vpkglist2
{
$$ = new list_vpkglist_t;
$$->push_back(*$1);
}
| vpkgorlist ',' vpkglist2
{
$$ = new list_vpkglist_t;
foreach(vpkglist_t& v, *$1) {
$$->push_back(v);
}
$$->push_back(*$3);
}
propval :
/* empty */
{
assert(false);
$$ = new propVariant;
}
|INTEGER { $$ = new propVariant($1); }
| IDENT { $$ = new propVariant(*$1); }
| BOOL { $$ = new propVariant($1); }
keepprop :
KEEPNONE { $$ = KP_NONE; }
| KEEPVERSION { $$ = KP_VERSION; }
| KEEPPACKAGE { $$ = KP_PACKAGE; }
| KEEPFEATURE { $$ = KP_FEATURE; }
pkgprop :
DEPENDSKW vpkgorlist EOL
{
$$ = new propData("_deps",*$2);
}
| CONFLICTSKW vpkglist EOL
{
$$ = new propData("_confs",*$2);
}
| PROVIDESKW vpkglist EOL
{
$$ = new propData("_pvds",*$2);
}
| KEEPKW keepprop EOL
{
$$ = new propData("_keep",$2);
}
| INSTALLEDKW BOOL EOL
{
$$ = new propData("_installed",$2);
}
|PROPNAME propval EOL
{
$$ = new propData(*$1,*$2);
}
pkgprops :
/*empty */
{
$$ = new pkgProps;
}
| pkgprop
{
$$ = new pkgProps;
$$->insert(std::make_pair($1->get<0>(),$1->get<1>()));
assert($$->size() == 1);
}
| pkgprops pkgprop
{
$$ = new pkgProps(*$1);
$$->insert(std::make_pair($2->get<0>(),$2->get<1>()));
assert($$->size() == $1->size() + 1);
}
package :
PACKAGEKW IDENT EOL VERSIONKW INTEGER EOL pkgprops
{
/* This rule does not return, it just access the document an register
the new package in the list. */
CudfPackage pkg;
pkg.pk_info.get<5>() = *$2;
pkg.pk_info.get<6>() = $5;
//std::cout << "size: " << $7->size() << std::endl;
pkgProps *props = $7;
//typedef std::pair<std::string,propVariant> data_t;
//foreach (const data_t& d, *$7) {
// std::cout << d.first << std::endl;
//}
if (props->count("_pvds") > 0) {
vpkglist_t& pvds = boost::get<vpkglist_t>(props->at("_pvds"));
pkg.pk_info.get<3>() = pvds;
}
if (props->count("_confs") > 0) {
vpkglist_t& cnfs = boost::get<vpkglist_t>(props->at("_confs"));
pkg.pk_info.get<2>() = cnfs;
}
if (props->count("_deps") > 0) {
list_vpkglist_t& deps = boost::get<list_vpkglist_t>(props->at("_deps"));
pkg.pk_info.get<4>() = deps;
}
if (props->count("_keep") > 0) {
std::cerr << "**KEEP**" << std::endl;
Keep k = boost::get<Keep>(props->at("_keep"));
pkg.pk_info.get<0>() = k;
} else {
pkg.pk_info.get<0>() = KP_NONE;
}
if (props->count("_installed") > 0) {
bool i = boost::get<bool>(props->at("_installed"));
pkg.pk_info.get<1>() = i;
//std::cout << "installed property found for package " << *$2 << std::endl;
} else {
pkg.pk_info.get<1>() = false;
}
// finally add the new created package
driver.doc.addPackage(pkg);
}
universe :
package
| universe EOL package
reqst :
INSTALL vpkglist EOL
{
driver.doc.addInstall(*$2);
}
| UPGRADE vpkglist EOL
{
driver.doc.addUpgrade(*$2);
}
| REMOVE vpkglist EOL
{
driver.doc.addRemove(*$2);
}
reqlist:
reqst
| reqlist reqst
request :
REQUEST EOL reqlist
start :
preamble EOL EOL universe EOL EOL request
{
}
%% /*** Additional Code ***/
void example::Parser::error(const Parser::location_type& l, const string& m)
{
driver.error(l, m);
}