-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgram.y
583 lines (530 loc) · 13.3 KB
/
gram.y
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
%{
/*
** RFC5246 Data definition language parser
** Rich Salz, rsalz@akamai.com, June 2013.
**
** Copyright 2013-2015, Rich Salz
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <unistd.h>
#include <stdarg.h>
#include "nodes.hpp"
using namespace std;
#define YYDEBUG 1
// Internals
typedef pair<const Node*,bool> Entry;
typedef map<string, Entry> Symtab;
enum lookup_type {
look_variant, look_size, look_member
};
static int depth = 0;
static int errors = 0;
static bool verbose = false;
static bool dupsok = false;
static const char* infile = "<stdin>";
static const char UNNAMED[] = "<unnamed>";
static Symtab table;
static void yyerror(const char* cp);
static void intern(const string* name, const Node* np, bool external=false);
static void lookup(const string& ref, lookup_type lt=look_size);
static void lookup(const NodeList& nlr);
static void checkdups(const string* name, const NodeList& nlr);
static void checkdups(const string* name, const ArmList& alr);
static void checkreflist(const ReferenceList& rlr);
%}
%union {
int num;
std::string* str;
IDSet* idset;
Node* node;
NodeList* nodelist;
Reference* ref;
ReferenceList* reflist;
Arm* arm;
ArmList* armlist;
}
%token tCASE tDOTDOT tENUM tEXTERN tSTRUCT tSELECT
%token <num> tAEAD_CIPHERED tBLOCK_CIPHERED tSTREAM_CIPHERED
%token <num> tDIGITALLY_SIGNED tPUBLIC_KEY_ENCRYPTED
%token <num> tNUMBER tOPAQUE tUINT8 tUINT16 tUINT24 tUINT32
%token <str> tID
%type <num> primitive opt_size crypto opt_crypto strprolog
%type <str> opt_id varprolog
%type <idset> cases external_choices internal_choices
%type <node> statement simple enumerated struct variant
%type <nodelist> list
%type <ref> reference
%type <reflist> references
%type <arm> arm
%type <armlist> arms
%error-verbose
%start list
%%
list
: statement {
$$ = new NodeList;
if ($1)
$$->push_back($1);
if (verbose)
printf("\nParsed ok\n---------\n");
}
| list statement {
$$ = $1;
if ($2)
$$->push_back($2);
if (verbose)
printf("\nParsed ok\n---------\n");
}
;
statement
: simple
| reference {
$$ = $1;
}
| enumerated
| struct
| variant
| error ';' {
$$ = NULL;
}
;
simple
: primitive tID ';' {
$$ = new Simple($2, $1);
intern($2, $$);
}
| primitive tID '[' tNUMBER ']' ';' {
if ($4 <= 0)
vyyerror("``%s'' size (%d) is not positive", $2->c_str(), $4);
$$ = new Simple($2, $1);
intern($2, $$);
}
| primitive tID '[' tID ']' ';' {
lookup(*$4);
$$ = new Simple($2, $1);
intern($2, $$);
}
| primitive tID '<' tNUMBER tDOTDOT tNUMBER '>' ';' {
if ($4 >= $6)
vyyerror("``%s'' range %d less than %d", $2->c_str(), $4, $6);
$$ = new Simple($2, $1);
intern($2, $$);
}
| tEXTERN tID ';' {
$$ = NULL;
intern($2, $$, true);
}
;
primitive
: tOPAQUE
| tUINT8
| tUINT16
| tUINT24
| tUINT32
;
reference
: tID tID ';' {
$$ = new Reference($2, $1);
}
| tID tID '[' tNUMBER ']' ';' {
if ($4 <= 0)
vyyerror("``%s'' size is not positive, %d", $2->c_str(), $4);
$$ = new Reference($2, $1);
}
| tID tID '[' tID ']' ';' {
lookup(*$4);
$$ = new Reference($2, $1);
}
| tID tID '<' tNUMBER tDOTDOT tNUMBER '>' ';' {
if ($4 >= $6)
vyyerror("``%s'' range %d less than %d", $2->c_str(), $4, $6);
$$ = new Reference($2, $1);
}
| crypto tID ';' {
$$ = new Reference(new string(UNNAMED), $2, $1);
}
| crypto tID tID ';' {
$$ = new Reference($3, $2, $1);
}
;
crypto
: tAEAD_CIPHERED
| tBLOCK_CIPHERED
| tSTREAM_CIPHERED
| tDIGITALLY_SIGNED
| tPUBLIC_KEY_ENCRYPTED
;
enumerated
: tENUM '{' internal_choices '}' tID ';' {
$$ = new Compound($5, tENUM, $3);
intern($5, $$);
}
| tENUM '{' external_choices opt_size '}' tID ';' {
set<int> values;
int size = $4;
IDSet::iterator end($3->end());
for (IDSet::iterator it($3->begin()); it != end; ++it) {
if (values.find(it->second) != values.end())
vyyerror("Enum ``%s'' duplicates value %d",
it->first.c_str(), it->second);
else
values.insert(it->second);
if (size && it->second > size) {
vyyerror("Value for ``%s'' is too big (%d > %d)",
it->first.c_str(), it->second, size);
}
}
$$ = new Compound($6, tENUM, $3);
intern($6, $$);
}
;
internal_choices
: tID {
$$ = new IDSet(*$1);
}
| internal_choices ',' tID {
if ($1->has(*$3))
vyyerror("Duplicate ``%s'' in enum", $3->c_str());
else
$1->insert(*$3);
$$ = $1;
}
;
external_choices
: tID '(' tNUMBER ')' {
$$ = new IDSet(*$1, $3);
}
| external_choices ',' tID '(' tNUMBER ')' {
if ($1->has(*$3))
vyyerror("Duplicate ``%s'' in enum", $3->c_str());
else
$1->insert(*$3, $5);
$$ = $1;
}
| external_choices ',' tID '(' tNUMBER tDOTDOT tNUMBER ')' {
if ($3->find("reserved") == string::npos
&& $3->find("RESERVED") == string::npos)
vyyerror("Enum range for ``%s'' for non-reserved name",
$3->c_str());
$$ = $1;
}
;
opt_size
: {
$$ = 0;
}
| ',' '(' tNUMBER ')' {
$$ = $3;
}
;
struct
: strprolog list '}' opt_id ';' {
--depth;
checkdups($4, *$2);
if ($4 == NULL)
$$ = NULL;
else {
$$ = new Compound($4, tSTRUCT, $2, $1);
intern($4, $$);
lookup(*$2);
}
}
| strprolog '}' opt_id ';' {
--depth;
if ($3 == NULL)
$$ = NULL;
else {
$$ = new Compound($3, tSTRUCT, new NodeList, $1);
intern($3, $$);
}
}
;
strprolog
: opt_crypto tSTRUCT '{' {
++depth;
$$ = $1;
}
;
opt_crypto
: {
$$ = 0;
}
| crypto
;
opt_id
: {
$$ = NULL;
}
| tID
;
variant
: varprolog arms '}' opt_id ';' {
--depth;
checkdups($4, *$2);
if ($4 == NULL)
$$ = NULL;
else {
$$ = new Compound($4, tSELECT, $2);
intern($4, $$);
}
}
;
varprolog
: tSELECT '(' tID ')' '{' {
lookup(*$3, look_variant);
++depth;
$$ = $3;
}
;
arms
: arm {
$$ = new ArmList;
$$->push_back($1);
}
| arms arm {
$$ = $1;
$$->push_back($2);
}
;
arm
: cases tID ';' {
ReferenceList* rlp = new ReferenceList;
rlp->push_back(new Reference(new string(UNNAMED), $2));
$$ = new Arm($1, rlp);
}
| cases references {
checkreflist(*$2);
$$ = new Arm($1, $2);
}
| cases ';' {
$$ = new Arm($1, new ReferenceList);
}
;
cases
: tCASE tID ':' {
$$ = new IDSet(*$2);
}
| cases tCASE tID ':' {
if ($1->has(*$3))
vyyerror("Duplicate case ``%s''", $3->c_str());
else
$1->insert(*$3);
$$ = $1;
}
;
references
: reference {
$$ = new ReferenceList;
$$->push_back($1);
}
| references reference {
$$ = $1;
$$->push_back($2);
}
;
%%
/// Set yydebug variable.
void
setyydebug(bool flag)
{
yydebug = flag ? 1 : 0;
printf("YYDEBUG set to %s\n", flag ? "ON" : "OFF");
}
/// Print an error message, bump error count.
static void
yyerror(const char* cp)
{
printf("%s:%d: error near ``%s'': %s\n",
infile, lineno, yytext, cp);
++errors;
}
/// Format an error message and report it.
void
vyyerror(const char* fmt, ...)
{
char buff[2048];
va_list ap;
va_start(ap, fmt);
vsnprintf(buff, sizeof buff, fmt, ap);
va_end(ap);
yyerror(buff);
}
/// Add a data type (name) to the symbol table.
static void
intern(const string* name, const Node* np, bool external)
{
// Only put items at top-level into the symbol table.
if (depth)
return;
// Check for dups, but ignore if it's an extern declaration.
if (!dupsok) {
Symtab::iterator it(table.find(*name));
if (it != table.end() && it->second.second == false) {
if (!external)
vyyerror("Duplicate symbol ``%s'' found", name->c_str());
return;
}
}
const string key(*name);
table[key] = make_pair(np, external);
}
/// Check that every name in a NodeList has been defined.
static void
lookup(const NodeList& nlr)
{
for (NodeList::const_iterator it(nlr.begin()); it != nlr.end(); ++it) {
const Node* n = *it;
if (dynamic_cast<const Simple*>(n) != NULL)
continue;
const Reference* r = dynamic_cast<const Reference*>(n);
if (r)
lookup(r->Ref(), look_member);
}
}
/// Lookup a name and print an error if not already defined.
static void
lookup(const string& sr, lookup_type lt)
{
if (table.find(sr) != table.end())
return;
switch (lt) {
case look_variant:
vyyerror("Unknown variant selector ``%s''", sr.c_str());
break;
case look_size:
vyyerror("Unknown size reference ``%s''", sr.c_str());
break;
case look_member:
vyyerror("Unknown member type ``%s''", sr.c_str());
break;
}
if (sr.find('.') != string::npos) {
// Work around a parser limitation. We can't do
// struct {
// uint8 size;
// opaque data[foo.size]; <<Field within struct being defined.
// } foo;
--errors;
static bool warned = false;
if (!warned) {
vyyerror("Note: cannot resolve dotted items.");
--errors;
warned = true;
}
return;
}
}
/// Check for duplicates in a NodeList
static void
checkdups(const string* name, const NodeList& nlr)
{
const char* namestr = name ? name->c_str() : UNNAMED;
set<string> names;
for (NodeList::const_iterator it(nlr.begin()); it != nlr.end(); ++it) {
const Node* n = *it;
const string* s(n->Name());
if (s == NULL || *s == UNNAMED)
continue;
if (names.find(*s) != names.end())
vyyerror("Duplicate item ``%s'' in ``%s''", s->c_str(), namestr);
else
names.insert(*s);
}
}
/// Check all the arms in a variant for duplicate values.
static void
checkdups(const string* name, const ArmList& alr)
{
const char* namestr = name ? name->c_str() : UNNAMED;
set<string> casenames;
for (ArmList::const_iterator it(alr.begin()); it != alr.end(); ++it) {
const Arm* a = *it;
IDSet::iterator end(a->Cases().end());
for (IDSet::iterator idit(a->Cases().begin()); idit != end; ++idit) {
if (casenames.find(idit->first) != casenames.end())
vyyerror("Duplicate case ``%s'' in ``%s''",
idit->first.c_str(), namestr);
else
casenames.insert(idit->first);
}
}
}
/// Check ReferenceList for duplicates and unknown names
static void
checkreflist(const ReferenceList& rlr)
{
set<string> names;
for (ReferenceList::const_iterator it(rlr.begin()); it != rlr.end(); ++it) {
const Reference* r = *it;
const string* s(r->Name());
if (s && *s != UNNAMED) {
if (names.find(*s) != names.end())
vyyerror("Duplicate item ``%s'' in variant arm", s->c_str());
else
names.insert(*s);
}
lookup(r->Ref(), look_member);
}
}
/// Print usage message and exit
static void
usage()
{
printf("usage:\n"
" parser [-d][-n][-v] [input]\n"
"where:\n"
" -d Turn on yydebug\n"
" -n Do not complain about duplicate names\n"
" -v Verbose (message when parsed succesfully)\n"
" input Input file, defaults to stdin\n"
);
exit(1);
}
int
main(int ac, char *av[])
{
int i;
// Parse JCL.
while ((i = getopt(ac, av, "dnv")) != EOF)
switch (i) {
default:
usage();
case 'd':
setyydebug(true);
break;
case 'n':
dupsok = true;
break;
case 'v':
verbose = true;
break;
}
ac -= optind;
av += optind;
// Open input.
if (ac == 1) {
infile = av[0];
if (freopen(infile, "r", stdin) == NULL) {
perror(infile);
return 1;
}
}
else if (ac != 0)
usage();
yyparse();
return errors;
}