Version 7.0
Copyright © 2021 Lowell D. Thomas
APG
… an ABNF Parser Generator
|
Go to the documentation of this file.
79 #include "../../utilities/utilities.h"
82 static char* s_cpDescription =
83 "Illustrate the simple basics of constructing a parser from pre-generated grammar files.";
85 static char* s_cppCases[] = {
86 "Display application information.",
87 "Parse an input string and display the parser state.",
88 "Define rule call back functions to display phrase information during the parser's traversal of the parse tree.",
89 "Illustrate reporting a fatal parsing error.",
91 static long int s_iCaseCount = (
long int)(
sizeof(s_cppCases) /
sizeof(s_cppCases[0]));
93 static int iHelp(
void){
96 printf(
"description: %s\n", s_cpDescription);
97 printf(
" usage: ex-basic arg\n");
98 printf(
" arg = n, 1 <= n <= 4\n");
99 printf(
" execute case number n\n");
100 printf(
" arg = anthing else\n");
101 printf(
" print this help screen\n");
103 for(; i < s_iCaseCount; i++){
104 printf(
"case %ld %s\n", (i + 1), s_cppCases[i]);
119 static int iParseFile() {
120 int iReturn = EXIT_SUCCESS;
121 static void* vpMem = NULL;
122 static void* vpParser = NULL;
127 achar acaBuffer[128];
128 aint uiBufSize = 128;
138 char* cpInput =
"-12.3456e+10";
143 XTHROW(&e,
"alphabet buffer size to small for input string");
148 memset((
void*)&sInput, 0,
sizeof(sInput));
150 sInput.uiInputLength = spPhrase->
uiLength;
151 sInput.uiStartRule = 0;
161 iReturn = EXIT_FAILURE;
168 static char s_acBuffer[1024];
169 static aint s_uiBufferSize = 1024;
178 printf(
"float: going down\n");
185 if(sPhrase.
uiLength >= s_uiBufferSize){
189 printf(
"float: going up\n");
190 printf(
" phrase: %s\n", cpStr);
196 printf(
"sign: going down\n");
203 if(sPhrase.
uiLength >= s_uiBufferSize){
207 printf(
"sign: going up\n");
208 printf(
" phrase: %s\n", cpStr);
214 printf(
"decimal: going down\n");
221 if(sPhrase.
uiLength >= s_uiBufferSize){
225 printf(
"decimal: going up\n");
226 printf(
" phrase: %s\n", cpStr);
232 printf(
"exponent: going down\n");
239 if(sPhrase.
uiLength >= s_uiBufferSize){
243 printf(
"exponent: going up\n");
244 printf(
" phrase: %s\n", cpStr);
250 printf(
"exponent: going down\n");
254 XTHROW(spMyData->
spException,
"demonstration of reporting a fatal error from a call back function");
258 static int iParseString() {
259 int iReturn = EXIT_SUCCESS;
260 static void* vpMem = NULL;
261 static void* vpParser = NULL;
266 achar acaBuffer[128];
267 aint uiBufSize = 128;
278 char* cpInput =
"-12.3456e+10";
283 XTHROW(&e,
"alphabet buffer size to small for input string");
294 memset((
void*)&sInput, 0,
sizeof(sInput));
296 sInput.uiInputLength = spPhrase->
uiLength;
297 sInput.uiStartRule = 0;
298 sInput.vpUserData = (
void*)&sMyData;
309 iReturn = EXIT_FAILURE;
315 static int iParseError() {
316 int iReturn = EXIT_SUCCESS;
317 static void* vpMem = NULL;
318 static void* vpParser = NULL;
323 achar acaBuffer[128];
324 aint uiBufSize = 128;
335 char* cpInput =
"-12.3456e+10";
340 XTHROW(&e,
"alphabet buffer size to small for input string");
351 memset((
void*)&sInput, 0,
sizeof(sInput));
353 sInput.uiInputLength = spPhrase->
uiLength;
354 sInput.uiStartRule = 0;
355 sInput.vpUserData = (
void*)&sMyData;
366 iReturn = EXIT_FAILURE;
382 int main(
int argc,
char **argv) {
385 iCase = atol(argv[1]);
387 if(iCase == 0 || (iCase > s_iCaseCount)){
396 return iParseString();
398 return iParseError();
#define XCTOR(e)
This macro will initialize an exception structure and prepare entry to the "try" block.
void * vpParserCtor(exception *spException, void *vpParserInit)
The parser's constructor for file initialization data.
void vMemDtor(void *vpCtx)
Destroys a Memory component. Frees all memory allocated.
abool try
True for the try block, false for the catch block.
aint uiParserOffset
[read only] Offset from acpString to the first character to match
const achar * acpString
[read only] Pointer to the input sub-string,
aint uiParserState
[read only] ID_ACTIVE if the parser is going down the tree. ID_MATCH or ID_NOMATCH if coming up the t...
uint_fast8_t achar
achar is the type for the parser's alphabet characters.
void vParserSetRuleCallback(void *vpCtx, aint uiRuleId, parser_callback pfnCallback)
Set a call back function for a specific rule.
#define XTHROW(ctx, msg)
Exception throw macro.
uint_fast32_t aint
The APG parser's unsigned integer type.
aint uiParserPhraseLength
[read only] The parser's matched phrase length if uiParserState is ID_MATCH or ID_NOMATCH....
Defines the input string and other configuration parameters for the parser,.
int main(int argc, char **argv)
The executable from this main function is the ABNF Parser Generator application, APG.
The parser's final state.
A structure to describe the type and location of a caught exception.
Defines a pointer to an achar array plus its length. That is, a phrase as is often used by APG.
#define ID_ACTIVE
indicates active parser state, parser has just entered the node and is moving down the parse tree
#define ID_MATCH
indicates a matched phrase parser state on return from parse tree below this node
The data struct passed to each callback function.
void * vpUserData
[input/output] User-defined data passed to to the parser in parser_config.
aint uiLength
The number of characters in the array.
void vUtilApgInfo(void)
Display the current state of apg.h.
void vUtilPrintException(exception *spEx)
Prints exception information from an exception structure.
void vUtilCurrentWorkingDirectory(void)
Display the current working directory.
void vUtilPrintParserState(parser_state *spState)
Display the parser state in human-readable format to stdout.
const achar * acpPhrase
Pointer to an array of type achar APG alphabet characters.
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.