Version 7.0
Copyright © 2021 Lowell D. Thomas
APG
… an ABNF Parser Generator
|
Go to the documentation of this file.
117 #include "../../utilities/utilities.h"
119 static char* s_cpDescription =
120 "Illustrate the construction and use of the line parsing object.";
122 static char* s_cppCases[] = {
123 "Display application information.",
124 "Illustrate the use of the lines object for strings of characters.",
125 "Illustrate the use of the linesu object for arrays of 32-bit Unicode code points.",
127 static long int s_iCaseCount = (
long int)(
sizeof(s_cppCases) /
sizeof(s_cppCases[0]));
129 static int iHelp(
void){
132 printf(
"description: %s\n", s_cpDescription);
133 printf(
" usage: ex-api arg\n");
134 printf(
" arg = n, 1 <= n <= %ld\n", s_iCaseCount);
135 printf(
" execute case number n\n");
136 printf(
" arg = anthing else\n");
137 printf(
" print this help screen\n");
139 for(; i < s_iCaseCount; i++){
140 printf(
"case %ld %s\n", (i + 1), s_cppCases[i]);
155 static int iLines() {
156 int iReturn = EXIT_SUCCESS;
157 static void* vpMem = NULL;
158 static void* vpLines = NULL;
159 aint uiBufSize = 1024;
162 "float = sign decimal exponent\n"
163 "sign = [\"+\" / \"-\"]\r\n"
164 "decimal = integer [dot fraction]\n"
165 " / dot fraction\r\n"
166 "integer = 1*%d48-57\n"
168 "fraction = *%d48-57\n"
169 "exponent = [\"e\" esign exp]\r"
170 "esign = [\"+\" / \"-\"]\n\r"
172 line* spLine, *spLineFirst;
173 aint ui, uiChar, uiLine, uiRelIndex;
179 vpLines =
vpLinesCtor(&e, cpGrammar, strlen(cpGrammar));
183 "This example case uses the utilities lines object to parse an SABNF grammar\n"
184 "with multiple types of line endings, including no line ending on the last line,\n"
185 "and iterate over the lines, displaying the line information.\n";
186 printf(
"\n%s", cpHeader);
194 printf(
"\nThe line information from the iterator.\n");
195 printf(
"%12s %12s %12s %12s line\n",
"line index",
"char index",
"line length",
"text length");
198 XTHROW(&e,
"buffer size too small for line");
202 printf(
"%12d %12d %12d %12d %s\n",
209 printf(
"\nThe line information from the array of lines.\n");
210 printf(
"%12s %12s %12s %12s line\n",
"line index",
"char index",
"line length",
"text length");
212 spLine = &spLineFirst[ui];
214 XTHROW(&e,
"buffer size too small for line");
218 printf(
"%12d %12d %12d %12d %s\n",
225 printf(
"\nFind some lines.\n");
226 for(uiChar = 50; uiChar < 10000; uiChar += 50){
228 printf(
"Character %d is out of range (beyond the end of the last line.)\n", (
int)uiChar);
231 printf(
"Character %d is in line %d at relative character offset %d.\n",
232 (
int)uiChar, (
int)uiLine, (
int)uiRelIndex);
238 iReturn = EXIT_FAILURE;
247 static int iLinesu() {
248 int iReturn = EXIT_SUCCESS;
249 static void* vpMem = NULL;
250 static void* vpLinesu = NULL;
251 uint32_t uiaWords[] = {
252 0x000013C2, 0x000013A6, 0x000013D3, 0x00000020, 0x000013A0, 0x0A,
253 0x000013C2, 0x000013F4, 0x000013EB, 0x00000020, 0x0D,
254 0x000013C2, 0x000013A8, 0x000013AB, 0x000013D3, 0x000013B8, 0x0D,
255 0x000013BE, 0x00000020, 0x000013A0, 0x000013B4, 0x0B,
256 0x000013A4, 0x000013C2, 0x000013B6, 0x000013F1, 0x00000020, 0x0D, 0x0A,
257 0x000013A4, 0x000013BE, 0x000013D5, 0x000013BF, 0x2028,
258 0x0000002E, 0x00000020, 0x000013A8, 0x000013E5, 0x000013C1, 0x000013B3, 0x0C,
259 0x00000020, 0x000013A4, 0x000013C3, 0x000013B5, 0x000013CD, 0x000013D7, 0x85,
260 0x000013D9, 0x00000020, 0x000013AC, 0x000013D7, 0x0000002E, 0x2029
262 line_u* spLine, *spLineFirst;
263 aint ui, uj, uiChar, uiLine, uiRelIndex;
269 vpLinesu =
vpLinesuCtor(&e, uiaWords, (
sizeof(uiaWords) /
sizeof(uiaWords[0])));
273 "This example case uses the utilities linesu object to parse an array of \n"
274 "32-bit Unicode code points.\n";
275 printf(
"\n%s", cpHeader);
277 printf(
"\nUnicode recognizes the following line ending characters:\n");
278 printf(
"LF 0x0A Line Feed\n");
279 printf(
"VT 0x0B Vertical Tab\n");
280 printf(
"FF 0x0C Form Feed\n");
281 printf(
"CR 0x0D Carriage Return\n");
282 printf(
"CRLF 0x0D 0x0A Carriage Return, Line Feed pair\n");
283 printf(
"NEL 0x85 Next Line\n");
284 printf(
"LS 0x2028 Line Separator\n");
285 printf(
"PS 0x2029 Paragraph Separator\n");
293 printf(
"\nThe line information from the iterator.\n");
294 printf(
"%12s %12s %12s %12s line\n",
"line index",
"char index",
"line words",
"text words");
296 printf(
"%12d %12d %12d %12d ",
300 printf(
"0x%08X, ", uiaWords[spLine->
uiCharIndex + ui]);
307 printf(
"\nThe line information from the array of lines.\n");
308 printf(
"%12s %12s %12s %12s line\n",
"line index",
"char index",
"line words",
"text words");
310 spLine = &spLineFirst[ui];
311 printf(
"%12d %12d %12d %12d ",
315 printf(
"0x%08X, ", uiaWords[spLine->
uiCharIndex + uj]);
321 printf(
"\nFind some lines.\n");
322 for(uiChar = 10; uiChar < 10000; uiChar += 10){
324 printf(
"Code point %d is out of range (beyond the end of the last line.)\n", (
int)uiChar);
327 printf(
"Code point %d is in line %d at relative code point offset %d.\n",
328 (
int)uiChar, (
int)uiLine, (
int)uiRelIndex);
334 iReturn = EXIT_FAILURE;
350 int main(
int argc,
char **argv) {
353 iCase = atol(argv[1]);
355 if((iCase > 0) && (iCase <= s_iCaseCount)){
356 printf(
"%s\n", s_cppCases[iCase -1]);
aint uiLineIndex
zero-based line number
aint uiLineLength
The number of characters in the line, including the line end characters.
#define XCTOR(e)
This macro will initialize an exception structure and prepare entry to the "try" block.
void * vpLinesCtor(exception *spEx, const char *cpInput, aint uiLength)
The lines object constructor.
void vMemDtor(void *vpCtx)
Destroys a Memory component. Frees all memory allocated.
abool try
True for the try block, false for the catch block.
void * vpLinesuCtor(exception *spEx, const uint32_t *uipInput, aint uiLength)
The linesu object constructor.
line_u * spLinesuNext(void *vpCtx)
Returns the next line from the iterator.
Carries detailed information about the characters and line endings. One for each line in the input gr...
aint uiLinesLength(void *vpCtx)
Returns the number of text characters.
aint uiTextLength
number of Unicode text characters in the line, excluding line end characters
abool bLinesFindLine(void *vpCtx, aint uiOffset, aint *uipLine, aint *uipRelOffset)
Find the line that the given character is in.
#define XTHROW(ctx, msg)
Exception throw macro.
uint_fast32_t aint
The APG parser's unsigned integer type.
int main(int argc, char **argv)
The executable from this main function is the ABNF Parser Generator application, APG.
abool bLinesuFindLine(void *vpCtx, aint uiOffset, aint *uipLine, aint *uipRelOffset)
Find the line that the given integer is in.
A structure to describe the type and location of a caught exception.
uintmax_t luint
luint is used to cast integers suitable for the %"PRIuMAX" printf format.
aint uiLineIndex
The zero-based line index.
line_u * spLinesuFirst(void *vpCtx)
Initialize an iterator over the lines.
aint uiTextLength
The number of characters in the line, excluding the line end characters.
Defines the characteristics of a single line.
void * vpMemCtor(exception *spException)
Construct a memory component.
line * spLinesNext(void *vpCtx)
Returns the next line of text from the iterator.
line * spLinesFirst(void *vpCtx)
Initialize an iterator over the lines.
void vLinesDtor(void *vpCtx)
The lines object destructor.
void vUtilApgInfo(void)
Display the current state of apg.h.
void vLinesuDtor(void *vpCtx)
The linesu object destructor.
void vUtilPrintException(exception *spEx)
Prints exception information from an exception structure.
aint uiLinesuCount(void *vpCtx)
Returns the number of lines.
void vUtilCurrentWorkingDirectory(void)
Display the current working directory.
aint uiLinesuLength(void *vpCtx)
Returns the number of integers in the 32-bit integer array.
aint uiLineLength
number of Unicode characters in the line, including line end characters
aint uiCharIndex
The zero-based index of the first character of the line.
aint uiCharIndex
zero-based index of the first Unicode character of the line
aint uiLinesCount(void *vpCtx)
Returns the number of lines of text.
APG Version 7.0 is licensed under the
2-Clause BSD License,
an Open Source Initiative Approved License.