From 1d7ff04923850db125a1b9c53ee84a029602f18a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F=20=D0=9A=D0=BE=D0=BB=D0=B1=D0=B8?= =?UTF-8?q?=D0=BD?= Date: Sat, 8 Jan 2022 15:15:04 +0300 Subject: [PATCH] added defines to parser --- parser/raylib_api.json | 210 +++++++++++++++++++++++++++++++++++++++++ parser/raylib_api.lua | 210 +++++++++++++++++++++++++++++++++++++++++ parser/raylib_api.txt | 159 +++++++++++++++++++++++++++++++ parser/raylib_api.xml | 54 +++++++++++ parser/raylib_parser.c | 163 +++++++++++++++++++++++++++++++- 5 files changed, 793 insertions(+), 3 deletions(-) diff --git a/parser/raylib_api.json b/parser/raylib_api.json index 5f703a77c..e37e32a4f 100644 --- a/parser/raylib_api.json +++ b/parser/raylib_api.json @@ -2485,6 +2485,216 @@ ] } ], + "defs": [ + { + "name": "RAYLIB_H", + "type": "GUARD" + }, + { + "name": "RAYLIB_VERSION", + "type": "STRING" + }, + { + "name": "RLAPI", + "type": "UNKNOWN" + }, + { + "name": "PI", + "type": "FLOAT" + }, + { + "name": "DEG2RAD", + "type": "UNKNOWN" + }, + { + "name": "RAD2DEG", + "type": "UNKNOWN" + }, + { + "name": "RL_MALLOC(sz)", + "type": "MACRO" + }, + { + "name": "RL_CALLOC(n,sz)", + "type": "MACRO" + }, + { + "name": "RL_REALLOC(ptr,sz)", + "type": "MACRO" + }, + { + "name": "RL_FREE(ptr)", + "type": "MACRO" + }, + { + "name": "CLITERAL(type)", + "type": "MACRO" + }, + { + "name": "RL_COLOR_TYPE", + "type": "GUARD" + }, + { + "name": "RL_RECTANGLE_TYPE", + "type": "GUARD" + }, + { + "name": "RL_VECTOR2_TYPE", + "type": "GUARD" + }, + { + "name": "RL_VECTOR3_TYPE", + "type": "GUARD" + }, + { + "name": "RL_VECTOR4_TYPE", + "type": "GUARD" + }, + { + "name": "RL_QUATERNION_TYPE", + "type": "GUARD" + }, + { + "name": "RL_MATRIX_TYPE", + "type": "GUARD" + }, + { + "name": "LIGHTGRAY", + "type": "COLOR" + }, + { + "name": "GRAY", + "type": "COLOR" + }, + { + "name": "DARKGRAY", + "type": "COLOR" + }, + { + "name": "YELLOW", + "type": "COLOR" + }, + { + "name": "GOLD", + "type": "COLOR" + }, + { + "name": "ORANGE", + "type": "COLOR" + }, + { + "name": "PINK", + "type": "COLOR" + }, + { + "name": "RED", + "type": "COLOR" + }, + { + "name": "MAROON", + "type": "COLOR" + }, + { + "name": "GREEN", + "type": "COLOR" + }, + { + "name": "LIME", + "type": "COLOR" + }, + { + "name": "DARKGREEN", + "type": "COLOR" + }, + { + "name": "SKYBLUE", + "type": "COLOR" + }, + { + "name": "BLUE", + "type": "COLOR" + }, + { + "name": "DARKBLUE", + "type": "COLOR" + }, + { + "name": "PURPLE", + "type": "COLOR" + }, + { + "name": "VIOLET", + "type": "COLOR" + }, + { + "name": "DARKPURPLE", + "type": "COLOR" + }, + { + "name": "BEIGE", + "type": "COLOR" + }, + { + "name": "BROWN", + "type": "COLOR" + }, + { + "name": "DARKBROWN", + "type": "COLOR" + }, + { + "name": "WHITE", + "type": "COLOR" + }, + { + "name": "BLACK", + "type": "COLOR" + }, + { + "name": "BLANK", + "type": "COLOR" + }, + { + "name": "MAGENTA", + "type": "COLOR" + }, + { + "name": "RAYWHITE", + "type": "COLOR" + }, + { + "name": "RL_BOOL_TYPE", + "type": "GUARD" + }, + { + "name": "MOUSE_LEFT_BUTTON", + "type": "UNKNOWN" + }, + { + "name": "MOUSE_RIGHT_BUTTON", + "type": "UNKNOWN" + }, + { + "name": "MOUSE_MIDDLE_BUTTON", + "type": "UNKNOWN" + }, + { + "name": "MATERIAL_MAP_DIFFUSE", + "type": "UNKNOWN" + }, + { + "name": "MATERIAL_MAP_SPECULAR", + "type": "UNKNOWN" + }, + { + "name": "SHADER_LOC_MAP_DIFFUSE", + "type": "UNKNOWN" + }, + { + "name": "SHADER_LOC_MAP_SPECULAR", + "type": "UNKNOWN" + } + ], "functions": [ { "name": "InitWindow", diff --git a/parser/raylib_api.lua b/parser/raylib_api.lua index f30d003dc..96f218784 100644 --- a/parser/raylib_api.lua +++ b/parser/raylib_api.lua @@ -2485,6 +2485,216 @@ return { } } }, + defs = { + { + name = "RAYLIB_H", + type = "GUARD" + }, + { + name = "RAYLIB_VERSION", + type = "STRING" + }, + { + name = "RLAPI", + type = "UNKNOWN" + }, + { + name = "PI", + type = "FLOAT" + }, + { + name = "DEG2RAD", + type = "UNKNOWN" + }, + { + name = "RAD2DEG", + type = "UNKNOWN" + }, + { + name = "RL_MALLOC(sz)", + type = "MACRO" + }, + { + name = "RL_CALLOC(n,sz)", + type = "MACRO" + }, + { + name = "RL_REALLOC(ptr,sz)", + type = "MACRO" + }, + { + name = "RL_FREE(ptr)", + type = "MACRO" + }, + { + name = "CLITERAL(type)", + type = "MACRO" + }, + { + name = "RL_COLOR_TYPE", + type = "GUARD" + }, + { + name = "RL_RECTANGLE_TYPE", + type = "GUARD" + }, + { + name = "RL_VECTOR2_TYPE", + type = "GUARD" + }, + { + name = "RL_VECTOR3_TYPE", + type = "GUARD" + }, + { + name = "RL_VECTOR4_TYPE", + type = "GUARD" + }, + { + name = "RL_QUATERNION_TYPE", + type = "GUARD" + }, + { + name = "RL_MATRIX_TYPE", + type = "GUARD" + }, + { + name = "LIGHTGRAY", + type = "COLOR" + }, + { + name = "GRAY", + type = "COLOR" + }, + { + name = "DARKGRAY", + type = "COLOR" + }, + { + name = "YELLOW", + type = "COLOR" + }, + { + name = "GOLD", + type = "COLOR" + }, + { + name = "ORANGE", + type = "COLOR" + }, + { + name = "PINK", + type = "COLOR" + }, + { + name = "RED", + type = "COLOR" + }, + { + name = "MAROON", + type = "COLOR" + }, + { + name = "GREEN", + type = "COLOR" + }, + { + name = "LIME", + type = "COLOR" + }, + { + name = "DARKGREEN", + type = "COLOR" + }, + { + name = "SKYBLUE", + type = "COLOR" + }, + { + name = "BLUE", + type = "COLOR" + }, + { + name = "DARKBLUE", + type = "COLOR" + }, + { + name = "PURPLE", + type = "COLOR" + }, + { + name = "VIOLET", + type = "COLOR" + }, + { + name = "DARKPURPLE", + type = "COLOR" + }, + { + name = "BEIGE", + type = "COLOR" + }, + { + name = "BROWN", + type = "COLOR" + }, + { + name = "DARKBROWN", + type = "COLOR" + }, + { + name = "WHITE", + type = "COLOR" + }, + { + name = "BLACK", + type = "COLOR" + }, + { + name = "BLANK", + type = "COLOR" + }, + { + name = "MAGENTA", + type = "COLOR" + }, + { + name = "RAYWHITE", + type = "COLOR" + }, + { + name = "RL_BOOL_TYPE", + type = "GUARD" + }, + { + name = "MOUSE_LEFT_BUTTON", + type = "UNKNOWN" + }, + { + name = "MOUSE_RIGHT_BUTTON", + type = "UNKNOWN" + }, + { + name = "MOUSE_MIDDLE_BUTTON", + type = "UNKNOWN" + }, + { + name = "MATERIAL_MAP_DIFFUSE", + type = "UNKNOWN" + }, + { + name = "MATERIAL_MAP_SPECULAR", + type = "UNKNOWN" + }, + { + name = "SHADER_LOC_MAP_DIFFUSE", + type = "UNKNOWN" + }, + { + name = "SHADER_LOC_MAP_SPECULAR", + type = "UNKNOWN" + }, + }, functions = { { name = "InitWindow", diff --git a/parser/raylib_api.txt b/parser/raylib_api.txt index 87b97662d..f4e431eb3 100644 --- a/parser/raylib_api.txt +++ b/parser/raylib_api.txt @@ -3693,3 +3693,162 @@ Function 491: SetAudioStreamBufferSizeDefault() (1 input parameters) Return type: void Description: Default size for new audio streams Param[1]: size (type: int) + +Defines found: 52 + +Define 001: RAYLIB_H + Name: RAYLIB_H + Type: GUARD +Define 002: RAYLIB_VERSION + Name: RAYLIB_VERSION + Type: STRING +Define 003: RLAPI + Name: RLAPI + Type: UNKNOWN +Define 004: PI + Name: PI + Type: FLOAT +Define 005: DEG2RAD + Name: DEG2RAD + Type: UNKNOWN +Define 006: RAD2DEG + Name: RAD2DEG + Type: UNKNOWN +Define 007: RL_MALLOC(sz) + Name: RL_MALLOC(sz) + Type: MACRO +Define 008: RL_CALLOC(n,sz) + Name: RL_CALLOC(n,sz) + Type: MACRO +Define 009: RL_REALLOC(ptr,sz) + Name: RL_REALLOC(ptr,sz) + Type: MACRO +Define 010: RL_FREE(ptr) + Name: RL_FREE(ptr) + Type: MACRO +Define 011: CLITERAL(type) + Name: CLITERAL(type) + Type: MACRO +Define 012: RL_COLOR_TYPE + Name: RL_COLOR_TYPE + Type: GUARD +Define 013: RL_RECTANGLE_TYPE + Name: RL_RECTANGLE_TYPE + Type: GUARD +Define 014: RL_VECTOR2_TYPE + Name: RL_VECTOR2_TYPE + Type: GUARD +Define 015: RL_VECTOR3_TYPE + Name: RL_VECTOR3_TYPE + Type: GUARD +Define 016: RL_VECTOR4_TYPE + Name: RL_VECTOR4_TYPE + Type: GUARD +Define 017: RL_QUATERNION_TYPE + Name: RL_QUATERNION_TYPE + Type: GUARD +Define 018: RL_MATRIX_TYPE + Name: RL_MATRIX_TYPE + Type: GUARD +Define 019: LIGHTGRAY + Name: LIGHTGRAY + Type: COLOR +Define 020: GRAY + Name: GRAY + Type: COLOR +Define 021: DARKGRAY + Name: DARKGRAY + Type: COLOR +Define 022: YELLOW + Name: YELLOW + Type: COLOR +Define 023: GOLD + Name: GOLD + Type: COLOR +Define 024: ORANGE + Name: ORANGE + Type: COLOR +Define 025: PINK + Name: PINK + Type: COLOR +Define 026: RED + Name: RED + Type: COLOR +Define 027: MAROON + Name: MAROON + Type: COLOR +Define 028: GREEN + Name: GREEN + Type: COLOR +Define 029: LIME + Name: LIME + Type: COLOR +Define 030: DARKGREEN + Name: DARKGREEN + Type: COLOR +Define 031: SKYBLUE + Name: SKYBLUE + Type: COLOR +Define 032: BLUE + Name: BLUE + Type: COLOR +Define 033: DARKBLUE + Name: DARKBLUE + Type: COLOR +Define 034: PURPLE + Name: PURPLE + Type: COLOR +Define 035: VIOLET + Name: VIOLET + Type: COLOR +Define 036: DARKPURPLE + Name: DARKPURPLE + Type: COLOR +Define 037: BEIGE + Name: BEIGE + Type: COLOR +Define 038: BROWN + Name: BROWN + Type: COLOR +Define 039: DARKBROWN + Name: DARKBROWN + Type: COLOR +Define 040: WHITE + Name: WHITE + Type: COLOR +Define 041: BLACK + Name: BLACK + Type: COLOR +Define 042: BLANK + Name: BLANK + Type: COLOR +Define 043: MAGENTA + Name: MAGENTA + Type: COLOR +Define 044: RAYWHITE + Name: RAYWHITE + Type: COLOR +Define 045: RL_BOOL_TYPE + Name: RL_BOOL_TYPE + Type: GUARD +Define 046: MOUSE_LEFT_BUTTON + Name: MOUSE_LEFT_BUTTON + Type: UNKNOWN +Define 047: MOUSE_RIGHT_BUTTON + Name: MOUSE_RIGHT_BUTTON + Type: UNKNOWN +Define 048: MOUSE_MIDDLE_BUTTON + Name: MOUSE_MIDDLE_BUTTON + Type: UNKNOWN +Define 049: MATERIAL_MAP_DIFFUSE + Name: MATERIAL_MAP_DIFFUSE + Type: UNKNOWN +Define 050: MATERIAL_MAP_SPECULAR + Name: MATERIAL_MAP_SPECULAR + Type: UNKNOWN +Define 051: SHADER_LOC_MAP_DIFFUSE + Name: SHADER_LOC_MAP_DIFFUSE + Type: UNKNOWN +Define 052: SHADER_LOC_MAP_SPECULAR + Name: SHADER_LOC_MAP_SPECULAR + Type: UNKNOWN diff --git a/parser/raylib_api.xml b/parser/raylib_api.xml index 72ecf0509..3faa24ba7 100644 --- a/parser/raylib_api.xml +++ b/parser/raylib_api.xml @@ -542,6 +542,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/parser/raylib_parser.c b/parser/raylib_parser.c index ed534299c..290bb121b 100644 --- a/parser/raylib_parser.c +++ b/parser/raylib_parser.c @@ -2,12 +2,13 @@ raylib API parser - This parser scans raylib.h to get API information about structs, enums and functions. + This parser scans raylib.h to get API information about structs, enums, functions and defines. All data is divided into pieces, usually as strings. The following types are used for data: - struct FunctionInfo - struct StructInfo - struct EnumInfo + - struct DefInfo CONSTRAINTS: @@ -62,10 +63,12 @@ #include // Required for: malloc(), calloc(), realloc(), free(), atoi(), strtol() #include // Required for: printf(), fopen(), fseek(), ftell(), fread(), fclose() #include // Required for: bool +#include // Required for: isdigit() #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_LINE_LENGTH 512 // Maximum length of one line (including comments) #define MAX_STRUCT_LINE_LENGTH 2048 // Maximum length of one struct (multiple lines) @@ -108,6 +111,14 @@ typedef struct EnumInfo { char valueDesc[MAX_ENUM_VALUES][128]; // Value description } EnumInfo; +typedef enum { UNKNOWN, MACRO, GUARD, INT, FLOAT, DOUBLE, CHAR, STRING, COLOR } DefineType; + +// Define info data +typedef struct DefineInfo { + char name[64]; + DefineType type; +} DefineInfo; + // Output format for parsed data typedef enum { DEFAULT = 0, JSON, XML, LUA } OutputFormat; @@ -117,9 +128,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 FunctionInfo *funcs = NULL; static StructInfo *structs = NULL; static EnumInfo *enums = NULL; +static DefineInfo *defs = NULL; static char apiDefine[32] = "RLAPI\0"; // Command line variables @@ -140,6 +153,7 @@ 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 @@ -169,6 +183,9 @@ int main(int argc, char* argv[]) // Enums lines pointers, selected from buffer "lines" 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)); + // Prepare required lines for parsing //-------------------------------------------------------------------------------------------------- @@ -228,7 +245,22 @@ int main(int argc, char* argv[]) } } - // At this point we have all raylib structs, enums, functions lines data to start parsing + // Read const 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 + defLines[defCount] = i; + defCount++; + } + } + + // At this point we have all raylib structs, enums, functions, defs lines data to start parsing free(buffer); // Unload text buffer @@ -402,9 +434,72 @@ int main(int argc, char* argv[]) } } } - free(enumLines); + // Define info data + defs = (DefineInfo *)calloc(MAX_DEFINES_TO_PARSE, sizeof(DefineInfo)); + int defineIndex = 0; + + for (int i = 0; i < defCount; i++) + { + char *linePtr = lines[defLines[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; + } + } + + // Skip duplicates + int len = constNameEnd - constNameStart + 1; + bool isDuplicate = false; + for (int k = 0; k < defineIndex; k++) { + if (len == TextLength(defs[k].name) && IsTextEqual(defs[k].name, linePtr + constNameStart, len)) { + isDuplicate = true; + break; + } + } + if (isDuplicate) continue; + + MemoryCopy(defs[defineIndex].name, linePtr + constNameStart, len); + defineIndex++; + } + defCount = defineIndex; + free(defLines); + // Functions info data funcs = (FunctionInfo *)calloc(MAX_FUNCS_TO_PARSE, sizeof(FunctionInfo)); @@ -486,6 +581,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 // Process input file to output if (outFileName[0] == '\0') MemoryCopy(outFileName, "raylib_api.txt\0", 15); @@ -759,6 +855,23 @@ static char *EscapeBackslashes(char *text) return buffer; } +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"; + } + return ""; +} + /* // Replace text string // REQUIRES: strlen(), strstr(), strncpy(), strcpy() -> TODO: Replace by custom implementations! @@ -851,6 +964,14 @@ static void ExportParsedData(const char *fileName, int format) for (int p = 0; p < funcs[i].paramCount; p++) fprintf(outFile, " Param[%i]: %s (type: %s)\n", p + 1, funcs[i].paramName[p], funcs[i].paramType[p]); 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, "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)); + } } break; case LUA: { @@ -906,6 +1027,20 @@ 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++) + { + fprintf(outFile, " {\n"); + fprintf(outFile, " name = \"%s\",\n", defs[i].name); + fprintf(outFile, " type = \"%s\"\n", StrDefineType(defs[i].type)); + fprintf(outFile, " }"); + + if (i < funcCount - 1) fprintf(outFile, ",\n"); + else fprintf(outFile, "\n"); + } + fprintf(outFile, " },\n"); + // Print functions info fprintf(outFile, " functions = {\n"); for (int i = 0; i < funcCount; i++) @@ -989,6 +1124,20 @@ 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++) + { + fprintf(outFile, " {\n"); + fprintf(outFile, " \"name\": \"%s\",\n", defs[i].name); + fprintf(outFile, " \"type\": \"%s\"\n", StrDefineType(defs[i].type)); + fprintf(outFile, " }"); + + if (i < defCount - 1) fprintf(outFile, ",\n"); + else fprintf(outFile, "\n"); + } + fprintf(outFile, " ],\n"); + // Print functions info fprintf(outFile, " \"functions\": [\n"); for (int i = 0; i < funcCount; i++) @@ -1077,6 +1226,14 @@ static void ExportParsedData(const char *fileName, int format) } fprintf(outFile, " \n"); + // Print defs info + fprintf(outFile, " \n", defCount); + for (int i = 0; i < defCount; i++) + { + fprintf(outFile, " \n", defs[i].name, StrDefineType(defs[i].type)); + } + fprintf(outFile, " \n"); + // Print functions info fprintf(outFile, " \n", funcCount); for (int i = 0; i < funcCount; i++)