Version 7.0
Copyright © 2021 Lowell D. Thomas
APG
… an ABNF Parser Generator
|
Go to the documentation of this file.
46 static const char* s_cpTrue =
"yes";
47 static const char* s_cpFalse =
"no";
48 static const char* s_cpFatal =
"error";
49 static const char* s_cpEmpty =
"empty";
50 static const char* s_cpUndef =
"undef";
51 static const void* s_vpMagicNumber = (
void*)
"attributes";
53 static int iCompName(
const void* vpL,
const void* vpR);
54 static int iCompType(
const void* vpL,
const void* vpR);
55 static const char * cpEmpty(
abool aTF);
56 static const char * cpBool(
abool aTF);
57 static const char * cpShouldBeTrue(
abool aTF);
58 static const char * cpShouldBeFalse(
abool aTF);
87 "attempting to compute attributes before semantics (opcodes) are complete");
91 spAtt = spAttrsCtor(spApi);
123 memset((
void*) spCtx, 0,
sizeof(
attrs_ctx));
124 spCtx->vpMem = spApi->
vpMem;
126 spCtx->spApi = spApi;
147 spCtx->spWorkingAttrs[ui].cpRuleName = spApi->
spRules[ui].
cpName;
148 spCtx->spWorkingAttrs[ui].uiRuleIndex = spApi->
spRules[ui].
uiIndex;
151 spCtx->spWorkingAttrs[ui].bpRefersTo = (
abool*)
vpMemAlloc(spCtx->vpMem,
153 memset((
void*) spCtx->spWorkingAttrs[ui].bpRefersTo, 0, (
sizeof(
abool) * spApi->
uiRuleCount));
156 spCtx->spWorkingAttrs[ui].bpIsReferencedBy = (
abool*)
vpMemAlloc(spCtx->vpMem,
158 memset((
void*) spCtx->spWorkingAttrs[ui].bpIsReferencedBy, 0, (
sizeof(
abool) * spApi->
uiRuleCount));
162 spCtx->spWorkingAttrs[ui].bpRefersToUdt = (
abool*)
vpMemAlloc(spCtx->vpMem,
164 memset((
void*) spCtx->spWorkingAttrs[ui].bpRefersToUdt, 0, (
sizeof(
abool) * spApi->
uiUdtCount));
172 spCtx->vpValidate = s_vpMagicNumber;
188 void* vpMem = spCtx->
vpMem;
224 api *spApi = (
api*) vpCtx;
227 "no attributes available - must first call bApiAttrs()");
229 FILE *spOut = stdout;
231 spOut = fopen(cpFileName,
"wb");
234 snprintf(caBuf, 126,
"cannot open file name %s for writing", cpFileName);
240 if (cpMode[0] ==
'a' || cpMode[0] ==
'A') {
242 }
else if (cpMode[0] ==
't' || cpMode[0] ==
'T') {
272 api *spApi = (
api*) vpCtx;
275 "no attributes available - must first call bApiAttrs()");
277 FILE *spOut = stdout;
279 spOut = fopen(cpFileName,
"wb");
282 snprintf(caBuf, 126,
"cannot open file name %s for writing", cpFileName);
287 fprintf(spOut,
"ATTRIBUTE ERRORS\n");
290 if (cpMode[0] ==
'a' || cpMode[0] ==
'A') {
292 }
else if (cpMode[0] ==
't' || cpMode[0] ==
'T') {
303 fprintf(spOut,
"<none>\n");
310 static void vPrintOneAttrByName(
api_attr* a, FILE* spStream){
311 fprintf(spStream,
"%7s |%7s |%7s |%7s |%7s |%7s | %s\n",
317 static void vPrintOneAttrByType(
api_attr* a, FILE* spStream){
319 fprintf(spStream,
"%7s |%7s |%7s |%7s |%7s |%7s |%7"PRIuMAX
" |%7s | %s\n",
325 fprintf(spStream,
"%7s |%7s |%7s |%7s |%7s |%7s | |%7s | %s\n",
340 for(ui = 0; ui < uiCount; ui++){
341 sAttrs[ui] = spAttrs[ui];
343 qsort((
void*)&sAttrs[0], uiCount,
sizeof(
api_attr), iCompName);
344 qsort((
void*)&sAttrs[0], uiCount,
sizeof(
api_attr), iCompType);
345 fprintf(spStream,
"ATTRIBUTES BY TYPE\n");
346 fprintf(spStream,
" left | nested | right | cyclic | empty | finite | group | type | name\n");
347 fprintf(spStream,
"--------|--------|--------|--------|--------|--------|--------|--------|--------\n");
348 for(ui = 0; ui < uiCount; ui++){
349 vPrintOneAttrByType(&sAttrs[ui], spStream);
351 fprintf(spStream,
"\n");
361 for(ui = 0; ui < uiCount; ui++){
362 sAttrs[ui] = spAttrs[ui];
364 qsort((
void*)&sAttrs[0], uiCount,
sizeof(
api_attr), iCompName);
366 fprintf(spStream,
"ATTRIBUTES BY NAME\n");
367 fprintf(spStream,
" left | nested | right | cyclic | empty | finite | name\n");
368 fprintf(spStream,
"--------|--------|--------|--------|--------|--------|--------\n");
369 for(ui = 0; ui < uiCount; ui++){
370 vPrintOneAttrByName(&sAttrs[ui], spStream);
372 fprintf(spStream,
"\n");
381 fprintf(spStream,
"ATTRIBUTES BY INDEX\n");
382 fprintf(spStream,
" left | nested | right | cyclic | empty | finite | name\n");
383 fprintf(spStream,
"--------|--------|--------|--------|--------|--------|--------\n");
384 for(ui = 0; ui < uiCount; ui++){
385 vPrintOneAttrByName(&spAttrs[ui], spStream);
387 fprintf(spStream,
"\n");
390 static int iCompName(
const void* vpL,
const void* vpR) {
398 aint uiLesser = uiLenL < uiLenR ? uiLenL : uiLenR;
401 if (l >= 65 && l <= 90) {
405 if (r >= 65 && r <= 90) {
417 if (uiLenL < uiLenR) {
420 if (uiLenL > uiLenR) {
425 static const char * cpBool(
abool aTF){
434 static const char * cpEmpty(
abool aTF){
443 static const char * cpShouldBeTrue(
abool aTF){
452 static const char * cpShouldBeFalse(
abool aTF){
461 static int iCompType(
const void* vpL,
const void* vpR) {
485 char* cpReturn =
"UNKNOWN";
abool * bpRefersToUdt
a list of all the UDTs that this rule refers to
aint uiUdtCount
The number of UDTs referenced in the SABNF grammar.
abool bApiValidate(void *vpCtx)
Validates an API context pointer.
aint uiIndex
index of this rule in the rule list
api_rule * spRules
Points to an array of rule structures.
void vRuleAttributes(attrs_ctx *spAtt)
Computes the attributes of each rule in the grammar.
abool * bpIsReferencedBy
a list of all the rules that refer to this rule
abool bFinite
APG_TRUE if the rule is finite.
The API will construct an attributes object. This is the attribute object's context.
Working attribute information about a each rule.
abool bEmpty
APG_TRUE if the rule can be empty.
void vVecDtor(void *vpCtx)
The vector component destructor.
Private header file for the APG API suite of functions.
void vExContext()
Handles bad context pointers.
const void * vpValidate
True if the context is valid.
void * vpAttrsCtx
context handle to the attributes object
aint uiMRGroup
the group number, if this is a member of a mutually-recursive group (there may be multiple groups)
abool bApiAttrs(void *vpCtx)
Computes the grammar's attributes.
abool bNested
APG_TRUE if the rule is nested recursive.
The recursive attributes of a single SABNF grammra rule.
const char * cpRuleName
the rule name for these attributes
#define ID_ATTR_N
rule is non-recursive - never refers to itself
#define XTHROW(ctx, msg)
Exception throw macro.
const char * cpType(aint uiId)
Convert an attribute type ID to an ASCII string.
aint uiRuleCount
The number of rules in the SABNF grammar and in the array.
uint_fast32_t aint
The APG parser's unsigned integer type.
exception * spMemException(void *vpCtx)
Get a pointer to this memory objects's exception handler.
void * vpMemAlloc(void *vpCtx, aint uiBytes)
Allocates memory.
void vAttrsByType(api_attr *spAttrs, aint uiCount, FILE *spStream)
Display the attributes sorted by attribute type.
void * vpVecCtor(void *vpMem, aint uiElementSize, aint uiInitialAlloc)
The vector object constructor.
api_attr * spErrorAttrs
An array of all rule attributes that have errors. (i.e. left recursive)
void vMemFree(void *vpCtx, const void *vpData)
Free memory previously allocated with vpMemAlloc().
api * spApi
Pointer to the parent API context.
void vRuleDependencies(attrs_ctx *spAtt)
Compute each rule's dependencies on the other rules, and possibly on itself if the rule is recursive.
void vApiAttrsErrorsToAscii(void *vpCtx, const char *cpMode, const char *cpFileName)
Display all rule attributes with errors.
void * vpMem
Pointer to the memory context inherited from the parent API.
uintmax_t luint
luint is used to cast integers suitable for the %"PRIuMAX" printf format.
aint uiRecursiveType
ID_ATTR_N, ID_ATTR_R, ID_ATTR_MR, ID_ATTR_NMR, or ID_ATTR_RMR.
void * vpVecGroupNumbers
A vector for the discovery of groups of mutually recursive rules.
Header file for the attributes functions.
abool bAttributesComputed
APG_TRUE if attributes have been computed (even is there are attribute errors), APG_FALSE otherwise.
#define ID_ATTR_MR
rule is one of a mutually-recursive group (each rule in the group refers to itself and all others)
abool bLeft
APG_TRUE if the rule is left recursive.
abool bRight
APG_TRUE if the rule is right recursive.
abool bCyclic
APG_TRUE if the rule is cyclic.
abool * bpRefersTo
a list of all the rules that this rule refers to
void vApiAttrsToAscii(void *vpCtx, const char *cpMode, const char *cpFileName)
Display all rule attributes.
abool bAttributesValid
APG_TRUE if there the rule attributes have been computed and have no fatal errors,...
api_attr_w * spWorkingAttrs
An array of private attribute structures.
uint8_t abool
abool is the APG bool type.
char * cpName
pointer to null-terminated string in the string table
void * vpMem
Pointer to the memory context used for all memory allocations and exceptions thrown.
#define ID_ATTR_R
rule is recursive - refers to itself, directly or indirectly, one or more times
api_attr_w * spAttrs
An array of private attribute structures used in their construction.
abool bSemanticsValid
APG_TRUE if the the input semantics are valid. That is, the opcodes for the parser have been generate...
void vAttrsByIndex(api_attr *spAttrs, aint uiCount, FILE *spStream)
Display the attributes sorted by rule index.
void vAttrsByName(api_attr *spAttrs, aint uiCount, FILE *spStream)
Display the attributes sorted by rule name.
void vAttrsDtor(void *vpCtx)
The API object destructor.
api_attr * spPublicAttrs
When attributes a complete, the public version strips some of the unneeded variables used only in con...
aint uiErrorCount
The number of rules that have attribute errors.
Public header file for the APG API suite of functions.
APG Version 7.0 is licensed under the
2-Clause BSD License,
an Open Source Initiative Approved License.