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
|
(* mcError.mod provides an interface between the string handling modules.
Copyright (C) 2015-2024 Free Software Foundation, Inc.
Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
This file is part of GNU Modula-2.
GNU Modula-2 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.
GNU Modula-2 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 GNU Modula-2; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. *)
IMPLEMENTATION MODULE mcError ;
FROM ASCII IMPORT nul, nl ;
FROM DynamicStrings IMPORT String, InitString, InitStringCharStar, ConCat, ConCatChar, Mark, string, KillString, Dup ;
FROM FIO IMPORT StdOut, WriteNBytes, Close, FlushBuffer ;
FROM StrLib IMPORT StrLen, StrEqual ;
FROM FormatStrings IMPORT Sprintf0, Sprintf1, Sprintf2, Sprintf3 ;
FROM Storage IMPORT ALLOCATE, DEALLOCATE ;
FROM M2RTS IMPORT ExitOnHalt ;
FROM SYSTEM IMPORT ADDRESS ;
IMPORT StdIO ;
FROM nameKey IMPORT Name, keyToCharStar ;
FROM mcLexBuf IMPORT findFileNameFromToken, tokenToLineNo, tokenToColumnNo, getTokenNo ;
FROM mcPrintf IMPORT printf0, printf1, printf2 ;
CONST
Debugging = TRUE ;
DebugTrace = FALSE ;
Xcode = TRUE ;
TYPE
error = POINTER TO RECORD
parent,
child,
next : error ;
fatal : BOOLEAN ;
s : String ;
token : CARDINAL ; (* index of token causing the error *)
END ;
VAR
head : error ;
inInternal: BOOLEAN ;
(*
cast - casts a := b
*)
PROCEDURE cast (VAR a: ARRAY OF BYTE; b: ARRAY OF BYTE) ;
VAR
i: CARDINAL ;
BEGIN
IF HIGH(a)=HIGH(b)
THEN
FOR i := 0 TO HIGH(a) DO
a[i] := b[i]
END
END
END cast ;
(*
translateNameToString - takes a format specification string, a, and
if they consist of of %a then this is translated
into a String and %a is replaced by %s.
*)
PROCEDURE translateNameToCharStar (VAR a: ARRAY OF CHAR;
n: CARDINAL) : BOOLEAN ;
VAR
argno,
i, h : CARDINAL ;
BEGIN
argno := 1 ;
i := 0 ;
h := StrLen (a) ;
WHILE i<h DO
IF (a[i]='%') AND (i+1<h)
THEN
IF (a[i+1]='a') AND (argno=n)
THEN
a[i+1] := 's' ;
RETURN TRUE
END ;
INC (argno) ;
IF argno>n
THEN
(* all done *)
RETURN FALSE
END
END ;
INC (i)
END ;
RETURN FALSE
END translateNameToCharStar ;
(*
outString - writes the contents of String to stdout.
The string, s, is destroyed.
*)
PROCEDURE outString (file: String; line, col: CARDINAL; s: String) ;
VAR
leader : String ;
p, q : POINTER TO CHAR ;
space,
newline: BOOLEAN ;
BEGIN
INC (col) ;
IF Xcode
THEN
leader := Sprintf2(Mark(InitString('%s:%d:')), file, line)
ELSE
leader := Sprintf3(Mark(InitString('%s:%d:%d:')), file, line, col)
END ;
p := string(s) ;
newline := TRUE ;
space := FALSE ;
WHILE (p#NIL) AND (p^#nul) DO
IF newline
THEN
q := string (leader) ;
WHILE (q#NIL) AND (q^#nul) DO
StdIO.Write (q^) ;
INC (q)
END
END ;
newline := (p^=nl) ;
space := (p^=' ') ;
IF newline AND Xcode
THEN
printf1 ('(pos: %d)', col)
END ;
StdIO.Write (p^) ;
INC (p)
END ;
IF NOT newline
THEN
IF Xcode
THEN
IF NOT space
THEN
StdIO.Write (' ')
END ;
printf1 ('(pos: %d)', col)
END ;
StdIO.Write (nl)
END ;
FlushBuffer (StdOut) ;
IF NOT Debugging
THEN
s := KillString (s) ;
leader := KillString (leader)
END
END outString ;
(*
internalError - displays an internal error message together with the compiler source
file and line number.
This function is not buffered and is used when the compiler is about
to give up.
*)
PROCEDURE internalError (a: ARRAY OF CHAR; file: ARRAY OF CHAR; line: CARDINAL) ;
BEGIN
ExitOnHalt (1) ;
IF NOT inInternal
THEN
inInternal := TRUE ;
flushErrors ;
outString (findFileNameFromToken (getTokenNo (), 0),
tokenToLineNo (getTokenNo (), 0),
tokenToColumnNo (getTokenNo (), 0),
Mark(InitString ('*** fatal error ***')))
END ;
outString (Mark (InitString (file)), line, 0,
ConCat (Mark (InitString('*** internal error *** ')), Mark (InitString (a)))) ;
HALT
END internalError ;
(* ***************************************************************************
The following routines are used for normal syntax and semantic error reporting
*************************************************************************** *)
(*
writeFormat0 - displays the source module and line together
with the encapsulated format string.
Used for simple error messages tied to the current token.
*)
PROCEDURE writeFormat0 (a: ARRAY OF CHAR) ;
VAR
e: error ;
BEGIN
e := newError (getTokenNo ()) ;
WITH e^ DO
s := Sprintf0 (Mark (InitString(a)))
END
END writeFormat0 ;
(*
WarnFormat0 - displays the source module and line together
with the encapsulated format string.
Used for simple warning messages tied to the current token.
*)
PROCEDURE warnFormat0 (a: ARRAY OF CHAR) ;
VAR
e: error ;
BEGIN
e := newWarning (getTokenNo()) ;
WITH e^ DO
s := Sprintf0 (Mark (InitString (a)))
END
END warnFormat0 ;
(*
DoFormat1 -
*)
PROCEDURE doFormat1 (a: ARRAY OF CHAR; w: ARRAY OF BYTE) : String ;
VAR
s: String ;
n: Name ;
BEGIN
IF translateNameToCharStar(a, 1)
THEN
cast(n, w) ;
s := Mark (InitStringCharStar (keyToCharStar (n))) ;
s := Sprintf1 (Mark (InitString (a)), s)
ELSE
s := Sprintf1 (Mark (InitString (a)), w)
END ;
RETURN s
END doFormat1 ;
(*
writeFormat1 - displays the source module and line together
with the encapsulated format string.
Used for simple error messages tied to the current token.
*)
PROCEDURE writeFormat1 (a: ARRAY OF CHAR; w: ARRAY OF BYTE) ;
VAR
e: error ;
BEGIN
e := newError (getTokenNo ()) ;
e^.s := doFormat1 (a, w)
END writeFormat1 ;
(*
warnFormat1 - displays the source module and line together
with the encapsulated format string.
Used for simple warning messages tied to the current token.
*)
PROCEDURE warnFormat1 (a: ARRAY OF CHAR; w: ARRAY OF BYTE) ;
VAR
e: error ;
BEGIN
e := newWarning (getTokenNo ()) ;
e^.s := doFormat1 (a, w)
END warnFormat1 ;
(*
doFormat2 -
*)
PROCEDURE doFormat2 (a: ARRAY OF CHAR; w1, w2: ARRAY OF BYTE) : String ;
VAR
n : Name ;
s,
s1, s2: String ;
b : BITSET ;
BEGIN
b := {} ;
IF translateNameToCharStar (a, 1)
THEN
cast (n, w1) ;
s1 := Mark (InitStringCharStar (keyToCharStar (n))) ;
INCL (b, 1)
END ;
IF translateNameToCharStar(a, 2)
THEN
cast (n, w2) ;
s2 := Mark (InitStringCharStar (keyToCharStar(n))) ;
INCL (b, 2)
END ;
CASE b OF
{} : s := Sprintf2 (Mark (InitString (a)), w1, w2) |
{1} : s := Sprintf2 (Mark (InitString (a)), s1, w2) |
{2} : s := Sprintf2 (Mark (InitString (a)), w1, s2) |
{1,2}: s := Sprintf2 (Mark (InitString (a)), s1, s2)
ELSE
HALT
END ;
RETURN s
END doFormat2 ;
(*
writeFormat2 - displays the module and line together with the encapsulated
format strings.
Used for simple error messages tied to the current token.
*)
PROCEDURE writeFormat2 (a: ARRAY OF CHAR; w1, w2: ARRAY OF BYTE) ;
VAR
e: error ;
BEGIN
e := newError (getTokenNo()) ;
e^.s := doFormat2 (a, w1, w2)
END writeFormat2 ;
PROCEDURE doFormat3 (a: ARRAY OF CHAR; w1, w2, w3: ARRAY OF BYTE) : String ;
VAR
n : Name ;
s, s1, s2, s3: String ;
b : BITSET ;
BEGIN
b := {} ;
IF translateNameToCharStar (a, 1)
THEN
cast (n, w1) ;
s1 := Mark (InitStringCharStar (keyToCharStar (n))) ;
INCL(b, 1)
END ;
IF translateNameToCharStar (a, 2)
THEN
cast (n, w2) ;
s2 := Mark (InitStringCharStar (keyToCharStar (n))) ;
INCL (b, 2)
END ;
IF translateNameToCharStar (a, 3)
THEN
cast(n, w3) ;
s3 := Mark (InitStringCharStar (keyToCharStar (n))) ;
INCL (b, 3)
END ;
CASE b OF
{} : s := Sprintf3 (Mark (InitString (a)), w1, w2, w3) |
{1} : s := Sprintf3 (Mark (InitString (a)), s1, w2, w3) |
{2} : s := Sprintf3 (Mark (InitString (a)), w1, s2, w3) |
{1,2} : s := Sprintf3 (Mark (InitString (a)), s1, s2, w3) |
{3} : s := Sprintf3 (Mark (InitString (a)), w1, w2, s3) |
{1,3} : s := Sprintf3 (Mark (InitString (a)), s1, w2, s3) |
{2,3} : s := Sprintf3 (Mark (InitString (a)), w1, s2, s3) |
{1,2,3}: s := Sprintf3 (Mark (InitString (a)), s1, s2, s3)
ELSE
HALT
END ;
RETURN s
END doFormat3 ;
(*
writeFormat3 - displays the module and line together with the encapsulated
format strings.
Used for simple error messages tied to the current token.
*)
PROCEDURE writeFormat3 (a: ARRAY OF CHAR; w1, w2, w3: ARRAY OF BYTE) ;
VAR
e: error ;
BEGIN
e := newError (getTokenNo ()) ;
e^.s := doFormat3 (a, w1, w2, w3)
END writeFormat3 ;
(*
newError - creates and returns a new error handle.
*)
PROCEDURE newError (atTokenNo: CARDINAL) : error ;
VAR
e, f: error ;
BEGIN
NEW (e) ;
WITH e^ DO
s := NIL ;
token := atTokenNo ;
next := NIL ;
parent := NIL ;
child := NIL ;
fatal := TRUE
END ;
IF (head=NIL) OR (head^.token>atTokenNo)
THEN
e^.next := head ;
head := e
ELSE
f := head ;
WHILE (f^.next#NIL) AND (f^.next^.token<atTokenNo) DO
f := f^.next
END ;
e^.next := f^.next ;
f^.next := e
END ;
RETURN e
END newError ;
(*
newWarning - creates and returns a new error handle suitable for a warning.
A warning will not stop compilation.
*)
PROCEDURE newWarning (atTokenNo: CARDINAL) : error ;
VAR
e: error ;
BEGIN
e := newError (atTokenNo) ;
e^.fatal := FALSE ;
RETURN e
END newWarning ;
(*
chainError - creates and returns a new error handle, this new error
is associated with, e, and is chained onto the end of, e.
If, e, is NIL then the result to NewError is returned.
*)
PROCEDURE chainError (atTokenNo: CARDINAL; e: error) : error ;
VAR
f: error ;
BEGIN
IF e=NIL
THEN
RETURN newError (atTokenNo)
ELSE
NEW (f) ;
WITH f^ DO
s := NIL ;
token := atTokenNo ;
next := e^.child ;
parent := e ;
child := NIL ;
fatal := e^.fatal
END ;
e^.child := f
END ;
RETURN f
END chainError ;
(*
errorFormat routines provide a printf capability for the error handle.
*)
PROCEDURE errorFormat0 (e: error; a: ARRAY OF CHAR) ;
BEGIN
WITH e^ DO
IF s=NIL
THEN
s := Sprintf0 (Mark (InitString (a)))
ELSE
s := ConCat(s, Mark(Sprintf0 (Mark (InitString (a)))))
END
END
END errorFormat0 ;
PROCEDURE errorFormat1 (e: error; a: ARRAY OF CHAR; w: ARRAY OF BYTE) ;
VAR
s1: String ;
BEGIN
s1 := doFormat1 (a, w) ;
WITH e^ DO
IF s=NIL
THEN
s := s1
ELSE
s := ConCat (s, Mark (s1))
END
END
END errorFormat1 ;
PROCEDURE errorFormat2 (e: error; a: ARRAY OF CHAR; w1, w2: ARRAY OF BYTE) ;
VAR
s1: String ;
BEGIN
s1 := doFormat2 (a, w1, w2) ;
WITH e^ DO
IF s=NIL
THEN
s := s1
ELSE
s := ConCat (s, Mark (s1))
END
END
END errorFormat2 ;
PROCEDURE errorFormat3 (e: error; a: ARRAY OF CHAR;
w1, w2, w3: ARRAY OF BYTE) ;
VAR
s1: String ;
BEGIN
s1 := doFormat3 (a, w1, w2, w3) ;
WITH e^ DO
IF s=NIL
THEN
s := s1
ELSE
s := ConCat (s, Mark (s1))
END
END
END errorFormat3 ;
PROCEDURE errorString (e: error; str: String) ;
BEGIN
WITH e^ DO
s := str
END
END errorString ;
(*
init - initializes the error list.
*)
PROCEDURE init ;
BEGIN
head := NIL ;
inInternal := FALSE
END init ;
(*
checkIncludes - generates a sequence of error messages which determine the relevant
included file and line number.
For example:
gcc a.c
In file included from b.h:1,
from a.c:1:
c.h:1: parse error before `and'
where a.c is: #include "b.h"
b.h is: #include "c.h"
c.h is: and this and that
we attempt to follow the error messages that gcc issues.
*)
PROCEDURE checkIncludes (token: CARDINAL; depth: CARDINAL) ;
VAR
included: String ;
lineno : CARDINAL ;
BEGIN
included := findFileNameFromToken (token, depth+1) ;
IF included#NIL
THEN
lineno := tokenToLineNo (token, depth+1) ;
IF depth=0
THEN
printf2('In file included from %s:%d', included, lineno)
ELSE
printf2(' from %s:%d', included, lineno)
END ;
IF findFileNameFromToken (token, depth+2)=NIL
THEN
printf0(':\n')
ELSE
printf0(',\n')
END ;
checkIncludes (token, depth+1)
END
END checkIncludes ;
(*
flushAll - flushes all errors in list, e.
*)
PROCEDURE flushAll (e: error; FatalStatus: BOOLEAN) : BOOLEAN ;
VAR
f : error ;
written: BOOLEAN ;
BEGIN
written := FALSE ;
IF e#NIL
THEN
REPEAT
WITH e^ DO
IF (FatalStatus=fatal) AND (s#NIL)
THEN
checkIncludes (token, 0) ;
IF fatal
THEN
s := ConCat (InitString (' error: '), Mark (s))
ELSE
s := ConCat (InitString(' warning: '), Mark (s))
END ;
outString (findFileNameFromToken (token, 0),
tokenToLineNo (token, 0), tokenToColumnNo (token, 0), s) ;
IF (child#NIL) AND flushAll (child, FatalStatus)
THEN
END ;
s := NIL ;
written := TRUE
END
END ;
f := e ;
e := e^.next ;
IF NOT Debugging
THEN
WITH f^ DO
s := KillString(s)
END ;
DISPOSE (f)
END ;
UNTIL e=NIL
END ;
RETURN written
END flushAll ;
(*
flushErrors - switches the output channel to the error channel
and then writes out all errors.
*)
PROCEDURE flushErrors ;
BEGIN
IF DebugTrace
THEN
printf0 ('\nFlushing all errors\n') ;
printf0 ('===================\n')
END ;
IF flushAll (head, TRUE)
THEN
ExitOnHalt (1) ;
HALT
END
END flushErrors ;
(*
flushWarnings - switches the output channel to the error channel
and then writes out all warnings.
If an error is present the compilation is terminated,
if warnings only were emitted then compilation will
continue.
*)
PROCEDURE flushWarnings ;
BEGIN
IF flushAll (head, FALSE)
THEN
END
END flushWarnings ;
(*
errorStringsAt2 - given error strings, s1, and, s2, it places these
strings at token positions, tok1 and tok2, respectively.
Both strings are consumed.
*)
PROCEDURE errorStringsAt2 (s1, s2: String; tok1, tok2: CARDINAL) ;
VAR
e: error ;
BEGIN
IF s1=s2
THEN
s2 := Dup (s1)
END ;
e := newError (tok1) ;
errorString (e, s1) ;
errorString (chainError (tok2, e), s2)
END errorStringsAt2 ;
(*
errorStringAt2 - given an error string, s, it places this
string at token positions, tok1 and tok2, respectively.
The string is consumed.
*)
PROCEDURE errorStringAt2 (s: String; tok1, tok2: CARDINAL) ;
BEGIN
errorStringsAt2 (s, s, tok1, tok2)
END errorStringAt2 ;
(*
errorStringAt - given an error string, s, it places this
string at token position, tok.
The string is consumed.
*)
PROCEDURE errorStringAt (s: String; tok: CARDINAL) ;
VAR
e: error ;
BEGIN
e := newError (tok) ;
errorString (e, s)
END errorStringAt ;
(*
warnStringsAt2 - given warning strings, s1, and, s2, it places these
strings at token positions, tok1 and tok2, respectively.
Both strings are consumed.
*)
PROCEDURE warnStringsAt2 (s1, s2: String; tok1, tok2: CARDINAL) ;
VAR
e: error ;
BEGIN
IF s1=s2
THEN
s2 := Dup (s1)
END ;
e := newWarning (tok1) ;
errorString (e, s1) ;
errorString (chainError (tok2, e), s2)
END warnStringsAt2 ;
(*
warnStringAt2 - given an warning string, s, it places this
string at token positions, tok1 and tok2, respectively.
The string is consumed.
*)
PROCEDURE warnStringAt2 (s: String; tok1, tok2: CARDINAL) ;
BEGIN
warnStringsAt2 (s, s, tok1, tok2)
END warnStringAt2 ;
(*
warnStringAt - given an error string, s, it places this
string at token position, tok.
The string is consumed.
*)
PROCEDURE warnStringAt (s: String; tok: CARDINAL) ;
VAR
e: error ;
BEGIN
e := newWarning (tok) ;
errorString (e, s)
END warnStringAt ;
(*
errorAbort0 - aborts compiling, it flushes all warnings and errors before aborting.
*)
PROCEDURE errorAbort0 (a: ARRAY OF CHAR) ;
BEGIN
flushWarnings ;
IF NOT StrEqual (a, '')
THEN
writeFormat0(a)
END ;
IF NOT flushAll(head, TRUE)
THEN
writeFormat0 ('unidentified error') ;
IF flushAll (head, TRUE)
THEN
END
END ;
ExitOnHalt (1) ;
HALT
END errorAbort0 ;
BEGIN
init
END mcError.
|