Version 7.0
Copyright © 2021 Lowell D. Thomas
APG
… an ABNF Parser Generator
|
Go to the documentation of this file.
39 #include "../library/parserp.h"
40 #include "../library/tracep.h"
43 #define BUF_SIZE (PATH_MAX + 256)
159 static const void* s_vpMagicNumber = (
const void*)
"apgex";
160 static const char* s_cpNoPattern =
"No pattern or properties defined yet. This function call must be preceded by\n"
161 "vApgexPattern(), vApgexPatternFile() or vApgexPatternParser()";
162 static char s_cZero = 0;
163 static char* s_cpExternalPattern =
"<external>";
164 static const char* s_cpEmptySource =
"source cannot be NULL or empty";
165 static char* s_cpEndian =
"???";
168 static inline void vMakeRelPhrase(
apgex* spExp,
aint uiSourceOffset,
aint uiLen,
phrase_r* spPhrase);
169 static inline char* cpBool(
abool bVal);
170 static void vClearForParse(
apgex* spExp);
171 static void vClearForPattern(
apgex* spExp);
184 static rule_r* spFindRule(
apgex* spExp,
const char* cpName);
185 static udt_r* spFindUdt(
apgex* spExp,
const char* cpName);
191 static void vDecodeFlags(
apgex* spExp,
const char* cpFlags);
192 static void vConstructParser(
apgex* spExp);
193 static void vInitRules(
apgex* spExp);
194 static void vInitCallbacks(
apgex* spExp);
195 static void vResetCallbacks(
apgex* spExp);
196 static abool bIsNameChar(
char cChar);
206 #define TRACE_APGEX_HEADER(t) vTraceApgexHeader((t))
207 #define TRACE_APGEX_FOOTER(t) vTraceApgexFooter((t))
208 #define TRACE_APGEX_SEPARATOR(x) vTraceApgexSeparator((x)->vpTrace, (x)->uiLastIndex)
209 #define TRACE_APGEX_CHECK(x)
210 #define TRACE_APGEX_OUTPUT(x) vTraceApgexOutput((x))
211 static void vTraceApgexOutput(
apgex *spExp) {
221 #define TRACE_APGEX_HEADER(t)
222 #define TRACE_APGEX_FOOTER(t)
223 #define TRACE_APGEX_SEPARATOR(x)
224 #define TRACE_APGEX_CHECK(x) vTraceApgexCheck((x))
225 #define TRACE_APGEX_OUTPUT(x)
226 static void vTraceApgexCheck(
apgex* spExp){
228 XTHROW(spExp->
spException,
"to use the 't' or 'th' flag the application must be compiled with APG_TRACE defined");
246 memset((
void*) spExp, 0,
sizeof(
apgex));
247 spExp->vpMem = vpMem;
248 spExp->spException = spEx;
249 spExp->vpApi =
vpApiCtor(spExp->spException);
250 spExp->vpVecStrings =
vpVecCtor(vpMem,
sizeof(
char), 1024);
253 spExp->vpVecPattern =
vpVecCtor(vpMem,
sizeof(
char), 4096);
254 spExp->vpVecFlags =
vpVecCtor(vpMem,
sizeof(
char), 32);
266 spExp->vpValidate = s_vpMagicNumber;
268 return (
void*) spExp;
283 XTHROW(spEx,
"apgex must be compiled with the macro APG_BKR defined.");
301 void* vpMem = spExp->
vpMem;
312 memset(vpCtx, 0,
sizeof(
apgex));
378 void vApgexPattern(
void* vpCtx,
const char* cpPattern,
const char* cpFlags){
380 if(!vpCtx || (spExp->
vpValidate != s_vpMagicNumber)){
383 vClearForPattern(spExp);
384 vDecodeFlags(spExp, cpFlags);
390 vConstructParser(spExp);
406 if(!vpCtx || (spExp->
vpValidate != s_vpMagicNumber)){
409 if(!cpFileName || (strlen(cpFileName) == 0)){
412 vClearForPattern(spExp);
413 vDecodeFlags(spExp, cpFlags);
417 aint uiPatternSize = uiSize;
421 ucpPattern[uiPatternSize] = 0;
424 vConstructParser(spExp);
446 if(!vpCtx || (spExp->
vpValidate != s_vpMagicNumber)){
453 XTHROW(spExp->
spException,
"vpParser is not a pointer to a valid parser object context");
455 vClearForPattern(spExp);
456 vDecodeFlags(spExp, cpFlags);
481 if(!vpCtx || (spExp->
vpValidate != s_vpMagicNumber)){
491 vClearForParse(spExp);
494 vInitCallbacks(spExp);
495 vExecResult(spExp, &sResult);
496 vResetCallbacks(spExp);
530 if(!vpCtx || (spExp->
vpValidate != s_vpMagicNumber)){
538 vClearForParse(spExp);
539 vInitCallbacks(spExp);
547 vExecResult(spExp, &sResult);
549 vReplacement(spExp, &sResult);
550 vReplace(spExp, &sResult);
553 vExecResult(spExp, &sResult);
555 vReplacement(spExp, &sResult);
556 vReplace(spExp, &sResult);
557 vExecResult(spExp, &sResult);
563 vResetCallbacks(spExp);
583 if(!vpCtx || (spExp->
vpValidate != s_vpMagicNumber)){
592 vClearForParse(spExp);
593 vInitCallbacks(spExp);
600 vExecResult(spExp, &sResult);
602 vReplaceFunc(spExp, &sResult, pfnFunc, vpUser);
603 vReplace(spExp, &sResult);
606 vExecResult(spExp, &sResult);
608 vReplaceFunc(spExp, &sResult, pfnFunc, vpUser);
609 vReplace(spExp, &sResult);
610 vExecResult(spExp, &sResult);
616 vResetCallbacks(spExp);
650 if(!vpCtx || (spExp->
vpValidate != s_vpMagicNumber)){
669 vClearForParse(spExp);
709 vMatchGlobal(spExp, &sConfig, &sResult);
739 vResetCallbacks(spExp);
754 if(!vpCtx || (spExp->
vpValidate != s_vpMagicNumber)){
760 vClearForParse(spExp);
762 return bExecTest(spExp);
788 if(!vpCtx || (spExp->
vpValidate != s_vpMagicNumber)){
794 if(!cpNames && !cpNames[0]){
797 aint uiStrLen = (
aint)strlen(cpNames) + 1;
807 for(; ui < uiStrLen; ui++){
809 if(bIsNameChar(cChar)){
811 cpNameBuf[uiNameLen++] = cChar;
815 cpNameBuf[uiNameLen] = (char)0;
817 if(
iStriCmp(cpNameBuf,
"--all") == 0){
822 spRule = spFindRule(spExp, cpNameBuf);
826 spUdt = spFindUdt(spExp, cpNameBuf);
830 snprintf(caBuf,
BUF_SIZE,
"\"%s\" is not a valid rule or UDT name", cpNameBuf);
876 if(!vpCtx || (spExp->
vpValidate != s_vpMagicNumber)){
890 if(!vpCtx || (spExp->
vpValidate != s_vpMagicNumber)){
959 if(!vpCtx || (spExp->
vpValidate != s_vpMagicNumber)){
986 if(!vpCtx || (spExp->
vpValidate != s_vpMagicNumber)){
1011 if(!vpCtx || (spExp->
vpValidate != s_vpMagicNumber)){
1030 if(!vpCtx || (spExp->
vpValidate != s_vpMagicNumber)){
1038 spExp->
spDisplay = fopen(cpFileName,
"wb");
1041 snprintf(caBuf,
BUF_SIZE,
"can't open file %s for writing", cpFileName);
1047 fprintf(spOut,
"PROPERTIES:\n");
1048 fprintf(spOut,
"Pattern:\n");
1050 fprintf(spOut,
"\n");
1052 fprintf(spOut,
" default: %s\n", cpBool(spProperties->
bDefaultMode));
1053 fprintf(spOut,
" global: %s\n", cpBool(spProperties->
bGlobalMode));
1054 fprintf(spOut,
" sticky: %s\n", cpBool(spProperties->
bStickyMode));
1055 fprintf(spOut,
" pppt: %s\n", cpBool(spProperties->
bPpptMode));
1058 fprintf(spOut,
" trace: yes(html)\n");
1060 fprintf(spOut,
" trace: yes(ascii)\n");
1063 fprintf(spOut,
" trace: no\n");
1066 fprintf(spOut,
"Original Source: ");
1068 vPrintPhrase(spExp, &sOutPhrase, spOut);
1069 fprintf(spOut,
"Replaced Source: ");
1071 vPrintPhrase(spExp, &sOutPhrase, spOut);
1073 fprintf(spOut,
"Original Source: ");
1075 vPrintPhrase(spExp, &sOutPhrase, spOut);
1078 fprintf(spOut,
" Last Match: ");
1079 vPrintPhrase(spExp, &spProperties->
sLastMatch, spOut);
1080 fprintf(spOut,
" Left Context: ");
1081 vPrintPhrase(spExp, &spProperties->
sLeftContext, spOut);
1082 fprintf(spOut,
" Right Context: ");
1084 if(spOut && (spOut != stdout)){
1100 if(!vpCtx || (spExp->
vpValidate != s_vpMagicNumber)){
1108 spExp->
spDisplay = fopen(cpFileName,
"wb");
1111 snprintf(caBuf,
BUF_SIZE,
"can't open file %s for writing", cpFileName);
1115 vPrintPhrase(spExp, spPhrase, spExp->
spDisplay);
1131 if(!vpCtx || (spExp->
vpValidate != s_vpMagicNumber)){
1139 spExp->
spDisplay = fopen(cpFileName,
"wb");
1142 snprintf(caBuf,
BUF_SIZE,
"can't open file %s for writing", cpFileName);
1147 fprintf(spOut,
"RESULT:\n");
1148 fprintf(spOut,
"result : ");
1150 fprintf(spOut,
"no match\n");
1152 vPrintPhrase(spExp, spResult->
spResult, spOut);
1153 fprintf(spOut,
"left context : ");
1155 fprintf(spOut,
"right context: ");
1157 fprintf(spOut,
"uiLastIndex : %"PRIuMAX
"\n", (
luint)spResult->
uiLastIndex);
1158 fprintf(spOut,
"node hits : %"PRIuMAX
"\n", (
luint)spResult->
uiNodeHits);
1162 fprintf(spOut,
"\nRULES:\n");
1165 fprintf(spOut,
"\n");
1174 if(spOut && (spOut != stdout)){
1182 if(!vpCtx || (spExp->
vpValidate != s_vpMagicNumber)){
1187 spExp->
spDisplay = fopen(cpFileName,
"wb");
1190 snprintf(caBuf,
BUF_SIZE,
"can't open file %s for writing", cpFileName);
1201 fprintf(spOut,
"%s\n", cpMsg);
1205 if(spOut && (spOut != stdout)){
1224 if(!vpCtx || (spExp->
vpValidate != s_vpMagicNumber)){
1230 udt_r* spUdt = spFindUdt(spExp, cpName);
1233 snprintf(caBuf,
BUF_SIZE,
"pattern has no UDT named %s", cpName);
1249 static inline void vMakeRelPhrase(
apgex* spExp,
aint uiSourceOffset,
aint uiLen,
phrase_r* spPhrase){
1254 static inline char* cpBool(
abool bVal){
1263 udt_r* spUdt = NULL;
1271 memset(spPhrase, 0,
sizeof(*spPhrase));
1273 for(ui = 0; ui < uiNameLen; ui++){
1274 char cChar = (char)acpName[ui];
1279 spRule = spFindRule(spExp, (
const char*)cpNameStr);
1288 spUdt = spFindUdt(spExp, (
const char*)cpNameStr);
1297 snprintf(caBuf,
BUF_SIZE,
"replacement error: &<%s> not a valid rule or UDT name", (
char*)cpNameStr);
1306 apg_phrase sPhrase = pfnFunc(spResult, &sProps, vpUser);
1319 aint ui, uii, uj, uiNameLen;
1328 while(ui < uiRawLen){
1330 if(acpRaw[ui] ==
DOLLAR){
1331 if(uii >= uiRawLen){
1332 XTHROW(spExp->
spException,
"replacement error: $ found at end of string - must be $`, $&, $', $$ or $<rulename>");
1334 if(acpRaw[uii] ==
DOLLAR){
1340 if(acpRaw[uii] ==
UNDER){
1348 if(acpRaw[uii] ==
AMP){
1356 if(acpRaw[uii] ==
ACCENT){
1364 if(acpRaw[uii] ==
APOS){
1372 if(acpRaw[uii] ==
LANGLE){
1373 for(uj = uii + 1; uj < uiRawLen; uj++){
1374 if(acpRaw[uj] ==
RANGLE){
1375 uiNameLen = uj - uii -1;
1380 vNamePhrase(spExp, &acpRaw[uii + 1], uiNameLen, &sPhrase);
1385 ui += uiNameLen + 3;
1390 XTHROW(spExp->
spException,
"replacement error: found &< but closing angle bracket, >, not found");
1394 snprintf(caBuf,
BUF_SIZE,
"replacement error: $ followed by %"PRIuMAX
" - must be $`, $&, $', $$ or $<rulename>", (
luint)acpRaw[uii]);
1408 achar* acpRight = NULL;
1456 vMatchDefault(spExp, &sConfig, spResult);
1459 vMatchGlobal(spExp, &sConfig, spResult);
1464 vMatchSticky(spExp, &sConfig, spResult);
1506 bReturn = bTestDefault(spExp, &sConfig);
1509 bReturn = bTestGlobal(spExp, &sConfig);
1514 bReturn = bTestSticky(spExp, &sConfig);
1529 static void vDecodeFlags(
apgex* spExp,
const char* cpFlags){
1532 aint uiFlagsLen = 0;
1534 uiFlagsLen = (
aint)strlen(cpFlags);
1539 for(; ui < uiFlagsLen; ui++){
1540 switch(cpFlags[ui]){
1562 XTHROW(spExp->
spException,
"%'h' flag (for HTML trace output) must follow 't' flag");
1569 snprintf(caBuf,
BUF_SIZE,
"%c unrecognized flag character, must be one or more of \"gyta\"", cpFlags[ui]);
1577 if((cpFlags == NULL) || (uiFlagsLen == 0)){
1583 static void vConstructParser(
apgex* spExp){
1585 aint uiPatternLen = (
aint)strlen(cpPattern);
1586 if(!cpPattern || (uiPatternLen <= 1)){
1587 XTHROW(spExp->
spException,
"attmpting to construct the parser but the pattern is not yet defined");
1603 static void vInitRules(
apgex* spExp){
1605 XTHROW(spExp->
spException,
"attempting to initialize rules and UDTS but parser not defined");
1611 parser* spParser = (parser*)spExp->
vpParser;
1634 static void vInitCallbacks(
apgex* spExp){
1636 XTHROW(spExp->
spException,
"attempting to initialize rules and UDTS but parser not defined");
1653 static void vResetCallbacks(
apgex* spExp){
1655 XTHROW(spExp->
spException,
"attempting to initialize rules and UDTS but parser not defined");
1686 vMakeRelPhrase(spExp, uiOffset, (spConfig->
uiInputLength - uiOffset), spRelRight);
1694 vMakeAbsPhrase(spExp, spRelResult, spAbsPhrase);
1696 spResult->
spResult = spAbsPhrase++;
1699 vMakeAbsPhrase(spExp, spRelLeft, spAbsPhrase);
1704 vMakeAbsPhrase(spExp, spRelRight, spAbsPhrase);
1725 for(; spRule < spRuleEnd; spRule++){
1733 aint uiSanityCount = 0;
1735 spListPhrase = spRelPhrases + uiNext;
1737 vMakeAbsPhrase(spExp, &sRelPhrase, spAbsPhrase);
1738 uiNext = spListPhrase->
uiNext;
1743 XTHROW(spExp->
spException,
"number of phrases in linked list not the same as the phrase count");
1756 for(; spUdt < spUdtEnd; spUdt++){
1764 aint uiSanityCount = 0;
1766 spListPhrase = spRelPhrases + uiNext;
1768 vMakeAbsPhrase(spExp, &sRelPhrase, spAbsPhrase);
1769 uiNext = spListPhrase->
uiNext;
1774 XTHROW(spExp->
spException,
"number of phrases in linked list not the same as the phrase count");
1795 memset(spResult, 0,
sizeof(*spResult));
1802 vMatchResult(spExp, spConfig, &sState, spResult);
1812 memset(spResult, 0,
sizeof(*spResult));
1819 vMatchResult(spExp, spConfig, &sState, spResult);
1834 memset(spResult, 0,
sizeof(*spResult));
1840 vMatchResult(spExp, spConfig, &sState, spResult);
1922 fprintf(spOut,
"offset: %"PRIuMAX
" length: %"PRIuMAX
": '",
1927 fprintf(spOut,
"\\t");
1931 fprintf(spOut,
"\\n");
1935 fprintf(spOut,
"\\r");
1938 fprintf(spOut,
"%c", cChar);
1940 fprintf(spOut,
"'\n");
1945 long int iByteCount = (
long int)(uiWordSize * spPhrase->
sPhrase.
uiLength);
1946 if(uiWordSize == 1){
1947 fprintf(spOut,
"offset: %"PRIuMAX
" length: %"PRIuMAX
" bytes\n",
1950 fprintf(spOut,
"offset: %"PRIuMAX
" length: %"PRIuMAX
", %d-byte, %s-endian words\n",
1955 fprintf(spOut,
"%s", cpLine);
1960 fprintf(spOut,
"''\n");
1974 vMakeRelPhrase(spExp, uiAbsoluteOffset, spData->
uiPhraseLength, spPhrase);
1984 XTHROW(spExp->
spException,
"rule phrases malfunction: last phrase is not in the list");
1986 spLastPhrase->uiNext = uiPhraseIndex;
2036 vMakeRelPhrase(spExp, uiAbsoluteOffset, spData->
uiPhraseLength, spPhrase);
2046 XTHROW(spExp->
spException,
"UDT phrases malfunction: last phrase is not in the list");
2048 spLastPhrase->uiNext = uiPhraseIndex;
2086 static void vClearForParse(
apgex* spExp){
2118 static void vClearForPattern(
apgex* spExp){
2147 spExp->
vpAst = NULL;
2170 static rule_r* spFindRule(
apgex* spExp,
const char* cpName){
2172 XTHROW(spExp->
spException,
"no rules defined - vApgexPattern() or vApgexParser() must preceed this call");
2174 if(!cpName || !cpName[0]){
2187 static udt_r* spFindUdt(
apgex* spExp,
const char* cpName){
2189 XTHROW(spExp->
spException,
"no rules defined - vApgexPattern() or vApgexParser() must preceed this call");
2191 if(!cpName || !cpName[0]){
2204 static abool bIsNameChar(
char cChar){
2205 if(cChar >= 65 && cChar <= 90){
2208 if(cChar >= 97 && cChar <= 122){
parser_callback pfnUdt
The UDT callback, if this is a UDT. Cannot be NULL;.
aint uiNodeHits
The number of parser node hits.
void * vpVecPattern
Vector for the pattern string, if any.
For internal object use only. Defines a phrase as an offset into vpVecRelPhases.
apgex_phrase * spPhrases
The list of matched phrases. Any given rule or UDT may have multiple matched sub-phrases.
void * vpVecFlags
Vector for the input flags string.
abool bParseSubString
If true (non-zero), only parse the defined sub-string of the input string.
#define TRACE_APGEX_CHECK(x)
void vUtilFileRead(void *vpMem, const char *cpFileName, uint8_t *ucpData, aint *uipLen)
Read a file into the caller's data area.
aint uiSubStringBeg
The first character of the sub-string to parse. Must be < uiInputLength or exception is thrown.
void * vpVecSplitPhrases
Vector for the resulting phrases of spApgexSplit().
#define TRACE_APGEX_FOOTER(t)
aint uiPhraseCount
The number of matched sub-phrases for this rule/UDT.
apg_phrase sApgexReplace(void *vpCtx, apg_phrase *spSource, apg_phrase *spReplacement)
Replace the matched phrase with a specified phrase.
abool bTraceMode
True if tracing is requested.
void vMemDtor(void *vpCtx)
Destroys a Memory component. Frees all memory allocated.
abool bTraceHtmlMode
True if tracing in HTML mode is requested.
void * vpParser
Pointer to the SABNF grammar parser.
void vApgexPatternFile(void *vpCtx, const char *cpFileName, const char *cpFlags)
Reads the SABNF grammar defining the pattern from a file.
abool bTraceMode
True if the t flag is set in the cpFlags string.
apgex_phrase * spLeftContext
Pointer to the left context of the last result.
void * vpVecRelRules
Vector of relative rules.
aint uiNodeHits
Number of node hits in the pattern-matching parse.
Detailed information about the apgex object after vApgexPattern() has been called.
void vApiInClear(void *vpCtx)
Clears the input and related memory.
void vAstSetRuleCallback(void *vpCtx, aint uiRuleIndex, ast_callback pfnCallback)
Define a callback function for a single rule on the AST.
void * vpApi
Pointer to the API object used to generate the parser.
void vApgexDefineUDT(void *vpCtx, const char *cpName, parser_callback pfnUdt)
Define the callback function for a User-Defined Terminal (UDT).
apgex_phrase sRightContext
The right context of the last match - that is the phrase suffix to the matched phrase.
apgex_result sApgexExec(void *vpCtx, apg_phrase *spSource)
Attempt a pattern match on the source array of APG alphabet characters.
const achar * acpInput
Pointer to the input string.
void * vpApiCtor(exception *spEx)
Construct an API component context (object).
aint uiPhraseCount
The number of matched phrases.
abool bReplaceMode
True if in replace mode.
abool bEnabled
True if this rule has been enabled for phrase capture.
abool bStickyMode
True if the y flag is set prior to any occurrence of g in the cpFlags string.
udt_r * spRelUdts
Pointer to the relative UDTs in the vector of relative UDTs.
void vApiInValidate(void *vpCtx, abool bStrict)
Scans the input SABNF grammar for invalid characters and line ends.
#define ID_AST_OK
normal AST callback function return
void vExContext()
Handles bad context pointers.
#define ID_AST_PRE
indicates pre-node-traversal AST callback state (down the tree)
void * vpUserData
Pointer to user data, if any. Not examined or used by the parser in any way. Presented to the user's ...
void vApiOpcodes(void *vpCtx)
Parse the SABNF grammar and translate its AST into opcodes for all the rules.
Input data to the AST callback functions.
aint uiTreeDepth
The maximum parsing tree depth.
uint_fast8_t achar
achar is the type for the parser's alphabet characters.
void * vpVecUdts
Vector of UDT structures.
void vApgexPattern(void *vpCtx, const char *cpPattern, const char *cpFlags)
Prepare a phrase-matching parser for the given pattern.
The phrase matching results.
apgex_phrase sLastMatch
The last-matched phrase. Same as spResult from the last call to sApgexExec().
const char * cpUdtName
UDT name.
void * vpFmt
Pointer to a format object used for display.
void vAstTranslate(void *vpCtx, void *vpUserData)
Do a depth-first traversal of the AST with user-defined callback functions to translate the AST recor...
abool bGlobalMode
True if in global mode.
void * vpParser
Pointer to the parser object context.
void vApiDtor(void *vpCtx)
The API component destructor.
void * vpVecStrings
Vector for string scratch space.
void * vpApiGetErrorLog(void *vpCtx)
Get the internal message log.
aint uiRuleIndex
Rule index or index.
aint uiIndex
The rule or UDT grammar index.
aint uiFirstPhrase
Offset in vpVecRelPhases for the first phrase of a singly-linked list of phrases matched by this rule...
abool bDefaultMode
True if the cpFlags parameter in vApgexPattern() is NULL or empty.
void * vpVecAt(void *vpCtx, aint uiIndex)
Get a the indexed vector element. The vector is not altered.
void * vpVecPhrases
Vector of matched phrases.
const char * cpFlags
The original string of flags from vApgexPattern().
aint uiIndex
Index of the rule or UDT.
#define XTHROW(ctx, msg)
Exception throw macro.
void(* parser_callback)(callback_data *spData)
User-written callback function prototype.
apgex_phrase * spLeftContext
The phrase prefix.
void vTraceDtor(void *vpCtx)
Trace destructor.
aint uiLength
The length or number of characters in the phrase.
void vParserSetUdtCallback(void *vpCtx, aint uiUdtId, parser_callback pfnCallback)
Set a call back function for a specific UDT.
aint uiRuleCount
The number of combined rules and UDTs in the pattern.
uint_fast32_t aint
The APG parser's unsigned integer type.
aint uiInputLength
Number of input string alphabet characters.
void * vpTrace
Pointer to the trace object if any.
void * vpTraceCtor(void *vpCtx)
The trace object constructor.
Defines the input string and other configuration parameters for the parser,.
apgex_phrase * spResult
The matched phrase. NULL if no match.
void vAstDtor(void *vpCtx)
aint uiLastIndex
The index of the character in the input string where the attempted pattern match begins....
void * vpVecRelPhrases
Vector of relative phrases - offsets rather than absolute pointers.
const char * cpRuleName
Rule name.
aint uiUdtCount
Number of UDTs in the SABNF pattern grammar.
#define BKR_APGEX_CHECK(e)
void * vpVecOriginalSource
Vector to hold the original source with no replacements.
void * vpMemAlloc(void *vpCtx, aint uiBytes)
Allocates memory.
apg_phrase(* pfn_replace)(apgex_result *spResult, apgex_properties *spProperties, void *vpUser)
Prototype for the replacement function used by sApgexReplaceFunc().
aint uiLastIndex
Last index - the offset to the first character to begin the search for a pattern match.
aint uiRuleCount
Number of rules in the SABNF pattern grammar.
void * vpMem
Pointer to a memory object used for all memory allocations.
const void * vpValidate
Must be the "magic number" to be a valid context.
aint uiVecLen(void *vpCtx)
Get the vector length. That is, the number of elements on the vector.
void vApiSyntax(void *vpCtx, abool bStrict)
Parse the SABNF grammar to validate that the grammar structure is valid.
void * vpVecCtor(void *vpMem, aint uiElementSize, aint uiInitialAlloc)
The vector object constructor.
void * vpApgexCtor(exception *spEx)
The phrase-matching engine object constructor.
The parser's final state.
aint uiFirstPhrase
Offset in vpVecRelPhases for the first phrase match for this rule.
apgex_phrase * spRightContext
Pointer to the right context of the last result.
abool bPpptMode
True if PPPTs are used.
Header file for the apgex object.
A structure to describe the type and location of a caught exception.
aint uiLastIndex
The last index following the last pattern match attempt.
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 * vpApgexGetParser(void *vpCtx)
Get a pointer to the parser object's context.
const char * cpRuleName
The rule or UDT name.
void * vpVecReplaceRaw
Vector for the original replacement string with no modifications.
abool bTraceHtmlMode
True if the th flags ar set for HTML trace output in the cpFlags string.
FILE * spDisplay
The open file for display - may be stdout.
#define ID_MATCH
indicates a matched phrase parser state on return from parse tree below this node
uintmax_t luint
luint is used to cast integers suitable for the %"PRIuMAX" printf format.
void * vpApiOutputParser(void *vpCtx)
Generate a parser object directly from the specified SABNF grammar.
void * vpApgexGetTrace(void *vpCtx)
Get a pointer to the trace object's context.
void * vpExternalParser
Pointer to the externally-supplied parsed, if any.
apg_phrase sLastSource
The last source or input string as a phrase - may be different from original if sApgexReplace() or sA...
aint uiTreeDepth
Maximum tree depth reached in the pattern-matching parse.
const char * cpMsgsFirst(void *vpCtx)
Get a pointer to the first logged message, if any.
const char * cpMsgsNext(void *vpCtx)
Get a pointer to the next logged message, if any.
abool bExValidate(exception *spException)
Test an exception structure for validity.
aint uiSourceOffset
Offset into the source array for the first character of the phrase.
void vApgexDisplayPhrase(void *vpCtx, apgex_phrase *spPhrase, const char *cpFileName)
Display the object's properties.
void vApgexDisplayResult(void *vpCtx, apgex_result *spResult, const char *cpFileName)
Display the complete results from a pattern match.
void vApgexDisplayProperties(void *vpCtx, apgex_properties *spProperties, const char *cpFileName)
Display the object's properties.
abool bApiAttrs(void *vpCtx)
Computes the grammar's attributes.
void * vpVecFirst(void *vpCtx)
Get the first element one the vector. The vector is not altered.
apg_phrase * spApgexSplit(void *vpCtx, apg_phrase *spSource, aint uiLimit, aint *uipCount)
Split a phrase into an array of sub-phrases.
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 * vpVecReplacement
Vector for the final form of the replacement string.
aint uiState
One of ID_EMPTY, ID_MATCH or ID_NOMATCH. Note that it is possible for the parser to match a phrase wi...
aint uiUdtIndex
Rule or UDT index or index.
abool bDefaultMode
True if in default mode.
#define TRACE_HEADER_APGEX
Identifies apgex as the header handler.
#define TRACE_APGEX_OUTPUT(x)
const char * cpPattern
Internally preserve copy of the SABNF grammar defining the string to match. NULL if the pattern is de...
aint uiEnabledRuleCount
Number of enabled rules.
aint uiLastPhrase
Offset into vpVecRelPhrases for the last matched phrase. APG_UNDEFINED if no phrases for this rule ex...
void vTraceApgexType(void *vpCtx, aint uiType)
Called only by apgex. Sets the display type for apgex tracing.
For internal object use only. The phrase matching result in relative phrases.
void * vpAstCtor(void *vpParserCtx)
The AST object constructor.
abool bStickyMode
True if in sticky mode.
apg_phrase sOriginalSource
The original source or input string as a phrase.
abool bGlobalMode
True if the g flag is set prior to any occurrence of y in the cpFlags string.
For internal object use only. Relative offsets to phrase information for rules.
const char * cpApiInString(void *vpCtx, const char *cpString)
Reads an SABNF grammar byte stream from a string.
aint uiLength
The number of characters in the array.
apgex_phrase * spRightContext
The phrase suffix.
aint uiMaxTreeDepth
The maximum tree depth reached during the parse.
void vApgexSetLastIndex(void *vpCtx, aint uiLastIndex)
Sets the index of the character in the source where the pattern-match search is to begin.
void vParserDtor(void *vpCtx)
Clears the parser component's context and frees all heap memory associated with this parser.
#define TRACE_APGEX_SEPARATOR(x)
aint uiPhraseCount
The number of matched phrases.
#define TRACE_HTML
Identifier for HTML trace record format.
#define APG_MAX_AINT
Since the maximum unsigned integer value is used to indicate Infinite and Undefined values,...
aint uiHitCount
The number of nodes visited during the traversal of the parse tree.
void * vpTrace
Pointer to the trace object context. NULL unless the t flag is used.
void vApgexDisplayPatternErrors(void *vpCtx, const char *cpFileName)
For internal object use only. The phrase matching object context.
void vApgexDtor(void *vpCtx)
The phrase-matching engine object destructor.
uint8_t abool
abool is the APG bool type.
phrase_r * spRightContext
aint uiPhraseLength
Length of the matched phrase.
aint uiEnabledUdtCount
Number of enabled UDTs.
void * vpVecPopi(void *vpCtx, aint uiIndex)
Pops the element at the given zero-based index and all higher indexes.
void vApgexBkrCheck(exception *spEx)
Back referencing check.
abool bEnabled
True if this UDT has been enabled for phrase capture.
Information about each rule or UDT in the SABNF pattern.
void vApgexEnableRules(void *vpCtx, const char *cpNames, abool bEnable)
Enable or disable specified rule and/or UDT names for phrase capture.
void vApgexPatternParser(void *vpCtx, void *vpParser, const char *cpFlags)
Define the SABNF pattern with a user-created parser.
apgex_properties sApgexProperties(void *vpCtx)
Get a copy of the object's properties.
apgex_phrase * spLastMatch
Pointer to the last matched result.
apg_phrase sApgexReplaceFunc(void *vpCtx, apg_phrase *spSource, pfn_replace pfnFunc, void *vpUser)
Replace the matched phrase with a user-generated phrase.
void * vpAst
Pointer to the AST object if any.
void vAstSetUdtCallback(void *vpCtx, aint uiUdtIndex, ast_callback pfnCallback)
Define a callback function for a single UDT on the AST.
void * vpVecRelUdts
Vector of relative UDTs.
apgex_phrase sLeftContext
The left context of the last match - that is the phrase prefix to the matched phrase.
apg_phrase sPhrase
The matched phrase.
void * vpAst
Pointer to the AST object context. NULL unless the a flag is used.
abool bPpptMode
True if the p flag is set. The parser will use Partially-Predictive Parsing Tables.
abool bApgexTest(void *vpCtx, apg_phrase *spSource)
Report only success or failure on a pattern match.
void * vpApgexGetAst(void *vpCtx)
Get a pointer to the AST object's context.
The representation of a matched phrase.
void vTraceOutputType(void *vpCtx, aint uiType)
Set the trace record display type.
void * vpVecRules
Vector of rule structures.
void * vpVecPush(void *vpCtx, void *vpElement)
Adds one element to the end of the array.
apgex_rule * spRules
The phrases matched by all enabled rules and/or UDTs. NULL if no match.
abool bParserValidate(void *vpCtx)
Validate the context pointer of a parser.
void vVecClear(void *vpCtx)
Clears all used elements in a vector component.
rule_r * spRelRules
Pointer to the relative rules in the vector of relative rules.
aint uiLastPhrase
Index into vpVecRelPhrases for the next match or APG_UNDEFINED if this is the last block.
aint uiNext
The index to the next phrase in a singly-linked list. APG_UNDEFINED marks last phrase in list.
aint uiPhraseOffset
Offset into the source string where the matched phrase begins.
const achar * acpPhrase
Pointer to an array of type achar APG alphabet characters.
For internal object use only. Relative offsets to phrase information for UDTs.
exception * spException
Pointer to the exception structure for reporting errors to the application catch block.
abool bMsgsValidate(void *vpCtx)
Validate a msglog context pointer.
#define TRACE_APGEX_HEADER(t)
void vApiPppt(void *vpCtx, char **cppProtectedRules, aint uiProtectedRules)
Compute the Partially-Predictive Parsing Tables.
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.