-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
raylib_parser.c
1989 lines (1737 loc) · 80.6 KB
/
raylib_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
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
/**********************************************************************************************
raylib API parser
This parser scans raylib.h to get API information about defines, structs, aliases, enums, callbacks and functions.
All data is divided into pieces, usually as strings. The following types are used for data:
- struct DefineInfo
- struct StructInfo
- struct AliasInfo
- struct EnumInfo
- struct FunctionInfo
CONSTRAINTS:
This parser is specifically designed to work with raylib.h, so, it has some constraints:
- Functions are expected as a single line with the following structure:
<retType> <name>(<paramType[0]> <paramName[0]>, <paramType[1]> <paramName[1]>); <desc>
Be careful with functions broken into several lines, it breaks the process!
- Structures are expected as several lines with the following form:
<desc>
typedef struct <name> {
<fieldType[0]> <fieldName[0]>; <fieldDesc[0]>
<fieldType[1]> <fieldName[1]>; <fieldDesc[1]>
<fieldType[2]> <fieldName[2]>; <fieldDesc[2]>
} <name>;
- Enums are expected as several lines with the following form:
<desc>
typedef enum {
<valueName[0]> = <valueInteger[0]>, <valueDesc[0]>
<valueName[1]>,
<valueName[2]>, <valueDesc[2]>
<valueName[3]> <valueDesc[3]>
} <name>;
NOTE: Multiple options are supported for enums:
- If value is not provided, (<valueInteger[i -1]> + 1) is assigned
- Value description can be provided or not
OTHER NOTES:
- This parser could work with other C header files if mentioned constraints are followed.
- This parser does not require <string.h> library, all data is parsed directly from char buffers.
LICENSE: zlib/libpng
raylib-parser is licensed under an unmodified zlib/libpng license, which is an OSI-certified,
BSD-like license that allows static linking with closed source software:
Copyright (c) 2021-2023 Ramon Santamaria (@raysan5)
**********************************************************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h> // Required for: malloc(), calloc(), realloc(), free(), atoi(), strtol()
#include <stdio.h> // Required for: printf(), fopen(), fseek(), ftell(), fread(), fclose()
#include <stdbool.h> // Required for: bool
#include <ctype.h> // Required for: isdigit()
#define MAX_DEFINES_TO_PARSE 2048 // Maximum number of defines to parse
#define MAX_STRUCTS_TO_PARSE 64 // Maximum number of structures to parse
#define MAX_ALIASES_TO_PARSE 64 // Maximum number of aliases to parse
#define MAX_ENUMS_TO_PARSE 64 // Maximum number of enums to parse
#define MAX_CALLBACKS_TO_PARSE 64 // Maximum number of callbacks to parse
#define MAX_FUNCS_TO_PARSE 1024 // Maximum number of functions to parse
#define MAX_LINE_LENGTH 512 // Maximum length of one line (including comments)
#define MAX_STRUCT_FIELDS 64 // Maximum number of struct fields
#define MAX_ENUM_VALUES 512 // Maximum number of enum values
#define MAX_FUNCTION_PARAMETERS 12 // Maximum number of function parameters
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
// Type of parsed define
typedef enum {
UNKNOWN = 0,
MACRO,
GUARD,
INT,
INT_MATH,
LONG,
LONG_MATH,
FLOAT,
FLOAT_MATH,
DOUBLE,
DOUBLE_MATH,
CHAR,
STRING,
COLOR
} DefineType;
// Define info data
typedef struct DefineInfo {
char name[64]; // Define name
int type; // Define type
char value[256]; // Define value
char desc[128]; // Define description
bool isHex; // Define is hex number (for types INT, LONG)
} DefineInfo;
// Struct info data
typedef struct StructInfo {
char name[64]; // Struct name
char desc[128]; // Struct type description
int fieldCount; // Number of fields in the struct
char fieldType[MAX_STRUCT_FIELDS][64]; // Field type
char fieldName[MAX_STRUCT_FIELDS][64]; // Field name
char fieldDesc[MAX_STRUCT_FIELDS][128]; // Field description
} StructInfo;
// Alias info data
typedef struct AliasInfo {
char type[64]; // Alias type
char name[64]; // Alias name
char desc[128]; // Alias description
} AliasInfo;
// Enum info data
typedef struct EnumInfo {
char name[64]; // Enum name
char desc[128]; // Enum description
int valueCount; // Number of values in enumerator
char valueName[MAX_ENUM_VALUES][64]; // Value name definition
int valueInteger[MAX_ENUM_VALUES]; // Value integer
char valueDesc[MAX_ENUM_VALUES][128]; // Value description
} EnumInfo;
// Function info data
typedef struct FunctionInfo {
char name[64]; // Function name
char desc[128]; // Function description (comment at the end)
char retType[32]; // Return value type
int paramCount; // Number of function parameters
char paramType[MAX_FUNCTION_PARAMETERS][32]; // Parameters type
char paramName[MAX_FUNCTION_PARAMETERS][32]; // Parameters name
char paramDesc[MAX_FUNCTION_PARAMETERS][128]; // Parameters description
} FunctionInfo;
// Output format for parsed data
typedef enum { DEFAULT = 0, JSON, XML, LUA, CODE } OutputFormat;
//----------------------------------------------------------------------------------
// Global Variables Definition
//----------------------------------------------------------------------------------
static int defineCount = 0;
static int structCount = 0;
static int aliasCount = 0;
static int enumCount = 0;
static int callbackCount = 0;
static int funcCount = 0;
static DefineInfo *defines = NULL;
static StructInfo *structs = NULL;
static AliasInfo *aliases = NULL;
static EnumInfo *enums = NULL;
static FunctionInfo *callbacks = NULL;
static FunctionInfo *funcs = NULL;
// Command line variables
static char apiDefine[32] = { 0 }; // Functions define (i.e. RLAPI for raylib.h, RMDEF for raymath.h, etc.)
static char truncAfter[32] = { 0 }; // Truncate marker (i.e. "RLGL IMPLEMENTATION" for rlgl.h)
static int outputFormat = DEFAULT;
// NOTE: Max length depends on OS, in Windows MAX_PATH = 256
static char inFileName[512] = { 0 }; // Input file name (required in case of drag & drop over executable)
static char outFileName[512] = { 0 }; // Output file name (required for file save/export)
//----------------------------------------------------------------------------------
// Module Functions Declaration
//----------------------------------------------------------------------------------
static void ShowCommandLineInfo(void); // Show command line usage info
static void ProcessCommandLine(int argc, char *argv[]); // Process command line input
static char *LoadFileText(const char *fileName, int *length);
static char **GetTextLines(const char *buffer, int length, int *linesCount);
static void GetDataTypeAndName(const char *typeName, int typeNameLen, char *type, char *name);
static void GetDescription(const char *source, char *description);
static void MoveArraySize(char *name, char *type); // Move array size from name to type
static unsigned int TextLength(const char *text); // Get text length in bytes, check for \0 character
static bool IsTextEqual(const char *text1, const char *text2, unsigned int count);
static int TextFindIndex(const char *text, const char *find); // Find first text occurrence within a string
static void MemoryCopy(void *dest, const void *src, unsigned int count);
static char *EscapeBackslashes(char *text); // Replace '\' by "\\" when exporting to JSON and XML
static const char *StrDefineType(DefineType type); // Get string of define type
static void ExportParsedData(const char *fileName, int format); // Export parsed data in desired format
//----------------------------------------------------------------------------------
// Program main entry point
//----------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
if (argc > 1) ProcessCommandLine(argc, argv);
if (inFileName[0] == '\0') MemoryCopy(inFileName, "../src/raylib.h\0", 16);
if (outFileName[0] == '\0') MemoryCopy(outFileName, "raylib_api.txt\0", 15);
if (apiDefine[0] == '\0') MemoryCopy(apiDefine, "RLAPI\0", 6);
int length = 0;
char *buffer = LoadFileText(inFileName, &length);
if (buffer == NULL)
{
printf("Could not read input file: %s\n", inFileName);
return 1;
}
// Preprocess buffer to get separate lines
// NOTE: GetTextLines() also removes leading spaces/tabs
int linesCount = 0;
char **lines = GetTextLines(buffer, length, &linesCount);
// Truncate lines
if (truncAfter[0] != '\0')
{
int newCount = -1;
for (int i = 0; i < linesCount; i++)
{
if (newCount > -1) free(lines[i]);
else if (TextFindIndex(lines[i], truncAfter) > -1) newCount = i;
}
if (newCount > -1) linesCount = newCount;
printf("Number of truncated text lines: %i\n", linesCount);
}
// Defines line indices
int *defineLines = (int *)malloc(MAX_DEFINES_TO_PARSE*sizeof(int));
// Structs line indices
int *structLines = (int *)malloc(MAX_STRUCTS_TO_PARSE*sizeof(int));
// Aliases line indices
int *aliasLines = (int *)malloc(MAX_ALIASES_TO_PARSE*sizeof(int));
// Enums line indices
int *enumLines = (int *)malloc(MAX_ENUMS_TO_PARSE*sizeof(int));
// Callbacks line indices
int *callbackLines = (int *)malloc(MAX_CALLBACKS_TO_PARSE*sizeof(int));
// Function line indices
int *funcLines = (int *)malloc(MAX_FUNCS_TO_PARSE*sizeof(int));
// Prepare required lines for parsing
//----------------------------------------------------------------------------------
// Read define lines
for (int i = 0; i < linesCount; i++)
{
int j = 0;
while ((lines[i][j] == ' ') || (lines[i][j] == '\t')) j++; // skip spaces and tabs in the begining
// Read define line
if (IsTextEqual(lines[i]+j, "#define ", 8))
{
// Keep the line position in the array of lines,
// so, we can scan that position and following lines
defineLines[defineCount] = i;
defineCount++;
}
}
// Read struct lines
for (int i = 0; i < linesCount; i++)
{
// Find structs
// starting with "typedef struct ... {" or "typedef struct ... ; \n struct ... {"
// ending with "} ... ;"
// i.e. excluding "typedef struct rAudioBuffer rAudioBuffer;" -> Typedef and forward declaration only
if (IsTextEqual(lines[i], "typedef struct", 14))
{
bool validStruct = IsTextEqual(lines[i + 1], "struct", 6);
if (!validStruct)
{
for (int c = 0; c < MAX_LINE_LENGTH; c++)
{
char v = lines[i][c];
if (v == '{') validStruct = true;
if ((v == '{') || (v == ';') || (v == '\0')) break;
}
}
if (!validStruct) continue;
structLines[structCount] = i;
while (lines[i][0] != '}') i++;
while (lines[i][0] != '\0') i++;
structCount++;
}
}
// Read alias lines
for (int i = 0; i < linesCount; i++)
{
// Find aliases (lines with "typedef ... ...;")
if (IsTextEqual(lines[i], "typedef", 7))
{
int spaceCount = 0;
bool validAlias = false;
for (int c = 0; c < MAX_LINE_LENGTH; c++)
{
char v = lines[i][c];
if (v == ' ') spaceCount++;
if ((v == ';') && (spaceCount == 2)) validAlias = true;
if ((v == ';') || (v == '(') || (v == '\0')) break;
}
if (!validAlias) continue;
aliasLines[aliasCount] = i;
aliasCount++;
}
}
// Read enum lines
for (int i = 0; i < linesCount; i++)
{
// Read enum line
if (IsTextEqual(lines[i], "typedef enum {", 14) && (lines[i][TextLength(lines[i])-1] != ';')) // ignore inline enums
{
// Keep the line position in the array of lines,
// so, we can scan that position and following lines
enumLines[enumCount] = i;
enumCount++;
}
}
// Read callback lines
for (int i = 0; i < linesCount; i++)
{
// Find callbacks (lines with "typedef ... (* ... )( ... );")
if (IsTextEqual(lines[i], "typedef", 7))
{
bool hasBeginning = false;
bool hasMiddle = false;
bool hasEnd = false;
for (int c = 0; c < MAX_LINE_LENGTH; c++)
{
if ((lines[i][c] == '(') && (lines[i][c + 1] == '*')) hasBeginning = true;
if ((lines[i][c] == ')') && (lines[i][c + 1] == '(')) hasMiddle = true;
if ((lines[i][c] == ')') && (lines[i][c + 1] == ';')) hasEnd = true;
if (hasEnd) break;
}
if (hasBeginning && hasMiddle && hasEnd)
{
callbackLines[callbackCount] = i;
callbackCount++;
}
}
}
// Read function lines
for (int i = 0; i < linesCount; i++)
{
// Read function line (starting with `define`, i.e. for raylib.h "RLAPI")
if (IsTextEqual(lines[i], apiDefine, TextLength(apiDefine)))
{
funcLines[funcCount] = i;
funcCount++;
}
}
// At this point we have all raylib defines, structs, aliases, enums, callbacks, functions lines data to start parsing
free(buffer); // Unload text buffer
// Parsing raylib data
//----------------------------------------------------------------------------------
// Define info data
defines = (DefineInfo *)calloc(MAX_DEFINES_TO_PARSE, sizeof(DefineInfo));
int defineIndex = 0;
for (int i = 0; i < defineCount; i++)
{
char *linePtr = lines[defineLines[i]];
int j = 0;
while ((linePtr[j] == ' ') || (linePtr[j] == '\t')) j++; // Skip spaces and tabs in the begining
j += 8; // Skip "#define "
while ((linePtr[j] == ' ') || (linePtr[j] == '\t')) j++; // Skip spaces and tabs after "#define "
// Extract name
int defineNameStart = j;
int openBraces = 0;
while (linePtr[j] != '\0')
{
if (((linePtr[j] == ' ') || (linePtr[j] == '\t')) && (openBraces == 0)) break;
if (linePtr[j] == '(') openBraces++;
if (linePtr[j] == ')') openBraces--;
j++;
}
int defineNameEnd = j-1;
// Skip duplicates
unsigned int nameLen = defineNameEnd - defineNameStart + 1;
bool isDuplicate = false;
for (int k = 0; k < defineIndex; k++)
{
if ((nameLen == TextLength(defines[k].name)) && IsTextEqual(defines[k].name, &linePtr[defineNameStart], nameLen))
{
isDuplicate = true;
break;
}
}
if (isDuplicate) continue;
MemoryCopy(defines[defineIndex].name, &linePtr[defineNameStart], nameLen);
// Determine type
if (linePtr[defineNameEnd] == ')') defines[defineIndex].type = MACRO;
while ((linePtr[j] == ' ') || (linePtr[j] == '\t')) j++; // Skip spaces and tabs after name
int defineValueStart = j;
if ((linePtr[j] == '\0') || (linePtr[j] == '/')) defines[defineIndex].type = GUARD;
if (linePtr[j] == '"') defines[defineIndex].type = STRING;
else if (linePtr[j] == '\'') defines[defineIndex].type = CHAR;
else if (IsTextEqual(linePtr+j, "CLITERAL(Color)", 15)) defines[defineIndex].type = COLOR;
else if (isdigit(linePtr[j])) // Parsing numbers
{
bool isFloat = false, isNumber = true, isHex = false;
while ((linePtr[j] != ' ') && (linePtr[j] != '\t') && (linePtr[j] != '\0'))
{
char ch = linePtr[j];
if (ch == '.') isFloat = true;
if (ch == 'x') isHex = true;
if (!(isdigit(ch) ||
((ch >= 'a') && (ch <= 'f')) ||
((ch >= 'A') && (ch <= 'F')) ||
(ch == 'x') ||
(ch == 'L') ||
(ch == '.') ||
(ch == '+') ||
(ch == '-'))) isNumber = false;
j++;
}
if (isNumber)
{
if (isFloat)
{
defines[defineIndex].type = (linePtr[j-1] == 'f')? FLOAT : DOUBLE;
}
else
{
defines[defineIndex].type = (linePtr[j-1] == 'L')? LONG : INT;
defines[defineIndex].isHex = isHex;
}
}
}
// Extracting value
while ((linePtr[j] != '\\') && (linePtr[j] != '\0') && !((linePtr[j] == '/') && (linePtr[j+1] == '/'))) j++;
int defineValueEnd = j-1;
while ((linePtr[defineValueEnd] == ' ') || (linePtr[defineValueEnd] == '\t')) defineValueEnd--; // Remove trailing spaces and tabs
if ((defines[defineIndex].type == LONG) || (defines[defineIndex].type == FLOAT)) defineValueEnd--; // Remove number postfix
int valueLen = defineValueEnd - defineValueStart + 1;
if (valueLen > 255) valueLen = 255;
if (valueLen > 0) MemoryCopy(defines[defineIndex].value, &linePtr[defineValueStart], valueLen);
// Extracting description
if ((linePtr[j] == '/') && linePtr[j + 1] == '/')
{
j += 2;
while (linePtr[j] == ' ') j++;
int commentStart = j;
while ((linePtr[j] != '\\') && (linePtr[j] != '\0')) j++;
int commentEnd = j-1;
int commentLen = commentEnd - commentStart + 1;
if (commentLen > 127) commentLen = 127;
MemoryCopy(defines[defineIndex].desc, &linePtr[commentStart], commentLen);
}
// Parse defines of type UNKNOWN to find calculated numbers
if (defines[defineIndex].type == UNKNOWN)
{
int largestType = UNKNOWN;
bool isMath = true;
char *valuePtr = defines[defineIndex].value;
for (unsigned int c = 0; c < TextLength(valuePtr); c++)
{
char ch = valuePtr[c];
// Skip operators and whitespace
if ((ch == '(') ||
(ch == ')') ||
(ch == '+') ||
(ch == '-') ||
(ch == '*') ||
(ch == '/') ||
(ch == ' ') ||
(ch == '\t')) continue;
// Read number operand
else if (isdigit(ch))
{
bool isNumber = true, isFloat = false;
while (!((ch == '(') ||
(ch == ')') ||
(ch == '*') ||
(ch == '/') ||
(ch == ' ') ||
(ch == '\t') ||
(ch == '\0')))
{
if (ch == '.') isFloat = true;
if (!(isdigit(ch) ||
((ch >= 'a') && (ch <= 'f')) ||
((ch >= 'A') && (ch <= 'F')) ||
(ch == 'x') ||
(ch == 'L') ||
(ch == '.') ||
(ch == '+') ||
(ch == '-')))
{
isNumber = false;
break;
}
c++;
ch = valuePtr[c];
}
if (isNumber)
{
// Found a valid number -> update largestType
int numberType;
if (isFloat) numberType = (valuePtr[c - 1] == 'f')? FLOAT_MATH : DOUBLE_MATH;
else numberType = (valuePtr[c - 1] == 'L')? LONG_MATH : INT_MATH;
if (numberType > largestType) largestType = numberType;
}
else
{
isMath = false;
break;
}
}
else // Read string operand
{
int operandStart = c;
while (!((ch == '\0') ||
(ch == ' ') ||
(ch == '(') ||
(ch == ')') ||
(ch == '+') ||
(ch == '-') ||
(ch == '*') ||
(ch == '/')))
{
c++;
ch = valuePtr[c];
}
int operandEnd = c;
int operandLength = operandEnd - operandStart;
// Search previous defines for operand
bool foundOperand = false;
for (int previousDefineIndex = 0; previousDefineIndex < defineIndex; previousDefineIndex++)
{
if (IsTextEqual(defines[previousDefineIndex].name, &valuePtr[operandStart], operandLength))
{
if ((defines[previousDefineIndex].type >= INT) && (defines[previousDefineIndex].type <= DOUBLE_MATH))
{
// Found operand and it's a number -> update largestType
if (defines[previousDefineIndex].type > largestType) largestType = defines[previousDefineIndex].type;
foundOperand = true;
}
break;
}
}
if (!foundOperand)
{
isMath = false;
break;
}
}
}
if (isMath)
{
// Define is a calculated number -> update type
if (largestType == INT) largestType = INT_MATH;
else if (largestType == LONG) largestType = LONG_MATH;
else if (largestType == FLOAT) largestType = FLOAT_MATH;
else if (largestType == DOUBLE) largestType = DOUBLE_MATH;
defines[defineIndex].type = largestType;
}
}
defineIndex++;
}
defineCount = defineIndex;
free(defineLines);
// Structs info data
structs = (StructInfo *)calloc(MAX_STRUCTS_TO_PARSE, sizeof(StructInfo));
for (int i = 0; i < structCount; i++)
{
char **linesPtr = &lines[structLines[i]];
// Parse struct description
GetDescription(linesPtr[-1], structs[i].desc);
// Get struct name: typedef struct name {
const int TDS_LEN = 15; // length of "typedef struct "
for (int c = TDS_LEN; c < 64 + TDS_LEN; c++)
{
if ((linesPtr[0][c] == '{') || (linesPtr[0][c] == ' '))
{
int nameLen = c - TDS_LEN;
while (linesPtr[0][TDS_LEN + nameLen - 1] == ' ') nameLen--;
MemoryCopy(structs[i].name, &linesPtr[0][TDS_LEN], nameLen);
break;
}
}
// Get struct fields and count them -> fields finish with ;
int l = 1;
while (linesPtr[l][0] != '}')
{
// WARNING: Some structs have empty spaces and comments -> OK, processed
if ((linesPtr[l][0] != ' ') && (linesPtr[l][0] != '\0'))
{
// Scan one field line
char *fieldLine = linesPtr[l];
int fieldEndPos = 0;
while (fieldLine[fieldEndPos] != ';') fieldEndPos++;
if ((fieldLine[0] != '/') && !IsTextEqual(fieldLine, "struct", 6)) // Field line is not a comment and not a struct declaration
{
//printf("Struct field: %s_\n", fieldLine); // OK!
// Get struct field type and name
GetDataTypeAndName(fieldLine, fieldEndPos, structs[i].fieldType[structs[i].fieldCount], structs[i].fieldName[structs[i].fieldCount]);
// Get the field description
GetDescription(&fieldLine[fieldEndPos], structs[i].fieldDesc[structs[i].fieldCount]);
structs[i].fieldCount++;
// Split field names containing multiple fields (like Matrix)
int additionalFields = 0;
int originalIndex = structs[i].fieldCount - 1;
for (unsigned int c = 0; c < TextLength(structs[i].fieldName[originalIndex]); c++)
{
if (structs[i].fieldName[originalIndex][c] == ',') additionalFields++;
}
if (additionalFields > 0)
{
int originalLength = -1;
int lastStart;
for (unsigned int c = 0; c < TextLength(structs[i].fieldName[originalIndex]) + 1; c++)
{
char v = structs[i].fieldName[originalIndex][c];
bool isEndOfString = (v == '\0');
if ((v == ',') || isEndOfString)
{
if (originalLength == -1)
{
// Save length of original field name
// Don't truncate yet, still needed for copying
originalLength = c;
}
else
{
// Copy field data from original field
int nameLength = c - lastStart;
MemoryCopy(structs[i].fieldName[structs[i].fieldCount], &structs[i].fieldName[originalIndex][lastStart], nameLength);
MemoryCopy(structs[i].fieldType[structs[i].fieldCount], &structs[i].fieldType[originalIndex][0], TextLength(structs[i].fieldType[originalIndex]));
MemoryCopy(structs[i].fieldDesc[structs[i].fieldCount], &structs[i].fieldDesc[originalIndex][0], TextLength(structs[i].fieldDesc[originalIndex]));
structs[i].fieldCount++;
}
if (!isEndOfString)
{
// Skip comma and spaces
c++;
while (structs[i].fieldName[originalIndex][c] == ' ') c++;
// Save position for next field
lastStart = c;
}
}
}
// Set length of original field to truncate the first field name
structs[i].fieldName[originalIndex][originalLength] = '\0';
}
// Split field types containing multiple fields (like MemNode)
additionalFields = 0;
originalIndex = structs[i].fieldCount - 1;
for (unsigned int c = 0; c < TextLength(structs[i].fieldType[originalIndex]); c++)
{
if (structs[i].fieldType[originalIndex][c] == ',') additionalFields++;
}
if (additionalFields > 0)
{
// Copy original name to last additional field
structs[i].fieldCount += additionalFields;
MemoryCopy(structs[i].fieldName[originalIndex + additionalFields], &structs[i].fieldName[originalIndex][0], TextLength(structs[i].fieldName[originalIndex]));
// Copy names from type to additional fields
int fieldsRemaining = additionalFields;
int nameStart = -1;
int nameEnd = -1;
for (int k = TextLength(structs[i].fieldType[originalIndex]); k > 0; k--)
{
char v = structs[i].fieldType[originalIndex][k];
if ((v == '*') || (v == ' ') || (v == ','))
{
if (nameEnd != -1) {
// Don't copy to last additional field
if (fieldsRemaining != additionalFields)
{
nameStart = k + 1;
MemoryCopy(structs[i].fieldName[originalIndex + fieldsRemaining], &structs[i].fieldType[originalIndex][nameStart], nameEnd - nameStart + 1);
}
nameEnd = -1;
fieldsRemaining--;
}
}
else if (nameEnd == -1) nameEnd = k;
}
// Truncate original field type
int fieldTypeLength = nameStart;
structs[i].fieldType[originalIndex][fieldTypeLength] = '\0';
// Set field type and description of additional fields
for (int j = 1; j <= additionalFields; j++)
{
MemoryCopy(structs[i].fieldType[originalIndex + j], &structs[i].fieldType[originalIndex][0], fieldTypeLength);
MemoryCopy(structs[i].fieldDesc[originalIndex + j], &structs[i].fieldDesc[originalIndex][0], TextLength(structs[i].fieldDesc[originalIndex]));
}
}
}
}
l++;
}
// Move array sizes from name to type
for (int j = 0; j < structs[i].fieldCount; j++)
{
MoveArraySize(structs[i].fieldName[j], structs[i].fieldType[j]);
}
}
free(structLines);
// Alias info data
aliases = (AliasInfo *)calloc(MAX_ALIASES_TO_PARSE, sizeof(AliasInfo));
for (int i = 0; i < aliasCount; i++)
{
// Description from previous line
GetDescription(lines[aliasLines[i] - 1], aliases[i].desc);
char *linePtr = lines[aliasLines[i]];
// Skip "typedef "
int c = 8;
// Type
int typeStart = c;
while(linePtr[c] != ' ') c++;
int typeLen = c - typeStart;
MemoryCopy(aliases[i].type, &linePtr[typeStart], typeLen);
// Skip space
c++;
// Name
int nameStart = c;
while(linePtr[c] != ';') c++;
int nameLen = c - nameStart;
MemoryCopy(aliases[i].name, &linePtr[nameStart], nameLen);
// Description
GetDescription(&linePtr[c], aliases[i].desc);
}
free(aliasLines);
// Enum info data
enums = (EnumInfo *)calloc(MAX_ENUMS_TO_PARSE, sizeof(EnumInfo));
for (int i = 0; i < enumCount; i++)
{
// Parse enum description
// NOTE: This is not necessarily from the line immediately before,
// some of the enums have extra lines between the "description"
// and the typedef enum
for (int j = enumLines[i] - 1; j > 0; j--)
{
char *linePtr = lines[j];
if ((linePtr[0] != '/') || (linePtr[2] != ' '))
{
GetDescription(&lines[j + 1][0], enums[i].desc);
break;
}
}
for (int j = 1; j < MAX_ENUM_VALUES*2; j++) // Maximum number of lines following enum first line
{
char *linePtr = lines[enumLines[i] + j];
if ((linePtr[0] >= 'A') && (linePtr[0] <= 'Z'))
{
// Parse enum value line, possible options:
//ENUM_VALUE_NAME,
//ENUM_VALUE_NAME
//ENUM_VALUE_NAME = 99
//ENUM_VALUE_NAME = 99,
//ENUM_VALUE_NAME = 0x00000040, // Value description
// We start reading the value name
int c = 0;
while ((linePtr[c] != ',') &&
(linePtr[c] != ' ') &&
(linePtr[c] != '=') &&
(linePtr[c] != '\0'))
{
enums[i].valueName[enums[i].valueCount][c] = linePtr[c];
c++;
}
// After the name we can have:
// '=' -> value is provided
// ',' -> value is equal to previous + 1, there could be a description if not '\0'
// ' ' -> value is equal to previous + 1, there could be a description if not '\0'
// '\0' -> value is equal to previous + 1
// Let's start checking if the line is not finished
if ((linePtr[c] != ',') && (linePtr[c] != '\0'))
{
// Two options:
// '=' -> value is provided
// ' ' -> value is equal to previous + 1, there could be a description if not '\0'
bool foundValue = false;
while ((linePtr[c] != '\0') && (linePtr[c] != '/'))
{
if (linePtr[c] == '=')
{
foundValue = true;
break;
}
c++;
}
if (foundValue)
{
if (linePtr[c + 1] == ' ') c += 2;
else c++;
// Parse integer value
int n = 0;
char integer[16] = { 0 };
while ((linePtr[c] != ',') && (linePtr[c] != ' ') && (linePtr[c] != '\0'))
{
integer[n] = linePtr[c];
c++; n++;
}
if (integer[1] == 'x') enums[i].valueInteger[enums[i].valueCount] = (int)strtol(integer, NULL, 16);
else enums[i].valueInteger[enums[i].valueCount] = atoi(integer);
}
else enums[i].valueInteger[enums[i].valueCount] = (enums[i].valueInteger[enums[i].valueCount - 1] + 1);
}
else enums[i].valueInteger[enums[i].valueCount] = (enums[i].valueInteger[enums[i].valueCount - 1] + 1);
// Parse value description
GetDescription(&linePtr[c], enums[i].valueDesc[enums[i].valueCount]);
enums[i].valueCount++;
}
else if (linePtr[0] == '}')
{
// Get enum name from typedef
int c = 0;
while (linePtr[2 + c] != ';')
{
enums[i].name[c] = linePtr[2 + c];
c++;
}
break; // Enum ended, break for() loop
}
}
}
free(enumLines);
// Callback info data
callbacks = (FunctionInfo *)calloc(MAX_CALLBACKS_TO_PARSE, sizeof(FunctionInfo));
for (int i = 0; i < callbackCount; i++)
{
char *linePtr = lines[callbackLines[i]];
// Skip "typedef "
unsigned int c = 8;
// Return type
int retTypeStart = c;
while(linePtr[c] != '(') c++;
int retTypeLen = c - retTypeStart;
while(linePtr[retTypeStart + retTypeLen - 1] == ' ') retTypeLen--;
MemoryCopy(callbacks[i].retType, &linePtr[retTypeStart], retTypeLen);
// Skip "(*"
c += 2;
// Name
int nameStart = c;
while(linePtr[c] != ')') c++;
int nameLen = c - nameStart;
MemoryCopy(callbacks[i].name, &linePtr[nameStart], nameLen);
// Skip ")("
c += 2;
// Params
int paramStart = c;
for (; c < MAX_LINE_LENGTH; c++)
{
if ((linePtr[c] == ',') || (linePtr[c] == ')'))
{
// Get parameter type + name, extract info
int paramLen = c - paramStart;
GetDataTypeAndName(&linePtr[paramStart], paramLen, callbacks[i].paramType[callbacks[i].paramCount], callbacks[i].paramName[callbacks[i].paramCount]);
callbacks[i].paramCount++;
paramStart = c + 1;
while(linePtr[paramStart] == ' ') paramStart++;
}
if (linePtr[c] == ')') break;
}
// Description
GetDescription(&linePtr[c], callbacks[i].desc);
// Move array sizes from name to type
for (int j = 0; j < callbacks[i].paramCount; j++)
{
MoveArraySize(callbacks[i].paramName[j], callbacks[i].paramType[j]);
}
}
free(callbackLines);
// Functions info data
funcs = (FunctionInfo *)calloc(MAX_FUNCS_TO_PARSE, sizeof(FunctionInfo));
for (int i = 0; i < funcCount; i++)
{
char *linePtr = lines[funcLines[i]];
int funcParamsStart = 0;
int funcEnd = 0;
// Get return type and function name from func line
for (int c = 0; (c < MAX_LINE_LENGTH) && (linePtr[c] != '\n'); c++)
{
if (linePtr[c] == '(') // Starts function parameters
{
funcParamsStart = c + 1;
// At this point we have function return type and function name
char funcRetTypeName[128] = { 0 };
int dc = TextLength(apiDefine) + 1;
int funcRetTypeNameLen = c - dc; // Substract `define` ("RLAPI " for raylib.h)
MemoryCopy(funcRetTypeName, &linePtr[dc], funcRetTypeNameLen);
GetDataTypeAndName(funcRetTypeName, funcRetTypeNameLen, funcs[i].retType, funcs[i].name);
break;
}
}
// Get parameters from func line
for (int c = funcParamsStart; c < MAX_LINE_LENGTH; c++)
{
if (linePtr[c] == ',') // Starts function parameters
{
// Get parameter type + name, extract info
char funcParamTypeName[128] = { 0 };
int funcParamTypeNameLen = c - funcParamsStart;
MemoryCopy(funcParamTypeName, &linePtr[funcParamsStart], funcParamTypeNameLen);
GetDataTypeAndName(funcParamTypeName, funcParamTypeNameLen, funcs[i].paramType[funcs[i].paramCount], funcs[i].paramName[funcs[i].paramCount]);