APG
… ABNF Parser Generator
|
The Parser component API. This is the heart of an APG parser. More...
Go to the source code of this file.
Macros | |
#define | PASSERT(cond) if(!(cond)){spCtx->pfnAlertHandler(__LINE__, __FILE__);} |
Functions | |
void * | vpParserCtor (void *vpParserInit, PFN_ALERT pfnAlertHandler) |
void | vParserDtor (void *vpCtx) |
apg_uint | uiParserSyntaxInitCallbacks (void *vpCtx, APG_CALLBACK *spRuleCallbacks, APG_CALLBACK *spUdtCallbacks) |
apg_uint | uiParserSyntaxAnalysis (void *vpCtx, apg_uint uiStartRule, const apg_achar *acpSrc, apg_uint uiSrcLen, void *vpData) |
void | vParserAstInitNodes (void *vpCtx, apg_uint *uipRules, apg_uint *uipUdts) |
void | vParserAstInitCallbacks (void *vpCtx, APG_CALLBACK *pfnRuleCallbacks, APG_CALLBACK *pfnUdtCallbacks) |
apg_uint | uiParserAstTranslate (void *vpCtx, APG_CALLBACK *pfnRuleCallbacks, APG_CALLBACK *pfnUdtCallbacks, void *vpData) |
apg_uint | uiParserState (void *vpCtx, APG_PARSER_STATE *spState) |
apg_uint | uiParserStatsEnable (void *vpCtx, apg_uint uiEnable) |
apg_uint | uiParserStatsGet (void *vpCtx, APG_PARSER_STATS *spStats, apg_uint *uipBufferSize) |
apg_uint | uiParserTraceEnable (void *vpCtx, apg_uint uiEnable) |
void | vParserTraceOp (void *vpCtx, apg_uint uiOpId, apg_uint uiEnable) |
void | vParserTraceRule (void *vpCtx, apg_uint uiIndex, apg_uint uiEnable) |
void | vParserTraceUdt (void *vpCtx, apg_uint uiIndex, apg_uint uiEnable) |
void | vParserTraceRange (void *vpCtx, apg_uint uiBegin, apg_uint uiCount) |
void | vExecuteRule (APG_CBDATA *spData, apg_uint uiId, apg_uint uiOffset) |
void | vExecuteUdt (APG_CBDATA *spData, apg_uint uiId, apg_uint uiOffset) |
The Parser component API. This is the heart of an APG parser.
Definition in file Parser.c.
#define PASSERT | ( | cond | ) | if(!(cond)){spCtx->pfnAlertHandler(__LINE__, __FILE__);} |
Used exclusively for terminal error detection and handling.
It is highly recommended that the user write his own alert handler and install it at parser construction time. The default alert handler simply exits.
apg_uint uiParserAstTranslate | ( | void * | vpCtx, |
APG_CALLBACK * | pfnRuleCallbacks, | ||
APG_CALLBACK * | pfnUdtCallbacks, | ||
void * | vpData | ||
) |
Translates an AST.
Translation is done via user-written callback functions assigned to the AST nodes. The callback functions may be defined with a previous call to vParserAstInitCallbacks() or may be assigned/overridden here. A "depth first" traversal of the AST is done. The callback function defined for each node is called twice. Once on the downward traversal (PRE_AST) of the node and again later on the upward traversal (POST_AST) of the node.
vpCtx | pointer to a parser context, returned by vpParserCtor(). |
pfnRuleCallbacks | pointer to an array of rule name callback function pointers. May be NULL. Will override any functions previously assigned in a call to vParserAstInitCallbacks(). |
pfnUdtCallbacks | pointer to an array of UDT callback function pointers. May be NULL. Will override any functions previously assigned in a call to vParserAstInitCallbacks(). |
vpData | user-defined data to be passed to the callback functions. May be NULL. Ignored by Parser. |
apg_uint uiParserState | ( | void * | vpCtx, |
APG_PARSER_STATE * | spState | ||
) |
Returns the parser state. Only meaningful after a call to uiParserSyntaxAnalysis().
vpCtx | pointer to a parser context, returned by vpParserCtor(). |
spState | pointer to a user-supplied APG_PARSER_STATE struct. |
Enables/disables collection of parser statistics.
Statistics gathering will negatively affect the parser's performance. Therefore, statistics gathering is disabled by default.
Note: a call to this function will have no affect unless _APG_CFG_STATS is defined.
vpCtx | pointer to a parser context, returned by vpParserCtor(). |
uiEnable | true if statistics gathering is to be enabled, false to disable (default.) |
apg_uint uiParserStatsGet | ( | void * | vpCtx, |
APG_PARSER_STATS * | spStats, | ||
apg_uint * | uipBufferSize | ||
) |
Retrieve the statistics collected by uiParserSyntaxAnalysis().
vpCtx | pointer to a parser context, returned by vpParserCtor(). |
spStats | pointer to a user-supplied statistics data buffer. If NULL, only the required buffer size is returned. |
uipBufferSize | pointer to the variable to receive the required buffer size. |
apg_uint uiParserSyntaxAnalysis | ( | void * | vpCtx, |
apg_uint | uiStartRule, | ||
const apg_achar * | acpSrc, | ||
apg_uint | uiSrcLen, | ||
void * | vpData | ||
) |
Parses an input string of alphabet characters.
vpCtx | pointer to a parser context, returned by vpParserCtor(). |
uiStartRule | index of the grammar rule to be used as the start rule. 0 will always be the index of the first rule defined by the grammar. Good usage practice is to always use an index defined in the ProjectName.h header file generated by apg, the parser generator. This will be a #define of the form "RULE_PROJECTNAME_RULENAME". |
acpSrc | pointer to the array of alphabet character codes defining the string to be parsed. |
uiSrcLen | the number of alphabet characters in the input string. |
vpData | optional pointer to user-defined data. This pointer, which may be NULL, will be passed on to the user-defined callback functions. It will be the vpUserData member of the APG_CBDATA structs passed to the callback functions. It is otherwise ignored by the parser. |
apg_uint uiParserSyntaxInitCallbacks | ( | void * | vpCtx, |
APG_CALLBACK * | spRuleCallbacks, | ||
APG_CALLBACK * | spUdtCallbacks | ||
) |
Defined the syntax callback functions to the parser.
vpCtx | pointer to a parser context, returned by vpParserCtor(). |
spRuleCallbacks | an array of pointers to the rule callback functions. May be NULL. |
spUdtCallbacks | an array of pointers to the UDT callback functions, if any. May be NULL if the grammar defines no UDTs. Each UDT defined in the grammar must have a callback function defined here. Otherwise, this function will return false and the parser syntax analysis will fail. |
Enables/disables tracing.
Tracing will negatively affect the parser's performance. Therefore, tracing is disabled by default.
If tracing is enabled, trace records will be printed to the standard output as the parser traverses the syntax tree. By default, records are printed only for rule and UDT nodes (RNM and UDT.) However, prior to parsing, tracing can be configured to trace or not trace any type of node. For tracing other than the default nodes:
Note: a call to this function will have no affect unless _APG_CFG_TRACE is defined.
vpCtx | pointer to a parser context, returned by vpParserCtor(). |
uiEnable | if true, enables tracing, if false, disables tracing |
void vExecuteRule | ( | APG_CBDATA * | spData, |
apg_uint | uiId, | ||
apg_uint | uiOffset | ||
) |
To be called only from callback functions. Executes the specified rule as if it were the following node in the syntax tree.
spData | the data supplied to the callback function by the parser |
uiId | index of the rule to execute |
uiOffset | offset into the input string of the first character of the phrase to match |
void vExecuteUdt | ( | APG_CBDATA * | spData, |
apg_uint | uiId, | ||
apg_uint | uiOffset | ||
) |
To be called only from callback functions. Executes the specified UDT as if it were the following node in the syntax tree.
spData | the data supplied to the callback function by the parser |
uiId | index of the UDT to execute |
uiOffset | offset into the input string of the first character of the phrase to match |
void vParserAstInitCallbacks | ( | void * | vpCtx, |
APG_CALLBACK * | pfnRuleCallbacks, | ||
APG_CALLBACK * | pfnUdtCallbacks | ||
) |
Defines which rule/UDT nodes are to be assigned callback functions in the AST.
These functions need only be assigned if translation with the function uiParserAstTranslate() is to be used.
vpCtx | pointer to a parser context, returned by vpParserCtor(). |
pfnRuleCallbacks | pointer to an array of rule name callback function pointers. May be NULL. |
pfnUdtCallbacks | pointer to an array of UDT callback function pointers. May be NULL. |
Defines which rule/UDT names are to be kept as nodes in the Abstract Syntax Tree (AST).
Note that defining the nodes to be kept is a separate issue from defining which nodes have callback functions. The AST nodes may or may not be used for translation.
vpCtx | pointer to a parser context, returned by vpParserCtor(). |
uipRules | pointer to an array of integers defining the rule indexes of the rule name nodes to be kept in the AST. May be NULL. |
uipUdts | pointer to an array of integers defining the UDT indexes of the UDT nodes to be kept in the AST. May be NULL. |
void vParserDtor | ( | void * | vpCtx | ) |
Parser component destructor
vpCtx | pointer to parser component context (returned from vpParserCtor()) |
Configures the types of operator nodes to trace.
vpCtx | pointer to a parser context, returned by vpParserCtor(). |
uiOpId | should be one of the following: APG_TRACE_ALL - trace all operator nodes APG_TRACE_TRG - trace the TRG nodes APG_TRACE_TBS - trace the TBS nodes APG_TRACE_TLS - trace the TLS nodes APG_TRACE_AND - trace the AND nodes APG_TRACE_NOT - trace the NOT nodes APG_TRACE_ALT - trace the ALT nodes APG_TRACE_CAT - trace the CAT nodes APG_TRACE_REP - trace the REP nodes APG_TRACE_RNM - trace the RNM nodes APG_TRACE_UDT - trace the UDT nodes APG_TRACE_COUNT - count the trace records only, none are printed |
uiEnable | if true, the specified operators are printed, if false, not |
Configures the trace record range to print.
Trace records can be voluminous. This configuration can reduce the number printed to a managable set. In this context, it may be useful to first trace the parse with APG_TRACE_COUNT configured in vParserTraceOp().
vpCtx | pointer to a parser context, returned by vpParserCtor(). |
uiBegin | the record number of the first trace record to print |
uiCount | the number of trace records to print |
Configures which rule names to trace. By default all rule names are traced.
vpCtx | pointer to a parser context, returned by vpParserCtor(). |
uiIndex | rule index of the rule name to trace |
uiEnable | if true, the specified rule name records are printed, if false, not |
Configures which UDT names to trace. By default all UDT names are traced.
vpCtx | pointer to a parser context, returned by vpParserCtor(). |
uiIndex | UDT index of the UDT name to trace |
uiEnable | if true, the specified UDT name records are printed, if false, not |
void* vpParserCtor | ( | void * | vpParserInit, |
PFN_ALERT | pfnAlertHandler | ||
) |
Construct a new APG parser component.
vpParserInit | the initialization data from APG |
pfnAlertHandler | the alert handler, called on terminal errors. NULL for default alert handler. |