Version 7.0
Copyright © 2021 Lowell D. Thomas
APG
… an ABNF Parser Generator
|
Go to the documentation of this file.
40 static const void* s_vpMagicNumber = (
void*)
"ast";
56 parser* spParser = (parser*)vpParserCtx;
59 spParser->vpAst = NULL;
63 memset((
void*)spCtx, 0,
sizeof(
ast));
66 memset((
void*)spCtx->pfnRuleCallbacks, 0, (
sizeof(
ast_callback) * spParser->uiRuleCount));
67 if(spParser->uiUdtCount){
69 memset((
void*)spCtx->pfnUdtCallbacks, 0, (
sizeof(
ast_callback) * spParser->uiUdtCount));
71 spCtx->vpVecThatStack =
vpVecCtor(spParser->vpMem,
sizeof(
aint), 1000);
72 spCtx->vpVecOpenStack =
vpVecCtor(spParser->vpMem,
sizeof(
aint), 100);
75 spCtx->spParser = spParser;
76 spParser->vpAst = (
void*)spCtx;
77 spCtx->vpValidate = s_vpMagicNumber;
83 if(vpCtx && (spCtx->
vpValidate == s_vpMagicNumber)){
84 void* vpMem = spCtx->
spParser->vpMem;
93 memset((
void*)spCtx, 0,
sizeof(
ast));
106 if(vpCtx && (spCtx->
vpValidate == s_vpMagicNumber)){
139 if(vpCtx && (spCtx->
vpValidate == s_vpMagicNumber)){
167 if(vpCtx && (spCtx->
vpValidate == s_vpMagicNumber)){
179 while( ui < uiRecords){
180 spRecord = &spRecords[ui];
245 if(vpCtx && (spCtx->
vpValidate == s_vpMagicNumber)){
246 if(uiRuleIndex < spCtx->spParser->uiRuleCount){
268 if(vpCtx && (spCtx->
vpValidate == s_vpMagicNumber)){
269 if(uiUdtIndex < spCtx->spParser->uiUdtCount){
290 aint uiIndex = uiRuleIndex;
292 if(uiRuleIndex >= spCtx->
spParser->uiRuleCount){
293 uiIndex = uiRuleIndex - spCtx->
spParser->uiRuleCount;
335 aint uiRecordCount = *uipRecordCount;
337 aint uiIndex = uiRuleIndex;
339 if(uiRuleIndex >= spCtx->
spParser->uiRuleCount){
340 uiIndex = uiRuleIndex - spCtx->
spParser->uiRuleCount;
353 if(!uipThatRecordIndex){
409 aint uiRecordCount = *uipRecordCount;
This header "#include"s all publid lib headers and other standard headers needed by most objects.
aint uiThatRecord
The matching record number. That is, if uiThisRecord the number of the record that opens the rule,...
abool bAstValidate(void *vpCtx)
Validate an AST context pointer.
void vAstSetRuleCallback(void *vpCtx, aint uiRuleIndex, ast_callback pfnCallback)
Define a callback function for a single rule on the AST.
Private header for the SABNF parser.
const achar * acpString
The parsed input string.
aint uiRuleCount
The number of rules.
aint uiStringLength
The number of characters in the input string.
void * vpVecThatStack
Pointer to the vector holding a LIFO stack to match up and down node records.
void vVecDtor(void *vpCtx)
The vector component destructor.
aint uiPhraseLength
The number of characters in the matched phrase.
#define ID_AST_POST
indicates post-node-traversal AST callback state (up the tree)
aint uiUdtCount
The number of UDTs.
#define ID_AST_OK
normal AST callback function return
exception * spException
Use but don't alter. Use to throw exceptions to the AST catch block.
void vExContext()
Handles bad context pointers.
#define ID_AST_PRE
indicates pre-node-traversal AST callback state (down the tree)
Input data to the AST callback functions.
const char * cpName
Name of the rule or UDT.
const char * cpName
Name of the rule or UDT of this record.
void vAstTranslate(void *vpCtx, void *vpUserData)
Do a depth-first traversal of the AST with user-defined callback functions to translate the AST recor...
void * vpVecOpenStack
Pointer to a vector LIFO stack of open rule nodes.
void * vpVecAt(void *vpCtx, aint uiIndex)
Get a the indexed vector element. The vector is not altered.
aint uiIndex
Index of the rule or UDT.
void vAstInfo(void *vpCtx, ast_info *spInfo)
Retrieve basic information about the AST object.
parser * spParser
Pointer to the parent parser.
ast_callback * pfnRuleCallbacks
An array of rule name call back functions.
#define XTHROW(ctx, msg)
Exception throw macro.
void * vpVecPop(void *vpCtx)
Pops one element from the end of the array.
void vAstOpOpen(void *vpCtx)
Called in preparation for a downward traversal of an RNM or UDT node.
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 vAstDtor(void *vpCtx)
void vAstOpClose(void *vpCtx, aint uiState)
Called to finish up after an upward traversal of an RNM or UDT node.
void * vpMemAlloc(void *vpCtx, aint uiBytes)
Allocates memory.
aint uiVecLen(void *vpCtx)
Get the vector length. That is, the number of elements on the vector.
void * vpVecCtor(void *vpMem, aint uiElementSize, aint uiInitialAlloc)
The vector object constructor.
The AST object context. Holds the object's state.
abool bIsUdt
True if this record is for a UDT.
All the information a user needs to write a custom AST translator.
exception * spException
Pointer to an exception structure for reporting fatal errors back to the parser's catch block scope.
void vMemFree(void *vpCtx, const void *vpData)
Free memory previously allocated with vpMemAlloc().
void * vpVecRecords
Pointer to the vector holding the AST records (two for each saved node).
aint(* ast_callback)(ast_data *spData)
The prototype for AST translation callback functions.
#define ID_AST_SKIP
on return from AST callback, skip all nodes below (ignored on return from ID_AST_POST state)
aint uiRecordCount
The number of records (two for each node of the AST, one down traversal, one up.)
#define ID_MATCH
indicates a matched phrase parser state on return from parse tree below this node
aint uiState
ID_AST_PRE if the current record opens the rule, ID_AST_POST if the current record closes the rule.
aint uiThisRecord
The record number.
void vAstClear(void *vpCtx)
Clear the AST records for reuse of the AST object.
void * vpVecFirst(void *vpCtx)
Get the first element one the vector. The vector is not altered.
ast_record * spRecords
The list of records in the order of a depth-first traversal of the AST.
abool bIsUdt
True if this record is for a UDT. False if it is for a rule.
void * vpAstCtor(void *vpParserCtx)
The AST object constructor.
ast_callback * pfnUdtCallbacks
An array of UDT call back functions.
uint8_t abool
abool is the APG bool type.
const void * vpValidate
A "magic number" indicating a valid, initialized AST object.
void * vpVecPopi(void *vpCtx, aint uiIndex)
Pops the element at the given zero-based index and all higher indexes.
#define ID_NOMATCH
indicates that no phrase was matched on return from parse tree below this node
void vAstSetUdtCallback(void *vpCtx, aint uiUdtIndex, ast_callback pfnCallback)
Define a callback function for a single UDT on the AST.
void vAstRuleOpen(void *vpCtx, aint uiRuleIndex, aint uiPhraseOffset)
Called by parser's RNM operator before downward traversal.
Private header file for the AST functions.
aint uiPhraseOffset
The offset into the input string to the first character of the matched phrase.
aint uiIndex
Index of the rule or UDT of this record.
void * vpVecPush(void *vpCtx, void *vpElement)
Adds one element to the end of the array.
void vAstRuleClose(void *vpCtx, aint uiRuleIndex, aint uiState, aint uiPhraseOffset, aint uiPhraseLength)
Called by parser's RNM operator after upward traversal.
abool bParserValidate(void *vpCtx)
Validate the context pointer of a parser.
void vVecClear(void *vpCtx)
Clears all used elements in a vector component.
APG Version 7.0 is licensed under the
2-Clause BSD License,
an Open Source Initiative Approved License.