-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmlParser.cpp
476 lines (438 loc) · 17.6 KB
/
xmlParser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//---------------------------------------------------------------------------
#include "xmlParser.h"
#include "macros.h"
//==============================================================================
xmlParser::xmlParser(FILE * fp)
//
{
phome = 0; // External data structure tag
Lx.SetFile(fp); // Point the lexer at it
Lx.SetNFlag(false); // Tune lexer to *not* recognise numbers
// And away we go...
enum loctok {t0=0,t1,t2,t3,t4,t5} toktyp;
struct duple {int ns,ac;} next;
duple table[5][t5+1] =
// Incident symbol // Next
// 0 1 2 3 4 5 // state
{{{X, 1},{1, 0},{E, 0},{E, 0},{0, 2},{E, 0}}, // 0
{{E, 0},{E, 0},{2, 3},{E, 0},{1, 2},{E, 0}}, // 1
{{E, 0},{E, 0},{3, 4},{4, 0},{2, 2},{E, 0}}, // 2
{{E, 0},{E, 0},{3, 4},{4, 0},{3, 2},{E, 0}}, // 3
{{X, 1},{E, 0},{E, 0},{E, 0},{4, 2},{E, 0}}}; // 4
for(int state=0;;) {
Lx.GetTok(Td); // Get the next token...
if (Td.t==Lex::Sy_EOR) continue; // Chuck away any newlines
if (Lx.IsError(Td)) problem = true;
switch (Td.t) { // Map to array index
case Lex::Sy_LT : toktyp = t0; break;
case Lex::Xy_sdcl : toktyp = t1; break;
case Lex::Xy_edcl : toktyp = t3; break;
case Lex::Xy_scmt : toktyp = t4; break;
default : toktyp = t5; break;
}
if (Lex::IsStr(Td.t)) toktyp = t2; // Reduction functions
// Override the reduction functions ?
next = table[state][toktyp]; // Make the transition
switch (next.ac) { // Post-transition (exit) actions
case 0 : break;
case 1 : Lx.push_back();
problem = !StartDocument(phome,xmlname,attr);
if (problem) break;
Element();
attr.clear();
break;
case 2 : comments = Lx.SkipTo(Lx.Xy_ecmt);
Comments(phome,comments);
comments.clear();
break;
case 3 : xmlname=Td.s;
break;
case 4 : Lx.push_back();
Attr();
break;
default : break;
}
switch (state=next.ns) {
case X : goto out; // Exit
case R : goto out; // Return
case E : problem = true; // Error
break;
}
if (problem==true) { // May be set elsewhere
Error(phome,Td.l,Td.c,Td.s);
goto out;
}
}
out: return;
}
//------------------------------------------------------------------------------
xmlParser::~xmlParser()
//
{
}
//------------------------------------------------------------------------------
bool xmlParser::CDATA(const void * p,const unsigned & type,const string & s)
{
printf("\n==============================================\n");
printf("xmlParser::CDATA() SHOULD BE OVERLOADED\n");
printf("Data structure tag : 0x%#08p\n",p);
printf("CDATA type : %u\n",type);
printf("CDATA string : %s\n",s.c_str());
printf("\n==============================================\n");
return true;
}
//------------------------------------------------------------------------------
bool xmlParser::Comments(const void * p,const string & s)
{
printf("\n==============================================\n");
printf("xmlParser::Comments() SHOULD BE OVERLOADED\n");
printf("Data structure tag : 0x%#08p\n",p);
printf("comment string : %s\n",s.c_str());
printf("\n==============================================\n");
return true;
}
//------------------------------------------------------------------------------
bool xmlParser::EndDocument(const void * p)
{
printf("\n==============================================\n");
printf("xmlParser::EndDocument() SHOULD BE OVERLOADED\n");
printf("Data structure tag : 0x%#08p\n",p);
printf("\n==============================================\n");
return true;
}
//------------------------------------------------------------------------------
bool xmlParser::EndElement(const void * p,const string & s)
{
printf("\n==============================================\n");
printf("xmlParser::EndElement() SHOULD BE OVERLOADED\n");
printf("Data structure tag : 0x%#08p\n",p);
printf("Element name : %s\n",s.c_str());
printf("\n==============================================\n");
return true;
}
//------------------------------------------------------------------------------
bool xmlParser::Error(const void * p,const unsigned & r,const unsigned & c,
const string & s)
{
printf("\n==============================================\n");
printf("xmlParser::Error() SHOULD BE OVERLOADED\n");
printf("Data structure tag : 0x%#08p\n",p);
printf("Row, col : %u,%u\n",r,c);
printf("(Last) symbol token : %s\n",s.c_str());
printf("\n==============================================\n");
return true;
}
//------------------------------------------------------------------------------
bool xmlParser::JSON(const void * p,const string & s,
const vector<pair<string,string> > & vps)
{
printf("\n==============================================\n");
printf("xmlParser::JSON() SHOULD BE OVERLOADED\n");
printf("Data structure tag : 0x%#08p\n",p);
printf("Origonal string : %s\n",s.c_str());
printf("JSON elements\n");
if (vps.empty()) printf(" .. there are none\n");
for(unsigned i=0;i<vps.size();i++)
printf("%s : %s\n",vps[i].first.c_str(),vps[i].second.c_str());
printf("\n==============================================\n");
return true;
}
//------------------------------------------------------------------------------
bool xmlParser::SetPtr(const void * p)
{
phome = const_cast<void *>(p);
return true;
}
//------------------------------------------------------------------------------
bool xmlParser::StartDocument(const void * p,const string & s,
const vector<pair<string,string> > & vps)
{
printf("\n==============================================\n");
printf("xmlParser::StartDocument() SHOULD BE OVERLOADED\n");
printf("Data structure tag : 0x%#08p\n",p);
printf("Document name : %s\n",s.c_str());
printf("Attributes\n");
if (vps.empty()) printf(" .. there are none\n");
for(unsigned i=0;i<vps.size();i++)
printf("%s = %s\n",vps[i].first.c_str(),vps[i].second.c_str());
printf("\n==============================================\n");
return true;
}
//------------------------------------------------------------------------------
bool xmlParser::StartElement(const void * p,const string & s,
const vector<pair<string,string> > & vps)
{
printf("\n==============================================\n");
printf("xmlParser::StartElement() SHOULD BE OVERLOADED\n");
printf("Data structure tag : 0x%#08p\n",p);
printf("Element name : %s\n",s.c_str());
printf("Attributes\n");
if (vps.empty()) printf(" .. there are none\n");
for(unsigned i=0;i<vps.size();i++)
printf("%s = %s\n",vps[i].first.c_str(),vps[i].second.c_str());
printf("\n==============================================\n");
return true;
}
//==============================================================================
void xmlParser::Attr()
{
// And away we go...
enum loctok {t0=0,t1,t2,t3,t4,t5} toktyp;
struct duple {int ns,ac;} next;
duple table[5][t5+1] =
// Incident symbol // Next
// 0 1 2 3 4 5 // state
{{{1, 1},{E, 0},{E, 0},{0, 2},{E, 0},{E, 0}}, // 0
{{E, 0},{2, 0},{E, 0},{1, 2},{R, 3},{R, 3}}, // 1
{{E, 0},{E, 0},{R, 4},{2, 2},{E, 0},{3, 0}}, // 2
{{4, 4},{E, 0},{4, 4},{3, 2},{E, 0},{R, 0}}, // 3
{{E, 0},{E, 0},{E, 0},{4, 2},{E, 0},{R, 0}}}; // 4
for(int state=0;;) {
Lx.GetTok(Td); // Get the next token...
if (Td.t==Lex::Sy_EOR) continue; // Chuck away any newlines
if (Lx.IsError(Td)) problem = true;
switch (Td.t) { // Map to array index
case Lex::Sy_STR : toktyp = t0; break;
case Lex::Sy_AS : toktyp = t1; break;
case Lex::Sy_dqut : toktyp = t2; break;
case Lex::Xy_scmt : toktyp = t3; break;
case Lex::Sy_squt : toktyp = t5; break;
default : toktyp = t4; break;
}
// if (Lex::IsStr(Td.t)) toktyp = t0; // Reduction functions
// Override the reduction functions
next = table[state][toktyp]; // Make the transition
switch (next.ac) { // Post-transition (exit) actions
case 0 : break;
case 1 : attr.push_back(pair<string,string>(Td.s,string()));
break;
case 2 : comments = Lx.SkipTo(Lx.Xy_ecmt);
Comments(phome,comments);
comments.clear();
break;
case 3 : Lx.push_back();
break;
case 4 : attr.back().second = Td.s;
break;
default : break;
}
switch (state=next.ns) {
case X : goto out; // Exit
case R : goto out; // Return
case E : problem = true; // Error
break;
}
if (problem==true) { // May be set elsewhere
Error(phome,Td.l,Td.c,Td.s);
goto out;
}
}
out: return;
}
//------------------------------------------------------------------------------
void xmlParser::Dump()
{
FILE * fp = stdout;
fprintf(fp,"xmlParser+++++++++++++++++++++++++++++++++++++++++\n");
fprintf(fp,"Element name stack\n");
if (elename.empty()) fprintf(fp," .. is empty\n");
WALKVECTOR(string,elename,i) fprintf(fp," .. %s\n",(*i).c_str());
fprintf(fp,"Attributes\n");
if (attr.empty()) fprintf(fp," .. there are none\n");
for(unsigned i=0;i<attr.size();i++)
fprintf(fp," .. %s = %s\n",attr[i].first.c_str(),attr[i].second.c_str());
fprintf(fp,"JSON\n");
if (json.empty()) fprintf(fp," .. there are none\n");
for(unsigned i=0;i<json.size();i++)
fprintf(fp," ..%s : %s\n",json[i].first.c_str(),json[i].second.c_str());
fprintf(fp,"JSON string : %s\n",jsonstr.c_str());
fprintf(fp,"xmlname : %s\n",xmlname.c_str());
fprintf(fp,"cdata : %s\n",cdata.c_str());
fprintf(fp,"comments : %s\n",comments.c_str());
fprintf(fp,"problem : %s\n",problem ? "TRUE" : "FALSE");
fprintf(fp,"phome : 0x%#08p\n",phome);
fprintf(fp,"xmlParser-----------------------------------------\n");
}
//------------------------------------------------------------------------------
void xmlParser::Element()
{
//printf("\nIN xmlParser::Element()\n\n");
// And away we go...
enum loctok {t0=0,t1,t2,t3,t4,t5,t6,t7,t8} toktyp;
struct duple {int ns,ac;} next;
duple table[9][t8+1] =
// Incident symbol // Next
// 0 1 2 3 4 5 6 7 8 // state
{{{1, 0},{E, 0},{E, 0},{E, 0},{E, 0},{E, 0},{0, 1},{E, 0},{E, 0}}, // 0
{{E, 0},{2, 2},{E, 0},{E, 0},{E, 0},{E, 0},{1, 1},{E, 0},{E, 0}}, // 1
{{E, 0},{3, 4},{4, 3},{R, 5},{E, 0},{E, 0},{2, 1},{E, 0},{E, 0}}, // 2
{{E, 0},{3, 4},{4, 3},{R, 5},{E, 0},{E, 0},{3, 1},{E, 0},{E, 0}}, // 3
{{5, 7},{6, 8},{E, 0},{E, 0},{4, 6},{7, 0},{4, 1},{E, 0},{E, 0}}, // 4
{{5, 7},{E, 0},{E, 0},{E, 0},{5, 9},{7, 0},{5, 1},{X,11},{E, 0}}, // 5
{{E, 0},{E, 0},{E, 0},{E, 0},{E, 0},{7, 0},{6, 1},{E, 0},{E, 0}}, // 6
{{E, 0},{8,12},{E, 0},{E, 0},{E, 0},{E, 0},{7, 1},{E, 0},{E, 0}}, // 7
{{E, 0},{E, 0},{R, 0},{E, 0},{E, 0},{E, 0},{8, 1},{E, 0},{E, 0}}}; // 8
for(int state=0;;) {
Lx.GetTok(Td); // Get the next token...
if (Td.t==Lex::Sy_EOR) continue; // Chuck away any newlines
if (Lx.IsError(Td)) problem = true;
switch (Td.t) { // Map to array index
case Lex::Sy_LT : toktyp = t0; break;
case Lex::Sy_dqut : toktyp = t1; break;
case Lex::Sy_STR : toktyp = t1; break;
case Lex::Sy_GT : toktyp = t2; break;
case Lex::Xy_eel0 : toktyp = t3; break;
case Lex::Xy_scdt : toktyp = t4; break;
case Lex::Xy_eel1 : toktyp = t5; break;
case Lex::Xy_scmt : toktyp = t6; break;
case Lex::Sy_EOF : toktyp = t7; break;
default : toktyp = t8; break;
}
// if (Lex::IsStr(Td.t)) toktyp = t0; // Reduction functions
// Override the reduction functions
next = table[state][toktyp]; // Make the transition
//printf("next,action = %d,%d\n",next.ns,next.ac);
switch (next.ac) { // Post-transition (exit) actions
case 0 : break;
case 1 : comments = Lx.SkipTo(Lex::Xy_ecmt);
Comments(phome,comments);
comments.clear();
break;
case 2 : elename.push_back(Td.s);
break;
case 3 : problem = !StartElement(phome,elename.back(),attr);
attr.clear();
break;
case 4 : Lx.push_back();
Attr();
break;
case 5 : problem = !StartElement(phome,elename.back(),attr);
if (problem) break;
attr.clear();
problem = !EndElement(phome,elename.back());
elename.pop_back();
break;
case 6 : cdata = Lx.SkipTo(Lex::Xy_ecdt);
cdata.erase(cdata.size()-3,cdata.size()); // Remove the "]]>"
problem = !CDATA(phome,unsigned(1),cdata);
cdata.clear();
break;
case 7 : Lx.push_back();
Element();
break;
case 8 : jsonstr = Td.s + Lx.SkipTo(Lex::Xy_eel1);
Td.Dump();
{ int l_ = Td.l; // Get the line and column offsets right
int c_;
problem = Json(c_);
Td.l = l_;
Td.c += c_;
}
problem = !JSON(phome,jsonstr,json);
jsonstr.clear();
json.clear();
break;
case 9 : cdata = Lx.SkipTo(Lex::Xy_ecdt);
problem = !CDATA(phome,unsigned(2),cdata);
cdata.clear();
break;
case 11 : problem = !EndElement(phome,elename.back());
if (problem) break;
problem = !EndDocument(phome);
break;
case 12 : if (elename.back()!=Td.s) problem=true;
problem = !EndElement(phome,elename.back());
elename.pop_back();
break;
default : break;
}
switch (state=next.ns) {
case X : goto out; // Exit
case R : goto out; // Return
case E : problem = true; // Error
break;
}
if (problem==true) { // May be set elsewhere
Error(phome,Td.l,Td.c,Td.s);
goto out;
}
}
out:
//printf("\nOUT xmlParser::Element()\n\n");
return;
}
//------------------------------------------------------------------------------
bool xmlParser::Json(int & rc)
// A sort of optional sub-parse......
// This is called to parse a string that may or may not be JSON.
// It has its own, local lexer (Lx2) which operates on the class-global string
// "jsonstr". IF the string can be corectly parsed as JSON, the "json" class-
// global structure is loaded.
// THIS IS A NAIVE STRUCTURE THAT ASSUMES ALL JASON is of the form
// "xxx" : 999
// It won't handle anything else
// We DO NOT call Error from here, because the local lexer coordinates refer
// to the string jsonstring, not the parent file.
{
//printf("\nIN xmlParser::Json()\n\n");
// And away we go...
enum loctok {t0=0,t1,t2,t3,t4,t5} toktyp;
struct duple {int ns,ac;} next;
duple table[4][t5+1] =
// Incident symbol // Next
// 0 1 2 3 4 5 // state
{{{1, 1},{E, 0},{1, 1},{E, 0},{1, 2},{E, 0}}, // 0
{{E, 0},{2, 0},{E, 0},{E, 0},{2, 2},{E, 0}}, // 1
{{E, 0},{E, 0},{3, 3},{E, 0},{3, 2},{E, 0}}, // 2
{{E, 0},{E, 0},{E, 0},{0, 0},{E, 0},{R, 4}}}; // 3
Lex Lx2; // Need a local lexer
Lx2.SetFile(jsonstr); // Point the lexer at the JSON string
for(int state=0;;) {
Lx2.GetTok(Td); // Get the next token...
rc = Td.c;
//Td.Dump();
//Dump();
if (Td.t==Lex::Sy_EOR) continue; // Chuck away any newlines
if (Lx2.IsError(Td)) problem = true;
switch (Td.t) { // Map to array index
case Lex::Sy_dqut : toktyp = t0; break;
case Lex::Sy_col : toktyp = t1; break;
case Lex::Sy_cmma : toktyp = t3; break;
case Lex::Xy_scmt : toktyp = t4; break;
default : toktyp = t5; break;
}
if (Lex::IsStr(Td.t)) toktyp = t2; // Reduction functions
// Override the reduction functions
next = table[state][toktyp]; // Make the transition
//printf("next,action = %d,%d\n",next.ns,next.ac);
switch (next.ac) { // Post-transition (exit) actions
case 0 : break;
case 1 : json.push_back(pair<string,string>(Td.s,string()));
break;
case 2 : comments = Lx.SkipTo(Lex::Xy_ecmt);
Comments(phome,comments);
comments.clear();
break;
case 3 : json.back().second = Td.s;
break;
case 4 : Lx.push_back();
break;
default : break;
}
switch (state=next.ns) {
case X : return false; // Exit
case R : return false; // Return
case E : problem = true; // Error
goto out;
}
if (problem==true) { // May be set elsewhere
// Error(phome,Td.l,Td.c,Td.s); // DON'T call error here 'cos column is wrong
goto out;
}
}
out :
json.clear(); // Whatever, it's broken.
//printf("\nOUT xmlParser::Json()\n - error exit: \n\n");
return true;
}
//------------------------------------------------------------------------------