Repository navigation
Expand file tree
/
Copy pathparser_enum.c
More file actions
292 lines (251 loc) · 6.77 KB
/
Copy pathparser_enum.c
File metadata and controls
292 lines (251 loc) · 6.77 KB
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
/* $Id$ */
/*
* Copyright (c) 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 <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ort.h"
#include "extern.h"
#include "parser.h"
/*
* Parse an enumeration item whose value may be defined or automatically
* assigned at link time. Its syntax is:
*
* "item" ident [value]? ["comment" quoted_string]? ";"
*
* The identifier has already been parsed: this starts at the value.
* Both the identifier and the value (if provided) must be unique within
* the parent enumeration.
*/
static void
parse_enum_item(struct parse *p, struct eitem *ei)
{
struct eitem *eei;
const char *next;
if (parse_next(p) == TOK_INTEGER) {
ei->value = p->last.integer;
if (ei->value == INT64_MAX ||
ei->value == INT64_MIN) {
parse_errx(p, "enum item value too big or small");
return;
}
TAILQ_FOREACH(eei, &ei->parent->eq, entries) {
if (ei == eei || (eei->flags & EITEM_AUTO) ||
ei->value != eei->value)
continue;
parse_errx(p, "duplicate enum item value");
return;
}
parse_next(p);
} else
ei->flags |= EITEM_AUTO;
while (!PARSE_STOP(p) && p->lasttype == TOK_IDENT) {
next = p->last.string;
if (strcasecmp(next, "comment") == 0) {
if (!parse_comment(p, &ei->doc))
return;
parse_next(p);
} else if (strcasecmp(next, "jslabel") == 0) {
parse_label(p, &ei->labels);
parse_next(p);
} else
parse_errx(p, "unknown enum item attribute");
}
if (!PARSE_STOP(p) && p->lasttype != TOK_SEMICOLON)
parse_errx(p, "expected semicolon");
}
/*
* Parse semicolon-terminated labels of special phrase.
* Return zero on failure, non-zero on success.
*/
static int
parse_enum_label(struct parse *p, struct labelq *q)
{
for (;;) {
if (parse_next(p) == TOK_SEMICOLON)
return 1;
if (p->lasttype != TOK_IDENT ||
strcasecmp(p->last.string, "jslabel")) {
parse_errx(p, "expected \"jslabel\"");
return 0;
}
if (!parse_label(p, q))
return 0;
}
}
/*
* Read an individual enumeration.
* This opens and closes the enumeration, then reads all of the enum
* data within.
* Its syntax is:
*
* "{"
* ["item" ident ITEM]+
* ["comment" quoted_string]?
* "};"
*/
static void
parse_enum_data(struct parse *p, struct enm *e)
{
struct eitem *ei;
int64_t maxvalue = INT64_MIN;
int hasauto = 0;
char *cp;
if (parse_next(p) != TOK_LBRACE) {
parse_errx(p, "expected left brace");
return;
}
while (!PARSE_STOP(p)) {
if (parse_next(p) == TOK_RBRACE)
break;
if (p->lasttype != TOK_IDENT) {
parse_errx(p, "expected enum attribute");
return;
}
if (strcasecmp(p->last.string, "comment") == 0) {
if (!parse_comment(p, &e->doc))
return;
if (parse_next(p) != TOK_SEMICOLON) {
parse_errx(p, "expected semicolon");
return;
}
continue;
} else if (strcasecmp(p->last.string, "isnull") == 0) {
if (!parse_enum_label(p, &e->labels_null))
return;
continue;
} else if (strcasecmp(p->last.string, "item")) {
parse_errx(p, "unknown enum attribute");
return;
}
/* Now we have a new item: validate and parse it. */
if (parse_next(p) != TOK_IDENT) {
parse_errx(p, "expected enum item name");
return;
} else if (!parse_check_badidents(p, p->last.string))
return;
if (strcasecmp(p->last.string, "format") == 0) {
parse_errx(p, "cannot use reserved name");
return;
}
TAILQ_FOREACH(ei, &e->eq, entries) {
if (strcasecmp(ei->name, p->last.string))
continue;
parse_errx(p, "duplicate enum item name");
return;
}
if ((ei = calloc(1, sizeof(struct eitem))) == NULL) {
parse_err(p);
return;
}
TAILQ_INSERT_TAIL(&e->eq, ei, entries);
TAILQ_INIT(&ei->labels);
parse_point(p, &ei->pos);
ei->parent = e;
if ((ei->name = strdup(p->last.string)) == NULL) {
parse_err(p);
return;
}
for (cp = ei->name; *cp != '\0'; cp++)
*cp = (char)tolower((unsigned char)*cp);
parse_enum_item(p, ei);
if (hasauto == 0 && (ei->flags & EITEM_AUTO))
hasauto = 1;
}
/*
* If we have any values to assign automatically, do so here.
* First we determine the greatest current value.
* If negative, start from zero.
* Then increase monotonically.
*/
if (hasauto) {
TAILQ_FOREACH(ei, &e->eq, entries)
if (!(ei->flags & EITEM_AUTO))
if (ei->value > maxvalue)
maxvalue = ei->value;
maxvalue = maxvalue < 0 ? 0 : maxvalue + 1;
TAILQ_FOREACH(ei, &e->eq, entries)
if (maxvalue == INT64_MAX) {
parse_errx(p, "integer overflow "
"when assigning dynamic "
"enum value");
return;
} else if ((ei->flags & EITEM_AUTO))
ei->value = maxvalue++;
}
if (PARSE_STOP(p))
return;
if (parse_next(p) != TOK_SEMICOLON)
parse_errx(p, "expected semicolon");
else if (TAILQ_EMPTY(&e->eq))
parse_errx(p, "no items in enum");
}
/*
* Verify and allocate an enum, then start parsing it.
*/
void
parse_enum(struct parse *p)
{
struct enm *e;
struct eitem *ei;
int foundauto = 0;
char *cp;
int64_t maxv = INT64_MIN;
if (parse_next(p) != TOK_IDENT) {
parse_errx(p, "expected enum name");
return;
}
/* Disallow top-level duplicate names and bad names. */
if (!parse_check_dupetoplevel(p, p->last.string) ||
!parse_check_badidents(p, p->last.string))
return;
if ((e = calloc(1, sizeof(struct enm))) == NULL) {
parse_err(p);
return;
}
parse_point(p, &e->pos);
TAILQ_INSERT_TAIL(&p->cfg->eq, e, entries);
TAILQ_INIT(&e->eq);
TAILQ_INIT(&e->labels_null);
if ((e->name = strdup(p->last.string)) == NULL) {
parse_err(p);
return;
}
for (cp = e->name; *cp != '\0'; cp++)
*cp = (char)tolower((unsigned char)*cp);
parse_enum_data(p, e);
/* Get the sup(max) bount below at zero. */
TAILQ_FOREACH(ei, &e->eq, entries)
if (!(ei->flags & EITEM_AUTO) && ei->value > maxv) {
maxv = ei->value;
foundauto = 1;
}
/* Auto-assign values, if not given. */
if (foundauto) {
maxv = maxv < 0 ? 0 : maxv + 1;
TAILQ_FOREACH(ei, &e->eq, entries)
if (ei->flags & EITEM_AUTO)
ei->value = maxv++;
}
}