-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathparser.c
790 lines (701 loc) · 14.7 KB
/
parser.c
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
/* $Id$ */
/*
* Copyright (c) 2017--2020 Kristaps Dzonsons <kristaps@bsd.lv>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#if HAVE_SYS_QUEUE
# include <sys/queue.h>
#endif
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ort.h"
#include "extern.h"
#include "parser.h"
/*
* Disallowed field names.
* The SQL ones are from https://sqlite.org/lang_keywords.html.
* FIXME: think about this more carefully, as in SQL, there are many
* things that we can put into string literals.
*/
static const char *const badidents[] = {
/* Things not allowed in C. */
"auto",
"break",
"case",
"char",
"const",
"continue",
"default",
"do",
"double",
"enum",
"extern",
"float",
"goto",
"long",
"register",
"short",
"signed",
"static",
"struct",
"typedef",
"union",
"unsigned",
"void",
"volatile",
/* Things not allowed in SQLite. */
"ABORT",
"ACTION",
"ADD",
"AFTER",
"ALL",
"ALTER",
"ANALYZE",
"AND",
"AS",
"ASC",
"ATTACH",
"AUTOINCREMENT",
"BEFORE",
"BEGIN",
"BETWEEN",
"BY",
"CASCADE",
"CASE",
"CAST",
"CHECK",
"COLLATE",
"COLUMN",
"COMMIT",
"CONFLICT",
"CONSTRAINT",
"CREATE",
"CROSS",
"CURRENT_DATE",
"CURRENT_TIME",
"CURRENT_TIMESTAMP",
"DATABASE",
"DEFAULT",
"DEFERRABLE",
"DEFERRED",
"DELETE",
"DESC",
"DETACH",
"DISTINCT",
"DROP",
"EACH",
"ELSE",
"END",
"ESCAPE",
"EXCEPT",
"EXCLUSIVE",
"EXISTS",
"EXPLAIN",
"FAIL",
"FOR",
"FOREIGN",
"FROM",
"FULL",
"GLOB",
"GROUP",
"HAVING",
"IF",
"IGNORE",
"IMMEDIATE",
"IN",
"INDEX",
"INDEXED",
"INITIALLY",
"INNER",
"INSERT",
"INSTEAD",
"INTERSECT",
"INTO",
"IS",
"ISNULL",
"JOIN",
"KEY",
"LEFT",
"LIKE",
"LIMIT",
"MATCH",
"NATURAL",
"NOT",
"NOTNULL",
"NULL",
"OF",
"OFFSET",
"ON",
"OR",
"ORDER",
"OUTER",
"PLAN",
"PRAGMA",
"PRIMARY",
"QUERY",
"RAISE",
"RECURSIVE",
"REFERENCES",
"REGEXP",
"REINDEX",
"RELEASE",
"RENAME",
"REPLACE",
"RESTRICT",
"RIGHT",
"ROLLBACK",
"ROW",
"SAVEPOINT",
"SELECT",
"SET",
"TABLE",
"TEMP",
"TEMPORARY",
"THEN",
"TO",
"TRANSACTION",
"TRIGGER",
"UNION",
"UNIQUE",
"UPDATE",
"USING",
"VACUUM",
"VALUES",
"VIEW",
"VIRTUAL",
"WHEN",
"WHERE",
"WITH",
"WITHOUT",
NULL
};
/*
* Copy the current parse point into a saved position buffer.
*/
void
parse_point(const struct parse *p, struct pos *pp)
{
pp->fname = p->fname;
pp->line = p->line;
pp->column = p->column;
}
void
parse_warnx(struct parse *p, const char *fmt, ...)
{
va_list ap;
struct pos pos;
pos.fname = p->fname;
pos.line = p->line;
pos.column = p->column;
if (fmt != NULL) {
va_start(ap, fmt);
ort_msgv(&p->cfg->mq, MSGTYPE_WARN, 0, &pos, fmt, ap);
va_end(ap);
} else
ort_msg(&p->cfg->mq, MSGTYPE_WARN, 0, &pos, NULL);
}
enum tok
parse_err(struct parse *p)
{
int er = errno;
struct pos pos;
pos.fname = p->fname;
pos.line = p->line;
pos.column = p->column;
ort_msg(&p->cfg->mq, MSGTYPE_FATAL, er, &pos, NULL);
p->lasttype = TOK_ERR;
return p->lasttype;
}
enum tok
parse_errx(struct parse *p, const char *fmt, ...)
{
va_list ap;
struct pos pos;
pos.fname = p->fname;
pos.line = p->line;
pos.column = p->column;
if (fmt != NULL) {
va_start(ap, fmt);
ort_msgv(&p->cfg->mq, MSGTYPE_ERROR, 0, &pos, fmt, ap);
va_end(ap);
} else
ort_msg(&p->cfg->mq, MSGTYPE_ERROR, 0, &pos, NULL);
p->lasttype = TOK_ERR;
return p->lasttype;
}
/*
* Push a single character into the retaining buffer.
* XXX: creates a binary buffer.
* Returns zero on memory failure, non-zero otherwise.
*/
static int
buf_push(struct parse *p, int c)
{
void *pp;
const size_t inc = 1024;
if (p->bufsz + 1 >= p->bufmax) {
pp = realloc(p->buf, p->bufmax + inc);
if (pp == NULL) {
parse_err(p);
return 0;
}
p->buf = pp;
p->bufmax += inc;
}
p->buf[p->bufsz++] = (char)c;
return 1;
}
/*
* Check to see whether "s" is a reserved identifier or not.
* Returns zero if this is a reserved identifier, non-zero if it's ok
*/
int
parse_check_badidents(struct parse *p, const char *s)
{
const char *const *cp;
for (cp = badidents; *cp != NULL; cp++)
if (strcasecmp(*cp, s) == 0) {
parse_errx(p, "reserved identifier");
return 0;
}
return 1;
}
/*
* For all structs, enumerations, and bitfields, make sure that we don't
* have any of the same top-level names.
* That's because the C output for these will be, given "name", "struct
* name", "enum name", which can't be the same.
*/
int
parse_check_dupetoplevel(struct parse *p, const char *name)
{
const struct enm *e;
const struct bitf *b;
const struct strct *s;
TAILQ_FOREACH(e, &p->cfg->eq, entries)
if (0 == strcasecmp(e->name, name)) {
parse_errx(p, "duplicates enum name: "
"%s:%zu:%zu", e->pos.fname,
e->pos.line, e->pos.column);
return(0);
}
TAILQ_FOREACH(b, &p->cfg->bq, entries)
if (0 == strcasecmp(b->name, name)) {
parse_errx(p, "duplicates bitfield name: "
"%s:%zu:%zu", b->pos.fname,
b->pos.line, b->pos.column);
return(0);
}
TAILQ_FOREACH(s, &p->cfg->sq, entries)
if (0 == strcasecmp(s->name, name)) {
parse_errx(p, "duplicates struct name: "
"%s:%zu:%zu", s->pos.fname,
s->pos.line, s->pos.column);
return(0);
}
return(1);
}
static int
parse_check_eof(struct parse *p)
{
return feof(p->f);
}
static int
parse_check_error(struct parse *p)
{
return ferror(p->f);
}
static int
parse_check_error_eof(struct parse *p)
{
return feof(p->f) || ferror(p->f);
}
static void
parse_ungetc(struct parse *p, int c)
{
/* FIXME: puts us at last line, not column */
if ('\n' == c)
p->line--;
else if (p->column > 0)
p->column--;
ungetc(c, p->f);
}
/*
* Get the next character and advance us within the file.
* If we've already reached an error condition, this just keeps us
* there.
*/
static int
parse_nextchar(struct parse *p)
{
int c;
c = fgetc(p->f);
if (parse_check_error_eof(p))
return EOF;
p->column++;
if ('\n' == c) {
p->line++;
p->column = 0;
}
return c;
}
/*
* Trigger a "hard" error condition at stream error or EOF.
* This sets the lasttype appropriately.
*/
static enum tok
parse_read_err(struct parse *p)
{
if (parse_check_error(p))
parse_err(p);
else
p->lasttype = TOK_EOF;
return p->lasttype;
}
/*
* Parse the next token from "f".
* If we've already encountered an error or an EOF condition, this
* doesn't do anything.
* Otherwise, lasttype will be set to the last token type.
*/
enum tok
parse_next(struct parse *p)
{
int c, last, hasdot, minus = 0;
const char *ep = NULL;
char *epp = NULL;
again:
if (PARSE_STOP(p))
return(p->lasttype);
/*
* Read past any white-space.
* If we're at the EOF or error, just return.
*/
do {
c = parse_nextchar(p);
} while (isspace(c));
if (parse_check_error_eof(p))
return(parse_read_err(p));
if ('#' == c) {
do {
c = parse_nextchar(p);
} while (EOF != c && '\n' != c);
if ('\n' != c)
return(parse_read_err(p));
goto again;
}
/*
* The "-" appears before integers and doubles.
* It has no other syntactic function.
*/
if ('-' == c) {
c = parse_nextchar(p);
if (parse_check_error_eof(p))
return(parse_read_err(p));
if ( ! isdigit(c))
return(parse_errx(p, "expected digit"));
minus = 1;
}
/*
* Now we're at a hard token.
* It's either going to be a simple lexical token (like a
* right-brace) or part of a multi-character sequence.
*/
if ('}' == c) {
p->lasttype = TOK_RBRACE;
} else if ('{' == c) {
p->lasttype = TOK_LBRACE;
} else if (';' == c) {
p->lasttype = TOK_SEMICOLON;
} else if (',' == c) {
p->lasttype = TOK_COMMA;
} else if ('.' == c) {
p->lasttype = TOK_PERIOD;
} else if (':' == c) {
p->lasttype = TOK_COLON;
} else if ('"' == c) {
p->bufsz = 0;
last = ' ';
for (;;) {
c = parse_nextchar(p);
/* Stop at eof or unescaped quote. */
if (c == EOF || (c == '"' && last != '\\'))
break;
/* Strip out CRLF. */
if (last == '\r' && c == '\n') {
p->bufsz--;
last = p->bufsz == 0 ?
' ' : p->buf[p->bufsz];
}
if (!buf_push(p, c))
return TOK_ERR;
last = c;
}
if (parse_check_error(p))
return(parse_read_err(p));
if ( ! buf_push(p, '\0'))
return TOK_ERR;
p->last.string = p->buf;
p->lasttype = TOK_LITERAL;
} else if (isdigit(c)) {
hasdot = 0;
p->bufsz = 0;
if (minus && ! buf_push(p, '-'))
return TOK_ERR;
if ( ! buf_push(p, c))
return TOK_ERR;
do {
c = parse_nextchar(p);
/*
* Check for a decimal number: if we encounter a
* full-stop within the number, we convert to a
* decimal value.
* But only for the first decimal point.
*/
if ('.' == c) {
if (hasdot)
break;
hasdot = 1;
}
if (isdigit(c) || '.' == c)
if ( ! buf_push(p, c))
return TOK_ERR;
} while (isdigit(c) || '.' == c);
if (parse_check_error(p))
return(parse_read_err(p));
else if ( ! parse_check_eof(p))
parse_ungetc(p, c);
if ( ! buf_push(p, '\0'))
return TOK_ERR;
if (hasdot) {
p->last.decimal = strtod(p->buf, &epp);
if (epp == p->buf || ERANGE == errno)
return(parse_errx(p,
"malformed decimal"));
p->lasttype = TOK_DECIMAL;
} else {
p->last.integer = strtonum
(p->buf, -INT64_MAX, INT64_MAX, &ep);
if (NULL != ep)
return(parse_errx(p,
"malformed integer"));
p->lasttype = TOK_INTEGER;
}
} else if (isalpha(c)) {
p->bufsz = 0;
if ( ! buf_push(p, c))
return TOK_ERR;
do {
c = parse_nextchar(p);
if (isalnum(c) && ! buf_push(p, c))
return TOK_ERR;
} while (isalnum(c));
if (parse_check_error(p))
return(parse_read_err(p));
else if ( ! parse_check_eof(p))
parse_ungetc(p, c);
if ( ! buf_push(p, '\0'))
return TOK_ERR;
p->last.string = p->buf;
p->lasttype = TOK_IDENT;
} else
return(parse_errx(p, "unknown input token"));
return(p->lasttype);
}
/*
* Parse the quoted_string part following "jslabel":
*
* jslabel ["." language] string_literal
*
* Attach it to the given queue of labels, possibly clearing out any
* prior labels of the same language.
* If this is the case, emit a warning.
* Return zero on failure, non-zero on success.
*/
int
parse_label(struct parse *p, struct labelq *q)
{
size_t lang = 0;
struct label *l;
void *pp;
char *cp;
/*
* If we have a period (like jslabel.en), then interpret the
* word after the period as a language code for which we'll use
* the label.
*/
if (parse_next(p) == TOK_PERIOD) {
if (parse_next(p) != TOK_IDENT) {
parse_errx(p, "expected language");
return 0;
}
assert(p->last.string[0] != '\0');
for ( ; lang < p->cfg->langsz; lang++)
if (strcasecmp
(p->cfg->langs[lang], p->last.string) == 0)
break;
if (lang == p->cfg->langsz) {
pp = reallocarray
(p->cfg->langs,
p->cfg->langsz + 1,
sizeof(char *));
if (pp == NULL) {
parse_err(p);
return 0;
}
p->cfg->langs = pp;
p->cfg->langs[p->cfg->langsz] =
strdup(p->last.string);
if (p->cfg->langs[p->cfg->langsz] == NULL) {
parse_err(p);
return 0;
}
cp = p->cfg->langs[p->cfg->langsz++];
for (; *cp != '\0'; cp++)
*cp = (char)tolower((unsigned char)*cp);
}
parse_next(p);
} else if (p->lasttype != TOK_LITERAL) {
parse_errx(p, "expected period or quoted string");
return 0;
}
/* Now for the label itself. */
if (p->lasttype != TOK_LITERAL) {
parse_errx(p, "expected quoted string");
return 0;
} else if (p->last.string[0] == '\0') {
parse_errx(p, "label must be non-empty");
return 0;
}
/* Disallow duplicates. */
TAILQ_FOREACH(l, q, entries)
if (lang == l->lang) {
parse_errx(p, "duplicate label");
return 0;
}
l = calloc(1, sizeof(struct label));
if (l == NULL) {
parse_err(p);
return 0;
}
TAILQ_INSERT_TAIL(q, l, entries);
parse_point(p, &l->pos);
l->lang = lang;
l->label = strdup(p->last.string);
if (l->label == NULL) {
parse_err(p);
return 0;
}
return 1;
}
/*
* Parse the quoted_string part following "comment".
* Attach it to the given "doc".
*/
int
parse_comment(struct parse *p, char **doc)
{
if (parse_next(p) != TOK_LITERAL) {
parse_errx(p, "expected quoted string");
return 0;
} else if (*doc != NULL) {
parse_errx(p, "duplicate comment");
return 0;
}
if ((*doc = strdup(p->last.string)) == NULL) {
parse_err(p);
return 0;
}
return 1;
}
/*
* Re-entrant top-level parse with syntax:
*
* [ "struct" ident STRUCT |
* "enum" ident ENUM |
* "bitfield" ident BITFIELD |
* "roles" ident ROLESET ]*
*
* Returns zero on failure, non-zero otherwise.
* This doesn't guarantee that anything has actually been parsed: this
* must be done after invoking parse_root() for as many input blocks as
* possible.
*/
static int
parse_root(struct parse *p)
{
for (;;) {
if (parse_next(p) == TOK_ERR)
return 0;
else if (p->lasttype == TOK_EOF)
break;
/* Our top-level identifier. */
if (p->lasttype != TOK_IDENT) {
parse_errx(p, "expected top-level type");
continue;
}
if (strcasecmp(p->last.string, "roles") == 0)
parse_roles(p);
else if (strcasecmp(p->last.string, "struct") == 0)
parse_struct(p);
else if (strcasecmp(p->last.string, "enum") == 0)
parse_enum(p);
else if (strcasecmp(p->last.string, "bits") == 0 ||
strcasecmp(p->last.string, "bitfield") == 0)
parse_bitfield(p);
else
parse_errx(p, "unknown top-level type");
}
return 1;
}
/*
* Parse file "f", augmenting the configuration already in "cfg".
* Returns zero on failure, non-zero on success.
*/
int
ort_parse_file(struct config *cfg, FILE *f, const char *fname)
{
struct parse p;
int rc = 0;
void *pp;
pp = reallocarray(cfg->fnames,
cfg->fnamesz + 1, sizeof(char *));
if (pp == NULL) {
ort_msg(&cfg->mq, MSGTYPE_FATAL, ENOMEM, NULL, NULL);
return 0;
}
cfg->fnames = pp;
cfg->fnamesz++;
cfg->fnames[cfg->fnamesz - 1] = strdup(fname);
if (cfg->fnames[cfg->fnamesz - 1] == NULL) {
ort_msg(&cfg->mq, MSGTYPE_FATAL, ENOMEM, NULL, NULL);
return 0;
}
memset(&p, 0, sizeof(struct parse));
p.column = 0;
p.line = 1;
p.fname = cfg->fnames[cfg->fnamesz - 1];
p.f = f;
p.cfg = cfg;
rc = parse_root(&p);
free(p.buf);
return rc;
}