Version 7.0
Copyright © 2021 Lowell D. Thomas
APG
… an ABNF Parser Generator
|
Go to the documentation of this file.
115 #include "../../utilities/utilities.h"
116 #include "../../api/api.h"
120 static const char* cpMakeFileName(
char* cpBuffer,
const char* cpBase,
const char* cpDivider,
const char* cpName){
121 strcpy(cpBuffer, cpBase);
122 strcat(cpBuffer, cpDivider);
123 strcat(cpBuffer, cpName);
126 static char caBuf[PATH_MAX];
128 static char* s_cpDescription =
129 "Illustrate parser tracing and parser, memory and vector statistics.";
131 static char* s_cppCases[] = {
132 "Display application information.",
133 "Illustrate default tracing with and without PPPT.",
134 "Generate a trace configuration file template.",
135 "Trace a restricted record range.",
136 "Trace rule names only.",
137 "Trace only specific rule names.",
138 "Parsing statistics, hit count vs alphabetical.",
139 "Parsing statistics, with and without PPPT.",
140 "Parsing statistics, cumulative for multiple parses.",
141 "Illustrate memory statistics.",
142 "Illustrate vector statistics.",
144 static long int s_iCaseCount = (
long int)(
sizeof(s_cppCases) /
sizeof(s_cppCases[0]));
146 static int iHelp(
void){
149 printf(
"description: %s\n", s_cpDescription);
150 printf(
" usage: ex-trace [arg]\n");
151 printf(
" arg = n, 1 <= n <= %ld\n", s_iCaseCount);
152 printf(
" execute case number n\n");
153 printf(
" arg = anything else, or nothing at all\n");
154 printf(
" print this help screen\n");
156 for(; i < s_iCaseCount; i++){
157 printf(
"case %ld %s\n", (i + 1), s_cppCases[i]);
172 static int iTraceDefault() {
173 int iReturn = EXIT_SUCCESS;
174 static void* vpApi = NULL;
175 static void* vpMem = NULL;
176 static void* vpParser = NULL;
177 static void* vpParserPppt = NULL;
178 char* cpInput =
"+123456789.0987654321E+100";
197 printf(
"\nTrace without PPPT\n");
199 memset(&sConfig, 0,
sizeof(sConfig));
206 printf(
"\nParser State without PPPT\n");
214 printf(
"\nTrace with PPPT\n");
221 printf(
"\nParser State with PPPT\n");
230 iReturn = EXIT_FAILURE;
243 static int iTraceConfigGen() {
244 int iReturn = EXIT_SUCCESS;
245 static void* vpApi = NULL;
246 static void* vpParser = NULL;
247 static void* vpTrace = NULL;
259 printf(
"\nDisplay the Trace Configuration File to stdout\n");
266 iReturn = EXIT_FAILURE;
275 static int iTraceConfigRange() {
276 int iReturn = EXIT_SUCCESS;
277 static void* vpApi = NULL;
278 static void* vpMem = NULL;
279 static void* vpParser = NULL;
280 static void* vpTrace = NULL;
281 char* cpInput =
"+123456789.0987654321E+100";
300 const char* cpConfig = cpMakeFileName(caBuf, SOURCE_DIR,
"/../input/",
"float-config-range");
301 printf(
"\nUsing trace configuration file %s \n", cpConfig);
304 memset(&sConfig, 0,
sizeof(sConfig));
311 printf(
"\nParser State without PPPT\n");
320 iReturn = EXIT_FAILURE;
330 static int iTraceConfigRules() {
331 int iReturn = EXIT_SUCCESS;
332 static void* vpApi = NULL;
333 static void* vpMem = NULL;
334 static void* vpParser = NULL;
335 static void* vpTrace = NULL;
336 char* cpInput =
"+123456789.0987654321E+100";
355 const char* cpConfig = cpMakeFileName(caBuf, SOURCE_DIR,
"/../input/",
"float-config-rules");
356 printf(
"\nUsing trace configuration file %s \n", cpConfig);
359 memset(&sConfig, 0,
sizeof(sConfig));
366 printf(
"\nParser State without PPPT\n");
375 iReturn = EXIT_FAILURE;
385 static int iTraceConfigSelect() {
386 int iReturn = EXIT_SUCCESS;
387 static void* vpApi = NULL;
388 static void* vpMem = NULL;
389 static void* vpParser = NULL;
390 static void* vpTrace = NULL;
391 char* cpInput =
"+123456789.0987654321E+100";
410 const char* cpConfig = cpMakeFileName(caBuf, SOURCE_DIR,
"/../input/",
"float-config-select");
411 printf(
"\nUsing trace configuration file %s \n", cpConfig);
414 memset(&sConfig, 0,
sizeof(sConfig));
421 printf(
"\nParser State without PPPT\n");
430 iReturn = EXIT_FAILURE;
440 static int iStatsHits() {
441 int iReturn = EXIT_SUCCESS;
442 static void* vpApi = NULL;
443 static void* vpMem = NULL;
444 static void* vpParser = NULL;
445 static void* vpStats = NULL;
448 "\"array\": [1, true, false, null, 2345],\n"
453 "\"s\": \"string\"\n"
455 "\"more\": [12345, 6789809, 234, 3456,456],\n"
457 "\"key2\": \"end of object\"\n"
479 memset(&sConfig, 0,
sizeof(sConfig));
486 printf(
"\nParser State\n");
490 printf(
"\nStatistics ordered on hit count.\n");
494 printf(
"\nStatistics ordered alphabetically on rule names.\n");
503 iReturn = EXIT_FAILURE;
515 static int iStatsPppt() {
516 int iReturn = EXIT_SUCCESS;
517 static void* vpApi = NULL;
518 static void* vpMem = NULL;
519 static void* vpParser = NULL;
520 static void* vpParserPppt = NULL;
521 static void* vpStats = NULL;
522 static void* vpStatsPppt = NULL;
525 "\"array\": [1, true, false, null, 2345],\n"
530 "\"s\": \"string\"\n"
532 "\"more\": [12345, 6789809, 234, 3456,456],\n"
534 "\"key2\": \"end of object\"\n"
561 memset(&sConfig, 0,
sizeof(sConfig));
568 printf(
"\nParser State\n");
572 printf(
"\nStatistics without PPPT ordered on hit count.\n");
579 printf(
"\nParser State\n");
583 printf(
"\nStatistics with PPPT ordered on hit count.\n");
592 iReturn = EXIT_FAILURE;
605 static int iStatsCumulative() {
606 int iReturn = EXIT_SUCCESS;
607 static void* vpApi = NULL;
608 static void* vpMem = NULL;
609 static void* vpParser = NULL;
610 static void* vpStats = NULL;
611 char* cpInput2 =
"[true, false, 123456, {\"key\": \"string\"}]";
614 "\"array\": [1, true, false, null, 2345],\n"
619 "\"s\": \"string\"\n"
621 "\"more\": [12345, 6789809, 234, 3456,456],\n"
623 "\"key2\": \"end of object\"\n"
645 memset(&sConfig, 0,
sizeof(sConfig));
652 printf(
"\nParser State Input 1\n");
656 printf(
"\nStatistics for input 1.\n");
661 memset(&sConfig, 0,
sizeof(sConfig));
668 printf(
"\nParser State Input 2\n");
672 printf(
"\nStatistics for input 1 + input 2.\n");
673 printf(
"Notice that the start rule, JSON-text, appears twice, once for each parsed string.\n");
682 iReturn = EXIT_FAILURE;
694 static int iMemStats() {
695 int iReturn = EXIT_SUCCESS;
697 static void* vpMem = NULL;
710 printf(
"\nMemory Statistics: 3 allocations.\n");
716 printf(
"\nMemory Statistics: 2 allocations after 1 free.\n");
721 printf(
"\nMemory Statistics: 2 allocations after 1 free and 1 reallocation.\n");
727 printf(
"\nMemory Statistics: after clearing the memory with vMemClear().\n");
739 static int iVecStats() {
740 int iReturn = EXIT_SUCCESS;
746 static void* vpMem = NULL;
747 static void* vpVec = NULL;
755 vpVec =
vpVecCtor(vpMem,
sizeof(
struct info), 15);
759 strcpy(sInfo.caInfo,
"first");
763 printf(
"\nVector Statistics: 1 push.\n");
768 spTest = (
struct info*)
vpVecPushn(vpVec, NULL, 3);
769 spTest[0].uiIndex = 1;
770 strcpy(spTest[0].caInfo,
"second");
771 printf(
"\nVector Statistics: more data.\n");
777 printf(
"\nVector Statistics: make the vector grow.\n");
785 printf(
"\nVector Statistics: pop some data.\n");
791 printf(
"\nVector Statistics: clear the vector.\n");
814 int main(
int argc,
char **argv) {
817 iCase = atol(argv[1]);
819 if((iCase > 0) && (iCase <= s_iCaseCount)){
820 printf(
"%s\n", s_cppCases[iCase -1]);
826 return iTraceDefault();
828 return iTraceConfigGen();
830 return iTraceConfigRange();
832 return iTraceConfigRules();
834 return iTraceConfigSelect();
840 return iStatsCumulative();
#define XCTOR(e)
This macro will initialize an exception structure and prepare entry to the "try" block.
void vMemDtor(void *vpCtx)
Destroys a Memory component. Frees all memory allocated.
abool try
True for the try block, false for the catch block.
void vVecStats(void *vpCtx, vec_stats *spStats)
void * vpStatsCtor(void *vpParserCtx)
The statistics object constructor.
const achar * acpInput
Pointer to the input string.
void vMemClear(void *vpCtx)
Frees all memory allocations.
void * vpApiCtor(exception *spEx)
Construct an API component context (object).
void vUtilPrintMemStats(const mem_stats *spStats)
Display the memory object's statistics.
aint uiParserRuleLookup(void *vpCtx, const char *cpRuleName)
Find the rule index corresponding to a rule name.
void vApiDtor(void *vpCtx)
The API component destructor.
Available to the user for display of memory statistics.
void * vpMemRealloc(void *vpCtx, const void *vpData, aint uiBytes)
Re-allocates memory previously allocated with vpMemAlloc().
void * vpVecPop(void *vpCtx)
Pops one element from the end of the array.
uint_fast32_t aint
The APG parser's unsigned integer type.
void * vpVecPopn(void *vpCtx, aint uiCount)
Pops one or more elements from the end of the array.
aint uiInputLength
Number of input string alphabet characters.
void * vpTraceCtor(void *vpCtx)
The trace object constructor.
Defines the input string and other configuration parameters for the parser,.
void vApiFile(void *vpCtx, const char *cpFileName, abool bStrict, abool bPppt)
Quicky way to generate a parser from a grammar file.
int main(int argc, char **argv)
The executable from this main function is the ABNF Parser Generator application, APG.
void * vpMemAlloc(void *vpCtx, aint uiBytes)
Allocates memory.
void * vpVecCtor(void *vpMem, aint uiElementSize, aint uiInitialAlloc)
The vector object constructor.
The parser's final state.
void vStatsToAscii(void *vpCtx, const char *cpMode, const char *cpFileName)
Display the statistics in ASCII format.
A structure to describe the type and location of a caught exception.
Defines a pointer to an achar array plus its length. That is, a phrase as is often used by APG.
void vMemFree(void *vpCtx, const void *vpData)
Free memory previously allocated with vpMemAlloc().
void * vpApiOutputParser(void *vpCtx)
Generate a parser object directly from the specified SABNF grammar.
void * vpVecPushn(void *vpCtx, void *vpElement, aint uiCount)
Adds one or more elements to the end of the array.
void * vpMemCtor(exception *spException)
Construct a memory component.
void vTraceConfigGen(void *vpCtx, const char *cpFileName)
Generate a configuration file for the current parser.
void vUtilPrintVecStats(const vec_stats *spStats)
Display the vector object's statistics.
aint uiLength
The number of characters in the array.
void vParserDtor(void *vpCtx)
Clears the parser component's context and frees all heap memory associated with this parser.
void vMemStats(void *vpCtx, mem_stats *spStats)
Returns a copy of the Memory component's current statistics.
void vTraceConfig(void *vpCtx, const char *cpFileName)
Read a configuration file and set the trace configuration accordingly.
void vUtilApgInfo(void)
Display the current state of apg.h.
aint uiStartRule
Index of the start rule. Any rule in the SABNF grammar may be used as the start rule.
apg_phrase * spUtilStrToPhrase(void *vpMem, const char *cpStr)
Convert a null-terminated ASCII string to an apg_phrase.
void vUtilPrintException(exception *spEx)
Prints exception information from an exception structure.
void * vpVecPush(void *vpCtx, void *vpElement)
Adds one element to the end of the array.
void vUtilCurrentWorkingDirectory(void)
Display the current working directory.
void vVecClear(void *vpCtx)
Clears all used elements in a vector component.
void vUtilPrintParserState(parser_state *spState)
Display the parser state in human-readable format to stdout.
const achar * acpPhrase
Pointer to an array of type achar APG alphabet characters.
void vParserParse(void *vpCtx, parser_config *spConfig, parser_state *spState)
Parse an input string of alphabet characters.
APG Version 7.0 is licensed under the
2-Clause BSD License,
an Open Source Initiative Approved License.