-
Notifications
You must be signed in to change notification settings - Fork 28
/
pcc_ast.peg
413 lines (376 loc) · 14.1 KB
/
pcc_ast.peg
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
# Copyright (c) 2024 Arihiro Yoshida. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# PackCC AST import file version: 1.0.0
%header {
#define pcc_ast_node__create_0() pcc_ast_node__create_nullary(auxil)
#define pcc_ast_node__create_1(node) pcc_ast_node__create_unary(auxil, node)
#define pcc_ast_node__create_2(node0, node1) pcc_ast_node__create_binary(auxil, node0, node1)
#define pcc_ast_node__create_3(node0, node1, node2) pcc_ast_node__create_ternary(auxil, node0, node1, node2)
#define pcc_ast_node__create_v() pcc_ast_node__create_variadic(auxil)
#ifdef __cplusplus
extern "C" {
#endif
typedef struct pcc_ast_manager_tag pcc_ast_manager_t;
typedef struct pcc_ast_node_tag pcc_ast_node_t;
typedef enum pcc_ast_node_type_tag {
PCC_AST_NODE_TYPE_NULLARY,
PCC_AST_NODE_TYPE_UNARY,
PCC_AST_NODE_TYPE_BINARY,
PCC_AST_NODE_TYPE_TERNARY,
PCC_AST_NODE_TYPE_VARIADIC
} pcc_ast_node_type_t;
typedef struct pcc_ast_node_single_tag {
pcc_ast_node_t *node;
} pcc_ast_node_single_t;
typedef struct pcc_ast_node_double_tag {
pcc_ast_node_t *node[2];
} pcc_ast_node_double_t;
typedef struct pcc_ast_node_triple_tag {
pcc_ast_node_t *node[3];
} pcc_ast_node_triple_t;
typedef struct pcc_ast_node_array_tag {
pcc_ast_node_t **node;
size_t max, len;
} pcc_ast_node_array_t;
typedef union pcc_ast_node_data_tag {
pcc_ast_node_single_t unary;
pcc_ast_node_double_t binary;
pcc_ast_node_triple_t ternary;
pcc_ast_node_array_t variadic;
} pcc_ast_node_data_t;
struct pcc_ast_node_tag {
pcc_ast_manager_t *manager;
pcc_ast_node_t *next; /* the next element in the doubly linked list of managed nodes */
pcc_ast_node_t *prev; /* the previous element in the doubly linked list of managed nodes */
pcc_ast_node_t *parent;
pcc_ast_node_type_t type;
pcc_ast_node_data_t data;
#ifdef PCC_AST_NODE_CUSTOM_DATA_DEFINED
pcc_ast_node_custom_data_t custom;
#endif
};
struct pcc_ast_manager_tag {
pcc_ast_node_t *first; /* the first element in the doubly linked list of managed nodes */
#ifdef PCC_AST_MANAGER_CUSTOM_DATA_DEFINED
pcc_ast_manager_custom_data_t custom;
#endif
};
void pcc_ast_manager__initialize(pcc_ast_manager_t *obj);
void pcc_ast_manager__finalize(pcc_ast_manager_t *obj);
void pcc_ast_node_array__initialize(pcc_ast_manager_t *mgr, pcc_ast_node_array_t *obj);
void pcc_ast_node_array__finalize(pcc_ast_manager_t *mgr, pcc_ast_node_array_t *obj);
void pcc_ast_node_array__add(pcc_ast_manager_t *mgr, pcc_ast_node_array_t *obj, pcc_ast_node_t *node);
pcc_ast_node_t *pcc_ast_node__create_nullary(pcc_ast_manager_t *mgr);
pcc_ast_node_t *pcc_ast_node__create_unary(pcc_ast_manager_t *mgr, pcc_ast_node_t *node);
pcc_ast_node_t *pcc_ast_node__create_binary(pcc_ast_manager_t *mgr, pcc_ast_node_t *node0, pcc_ast_node_t *node1);
pcc_ast_node_t *pcc_ast_node__create_ternary(pcc_ast_manager_t *mgr, pcc_ast_node_t *node0, pcc_ast_node_t *node1, pcc_ast_node_t *node2);
pcc_ast_node_t *pcc_ast_node__create_variadic(pcc_ast_manager_t *mgr);
pcc_ast_node_t *pcc_ast_node__add_child(pcc_ast_node_t *obj, pcc_ast_node_t *node); /* for variadic node only */
void pcc_ast_node__destroy(pcc_ast_node_t *obj);
#ifdef PCC_AST_MANAGER_CUSTOM_DATA_DEFINED
void pcc_ast_manager_custom_data__initialize(pcc_ast_manager_custom_data_t *obj);
void pcc_ast_manager_custom_data__finalize(pcc_ast_manager_custom_data_t *obj);
#else
#define pcc_ast_manager_custom_data__initialize(obj) ((void)0)
#define pcc_ast_manager_custom_data__finalize(obj) ((void)0)
#endif
#ifdef PCC_AST_NODE_CUSTOM_DATA_DEFINED
void pcc_ast_node_custom_data__initialize(pcc_ast_node_custom_data_t *obj);
void pcc_ast_node_custom_data__finalize(pcc_ast_node_custom_data_t *obj);
#else
#define pcc_ast_node_custom_data__initialize(obj) ((void)0)
#define pcc_ast_node_custom_data__finalize(obj) ((void)0)
#endif
#ifdef __cplusplus
}
#endif
}
%source {
#include <assert.h>
}
%%
#ifndef PCC_AST_NODE_ARRAY_MIN_SIZE
#define PCC_AST_NODE_ARRAY_MIN_SIZE 4
#endif
#ifndef PCC_AST_MALLOC
#define PCC_AST_MALLOC(mgr, size) PCC_MALLOC(mgr, size)
#endif
#ifndef PCC_AST_REALLOC
#define PCC_AST_REALLOC(mgr, ptr, size) PCC_REALLOC(mgr, ptr, size)
#endif
#ifndef PCC_AST_FREE
#define PCC_AST_FREE(mgr, ptr) PCC_FREE(mgr, ptr)
#endif
void pcc_ast_manager__initialize(pcc_ast_manager_t *obj) {
obj->first = NULL;
pcc_ast_manager_custom_data__initialize(&(obj->custom));
}
void pcc_ast_manager__finalize(pcc_ast_manager_t *obj) {
pcc_ast_manager_custom_data__finalize(&(obj->custom));
while (obj->first) {
assert(obj->first->manager == obj);
pcc_ast_node__destroy(obj->first);
}
}
void pcc_ast_node_array__initialize(pcc_ast_manager_t *mgr, pcc_ast_node_array_t *obj) {
obj->len = 0;
obj->max = 0;
obj->node = NULL;
}
void pcc_ast_node_array__finalize(pcc_ast_manager_t *mgr, pcc_ast_node_array_t *obj) {
while (obj->len > 0) {
obj->len--;
pcc_ast_node__destroy(obj->node[obj->len]);
}
PCC_AST_FREE(mgr, obj->node);
}
void pcc_ast_node_array__add(pcc_ast_manager_t *mgr, pcc_ast_node_array_t *obj, pcc_ast_node_t *node) {
if (obj->max <= obj->len) {
const size_t n = obj->len + 1;
size_t m = obj->max;
if (m == 0) m = PCC_AST_NODE_ARRAY_MIN_SIZE;
while (m < n && m != 0) m <<= 1;
if (m == 0) m = n;
obj->node = (pcc_ast_node_t **)PCC_AST_REALLOC(mgr, obj->node, sizeof(pcc_ast_node_t *) * m);
obj->max = m;
}
obj->node[obj->len++] = node;
}
static void pcc_ast_node__unlink_parent_(pcc_ast_node_t *obj) {
if (obj->parent) {
switch (obj->parent->type) {
case PCC_AST_NODE_TYPE_NULLARY:
assert(((void)"Nullary node illegally linked as a parent", 0));
break;
case PCC_AST_NODE_TYPE_UNARY:
if (obj->parent->data.unary.node == obj)
obj->parent->data.unary.node = NULL;
else
assert(((void)"parent node with no backward link", 0));
break;
case PCC_AST_NODE_TYPE_BINARY:
{
size_t i;
for (i = 0; i < 2; i++) {
if (obj->parent->data.binary.node[i] == obj) break;
}
if (i < 2)
obj->parent->data.binary.node[i] = NULL;
else
assert(((void)"parent node with no backward link", 0));
}
break;
case PCC_AST_NODE_TYPE_TERNARY:
{
size_t i;
for (i = 0; i < 3; i++) {
if (obj->parent->data.ternary.node[i] == obj) break;
}
if (i < 3)
obj->parent->data.ternary.node[i] = NULL;
else
assert(((void)"parent node with no backward link", 0));
}
break;
case PCC_AST_NODE_TYPE_VARIADIC:
{
const size_t n = obj->parent->data.variadic.len;
size_t i;
for (i = 0; i < n; i++) {
if (obj->parent->data.variadic.node[i] == obj) break;
}
if (i < n)
obj->parent->data.variadic.node[i] = NULL;
else
assert(((void)"parent node with no backward link", 0));
}
break;
default:
assert(((void)"Node type unknown", 0));
break;
}
obj->parent = NULL;
}
}
static pcc_ast_node_t *pcc_ast_node__create_(pcc_ast_manager_t *mgr) {
pcc_ast_node_t *const obj = (pcc_ast_node_t *)PCC_AST_MALLOC(mgr, sizeof(pcc_ast_node_t));
obj->manager = mgr;
if (mgr->first) {
obj->next = mgr->first;
assert(((void)"Inconsistency in the doubly linked list of managed nodes", obj->next->prev == NULL));
obj->next->prev = obj;
}
else {
obj->next = NULL;
}
obj->prev = NULL;
mgr->first = obj;
obj->parent = NULL;
return obj;
}
pcc_ast_node_t *pcc_ast_node__create_nullary(pcc_ast_manager_t *mgr) {
pcc_ast_node_t *const obj = pcc_ast_node__create_(mgr);
obj->type = PCC_AST_NODE_TYPE_NULLARY;
pcc_ast_node_custom_data__initialize(&(obj->custom));
return obj;
}
pcc_ast_node_t *pcc_ast_node__create_unary(pcc_ast_manager_t *mgr, pcc_ast_node_t *node) {
pcc_ast_node_t *const obj = pcc_ast_node__create_(mgr);
obj->type = PCC_AST_NODE_TYPE_UNARY;
obj->data.unary.node = node;
if (node) {
pcc_ast_node__unlink_parent_(node);
node->parent = obj;
}
pcc_ast_node_custom_data__initialize(&(obj->custom));
return obj;
}
pcc_ast_node_t *pcc_ast_node__create_binary(pcc_ast_manager_t *mgr, pcc_ast_node_t *node0, pcc_ast_node_t *node1) {
pcc_ast_node_t *const obj = pcc_ast_node__create_(mgr);
obj->type = PCC_AST_NODE_TYPE_BINARY;
obj->data.binary.node[0] = node0;
obj->data.binary.node[1] = node1;
{
size_t i = 0;
for (i = 0; i < 2; i++) {
pcc_ast_node_t *const node = obj->data.binary.node[i];
if (node) {
pcc_ast_node__unlink_parent_(node);
node->parent = obj;
}
}
}
pcc_ast_node_custom_data__initialize(&(obj->custom));
return obj;
}
pcc_ast_node_t *pcc_ast_node__create_ternary(pcc_ast_manager_t *mgr, pcc_ast_node_t *node0, pcc_ast_node_t *node1, pcc_ast_node_t *node2) {
pcc_ast_node_t *const obj = pcc_ast_node__create_(mgr);
obj->type = PCC_AST_NODE_TYPE_TERNARY;
obj->data.ternary.node[0] = node0;
obj->data.ternary.node[1] = node1;
obj->data.ternary.node[2] = node2;
{
size_t i = 0;
for (i = 0; i < 3; i++) {
pcc_ast_node_t *const node = obj->data.ternary.node[i];
if (node) {
pcc_ast_node__unlink_parent_(node);
node->parent = obj;
}
}
}
pcc_ast_node_custom_data__initialize(&(obj->custom));
return obj;
}
pcc_ast_node_t *pcc_ast_node__create_variadic(pcc_ast_manager_t *mgr) {
pcc_ast_node_t *const obj = pcc_ast_node__create_(mgr);
obj->type = PCC_AST_NODE_TYPE_VARIADIC;
pcc_ast_node_array__initialize(mgr, &(obj->data.variadic));
pcc_ast_node_custom_data__initialize(&(obj->custom));
return obj;
}
pcc_ast_node_t *pcc_ast_node__add_child(pcc_ast_node_t *obj, pcc_ast_node_t *node) {
if (obj->type == PCC_AST_NODE_TYPE_VARIADIC) {
pcc_ast_node_array__add(obj->manager, &(obj->data.variadic), node);
if (node) {
pcc_ast_node__unlink_parent_(node);
node->parent = obj;
}
}
else {
assert(((void)"Attempted to add node to non-variadic node", obj->next->prev == NULL));
}
return obj;
}
void pcc_ast_node__destroy(pcc_ast_node_t *obj) {
if (obj) {
pcc_ast_node_custom_data__finalize(&(obj->custom));
switch (obj->type) {
case PCC_AST_NODE_TYPE_NULLARY:
break;
case PCC_AST_NODE_TYPE_UNARY:
{
pcc_ast_node_t *const node = obj->data.unary.node;
if (node) {
assert(node->parent == obj);
node->parent = NULL;
pcc_ast_node__destroy(node);
}
}
break;
case PCC_AST_NODE_TYPE_BINARY:
{
size_t i;
for (i = 0; i < 2; i++) {
pcc_ast_node_t *const node = obj->data.binary.node[i];
if (node) {
assert(node->parent == obj);
node->parent = NULL;
pcc_ast_node__destroy(node);
}
}
}
break;
case PCC_AST_NODE_TYPE_TERNARY:
{
size_t i;
for (i = 0; i < 3; i++) {
pcc_ast_node_t *const node = obj->data.ternary.node[i];
if (node) {
assert(node->parent == obj);
node->parent = NULL;
pcc_ast_node__destroy(node);
}
}
}
break;
case PCC_AST_NODE_TYPE_VARIADIC:
while (obj->data.variadic.len > 0) {
const size_t i = --obj->data.variadic.len;
pcc_ast_node_t *const node = obj->data.variadic.node[i];
if (node) {
assert(node->parent == obj);
node->parent = NULL;
pcc_ast_node__destroy(node);
obj->data.variadic.node[i] = NULL;
}
}
pcc_ast_node_array__finalize(obj->manager, &(obj->data.variadic));
break;
default:
assert(((void)"Node type unknown", 0));
break;
}
pcc_ast_node__unlink_parent_(obj);
if (obj->prev) {
assert(obj->prev->next == obj);
obj->prev->next = obj->next;
}
else {
assert(obj->manager->first == obj);
obj->manager->first = obj->next;
}
if (obj->next) {
assert(obj->next->prev == obj);
obj->next->prev = obj->prev;
}
PCC_AST_FREE(obj->manager, obj);
}
}