-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBookCompiler.Mod
2067 lines (1999 loc) · 50.1 KB
/
BookCompiler.Mod
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
(* OBERON System 3, Release 2.3.
Copyright 1999 ETH Zürich Institute for Computer Systems,
ETH Center, CH-8092 Zürich. e-mail: [email protected].
This module may be used under the conditions of the general Oberon
System 3 license contract. The full text can be downloaded from
"ftp://ftp.inf.ethz.ch/pub/software/Oberon/System3/license.txt;A"
Under the license terms stated it is in particular (a) prohibited to modify
the interface of this module in any way that disagrees with the style
or content of the system and (b) requested to provide all conversions
of the source code to another platform with the name OBERON. *)
MODULE BookCompiler; (** portable *)
IMPORT Oberon, Texts, Files, Display, Gadgets, TextGadgets, Fonts, Objects, Books, Books0, BookDocs,
Strings, Documents, Lists, TextFields, TextDocs, Desktops, Panels;
CONST
(* symbols for scanner *)
noSy = 0; identSy = 1; bsSy = 2; lbrSy = 3; rbrSy = 4; starSy = 5; minusSy = 6; plusSy = 7; eqSy = 8;
bookSy = 9; chapterSy = 10; labelSy = 11; linkSy = 12; indexSy = 13; callSy = 14; noteSy = 15;
lprSy = 17; rprSy = 18; commaSy = 19; numberSy = 20;
(* limit for the number of objects in a text *)
maxObj = 256;
TYPE
Chapter = POINTER TO ChapterDesc;
Label = POINTER TO LabelDesc;
Node = POINTER TO NodeDesc;
PosList = POINTER TO PosListDesc;
NotePosList = POINTER TO NotePosListDesc;
SectNode = POINTER TO SectNodeDesc;
(* list of chapters/pages *)
ChapterDesc = RECORD
nObj: INTEGER;
text: Texts.Text;
ind: LONGINT;
next: Chapter
END;
(* multi-way tree for contents page *)
SectNodeDesc = RECORD
B: Texts.Buffer;
next, desc: SectNode;
c: Books0.ContElem
END;
(* list of labels/footnotes *)
LabelDesc = RECORD
name: ARRAY Books0.identLen OF CHAR;
frame: Books0.LocFrame;
export: BOOLEAN;
next: Label
END;
(* binary tree for index page *)
NodeDesc = RECORD
beg, end: LONGINT;
pos: PosList;
left, right: Node
END;
(* list of double index entries *)
PosListDesc = RECORD
pos: LONGINT;
chapter: Chapter;
next: PosList
END;
NotePosListDesc = RECORD (PosListDesc)
pos2: LONGINT
END;
VAR
chapters, contents, curChap: Chapter;
labels: Label;
root: Node;
imports: Books0.ImpList;
bookName: ARRAY Books0.nameLen OF CHAR;
ident: ARRAY Books0.identLen OF CHAR;
B: Texts.Buffer;
inText, callText, noteText: Texts.Text;
R: Texts.Reader;
W, Wr: Texts.Writer;
sym, width, heigth: INTEGER;
ch: CHAR;
number: LONGINT;
begId, endId, lastPos, oldPos, nextChap: LONGINT;
sections: ARRAY Books.maxSect OF INTEGER;
eol, expand, error: BOOLEAN;
options, styleMode: SET;
iconStr: ARRAY 2*Books0.nameLen OF CHAR;
sectRoot: SectNode;
(* report error message *)
PROCEDURE Mark(msg1, msg2: ARRAY OF CHAR);
BEGIN
error := TRUE;
IF Texts.Pos(R) > lastPos-1 THEN
Texts.WriteLn(Wr);
Texts.WriteString(Wr, msg1);
Texts.WriteString(Wr, msg2);
Texts.WriteString(Wr, " at ");
Texts.WriteInt(Wr, lastPos, 0);
Texts.Append(Oberon.Log, Wr.buf)
END
END Mark;
(* change font *)
PROCEDURE MarkFnt(T: Texts.Text; beg, end: LONGINT; fnt: Fonts.Font);
BEGIN
Texts.ChangeLooks(T, beg, end, {Books.looksLib}, fnt, 0, 0)
END MarkFnt;
(* increment number of objects in this chapter-page *)
PROCEDURE IncObj(c: Chapter);
BEGIN
INC(c.nObj);
IF c.nObj > maxObj THEN
Mark("", "too many objects")
END
END IncObj;
(* replace old font in textstrecth (beg, end) to bew font *)
PROCEDURE ChangeFont(t: Texts.Text; old, new: Fonts.Font; beg, end: LONGINT);
VAR
R: Texts.Reader;
ch: CHAR;
pos: LONGINT;
BEGIN
Texts.OpenReader(R, t, beg);
Texts.Read(R, ch);
pos := Texts.Pos(R);
WHILE ~R.eot & (pos < end) DO
IF R.lib = old THEN
WHILE ~R.eot & (R.lib = old) & (pos < end) DO
Texts.Read(R, ch)
END;
MarkFnt(t, pos-1, Texts.Pos(R)-1, new)
END;
IF ~R.eot THEN
Texts.Read(R, ch);
pos := Texts.Pos(R)
END
END
END ChangeFont;
(* create a new contents-tree node *)
PROCEDURE NewSectNode(VAR nd: SectNode);
BEGIN
NEW(nd);
nd.next := NIL; nd.desc := NIL;
NEW(nd.B); Texts.OpenBuf(nd.B);
Books0.NewContElem();
nd.c := Objects.NewObj(Books0.ContElem)
END NewSectNode;
(* expand commands \\ and \} in the given text-stretch *)
PROCEDURE Expand(VAR t: Texts.Text; beg, end: LONGINT);
VAR
R: Texts.Reader;
ch: CHAR;
pos: LONGINT;
BEGIN
IF expand THEN
Texts.OpenReader(R, t, beg);
Texts.Read(R, ch);
WHILE ~R.eot & (Texts.Pos(R) < end) DO
IF ch = "\" THEN
Texts.Read(R, ch);
IF (ch = "}") OR (ch = "\") THEN
pos := Texts.Pos(R)-1;
Texts.Delete(t, pos-1, pos);
Texts.OpenReader(R, t, pos);
Texts.Read(R, ch);
DEC(end)
END
ELSE
Texts.Read(R, ch)
END
END
END
END Expand;
(* create a new chapter-page *)
PROCEDURE NewChapter(VAR c: Chapter);
BEGIN
NEW(c);
NEW(c.text);
Texts.Open(c.text, "");
c.nObj := 0;
c.next := NIL;
c.ind := nextChap;
INC(nextChap)
END NewChapter;
(* search a item named ident in the labels list *)
PROCEDURE SearchLabel(): Label;
VAR l: Label;
BEGIN
l := labels;
WHILE (l # NIL) & (l.name # ident) DO
l := l.next
END;
RETURN l
END SearchLabel;
(* define a new label (curC -> at the end of the current chapter) *)
PROCEDURE DefChapLabel(curC: BOOLEAN): Label;
VAR l, cl: Label;
BEGIN
l := SearchLabel();
IF l = NIL THEN
NEW(cl);
cl.name := ident;
cl.next := labels;
labels := cl
ELSIF l.frame.pos1 >= 0 THEN
NEW(cl);
cl.name := ident;
cl.next := labels;
labels := cl;
Mark(ident, " label allready defined")
ELSE
cl := l
END;
IF cl # l THEN
cl.export := FALSE;
Books0.NewLoc();
cl.frame := Objects.NewObj(Books0.LocFrame);
cl.frame.mode := Books.link
END;
IF curC THEN
cl.frame.pos1 := curChap.ind;
cl.frame.pos2 := curChap.text.len
ELSE
cl.frame.pos1 := -1;
cl.frame.pos2 := -1
END;
RETURN cl
END DefChapLabel;
(* initialize all global data for a new tutorial *)
PROCEDURE NewBook(title: BOOLEAN);
VAR
beg: LONGINT;
cl: Label;
BEGIN
labels := NIL;
imports := NIL;
nextChap := 0;
NewChapter(chapters);
chapters.next := NIL;
curChap := chapters;
contents := chapters;
ident := "Contents";
cl := DefChapLabel(TRUE);
IF ~title THEN
Texts.WriteString(W, "Contents of ");
Texts.WriteString(W, bookName);
Texts.WriteString(W, ": ");
Texts.Append(contents.text, W.buf)
ELSE
beg := contents.text.len;
Texts.Save(inText, begId, endId, B);
Texts.Append(contents.text, B);
Expand(contents.text, beg, contents.text.len)
END;
Texts.WriteLn(W);
Texts.Append(contents.text, W.buf);
MarkFnt(contents.text, 0, contents.text.len, BookDocs.titleFont);
NewSectNode(sectRoot);
Texts.Save(contents.text, 0, contents.text.len, sectRoot.B);
NEW(root);
root.left := NIL; root.right := NIL; root.pos := NIL;
root.beg := 0; root.end := 0;
NEW(callText);
Texts.Open(callText, "");
NEW(noteText);
Texts.Open(noteText, "")
END NewBook;
(* defines a new, empty footnote *)
PROCEDURE DefFootnoteLabel(): Label;
VAR l, pl: Label;
BEGIN
l := SearchLabel();
IF l = NIL THEN
NEW(pl);
pl.name := ident;
pl.next := labels;
labels := pl
ELSE
Mark(ident, " label allready defined");
pl := l
END;
pl.export := FALSE;
Books0.NewLoc();
pl.frame := Objects.NewObj(Books0.LocFrame);
pl.frame.mode := Books.note;
pl.frame.pos1 := -1;
pl.frame.pos2 := -1;
RETURN pl
END DefFootnoteLabel;
(* creates a link to the label/footnote named ident *)
PROCEDURE DefLink(): Books0.Frame;
VAR l: Label;
BEGIN
l := SearchLabel();
IF l = NIL THEN
l := DefChapLabel(FALSE)
END;
RETURN l.frame
END DefLink;
(* creates a external link to the label/footnote named name *)
PROCEDURE DefExtLink(i: Books0.ImpList; VAR name: ARRAY OF CHAR): Books0.Frame;
VAR
el: Books0.ExtLabel;
lk: Books0.Frame;
BEGIN
el := i.extLabels;
WHILE (el # NIL) & (el.name # name) DO
el := el.next
END;
IF el # NIL THEN
IF el.frame = NIL THEN
Books0.NewExt();
lk := Objects.NewObj(Books0.ExtFrame);
lk.mode := el.mode;
lk(Books0.ExtFrame).imp := el;
el.frame := lk
ELSE
lk := el.frame
END
ELSE
Texts.WriteLn(Wr);
Texts.WriteString(Wr, "Warning: external label ");
Texts.WriteString(Wr, i.name);
Texts.Write(Wr, ".");
Texts.WriteString(Wr, name);
Texts.WriteString(Wr, " not found");
Texts.Append(Oberon.Log, Wr.buf);
NEW(el);
el.name := name;
el.next := i.extLabels;
i.extLabels := el;
el.book := i;
el.mode := Books.link;
Books0.NewExt();
lk := Objects.NewObj(Books0.ExtFrame);
lk.mode := el.mode;
lk(Books0.ExtFrame).imp := el;
el.frame := lk
END;
RETURN lk
END DefExtLink;
(* defines a new comand link *)
PROCEDURE DefCallGadgets(beg: LONGINT);
VAR obj: Books0.LocFrame;
BEGIN
Books0.NewLoc();
obj := Objects.NewObj(Books0.LocFrame);
obj.mode:= Books.call;
obj.pos1 := beg;
obj.pos2 := callText.len;
Books0.AppendFrame(curChap.text, obj);
IncObj(curChap)
END DefCallGadgets;
(* mark the given text-stretch with the correct (mode) color and font (if ft) *)
PROCEDURE MarkText(T: Texts.Text; beg, end: LONGINT; mode: SHORTINT; ft: BOOLEAN);
BEGIN
IF ft THEN
CASE mode OF
Books.link: Texts.ChangeLooks(T, beg, end, {Books.looksCol, Books.looksLib}, BookDocs.linkFont, Books.linkCol, 0)
|Books.call: Texts.ChangeLooks(T, beg, end, {Books.looksCol, Books.looksLib}, BookDocs.callFont, Books.callCol, 0)
|Books.note: Texts.ChangeLooks(T, beg, end, {Books.looksCol, Books.looksLib}, BookDocs.noteFont, Books.noteCol, 0)
ELSE
END
ELSE
CASE mode OF
Books.link: Texts.ChangeLooks(T, beg, end, {Books.looksCol}, NIL, Books.linkCol, 0)
|Books.call: Texts.ChangeLooks(T, beg, end, {Books.looksCol}, NIL, Books.callCol, 0)
|Books.note: Texts.ChangeLooks(T, beg, end, {Books.looksCol}, NIL, Books.noteCol, 0)
ELSE
END
END;
END MarkText;
(* adds a chapter of level s to the contents tree *)
PROCEDURE Visit(VAR n: SectNode; d: INTEGER);
BEGIN
IF (n.desc # NIL) & (d > 0) THEN
n := n.desc;
WHILE n.next # NIL DO
n := n.next
END;
Visit(n, d-1)
END
END Visit;
PROCEDURE DefSection(s: INTEGER; e: BOOLEAN);
VAR
i: INTEGER;
obj: Books0.LocFrame;
beg: LONGINT;
cl: Label;
nd, nd2: SectNode;
BEGIN
IF ident # "" THEN
cl := DefChapLabel(TRUE);
cl.export := e
END;
Books0.NewLoc();
obj := Objects.NewObj(Books0.LocFrame);
obj.mode := Books.link;
obj.pos1 := curChap.ind;
obj.pos2 := curChap.text.len;
Texts.WriteLn(W);
Texts.Write(W, Books.Tab);
i := 0;
WHILE i < s DO
Texts.Write(W, Books.Tab);
INC(i)
END;
Texts.Append(contents.text, W.buf);
Texts.Save(inText, begId, endId, B);
beg := contents.text.len;
Texts.Append(contents.text, B);
Expand(contents.text, beg, contents.text.len);
MarkText(contents.text, beg, contents.text.len, Books.link, TRUE);
Books0.AppendFrame(contents.text, obj);
IncObj(contents);
IF (Books.formated IN options) & (s < Books.maxSect) THEN
MarkFnt(contents.text, beg, contents.text.len, BookDocs.sectionFonts[s])
END;
IF s >= Books.maxSect THEN
Mark("", "to deep chapter nesting"); s := 1
ELSIF sections[s-1] = 0 THEN
Mark("", "no chapter of previous level"); s := 1
ELSE
INC(sections[s])
END;
NewSectNode(nd);
Texts.Save(contents.text, beg, contents.text.len, nd.B);
nd2 := sectRoot;
Visit(nd2, s);
IF nd2.desc = NIL THEN
nd2.desc := nd
ELSE
nd2 := nd2.desc;
WHILE (nd2.next # NIL) DO
nd2 := nd2.next
END;
nd2.next := nd
END;
Texts.Save(inText, begId, endId, B);
beg := curChap.text.len;
Texts.Append(curChap.text, B);
Expand(curChap.text, beg, curChap.text.len);
IF (Books.formated IN options) & (s < Books.maxSect) THEN
MarkFnt(curChap.text, beg, curChap.text.len, BookDocs.sectionFonts[s])
END
END DefSection;
(* defines a new chapter-page *)
PROCEDURE DefChapter(e: BOOLEAN);
VAR
chap: Chapter;
i: INTEGER;
obj: Books0.LocFrame;
beg: LONGINT;
style: TextGadgets.Style;
cl: Label;
nd, nd2: SectNode;
BEGIN
IF (curChap # NIL) & (curChap # chapters) THEN
Texts.WriteLn(W);
Texts.Append(curChap.text, W.buf)
END;
NewChapter(chap);
curChap.next := chap;
curChap := chap;
IF ident # "" THEN
cl := DefChapLabel(TRUE);
cl.export := e
END;
Books0.NewLoc();
obj := Objects.NewObj(Books0.LocFrame);
obj.mode := Books.link;
obj.pos1 := curChap.ind;
obj.pos2 := 0;
Texts.WriteLn(W);
Texts.Write(W, Books.Tab);
Texts.Append(contents.text, W.buf);
beg := contents.text.len;
IF endId >= begId THEN
Texts.Save(inText, begId, endId, B);
Texts.Append(contents.text, B);
Expand(contents.text, beg, contents.text.len)
ELSE
Texts.WriteString(W, ident);
Texts.Append(contents.text, W.buf)
END;
MarkText(contents.text, beg, contents.text.len, Books.link, TRUE);
Books0.AppendFrame(contents.text, obj);
IncObj(contents);
IF Books.formated IN options THEN
MarkFnt(contents.text, beg, contents.text.len, BookDocs.sectionFonts[0])
END;
NewSectNode(nd);
Texts.Save(contents.text, beg, contents.text.len, nd.B);
IF sectRoot.desc = NIL THEN
sectRoot.desc := nd
ELSE
nd2 := sectRoot.desc;
WHILE (nd2.next # NIL) DO
nd2 := nd2.next
END;
nd2.next := nd
END;
sections[0] := 1;
i := 1;
WHILE i < Books.maxSect DO
sections[i] := 0;
INC(i)
END;
beg := chap.text.len;
IF endId >= begId THEN
Texts.Save(inText, begId, endId, B);
Texts.Append(chap.text, B)
END;
Expand(chap.text, beg, chap.text.len);
IF Books.formated IN options THEN
IF endId >= begId THEN
Texts.WriteLn(W);
Texts.Append(chap.text, W.buf)
END;
style := TextGadgets.newStyle();
style.width := width-Books.borderL-Books.borderR-Books.scrollBW;
style.mode := styleMode;
Books0.AppendFrame(curChap.text, style);
IncObj(curChap);
MarkFnt(curChap.text, beg, curChap.text.len, BookDocs.sectionFonts[0])
END
END DefChapter;
(* adds an index entry *)
PROCEDURE Compare(a, b: Node): INTEGER;
VAR
R0, R1: Texts.Reader;
ch0, ch1: CHAR;
BEGIN
Texts.OpenReader(R0, inText, a.beg);
Texts.OpenReader(R1, inText, b.beg);
Texts.Read(R0, ch0); Texts.Read(R1, ch1);
WHILE ~R0.eot & ~R1.eot & (CAP(ch0) = CAP(ch1)) & (Texts.Pos(R0) <= a.end) & (Texts.Pos(R1) <= b.end) DO
Texts.Read(R0, ch0); Texts.Read(R1, ch1)
END;
IF ~(R0.eot OR (Texts.Pos(R0) > a.end)) & ~(R1.eot OR (Texts.Pos(R1) > b.end)) THEN
RETURN ORD(CAP(ch1))-ORD(CAP(ch0))
ELSIF (R0.eot OR (Texts.Pos(R0) > a.end)) & (R1.eot OR (Texts.Pos(R1) > b.end)) THEN
RETURN 0
ELSIF R0.eot OR (Texts.Pos(R0) > a.end) THEN
RETURN +1
ELSIF R1.eot OR (Texts.Pos(R1) > b.end) THEN
RETURN -1
ELSE
RETURN 0
END
END Compare;
PROCEDURE DefIndex(pos: LONGINT; mode: SHORTINT);
VAR
n, cur, prev: Node;
res: INTEGER;
pl, pl2: PosList;
npl: NotePosList;
BEGIN
NEW(n);
n.beg := begId; n.end := endId;
n.pos := NIL; n.left := NIL; n.right := NIL;
cur := root; prev := NIL;
WHILE cur # NIL DO
prev := cur;
res := Compare(cur, n);
IF res < 0 THEN
cur := cur.left
ELSIF res > 0 THEN
cur := cur.right
ELSE
cur := NIL
END
END;
IF mode = Books.link THEN
NEW(pl);
pl.chapter := curChap;
pl.pos := pos;
pl.next := NIL
ELSIF mode = Books.note THEN
NEW(npl);
npl.chapter := NIL;
npl.pos := pos;
npl.pos2 := noteText.len;
npl.next := NIL;
pl := npl
END;
IF res < 0 THEN
prev.left := n;
n.pos := pl
ELSIF res > 0 THEN
prev.right := n;
n.pos := pl
ELSIF (prev.right # NIL) & ((endId-begId) > (prev.end-prev.beg)) THEN
prev.right := n;
n.pos := pl
ELSIF (prev.left # NIL) & ((endId-begId) < (prev.end-prev.beg)) THEN
prev.left := n;
n.pos := pl
ELSE
n := prev;
pl2 := n.pos;
WHILE pl2.next # NIL DO
pl2 := pl2.next
END;
pl2.next := pl
END
END DefIndex;
(* adds the text to an earlier defined footnote *)
PROCEDURE InsertFootnote(pl: Label);
VAR beg: LONGINT;
BEGIN
pl.frame.pos1 := noteText.len;
Texts.Save(inText, begId, endId, B);
beg := noteText.len;
Texts.Append(noteText, B);
Expand(noteText, beg, noteText.len);
pl.frame.pos2 := noteText.len
END InsertFootnote;
(* check if all labels/footnotes used got defined *)
PROCEDURE CheckLabels();
VAR l: Label;
BEGIN
ident := "Open";
l := SearchLabel();
IF l = NIL THEN
l := DefChapLabel(FALSE);
l.frame.pos1 := 0;
l.frame.pos2 := 0
ELSIF l.frame.pos1 < 0 THEN
l.frame.pos1 := 0;
l.frame.pos2 := 0
END;
l.export := TRUE;
l := labels;
WHILE l # NIL DO
IF l.frame.pos1 < 0 THEN
Mark(l.name, " label never defined")
END;
l := l.next
END
END CheckLabels;
(* create the tutorial file *)
PROCEDURE RegisterBook();
VAR
c: Chapter;
F: Files.File;
R: Files.Rider;
ind, len, pos, fixPos1, fixPos2, fixPos3: LONGINT;
il: Books0.ImpList;
l: Label;
IM: BookDocs.InValMsg;
BEGIN
F := Files.New(bookName);
Texts.WriteString(Wr, ", writing: ");
Texts.WriteString(Wr, bookName);
Texts.WriteLn(Wr);
Texts.Append(Oberon.Log, Wr.buf);
IF F = NIL THEN
HALT(99)
END;
Files.Set(R, F, 0);
ident := "Open";
l := SearchLabel();
l.export := TRUE;
ind := l.frame.pos1;
pos := l.frame.pos2;
BookDocs.WriteHeader(R, 0, 0, width, heigth, ind, pos, options, iconStr);
fixPos1 := Files.Pos(R);
Files.WriteLInt(R, 0);
fixPos2:= Files.Pos(R);
Files.WriteLInt(R, 0);
fixPos3 := Files.Pos(R);
Files.WriteLInt(R, 0);
ind := 0;
il := imports;
WHILE il # NIL DO
il.ind := ind;
INC(ind);
Files.WriteString(R, il.name);
il := il.next
END;
Files.WriteString(R, "");
c := chapters;
WHILE c # NIL DO
len := c.text.len;
IF c.text = noteText THEN
pos := Files.Pos(R);
Files.Set(R, F, fixPos2);
Files.WriteLInt(R, pos);
Files.Set(R, F, pos)
END;
Files.WriteLInt(R, len);
IF len > 0 THEN
pos := Files.Pos(R);
Texts.Store(c.text, F, pos, len);
Files.Set(R, F, pos+len)
END;
c := c.next
END;
Files.WriteLInt(R, -1);
pos := Files.Pos(R);
Files.Set(R, F, fixPos1);
Files.WriteLInt(R, pos);
Files.Set(R, F, pos);
l := labels;
WHILE l # NIL DO
IF l.export THEN
Files.Write(R, l.frame.mode);
Files.WriteLInt(R, l.frame.pos1);
Files.WriteLInt(R, l.frame.pos2);
Files.WriteString(R, l.name)
END;
l := l.next
END;
Files.Write(R, Books0.none);
pos := Files.Pos(R);
Files.Set(R, F, fixPos3);
Files.WriteLInt(R, pos);
Files.Set(R, F, pos);
Files.Register(F);
Files.Close(F);
IM.F := NIL;
IM.name := bookName;
IM.res := -1;
Display.Broadcast(IM)
END RegisterBook;
(* scanning/parsing procedures *)
PROCEDURE isLetter(ch: CHAR): BOOLEAN;
BEGIN
RETURN ((ch >= "a") & (ch <= "z")) OR ((ch >= "A") & (ch <= "Z"))
END isLetter;
PROCEDURE isDigit(ch: CHAR): BOOLEAN;
BEGIN
RETURN (ch >= "0") & (ch <= "9")
END isDigit;
PROCEDURE isChar(VAR R: Texts.Reader): BOOLEAN;
BEGIN
RETURN ~R.eot & (R.lib IS Fonts.Font)
END isChar;
PROCEDURE Ident(VAR c: CHAR);
VAR
i: INTEGER;
cc: CHAR;
BEGIN
i := 0;
cc := ch;
WHILE (i < Books0.identLen) & isChar(R) & (isLetter(cc) OR isDigit(cc)) DO
ident[i] := cc;
INC(i);
Texts.Read(R, cc)
END;
sym := identSy;
IF i >= Books0.identLen THEN
ident[Books0.identLen-1] := 0X;
Mark("", "identifier too long")
ELSE
ident[i] := 0X
END;
IF ident = "Book" THEN
sym := bookSy
ELSIF ident = "Chapter" THEN
sym := chapterSy
ELSIF ident = "Label" THEN
sym := labelSy
ELSIF ident = "Link" THEN
sym := linkSy
ELSIF ident = "Index" THEN
sym := indexSy
ELSIF ident = "Call" THEN
sym := callSy
ELSIF ident = "Note" THEN
sym := noteSy
ELSE
sym := identSy
END;
c := cc
END Ident;
PROCEDURE GetIdent();
BEGIN
begId := Texts.Pos(R)-1; lastPos := begId;
Ident(ch);
endId := Texts.Pos(R)-1
END GetIdent;
PROCEDURE GetNumber();
VAR i: INTEGER;
BEGIN
i := 0;
WHILE (i < Books0.identLen) & isChar(R) & isDigit(ch) DO
ident[i] := ch;
INC(i);
Texts.Read(R, ch)
END;
sym := numberSy;
IF i >= Books0.identLen THEN
ident[Books0.identLen-1] := 0X;
Mark("", "identifier too long")
ELSE
ident[i] := 0X
END;
Strings.StrToInt(ident, number)
END GetNumber;
PROCEDURE Get();
BEGIN
oldPos := Texts.Pos(R);
lastPos := oldPos;
WHILE isChar(R) & (ch <= " ") DO
Texts.Read(R, ch)
END;
sym := noSy;
IF isChar(R) THEN
CASE ch OF
"A".."Z", "a".."z": GetIdent()
|"0" .. "9": GetNumber()
|"\": sym := bsSy; Texts.Read(R, ch)
|"{": sym := lbrSy; Texts.Read(R, ch)
|"}": sym := rbrSy; Texts.Read(R, ch)
|"*": sym := starSy; Texts.Read(R, ch)
|"-": sym := minusSy; Texts.Read(R, ch)
|"+": sym := plusSy; Texts.Read(R, ch)
|"=": sym := eqSy; Texts.Read(R, ch)
|"[": sym := lprSy; Texts.Read(R, ch)
|"]": sym := rprSy; Texts.Read(R, ch)
|",": sym := commaSy; Texts.Read(R, ch)
ELSE
sym := noSy
END
END
END Get;
PROCEDURE SymToName(s: INTEGER; VAR na: ARRAY OF CHAR);
VAR name: ARRAY Books0.identLen OF CHAR;
BEGIN
CASE s OF
identSy: name := "ident"
|bsSy: name := "\ "
|lbrSy: name := "{ "
|rbrSy: name := "} "
|starSy: name := "* "
|minusSy: name := "- "
|plusSy: name := "+ "
|eqSy: name := "= "
|bookSy: name := "Book "
|chapterSy: name := "Chapter "
|labelSy: name := "Label "
|linkSy: name := "Link "
|indexSy: name := "Index "
|callSy: name := "Call "
|noteSy: name := "Note "
|lprSy: name := "[ "
|rprSy: name := "] "
|commaSy: name := " ,"
|numberSy: name := "number"
ELSE
name := "???"
END;
na := name
END SymToName;
PROCEDURE Check(sy: INTEGER);
VAR name: ARRAY Books0.identLen OF CHAR;
BEGIN
Get();
IF sy # sym THEN
SymToName(sy, name);
Mark(name, " expected");
number := 0;
ident := ""
END
END Check;
PROCEDURE GetString(objs: BOOLEAN);
BEGIN
expand := FALSE; eol := FALSE;
begId := Texts.Pos(R)-1;
WHILE ~R.eot & ~(isChar(R) & (ch = "}")) & (objs OR isChar(R)) DO
IF isChar(R) & (ch = "\") THEN
Texts.Read(R, ch);
IF isChar(R) THEN
CASE ch OF
"}": expand := TRUE; Texts.Read(R, ch)
|"\": expand := TRUE; Texts.Read(R, ch)
|"A" .. "Z", "a" .. "z": Mark("", "commands not allowed in string")
ELSE
END
END
ELSIF isChar(R) & (ch = Books.EOL) THEN
eol := TRUE;
Texts.Read(R, ch)
ELSE
Texts.Read(R, ch)
END
END;
IF R.eot THEN
Mark("", "unexpected end of text")
ELSIF ~objs & ~isChar(R) THEN
Mark("", "objects not allow in string")
END;
endId := Texts.Pos(R)-1
END GetString;
PROCEDURE GetName(VAR name: ARRAY OF CHAR): BOOLEAN;
VAR
i: INTEGER;
dot: BOOLEAN;
BEGIN
dot := FALSE;
begId := Texts.Pos(R)-1;
i := 0;
WHILE (i < Books0.nameLen+1+Books0.identLen) & isChar(R) & (isLetter(ch) OR isDigit(ch) OR (ch = ".")) DO
IF ch = "." THEN
dot := TRUE
END;
name[i] := ch;
INC(i);
Texts.Read(R, ch)
END;
IF i >= Books0.nameLen+1+Books0.identLen THEN
name[Books0.nameLen+Books0.identLen] := 0X;
Mark("", "identifier too long")
ELSIF ~dot & (i >= Books0.identLen) THEN
name[Books0.identLen-1] := 0X;
Mark("", "identifier too long")
ELSE
name[i] := 0X
END;
endId := Texts.Pos(R)-1;
RETURN dot
END GetName;
(* process the contents of the chapters *)
PROCEDURE Contents();
VAR
beg, end: LONGINT;
Fi: Texts.Finder;
obj: Objects.Object;
BEGIN
LOOP
beg := Texts.Pos(R)-1;
WHILE ~R.eot & ~((ch = "\") & (R.lib IS Fonts.Font)) DO
IF ~(R.lib IS Fonts.Font) THEN
Texts.OpenFinder(Fi, inText, Texts.Pos(R)-1);
Texts.FindObj(Fi, obj);
(*!!
IF (obj # NIL) & (obj IS Gadgets.Frame) & ~(Gadgets.nodelete IN obj(Gadgets.Frame).state) THEN
INCL(obj(Gadgets.Frame).state, Gadgets.nodelete);
INCL(obj(Gadgets.Frame).state, Gadgets.nomove)
END;
*)
IncObj(curChap)
END;
Texts.Read(R, ch)
END;
end := Texts.Pos(R)-1;
Texts.Save(inText, beg, end, B);
beg := curChap.text.len;
Texts.Append(curChap.text, B);
IF Books.formatText IN options THEN
ChangeFont(curChap.text, Fonts.Default, BookDocs.textFont, beg, curChap.text.len)
END;
IF R.eot THEN
sym := noSy; EXIT
ELSE