Version 7.0
Copyright © 2021 Lowell D. Thomas
APG
… an ABNF Parser Generator
|
Go to the documentation of this file.
38 #include "../utilities/utilities.h"
42 static void vUp(
trace* spCtx,
const opcode* spOp,
aint uiState,
aint uiOffset,
aint uiPhraseLength);
43 static void vDown(
trace* spCtx,
const opcode* spOp,
aint uiOffset);
44 static abool bTraceConfigCheck(
trace* spCtx,
const opcode* spOp);
46 static void vDownPppt(
trace* spCtx,
const opcode* spOp,
aint uiOffset){
49 vDown(spCtx, spOp, uiOffset);
51 if (bTraceConfigCheck(spCtx, spOp)) {
73 static void vUpPppt(
trace* spCtx,
const opcode* spOp,
aint uiState,
aint uiOffset,
aint uiPhraseLength){
76 vUp(spCtx, spOp, uiState, uiOffset, uiPhraseLength);
79 if (bTraceConfigCheck(spCtx, spOp)) {
96 parser* spParser = (parser*) vpCtx;
97 if(spParser->vpTrace){
99 spParser->vpTrace = NULL;
101 void* vpMem = spParser->vpMem;
103 memset((
void*) spTrace, 0,
sizeof(
trace));
104 spTrace->vpMem = vpMem;
106 spTrace->spParserCtx = spParser;
109 spTrace->spOut = stdout;
110 spTrace->cpFileName = NULL;
111 spTrace->vpVecFileName =
vpVecCtor(vpMem,
sizeof(
char), 1024);
114 spTrace->vpLookaroundStack =
vpVecCtor(spTrace->vpMem, (
aint)
sizeof(
aint), 500);
118 (
aint) (
sizeof(
abool) * spTrace->spParserCtx->uiRuleCount));
119 memset((
void*) spTrace->sConfig.bpRules, 0,
sizeof(
abool) * spTrace->spParserCtx->uiRuleCount);
120 if (spTrace->spParserCtx->uiUdtCount) {
123 (
aint) (
sizeof(
abool) * spTrace->spParserCtx->uiUdtCount));
124 memset((
void*) spTrace->sConfig.bpUdts, 0,
sizeof(
abool) * spTrace->spParserCtx->uiUdtCount);
128 memset((
void*) spTrace->sConfig.bpOps, 0,
sizeof(
abool) *
ID_GEN);
131 spTrace->uiBufSize = 4096;
132 spTrace->cpBuf = (
char*)
vpMemAlloc(spTrace->vpMem, (
aint) (
sizeof(char) * spTrace->uiBufSize));
133 memset((
void*) spTrace->cpBuf, 0, (
sizeof(
char) * spTrace->uiBufSize));
139 spParser->vpTrace = (
void*) spTrace;
140 return (
void*)spTrace;
158 if(spTrace->
spOut && (spTrace->
spOut != stdout)){
159 fclose(spTrace->
spOut);
160 spTrace->
spOut = NULL;
178 if(spTrace->
spOut && spTrace->
spOut != stdout){
179 fclose(spTrace->
spOut);
181 spTrace->
spOut = stdout;
189 snprintf(caBuf, 1024,
"can't open file %s for trace output", cpFileName);
217 fflush(spCtx->
spOut);
258 fflush(spCtx->
spOut);
268 vDown(spCtx, spOp, uiOffset);
274 vDownPppt(spCtx, spOp, uiOffset);
276 vDown(spCtx, spOp, uiOffset);
287 vUp(spCtx, spOp, uiState, uiOffset, uiPhraseLength);
290 void vTraceUp(
void* vpCtx,
const opcode* spOp,
aint uiState,
aint uiOffset,
aint uiPhraseLength){
293 vUpPppt(spCtx, spOp, uiState, uiOffset, uiPhraseLength);
295 vUp(spCtx, spOp, uiState, uiOffset, uiPhraseLength);
316 static void vDown(
trace* spCtx,
const opcode* spOp,
aint uiOffset){
319 if (bTraceConfigCheck(spCtx, spOp)) {
320 if(spOp->sGen.uiId ==
ID_NOT || spOp->sGen.uiId ==
ID_AND){
323 }
else if(spOp->sGen.uiId ==
ID_BKA || spOp->sGen.uiId ==
ID_BKN){
346 static void vUp(
trace* spCtx,
const opcode* spOp,
aint uiState,
aint uiOffset,
aint uiPhraseLength) {
349 if (bTraceConfigCheck(spCtx, spOp)) {
354 if(spOp->sGen.uiId ==
ID_NOT || spOp->sGen.uiId ==
ID_AND ||
355 spOp->sGen.uiId ==
ID_BKA || spOp->sGen.uiId ==
ID_BKN){
369 static abool bTraceConfigCheck(
trace* spCtx,
const opcode* spOp) {
372 aint uiId = spOp->sGen.uiId;
387 bReturn = spConfig->
bpOps[uiId];
390 bReturn = spConfig->
bpRules[spOp->sRnm.spRule->uiRuleIndex];
393 bReturn = spConfig->
bpUdts[spOp->sUdt.spUdt->uiUdtIndex];
void vTraceSetOutput(void *vpCtx, const char *cpFileName)
void vTraceApgexFooter(void *vpCtx)
Only called by apgex.
parser * spParserCtx
Pointer back to the parent parser's context.
#define ID_LOOKAROUND_BEHIND
the parser presently is in look behind mode
void vDisplaySeparator(trace *spCtx, aint uiLastIndex)
Display a separator between trace outputs (apgex only)
int iTraceDepthMax
The maximum (possibly partial) traced node depth.
FILE * spOut
Pointer to the open output file.
aint uiThisRecord
The record index of the current record.
Private header for the SABNF parser.
void vDisplayFooter(trace *spCtx)
Display the trace footer.
aint uiPhraseLength
The phrase length of a successful match.
Configuration defining the subset of nodes to display information for.
void vVecDtor(void *vpCtx)
The vector component destructor.
const opcode * spOpcode
Pointer to the opcode for the current node.
#define ID_ALT
alternation
void vTraceBegin(void *vpCtx)
Called by the parser to start the trace.
#define ID_BKR
back reference to a previously matched rule or UDT name
#define ID_UDT
user-defined terminal
FILE * spOpenFile
Pointer to any other open file.
void vExContext()
Handles bad context pointers.
abool bPppt
However, no records are actually displayed.
abool * bpUdts
An array of true/false indicators for each UDT in the SABNF grammar.
aint uiThisRecord
Index of the current trace record.
aint uiMaxRecords
Maximun number of records to display.
void vTraceApgexSeparator(void *vpCtx, aint uiLastIndex)
Only called by apgex.
void * vpLookaroundStack
Pointer to a stack require to keep track of when tracing in look around mode.
#define ID_NOT
negative look ahead
#define ID_AND
positive look ahead
abool * bpOps
An array of true/false indicators for each opcode in the SABNF grammar.
trace_config sConfig
Pointer to the trace configuration.
void vTraceUp(void *vpCtx, const opcode *spOp, aint uiState, aint uiOffset, aint uiPhraseLength)
Called by the parser following upward traversal of a parse tree node.
aint uiState
The parser's state for this node (ID_ACTIVE, ID_MATCH, etc.)
#define XTHROW(ctx, msg)
Exception throw macro.
void * vpVecPop(void *vpCtx)
Pops one element from the end of the array.
void vTraceDtor(void *vpCtx)
Trace destructor.
int iTraceDepthMin
The minimum (possibly partial) traced node depth.
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 * vpTraceCtor(void *vpCtx)
The trace object constructor.
int iTraceDepth
The partial parse tree depth (a subset possibly restricted by the configuration.)
#define ID_CAT
concatenation
void * vpMemAlloc(void *vpCtx, aint uiBytes)
Allocates memory.
aint uiTreeDepth
The actual parse tree depth.
void * vpVecCtor(void *vpMem, aint uiElementSize, aint uiInitialAlloc)
The vector object constructor.
#define ID_TRG
terminal range
aint uiTreeDepth
The current full parse tree depth.
#define ID_LOOKAROUND_AHEAD
the parser presently is in look ahead mode
#define ID_ACTIVE
indicates active parser state, parser has just entered the node and is moving down the parse tree
void vMsgsDtor(void *vpCtx)
The object destructor.
#define ID_MATCH
indicates a matched phrase parser state on return from parse tree below this node
void * vpVecPushn(void *vpCtx, void *vpElement, aint uiCount)
Adds one or more elements to the end of the array.
#define ID_BKA
positive look behind
aint uiHeaderType
Indicates whether the trace is being done by apgex.
#define ID_GEN
general opcode (not SABNF). Serves to locate the ID in any opcode structure and must be larger than a...
void vTraceApgexHeader(void *vpCtx)
Only called by apgex.
Private header file for the trace functions.
#define ID_TLS
terminal literal string
The information recorded & displayed by the trace object for each node visited.
void vTraceEnd(void *vpCtx)
Called by the parser to end the trace.
char * cpFileName
Name of the file to display the trace records to.
#define ID_BKN
negative look behind
aint uiTraceGetRecordCount(void *vpCtx)
Get the number of traced records, displayed or not.
uint8_t abool
abool is the APG bool type.
abool * bpRules
An array of true/false indicators for each rule in the SABNF grammar.
int iTraceDepth
The current (possibly partial) traced node depth.
#define TRACE_HEADER_TRACE
Identifies the trace object as the header handler.
aint uiTreeDepthMax
The maximum full parse tree depth achieved.
#define ID_ABG
anchor - beginning of string
void * vpVecFileName
Vector to allocate and re-allocate the memory for the file name.
void vDisplayRecord(trace *spCtx, trace_record *spRec, abool bIsMatchedPppt)
Display one trace record.
The trace object context. Maintains the trace object's state.
aint uiFirstRecord
Number of the first record to display.
#define ID_TBS
terminal binary string
void vTraceDown(void *vpCtx, const opcode *spOp, aint uiOffset)
Called by the parser prior to downward traversal of a parse tree node.
aint uiOffset
The offset into the input string for the first character of the sub-phrase being matched.
abool bCountOnly
If true, trace will only count the number of records that would have been displayed.
void * vpVecPush(void *vpCtx, void *vpElement)
Adds one element to the end of the array.
abool bParserValidate(void *vpCtx)
Validate the context pointer of a parser.
void * vpLog
Pointer to a message log object for reporting configuration errors.
void vVecClear(void *vpCtx)
Clears all used elements in a vector component.
exception * spException
Pointer to the exception to use to report fatal errors back to the parser's catch block.
void vDisplayHeader(trace *spCtx)
Display the trace header.
void vSetDefaultConfig(trace *spTrace)
Sets the default trace configuration on construction.
#define ID_AEN
anchor - end of string
APG Version 7.0 is licensed under the
2-Clause BSD License,
an Open Source Initiative Approved License.