Version 7.0
Copyright © 2021 Lowell D. Thomas
APG
… an ABNF Parser Generator
|
Go to the documentation of this file.
78 #include "../../api/api.h"
82 static const char* cpMakeFileName(
char* cpBuffer,
const char* cpBase,
const char* cpDivider,
const char* cpName){
83 strcpy(cpBuffer, cpBase);
84 strcat(cpBuffer, cpDivider);
85 strcat(cpBuffer, cpName);
89 static char* s_cpDescription =
90 "Illustrate parsing of wide characters.";
92 static char* s_cppCases[] = {
93 "Display application information.",
94 "Parse lines of Cherokee language UTF-32 Unicode text.",
96 static long int s_iCaseCount = (
long int)(
sizeof(s_cppCases) /
sizeof(s_cppCases[0]));
98 static int iHelp(
void){
101 printf(
"description: %s\n", s_cpDescription);
102 printf(
" usage: ex-api arg\n");
103 printf(
" arg = n, 1 <= n <= %ld\n", s_iCaseCount);
104 printf(
" execute case number n\n");
105 printf(
" arg = anthing else\n");
106 printf(
" print this help screen\n");
108 for(; i < s_iCaseCount; i++){
109 printf(
"case %ld %s\n", (i + 1), s_cppCases[i]);
137 static int iLines() {
138 int iReturn = EXIT_SUCCESS;
139 static void* vpApi = NULL;
140 static void* vpMem = NULL;
141 static void* vpVec = NULL;
142 static void* vpParser = NULL;
143 static FILE* spOut = NULL;
146 "line = line-text %d13.10\n"
147 "line-text = *(%x13A0-13F4 / %x20 / %x2e)\n";
148 uint8_t ucaBuf[1024];
149 aint uiBufSize = 1024;
152 const achar* acpBeg, *acpEnd;
153 const char* cpInput, *cpInputBig, *cpInputLittle;
154 char* cpOutName =
"cherokee.html";
155 char* cpInBig =
"cherokee.utf32be";
156 char* cpInLittle =
"cherokee.utf32le";
157 const char* cpOutput;
158 char caBufOut[PATH_MAX], caBufIn[PATH_MAX];
170 if(
sizeof(
achar) != 4){
171 XTHROW(&e,
"sizeof(achar) must be == 4");
176 cpInput = cpMakeFileName(caBufIn, SOURCE_DIR,
"/../input/", cpInBig);
178 cpInput = cpMakeFileName(caBufIn, SOURCE_DIR,
"/../input/", cpInLittle);
180 cpOutput = cpMakeFileName(caBufOut, SOURCE_DIR,
"/../output/", cpOutName);
183 printf(
" input file name: %s\n", cpInput);
184 printf(
"output file name: %s\n", cpOutput);
189 if(uiSize > uiBufSize){
190 XTHROW(&e,
"buffer size too small for input file");
198 memset(&sConfig, 0,
sizeof(sConfig));
207 printf(
"\nParser State\n");
209 printf(
"\nlines parsed: %"PRIuMAX
"\n", (
luint)
uiVecLen(vpVec));
213 spOut = fopen(cpOutput,
"wb");
215 XTHROW(&e,
"can't open output file for HTML");
217 fprintf(spOut,
"<!DOCTYPE html>\n");
218 fprintf(spOut,
"<html lang=\"en\">\n");
219 fprintf(spOut,
" <head>\n");
220 fprintf(spOut,
" <meta charset=\"utf-8\">\n");
221 fprintf(spOut,
" <title>Cherokee Text</title>\n");
222 fprintf(spOut,
" </head>\n");
223 fprintf(spOut,
" <body>\n");
224 fprintf(spOut,
" <h1>Cherokee Sample</h1>\n");
225 fprintf(spOut,
" <p>Wikipedia <a href=\"https://en.wikipedia.org/wiki/Cherokee_language#Samples\">source</a>. </p>\n");
226 fprintf(spOut,
" <p>\n");
227 for(ui = 0; ui <
uiVecLen(vpVec); ui++){
230 acpEnd = acpBeg + spMyLine->
uiLength;
231 for(; acpBeg < acpEnd; acpBeg++){
232 fprintf(spOut,
"&#%"PRIuMAX
";", (
luint)*acpBeg);
234 fprintf(spOut,
" <br>\n");
236 fprintf(spOut,
" </p>\n");
237 fprintf(spOut,
" </body>\n");
238 fprintf(spOut,
"</html>\n");
240 printf(
"\nOpen file %s in browser to view parsed lines.\n", cpOutput);
246 iReturn = EXIT_FAILURE;
266 int main(
int argc,
char **argv) {
269 iCase = atol(argv[1]);
271 if((iCase > 0) && (iCase <= s_iCaseCount)){
272 printf(
"%s\n", s_cppCases[iCase -1]);
#define XCTOR(e)
This macro will initialize an exception structure and prepare entry to the "try" block.
void vUtilFileRead(void *vpMem, const char *cpFileName, uint8_t *ucpData, aint *uipLen)
Read a file into the caller's data area.
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 * acpInput
Pointer to the input string.
void * vpApiCtor(exception *spEx)
Construct an API component context (object).
aint uiParserState
[read only] ID_ACTIVE if the parser is going down the tree. ID_MATCH or ID_NOMATCH if coming up the t...
aint uiParserRuleLookup(void *vpCtx, const char *cpRuleName)
Find the rule index corresponding to a rule name.
void * vpUserData
Pointer to user data, if any. Not examined or used by the parser in any way. Presented to the user's ...
uint_fast8_t achar
achar is the type for the parser's alphabet characters.
void vApiDtor(void *vpCtx)
The API component destructor.
void vParserSetRuleCallback(void *vpCtx, aint uiRuleId, parser_callback pfnCallback)
Set a call back function for a specific rule.
void * vpVecAt(void *vpCtx, aint uiIndex)
Get a the indexed vector element. The vector is not altered.
#define XTHROW(ctx, msg)
Exception throw macro.
uint_fast32_t aint
The APG parser's unsigned integer type.
aint uiInputLength
Number of input string alphabet characters.
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.
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 parser's final state.
A structure to describe the type and location of a caught exception.
#define ID_MATCH
indicates a matched phrase parser state on return from parse tree below this node
uintmax_t luint
luint is used to cast integers suitable for the %"PRIuMAX" printf format.
void * vpApiOutputParser(void *vpCtx)
Generate a parser object directly from the specified SABNF grammar.
The data struct passed to each callback function.
void * vpMemCtor(exception *spException)
Construct a memory component.
void * vpUserData
[input/output] User-defined data passed to to the parser in parser_config.
void vParserDtor(void *vpCtx)
Clears the parser component's context and frees all heap memory associated with this parser.
void vUtilApgInfo(void)
Display the current state of apg.h.
aint uiStartRule
Index of the start rule. Any rule in the SABNF grammar may be used as the start rule.
void vApiString(void *vpCtx, const char *cpString, abool bStrict, abool bPppt)
Quicky way to generate a parser from a grammar string.
void vUtilPrintException(exception *spEx)
Prints exception information from an exception structure.
void * vpVecPush(void *vpCtx, void *vpElement)
Adds one element to the end of the array.
void vUtilCurrentWorkingDirectory(void)
Display the current working directory.
void vUtilPrintParserState(parser_state *spState)
Display the parser state in human-readable format to stdout.
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.