Version 7.0
Copyright © 2021 Lowell D. Thomas
APG
… an ABNF Parser Generator
Exceptions and Objects

APG version 7.0 is written entirely in C. C, of course, is not an object-oriented language. However, using data encapsulation is relatively easy and a crude exception handler can be fashioned from the setjmp()/longjmp() facility.

Exception Handling

APG's exception handling is described in detail elsewhere (exception.h and exception.c). But briefly, any application using an APG parser, or any other of the version 7.0 components, needs to be aware that all fatal error conditions are handled with exceptions and to know how to deal with them. Here is a simple "Hello World" example demonstrating the basic application set up.

#include <stdio.h>
#include "./library/lib.h"
int main(int argc, char** argv){
XCTOR(e);
if(e.try){
//try block
printf("Hello, World\n");
// report an error
XTHROW(&e, "Hello, Exception!!!");
}else{
//catch block - handle exceptions here
printf("oops\n");
printf("%s:%s(%u):%s\n", e.caFile, e.caFunc, e.uiLine, e.caMsg);
}
return 0;
}

Assuming this code is in the file ex.c which resides in the APG repository directory, to compile and run with the gcc compiler,

gcc library/exception.c ex.c -o /tmp/ex
/tmp/ex

The output should look like

Hello, World
oops
ex.c:main(10):Hello, Exception!!!

The macro XCTOR(e) calls setjmp(), saving the call-stack state of the main() function and dropping into the user's try block. Fatal errors in APG use the macro XTHROW() to report the error location along with a descriptive message. XTHROW calls longjmp() which will restore the call stack state back the the main() function's XCTOR() call and drop into the caller's catch block.

Objects - Data Encapsulation

Data encapsulation is quite easily accomplished in C and APG makes heavy use of it. Most of APG's features, applications, tools and utilities are implemented as data-encapsulated objects, each with its own constructor and destructor. As in the exception handling example above, the object constructor returns a context pointer. That is, a pointer to an opaque chunk of data that holds the state of the object. The destructor will then close any files and free all memory associated with the object, including the context itself.

A constructor function name always ends with Ctor().
The function almost always takes a pointer to a valid, initialized exception structure as a first argument.

A destructor function name always ends with Dtor().
It always takes its context pointer as a single argument.

In lieu of namespaces, all members of an object's "class" use the object name as a function name prefix. For example, the parser "class" has member functions (ignoring arguments):

void* vpParserCtor();
void  vParserDtor();
abool bParserValidate();
void  vParserParse();
void  vParserSetRuleCallback();
void  vParserSetUdtCallback();
lib.h
This header "#include"s all publid lib headers and other standard headers needed by most objects.
XCTOR
#define XCTOR(e)
This macro will initialize an exception structure and prepare entry to the "try" block.
Definition: exception.h:77
exception::caFunc
char caFunc[64]
The source code function name where the error occurred. "__func__".
Definition: exception.h:54
exception::try
abool try
True for the try block, false for the catch block.
Definition: exception.h:49
exception::uiLine
unsigned int uiLine
The source code line number where the error occurred. "__LINE__".
Definition: exception.h:51
exception::caFile
char caFile[256]
The source code file name where the error occurred. "__FILE__".
Definition: exception.h:53
XTHROW
#define XTHROW(ctx, msg)
Exception throw macro.
Definition: exception.h:67
main
int main(int argc, char **argv)
The executable from this main function is the ABNF Parser Generator application, APG.
Definition: main.c:61
exception
A structure to describe the type and location of a caught exception.
Definition: exception.h:47
exception::caMsg
char caMsg[256]
A the caller's error message.
Definition: exception.h:52
APG Version 7.0 is licensed under the 2-Clause BSD License,
an Open Source Initiative Approved License.