Version 6.3
Copyright © 2005 - 2012 Lowell D. Thomas
APG
  … ABNF Parser Generator
All Data Structures Files Functions Variables Typedefs Macros Pages
main.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 
27 #include "main.h"
28 
29 static apg_uint uiCherokeeGen(apg_achar* acpPutItHere);
30 static apg_uint uiGreekGen(apg_achar* acpPutItHere);
31 static apg_uint uiCyrillicGen(apg_achar* acpPutItHere);
32 
48 int main() {
49  char* cpOutput = "WideCharacters.html";
50  apg_uint uiTrace = APG_FALSE;
51  apg_uint uiStats = APG_FALSE;
52  void* vpMemCtx;
53  apg_uint uiTest;
54  apg_uint uiInputLen;
55  apg_uint uiInputSize = 4096;
56  apg_achar acaInput[uiInputSize];
57  APG_CALLBACK saSemantic[RULE_COUNT_WIDEGRAMMAR];
58  CALLBACK_CTX sCallbackCtx;
59  void* vpParserCtx;
60 
61  printf("TYPE SIZES\n");
62  vDisplayTypeSizes(NULL);
63  printf("\n");
64  vpMemCtx = vpMemCtor();
65  MASSERT(vpMemCtx, "memory constructor failed");
66 
67  // get input
68  uiInputLen = 0;
69  uiInputLen += uiCherokeeGen(&acaInput[uiInputLen]);
70  MASSERT((uiInputLen <= uiInputSize), "buffer overrun: input size larger than allocated buffer");
71  uiInputLen += uiGreekGen(&acaInput[uiInputLen]);
72  MASSERT((uiInputLen <= uiInputSize), "buffer overrun: input size larger than allocated buffer");
73  uiInputLen += uiCyrillicGen(&acaInput[uiInputLen]);
74  MASSERT((uiInputLen <= uiInputSize), "buffer overrun: input size larger than allocated buffer");
75 
76  // set up parser
77  vpParserCtx = vpParserCtor(vpParserInit_WideGrammar, vTerminalAlert);
78  MASSERT(vpParserCtx, "parser construction failed");
79  vSemanticInit(saSemantic);
80  vParserAstInitCallbacks(vpParserCtx, saSemantic, NULL); // define the AST for translation
81  if(uiStats){uiParserStatsEnable(vpParserCtx, APG_TRUE);} // enable statistics gathering
82  if(uiTrace){uiParserTraceEnable(vpParserCtx, APG_TRUE);} // enable tracing
83 
84  // parse the examples
85  uiTest = uiParserSyntaxAnalysis(vpParserCtx, 0, acaInput, uiInputLen, NULL);
86  if(uiTest){
87  printf("WideCharacters example completed successfully.\n");
88  printf("To see the results, view the file \"./%s\" in your favorite Web Browser.\n", cpOutput);
89  printf("\n");
90  }
91  vDisplayParserState(NULL, vpParserCtx);
92  MASSERT(uiTest, "parser failed");
93 
94  // translate into HTML file
95  sCallbackCtx.cpFileName = cpOutput;
96  sCallbackCtx.spFile = NULL;
97  uiParserAstTranslate(vpParserCtx, NULL, NULL, (void*)&sCallbackCtx);
98 
99  printf("main: normal exit: EXIT_SUCCESS\n");
100  return EXIT_SUCCESS;
101 }
102 
103 static apg_uint uiPrefix(char* cpPrefix, apg_achar* acpPutItHere){
104  apg_uint uiLen = strlen(cpPrefix);
105  apg_uint i;
106  for(i = 0; i < uiLen; i++){acpPutItHere[i] = (apg_achar)cpPrefix[i];}
107  return uiLen;
108 }
109 static apg_uint uiCherokeeGen(apg_achar* acpPutItHere){
110  apg_uint uiRet = 0;
111  apg_uint i;
112  apg_uint uiBeg, uiEnd;
113  char* cpName = "Cherokee";
114 
115  // example 1
116  uiRet += uiPrefix(cpName, &acpPutItHere[uiRet]);
117  uiBeg = 0x13a0;
118  uiEnd = 0x13af;
119  for(i = uiBeg; i <= uiEnd; i++){acpPutItHere[uiRet++] = i;}
120  acpPutItHere[uiRet++] = (apg_achar)10;
121 
122  // example 2
123  uiRet += uiPrefix(cpName, &acpPutItHere[uiRet]);
124  uiBeg = 0x13b0;
125  uiEnd = 0x13bf;
126  for(i = uiBeg; i <= uiEnd; i++){acpPutItHere[uiRet++] = i;}
127  acpPutItHere[uiRet++] = (apg_achar)10;
128 
129  // example 3
130  uiRet += uiPrefix(cpName, &acpPutItHere[uiRet]);
131  uiBeg = 0x13c0;
132  uiEnd = 0x13cf;
133  for(i = uiBeg; i <= uiEnd; i++){acpPutItHere[uiRet++] = i;}
134  acpPutItHere[uiRet++] = (apg_achar)10;
135 
136  // example 4
137  uiRet += uiPrefix(cpName, &acpPutItHere[uiRet]);
138  uiBeg = 0x13d0;
139  uiEnd = 0x13df;
140  for(i = uiBeg; i <= uiEnd; i++){acpPutItHere[uiRet++] = i;}
141  acpPutItHere[uiRet++] = (apg_achar)10;
142 
143  // example 5
144  uiRet += uiPrefix(cpName, &acpPutItHere[uiRet]);
145  uiBeg = 0x13e0;
146  uiEnd = 0x13ef;
147  for(i = uiBeg; i <= uiEnd; i++){acpPutItHere[uiRet++] = i;}
148  acpPutItHere[uiRet++] = (apg_achar)10;
149 
150  // example 6
151  uiRet += uiPrefix(cpName, &acpPutItHere[uiRet]);
152  uiBeg = 0x13f0;
153  uiEnd = 0x13f4;
154  for(i = uiBeg; i <= uiEnd; i++){acpPutItHere[uiRet++] = i;}
155  acpPutItHere[uiRet++] = (apg_achar)10;
156 
157  return uiRet;
158 }
159 static apg_uint uiGreekGen(apg_achar* acpPutItHere){
160  apg_uint uiRet = 0;
161  apg_uint i;
162  apg_uint uiBeg, uiEnd;
163  char* cpName = "Greek";
164 
165  // example 1
166  uiRet += uiPrefix(cpName, &acpPutItHere[uiRet]);
167  uiBeg = 0x390;
168  uiEnd = 0x39f;
169  for(i = uiBeg; i <= uiEnd; i++){acpPutItHere[uiRet++] = i;}
170  acpPutItHere[uiRet++] = (apg_achar)10;
171 
172  // example 2
173  uiRet += uiPrefix(cpName, &acpPutItHere[uiRet]);
174  uiBeg = 0x3a0;
175  uiEnd = 0x3af;
176  for(i = uiBeg; i <= uiEnd; i++){acpPutItHere[uiRet++] = i;}
177  acpPutItHere[uiRet++] = (apg_achar)10;
178 
179  // example 3
180  uiRet += uiPrefix(cpName, &acpPutItHere[uiRet]);
181  uiBeg = 0x3b0;
182  uiEnd = 0x3bf;
183  for(i = uiBeg; i <= uiEnd; i++){acpPutItHere[uiRet++] = i;}
184  acpPutItHere[uiRet++] = (apg_achar)10;
185 
186  // example 4
187  uiRet += uiPrefix(cpName, &acpPutItHere[uiRet]);
188  uiBeg = 0x3c0;
189  uiEnd = 0x3cf;
190  for(i = uiBeg; i <= uiEnd; i++){acpPutItHere[uiRet++] = i;}
191  acpPutItHere[uiRet++] = (apg_achar)10;
192 
193  // example 5
194  uiRet += uiPrefix(cpName, &acpPutItHere[uiRet]);
195  uiBeg = 0x3d0;
196  uiEnd = 0x3df;
197  for(i = uiBeg; i <= uiEnd; i++){acpPutItHere[uiRet++] = i;}
198  acpPutItHere[uiRet++] = (apg_achar)10;
199 
200  // example 6
201  uiRet += uiPrefix(cpName, &acpPutItHere[uiRet]);
202  uiBeg = 0x3e0;
203  uiEnd = 0x3ef;
204  for(i = uiBeg; i <= uiEnd; i++){acpPutItHere[uiRet++] = i;}
205  acpPutItHere[uiRet++] = (apg_achar)10;
206 
207  return uiRet;
208 }
209 static apg_uint uiCyrillicGen(apg_achar* acpPutItHere){
210  apg_uint uiRet = 0;
211  apg_uint i;
212  apg_uint uiBeg, uiEnd;
213  char* cpName = "Cyrillic";
214 
215  // example 1
216  uiRet += uiPrefix(cpName, &acpPutItHere[uiRet]);
217  uiBeg = 0x400;
218  uiEnd = uiBeg + 0xf;
219  for(i = uiBeg; i <= uiEnd; i++){acpPutItHere[uiRet++] = i;}
220  acpPutItHere[uiRet++] = (apg_achar)10;
221 
222  // example 2
223  uiRet += uiPrefix(cpName, &acpPutItHere[uiRet]);
224  uiBeg = 0x410;
225  uiEnd = uiBeg + 0xf;
226  for(i = uiBeg; i <= uiEnd; i++){acpPutItHere[uiRet++] = i;}
227  acpPutItHere[uiRet++] = (apg_achar)10;
228 
229  // example 3
230  uiRet += uiPrefix(cpName, &acpPutItHere[uiRet]);
231  uiBeg = 0x420;
232  uiEnd = uiBeg + 0xf;
233  for(i = uiBeg; i <= uiEnd; i++){acpPutItHere[uiRet++] = i;}
234  acpPutItHere[uiRet++] = (apg_achar)10;
235 
236  // example 4
237  uiRet += uiPrefix(cpName, &acpPutItHere[uiRet]);
238  uiBeg = 0x430;
239  uiEnd = uiBeg + 0xf;
240  for(i = uiBeg; i <= uiEnd; i++){acpPutItHere[uiRet++] = i;}
241  acpPutItHere[uiRet++] = (apg_achar)10;
242 
243  // example 5
244  uiRet += uiPrefix(cpName, &acpPutItHere[uiRet]);
245  uiBeg = 0x440;
246  uiEnd = uiBeg + 0xf;
247  for(i = uiBeg; i <= uiEnd; i++){acpPutItHere[uiRet++] = i;}
248  acpPutItHere[uiRet++] = (apg_achar)10;
249 
250  // example 6
251  uiRet += uiPrefix(cpName, &acpPutItHere[uiRet]);
252  uiBeg = 0x450;
253  uiEnd = uiBeg + 0xf;
254  for(i = uiBeg; i <= uiEnd; i++){acpPutItHere[uiRet++] = i;}
255  acpPutItHere[uiRet++] = (apg_achar)10;
256 
257  return uiRet;
258 }
vDisplayParserState
void vDisplayParserState(FILE *spOut, void *vpParserCtx)
Definition: Utilities.c:373
uiParserAstTranslate
apg_uint uiParserAstTranslate(void *vpCtx, APG_CALLBACK *pfnRuleCallbacks, APG_CALLBACK *pfnUdtCallbacks, void *vpData)
Definition: Parser.c:361
vParserAstInitCallbacks
void vParserAstInitCallbacks(void *vpCtx, APG_CALLBACK *spRuleCallbacks, APG_CALLBACK *spUdtCallbacks)
Definition: Parser.c:316
uiParserStatsEnable
apg_uint uiParserStatsEnable(void *vpCtx, apg_uint uiEnable)
Definition: Parser.c:403
apg_uint
unsigned int apg_uint
Definition: Apg.h:169
APG_CALLBACK
apg_uint(* APG_CALLBACK)(APG_CBDATA *spData)
Definition: Apg.h:515
vpMemCtor
void * vpMemCtor()
Definition: Memory.c:77
APG_TRUE
#define APG_TRUE
Definition: Apg.h:187
MASSERT
#define MASSERT(cond, msg)
Definition: main.cpp:33
apg_achar
unsigned char apg_achar
Definition: Apg.h:183
main
int main(int argc, char **argv)
Definition: main.c:115
vpParserCtor
void * vpParserCtor(void *vpParserInit, PFN_ALERT pfnAlertHandler)
Definition: Parser.c:58
APG_FALSE
#define APG_FALSE
Definition: Apg.h:190
uiParserTraceEnable
apg_uint uiParserTraceEnable(void *vpCtx, apg_uint uiEnable)
Definition: Parser.c:452
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
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.