-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathz06.c
1611 lines (1411 loc) · 47.8 KB
/
z06.c
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
/*@z06.c:Parser:PushObj(), PushToken(), etc.@*********************************/
/* */
/* THE LOUT DOCUMENT FORMATTING SYSTEM (VERSION 3.43) */
/* COPYRIGHT (C) 1991, 2008 Jeffrey H. Kingston */
/* */
/* Jeffrey H. Kingston (jeff@it.usyd.edu.au) */
/* School of Information Technologies */
/* The University of Sydney 2006 */
/* AUSTRALIA */
/* */
/* 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 3, 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, write to the Free Software */
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307 USA */
/* */
/* FILE: z06.c */
/* MODULE: Parser */
/* EXTERNS: InitParser(), Parse() */
/* */
/*****************************************************************************/
#include "externs.h"
#define LEFT_ASSOC 0
#define RIGHT_ASSOC 1
#define PREV_OP 0 /* means an operator was previous */
#define PREV_OBJ 1 /* prev was object not ending in RBR */
#define PREV_RBR 2 /* prev was object ending in RBR */
static OBJECT cross_name; /* name of the cr database */
#define MAX_STACK 250 /* size of parser stacks */
static OBJECT obj_stack[MAX_STACK]; /* stack of objects */
static int otop; /* top of obj_stack */
static OBJECT tok_stack[MAX_STACK]; /* stack of tokens */
static int ttop; /* top of tok_stack */
static int unknown_count; /* no. of unknown symbols */
BOOLEAN InDefinitions; /* TRUE when in definitions */
#if DEBUG_ON
static BOOLEAN debug_now = FALSE; /* TRUE when want to debug */
#endif
/*****************************************************************************/
/* */
/* OBJECT OptimizeCase(x) */
/* */
/* Optimize the @Case expression x, which is known to be of the form */
/* "@BackEnd @Case ...", by evaluating it immediately if its choices */
/* are all literal words or "else". */
/* */
/*****************************************************************************/
static void check_yield(OBJECT y, OBJECT *res_yield, BOOLEAN *all_literals)
{ OBJECT s1, link, z;
Child(s1, Down(y))
;
debug1(DOP, DD, " checkyield(%s)", EchoObject(y));
if( is_word(type(s1)) )
{ if( StringEqual(string(s1), BackEnd->name) ||
StringEqual(string(s1),STR_ELSE) )
if( *res_yield == nilobj ) *res_yield = y;
}
else if( type(s1) == ACAT )
{ for( link = Down(s1); link != s1; link = NextDown(link) )
{ Child(z, link)
;
if( type(z) == GAP_OBJ ) continue;
if( is_word(type(z)) )
{ if( StringEqual(string(z), BackEnd->name) ||
StringEqual(string(s1), STR_ELSE))
if( *res_yield == nilobj ) *res_yield = y;
}
else
{ *all_literals = FALSE;
*res_yield = nilobj;
break;
}
}
}
else
{ *all_literals = FALSE;
*res_yield = nilobj;
}
debug2(DOP, DD, " checkyield returning (%s, %s)", EchoObject(*res_yield),
bool_str(*all_literals));
}
static OBJECT OptimizeCase(OBJECT x)
{ OBJECT link, s2, y, res_yield, res; BOOLEAN all_literals;
debug1(DOP, DD, "OptimizeCase(%s)", EchoObject(x));
assert( type(x) == CASE, "OptimizeCase: type(x) != CASE!" );
Child(s2, LastDown(x))
;
all_literals = TRUE; res_yield = nilobj;
if( type(s2) == YIELD )
{ check_yield(s2, &res_yield, &all_literals);
}
else if( type(s2) == ACAT )
{ for( link = Down(s2); link != s2 && all_literals; link = NextDown(link) )
{
Child(y, link)
;
debug2(DOP, DD, " OptimizeCase examining %s %s", Image(type(y)),
EchoObject(y));
if( type(y) == GAP_OBJ ) continue;
if( type(y) == YIELD )
{ check_yield(y, &res_yield, &all_literals);
}
else
{ all_literals = FALSE;
res_yield = nilobj;
}
}
}
else
{ all_literals = FALSE;
res_yield = nilobj;
}
if( all_literals && res_yield != nilobj )
{ Child(res, LastDown(res_yield))
;
DeleteLink(Up(res));
DisposeObject(x);
}
else
{ res = x;
}
debug1(DOP, DD, "OptimizeCase returning %s", EchoObject(res));
return res;
} /* end OptimizeCase */
/*****************************************************************************/
/* */
/* HuntCommandOptions(x) */
/* */
/* See if any of the command-line options apply to closure x. If so, */
/* change x to reflect the overriding command line option. */
/* */
/*****************************************************************************/
static void HuntCommandOptions(OBJECT x)
{ OBJECT colink, coname, coval, opt = nilobj, y = nilobj, link, sym; BOOLEAN found;
debug1(DOP, DD, "HuntCommandOptions(%s)", SymName(actual(x)));
sym = actual(x);
for( colink = Down(CommandOptions); colink != CommandOptions;
colink = NextDown(NextDown(colink)) )
{
Child(coname, colink)
;
Child(coval, NextDown(colink))
;
debug2(DOP, DD, " hunting \"%s\" with value \"%s\"", string(coname),
EchoObject(coval));
/* set found to TRUE iff coname is the name of an option of x */
found = FALSE;
for( link = Down(sym); link != sym; link = NextDown(link) )
{ Child(opt, link)
;
if( type(opt) == NPAR && StringEqual(SymName(opt), string(coname)) )
{ found = TRUE;
debug2(DOP, DD, " %s is an option of %s", string(coname),SymName(sym));
break;
}
}
if( found )
{
/* see whether this option is already set within x */
found = FALSE;
for( link = Down(x); link != x; link = NextDown(link) )
{ Child(y, link)
;
if( type(y) == PAR && actual(y) == opt )
{ found = TRUE;
debug2(DOP, DD, " %s is set in %s", string(coname), SymName(sym));
break;
}
}
if( found )
{
/* option exists already in x: replace it with oval */
DisposeChild(Down(y));
Link(y, coval);
debug2(DOP, DD, " replacing %s value with %s; x =", string(coname),
EchoObject(coval));
ifdebug(DOP, DD, DebugObject(x));
}
else
{
/* option applies to x but has not yet been set in x */
New(y, PAR);
Link(x, y);
actual(y) = opt;
Link(y, coval);
debug2(DOP, DD, " inserting %s with value %s; x =", string(coname),
EchoObject(coval));
ifdebug(DOP, DD, DebugObject(x));
}
}
}
debug1(DOP, DD, "HuntCommandOptions(%s) returning", SymName(sym));
} /* end HuntCommandOptions */
/*****************************************************************************/
/* */
/* PushObj(x) */
/* PushToken(t) */
/* OBJECT PopObj() */
/* OBJECT PopToken() */
/* OBJECT TokenTop */
/* OBJECT ObjTop */
/* */
/* Push and pop from the object and token stacks; examine top item. */
/* */
/*****************************************************************************/
#define PushObj(x) \
{ zz_hold = x; \
if( ++otop < MAX_STACK ) obj_stack[otop] = zz_hold; \
else Error(6, 1, "expression is too deeply nested", \
FATAL, &fpos(obj_stack[otop-1])); \
}
#define PushToken(t) \
{ if( ++ttop < MAX_STACK ) tok_stack[ttop] = t; \
else Error(6, 2, "expression is too deeply nested", \
FATAL, &fpos(tok_stack[ttop-1])); \
}
#define PopObj() obj_stack[otop--]
#define PopToken() tok_stack[ttop--]
#define TokenTop tok_stack[ttop]
#define ObjTop obj_stack[otop]
/*@::DebugStacks(), InsertSpace()@********************************************/
/* */
/* DebugStacks() */
/* */
/* Print debug output of the stacks state */
/* */
/*****************************************************************************/
#if DEBUG_ON
static void DebugStacks(int initial_ttop, int obj_prev)
{ int i;
debug3(ANY, D, " obj_prev: %s; otop: %d; ttop: %d",
obj_prev == PREV_OP ? "PREV_OP" : obj_prev == PREV_OBJ ? "PREV_OBJ" :
obj_prev == PREV_RBR ? "PREV_RBR" : "???", otop, ttop);
for( i = 0; i <= otop; i++ )
debug3(ANY, D, " obj[%d] = (%s) %s", i,
Image(type(obj_stack[i])), EchoObject(obj_stack[i]));
for( i = 0; i <= ttop; i++ )
{ if( i == initial_ttop+1 ) debug0(DOP, DD, " $");
debug3(ANY, D, " tok[%d] = %s (precedence %d)", i,
type(tok_stack[i]) == CLOSURE ?
SymName(actual(tok_stack[i])) : Image(type(tok_stack[i])),
precedence(tok_stack[i]));
}
} /* end DebugStacks */
#endif
/*****************************************************************************/
/* */
/* InsertSpace(t) */
/* */
/* Add any missing catenation operator in front of token t. */
/* */
/*****************************************************************************/
#define InsertSpace(t) \
if( obj_prev ) \
{ int typ, prec; \
if( hspace(t) + vspace(t) > 0 ) \
typ = TSPACE, prec = ACAT_PREC; \
else if( type(t) == LBR || obj_prev == PREV_RBR ) \
typ = TJUXTA, prec = ACAT_PREC; \
else \
typ = TJUXTA, prec = JUXTA_PREC; \
debugcond1(DOP, DD, debug_now, "[ InsertSpace(%s)", Image(typ)); \
while( obj_prev && precedence(TokenTop) >= prec ) \
obj_prev = Reduce(); \
if( obj_prev ) \
{ New(tmp, typ); precedence(tmp) = prec; \
vspace(tmp) = vspace(t); hspace(tmp) = hspace(t); \
width(gap(tmp)) = 0; nobreak(gap(tmp)) = TRUE; \
mark(gap(tmp)) = FALSE; join(gap(tmp)) = TRUE; \
units(gap(tmp)) = FIXED_UNIT; mode(gap(tmp)) = EDGE_MODE; \
FposCopy(fpos(tmp), fpos(t)); \
PushToken(tmp); \
} \
debugcond0(DOP, DD, debug_now, "] end InsertSpace()"); \
} /* end InsertSpace */
/*@::Shift(), ShiftObj()@*****************************************************/
/* */
/* static Shift(t, prec, rassoc, leftpar, rightpar) */
/* static ShiftObj(t) */
/* */
/* Shift token or object t onto the stacks; it has the attributes shown. */
/* */
/*****************************************************************************/
#define Shift(t, prec, rassoc, leftpar, rightpar) \
{ debugcond5(DOP, DD, debug_now, "[ Shift(%s, %d, %s, %s, %s)", \
Image(type(t)), prec, rassoc ? "rightassoc" : "leftassoc", \
leftpar ? "lpar" : "nolpar", rightpar ? "rpar" : "norpar"); \
if( leftpar ) \
{ for(;;) \
{ if( !obj_prev ) \
{ PushObj( MakeWord(WORD, STR_EMPTY, &fpos(t)) ); \
obj_prev = PREV_OBJ; \
} \
else if( precedence(TokenTop) >= prec + rassoc ) \
{ obj_prev = Reduce(); \
if( ttop == initial_ttop ) \
{ *token = t; \
debugcond0(DOP, DD, debug_now, \
"] ] end Shift() and Parse(); stacks are:"); \
ifdebugcond(DOP, DD, debug_now, \
DebugStacks(initial_ttop, obj_prev)); \
return PopObj(); \
} \
} \
else break; \
} \
} \
else InsertSpace(t); \
PushToken(t); \
if( rightpar ) obj_prev = FALSE; \
else \
{ obj_prev = Reduce(); \
if( ttop == initial_ttop ) \
{ *token = nilobj; \
debugcond0(DOP, DD, debug_now, \
"] ] end Shift and Parse; stacks are:"); \
ifdebugcond(DOP, DD, debug_now, \
DebugStacks(initial_ttop, obj_prev)); \
return PopObj(); \
} \
} \
debugcond0(DOP, DD, debug_now, "] end Shift()"); \
} /* end Shift */
#define ShiftObj(t, new_obj_prev) \
{ debugcond1(DOP, DD, debug_now, "[ ShiftObj(%s)", Image(type(t))); \
InsertSpace(t); \
PushObj(t); \
obj_prev = new_obj_prev; \
debugcond0(DOP, DD, debug_now, "] end ShiftObj()"); \
}
/*@::Reduce()@****************************************************************/
/* */
/* static Reduce() */
/* */
/* Perform a single reduction of the stacks. */
/* */
/*****************************************************************************/
static BOOLEAN Reduce(void)
{ OBJECT p1, p2, p3, s1, s2, tmp;
OBJECT op; int obj_prev;
debugcond0(DOP, DD, debug_now, "[ Reduce()");
/* ifdebugcond(DOP, DD, debug_now, DebugStacks(0, TRUE)); */
op = PopToken();
obj_prev = PREV_OBJ;
switch( type(op) )
{
case GSTUB_INT:
case GSTUB_EXT:
debug0(DGT, D, "calling TransferEnd( PopObj() ) from Reduce()");
TransferEnd( PopObj() );
New(p1, NULL_CLOS);
PushObj(p1);
Dispose(op);
break;
case GSTUB_NONE:
New(p1, NULL_CLOS);
PushObj(p1);
Dispose(op);
break;
case NULL_CLOS:
case PAGE_LABEL:
case BEGIN_HEADER:
case END_HEADER:
case SET_HEADER:
case CLEAR_HEADER:
case ONE_COL:
case ONE_ROW:
case WIDE:
case HIGH:
case HSHIFT:
case VSHIFT:
case HMIRROR:
case VMIRROR:
case HSCALE:
case VSCALE:
case HCOVER:
case VCOVER:
case SCALE:
case KERN_SHRINK:
case HCONTRACT:
case VCONTRACT:
case HLIMITED:
case VLIMITED:
case HEXPAND:
case VEXPAND:
case START_HVSPAN:
case START_HSPAN:
case START_VSPAN:
case HSPAN:
case VSPAN:
case PADJUST:
case HADJUST:
case VADJUST:
case ROTATE:
case BACKGROUND:
case YIELD:
case BACKEND:
case XCHAR:
case FONT:
case SPACE:
case YUNIT:
case ZUNIT:
case SET_CONTEXT:
case GET_CONTEXT:
case BREAK:
case UNDERLINE:
case UNDERLINE_COLOUR:
case COLOUR:
case TEXTURE:
case OUTLINE:
case LANGUAGE:
case CURR_LANG:
case CURR_FAMILY:
case CURR_FACE:
case CURR_YUNIT:
case CURR_ZUNIT:
case COMMON:
case RUMP:
case MELD:
case INSERT:
case ONE_OF:
case NEXT:
case PLUS:
case MINUS:
case TAGGED:
case INCGRAPHIC:
case SINCGRAPHIC:
case PLAIN_GRAPHIC:
case GRAPHIC:
case LINK_SOURCE:
case LINK_DEST:
case LINK_URL:
case OPEN:
case RAW_VERBATIM:
case VERBATIM:
if( has_rpar(actual(op)) )
{ s2 = PopObj();
Link(op, s2);
}
if( has_lpar(actual(op)) )
{ s1 = PopObj();
Link(Down(op), s1);
}
PushObj(op);
break;
case CASE:
if( has_rpar(actual(op)) )
{ s2 = PopObj();
Link(op, s2);
}
if( has_lpar(actual(op)) )
{ s1 = PopObj();
Link(Down(op), s1);
if( type(s1) == BACKEND )
{ op = OptimizeCase(op);
}
}
PushObj(op);
break;
case CROSS:
case FORCE_CROSS:
s2 = PopObj();
Link(op, s2);
s1 = PopObj();
Link(Down(op), s1);
if( type(s1) != CLOSURE )
Error(6, 3, "left parameter of %s is not a symbol (or not visible)",
WARN, &fpos(s1), Image(type(op)));
PushObj(op);
break;
case CLOSURE:
if( has_rpar(actual(op)) )
{ New(s2, PAR);
tmp = PopObj();
Link(s2, tmp);
FposCopy(fpos(s2), fpos(tmp));
actual(s2) = ChildSym(actual(op), RPAR);
Link(op, s2);
}
if( has_lpar(actual(op)) )
{ New(s1, PAR);
tmp = PopObj();
Link(s1, tmp);
FposCopy(fpos(s1), fpos(tmp));
actual(s1) = ChildSym(actual(op), LPAR);
Link(Down(op), s1);
}
PushObj(op);
break;
case LBR:
Error(6, 4, "unmatched %s (inserted %s)", WARN, &fpos(op),
KW_LBR, KW_RBR);
Dispose(op);
obj_prev = PREV_RBR;
break;
case BEGIN:
assert1(FALSE, "Reduce: unmatched", KW_BEGIN);
break;
case RBR:
if( type(TokenTop) == LBR )
{ /* *** FposCopy(fpos(ObjTop), fpos(TokenTop)); *** */
Dispose( PopToken() );
}
else if( type(TokenTop) == BEGIN )
{ if( file_num(fpos(TokenTop)) > 0 )
Error(6, 5, "unmatched %s; inserted %s at%s (after %s)",
WARN, &fpos(op), KW_RBR, KW_LBR,
EchoFilePos(&fpos(TokenTop)), KW_BEGIN);
else
Error(6, 6, "unmatched %s not enclosed in anything",
FATAL, &fpos(op), KW_RBR);
}
else
{ assert1(FALSE, "Reduce: unmatched", KW_RBR);
}
Dispose(op);
obj_prev = PREV_RBR;
break;
case END:
if( type(TokenTop) != BEGIN )
{ assert1(FALSE, "Reduce: unmatched", KW_END);
}
else
{ if( actual(op) != actual(TokenTop) )
{
if( actual(op) == StartSym )
Error(6, 7, "%s %s appended at end of file to match %s at%s",
WARN, &fpos(op), KW_END, SymName(actual(TokenTop)),
KW_BEGIN, EchoFilePos(&fpos(TokenTop)) );
else if( actual(op) == nilobj )
Error(6, 8, "%s replaced by %s %s to match %s at%s",
WARN, &fpos(op), KW_END, KW_END,
actual(TokenTop) == nilobj ? AsciiToFull("??") :
SymName(actual(TokenTop)),
KW_BEGIN, EchoFilePos(&fpos(TokenTop)) );
else
Error(6, 9, "%s %s replaced by %s %s to match %s at%s",
WARN, &fpos(op), KW_END, SymName(actual(op)),
KW_END, SymName(actual(TokenTop)),
KW_BEGIN, EchoFilePos(&fpos(TokenTop)) );
}
Dispose( PopToken() );
}
Dispose(op);
obj_prev = PREV_RBR;
break;
case GAP_OBJ:
p1 = PopObj();
Link(op, p1);
PushObj(op);
obj_prev = PREV_OP;
break;
case VCAT:
case HCAT:
case ACAT:
p3 = PopObj(); p2 = PopObj(); p1 = PopObj();
if( type(p1) == type(op) )
{ Dispose(op);
}
else
{ Link(op, p1);
p1 = op;
}
Link(p1, p2);
Link(p1, p3);
PushObj(p1);
break;
case TSPACE:
case TJUXTA:
p2 = PopObj(); p1 = PopObj();
if( type(p1) != ACAT )
{ New(tmp, ACAT);
Link(tmp, p1);
FposCopy(fpos(tmp), fpos(p1));
p1 = tmp;
}
type(op) = GAP_OBJ;
Link(p1, op);
Link(p1, p2);
PushObj(p1);
break;
default:
assert1(FALSE, "Reduce:", Image(type(op)));
break;
} /* end switch */
debugcond1(DOP, DD, debug_now, "] end Reduce(), returning %s",
obj_prev == PREV_OP ? "PREV_OP" : obj_prev == PREV_OBJ ? "PREV_OBJ" :
obj_prev == PREV_RBR ? "PREV_RBR" : "???");
return obj_prev;
} /* end Reduce */
/*@::SetScope(), InitParser()@************************************************/
/* */
/* SetScope(env, count, vis_only) */
/* */
/* Push scopes required to parse object whose environment is env. */
/* Add to *count the number of scope pushes made. */
/* */
/* If vis_only is true, we only want visible things of the top-level */
/* element of env to be visible in this scope. */
/* */
/*****************************************************************************/
void SetScope(OBJECT env, int *count, BOOLEAN vis_only)
{ OBJECT link, y, yenv; BOOLEAN visible_only;
debugcond2(DOP,DD, debug_now, "[ SetScope(%s, %d)", EchoObject(env), *count);
assert( env != nilobj && type(env) == ENV, "SetScope: type(env) != ENV!" );
if( Down(env) != env )
{ Child(y, Down(env))
;
assert( LastDown(y) != y, "SetScope: LastDown(y)!" );
link = LastDown(env) != Down(env) ? LastDown(env) : LastDown(y);
Child(yenv, link)
;
assert( type(yenv) == ENV, "SetScope: type(yenv) != ENV!" );
SetScope(yenv, count, FALSE);
visible_only = vis_only || (use_invocation(actual(y)) != nilobj);
/* i.e. from @Use clause */
PushScope(actual(y), FALSE, visible_only); (*count)++;
/*** this following was a bright idea that did not work owing to
allowing body parameters at times they definitely shouldn't be
BodyParAllowed();
***/
}
debugcond1(DOP, DD, debug_now, "] SetScope returning, count = %d", *count);
} /* end SetScope */
/*****************************************************************************/
/* */
/* InitParser() */
/* */
/* Initialise the parser to contain just GstubExt. */
/* Remember cross_db, the name of the cross reference database, for Parse. */
/* */
/*****************************************************************************/
void InitParser(FULL_CHAR *cross_db)
{
otop = -1;
ttop = -1;
unknown_count = 0;
InDefinitions = TRUE;
debug0(DOP, D, "InitParser setting InDefinitions to TRUE");
#if DEBUG_ON
debug_now = FALSE;
#endif
if( StringLength(cross_db) >= MAX_WORD )
Error(6, 10, "cross reference database file name %s is too long",
FATAL, no_fpos, cross_db);
cross_name = MakeWord(WORD, cross_db, no_fpos);
PushToken( NewToken(GSTUB_EXT, no_fpos, 0, 0, DEFAULT_PREC, StartSym) );
} /* end InitParser */
/*@::ParseEnvClosure()@*******************************************************/
/* */
/* static OBJECT ParseEnvClosure(t, encl) */
/* */
/* Parse an object which is a closure with environment. Consume the */
/* concluding @LClos. */
/* */
/*****************************************************************************/
static OBJECT ParseEnvClosure(OBJECT t, OBJECT encl)
{ OBJECT env, res, y; int count, i;
debugcond0(DOP, DDD, debug_now, "ParseEnvClosure(t, encl)");
assert( type(t) == ENV, "ParseEnvClosure: type(t) != ENV!" );
env = t; t = LexGetToken();
while( type(t) != CLOS ) switch( type(t) )
{
case LBR: count = 0;
SetScope(env, &count, FALSE);
y = Parse(&t, encl, FALSE, FALSE);
if( type(y) != CLOSURE )
{
debug1(DIO, D, " Parse() returning %s:", Image(type(y)));
ifdebug(DIO, D, DebugObject(y));
Error(6, 11, "syntax error in cross reference database",
FATAL, &fpos(y));
}
for( i = 1; i <= count; i++ ) PopScope();
AttachEnv(env, y);
debug0(DCR, DDD, " calling SetEnv from ParseEnvClosure (a)");
env = SetEnv(y, nilobj);
t = LexGetToken();
break;
case ENV: y = ParseEnvClosure(t, encl);
debug0(DCR, DDD, " calling SetEnv from ParseEnvClosure (b)");
env = SetEnv(y, env);
t = LexGetToken();
break;
default: Error(6, 12, "error in cross reference database",
FATAL, &fpos(t));
break;
}
Dispose(t);
if( Down(env) == env || Down(env) != LastDown(env) )
Error(6, 13, "error in cross reference database", FATAL, &fpos(env));
Child(res, Down(env))
;
DeleteNode(env);
debugcond1(DOP, DDD, debug_now, "ParseEnvClosure ret. %s", EchoObject(res));
assert( type(res) == CLOSURE, "ParseEnvClosure: type(res) != CLOSURE!" );
return res;
} /* end ParseEnvClosure */
/*@::Parse()@*****************************************************************/
/* */
/* OBJECT Parse(token, encl, defs_allowed, transfer_allowed) */
/* */
/* Parse input tokens, beginning with *token, looking for an object of the */
/* form { ... } or @Begin ... @End <sym>, and return the object. */
/* The parent definition is encl, and scope has been set appropriately. */
/* Parse reads up to and including the last token of the object */
/* (the right brace or <sym>), and returns nilobj in *token. */
/* */
/* If defs_allowed == TRUE, there may be local definitions in the object. */
/* In this case, encl is guaranteed to be the enclosing definition. */
/* */
/* If transfer_allowed == TRUE, the parser may transfer components to the */
/* galley handler as they are read. */
/* */
/* Note: the lexical analyser returns "@End \Input" at end of input, so the */
/* parser does not have to handle end of input separately. */
/* */
/*****************************************************************************/
OBJECT Parse(OBJECT *token, OBJECT encl,
BOOLEAN defs_allowed, BOOLEAN transfer_allowed)
{ OBJECT t, x, tmp, xsym, env, y, link, res, imps, xlink;
int i, offset, lnum, initial_ttop = ttop;
int obj_prev, scope_count, compulsory_count; BOOLEAN revealed;
debugcond4(DOP, DD, debug_now, "[ Parse(%s, %s, %s, %s)", EchoToken(*token),
SymName(encl), bool_str(defs_allowed), bool_str(transfer_allowed));
assert( type(*token) == LBR || type(*token) == BEGIN, "Parse: *token!" );
obj_prev = PREV_OP;
Shift(*token, precedence(*token), 0, FALSE, TRUE);
t = LexGetToken();
if( defs_allowed )
{ ReadDefinitions(&t, encl, LOCAL);
/* if error in definitions, stop now */
if( ErrorSeen() )
Error(6, 14, "exiting now (error in definitions)", FATAL, &fpos(t));
if( encl == StartSym )
{
/* read @Use, @Database, and @Prepend commands and defs and construct env */
New(env, ENV);
for(;;)
{
if( type(t) == WORD && (
StringEqual(string(t), KW_DEF) ||
/* StringEqual(string(t), KW_FONTDEF) || */
StringEqual(string(t), KW_LANGDEF) ||
StringEqual(string(t), KW_MACRO) ||
StringEqual(string(t), KW_IMPORT) ||
StringEqual(string(t), KW_EXTEND) ||
StringEqual(string(t), KW_EXPORT) ) )
{
ReadDefinitions(&t, encl, LOCAL);
/* if error in definitions, stop now */
if( ErrorSeen() )
Error(6, 39, "exiting now (error in definitions)", FATAL, &fpos(t));
}
else if( type(t) == USE )
{
OBJECT crs, res_env; STYLE style;
Dispose(t); t = LexGetToken();
if( type(t) != LBR )
Error(6, 15, "%s expected after %s", FATAL, &fpos(t),KW_LBR,KW_USE);
debug0(DOP, DD, " Parse() calling Parse for @Use clause");
y = Parse(&t, encl, FALSE, FALSE);
if( is_cross(type(y)) )
{ OBJECT z;
Child(z, Down(y))
;
if( type(z) == CLOSURE )
{ crs = nilobj;
y = CrossExpand(y, env, &style, &crs, &res_env);
if( crs != nilobj )
{ Error(6, 16, "%s or %s tag not allowed here",
FATAL, &fpos(y), KW_PRECEDING, KW_FOLLOWING);
}
HuntCommandOptions(y);
AttachEnv(res_env, y);
debug0(DCR, DDD, " calling SetEnv from Parse (a)");
env = SetEnv(y, env);
}
else Error(6, 17, "invalid parameter of %s", FATAL, &fpos(y), KW_USE);
}
else if( type(y) == CLOSURE )
{ if( use_invocation(actual(y)) != nilobj )
Error(6, 18, "symbol %s occurs in two %s clauses",
FATAL, &fpos(y), SymName(actual(y)), KW_USE);
use_invocation(actual(y)) = y;
HuntCommandOptions(y);
AttachEnv(env, y);
debug0(DCR, DDD, " calling SetEnv from Parse (b)");
env = SetEnv(y, nilobj);
}
else Error(6, 19, "invalid parameter of %s", FATAL, &fpos(y), KW_USE);
PushScope(actual(y), FALSE, TRUE);
t = LexGetToken();
}
else if( type(t) == PREPEND || type(t) == SYS_PREPEND )
{ ReadPrependDef(type(t), encl);
Dispose(t);
t = LexGetToken();
}
else if( type(t) == INCG_REPEATED || type(t) == SINCG_REPEATED )
{ ReadIncGRepeatedDef(type(t), encl);
Dispose(t);
t = LexGetToken();
}
else if( type(t) == DATABASE || type(t) == SYS_DATABASE )
{ ReadDatabaseDef(type(t), encl);
Dispose(t);
t = LexGetToken();
}
else break;
}
/* transition point from defs to content; turn on debugging now */
#if DEBUG_ON
debug_now = TRUE;
#endif
InDefinitions = FALSE;
debug0(DOP, D, "Parse() setting InDefinitions to FALSE");
debugcond4(DOP, DD, debug_now, "[ Parse (first) (%s, %s, %s, %s)",
EchoToken(*token), SymName(encl), bool_str(defs_allowed),
bool_str(transfer_allowed));
/* load cross-references from previous run, open new cross refs */
if( AllowCrossDb )
{
NewCrossDb = DbCreate(MakeWord(WORD, string(cross_name), no_fpos));
OldCrossDb = DbLoad(cross_name, SOURCE_PATH, FALSE, nilobj,
InMemoryDbIndexes);
}
else OldCrossDb = NewCrossDb = nilobj;
/* tidy up and possibly print symbol table */
FlattenUses();
ifdebug(DST, DD, DebugObject(StartSym));
TransferInit(env);
debug0(DMA, D, "at end of definitions:");
ifdebug(DMA, D, DebugMemory());
}
}
for(;;)
{
debugcond0(DOP, DD, debug_now, "");
ifdebugcond(DOP, DD, debug_now, DebugStacks(0, obj_prev));
debugcond0(DOP, DD, debug_now, "");
debugcond2(DOP, DD, debug_now, ">> %s (precedence %d)", EchoToken(t), precedence(t));
switch( type(t) )
{
case WORD:
if( string(t)[0] == CH_SYMSTART &&
(obj_prev != PREV_OBJ || vspace(t) + hspace(t) > 0) )
{
Error(6, 20, "symbol %s unknown or misspelt",
WARN, &fpos(t), string(t));
if( ++unknown_count > 25 )
{
Error(6, 21, "too many errors (%s lines missing or out of order?)",
FATAL, &fpos(t), KW_SYSINCLUDE);
}
}
ShiftObj(t, PREV_OBJ);
t = LexGetToken();
break;
case QWORD:
ShiftObj(t, PREV_OBJ);
t = LexGetToken();
break;
case VCAT:
case HCAT:
case ACAT:
/* clean up left context */
Shift(t, precedence(t), LEFT_ASSOC, TRUE, TRUE);
/* invoke transfer subroutines if appropriate */
/* *** if( type(t) == VCAT && !has_join(actual(t)) *** */
if( transfer_allowed && type(t) == VCAT && !has_join(actual(t))
&& type(tok_stack[ttop-2]) == GSTUB_EXT )
{
debug0(DGT, DD, " calling TransferComponent from Parse:");
ifdebug(DGT, DD, DebugStacks(0, obj_prev));
TransferComponent( PopObj() );
New(tmp, NULL_CLOS);
FposCopy( fpos(tmp), fpos(t) );
PushObj(tmp);