Version 7.0
Copyright © 2021 Lowell D. Thomas
APG
… an ABNF Parser Generator
|
Go to the documentation of this file.
40 #include "../api/api.h"
43 static const void* s_vpMagicNumber = (
void*)
"config";
44 static char* s_cpCommandLineError =
"COMMAND LINE ERROR: ";
87 static void vExtractFileOptions(
config_ctx* spCtx);
88 static void vExtractArgOptions(
config_ctx* spCtx,
char* cpParams);
90 static void vGetArgs(
config_ctx* spCtx,
int iArgCount,
char** cppArgs);
103 aint uiMedium = 1024;
105 char caCwd[PATH_MAX];
110 spCtx->vpMem = vpMem;
111 spCtx->spException = spEx;
114 spCtx->vpVecArgs =
vpVecCtor(vpMem, (
aint)
sizeof(
char), uiSmall);
117 if (getcwd(caCwd, PATH_MAX) == NULL) {
118 XTHROW(spEx,
"system error - can't get current working directory");
120 uiLen = (
aint)strlen(caCwd) + 1;
121 spCtx->vpVecCwd =
vpVecCtor(vpMem, (
aint)
sizeof(
char), (uiLen + 1));
124 spCtx->vpVecOutput =
vpVecCtor(vpMem, (
aint)
sizeof(
char), uiSmall);
127 spCtx->vpVecInput =
vpVecCtor(vpMem, (
aint)
sizeof(
char), uiMedium);
130 spCtx->vpVecPRules =
vpVecCtor(vpMem, (
aint)
sizeof(
char), uiMedium);
133 spCtx->vpVecInputAddrs =
vpVecCtor(vpMem, (
aint)
sizeof(
char*), uiSmall);
136 spCtx->vpVecPRulesAddrs =
vpVecCtor(vpMem, (
aint)
sizeof(
char*), uiSmall);
139 spCtx->vpVecGrammar =
vpVecCtor(vpMem, (
aint)
sizeof(
char), uiLarge);
142 spCtx->vpVecConfigOut =
vpVecCtor(vpMem, (
aint)
sizeof(
char), uiSmall);
145 spCtx->vpVecHtmlOut =
vpVecCtor(vpMem, (
aint)
sizeof(
char), uiSmall);
148 spCtx->vpVecRulesHtmlOut =
vpVecCtor(vpMem, (
aint)
sizeof(
char), uiSmall);
151 spCtx->vpVecConfigIn =
vpVecCtor(vpMem, (
aint)
sizeof(
char), uiSmall);
154 spCtx->vpVecLfOut =
vpVecCtor(vpMem, (
aint)
sizeof(
char), uiSmall);
157 spCtx->vpVecCrLfOut =
vpVecCtor(vpMem, (
aint)
sizeof(
char), uiSmall);
160 spCtx->vpValidate = s_vpMagicNumber;
161 return (
void*) spCtx;
178 void* vpMem = spCtx->
vpMem;
179 memset(vpCtx, 0,
sizeof(*spCtx));
196 vGetArgs(spCtx, iArgCount, cppArgs);
199 vExtractFileOptions(spCtx);
204 return spGetConfig(spCtx);
211 printf(
"usage: apg options\n");
212 printf(
"binary options:\n");
213 printf(
"-i filename : the file name of the input grammar (see note 5.)\n");
214 printf(
"-o filename : the file name of the generated C-language header and source files (see note 6.)\n");
216 printf(
"unary options:\n");
217 printf(
"--input=filename : the file name of the input grammar (see note 5.)\n");
218 printf(
"--output=filename : the file name of the generated C-language header and source files (see note 6.)\n");
219 printf(
"-c : generate a default configuration file named \"%s\"\n",
CONFIG_FILE);
220 printf(
"--config-file=fname : generate a default configuration file named \"fname\"\n");
221 printf(
"--p-rules=name[,name] : comma-delimited list of protected rule names (see note 9.)\n");
222 printf(
"--grammar-html=fname : display input grammar in HTML format on file \"fname\"\n");
223 printf(
"--rules-html=fname : display rule/UDT names and dependencies in HTML format on file \"fname\"\n");
224 printf(
"--lf=filename : converts all input line end to LF(\\n) on file \"filename\" (see note 8.)\n");
225 printf(
"--crlf=filename : converts all input line end to CRLF(\\r\\n) on file \"filename\" (see note 8.)\n");
226 printf(
"@ : read the options from the configuration file named \"%s\"\n",
CONFIG_FILE);
227 printf(
"@filename : read the options from the configuration file named \"filename\"\n");
229 printf(
"flags: true if present, otherwise false\n");
230 printf(
"? : display this help screen\n");
231 printf(
"-h : display this help screen\n");
232 printf(
"--help : display this help screen\n");
233 printf(
"-v : display version information\n");
234 printf(
"--version : display version information\n");
235 printf(
"--strict : only ABNF as strictly defined in RFC 5234 allowed\n");
236 printf(
"--no-pppt : do not produce Partially-Predictive Parsing Tables (PPPTs)\n");
238 printf(
"display flags\n");
239 printf(
"-dv : verbose - sets flags -dc, -dg, dr, -dp and -da\n");
240 printf(
"-dc : display the complete configuration found on the command line or in the command file\n");
241 printf(
"-dg : display an annotated version of the input grammar\n");
242 printf(
"-dr : display the grammar rule names, in the order they are found in the grammar\n");
243 printf(
"-dra : display the grammar rule names, in alphabetical order\n");
244 printf(
"-da : display the rule attributes\n");
245 printf(
"-dp : display the Partially-Predictive Parsing Table (PPPT) sizes\n");
246 printf(
"-do : display the opcodes in human-readable format (warning: may generate many lines of output)\n");
249 printf(
"1. All options and flags are case sensitive.\n");
250 printf(
"2. Binary options require one or more spaces between the flag and the name.\n");
251 printf(
"3. No spaces are allowed in unary options or flags (no space before or after \"=\").\n");
252 printf(
"4. If any or all of -h, -v or -c (or any of their alternatives) is present all other options are ignored.\n");
253 printf(
"5. File names may be absolute (/home/user/filname) or relative ([./ | ../]mydir/filename).\n");
254 printf(
" Directories in the path name must exist.\n");
255 printf(
"6. Any file name extension will be stripped and replaced with .h for the header file and .c for the source file.\n");
256 printf(
"7. Absent -h, -v or -c, if a configuration file is indicated (@ or @filename) all other options are ignored.\n");
257 printf(
"8. Both --lf and --crlf may be present. If either is present, all other options except -h, -v and -c are ignored.\n");
258 printf(
"9. Protected rules are protected from being hidden under predictive PPPT-mapped nodes.\n");
259 printf(
" Rule names are case insensitive. The argument may be a comma-delimited list with no spaces allowed.\n");
260 printf(
" Multiple instances of the --p-rules flag will accumulate rule names in the list.\n");
261 printf(
"10. No command line arguments generates this help screen.\n");
284 if(!vpCtx || (spCtx->
vpValidate != s_vpMagicNumber)){
287 FILE* spFile = stdout;
289 spFile = fopen(cpFileName,
"wb");
292 snprintf(caBuf, 128,
"can't open file \"%s\" for writing default configuration file", cpFileName);
296 fprintf(spFile,
"# APG CONFIGURATION FILE\n");
297 fprintf(spFile,
"#\n");
298 fprintf(spFile,
"# Comments begin with \"#\" and continue to end of line\n");
299 fprintf(spFile,
"# Blank lines are ignored\n");
300 fprintf(spFile,
"# Options must begin on first character of a line and must not contain spaces.\n");
301 fprintf(spFile,
"# File names must not contain spaces.\n");
302 fprintf(spFile,
"# Trailing white space after an option is stripped.\n");
303 fprintf(spFile,
"# APG command-line options, -c, --config-file=filename, @, and @filename are not allowed in configuration files.\n");
304 fprintf(spFile,
"# If present they will generate an error.\n");
305 fprintf(spFile,
"#\n");
306 fprintf(spFile,
"# THE INPUT GRAMMAR\n");
308 "# The --input option is used to specify the input grammar file.\n");
309 fprintf(spFile,
"# The file name may be absolute (/home/user/dir) or relative (../backone/dir)\n");
310 fprintf(spFile,
"# If multiple --input parameters are specified the named files \n");
311 fprintf(spFile,
"# will be concatenated into a single input grammar file in the order presented\n");
312 fprintf(spFile,
"#\n");
313 fprintf(spFile,
"#--input=grammar.bnf\n");
314 fprintf(spFile,
"#\n");
315 fprintf(spFile,
"# THE OUTPUT, GENERATED C-LANGUAGE FILES\n");
316 fprintf(spFile,
"# This option names names the output files, filename.h & filename.c.\n");
317 fprintf(spFile,
"# The extension, if any, will be stripped and \".c\" added for the source file and \".h\" added for the header file\n");
318 fprintf(spFile,
"# The file name may be absolute (/home/user/dir) or relative (../backone/dir)\n");
319 fprintf(spFile,
"# If no --output option exists, no output is generated.\n");
320 fprintf(spFile,
"#\n");
321 fprintf(spFile,
"#--output=filename\n");
322 fprintf(spFile,
"#\n");
323 fprintf(spFile,
"# ANNOTATED GRAMMAR IN HTML\n");
324 fprintf(spFile,
"# Output the annotated grammar in HTML format to filename\n");
325 fprintf(spFile,
"# (file name should have .html file extension)\n");
326 fprintf(spFile,
"#\n");
327 fprintf(spFile,
"#--grammar-html=filename\n");
328 fprintf(spFile,
"#\n");
329 fprintf(spFile,
"# RULE/UDT NAMES AND DEPENDENCIES IN HTML\n");
330 fprintf(spFile,
"# Output the rule/UDT names and dependencies in HTML format to filename\n");
331 fprintf(spFile,
"# (file name should have .html file extension)\n");
332 fprintf(spFile,
"#\n");
333 fprintf(spFile,
"#--rules-html=filename\n");
334 fprintf(spFile,
"#\n");
335 fprintf(spFile,
"# LINE ENDING CONVERSIONS\n");
336 fprintf(spFile,
"# Convert the grammar's line endings to LF(linefeed, \n, 0x0A) to filename\n");
337 fprintf(spFile,
"#\n");
338 fprintf(spFile,
"#--lf=filename\n");
339 fprintf(spFile,
"#\n");
340 fprintf(spFile,
"# Convert the grammar's line endings to CRLF(carriage return + linefeed, \r\n, 0x0D0A) to filename\n");
341 fprintf(spFile,
"#\n");
342 fprintf(spFile,
"#--crlf=filename\n");
343 fprintf(spFile,
"#\n");
344 fprintf(spFile,
"# THE HELP SCREEN\n");
345 fprintf(spFile,
"# If present this option will display a usage or help screen and quit.\n");
346 fprintf(spFile,
"#\n");
347 fprintf(spFile,
"#--help\n");
348 fprintf(spFile,
"#\n");
349 fprintf(spFile,
"# THE VERSION NUMBER AND COPYRIGHT\n");
350 fprintf(spFile,
"# If present this option will display the version number and copyright and quit.\n");
351 fprintf(spFile,
"#\n");
352 fprintf(spFile,
"#--version\n");
353 fprintf(spFile,
"#\n");
354 fprintf(spFile,
"# STRICT ABNF\n");
355 fprintf(spFile,
"# If the strict flag is set, the input grammar must conform strictly to ABNF as\n");
356 fprintf(spFile,
"# defined in RFCs 5234 & 7405.\n");
357 fprintf(spFile,
"#\n");
358 fprintf(spFile,
"#--strict\n");
359 fprintf(spFile,
"#\n");
360 fprintf(spFile,
"# IGNORE ATTRIBUTES\n");
361 fprintf(spFile,
"# If this flag is set, the input grammar attribute calculation will be skipped.\n");
362 fprintf(spFile,
"# The generator will proceed to output a parser whether there are attribute errors are not.\n");
363 fprintf(spFile,
"# Proceed at your own risk, or only if you know from previous runs that the attributes are OK.\n");
364 fprintf(spFile,
"# NOTE: rule/UDT dependencies will not be available if this option is chosen.\n");
365 fprintf(spFile,
"#\n");
366 fprintf(spFile,
"#--ignore-attributes\n");
367 fprintf(spFile,
"#\n");
368 fprintf(spFile,
"# IGNORE PPPT\n");
369 fprintf(spFile,
"# If this flag is set, the Partially-Predictive Parsing Tables (PPPTs) calculation will be skipped.\n");
370 fprintf(spFile,
"# If set, best to compile parsing applications with the macro APG_NO_PPPT defined.\n");
371 fprintf(spFile,
"#\n");
372 fprintf(spFile,
"#--no-pppt\n");
373 fprintf(spFile,
"#\n");
374 fprintf(spFile,
"# PROTECTED RULES\n");
375 fprintf(spFile,
"# This option allows for a list of rule names to be protected from being hidden under fully-predictive\n");
376 fprintf(spFile,
"# PPPT-mapped nodes in the parse tree. The argument may be a comma-delimited list.\n");
377 fprintf(spFile,
"# Multiple instances of the --p-rules option will accumulate rule names to the list.\n");
378 fprintf(spFile,
"# Rule names are case insensitive.\n");
379 fprintf(spFile,
"#\n");
380 fprintf(spFile,
"#--p-rules=rule[,rule[,rule]...]\n");
381 fprintf(spFile,
"#\n");
382 fprintf(spFile,
"# DISPLAY OPTIONS\n");
383 fprintf(spFile,
"# Display option all begin with \"d\"\n");
384 fprintf(spFile,
"#\n");
385 fprintf(spFile,
"# verbose display, turn on the flags, -dc, -dg, dr, and -da\n");
386 fprintf(spFile,
"#-dv\n");
387 fprintf(spFile,
"#\n");
388 fprintf(spFile,
"# Display the input grammar with line numbers and explicit control characters, \\t, \\n & \\r.\n");
389 fprintf(spFile,
"# as ASCII on the stream stdout.\n");
390 fprintf(spFile,
"#-dg\n");
391 fprintf(spFile,
"#\n");
392 fprintf(spFile,
"# Displays the full contents of the final configuration file.\n");
393 fprintf(spFile,
"#-dc\n");
394 fprintf(spFile,
"#\n");
395 fprintf(spFile,
"# Display the input grammar with line numbers and explicit control characters, \\t, \\n & \\r.\n");
396 fprintf(spFile,
"# as HTML on the file filename.\n");
397 fprintf(spFile,
"#--display-html=filename\n");
398 fprintf(spFile,
"#\n");
399 fprintf(spFile,
"# Display a list of all of the rules, in the order they are found in the grammar.\n");
400 fprintf(spFile,
"#-dr\n");
401 fprintf(spFile,
"#\n");
402 fprintf(spFile,
"# Display a list of all of the rules, in alphabetical order.\n");
403 fprintf(spFile,
"#-dra\n");
404 fprintf(spFile,
"#\n");
405 fprintf(spFile,
"# Display the rule attributes.\n");
406 fprintf(spFile,
"#-da\n");
407 fprintf(spFile,
"#\n");
408 fprintf(spFile,
"# Display the Partially-Predictive Parsing Table (PPPT) sizes.\n");
409 fprintf(spFile,
"#-dp\n");
410 fprintf(spFile,
"#\n");
411 fprintf(spFile,
"# Display the opcodes for each rule in human-readable form (warning: may generate lots of lines).\n");
412 fprintf(spFile,
"#-do\n");
413 fprintf(spFile,
"#\n");
414 if(spFile != stdout){
433 char* cpTrue =
"TRUE";
434 char* cpFalse =
"FALSE";
436 printf(
" THE APG CONFIGURATION:\n");
437 printf(
" command line args(%d):", iArgCount);
438 for(; i < iArgCount; i += 1){
439 printf(
" %s", cppArgs[i]);
442 printf(
" cwd: %s\n", spConfig->
cpCwd);
444 printf(
" input file: \"none\"\n");
446 printf(
" input files: %s\n", spConfig->
cppInput[0]);
448 printf(
" input files(s):\n");
450 printf(
" %d. %s\n", (i+1), spConfig->
cppInput[i]);
454 printf(
" protected rule names: \"none\"\n");
456 printf(
" protected rule names: %s\n", spConfig->
cppPRules[0]);
458 printf(
" protected rule names:\n");
459 for(i = 0; i < (int)(spConfig->
uiPRules); i += 1){
460 printf(
" %d. %s\n", (i+1), spConfig->
cppPRules[i]);
464 printf(
" output files(s): %s\n", spConfig->
cpOutput);
466 printf(
" output path name: \"none\"\n");
469 printf(
" LF line ends file: \"none\"\n");
471 printf(
" LF line ends file: %s\n", spConfig->
cpLfOut);
474 printf(
" CRLF line ends file: \"none\"\n");
476 printf(
" CRLF line ends file: %s\n", spConfig->
cpCrLfOut);
479 printf(
" grammar to html file: \"none\"\n");
481 printf(
" grammar to html file: %s\n", spConfig->
cpGrammarHtml);
484 printf(
"rules/UDT to html file: \"none\"\n");
486 printf(
"rules/UDT to html file: %s\n", spConfig->
cpRulesHtml);
491 printf(
" create default file: no\n");
494 printf(
"use configuration file: %s\n", spConfig->
cpUseConfig);
496 printf(
"use configuration file: no\n");
498 printf(
" --help: %s\n", (spConfig->
bHelp ? cpTrue : cpFalse));
499 printf(
" --version: %s\n", (spConfig->
bVersion ? cpTrue : cpFalse));
500 printf(
" --strict: %s\n", (spConfig->
bStrict ? cpTrue : cpFalse));
501 printf(
" --no-pppt: %s\n", (spConfig->
bNoPppt? cpTrue : cpFalse));
502 printf(
" -dv: %s\n", (spConfig->
bDv ? cpTrue : cpFalse));
503 printf(
" -dc: %s\n", (spConfig->
bDc ? cpTrue : cpFalse));
504 printf(
" -dg: %s\n", (spConfig->
bDg ? cpTrue : cpFalse));
505 printf(
" -da: %s\n", (spConfig->
bDa ? cpTrue : cpFalse));
506 printf(
" -dr: %s\n", (spConfig->
bDr ? cpTrue : cpFalse));
507 printf(
" -dp: %s\n", (spConfig->
bDp ? cpTrue : cpFalse));
508 printf(
" -dra: %s\n", (spConfig->
bDra ? cpTrue : cpFalse));
509 printf(
" -do: %s\n", (spConfig->
bDo ? cpTrue : cpFalse));
512 static void vGetArgs(
config_ctx* spCtx,
int iArgCount,
char** cppArgs){
516 for (; iOption < iArgCount; iOption += 1) {
517 uiStrLen = (
aint) (strlen(cppArgs[iOption]) + 1);
523 static char* s_cpPos = NULL;
524 static char* cpGetFirstName(
char* cpBegin,
aint* uipLength){
539 *uipLength = uiOffset;
542 static char* cpGetNextName(
aint* uipLength){
543 char* cpReturn = s_cpPos;
559 *uipLength = uiOffset;
562 static void vExtractArgOptions(
config_ctx* spCtx,
char* cpParams) {
569 uiStrLen = (
aint) (strlen(cpParams) + 1);
570 cpParams += uiStrLen;
571 if (*cpParams == 0) {
576 while (*cpParams != 0) {
577 uiStrLen = (
aint) (strlen(cpParams) + 1);
578 if (strcmp(cpParams,
"-i") == 0) {
579 uiStrLen = (
aint) (strlen(cpParams) + 1);
580 cpParams += uiStrLen;
581 if (*cpParams == 0) {
582 XTHROW(spCtx->
spException,
"options error: -i has no following input file name");
584 uiStrLen = (
aint) (strlen(cpParams) + 1);
586 cpParams += uiStrLen;
588 }
else if (strncmp(cpParams,
"--input=", 8) == 0) {
589 uiStrLen = (
aint) (strlen(&cpParams[8]) + 1);
592 uiStrLen = (
aint) (strlen(cpParams) + 1);
593 cpParams += uiStrLen;
594 }
else if (strncmp(cpParams,
"--p-rules=", 10) == 0) {
596 char* cpName = cpGetFirstName(&cpParams[10], &uiStrLen);
600 while((cpName = cpGetNextName(&uiStrLen))){
605 uiStrLen = (
aint) (strlen(cpParams) + 1);
606 cpParams += uiStrLen;
607 }
else if (strcmp(cpParams,
"-o") == 0) {
608 uiStrLen = (
aint) (strlen(cpParams) + 1);
609 cpParams += uiStrLen;
610 if (*cpParams == 0) {
611 XTHROW(spCtx->
spException,
"options error: -o has no following output file name");
613 uiStrLen = (
aint) (strlen(cpParams) + 1);
615 cpParams += uiStrLen;
616 }
else if (strncmp(cpParams,
"--output=", 9) == 0) {
617 uiStrLen = (
aint) (strlen(&cpParams[9]) + 1);
619 uiStrLen = (
aint) (strlen(cpParams) + 1);
620 cpParams += uiStrLen;
621 }
else if (strncmp(cpParams,
"--grammar-html=", 15) == 0) {
622 uiStrLen = (
aint) (strlen(&cpParams[15]) + 1);
624 uiStrLen = (
aint) (strlen(cpParams) + 1);
625 cpParams += uiStrLen;
626 }
else if (strncmp(cpParams,
"--rules-html=", 13) == 0) {
627 uiStrLen = (
aint) (strlen(&cpParams[13]) + 1);
629 uiStrLen = (
aint) (strlen(cpParams) + 1);
630 cpParams += uiStrLen;
631 }
else if (strncmp(cpParams,
"--lf=", 5) == 0) {
632 uiStrLen = (
aint) (strlen(&cpParams[5]) + 1);
634 uiStrLen = (
aint) (strlen(cpParams) + 1);
635 cpParams += uiStrLen;
636 }
else if (strncmp(cpParams,
"--crlf=", 7) == 0) {
637 uiStrLen = (
aint) (strlen(&cpParams[7]) + 1);
639 uiStrLen = (
aint) (strlen(cpParams) + 1);
640 cpParams += uiStrLen;
641 }
else if (strcmp(cpParams,
"-c") == 0) {
644 uiStrLen = (
aint) (strlen(cpParams) + 1);
645 cpParams += uiStrLen;
646 }
else if (strncmp(cpParams,
"--config-file=", 13) == 0) {
647 if (cpParams[13] ==
'='){
648 uiStrLen = (
aint) (strlen(&cpParams[14]) + 1);
653 uiStrLen = (
aint) (strlen(cpParams) + 1);
654 cpParams += uiStrLen;
655 }
else if (cpParams[0] ==
'@') {
656 if (cpParams[1] == 0) {
660 uiStrLen = (
aint) (strlen(&cpParams[1]) + 1);
663 uiStrLen = (
aint) (strlen(cpParams) + 1);
664 cpParams += uiStrLen;
665 }
else if (strcmp(cpParams,
"-v") == 0) {
667 uiStrLen = (
aint) (strlen(cpParams) + 1);
668 cpParams += uiStrLen;
669 }
else if (strcmp(cpParams,
"--version") == 0) {
671 uiStrLen = (
aint) (strlen(cpParams) + 1);
672 cpParams += uiStrLen;
673 }
else if (strcmp(cpParams,
"?") == 0) {
675 uiStrLen = (
aint) (strlen(cpParams) + 1);
676 cpParams += uiStrLen;
677 }
else if (strcmp(cpParams,
"-h") == 0) {
679 uiStrLen = (
aint) (strlen(cpParams) + 1);
680 cpParams += uiStrLen;
681 }
else if (strcmp(cpParams,
"--help") == 0) {
683 uiStrLen = (
aint) (strlen(cpParams) + 1);
684 cpParams += uiStrLen;
685 }
else if (strcmp(cpParams,
"-s") == 0) {
687 uiStrLen = (
aint) (strlen(cpParams) + 1);
688 cpParams += uiStrLen;
689 }
else if (strcmp(cpParams,
"--strict") == 0) {
691 uiStrLen = (
aint) (strlen(cpParams) + 1);
692 cpParams += uiStrLen;
693 }
else if (strcmp(cpParams,
"--no-pppt") == 0) {
695 uiStrLen = (
aint) (strlen(cpParams) + 1);
696 cpParams += uiStrLen;
697 }
else if (strcmp(cpParams,
"-dra") == 0) {
699 uiStrLen = (
aint) (strlen(cpParams) + 1);
700 cpParams += uiStrLen;
701 }
else if (strcmp(cpParams,
"-dr") == 0) {
703 uiStrLen = (
aint) (strlen(cpParams) + 1);
704 cpParams += uiStrLen;
705 }
else if (strcmp(cpParams,
"-dg") == 0) {
707 uiStrLen = (
aint) (strlen(cpParams) + 1);
708 cpParams += uiStrLen;
709 }
else if (strcmp(cpParams,
"-da") == 0) {
711 uiStrLen = (
aint) (strlen(cpParams) + 1);
712 cpParams += uiStrLen;
713 }
else if (strcmp(cpParams,
"-dc") == 0) {
715 uiStrLen = (
aint) (strlen(cpParams) + 1);
716 cpParams += uiStrLen;
717 }
else if (strcmp(cpParams,
"-do") == 0) {
719 uiStrLen = (
aint) (strlen(cpParams) + 1);
720 cpParams += uiStrLen;
721 }
else if (strcmp(cpParams,
"-dp") == 0) {
723 uiStrLen = (
aint) (strlen(cpParams) + 1);
724 cpParams += uiStrLen;
725 }
else if (strcmp(cpParams,
"-dv") == 0) {
727 uiStrLen = (
aint) (strlen(cpParams) + 1);
728 cpParams += uiStrLen;
730 printf(
"unrecognized option[%d]: %s\n", iOption, cpParams);
732 uiStrLen = (
aint) (strlen(cpParams) + 1);
733 cpParams += uiStrLen;
742 if (getcwd(cwd,
sizeof(cwd)) != NULL) {
757 static void vExtractFileOptions(
config_ctx* spCtx){
766 aint uiState = uiNewLine;
768 char* cpHelp =
"--help";
772 char* cpFirst =
"args from configuration file";
784 snprintf(caBuf, 128,
"%s\nno input configuration file name", s_cpCommandLineError);
789 snprintf(caBuf, 128,
"%s\nunable to open configuration file: %s", s_cpCommandLineError, cpIn);
792 uiSize = (
aint) (strlen(cpFirst) + 1);
796 if(uiState == uiOption){
797 if(c == cLB || c == cSP || c == cTAB || c == cLF || c == cCR){
799 if(c == cLF || c == cCR){
807 }
else if(uiState == uiComment){
808 if(c == cLF || c == cCR){
811 }
else if( uiState == uiNewLine){
815 }
else if(c == cLF || c == cCR){
820 }
else if(uiState == uiLineEnd){
821 if(!(c == cLF || c == cCR)){
838 cpOption += (
aint)(strlen(cpOption) + 1);
840 while(cpOption[0] != 0){
842 if(strncmp(cpOption,
"--output", 8) == 0){
845 if(strncmp(cpOption,
"--input", 7) == 0){
848 if(strncmp(cpOption,
"--p-rules", 7) == 0){
851 if(strncmp(cpOption,
"--grammar-html", 14) == 0){
854 if(strncmp(cpOption,
"--rules-html", 12) == 0){
857 if(strncmp(cpOption,
"--lf", 4) == 0){
860 if(strncmp(cpOption,
"--crlf", 4) == 0){
863 if(strcmp(cpOption,
"--help") == 0){
866 if(strcmp(cpOption,
"--version") == 0){
869 if(strcmp(cpOption,
"--strict") == 0){
872 if(strcmp(cpOption,
"--ignore-attributes") == 0){
875 if(strcmp(cpOption,
"--no-pppt") == 0){
878 if(strcmp(cpOption,
"-dc") == 0){
881 if(strcmp(cpOption,
"-dv") == 0){
884 if(strcmp(cpOption,
"-do") == 0){
887 if(strcmp(cpOption,
"-dp") == 0){
890 if(strcmp(cpOption,
"-dr") == 0){
893 if(strcmp(cpOption,
"-dra") == 0){
896 if(strcmp(cpOption,
"-dg") == 0){
899 if(strcmp(cpOption,
"-da") == 0){
903 "%s\noption unrecognized or not allowed in configuration file: %s", s_cpCommandLineError, cpOption);
906 cpOption += (
aint)(strlen(cpOption) + 1);
918 vExtractArgOptions(spCtx, (
char*)
vpVecFirst(vpVec));
929 spConfig->
bDc = spCtx->
bDc;
930 spConfig->
bDv = spCtx->
bDv;
931 spConfig->
bDo = spCtx->
bDo;
932 spConfig->
bDp = spCtx->
bDp;
933 spConfig->
bDr = spCtx->
bDr;
934 spConfig->
bDa = spCtx->
bDa;
936 spConfig->
bDg = spCtx->
bDg;
951 cpName += strlen(cpName) + 1;
955 for(ui = 0; ui < spCtx->
uiPRules; ui += 1){
957 cpName += strlen(cpName) + 1;
char * cpRulesHtml
if non-null, the file name for the HTML version of the rule/UDT names and dependencies
void vConfigDefault(void *vpCtx, char *cpFileName)
Prints a default configuration file.
const void * vpValidate
a "magic number" to validate the context
void * vpVecPRulesAddrs
addresses of the rule names
void * vpVecInputAddrs
addresses of the input file name, can be used for "char** argv" type access to file names
char ** cppInput
array of uiInputFiles input file names
aint uiPRules
the number of protected rule names found
void vMemDtor(void *vpCtx)
Destroys a Memory component. Frees all memory allocated.
abool bDo
display the opcodes for each rule in human-readable form
abool bDv
verobose - sets options -dc, -dg, -dr, and -da
char * cpDefaultConfig
if non-n=NULL, generate a default configuration file using this file name
aint uiInputFiles
the number of input files found
abool bNoPppt
if set, skip the PPPT calculation
void * vpVecArgs
command line or configuration file options as a null-terminated string of strings
This data controls the flow of the main program of the APG parser generator.
abool bHelp
the help flag, if set the help screen is printed and processing stops
void * vpMem
memory component context handle
void * vpVecPRules
one or more protected rule names as a null-terminated string of strings
abool bDp
display the PPPT size
void * vpVecCwd
the current working directory, the directory "main" is being run in
void vExContext()
Handles bad context pointers.
abool bNoPppt
if set, Partially-Predictive Parsing Tables (PPPTs) will not be produced
void vConfigHelp(void)
Prints the help screen when requested or if there is a command line options error.
char * cpCwd
the current working directory
const config * spConfigOptions(void *vpCtx, int iArgCount, char **cppArgs)
Reads the command line arguments and parses them into a configuration structure.
FILE * spConfigFile
Open file handle for reading a configuration file. NULL if not open.
void * vpVecCrLfOut
if non-empty string, put CRLF translated line ends on file name
void * vpVecHtmlOut
if non-empty string, put HTML version of annotated input grammar on file name
abool bStrict
if set, the grammar is treated as strict ABNF
#define XTHROW(ctx, msg)
Exception throw macro.
abool bDc
display the complete configuration as found on command line or configuration file
abool bHelp
the help flag, if set the help screen is printed and processing stops
void * vpVecGrammar
the input grammar, a concatenation of all input files
abool bStrict
if set, the grammar is treated as strict ABNF
uint_fast32_t aint
The APG parser's unsigned integer type.
abool bDo
display human-readable opcodes
void * vpVecInput
one or more input file names as a null-terminated string of strings
void * vpVecOutput
if non-empty string, the output file name, the name.h and name.c files will be the generated files
char * cpLfOut
if non-null, the file name for the converted LF line ends file
void * vpMemAlloc(void *vpCtx, aint uiBytes)
Allocates memory.
void vConfigVersion(void)
Display the version number.
char * cpGrammarHtml
if non-null, the file name for the HTML version of the annotated input grammar
void * vpVecCtor(void *vpMem, aint uiElementSize, aint uiInitialAlloc)
The vector object constructor.
abool bDra
display the grammar rule/UDT names alphabetically
config sConfig
the form of the options presented to the user
A structure to describe the type and location of a caught exception.
abool bDg
display an annotated version of the input grammar
abool bDra
display the grammar rule/UDT names alphabetically
char * cpCrLfOut
if non-null, the file name for the converted CRLF line ends file
abool bDa
display the rule attributes
void * vpVecConfigOut
if non-empty string, generate a default configuration file here
abool bExValidate(exception *spException)
Test an exception structure for validity.
The configuration component context.
abool bDc
display the complete configuration as found on command line or configuration file
void * vpVecFirst(void *vpCtx)
Get the first element one the vector. The vector is not altered.
char ** cppPRules
array of protected rule names
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.
abool bDg
display an annotated version of the input grammar
char * cpUseConfig
if non-n=NULL, use this configuration file instead of command line arguments
aint uiPRules
the number of protected rule names found
void vConfigDisplay(const config *spConfig, int iArgCount, char **cppArgs)
Displays the full configuration as determined from the command line or command file arguments.
abool bDr
display grammar rule/UDT names in the order they occur in the grammar
aint uiInputFiles
the number of input files found
abool bVersion
the version flag, if set the version number is printed and processing stops
abool bDp
display PPPT sizes
uint8_t abool
abool is the APG bool type.
The configuration object header file.
abool bDv
verbose display of information during processing - sets uiDg, uiDa, uiDr and uiDc
void vConfigDtor(void *vpCtx)
The configuration destructor.
abool bDr
display grammar rule/UDT names in the order they occur in the grammar
void * vpVecRulesHtmlOut
if non-empty string, put HTML version of rule/UDT dependencies on file name
char * cpOutput
the path name for the generated C source & header files
abool bDa
display grammar attributes
#define CONFIG_FILE
The default file name for generated configuration files.
abool bVersion
the version flag, if set the version number is printed and processing stops
void * vpVecPush(void *vpCtx, void *vpElement)
Adds one element to the end of the array.
void vVecClear(void *vpCtx)
Clears all used elements in a vector component.
void * vpVecLfOut
if non-empty string, put LF translated line ends on file name
void * vpConfigCtor(exception *spEx)
Constructs a configuration object to hold all data relating to this instance of the configuration.
void * vpVecConfigIn
if non-empty string, read the configuration from this file name
APG Version 7.0 is licensed under the
2-Clause BSD License,
an Open Source Initiative Approved License.