JSON
Parser program is generated as C99 code by default. However, you can generate it as JSON. You can convert the JSON program to C program using a function call. A library is provided utility file for this.
static int ReadFile(const char * filename, char ** text, unsigned int * length)
{
FILE* fp = fopen(filename, "rb");
if(fp == 0) return 0;
fseek(fp, 0, SEEK_END);
length[0] = ftell(fp);
fseek(fp, 0, SEEK_SET);
*text = malloc(length[0] + 1);
memset(*text, 0, length[0] + 1);
fread(*text, 1, length[0], fp);
fclose(fp);
return 1;
}
int main()
{
struct AlgodalParser_Program program = AlgodalJsonParser_GetProgramFromFile("ini-program.json", 0);
//AlgodalJsonParser_DebugProgram(program);
char * text = 0;
unsigned int length;
if(ReadFile("sample.ini", &text, &length) && program.successful_allocation_json)
{
clock_t begin = clock(), end;
double duration;
struct AlgodalParser_TokenizeResult tr = AlgodalParser_GetTokenizeResult(program, text, length, 0);
struct AlgodalParser_AnalyzeResult ar = {};
if(tr.error.flags == 0)
{
ar = AlgodalParser_GetAnalyzeResult(program, text, tr.tokens.addr, tr.tokens.size, 0);
}
end = clock();
duration = (double)(end - begin) / CLOCKS_PER_SEC;
struct AlgodalParser_Linenumber *L = AlgodalParser_CreateParserLinenumber(text, length);
cwpc_printf("\nTokenization:\n");
AlgodalParser_PrintTokenizeResult(program, tr, text, L);
if(ar.nodes.size)
{
cwpc_printf("\nAnalyzation:\n");
AlgodalParser_PrintAnalyzeResult(program, ar, tr.tokens.addr, text, L);
}
AlgodalParser_DestroyParserLinenumber(L);
AlgodalParser_DestroyTokenizeResult(tr);
if(ar.nodes.size) AlgodalParser_DestroyAnalyzeResult(ar);
AlgodalJsonParser_DestroyProgram(program);
free(text);
cwpc_printf("completed in %f milliseconds\n", duration * 1000);
}
return EXIT_SUCCESS;
}