-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuif.cpp
More file actions
1652 lines (1480 loc) · 63.1 KB
/
uif.cpp
File metadata and controls
1652 lines (1480 loc) · 63.1 KB
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//---------------------------------------------------------------------------
#include "uif.h"
#include "macros.h"
#include "flat.h"
#include "dfprintf.h"
//==============================================================================
//class Node;
const string UIF::s_ = string("");
//vector<UIF::Node *> UIF::Node::NodeVector;
//==============================================================================
UIF::UIF()
{
UIF_root = 0;
Init();
}
//------------------------------------------------------------------------------
UIF::UIF(int argc, char ** argv)
// Constructor from command line arguments
{
UIF_root = 0;
Init();
// Turn everything into one string
for (int i=1;i<argc;i++) fname += (argv[i]+string(" "));
Lx.SetFile(fname); // 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,t6} toktyp;
struct duple {int ns,ac;} next;
duple table[6][t6+1] =
// Incident symbol // Next
// 0 1 2 3 4 5 6 // state
{{{0, 4},{2, 2},{1, 0},{3, 0},{3, 0},{R, 0},{X, X}}, // 0
{{0, 1},{X, X},{X, X},{X, X},{X, X},{X, X},{X, X}}, // 1
{{0, 4},{X, X},{X, X},{X, X},{X, X},{X, X},{X, X}}, // 2
{{0, 4},{5, 2},{4, 0},{0, 5},{0, 5},{R, 5},{X, X}}, // 3
{{0, 1},{X, X},{X, X},{X, X},{X, X},{X, X},{X, X}}, // 4
{{0, 4},{X, X},{X, X},{X, X},{X, X},{X, X},{X, X}}}; // 5
Lex::Sytype cOp = Lex::S_0; // Keep the compiler happy
Node * pBody;
Node * pVari;
Node * pName;
Node * pLabl;
string sName;
int cnt = 0;
// Store command line as comment
pSect->Add(pRecd = pNH->new_Node(0,No_recd,string(argv[0])));
pRecd->Add(pNH->new_Node(0,No_body));
pSect->Add(pRecd = pNH->new_Node(0,No_recd,fname));
pRecd->Add(pNH->new_Node(0,No_body));
for(int state=0;;) {
Lx.GetTok(Td); // Get the next token...
switch (state) { // Pre-transition (entry) actions
case 0 : cOp = Lex::S_00; break; // Null current argument sign
}
// No exceptional cases (EOF in table)
if (Lx.IsError(Td)) problem = true;
switch (Td.t) { // Map to array index
case Lex::Sy_div : toktyp = t2; break;
case Lex::Sy_cmma : toktyp = t3; break;
case Lex::Sy_AS : toktyp = t4; break;
case Lex::Sy_EOF : toktyp = t5; break;
default : toktyp = t6; break;
}
if (Lex::IsStr(Td.t)) toktyp = t0; // Reduction functions
if (Lex::IsOp(Td.t)) toktyp = t1;
if (Td.t==Lex::Sy_div) toktyp = t2; // Override the reduction functions
if (Td.t==Lex::Sy_cmma) toktyp = t3;
next = table[state][toktyp]; // Make the transition
switch (next.ac) { // Post-transition (exit) actions
case 0 : break;
case X : problem = true; break;
case 1 : // New named section
UIF_root->Add(pSect = pNH->new_Node(0,No_sect,s_,Lex::Sy_div));
pSect->Add(pName = pNH->new_Node(Td.c,No_name));
pName->Add(pNH->new_Node(Td.c,No_name,sName=Td.s,Td.t));
cnt = 0; break;
case 2 : // Store single monadic operator
cOp = Td.t; break;
case 4 : // Create a new record; note conditional on variable name
case 5 : {
string s = (next.ac==4) ? Td.s : s_;
pSect->Add(pRecd = pNH->new_Node(Td.l,No_recd));
pRecd->Add(pBody = pNH->new_Node(Td.c,No_body));
pBody->Add(pVari = pNH->new_Node(Td.c,No_vari));
pVari->Add(pNH->new_Node(Td.c,No_name,s,cOp));
pBody->Add(pLabl = pNH->new_Node(Td.c,No_labl));
pLabl->Add(pName = pNH->new_Node(Td.c,No_name,sName,Td.t));
pName->Add(pNH->new_Node(Td.c,No_name,int2str(cnt),Td.t));
cnt++; break;
}
default : break;
}
switch (state=next.ns) {
case X : return;
case R : return;
}
if (problem==true) break; // May be set elsewhere
}
return;
}
//------------------------------------------------------------------------------
UIF::~UIF()
// Recursive destruction of node tree
{
Destroy(this,UIF_root);
delete pNH; // Kill the node-local heap
}
//------------------------------------------------------------------------------
void UIF::Add(string name)
// Adds the contents of a file to the already existing datastructure
{
problem = false; // So far, so good
Lx.SetFile((char *)name.c_str()); // Point the lexer at the input file
// BORLAND BUG: we need the cast???
if (Lx.GetErr()!=Lex::S_0) { // Problem - bomb
errcnt++;
return;
}
//Lx.SetNFlag(false); // Tune lexer to *not* recognise numbers
Lx.SetCChar('\\'); // Set lexer continuation character
// And away we go...
enum loctok {t0=0,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12} toktyp;
struct duple {int ns,ac;} next;
duple table[10][t12+1] =
// Incident symbol // Next
// 0 1 2 3 4 5 6 7 8 9 10 11 12 // state
{{{1, 3},{1, 3},{2, 0},{4, 0},{6, 6},{7,10},{X, X},{0, 7},{0, 0},{0, 8},{1, 3},{X, X},{R, 0}}, // 0
{{X, X},{X, X},{2, 9},{4, 0},{6, 6},{X, X},{X, X},{X, X},{0, 0},{0, 8},{X, X},{X, X},{R, 0}}, // 1
{{3, 4},{3, 4},{X, X},{4, 0},{6, 6},{X, X},{X, X},{X, X},{0, 0},{0, 8},{3, 4},{X, X},{R, 0}}, // 2
{{X, X},{X, X},{X, X},{4, 0},{6, 6},{X, X},{X, X},{X, X},{0, 0},{0, 8},{X, X},{X, X},{R, 0}}, // 3
{{5, 5},{5, 5},{X, X},{X, X},{X, X},{X, X},{X, X},{X, X},{0, 0},{0, 8},{5, 5},{X, X},{R, 0}}, // 4
{{X, X},{X, X},{X, X},{X, X},{6, 6},{X, X},{X, X},{X, X},{0, 0},{0, 8},{X, X},{X, X},{R, 0}}, // 5
{{X, X},{X, X},{X, X},{X, X},{X, X},{X, X},{X, X},{X, X},{0, 0},{0, 8},{X, X},{X, X},{R, 0}}, // 6
{{8, 1},{8, 1},{X, X},{X, X},{X, X},{X, X},{9, 2},{X, X},{X, X},{X, X},{8, 1},{X, X},{X, X}}, // 7
{{X, X},{X, X},{X, X},{X, X},{X, X},{X, X},{9, 0},{X, X},{X, X},{X, X},{X, X},{X, X},{X, X}}, // 8
{{X, X},{X, X},{X, X},{X, X},{X, X},{X, X},{X, X},{X, X},{0, 0},{0, 8},{X, X},{X, X},{R, 0}}}; // 9
Node * pName = 0;
Node * pBody = 0; // Local declares - keep compiler happy
Node * pLabl = 0;
Node * pVari = 0;
Node * pValu = 0;
Node * tmp;
for(int state=0;;) { // And around and around we go ...
Lx.GetTok(Td); // Get the next token...
if (Lx.IsError(Td)) break; // Lexer reports a problem?
switch (state) { // Pre-transition (entry) actions
case 0 : PruneRec(); // Delete unnecessary nodes
RCB();
pSect->Add(pRecd = pNH->new_Node(Td.l,No_recd));
pRecd->Add(pBody = pNH->new_Node(Td.c,No_body));
pBody->Add(pLabl = pNH->new_Node(Td.c,No_labl));
pBody->Add(pVari = pNH->new_Node(Td.c,No_vari));
pBody->Add(pValu = pNH->new_Node(Td.c,No_valu));
break;
case 9 : SCB(true);
break;
}
// No exceptional cases (EOF in table)
switch (Td.t) { // Map to array index
case Lex::Sy_col : toktyp = t2; break;
case Lex::Sy_AS : toktyp = t3; break;
case Lex::Sy_lbrc : toktyp = t4; break;
case Lex::Sy_lsqb : toktyp = t5; break;
case Lex::Sy_rsqb : toktyp = t6; break;
case Lex::Sy_LT : toktyp = t7; break;
case Lex::Sy_EOR : toktyp = t8; break;
case Lex::Sy_cmnt :
case Lex::Sy_semi : toktyp = t9; break;
case Lex::Sy_lrnb : toktyp = t10; break;
case Lex::Sy_EOF : toktyp = t12; break;
default : toktyp = t11; break;
}
if (Lex::IsStr(Td.t)) toktyp = t0; // Reduction functions
if (Lex::IsOp(Td.t)) toktyp = t1;
if (Td.t==Lex::Sy_LT) toktyp = t7; // Override the reduction functions
next = table[state][toktyp]; // Make the transition
switch (next.ac) { // Post-transition (exit) actions
case 0 : break;
case X : problem = true; break;
case 1 : // Create a new named section header
Lx.push_back();
UIF_root->Add(pSect = pNH->new_Node(Td.l,No_sect,s_));
pSect->Add(pName = pNH->new_Node(Td.c,No_name));
pQal(pName); break;
case 2 : // Create a blank section header. Need the name to hold a comment
UIF_root->Add(pSect = pNH->new_Node(Td.l,No_sect));
pSect->Add(pName = pNH->new_Node(Td.c,No_name)); break;
case 3 : // Pull in the first qualified name field - assume it's the
// *variable* field by default
Lx.push_back();
pQal(pVari); break;
case 4 : // Pull in the second qualified name field - here we *know* this
// is the variable field
Lx.push_back();
pQal(pVari); break;
case 5 : // Pull in the third qualified name field - here we *know* this
// is the value field
Lx.push_back();
pQal(pValu); break;
case 6 : // Start of attribute list
pAtl(pRecd); break;
case 7 : // Found a command; remove unnecessary record node
// Lx.push_back();
pSect->Sub(this);
pSect->Add(pRecd = pNH->new_Node(Td.l,No_cmnd));
CmdProc(pRecd);
CCB(); break;
case 8 : // The Great Comment Bodge. What can I say? Sorry.
if (pRecd==0) CmtProc(pSect,pName);
else CmtProc(pRecd,pBody); // GCC chokes if break follows else
break;
case 9 : // *Now* we know which field is which; swap variable and value
tmp = pVari; pVari = pLabl; pLabl = tmp;
pVari->Type(No_vari);
pLabl->Type(No_labl); break;
case 10 : // Start a new section; remove unnecessary record node
PruneRec(); SCB(false); pSect->Sub(this); pRecd = 0; break;
default : break;
}
if ((state=next.ns) == R) { // Legit exit - we're done
PruneRec();
SCB(false); // Report the last section
Lx.SetFile(); // Disconnect lexer
return;
}
if ((state==X)||(problem==true)) { // Error handler triggered
problem = false; // Clear flag
Lx.SkipTo('\n'); // Junk rest of offending record
state = 0; // Reset state
ECB(); // Error callback - and away we go again
}
if (stop) { // Legitimate exit
Lx.SetFile(); // Disconnect lexer
return;
}
}
ECB(); // Punch out the errors
Lx.SetFile(); // Disconnect lexer
return;
}
//------------------------------------------------------------------------------
void UIF::Addx(char * name)
// I can't overload Add(string), because BORLAND doesn't seem to be able to
// correctly resolve a call to Add(0)...
{
if (name!=0) Add(string(name));
}
//------------------------------------------------------------------------------
void UIF::Args()
// Clear preprocessor argument map
{
argMap.clear();
}
//------------------------------------------------------------------------------
void UIF::Args(string s_f)
// Preprocessor argument map defined by a file. Somewhat recursively, we need
// to parse this *inside* this, the parser. The problem is that we can't
// (reasonably) see JNJ from here, so the parsing is somewhat clunky and manual.
{
// Dud file just ignored (for now)
if (!file_readable(s_f.c_str())) return;
UIF * pUIF = new UIF; // Parser for argument file
pUIF->Add(s_f);
Node * root = pUIF->Root();
unsigned nsects = root->leaf.size(); // Count the sections
Node * psect = 0; // Section pointer
unsigned nrecds = 0; // Records in the section
if (nsects < 2) goto out; // Only the blank one present ?
psect = root->leaf[1]; // Grab the first named section
nrecds = psect->leaf.size(); // Count the records
if (nrecds < 2) goto out; // No records, just the section name
// Loop through argument list
for (unsigned recd=1;recd<nrecds;recd++) {
Node * precd = psect->leaf[recd];
if (precd->leaf.empty()) continue; // Record has no body
Node * pbody = precd->leaf[0]; // Body node (probably)
if (pbody->typ != No_body) continue; // Not a body
if (pbody->leaf.empty()) continue; // Body has no fields
Node * pvari = 0; // Now (try to) find {variable,value}
Node * pvalu = 0;
for (unsigned i=0;i<pbody->leaf.size();i++) {
if ((pvari==0)&&(pbody->leaf[i]->typ==No_vari)) pvari = pbody->leaf[i];
if ((pvalu==0)&&(pbody->leaf[i]->typ==No_valu)) pvalu = pbody->leaf[i];
}
if ((pvari==0)||(pvalu==0)) continue;// Not a sensible pair
if (pvari->leaf.empty()) continue; // No variable data
if (pvalu->leaf.empty()) continue; // No value data
// LHS properly labelled?
if (pvari->leaf[0]->qop!=Lex::Sy_hash) continue;
// RHS properly labelled?
// if (pvalu->leaf[0]->qop!=Lex::S_00) continue;
//*************************
// BUG: if the LHS is in double quotes, the opertaor is dqt, not null.
//*************************
// At last! Do it
Args(pvari->leaf[0]->str,pvalu->leaf[0]->str);
// And this, children, is why I wrote JNJ.
}
out: if (pUIF!=0) delete pUIF; // Chuck away the transient parser
}
//------------------------------------------------------------------------------
void UIF::Args(string s_a,string s_d)
// Add an entry to the preprocessor argument map
{
argMap[s_a] = s_d;
}
//------------------------------------------------------------------------------
void UIF::CCB()
// Command callback
{
if (cb.com_cb!=0)cb.com_cb(cb.com_pt,(void *)this,(void *)pRecd);
}
//------------------------------------------------------------------------------
void UIF::Collapse(vector<Node *> * ptmp)
// The expression parser builds - and hands in - a daisy-chain of tokens.
// After each *token* is added, this routine is called on the daisy-chain, to
// see if it can be collapsed in any way by one of the six transforms embodied
// in t123() or t456(). (There is no sinister reason why these are separate;
// it's just that 1,2 and 3 are related and so are 4,5 and 6. The loop here is
// because some transforms enable others, so for each addition of a token (i.e.
// each call of this routine) we try all the transforms again and again until
// nothing happens any more.
{
/*
int count = 0;
fprintf(ofp,"++Pre-collapse++\n");
WALKVECTOR(Node *,(*ptmp),i){
fprintf(ofp,"Node %d ...\n",count++);
(*i)->Dump();
fprintf(ofp,"... end of\n");
}
fprintf(ofp,"Pre-collapse +++\n");
WALKVECTOR(Node *,(*ptmp),i) (*i)->Dump();
fprintf(ofp,"\n"); */
do {} while (t123(ptmp)||t456(ptmp));
/*
fprintf(ofp,"Post-collapse ---\n");
WALKVECTOR(Node *,(*ptmp),i) (*i)->Dump();
fprintf(ofp,"\n");
count = 0;
WALKVECTOR(Node *,(*ptmp),i){
fprintf(ofp,"Node %d ...\n",count++);
(*i)->Dump();
fprintf(ofp,"... end of\n");
}
fprintf(ofp,"--Post-collapse--\n"); */
}
//------------------------------------------------------------------------------
void UIF::CmdProc(Node * pCmnd)
// Command handler
{
// And away we go...
enum loctok {t0=0,t1,t2,t3,t4} toktyp;
struct duple {int ns,ac;} next;
duple table[4][t4+1] =
// Incident symbol // Next
// 0 1 2 3 4 // state
{{{1, 1},{2, 2},{X, X},{R, 0},{X, X}}, // 0
{{X, X},{2, 2},{X, X},{X, X},{X, X}}, // 1
{{3, 3},{3, 3},{3, 3},{R, 0},{X, X}}, // 2
{{X, X},{X, X},{X, X},{R, 0},{X, X}}}; // 3
for(int state=0;;) {
Lx.GetTok(Td); // Get the next token...
// switch (state) { // Pre-transition (entry) actions
// }
if (Td.t == Lex::Sy_EOF) return; // Exceptional case
if (Lx.IsError(Td)) problem = true;
switch (Td.t) { // Map to array index
case Lex::Sy_lrnb : toktyp = t2; break;
case Lex::Sy_GT : toktyp = t3; break;
default : toktyp = t4; break;
}
if (Lex::IsStr(Td.t)) toktyp = t1; // Reduction functions
if (Lex::IsOp(Td.t)) toktyp = t0;
if (Td.t==Lex::Sy_GT) toktyp = t3; // Override the reduction functions
next = table[state][toktyp]; // Make the transition
switch (next.ac) { // Post-transition (exit) actions
case 0 : break;
case X : problem = true; break;
case 1 : // Command starts with an operator
pCmnd->qop = Td.t; break;
case 2 : // Command name
pCmnd->str = Td.s; break;
case 3 : // Command proper is a qualified list
Lx.push_back();
pQal(pCmnd); break;
default : break;
}
switch (state=next.ns) {
case X : return;
case R : return;
}
if (problem==true) break; // May be set elsewhere
}
//string buf = Lx.SkipTo(Lex::Sy_GT); // Pull in command guts
//Lx.SkipTo('\n'); // Junk the EOR
//return buf; // Return the command
return;
}
//------------------------------------------------------------------------------
void UIF::CmtProc(Node * pS, Node * pP)
// Comment processor. This is a bodge, because I forgot to design in anywhere
// to store the comment column address. As it is, if it's a SECTION comment,
// it's shoved into the position field in the section name node, and if it's a
// record, it goes into the record body node. Sorry. Me prat.
// In fact, it's an even bigger bodge:
// If we're here because the comment was recognised from a ';', the comment
// string will be pulled in as the rest of the line.
// If we're here because we got a comment *token* - i.e. Sy_cmnt, the comment
// string will have already been pulled in by the lexer, and it's in the token
{
if (Td.t == Lex::Sy_cmnt) {
pS->str = Td.s;
// Adjust pointer to hold comment start
pP->pos = Td.c - (int)(pS->str).size();
Lx.SkipTo('\n'); // Chuck away superfluous EOR
} else {
pS->str = Lx.SkipTo('\n');
pP->pos = Td.c;
}
return;
}
//------------------------------------------------------------------------------
void UIF::DeBody(UIF * pUIF,Node * p)
// Called on a record node; we chop off the entire subtree, leaving the record
// node behind. This is so that the 'current record' pointer doesn't get
// confused in the UIF body. The subtree will contain most of the memory
// resources of the record, so it's virtually a 'delete' on the record anyway,
// plus we don't have to bugger about deleteing the parent leaf pointer.
{
if (p==0) return; // Probably a problem......
if (p->Type()!=No_recd) return; // Probably a problem......
if (p->leaf.size()!=1) return; // Probably a problem......
if (p->leaf[0]->Type()!=No_body) return; // Oh, for heavens sake
Destroy(pUIF,p->leaf[0]); // Hose the subtree
p->leaf.clear(); // Tidy up
}
//------------------------------------------------------------------------------
void UIF::DefECB(void * pThis,void * p,int id)
// Default error callback
// pThis is the object address, which is n/u in this here default handler
// Never called with id=0, which is jusrt as well, 'cos Node::Dump() is
// expecting a string argument.....
{
FILE * chan = stdout;
fprintf(chan,"\n+------------------------+\n");
fprintf(chan,"UIF default error handler: id %d\n",id);
switch (id) {
case 0 : static_cast<UIF::Node *>(p)->Dump(chan); break;
case 1 : static_cast<UIF *>(p)->Lx.Hst.Dump(chan); break;
default : fprintf(chan,"Unrecognised error identifier\n"); break;
}
fprintf(chan,"\n+------------------------+\n");
}
//------------------------------------------------------------------------------
void UIF::DeNull(Node * p)
// Called by the expression handler; walk the tree rooted on p, and lose any
// null children
{
vector<Node *> vN = p->leaf;
p->leaf.clear();
WALKVECTOR(Node *,vN,i) if ((*i)!=0) p->leaf.push_back(*i);
WALKVECTOR(Node *,p->leaf,i) DeNull(*i);
}
//------------------------------------------------------------------------------
void UIF::Destroy(UIF * pUIF,Node * p)
// Trash one node and its children
{
if (p==0) return;
WALKVECTOR(Node *,p->leaf,i)Destroy(pUIF,*i);
pUIF->pNH->delete_Node(p);
}
//------------------------------------------------------------------------------
void UIF::Dump(string dumpfile)
// Diagnostic pretty print
{
static FILE * df;
df = ofp;
if (!dumpfile.empty()) df = fopen(dumpfile.c_str(),"w");
fprintf(df,"\n-----------------------------------------\n");
fprintf(df,"UIF object dump (%s)\n",fname.c_str());
fprintf(df,"problem = %c\n",problem ? 'T' : 'F');
fprintf(df,"stop = %c\n",stop ? 'T' : 'F');
Td.Dump(df);
fprintf(df,"UIF preprocessor string map - %lu entries:\n", argMap.size());
WALKMAP(string,string,argMap,i) {
fprintf(df,"%10s -> %10s\n",(*i).first.c_str(),(*i).second.c_str());
}
if (UIF_root!=0)UIF_root->Dump(df);
fprintf(df,"\n-----------------------------------------\n");
if (!dumpfile.empty()) fclose(df);
}
//------------------------------------------------------------------------------
void UIF::ECB()
// Error callback
{
errcnt++;
if (cb.err_cb!=0)cb.err_cb(cb.err_pt,(void *)this,1);
}
//------------------------------------------------------------------------------
int UIF::ErrCnt()
// Returns the number of times ECB has been invoked
{
return errcnt;
}
//------------------------------------------------------------------------------
UIF::Node * UIF::Expr()
// Hands out a (properly) formed expression tree
{
enum loctok {t0=0,t1,t2,t3,t4,t5,t6} toktyp;
struct duple {int ns,ac;} next;
duple table[1][t6+1] =
// Incident symbol
// 0 1 2 3 4 5 6
{{{0, 2},{0, 2},{0, 2},{0, 2},{R, 1},{R, 1},{X, X}}}; // 0 Next state
vector<Node *> tmp;
for(int state=0;;) {
Notype No_x;
Lex::tokdat Td;
Lx.GetTok(Td);
// No pre-transition actions
if (Td.t==Lex::Sy_EOF) break;
if (Lx.IsError(Td)) problem = true;
switch (Td.t) {
case Lex::Sy_lrnb : toktyp = t2; break;
case Lex::Sy_rrnb : toktyp = t3; break;
case Lex::Sy_semi : toktyp = t4; break;
case Lex::Sy_rbrc : toktyp = t5; break;
default : toktyp = t6; break;
}
if (Lex::IsStr(Td.t)) toktyp = t0; // Reduction functions
if (Td.t==Lex::Sy_col) toktyp = t1; // ':' - in this context - is an operator
if (Lex::IsOp(Td.t)) toktyp = t1;
// Map Symbol types onto expression types
switch (toktyp) { // This is necessary because Collapse()
case t0 : No_x = No_e_ex; break; // can change these types
case t1 : No_x = No_e_op; break;
case t2 : No_x = No_e_LB; break;
case t3 : No_x = No_e_RB; break;
case t4 : No_x = No_XXXX; break;
case t5 : No_x = No_XXXX; break;
case t6 : No_x = No_XXXX; break;
default : No_x = No_XXXX; break; // Keeping GCC happy
}
next = table[state][toktyp];
switch (next.ac) {
case 0 : break;
case X : problem = true; break;
case 1 : Lx.push_back(); break;
// Time for a rant: I *want* to overload the new() operator to finesse a local
// storage manager for the enode class, but whatever I do, I can't make the
// overloaded new() pick up any constructor apart from the default. And I don't
// want the default, I want the overload in the line below.
case 2 : tmp.push_back(pNH->new_Node(Td.c,No_x,Td.s,Td.t));
Collapse(&tmp); break;
default: break;
}
switch (state=next.ns) {
case X :
case R : WALKVECTOR(Node *,tmp,i)DeNull(*i);
if (!tmp.empty())Tertiaries(tmp[0]);
if (tmp.size()==1) return tmp[0];
problem = true;
if (tmp.empty()) return 0;
return tmp[0];
}
if (problem==true) break;
}
// Never here
return tmp[0];
}
//------------------------------------------------------------------------------
UIF::Node * UIF::FindNode(Node * p,Notype t)
// Given a node address, find the address of the first child of type t
{
WALKVECTOR(Node *,p->leaf,i) if ((*i)->Type()==t) return (*i);
return 0;
}
//------------------------------------------------------------------------------
vector<UIF::Node *> UIF::FindNodes(Node * p,Notype t)
// Given a node address, find the address of all the children of type t
{
vector<Node *> ans;
WALKVECTOR(Node *,p->leaf,i) if ((*i)->Type()==t) ans.push_back(*i);
return ans;
}
//------------------------------------------------------------------------------
void UIF::Init()
{
pNH = new NodeHeap(); // Internal memory manager
problem = false; // No errors yet
stop = false; // 'Stop' flag
Destroy(this,UIF_root); // Kill any old datastructure
UIF_root = pNH->new_Node(); // Initialise root
// Create blank section
UIF_root->Add(pSect = pNH->new_Node(0,No_sect));
pRecd = 0; // "Current record" isn't
// Initialise callback functions
cb.com_cb = 0; // Command....
cb.com_pt = 0;
cb.err_cb = DefECB; // Errors....
cb.err_pt = 0;
cb.rec_cb = 0; // Record....
cb.rec_pt = 0;
cb.sec_cb = 0; // Section....
cb.sec_pt = 0;
ofp = stdout; // Output stream goes to screen
errcnt = 0; // No errors yet
}
//------------------------------------------------------------------------------
void UIF::pAtl(Node * in)
// Given a node, this loads any attribute list nodes
{
// And away we go...
enum loctok {t0=0,t1,t2,t3,t4,t5,t6,t7} toktyp;
struct duple {int ns,ac;} next;
duple table[5][t7+1] =
// Incident symbol // Next
// 0 1 2 3 4 5 6 7 // state
{{{1, 1},{1, 1},{R, 0},{2, 0},{X, X},{X, X},{1, 1},{X, X}}, // 0
{{X, X},{X, X},{R, 0},{2, 0},{0, 0},{4, 4},{X, X},{X, X}}, // 1
{{3, 2},{3, 2},{R, 0},{X, X},{0, 0},{4, 3},{3, 2},{X, X}}, // 2
{{X, X},{X, X},{R, 0},{X, X},{0, 0},{4, 3},{X, X},{X, X}}, // 3
{{X, X},{X, X},{R, 0},{X, X},{0, 0},{X, X},{X, X},{X, X}}}; // 4
Node * pLabl = 0; // Keep the compiler happy
Node * pVari = 0;
Node * pExpr = 0;
Node * pAttr;
Node * tmp;
for(int state=0;;) {
Lx.GetTok(Td); // Get the next token...
switch (state) { // Pre-transition (entry) actions
case 0 : in->Add(pAttr = pNH->new_Node(in->pos,No_attr));
pAttr->Add(pLabl = pNH->new_Node(Td.c,No_labl));
pAttr->Add(pVari = pNH->new_Node(Td.c,No_vari));
pAttr->Add(pExpr = pNH->new_Node(Td.c,No_expr));
break;
}
if (Td.t == Lex::Sy_EOF) return; // Exceptional case
if (Lx.IsError(Td)) problem = true;
switch (Td.t) { // Map to array index
case Lex::Sy_rbrc : toktyp = t2; break;
case Lex::Sy_col : toktyp = t3; break;
case Lex::Sy_semi : toktyp = t4; break;
case Lex::Sy_AS : toktyp = t5; break;
case Lex::Sy_lrnb : toktyp = t6; break;
default : toktyp = t7; break;
}
if (Lex::IsStr(Td.t)) toktyp = t0; // Reduction functions
if (Lex::IsOp(Td.t)) toktyp = t1;
next = table[state][toktyp]; // Make the transition
switch (next.ac) { // Post-transition (exit) actions
case 0 : break;
case X : problem = true; break;
case 1 : // Load a single qualified name as the label
Lx.push_back();
pQal(pLabl); break;
case 2 : // Load a single qualified name as the variable
Lx.push_back();
pQal(pVari); break;
case 3 : // Load an expression
pExpr->Add(Expr()); break;
case 4 : // Load an expression
pExpr->Add(Expr());
// The first string is the variable, not the label
tmp = pVari; pVari = pLabl; pLabl = tmp;
pVari->Type(No_vari); pLabl->Type(No_labl); break;
default : break;
}
switch (state=next.ns) {
case X : return;
case R : return;
}
if (problem==true) break; // May be set elsewhere
}
return;
}
//------------------------------------------------------------------------------
UIF::Node * UIF::pQal(Node * in)
// Given a node, this loads any children with a qualified list
{
// 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
{{{2, 6},{1, 1},{2, 3},{R, 8},{0, 7},{R, 5}}, // 0
{{2, 2},{X, X},{3, 4},{R, 5},{0, 0},{R, 5}}, // 1
{{R, 5},{R, 5},{3, 4},{R, 5},{0, 0},{R, 5}}, // 2
{{X, X},{X, X},{X, X},{4, 0},{X, X},{X, X}}, // 3
{{R, 5},{R, 5},{R, 5},{R, 5},{0, 0},{R, 5}}}; // 4
Node * tmp = 0;
for(int state=0;;) {
Lx.GetTok(Td); // Get the next token...
// No pre-transition (entry) actions
if (Td.t == Lex::Sy_EOF) return in; // Exceptional case
if (Lx.IsError(Td)) problem = true;
switch (Td.t) { // Map to array index
case Lex::Sy_lrnb : toktyp = t2; break;
case Lex::Sy_rrnb : toktyp = t3; break;
case Lex::Sy_cmma : toktyp = t4; break;
default : toktyp = t5; break;
}
if (Lex::IsStr(Td.t)) toktyp = t0;
if (Lex::IsOp(Td.t)) toktyp = t1;
if (Td.t==Lex::Sy_cmma) toktyp = t4; // Override the reduction functions
next = table[state][toktyp]; // Make the transition
switch (next.ac) { // Post-transition (exit) actions
case 0 : break;
case X : problem = true; break;
case 1 : // Create a new Node, holding a name and operator symbol
in->Add(tmp = pNH->new_Node(Td.c,No_name,s_,Td.t)); break;
case 2 : // *Overwrite* the string
tmp->str = Td.s; break;
case 3 : // Found a '(' - add a new child node
Lx.push_back();
in->Add(tmp = pNH->new_Node(Td.c,No_name)); break;
case 4 : // Load the new child node
pQal(tmp); break;
case 5 : // Exit, pursued by a bear
Lx.push_back(); return in;
case 6 : // Qualified name has no operator
in->Add(tmp = pNH->new_Node(Td.c,No_name,Td.s,Td.t)); break;
case 7 : in->Add(pNH->new_Node(Td.c,No_name,s_,Td.t)); break;
case 8 : in->Add(pNH->new_Node(Td.c,No_name,s_,Td.t));
Lx.push_back(); break;
default : break;
}
switch (state=next.ns) {
case X : return in;
case R : return in;
}
if (problem==true) break; // May be set elsewhere
}
return in;
}
//------------------------------------------------------------------------------
void UIF::PruneRec()
// Routine to hack off empty record body, label, value and variable nodes
// There has to be a more elegant way of doing this, but, hey ....
{
if (pRecd==0) return; // Paranoia...
if (pRecd->Type()!=No_recd) return; // Paranoia...
WALKVECTOR(Node *,pRecd->leaf,i) { // The children must be body or attribute
PruneRec2(*i,No_valu); // Kill each type in turn
PruneRec2(*i,No_vari);
PruneRec2(*i,No_labl);
}
// Now loop to kill the body or attribute
// nodes themselves if they're empty
for (uint i=0;i<pRecd->leaf.size();i++) if (pRecd->leaf[i]->leaf.size()==0) {
pNH->delete_Node(pRecd->leaf[i]);
pRecd->leaf.erase(pRecd->leaf.begin()+i);
}
}
//------------------------------------------------------------------------------
void UIF::PruneRec2(Node * p,Notype t)
// Routine to kill off a subtree (of type t) if it's empty
{
WALKVECTOR(Node *,p->leaf,i) if (((*i)->Type()==t)&&((*i)->leaf.size()==0)) {
pNH->delete_Node(*i); // If the node is empty, kill it
p->leaf.erase(i);
break;
}
}
//------------------------------------------------------------------------------
Lex::tokdat & UIF::Query(bool * pe)
// Hand out the status (value) of the last token handed out from the lexer,
// plus an indication of if UIF thinks it's an error (useful when there's a
// problem connecting to input files and so on)
// The rather weird interface to this function is so that you can call it with
// the minimum of infrastructure build in the calling routine.
{
if (pe!=0) *pe = Lx.IsError(Td);
return Td;
}
//------------------------------------------------------------------------------
void UIF::RCB()
// Record callback
{
if (pRecd==0) return; // Suppress the very first one
if (pRecd->Type()==No_cmnd) return; // Don't push a command out as a record
pRecd->Args(this); // Handle any preprocessor substitutions
if (cb.rec_cb!=0)cb.rec_cb(cb.rec_pt,(void *)this,(void *)pRecd);
}
//------------------------------------------------------------------------------
void UIF::Reset()
// Clear and reset all internal data structures
{
Destroy(this,UIF_root);
Init();
}
//------------------------------------------------------------------------------
UIF::Node * UIF::Root()
// The routine that blows holes in any remaining notion of encapsulation. It
// returns the address of the root node, from which you can do what you like.
// The original intention was to provide a name::address map of all the
// section nodes as well. Like in Trondheim, I buggered about with the
// STL::multimap until my head hurt and gave up. Life's too short.
{
return UIF_root;
}
//------------------------------------------------------------------------------
void UIF::Save(string savefile)
// What comes out is pretty much what went in....
{
sf = stdout;
string sv; // Line buffer
if (!savefile.empty()) sf = fopen(savefile.c_str(),"w");
sv.clear(); // Clear line buffer
Save0(sf,UIF_root,sv); // Recurse the tree
if (!savefile.empty()) fclose(sf);
}
//------------------------------------------------------------------------------
void UIF::Save0(FILE * sf,Node * p,string & sv)
// Recursive pretty printer
{
if (p==0) return;
Notype t=p->Type(); // Save some typing
Node * px=NULL; // Scratch node pointer
switch (t) {
// Root: Save each sector in turn
case No_0000 : WALKVECTOR(Node *,p->leaf,i) Save0(sf,(*i),sv); return;
case No_sect : // Find the name
WALKVECTOR(Node *,p->leaf,i) {
if ((px=(*i))->Type()==No_name) {
dprintf(sv,"[");
WALKVECTOR(Node *,(*i)->leaf,j) Save0(sf,(*j),sv);
dprintf(sv,"]");
break;
}
}
// Any comment ?
if (p->str.size()!=0) {
unsigned int x = px->pos;
if (x > sv.size())sv.append(x-sv.size(),' ');
sv.append(1,';');
sv.append(p->str);
//dprintf(sv,"\t\t\t; %s",p->str.c_str());
}
fprintf(sf,"%s\n",sv.c_str());
sv.clear();
// Find the records and commands
WALKVECTOR(Node *,p->leaf,i)
if (((*i)->Type()==No_recd)||((*i)->Type()==No_cmnd))
Save0(sf,(*i),sv);
break;
case No_recd : {
bool fAttr = false;
// Find the body
px = p; // In case there isn't one.
WALKVECTOR(Node *,p->leaf,i) {
if ((px=(*i))->Type()==No_body) Save0(sf,(*i),sv);
if ((*i)->Type()==No_attr) fAttr = true;
}
if (fAttr) {
dprintf(sv," {");
// Find the attribute list (if it exists)
WALKVECTOR(Node *,p->leaf,i) {
if ((*i)->Type()==No_attr) Save0(sf,(*i),sv);
if (i!=p->leaf.begin())
if (i!=(p->leaf.end()-1)) dprintf(sv,"; ");
} // WALKVECTOR
dprintf(sv,"}");
}
// Any comment ?
if (p->str.size()!=0) {
unsigned int x = px->pos;
if (x > sv.size())sv.append(x-sv.size(),' ');
sv.append(1,';');
sv.append(p->str);
}
fprintf(sf,"%s\n",sv.c_str());
sv.clear();
break;
}
case No_cmnt : break;
case No_cmnd : dprintf(sv,"<%s%s>",Lex::Sytype_str[p->qop],p->str.c_str());
fprintf(sf,"%s\n",sv.c_str());
sv.clear();
break;
case No_body : // Find the single label node
WALKVECTOR(Node *,p->leaf,i)
if ((px=(*i))->Type()==No_labl) {
Save0(sf,(*i),sv);
if (px->leaf.size()!=0) dprintf(sv," : ");
}
// Find the single variable node
WALKVECTOR(Node *,p->leaf,i)
if ((*i)->Type()==No_vari) Save0(sf,(*i),sv);
// Find the single value node
WALKVECTOR(Node *,p->leaf,i)
if ((px=(*i))->Type()==No_valu) {
// Only if there is one do we need an "="
if (px->leaf.size()!=0)dprintf(sv," = ");
// And again
Save0(sf,px,sv);
}
break;
case No_attr : // Find the single label node
WALKVECTOR(Node *,p->leaf,i)
if ((px=(*i))->Type()==No_labl) {
Save0(sf,(*i),sv);
if (px->leaf.size()!=0) dprintf(sv," : ");
}
// Find the single variable node
WALKVECTOR(Node *,p->leaf,i)
if ((*i)->Type()==No_vari) Save0(sf,(*i),sv);
// Find the single value node
/* WALKVECTOR(Node *,p->leaf,i)
if ((px=(*i))->Type()==No_valu) {
// Only if there is one do we need an "="
if (px->leaf.size()!=0)dprintf(sv," = ");