Version 6.3
Copyright © 2005 - 2012 Lowell D. Thomas
APG
  … ABNF Parser Generator
All Data Structures Files Functions Variables Typedefs Macros Pages
UdtLibDemo.c
Go to the documentation of this file.
1 /*******************************************************************************
2  APG Version 6.3
3  Copyright (C) 2005 - 2012 Lowell D. Thomas, all rights reserved
4 
5  author: Lowell D. Thomas
6  email: lowell@coasttocoastresearch.com
7  website: http://www.coasttocoastresearch.com
8 
9  This program is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 2 of the License, or
12  (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program. If not, see
21  <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
22  or write to the Free Software Foundation, Inc.,
23  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 *******************************************************************************/
25 
26 #include "main.h"
27 #include "UdtLibGrammar.h"
30 // NOTE: error handling is done with the statement:
31 // if(error){vTerminalAlert(__LINE__, __FILE__);}
32 // prints line and file where error was detected and exits
33 
39 void vDemoUdts(){
40  apg_uint uiTrace = APG_FALSE;
41  apg_uint uiStats = APG_FALSE;
42  apg_uint uiBufferSize = 1024;
43  char caInputString[uiBufferSize];
44  apg_achar acaParserString[uiBufferSize];
45  apg_uint uiStrLen;
46  apg_uint uiTest;
47  apg_uint i = 0;
48  APG_PARSER_STATE sState;
49  apg_uint uiaRuleAstNodes[RULE_COUNT_UDTLIBGRAMMAR];
50  apg_uint uiaUdtAstNodes[UDT_COUNT_UDTLIBGRAMMAR];
51  APG_CALLBACK saUdtSyntaxCallbacks[UDT_COUNT_UDTLIBGRAMMAR];
52  void* vpParser = NULL;
53  char* cpOutFile = "UdtLib.output";
54  FILE* spOut = NULL;
55  apg_uint uiStatsBufferSize = 10000;
56  char caStatsBuffer[uiStatsBufferSize];
57  APG_PARSER_STATS* spStats = (APG_PARSER_STATS*)&caStatsBuffer[0];
58 
59  // redirects output
60  printf("%s - output redirect file for Demonstration UdtLib\n", cpOutFile);
61  spOut = fopen(cpOutFile, "w");
62  if(!spOut){vTerminalAlert(__LINE__, __FILE__);}
63 
64  fprintf(spOut, "*** LICENSE NOTICE ***\n");
65  vLicenseNotice(spOut);
66 
67  fprintf(spOut, "\n*** DEMO UDTLIB ***\n");
68  fprintf(spOut, " Tests all functions in the UDT library &\n");
69  fprintf(spOut, " demonstrates how they are implemented in an application.\n");
70 
71  // display the integer sizes
72  fprintf(spOut, "\n*** TYPE SIZES ***\n");
73  vDisplayTypeSizes(spOut);
74 
75  // display the grammar information
76  fprintf(spOut, "\n*** GRAMMAR INFORMATION ***\n");
77  vDisplayGrammarInfo(spOut, vpParserInit_UdtLibGrammar);
78 
79  // construct the parser
80  vpParser = vpParserCtor(vpParserInit_UdtLibGrammar, vTerminalAlert);
81  if(!vpParser){vTerminalAlert(__LINE__, __FILE__);}
82 
83  // set up for parsing statistics
84  uiStats = uiParserStatsEnable(vpParser, APG_TRUE);
85 
86  // set up for UDT syntax callbacks
87  // NOTE: tests all UdtLib functions
88  saUdtSyntaxCallbacks[UDT_UDTLIBGRAMMAR_E_OWSP] = (APG_CALLBACK)uiUdtLib_e_owsp;
89  saUdtSyntaxCallbacks[UDT_UDTLIBGRAMMAR_U_WSP] = (APG_CALLBACK)uiUdtLib_u_wsp;
90  saUdtSyntaxCallbacks[UDT_UDTLIBGRAMMAR_U_LINEEND] = (APG_CALLBACK)uiUdtLib_u_lineend;
91  saUdtSyntaxCallbacks[UDT_UDTLIBGRAMMAR_E_ANY] = (APG_CALLBACK)uiUdtLib_e_any;
92  saUdtSyntaxCallbacks[UDT_UDTLIBGRAMMAR_U_ALPHANUM] = (APG_CALLBACK)uiUdtLib_u_alphanum;
93  saUdtSyntaxCallbacks[UDT_UDTLIBGRAMMAR_U_ALPHANUM_HYPHEN] = (APG_CALLBACK)uiUdtLib_u_alphanum_hyphen;
94  saUdtSyntaxCallbacks[UDT_UDTLIBGRAMMAR_U_ALPHANUM_UNDER] = (APG_CALLBACK)uiUdtLib_u_alphanum_under;
95  saUdtSyntaxCallbacks[UDT_UDTLIBGRAMMAR_U_DECIMAL] = (APG_CALLBACK)uiUdtLib_u_decnum;
96  saUdtSyntaxCallbacks[UDT_UDTLIBGRAMMAR_U_HEXIDECIMAL] = (APG_CALLBACK)uiUdtLib_u_hexnum;
97  saUdtSyntaxCallbacks[UDT_UDTLIBGRAMMAR_U_QUOTEDSTRING] = (APG_CALLBACK)uiUdtLib_u_quotedstring;
98  saUdtSyntaxCallbacks[UDT_UDTLIBGRAMMAR_U_SINGLEQUOTEDSTRING] = (APG_CALLBACK)uiUdtLib_u_singlequotedstring;
99  saUdtSyntaxCallbacks[UDT_UDTLIBGRAMMAR_U_DOUBLEQUOTEDSTRING] = (APG_CALLBACK)uiUdtLib_u_doublequotedstring;
100  saUdtSyntaxCallbacks[UDT_UDTLIBGRAMMAR_U_CCOMMENT] = (APG_CALLBACK)uiUdtLib_u_c_comment;
101  saUdtSyntaxCallbacks[UDT_UDTLIBGRAMMAR_U_CPPCOMMENT] = (APG_CALLBACK)uiUdtLib_u_cpp_comment;
102  saUdtSyntaxCallbacks[UDT_UDTLIBGRAMMAR_U_SEMICOMMENT] = (APG_CALLBACK)uiUdtLib_u_semi_comment;
103  uiParserSyntaxInitCallbacks(vpParser, NULL, &saUdtSyntaxCallbacks[0]);
104 
105 
106  // set up for AST generation & rule & UDT semantic callbacks
107  for(; i < RULE_COUNT_UDTLIBGRAMMAR; i++){
108  uiaRuleAstNodes[i] = APG_FALSE;
109  }
110  uiaRuleAstNodes[RULE_UDTLIBGRAMMAR_FILE] = APG_TRUE;
111  uiaRuleAstNodes[RULE_UDTLIBGRAMMAR_BLANKLINE] = APG_TRUE;
112  uiaRuleAstNodes[RULE_UDTLIBGRAMMAR_LINE] = APG_TRUE;
113  uiaRuleAstNodes[RULE_UDTLIBGRAMMAR_LINEALPHA] = APG_TRUE;
114  uiaRuleAstNodes[RULE_UDTLIBGRAMMAR_LINEHYPHEN] = APG_TRUE;
115  uiaRuleAstNodes[RULE_UDTLIBGRAMMAR_LINEUNDER] = APG_TRUE;
116  uiaRuleAstNodes[RULE_UDTLIBGRAMMAR_LINEERROR] = APG_TRUE;
117  uiaRuleAstNodes[RULE_UDTLIBGRAMMAR_NUMBER] = APG_TRUE;
118  uiaRuleAstNodes[RULE_UDTLIBGRAMMAR_COMMENT] = APG_TRUE;
119  for(; i < UDT_COUNT_UDTLIBGRAMMAR; i++){
120  uiaUdtAstNodes[i] = (saUdtSyntaxCallbacks[i]) ? APG_TRUE: APG_FALSE;
121  }
122  vParserAstInitNodes(vpParser, &uiaRuleAstNodes[0], &uiaUdtAstNodes[0]);
123 
124 
125  // get the input string
126  memset((void*)&caInputString[0], 0, uiBufferSize);
127 
128  // LF line ending
129  strcat(caInputString, "\n");
130  if(strlen(caInputString) >= uiBufferSize){vTerminalAlert(__LINE__, __FILE__);}
131 
132  // CRLF line ending
133  strcat(caInputString, " \r\n");
134  if(strlen(caInputString) >= uiBufferSize){vTerminalAlert(__LINE__, __FILE__);}
135 
136  // CR line ending
137  strcat(caInputString, " \r");
138  if(strlen(caInputString) >= uiBufferSize){vTerminalAlert(__LINE__, __FILE__);}
139 
140  // semicolon style comment
141  strcat(caInputString, "; semi comment using CR line end\r");
142  if(strlen(caInputString) >= uiBufferSize){vTerminalAlert(__LINE__, __FILE__);}
143 
144  // C++ style comment
145  strcat(caInputString, "// C++ comment using CRLF line end\r\n");
146  if(strlen(caInputString) >= uiBufferSize){vTerminalAlert(__LINE__, __FILE__);}
147 
148  // C style comment
149  strcat(caInputString, "/* C comment using LF line end */\n");
150  if(strlen(caInputString) >= uiBufferSize){vTerminalAlert(__LINE__, __FILE__);}
151 
152  // C style comment spanning multiple lines
153  strcat(caInputString, "/* multi-line C comment \n");
154  if(strlen(caInputString) >= uiBufferSize){vTerminalAlert(__LINE__, __FILE__);}
155  strcat(caInputString, "continuing multi-line C comment \n");
156  if(strlen(caInputString) >= uiBufferSize){vTerminalAlert(__LINE__, __FILE__);}
157  strcat(caInputString, " end of multi-line C comment */\n");
158  if(strlen(caInputString) >= uiBufferSize){vTerminalAlert(__LINE__, __FILE__);}
159 
160  // named line
161  strcat(caInputString, "\n// alphanum named line\n");
162  if(strlen(caInputString) >= uiBufferSize){vTerminalAlert(__LINE__, __FILE__);}
163  strcat(caInputString, "john = \'John Doe\'\n");
164  if(strlen(caInputString) >= uiBufferSize){vTerminalAlert(__LINE__, __FILE__);}
165  strcat(caInputString, "mary-jane = \"Mary Jane\"\n");
166  if(strlen(caInputString) >= uiBufferSize){vTerminalAlert(__LINE__, __FILE__);}
167  if(strlen(caInputString) >= uiBufferSize){vTerminalAlert(__LINE__, __FILE__);}
168  strcat(caInputString, "jimmy_bob = \"Jimmy Bob\"\n");
169  if(strlen(caInputString) >= uiBufferSize){vTerminalAlert(__LINE__, __FILE__);}
170 
171  // numbered line
172  strcat(caInputString, "\n// test decimal numbers\n");
173  if(strlen(caInputString) >= uiBufferSize){vTerminalAlert(__LINE__, __FILE__);}
174  strcat(caInputString, "number = 123456789\n");
175  if(strlen(caInputString) >= uiBufferSize){vTerminalAlert(__LINE__, __FILE__);}
176  strcat(caInputString, "hexi-decimal = 0x1234ABCD\n");
177  if(strlen(caInputString) >= uiBufferSize){vTerminalAlert(__LINE__, __FILE__);}
178  if(strlen(caInputString) >= uiBufferSize){vTerminalAlert(__LINE__, __FILE__);}
179  strcat(caInputString, "multiple_numbers = 123 0xabCD 567 0x1a2A3b4cdEF \n");
180  if(strlen(caInputString) >= uiBufferSize){vTerminalAlert(__LINE__, __FILE__);}
181 
182  strcat(caInputString, "\n");
183  if(strlen(caInputString) >= uiBufferSize){vTerminalAlert(__LINE__, __FILE__);}
184 
185  strcat(caInputString, "; end of test\n");
186  if(strlen(caInputString) >= uiBufferSize){vTerminalAlert(__LINE__, __FILE__);}
187  uiStrLen = strlen(caInputString);
188 
189  // display the input string
190  vDisplayAscii(spOut, caInputString, uiStrLen);
191 
192  // convert the input string to alphabet characters
193  vCharToAChar(&acaParserString[0], &caInputString[0], uiStrLen);
194 
195  if(uiTrace){
196  // set up for tracing the parse tree
197  uiParserTraceEnable(vpParser, APG_TRUE);
198  }
199 
200  // set up for parsing statistics
201  uiStats = uiParserStatsEnable(vpParser, APG_TRUE);
202 
203  // parse the string
204  uiTest = uiParserSyntaxAnalysis(vpParser, RULE_UDTLIBGRAMMAR_FILE, &acaParserString[0], uiStrLen, NULL);
205  if(!uiTest){vTerminalAlert(__LINE__, __FILE__);}
206  uiParserState(vpParser, &sState);
207 
208  // examine the parser state
209  fprintf(spOut, "\n*** PARSER STATE ***\n");
210  vDisplayParserState(spOut, vpParser);
211 
212  // look at the parsing statistics
213  if(uiStats){
214  uiParserStatsGet(vpParser, NULL, &uiTest);
215  if(uiTest <= uiStatsBufferSize){
216  uiParserStatsGet(vpParser, spStats, &uiTest);
217  fprintf(spOut, "\n*** PARSER STATISTICS ***\n");
218  vDisplayOperatorStats(spOut, spStats);
219  vDisplayRuleStats(spOut, spStats, "count");
220  } else{
221  fprintf(spOut, "\n*** NO PARSER STATISTICS: insufficient buffer size: needed: %lu: have: %lu\n",
222  (printf_uint)uiTest, (printf_uint)uiStatsBufferSize);
223  }
224  }
225 
226  // look at the parsed phrases (AST nodes)
227  fprintf(spOut, "\n*** AST NODES AND PHRASES ***\n");
228  vDisplayAstRecords(spOut, vpParser);
229 
230  fprintf(spOut, "\n*** DEMO UDTLIB - test ended successfully ***\n");
231  fclose(spOut);
232 }
vDisplayAscii
void vDisplayAscii(FILE *spOut, const char *cpSrc, apg_uint uiSrcLen)
Definition: Utilities.c:62
vDisplayParserState
void vDisplayParserState(FILE *spOut, void *vpParserCtx)
Definition: Utilities.c:373
uiUdtLib_u_doublequotedstring
apg_uint uiUdtLib_u_doublequotedstring(APG_CBDATA *spData)
Definition: UdtLib.c:561
APG_PARSER_STATS
full set of statistics gathered during parsing, uiParserSyntaxAnalysis()
Definition: Apg.h:591
uiParserStatsEnable
apg_uint uiParserStatsEnable(void *vpCtx, apg_uint uiEnable)
Definition: Parser.c:403
APG_PARSER_STATE
the state of the parser after parsing an input string.
Definition: Apg.h:561
apg_uint
unsigned int apg_uint
Definition: Apg.h:169
APG_CALLBACK
apg_uint(* APG_CALLBACK)(APG_CBDATA *spData)
Definition: Apg.h:515
uiUdtLib_u_alphanum_hyphen
apg_uint uiUdtLib_u_alphanum_hyphen(APG_CBDATA *spData)
Definition: UdtLib.c:434
uiUdtLib_u_lineend
apg_uint uiUdtLib_u_lineend(APG_CBDATA *spData)
Definition: UdtLib.c:320
uiUdtLib_u_semi_comment
apg_uint uiUdtLib_u_semi_comment(APG_CBDATA *spData)
Definition: UdtLib.c:354
uiUdtLib_u_cpp_comment
apg_uint uiUdtLib_u_cpp_comment(APG_CBDATA *spData)
Definition: UdtLib.c:372
vDisplayGrammarInfo
void vDisplayGrammarInfo(FILE *spOut, void *vpHdr)
Definition: Utilities.c:409
vDisplayAstRecords
void vDisplayAstRecords(FILE *spOut, void *vpParserCtx)
Definition: Utilities.c:183
uiParserSyntaxInitCallbacks
apg_uint uiParserSyntaxInitCallbacks(void *vpCtx, APG_CALLBACK *spRuleCallbacks, APG_CALLBACK *spUdtCallbacks)
Definition: Parser.c:187
uiUdtLib_e_any
apg_uint uiUdtLib_e_any(APG_CBDATA *spData)
Definition: UdtLib.c:337
uiUdtLib_e_owsp
apg_uint uiUdtLib_e_owsp(APG_CBDATA *spData)
Definition: UdtLib.c:280
vDisplayOperatorStats
void vDisplayOperatorStats(FILE *spOut, APG_PARSER_STATS *spStats)
Definition: Utilities.c:296
uiUdtLib_u_c_comment
apg_uint uiUdtLib_u_c_comment(APG_CBDATA *spData)
Definition: UdtLib.c:395
APG_TRUE
#define APG_TRUE
Definition: Apg.h:187
vDisplayRuleStats
void vDisplayRuleStats(FILE *spOut, APG_PARSER_STATS *spStats, const char *cpType)
Definition: Utilities.c:338
uiUdtLib_u_hexnum
apg_uint uiUdtLib_u_hexnum(APG_CBDATA *spData)
Definition: UdtLib.c:495
vParserAstInitNodes
void vParserAstInitNodes(void *vpCtx, apg_uint *uipRules, apg_uint *uipUdts)
Definition: Parser.c:283
uiParserState
apg_uint uiParserState(void *vpCtx, APG_PARSER_STATE *spState)
Definition: Parser.c:379
vLicenseNotice
void vLicenseNotice(FILE *spFile)
Definition: Utilities.c:442
apg_achar
unsigned char apg_achar
Definition: Apg.h:183
printf_uint
unsigned long int printf_uint
Definition: ApgUtilities.h:52
vDemoUdts
void vDemoUdts()
Definition: UdtLibDemo.c:39
vpParserCtor
void * vpParserCtor(void *vpParserInit, PFN_ALERT pfnAlertHandler)
Definition: Parser.c:58
uiUdtLib_u_quotedstring
apg_uint uiUdtLib_u_quotedstring(APG_CBDATA *spData)
Definition: UdtLib.c:525
uiUdtLib_u_decnum
apg_uint uiUdtLib_u_decnum(APG_CBDATA *spData)
Definition: UdtLib.c:472
APG_FALSE
#define APG_FALSE
Definition: Apg.h:190
uiUdtLib_u_singlequotedstring
apg_uint uiUdtLib_u_singlequotedstring(APG_CBDATA *spData)
Definition: UdtLib.c:544
uiUdtLib_u_wsp
apg_uint uiUdtLib_u_wsp(APG_CBDATA *spData)
Definition: UdtLib.c:301
uiParserTraceEnable
apg_uint uiParserTraceEnable(void *vpCtx, apg_uint uiEnable)
Definition: Parser.c:452
vCharToAChar
void vCharToAChar(apg_achar *acpAChars, const char *cpChars, apg_uint uiLen)
Definition: Tools.c:79
uiUdtLib_u_alphanum
apg_uint uiUdtLib_u_alphanum(APG_CBDATA *spData)
Definition: UdtLib.c:414
vTerminalAlert
void vTerminalAlert(unsigned int uiLine, const char *cpFile)
Definition: Utilities.c:557
vDisplayTypeSizes
void vDisplayTypeSizes(FILE *spOut)
Definition: Utilities.c:279
uiParserSyntaxAnalysis
apg_uint uiParserSyntaxAnalysis(void *vpCtx, apg_uint uiStartRule, const apg_achar *acpSrc, apg_uint uiSrcLen, void *vpData)
Definition: Parser.c:228
uiUdtLib_u_alphanum_under
apg_uint uiUdtLib_u_alphanum_under(APG_CBDATA *spData)
Definition: UdtLib.c:454
uiParserStatsGet
apg_uint uiParserStatsGet(void *vpCtx, APG_PARSER_STATS *spStats, apg_uint *uipBufferSize)
Definition: Parser.c:422
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/licenses.html or write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.