-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspecs-gen.c
2281 lines (2023 loc) · 61.4 KB
/
specs-gen.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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stddef.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include "json-actor.h"
#include "cog-utils.h"
#include "debug.h"
/* generated code dependencies */
static const char SPECS_DEPS_H[] = "#include <stdbool.h>\n"
"#include <stdlib.h>\n"
"#include <string.h>\n"
"#include <strings.h>\n"
"#include \"json-actor.h\"\n"
"#include \"json-actor-boxed.h\"\n"
"#include \"cog-utils.h\"\n";
/*
*
* Simple JSON/Query/Body <-> Struct Conversion Spec
*
* <definition> := {
* "disable"?:<bool>,
* "title"?:<string>,
* "comment"?:<string>,
* "namespace"?: [<string>+],
* "namespace_alias"?: [<string>+],
* "defs": [<def-list>]
* }
*
* <def> := "title"?:<string>,
* "comment"?:<string>,
* "namespace"?:[<string>+],
* (<struct> | <enum> | <define>)
*
* <struct> := "struct" : <string>, "typedef" : <string>, "fields": [<field>+]
*
*
* <field> := { "name"?:<string>,
* "json_key"?:<string>,
* (<field-type>| "copy_json_value":true)
* <field-loc>?
* "comment"?:<string>
* "inject_if_not"?:<string>|<bool>|<number>|null
* "todo"?:<bool>
* }
*
*
* <field-type> := "type" : { "base":<string>,
* "int_alias"? : <string>,
* "dec"?:("ntl"|"*"|"[<string>]"),
* "converter"?:<string>,
* "default_value"?:<string>|<bool>|<number>
* }
*
*
* <field-loc> := "loc" : ("json" | "query" | "body" | "url" | "multipart")
*
*
* <enum> := "enum" :<string>, "typedef" : <string>, "items": [ <items>+ ]
* <item> := { "name" : <string>, "value": <integer>? }
*
* <enum> := "enum" :<string>, "typedef" : <string>, "items": [ <items>+ ]
* <item> := { "name" : <string>, "value": <number>?|<string> }
*
*/
typedef char name_t[80];
typedef void (*vvpvp)(void *, void *);
typedef void (*vcpsvp)(char *, size_t, void *);
static char *spec_name = "";
static struct sized_buffer spec_buffer = { 0 };
static void
adjust_lnc(char *json, struct line_and_column *out_lnc)
{
if (!spec_buffer.start) return;
struct line_and_column lnc = { 0 };
addr_to_lnc(spec_buffer.start, spec_buffer.size, json, &lnc);
out_lnc->line += (lnc.line + 1);
out_lnc->column += lnc.column;
}
struct converter {
char *name;
char *input_type;
char *output_type;
char *extractor;
char *injector;
char *extractor_addrof;
char *injector_addrof;
char *free;
char *converted_builtin_type;
bool need_double_quotes;
bool inject_is_user_def;
bool extract_is_user_def;
};
static NTL_T(struct converter) converters = NULL;
/* @todo creating a callback for each converter to modify struct action would
* be much easier to maintain */
static void
init_converters(void)
{
converters = (struct converter **)ntl_calloc(3, sizeof(struct converter));
converters[0]->name = "iso8601";
converters[0]->input_type = "char*";
converters[0]->output_type = "u64_unix_ms_t";
converters[0]->free = NULL;
converters[0]->extractor = "cog_iso8601_to_unix_ms";
converters[0]->injector = "cog_unix_ms_to_iso8601";
converters[0]->extractor_addrof = "&";
converters[0]->injector_addrof = "&";
converters[0]->converted_builtin_type = "uint64_t";
converters[0]->need_double_quotes = true;
converters[0]->inject_is_user_def = true;
converters[0]->extract_is_user_def = true;
converters[1]->name = "snowflake";
converters[1]->input_type = "char*";
converters[1]->output_type = "u64_snowflake_t";
converters[1]->free = NULL;
converters[1]->extractor = "cog_strtou64";
converters[1]->injector = "cog_u64tostr";
converters[1]->extractor_addrof = "&";
converters[1]->injector_addrof = "&";
converters[1]->converted_builtin_type = "uint64_t";
converters[1]->need_double_quotes = true;
converters[1]->inject_is_user_def = true;
converters[1]->extract_is_user_def = true;
converters[2]->name = "mixed";
converters[2]->input_type = "char*";
converters[2]->output_type = "json_char_t*";
converters[2]->free = "free";
converters[2]->extractor = "cog_strndup";
converters[2]->injector = "s";
converters[2]->extractor_addrof = "&";
converters[2]->injector_addrof = "";
converters[2]->converted_builtin_type = ""; /* will fallback to str */
converters[2]->need_double_quotes = false;
converters[2]->inject_is_user_def = false;
converters[2]->extract_is_user_def = true;
}
static struct converter *
get_converter(char *name)
{
int i;
for (i = 0; converters[i]; i++) {
if (0 == strcmp(name, converters[i]->name)) {
return converters[i];
}
}
ERR("converter '%s' is not defined\n", name);
return NULL;
}
enum file_type {
FILE_SINGLE_FILE = 0,
FILE_ENUM_DECLARATION,
FILE_STRUCT_DECLARATION,
FILE_OPAQUE_STRUCT_DECLARATION,
FILE_FUN_DECLARATION,
FILE_DECLARATION,
FILE_DEFINITION,
FILE_HEADER,
FILE_CODE
};
struct emit_option {
enum file_type type;
NTL_T(name_t) namespace_stack[8];
int stack_top;
};
static struct emit_option global_option;
static void
init_emit_option(struct emit_option *opt)
{
memset(&global_option, 0, sizeof(global_option));
global_option.type = opt->type;
}
static char *
ns_to_symbol_name(char *name)
{
char *buf;
size_t len;
FILE *fp = open_memstream(&buf, &len);
int s, i;
for (s = 0; s < global_option.stack_top; s++) {
NTL_T(name_t) ns = global_option.namespace_stack[s];
for (i = 0; ns[i]; i++)
fprintf(fp, "%s_", (char *)ns[i]);
}
fprintf(fp, "%s", name);
fclose(fp);
return buf;
}
static char *
ns_to_item_name(char *name)
{
char *buf;
size_t len;
FILE *fp = open_memstream(&buf, &len);
int si, i;
for (si = 0; si < global_option.stack_top; si++) {
NTL_T(name_t) ns = global_option.namespace_stack[si];
for (i = 0; ns[i]; i++)
fprintf(fp, "%s_", (char *)ns[i]);
}
fprintf(fp, "%s", name);
fclose(fp);
char *s = buf;
while (*s) {
*s = toupper((unsigned char)*s);
s++;
}
return buf;
}
static char *
get_file_suffix(enum file_type t)
{
switch (t) {
case FILE_SINGLE_FILE:
return "cc";
case FILE_DECLARATION:
return "hh";
case FILE_DEFINITION:
return "cc";
case FILE_HEADER:
return "hh";
case FILE_CODE:
return "cc";
default:
ERR("Unknown file suffix (code %d)", t);
}
return "";
}
enum decor_tag {
DEC_NONE = 0, /* this has to be zero as the absence means DEC_NONE */
DEC_POINTER = 1,
DEC_ARRAY = 2,
DEC_NTL
};
struct decor {
enum decor_tag tag;
char *value;
};
enum type_opcode {
TYPE_UNDEFINED = 0,
TYPE_RAW_JSON,
TYPE_NULL,
TYPE_EMPTY_STR,
TYPE_STR,
TYPE_BOOL,
TYPE_INT,
TYPE_DOUBLE
};
struct type_value {
enum type_opcode opcode;
union {
uint64_t ival;
double dval;
char *sval;
} _;
char *token; /* will be used for conversion */
};
struct jc_type {
char *base;
char *int_alias; /* use for enum type names that are represented as int */
struct decor decor;
char *converter;
bool nullable;
struct type_value default_value;
};
static void
print_type(FILE *fp, struct jc_type *p)
{
fprintf(fp, "base:%s, dec:%d", p->base, p->decor.tag);
}
enum loc {
LOC_IN_JSON = 0, /* this has to be zero as the absence means LOC_IN_JSON */
LOC_IN_QUERY,
LOC_IN_BODY,
LOC_IN_URL,
LOC_IN_MULTIPART
};
struct jc_field {
struct jc_type type;
struct type_value inject_condition;
struct line_and_column lnc;
bool todo;
char *name;
char *json_key;
enum loc loc;
char *comment;
char spec[512];
bool option;
bool copy_json_value;
};
static void
print_field(FILE *fp, struct jc_field *p)
{
if (p->todo)
fprintf(fp, "/* @todo name: %s */\n", p->name);
else {
fprintf(fp, "name:%s, ", p->name);
if (p->json_key) fprintf(fp, "json_key:%s, ", p->json_key);
print_type(fp, &p->type);
fprintf(fp, ", loc:%d\n", p->loc);
}
}
#define DEF_HEADER \
NTL_T(name_t) disable_methods; \
struct line_and_column disable_methods_lnc; \
char *title; \
char *comment; \
NTL_T(name_t) namespace; \
NTL_T(NTL_T(name_t)) namespaces; \
char *name; \
char *typedef_name; \
bool enum_is_bitwise_flag; \
struct line_and_column name_lnc;
struct jc_struct {
DEF_HEADER
NTL_T(struct jc_field) fields;
};
static void
print_struct(FILE *fp, struct jc_struct *p)
{
fprintf(fp, "name %s\n", p->name);
ntl_apply(fp, (ntl_t)p->fields, (vvpvp)print_field);
}
struct jc_item {
char *name;
long long value;
char *comment;
bool todo;
bool has_value;
};
static void
print_item(FILE *fp, struct jc_item *p)
{
fprintf(fp, "name %s: ", p->name);
if (p->has_value) fprintf(fp, "%lld", p->value);
}
struct jc_enum {
DEF_HEADER
NTL_T(struct jc_item) items;
};
struct jc_def {
DEF_HEADER
union {
NTL_T(struct jc_field) fields;
NTL_T(struct jc_item) items;
} list;
enum { DEF_TYPE_STRUCT = 0, DEF_TYPE_ENUM, DEF_TYPE_DEFINE } type;
};
static void
print_enum(FILE *fp, struct jc_enum *p)
{
fprintf(fp, "name %s\n", p->name);
ntl_apply(fp, (ntl_t)p->items, (vvpvp)print_item);
}
static void
print_ns(FILE *fp, name_t *n)
{
fprintf(fp, "%s\n", *n);
}
static void
print_def(FILE *fp, struct jc_def *d)
{
switch (d->type) {
case DEF_TYPE_STRUCT:
print_struct(fp, (struct jc_struct *)d);
break;
case DEF_TYPE_ENUM:
case DEF_TYPE_DEFINE:
print_enum(fp, (struct jc_enum *)d);
break;
}
}
static void
emit_field_spec(void *cxt, FILE *fp, struct jc_field *f)
{
(void)cxt;
fprintf(fp, " /* %s:%d:%d\n", spec_name, f->lnc.line, f->lnc.column);
fprintf(fp, " '%s' */\n", f->spec);
}
struct jc_definition {
char *spec_name;
NTL_T(name_t) incl_headers;
bool is_disabled;
char *comment;
NTL_T(name_t) namespace; /* ntl */
NTL_T(struct jc_def) defs; /*ntl */
};
static char *namespace_to_str(NTL_T(name_t) ns)
{
int i;
char *buf;
size_t len;
FILE *fp = open_memstream(&buf, &len);
for (i = 0; ns && ns[i]; i++)
fprintf(fp, "%s.", (char *)ns[i]);
fclose(fp);
return buf;
}
void
print_definition(FILE *fp, struct jc_definition *p)
{
fprintf(fp, "/*\n %s */\n", p->comment);
fprintf(fp, "namespace: ");
ntl_apply(fp, (ntl_t)p->namespace, (vvpvp)print_ns);
fprintf(fp, "\n");
ntl_apply(fp, (ntl_t)p->defs, (vvpvp)print_def);
}
static size_t
loc_from_json(char *json, size_t size, enum loc *p)
{
if (4 == size && 0 == strncmp(json, "json", size)) {
*p = LOC_IN_JSON;
}
else if (5 == size && 0 == strncmp(json, "query", size)) {
*p = LOC_IN_QUERY;
}
else if (4 == size && 0 == strncmp(json, "body", size)) {
*p = LOC_IN_BODY;
}
else if (3 == size && 0 == strncmp(json, "url", size)) {
*p = LOC_IN_URL;
}
else if (9 == size && 0 == strncmp(json, "multipart", size)) {
*p = LOC_IN_MULTIPART;
}
return 1;
}
static size_t
decor_from_json(char *json, size_t size, struct decor *p)
{
if (1 == size && '*' == *json) {
p->tag = DEC_POINTER;
}
else if (3 == size && 0 == strncmp(json, "ntl", size)) {
p->tag = DEC_NTL;
}
else if (4 == size && 0 == strncmp(json, "none", size)) {
p->tag = DEC_NONE;
}
else if (7 == size && 0 == strncmp(json, "pointer", size)) {
p->tag = DEC_POINTER;
}
else if ('[' == *json) {
p->tag = DEC_ARRAY;
p->value = malloc(size + 1);
strncpy(p->value, json, size);
p->value[size] = 0;
}
return 1;
}
static void
field_from_json(char *json, size_t size, void *x)
{
struct jc_field *p = (struct jc_field *)x;
bool has_inject_if_not = false;
struct sized_buffer t_inject_if_not = { 0 };
struct sized_buffer t_default_value = { 0 };
#if 0
bool copy_json_value = false;
#endif
json_extract(json, size,
"(name):?s,"
"(name):lnc,"
"(todo):b,"
"(json_key):?s,"
#if 0
"(type):?s,"
#endif
"(type.base):?s,"
"(type.int_alias):?s,"
"(type.dec):F,"
"(type.converter):?s,"
"(type.nullable):b,"
"(type.default_value):T,"
"(option):b,"
"(inject_if_not):key,"
"(inject_if_not):T,"
"(loc):F,"
"(comment):?s",
&p->name, &p->lnc, &p->todo, &p->json_key,
#if 0
©_json_value,
#endif
&p->type.base, &p->type.int_alias, decor_from_json,
&p->type.decor, &p->type.converter, &p->type.nullable,
&t_default_value, &p->option, &has_inject_if_not,
&t_inject_if_not, loc_from_json, &p->loc, &p->comment);
snprintf(p->spec, sizeof(p->spec), "%.*s", (int)size, json);
adjust_lnc(json, &p->lnc);
if (has_inject_if_not) {
if (t_inject_if_not.size == 0) {
p->inject_condition.opcode = TYPE_EMPTY_STR;
}
else if (4 == t_inject_if_not.size
&& 0
== strncmp("null", t_inject_if_not.start,
t_inject_if_not.size))
{
p->inject_condition.opcode = TYPE_NULL;
}
else { /* we will convert this to actual type later */
p->inject_condition.opcode = TYPE_RAW_JSON;
cog_strndup(t_inject_if_not.start, t_inject_if_not.size,
&p->inject_condition.token);
}
}
if (t_default_value.size != 0) {
p->type.default_value.opcode = TYPE_RAW_JSON;
cog_strndup(t_default_value.start, t_default_value.size,
&p->type.default_value.token);
}
}
static void
name_from_json(char *json, size_t size, char *p)
{
ASSERT_S(size < sizeof(name_t), "namespace is too long");
memcpy(p, json, size);
p[size] = 0;
}
static void
namespace_from_json(char *json, size_t size, NTL_T(name_t) * ns_p)
{
struct ntl_deserializer d0 = { .elem_size = sizeof(name_t),
.elem_from_buf = (vcpsvp)name_from_json,
.init_elem = NULL,
.ntl_recipient_p = (ntl_t *)ns_p };
extract_ntl_from_json(json, size, &d0);
}
static size_t
struct_from_json(char *json, size_t size, struct jc_struct *s)
{
struct ntl_deserializer dx = { .elem_size = sizeof(name_t),
.elem_from_buf = (vcpsvp)name_from_json,
.init_elem = NULL,
.ntl_recipient_p =
(ntl_t *)&(s->disable_methods) };
struct ntl_deserializer d1 = { .elem_size = sizeof(struct jc_field),
.elem_from_buf = (vcpsvp)field_from_json,
.init_elem = NULL,
.ntl_recipient_p = (ntl_t *)&(s->fields) };
size_t ret =
json_extract(json, size,
"(disable_methods):F,"
"(disable_methods):lnc,"
"(fields):F",
extract_ntl_from_json, &dx, &s->disable_methods_lnc,
extract_ntl_from_json, &d1);
adjust_lnc(json, &s->disable_methods_lnc);
return ret;
}
static void
item_from_json(char *json, size_t size, void *x)
{
struct jc_item *p = (struct jc_item *)x;
void *defined[4] = { 0 };
json_extract(json, size,
"(name):?s,"
"(todo):b,"
"(value):lld,"
"(comment):?s"
"@record_defined",
&p->name, &p->todo, &p->value, &p->comment, defined,
sizeof(defined));
int i;
for (i = 0; i < 4; i++) {
if (defined[i] == &p->value) p->has_value = true;
}
}
static size_t
enum_from_json(char *json, size_t size, struct jc_enum *e)
{
struct ntl_deserializer d1 = { .elem_size = sizeof(struct jc_item),
.elem_from_buf = (vcpsvp)item_from_json,
.init_elem = NULL,
.ntl_recipient_p = (ntl_t *)&(e->items) };
size_t ret =
json_extract(json, size, "(items):F", extract_ntl_from_json, &d1);
return ret;
}
static void
def_from_json(char *json, size_t size, struct jc_def *def)
{
bool is_struct = false, is_enum = false, is_define = false;
struct ntl_deserializer d0 = { .elem_size = sizeof(name_t),
.elem_from_buf = (vcpsvp)name_from_json,
.init_elem = NULL,
.ntl_recipient_p =
(ntl_t *)&(def->namespace) };
struct ntl_deserializer d0_alias = { .elem_size = sizeof(void *),
.elem_from_buf =
(vcpsvp)namespace_from_json,
.init_elem = NULL,
.ntl_recipient_p =
(ntl_t *)&(def->namespaces) };
json_extract(json, size,
"(comment):?s,"
"(title):?s,"
"(namespace):F,"
"(namespaces):F,"
"(typedef):?s,"
"(struct):key,(enum):key,(define):key"
"(struct):?s, (enum):?s,(define):?s"
"(struct):lnc,"
"(bitwise):b",
&def->comment, &def->title, extract_ntl_from_json, &d0,
extract_ntl_from_json, &d0_alias, &def->typedef_name,
&is_struct, &is_enum, &is_define, &def->name, &def->name,
&def->name, &def->name_lnc, &def->enum_is_bitwise_flag);
adjust_lnc(json, &def->name_lnc);
if (is_struct) {
def->type = DEF_TYPE_STRUCT;
struct_from_json(json, size, (struct jc_struct *)def);
}
else if (is_enum) {
def->type = DEF_TYPE_ENUM;
enum_from_json(json, size, (struct jc_enum *)def);
}
else if (is_define) {
def->type = DEF_TYPE_DEFINE;
enum_from_json(json, size, (struct jc_enum *)def);
}
else {
ERR("missing 'struct', 'enum' or 'define' in '%.*s'", (int)size, json);
}
}
static void
gen_open_namespace(FILE *fp, NTL_T(name_t) p)
{
(void)fp;
if (NULL == p) return;
global_option.namespace_stack[global_option.stack_top] = p;
++global_option.stack_top;
}
static void
gen_close_namespace(FILE *fp, NTL_T(name_t) p)
{
(void)fp;
if (NULL == p) return;
global_option.stack_top--;
global_option.namespace_stack[global_option.stack_top] = NULL;
}
static void
gen_enum(FILE *fp, struct jc_enum *e)
{
char *t = ns_to_symbol_name(e->name);
long long prev_value = -1;
char *t_alias = NULL;
if (e->typedef_name) t_alias = ns_to_symbol_name(e->typedef_name);
if (e->title) fprintf(fp, "/* %s */\n", e->title);
fprintf(fp, "/* defined at %s:%d:%d */\n", spec_name, e->name_lnc.line,
e->name_lnc.column);
fputs("/**\n", fp);
{
if (e->comment) fprintf(fp, " * @see %s\n *\n", e->comment);
fprintf(fp,
" * @verbatim embed:rst:leading-asterisk\n"
" * .. container:: toggle\n\n"
" * .. container:: header\n\n"
" * **Methods**\n\n"
" * * :code:`char* %s_print(enum %s code)`\n"
" * * :code:`enum %s %s_eval(char *code_as_str)`\n"
" * @endverbatim\n",
t, t, t, t);
}
fputs(" */\n", fp);
if (t_alias) fprintf(fp, "typedef ");
fprintf(fp, "enum %s {\n", t);
int i = 0;
for (; e->items && e->items[i]; i++) {
struct jc_item *item = e->items[i];
char *item_name = ns_to_item_name(item->name);
if (item->todo) {
fprintf(fp, "/* @todo %s %s */\n", item_name, item->comment);
}
else {
fprintf(fp, " %s", item_name);
if (item->has_value) {
fprintf(fp, " = %lld", item->value);
prev_value = item->value;
}
else {
fprintf(fp, " = %lld", prev_value + 1);
prev_value++;
}
if (item->comment)
fprintf(fp, ", /**< %s */\n", item->comment);
else
fprintf(fp, ",\n");
}
}
if (t_alias)
fprintf(fp, "} %s\n", t_alias);
else
fprintf(fp, "};\n");
}
static void
gen_enum_eval(FILE *fp, struct jc_enum *e)
{
char *t = ns_to_symbol_name(e->name);
char *t_alias = NULL;
int i;
if (e->typedef_name) t_alias = ns_to_symbol_name(e->typedef_name);
if (t_alias)
fprintf(fp, "%s %s_eval(char *s){\n", t_alias, t_alias);
else
fprintf(fp, "enum %s %s_eval(char *s){\n", t, t);
for (i = 0; e->items && e->items[i]; i++) {
struct jc_item *item = e->items[i];
char *item_name = ns_to_item_name(item->name);
if (item->todo)
fprintf(fp, "/* %s */\n", item->name);
else
fprintf(fp, " if(strcasecmp(\"%s\", s) == 0) return %s;\n", item->name,
item_name);
}
fprintf(fp, " ERR(\"'%%s' doesn't match any known enumerator.\", s);\n");
fprintf(fp, " return -1;\n");
fprintf(fp, "}\n");
}
static void
gen_enum_print(FILE *fp, struct jc_enum *e)
{
char *t = ns_to_symbol_name(e->name);
char *t_alias = NULL;
int i;
if (e->typedef_name) t_alias = ns_to_symbol_name(e->typedef_name);
if (t_alias)
fprintf(fp, "char* %s_print(%s v){\n", t_alias, t_alias);
else
fprintf(fp, "char* %s_print(enum %s v){\n", t, t);
fprintf(fp, "\n switch (v) {\n");
for (i = 0; e->items && e->items[i]; i++) {
struct jc_item *item = e->items[i];
if (item->todo)
fprintf(fp, "/* %s */\n", item->name);
else
fprintf(fp, " case %s: return \"%s\";\n", ns_to_item_name(item->name),
item->name);
}
fprintf(fp, " }\n");
fprintf(fp, "\n return NULL;\n");
fprintf(fp, "}\n");
}
static void gen_forward_fun_declare(FILE *fp, struct jc_def *d);
static void gen_default(FILE *fp, struct jc_def *d);
static void gen_wrapper(FILE *fp, struct jc_def *d);
static void
gen_enum_all(FILE *fp, struct jc_def *d, name_t **ns)
{
struct jc_enum *e = (struct jc_enum *)d;
fprintf(fp, "\n\n");
gen_open_namespace(fp, ns);
/* */
switch (global_option.type) {
case FILE_DECLARATION:
case FILE_ENUM_DECLARATION:
case FILE_HEADER:
gen_enum(fp, e);
gen_forward_fun_declare(fp, d);
break;
case FILE_CODE:
gen_wrapper(fp, d);
gen_enum_eval(fp, e);
fprintf(fp, "\n");
gen_enum_print(fp, e);
fprintf(fp, "\n");
gen_default(fp, d);
fprintf(fp, "\n");
break;
default:
break;
}
/* */
gen_close_namespace(fp, ns);
}
static void
gen_define(FILE *fp, struct jc_enum *e)
{
int i;
if (e->title) fprintf(fp, "/* %s */\n", e->title);
fprintf(fp, "/* defined at %s:%d:%d */\n", spec_name, e->name_lnc.line,
e->name_lnc.column);
if (e->items)
for (i = 0; e->items[i]; i++) {
struct jc_item *item = e->items[i];
char *item_name = ns_to_item_name(item->name);
if (item->todo) {
fprintf(fp, "/* @todo %s %s */\n", item_name, item->comment);
}
else {
if (item->comment) fprintf(fp, "/** %s */\n", item->comment);
fprintf(fp, "#define %s", item_name);
if (item->has_value) fprintf(fp, " %lld", item->value);
fprintf(fp, "\n");
}
}
}
static void
gen_define_all(FILE *fp, struct jc_def *d, name_t **ns)
{
struct jc_enum *e = (struct jc_enum *)d;
fprintf(fp, "\n\n");
gen_open_namespace(fp, ns);
/* */
switch (global_option.type) {
case FILE_DECLARATION:
case FILE_ENUM_DECLARATION:
case FILE_HEADER:
gen_define(fp, e);
break;
default:
break;
}
/* */
gen_close_namespace(fp, ns);
}
static void
definition_from_json(char *json, size_t size, struct jc_definition *s)
{
struct ntl_deserializer d1 = { .elem_size = sizeof(name_t),
.elem_from_buf = (vcpsvp)name_from_json,
.init_elem = NULL,
.ntl_recipient_p = (ntl_t *)&(s->namespace) };
struct ntl_deserializer d2 = { .elem_size = sizeof(struct jc_def),
.elem_from_buf = (vcpsvp)def_from_json,
.init_elem = NULL,
.ntl_recipient_p = (ntl_t *)&(s->defs) };
json_extract(json, size,
"(disabled):b"
"(comment):?s"
"(namespace):F"
"(defs):F",
&s->is_disabled, &s->comment, extract_ntl_from_json, &d1,
extract_ntl_from_json, &d2);
}
static void
definition_list_from_json(char *json,
size_t size,
NTL_T(struct jc_definition) * s)
{
struct ntl_deserializer d = { .elem_size = sizeof(struct jc_definition),
.elem_from_buf = (vcpsvp)definition_from_json,
.init_elem = NULL,
.ntl_recipient_p = (ntl_t *)s };
extract_ntl_from_json(json, size, &d);
}
void
spec_from_json(char *json, size_t size, NTL_T(struct jc_definition) * s)
{
char *const xend_pos = json + size;
while (isspace(*json)) {
json++;
}
if ('[' == *json)
definition_list_from_json(json, xend_pos - json, s);
else {
*s =
(NTL_T(struct jc_definition))ntl_calloc(1, sizeof(struct jc_definition));
definition_from_json(json, xend_pos - json, (*s)[0]);
}
}
struct action {
bool todo;
char *c_name;
char *json_key;
char *c_type;
char *fun_prefix;
char *pre_dec;
char *post_dec;
char *extract_arg_decor;
char *inject_arg_decor;
char *extractor;
char *injector;
char *alloc;
char *free;
bool inject_is_user_def;