added value and description for defines in parser

This commit is contained in:
Ilya 2022-01-10 18:43:19 +03:00
commit 5097077597

View file

@ -68,7 +68,7 @@
#define MAX_FUNCS_TO_PARSE 512 // Maximum number of functions to parse
#define MAX_STRUCTS_TO_PARSE 64 // Maximum number of structures to parse
#define MAX_ENUMS_TO_PARSE 64 // Maximum number of enums to parse
#define MAX_DEFINES_TO_PARSE 512
#define MAX_DEFINES_TO_PARSE 2048 // Maximum number of defines to parse
#define MAX_LINE_LENGTH 512 // Maximum length of one line (including comments)
#define MAX_STRUCT_LINE_LENGTH 2048 // Maximum length of one struct (multiple lines)
@ -111,12 +111,15 @@ typedef struct EnumInfo {
char valueDesc[MAX_ENUM_VALUES][128]; // Value description
} EnumInfo;
typedef enum { UNKNOWN, MACRO, GUARD, INT, FLOAT, DOUBLE, CHAR, STRING, COLOR } DefineType;
// Type of parsed define
typedef enum { UNKNOWN = 0, MACRO, GUARD, INT, FLOAT, DOUBLE, CHAR, STRING, COLOR } DefineType;
// Define info data
typedef struct DefineInfo {
char name[64];
DefineType type;
char name[64]; // Define name
DefineType type; // Define type
char value[256]; // Define value
char desc[128]; // Define description
} DefineInfo;
// Output format for parsed data
@ -128,11 +131,11 @@ typedef enum { DEFAULT = 0, JSON, XML, LUA } OutputFormat;
static int funcCount = 0;
static int structCount = 0;
static int enumCount = 0;
static int defCount = 0;
static int defineCount = 0;
static FunctionInfo *funcs = NULL;
static StructInfo *structs = NULL;
static EnumInfo *enums = NULL;
static DefineInfo *defs = NULL;
static DefineInfo *defines = NULL;
static char apiDefine[32] = "RLAPI\0";
// Command line variables
@ -153,10 +156,11 @@ static unsigned int TextLength(const char *text); // Get text length i
static bool IsTextEqual(const char *text1, const char *text2, unsigned int count);
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
static const char *StrDefineType(DefineType type); // Get string of define type
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
@ -184,7 +188,7 @@ int main(int argc, char* argv[])
int *enumLines = (int *)malloc(MAX_ENUMS_TO_PARSE*sizeof(int));
// Defines lines pointers, selected from buffer "lines"
int *defLines = (int *)malloc(MAX_DEFINES_TO_PARSE*sizeof(int));
int *defineLines = (int *)malloc(MAX_DEFINES_TO_PARSE*sizeof(int));
// Prepare required lines for parsing
//--------------------------------------------------------------------------------------------------
@ -255,12 +259,12 @@ int main(int argc, char* argv[])
{
// Keep the line position in the array of lines,
// so, we can scan that position and following lines
defLines[defCount] = i;
defCount++;
defineLines[defineCount] = i;
defineCount++;
}
}
// At this point we have all raylib structs, enums, functions, defs lines data to start parsing
// At this point we have all raylib structs, enums, functions, defines lines data to start parsing
free(buffer); // Unload text buffer
@ -437,68 +441,83 @@ int main(int argc, char* argv[])
free(enumLines);
// Define info data
defs = (DefineInfo *)calloc(MAX_DEFINES_TO_PARSE, sizeof(DefineInfo));
defines = (DefineInfo *)calloc(MAX_DEFINES_TO_PARSE, sizeof(DefineInfo));
int defineIndex = 0;
for (int i = 0; i < defCount; i++)
for (int i = 0; i < defineCount; i++)
{
char *linePtr = lines[defLines[i]];
char *linePtr = lines[defineLines[i]];
int j = 0;
int constNameStart, constNameEnd;
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 "
constNameStart = j;
while (linePtr[j] != ' ' && linePtr[j] != '\t' && linePtr[j] != '\0') j++; // Extract name
constNameEnd = j-1;
// Functional macros
if (linePtr[constNameEnd] == ')') {
defs[defineIndex].type = MACRO;
}
while (linePtr[j] == ' ' || linePtr[j] == '\t') j++; // Skip spaces and tabs after name
// Empty
if (linePtr[j] == '\0' || linePtr == "/") {
defs[defineIndex].type = GUARD;
}
if (linePtr[j] == '"') defs[defineIndex].type = STRING;
else if (linePtr[j] == '\'') defs[defineIndex].type = CHAR;
else if (IsTextEqual(linePtr+j, "CLITERAL(Color)", 15)) defs[defineIndex].type = COLOR;
// Parsing numbers
else if (isdigit(linePtr[j])) {
bool isFloat = false;
while (linePtr[j] != ' ' && linePtr[j] != '\t' && linePtr[j] != '\n' && linePtr[j] != '\r') {
j++;
if (linePtr[j] == '.') isFloat = true;
}
if (isFloat) {
defs[defineIndex].type = linePtr[j-1] == 'f' ? FLOAT : DOUBLE;
} else {
defs[defineIndex].type = INT;
}
}
// Extract name
int defineNameStart = j;
while (linePtr[j] != ' ' && linePtr[j] != '\t' && linePtr[j] != '\0') j++;
int defineNameEnd = j-1;
// Skip duplicates
int len = constNameEnd - constNameStart + 1;
int nameLen = defineNameEnd - defineNameStart + 1;
bool isDuplicate = false;
for (int k = 0; k < defineIndex; k++) {
if (len == TextLength(defs[k].name) && IsTextEqual(defs[k].name, linePtr + constNameStart, len)) {
if (nameLen == TextLength(defines[k].name) && IsTextEqual(defines[k].name, linePtr + defineNameStart, nameLen)) {
isDuplicate = true;
break;
}
}
if (isDuplicate) continue;
MemoryCopy(defs[defineIndex].name, linePtr + constNameStart, len);
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 == "/") 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;
while (linePtr[j] != ' ' && linePtr[j] != '\t' && linePtr[j] != '\n' && linePtr[j] != '\r') {
j++;
if (linePtr[j] == '.') isFloat = true;
}
if (isFloat) {
defines[defineIndex].type = linePtr[j-1] == 'f' ? FLOAT : DOUBLE;
} else {
defines[defineIndex].type = INT;
}
}
// 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
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] == '/') {
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);
}
defineIndex++;
}
defCount = defineIndex;
free(defLines);
defineCount = defineIndex;
free(defineLines);
// Functions info data
funcs = (FunctionInfo *)calloc(MAX_FUNCS_TO_PARSE, sizeof(FunctionInfo));
@ -581,7 +600,7 @@ int main(int argc, char* argv[])
// structs[] -> We have all the structs decomposed into pieces for further analysis
// enums[] -> We have all the enums decomposed into pieces for further analysis
// funcs[] -> We have all the functions decomposed into pieces for further analysis
// defs[] -> We have all the defines decomposed into pieces for further analysis
// defines[] -> We have all the defines decomposed into pieces for further analysis
// Process input file to output
if (outFileName[0] == '\0') MemoryCopy(outFileName, "raylib_api.txt\0", 15);
@ -855,19 +874,20 @@ static char *EscapeBackslashes(char *text)
return buffer;
}
// Get string of define type
static const char *StrDefineType(DefineType type)
{
switch (type)
{
case UNKNOWN: return "UNKNOWN";
case GUARD: return "GUARD";
case MACRO: return "MACRO";
case INT: return "INT";
case FLOAT: return "FLOAT";
case DOUBLE: return "DOUBLE";
case CHAR: return "CHAR";
case STRING: return "STRING";
case COLOR: return "COLOR";
case GUARD: return "GUARD";
case MACRO: return "MACRO";
case INT: return "INT";
case FLOAT: return "FLOAT";
case DOUBLE: return "DOUBLE";
case CHAR: return "CHAR";
case STRING: return "STRING";
case COLOR: return "COLOR";
}
return "";
}
@ -949,7 +969,7 @@ static void ExportParsedData(const char *fileName, int format)
{
fprintf(outFile, "Enum %02i: %s (%i values)\n", i + 1, enums[i].name, enums[i].valueCount);
fprintf(outFile, " Name: %s\n", enums[i].name);
fprintf(outFile, " Description: %s\n", enums[i].desc + 3);
fprintf(outFile, " Description: %s\n", enums[i].desc + 3);
for (int e = 0; e < enums[i].valueCount; e++) fprintf(outFile, " Value[%s]: %i\n", enums[i].valueName[e], enums[i].valueInteger[e]);
}
@ -965,12 +985,14 @@ static void ExportParsedData(const char *fileName, int format)
if (funcs[i].paramCount == 0) fprintf(outFile, " No input parameters\n");
}
fprintf(outFile, "\nDefines found: %i\n\n", defCount);
for (int i = 0; i < defCount; i++)
fprintf(outFile, "\nDefines found: %i\n\n", defineCount);
for (int i = 0; i < defineCount; i++)
{
fprintf(outFile, "Define %03i: %s\n", i + 1, defs[i].name);
fprintf(outFile, " Name: %s\n", defs[i].name);
fprintf(outFile, " Type: %s\n", StrDefineType(defs[i].type));
fprintf(outFile, "Define %03i: %s\n", i + 1, defines[i].name);
fprintf(outFile, " Name: %s\n", defines[i].name);
fprintf(outFile, " Type: %s\n", StrDefineType(defines[i].type));
fprintf(outFile, " Value: %s\n", defines[i].value);
fprintf(outFile, " Description: %s\n", defines[i].desc + 3);
}
} break;
case LUA:
@ -1027,13 +1049,24 @@ static void ExportParsedData(const char *fileName, int format)
}
fprintf(outFile, " },\n");
// Print defs info
fprintf(outFile, " defs = {\n");
for (int i = 0; i < defCount; i++)
// Print defines info
fprintf(outFile, " defines = {\n");
for (int i = 0; i < defineCount; i++)
{
fprintf(outFile, " {\n");
fprintf(outFile, " name = \"%s\",\n", defs[i].name);
fprintf(outFile, " type = \"%s\"\n", StrDefineType(defs[i].type));
fprintf(outFile, " name = \"%s\",\n", defines[i].name);
fprintf(outFile, " type = \"%s\",\n", StrDefineType(defines[i].type));
if (defines[i].type == INT || defines[i].type == DOUBLE || defines[i].type == STRING) {
fprintf(outFile, " value = %s,\n", defines[i].value);
} else if (defines[i].type == FLOAT) {
int n = TextLength(defines[i].value);
defines[i].value[n-1] = '\0';
fprintf(outFile, " value = %s,\n", defines[i].value);
defines[i].value[n-1] = 'f';
} else {
fprintf(outFile, " value = \"%s\",\n", defines[i].value);
}
fprintf(outFile, " description = \"%s\"\n", defines[i].desc + 3);
fprintf(outFile, " }");
if (i < funcCount - 1) fprintf(outFile, ",\n");
@ -1124,16 +1157,27 @@ static void ExportParsedData(const char *fileName, int format)
}
fprintf(outFile, " ],\n");
// Print defs info
fprintf(outFile, " \"defs\": [\n");
for (int i = 0; i < defCount; i++)
// Print defines info
fprintf(outFile, " \"defines\": [\n");
for (int i = 0; i < defineCount; i++)
{
fprintf(outFile, " {\n");
fprintf(outFile, " \"name\": \"%s\",\n", defs[i].name);
fprintf(outFile, " \"type\": \"%s\"\n", StrDefineType(defs[i].type));
fprintf(outFile, " \"name\": \"%s\",\n", defines[i].name);
fprintf(outFile, " \"type\": \"%s\",\n", StrDefineType(defines[i].type));
if (defines[i].type == INT || defines[i].type == DOUBLE || defines[i].type == STRING) {
fprintf(outFile, " \"value\": %s,\n", defines[i].value);
} else if (defines[i].type == FLOAT) {
int n = TextLength(defines[i].value);
defines[i].value[n-1] = '\0';
fprintf(outFile, " \"value\": %s,\n", defines[i].value);
defines[i].value[n-1] = 'f';
} else {
fprintf(outFile, " \"value\": \"%s\",\n", defines[i].value);
}
fprintf(outFile, " \"description\": \"%s\"\n", defines[i].desc + 3);
fprintf(outFile, " }");
if (i < defCount - 1) fprintf(outFile, ",\n");
if (i < defineCount - 1) fprintf(outFile, ",\n");
else fprintf(outFile, "\n");
}
fprintf(outFile, " ],\n");
@ -1226,11 +1270,11 @@ static void ExportParsedData(const char *fileName, int format)
}
fprintf(outFile, " </Enums>\n");
// Print defs info
fprintf(outFile, " <Defines count=\"%i\">\n", defCount);
for (int i = 0; i < defCount; i++)
// Print defines info
fprintf(outFile, " <Defines count=\"%i\">\n", defineCount);
for (int i = 0; i < defineCount; i++)
{
fprintf(outFile, " <Define name=\"%s\" type=\"%s\" />\n", defs[i].name, StrDefineType(defs[i].type));
fprintf(outFile, " <Define name=\"%s\" type=\"%s\" value=\"%s\" desc=\"%s\" />\n", defines[i].name, StrDefineType(defines[i].type), defines[i].value, defines[i].desc);
}
fprintf(outFile, " </Defines>\n");