xref: /csrg-svn/usr.bin/error/pi.c (revision 1458)
1*1458Sroot static	char *sccsid = "@(#)pi.c	1.1 (Berkeley) 10/16/80";
2*1458Sroot #include <stdio.h>
3*1458Sroot #include <ctype.h>
4*1458Sroot #include "error.h"
5*1458Sroot 
6*1458Sroot extern	char	*currentfilename;
7*1458Sroot static	char	*c_linenumber;
8*1458Sroot static	char	*unk_hdr[] = {"In", "program", "???"};
9*1458Sroot static	char	**c_header = &unk_hdr[0];
10*1458Sroot 
11*1458Sroot /*
12*1458Sroot  *	Attempt to handle error messages produced by pi (and by pc)
13*1458Sroot  *
14*1458Sroot  *	problem #1:	There is no file name available when a file does not
15*1458Sroot  *			use a #include; this will have to be given to error
16*1458Sroot  *			in the command line.
17*1458Sroot  *	problem #2:	pi doesn't always tell you what line number
18*1458Sroot  *			a error refers to; for example during the tree
19*1458Sroot  *			walk phase of code generation and error detection,
20*1458Sroot  *			an error can refer to "variable foo in procedure bletch"
21*1458Sroot  *			without giving a line number
22*1458Sroot  *	problem #3:	line numbers, when available, are attached to
23*1458Sroot  *			the source line, along with the source line itself
24*1458Sroot  *			These line numbers must be extracted, and
25*1458Sroot  *			the source line thrown away.
26*1458Sroot  *	problem #4:	Some error messages produce more than one line number
27*1458Sroot  *			on the same message.
28*1458Sroot  *			There are only two (I think):
29*1458Sroot  *				%s undefined on line%s
30*1458Sroot  *				%s improperly used on line%s
31*1458Sroot  *			here, the %s makes line plural or singular.
32*1458Sroot  *
33*1458Sroot  *	Here are the error strings used in pi version 1.2 that can refer
34*1458Sroot  *	to a file name or line number:
35*1458Sroot  *
36*1458Sroot  *		Multiply defined label in case, lines %d and %d
37*1458Sroot  *		Goto %s from line %d is into a structured statement
38*1458Sroot  *		End matched %s on line %d
39*1458Sroot  *		Inserted keyword end matching %s on line %d
40*1458Sroot  *
41*1458Sroot  *	Here are the general pi patterns recognized:
42*1458Sroot  *	define piptr == -.*^-.*
43*1458Sroot  *	define msg = .*
44*1458Sroot  *	define digit = [0-9]
45*1458Sroot  *	definename = .*
46*1458Sroot  *	define date_format letter*3 letter*3 (digit | (digit digit))
47*1458Sroot  *			(digit | (digit digit)):digit*2 digit*4
48*1458Sroot  *
49*1458Sroot  *	{e,E} (piptr) (msg)	Encounter an error during textual scan
50*1458Sroot  *	E {digit}* - (msg)	Have an error message that refers to a new line
51*1458Sroot  *	E - msg			Have an error message that refers to current
52*1458Sroot  *					function, program or procedure
53*1458Sroot  *	(date_format) (name):	When switch compilation files
54*1458Sroot  *	... (msg)		When refer to the previous line
55*1458Sroot  *	'In' ('procedure'|'function'|'program') (name):
56*1458Sroot  *				pi is now complaining about 2nd pass errors.
57*1458Sroot  *
58*1458Sroot  *	Here is the output from a compilation
59*1458Sroot  *
60*1458Sroot  *
61*1458Sroot  *	     2  	var	i:integer;
62*1458Sroot  *	e --------------^--- Inserted ';'
63*1458Sroot  *	E 2 - All variables must be declared in one var part
64*1458Sroot  *	E 5 - Include filename must end in .i
65*1458Sroot  *	Mon Apr 21 15:56 1980  test.h:
66*1458Sroot  *	     2  begin
67*1458Sroot  *	e ------^--- Inserted ';'
68*1458Sroot  *	Mon Apr 21 16:06 1980  test.p:
69*1458Sroot  *	E 2 - Function type must be specified
70*1458Sroot  *	     6  procedure foo(var x:real);
71*1458Sroot  *	e ------^--- Inserted ';'
72*1458Sroot  *	In function bletch:
73*1458Sroot  *	  E - No assignment to the function variable
74*1458Sroot  *	  w - variable x is never used
75*1458Sroot  *	E 6 - foo is already defined in this block
76*1458Sroot  *	In procedure foo:
77*1458Sroot  *	  w - variable x is neither used nor set
78*1458Sroot  *	     9  	z : = 23;
79*1458Sroot  *	E --------------^--- Undefined variable
80*1458Sroot  *	    10  	y = [1];
81*1458Sroot  *	e ----------------^--- Inserted ':'
82*1458Sroot  *	    13  	z := 345.;
83*1458Sroot  *	e -----------------------^--- Digits required after decimal point
84*1458Sroot  *	E 10 - Constant set involved in non set context
85*1458Sroot  *	E 11 - Type clash: real is incompatible with integer
86*1458Sroot  *	   ... Type of expression clashed with type of variable in assignment
87*1458Sroot  *	E 12 - Parameter type not identical to type of var parameter x of foo
88*1458Sroot  *	In program mung:
89*1458Sroot  *	  w - variable y is never used
90*1458Sroot  *	  w - type foo is never used
91*1458Sroot  *	  w - function bletch is never used
92*1458Sroot  *	  E - z undefined on lines 9 13
93*1458Sroot  */
94*1458Sroot char *Months[] = {
95*1458Sroot 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
96*1458Sroot 	"Jul", "Aug", "Sep", "Oct","Nov", "Dec",
97*1458Sroot 	0
98*1458Sroot };
99*1458Sroot char *Days[] = {
100*1458Sroot 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", 0
101*1458Sroot };
102*1458Sroot char *Piroutines[] = {
103*1458Sroot 		"program", "function", "procedure", 0
104*1458Sroot };
105*1458Sroot 
106*1458Sroot 
107*1458Sroot static boolean	structured, multiple;
108*1458Sroot 
109*1458Sroot char *pi_Endmatched[] = {"End", "matched"};
110*1458Sroot char *pi_Inserted[] = {"Inserted", "keyword", "end", "matching"};
111*1458Sroot 
112*1458Sroot char *pi_multiple[] = {"Mutiply", "defined", "label", "in", "case,", "line"};
113*1458Sroot char *pi_structured[] = {"is", "into", "a", "structured", "statement"};
114*1458Sroot 
115*1458Sroot char *pi_und1[] = {"undefined", "on", "line"};
116*1458Sroot char *pi_und2[] = {"undefined", "on", "lines"};
117*1458Sroot char *pi_imp1[] = {"improperly", "used", "on", "line"};
118*1458Sroot char *pi_imp2[] = {"improperly", "used", "on", "lines"};
119*1458Sroot 
120*1458Sroot boolean alldigits(string)
121*1458Sroot 	register	char	*string;
122*1458Sroot {
123*1458Sroot 	for (; *string && isdigit(*string); string++)
124*1458Sroot 		continue;
125*1458Sroot 	return(*string == '\0');
126*1458Sroot }
127*1458Sroot boolean instringset(member, set)
128*1458Sroot 			char	*member;
129*1458Sroot 	register	char	**set;
130*1458Sroot {
131*1458Sroot 	for(; *set; set++){
132*1458Sroot 		if (strcmp(*set, member) == 0)
133*1458Sroot 			return(TRUE);
134*1458Sroot 	}
135*1458Sroot 	return(FALSE);
136*1458Sroot }
137*1458Sroot 
138*1458Sroot boolean isdateformat(wordc, wordv)
139*1458Sroot 	int	wordc;
140*1458Sroot 	char	**wordv;
141*1458Sroot {
142*1458Sroot 	return(
143*1458Sroot 	        (wordc == 5)
144*1458Sroot 	     && (instringset(wordv[0], Days))
145*1458Sroot 	     && (instringset(wordv[1], Months))
146*1458Sroot 	     && (alldigits(wordv[2]))
147*1458Sroot 	     && (alldigits(wordv[4])) );
148*1458Sroot }
149*1458Sroot 
150*1458Sroot boolean piptr(string)
151*1458Sroot 	register	char	*string;
152*1458Sroot {
153*1458Sroot 	int	state = 0;
154*1458Sroot 	if (*string != '-')
155*1458Sroot 		return(FALSE);
156*1458Sroot 	while (*string && *string == '-')
157*1458Sroot 		string++;
158*1458Sroot 	if (*string != '^')
159*1458Sroot 		return(FALSE);
160*1458Sroot 	string++;
161*1458Sroot 	while (*string && *string == '-')
162*1458Sroot 		string++;
163*1458Sroot 	return(*string == '\0');
164*1458Sroot }
165*1458Sroot 
166*1458Sroot extern	int	wordc;
167*1458Sroot extern	char	**wordv;
168*1458Sroot 
169*1458Sroot Errorclass pi()
170*1458Sroot {
171*1458Sroot 	char	**nwordv;
172*1458Sroot 	char	buffer[128];
173*1458Sroot 
174*1458Sroot 	if (   ( strlen(wordv[1]) == 1)
175*1458Sroot 	    && ( (wordv[1][0] == 'e') || (wordv[1][0] == 'E') )
176*1458Sroot 	    && ( piptr(wordv[2]) )
177*1458Sroot 	) {
178*1458Sroot 		boolean	longpiptr = 0;
179*1458Sroot 		/*
180*1458Sroot 		 *	We have recognized a first pass error of the form:
181*1458Sroot 		 *	letter ------^---- message
182*1458Sroot 		 *
183*1458Sroot 		 *	turn into an error message of the form:
184*1458Sroot 		 *
185*1458Sroot 		 *	file line 'pascal errortype' letter \n |---- message
186*1458Sroot 		 *	or of the form:
187*1458Sroot 		 *	file line letter |---- message
188*1458Sroot 		 *		when there are strlen("(*[pi]") or more
189*1458Sroot 		 *		preceding '-' on the error pointer.
190*1458Sroot 		 *
191*1458Sroot 		 *	Where the | is intended to be a down arrow, so that
192*1458Sroot 		 *	the pi error messages can be inserted above the
193*1458Sroot 		 *	line in error, instead of below.  (All of the other
194*1458Sroot 		 *	langauges put thier messages before the source line,
195*1458Sroot 		 *	instead of after it as does pi.)
196*1458Sroot 		 *
197*1458Sroot 		 *	where the pointer to the error has been truncated
198*1458Sroot 		 *	by 6 characters to account for the fact that
199*1458Sroot 		 *	the pointer points into a tab preceded input line.
200*1458Sroot 		 */
201*1458Sroot 		language = INPI;
202*1458Sroot 		substitute(wordv[2], '^', '|');
203*1458Sroot 		longpiptr = position(wordv[2],'|') > (6+8);
204*1458Sroot 		nwordv = wordvsplice(longpiptr ? 2 : 4, wordc, wordv+1);
205*1458Sroot 		nwordv[0] = strsave(currentfilename);
206*1458Sroot 		nwordv[1] = strsave(c_linenumber);
207*1458Sroot 		if (!longpiptr){
208*1458Sroot 			nwordv[2] = "pascal errortype";
209*1458Sroot 			nwordv[3] = wordv[1];
210*1458Sroot 			nwordv[4] = strsave("%%%\n");
211*1458Sroot 			if (strlen(nwordv[5]) > (8-2))	/* this is the pointer */
212*1458Sroot 				nwordv[5] += (8-2);	/* bump over 6 characters */
213*1458Sroot 		}
214*1458Sroot 		wordv = nwordv - 1;		/* convert to 1 based */
215*1458Sroot 		wordc += longpiptr ? 2 : 4;
216*1458Sroot 		return(C_TRUE);
217*1458Sroot 	}
218*1458Sroot 	if (   (wordc >= 4)
219*1458Sroot 	    && (strlen(wordv[1]) == 1)
220*1458Sroot 	    && ( (*wordv[1] == 'E') || (*wordv[1] == 'w') || (*wordv[1] == 'e') )
221*1458Sroot 	    && (alldigits(wordv[2]))
222*1458Sroot 	    && (strlen(wordv[3]) == 1)
223*1458Sroot 	    && (wordv[3][0] == '-')
224*1458Sroot 	){
225*1458Sroot 		/*
226*1458Sroot 		 *	Message of the form: letter linenumber - message
227*1458Sroot 		 *	Turn into form: filename linenumber letter - message
228*1458Sroot 		 */
229*1458Sroot 		language = INPI;
230*1458Sroot 		nwordv = wordvsplice(1, wordc, wordv + 1);
231*1458Sroot 		nwordv[0] = strsave(currentfilename);
232*1458Sroot 		nwordv[1] = wordv[2];
233*1458Sroot 		nwordv[2] = wordv[1];
234*1458Sroot 		c_linenumber = wordv[2];
235*1458Sroot 		wordc += 1;
236*1458Sroot 		wordv = nwordv - 1;
237*1458Sroot 		return(C_TRUE);
238*1458Sroot 	}
239*1458Sroot 	if (   (wordc >= 3)
240*1458Sroot 	    && (strlen(wordv[1]) == 1)
241*1458Sroot 	    && ( (*(wordv[1]) == 'E') || (*(wordv[1]) == 'w') || (*(wordv[1]) == 'e') )
242*1458Sroot 	    && (strlen(wordv[2]) == 1)
243*1458Sroot 	    && (wordv[2][0] == '-')
244*1458Sroot 	) {
245*1458Sroot 		/*
246*1458Sroot 		 *	Message of the form: letter - message
247*1458Sroot 		 *	This happens only when we are traversing the tree
248*1458Sroot 		 *	during the second pass of pi, and discover semantic
249*1458Sroot 		 *	errors.
250*1458Sroot 		 *
251*1458Sroot 		 *	We have already (presumably) saved the header message
252*1458Sroot 		 *	and can now construct a nulled error message for the
253*1458Sroot 		 *	current file.
254*1458Sroot 		 *
255*1458Sroot 		 *	Turns into a message of the form:
256*1458Sroot 		 *	filename (header) letter - message
257*1458Sroot 		 *
258*1458Sroot 		 *	First, see if it is a message referring to more than
259*1458Sroot 		 *	one line number.  Only of the form:
260*1458Sroot  		 *		%s undefined on line%s
261*1458Sroot  		 *		%s improperly used on line%s
262*1458Sroot 		 */
263*1458Sroot 		boolean undefined = 0;
264*1458Sroot 		int	wordindex;
265*1458Sroot 
266*1458Sroot 		language = INPI;
267*1458Sroot 		if (    (undefined = (wordvcmp(wordv+2, 3, pi_und1) == 0) )
268*1458Sroot 		     || (undefined = (wordvcmp(wordv+2, 3, pi_und2) == 0) )
269*1458Sroot 		     || (wordvcmp(wordv+2, 4, pi_imp1) == 0)
270*1458Sroot 		     || (wordvcmp(wordv+2, 4, pi_imp2) == 0)
271*1458Sroot 		){
272*1458Sroot 			for (wordindex = undefined ? 5 : 6; wordindex <= wordc;
273*1458Sroot 			    wordindex++){
274*1458Sroot 				nwordv = wordvsplice(2, undefined ? 2 : 3, wordv+1);
275*1458Sroot 				nwordv[0] = strsave(currentfilename);
276*1458Sroot 				nwordv[1] = wordv[wordindex];
277*1458Sroot 				if (wordindex != wordc)
278*1458Sroot 					erroradd(undefined ? 4 : 5, nwordv,
279*1458Sroot 						C_TRUE, C_UNKNOWN);
280*1458Sroot 			}
281*1458Sroot 			wordc = undefined ? 4 : 5;
282*1458Sroot 			wordv = nwordv - 1;
283*1458Sroot 			return(C_TRUE);
284*1458Sroot 		}
285*1458Sroot 
286*1458Sroot 		nwordv = wordvsplice(1+3, wordc, wordv+1);
287*1458Sroot 		nwordv[0] = strsave(currentfilename);
288*1458Sroot 		nwordv[1] = strsave(c_header[0]);
289*1458Sroot 		nwordv[2] = strsave(c_header[1]);
290*1458Sroot 		nwordv[3] = strsave(c_header[2]);
291*1458Sroot 		wordv = nwordv - 1;
292*1458Sroot 		wordc += 1 + 3;
293*1458Sroot 		return(C_THISFILE);
294*1458Sroot 	}
295*1458Sroot 	if (strcmp(wordv[1], "...") == 0){
296*1458Sroot 		/*
297*1458Sroot 		 *	have a continuation error message
298*1458Sroot 		 *	of the form: ... message
299*1458Sroot 		 *	Turn into form : filename linenumber message
300*1458Sroot 		 */
301*1458Sroot 		language = INPI;
302*1458Sroot 		nwordv = wordvsplice(1, wordc, wordv+1);
303*1458Sroot 		nwordv[0] = strsave(currentfilename);
304*1458Sroot 		nwordv[1] = strsave(c_linenumber);
305*1458Sroot 		wordv = nwordv - 1;
306*1458Sroot 		wordc += 1;
307*1458Sroot 		return(C_TRUE);
308*1458Sroot 	}
309*1458Sroot 	if(   (wordc == 6)
310*1458Sroot 	   && (lastchar(wordv[6]) == ':')
311*1458Sroot 	   && (isdateformat(5, wordv + 1))
312*1458Sroot 	){
313*1458Sroot 		/*
314*1458Sroot 		 *	Have message that tells us we have changed files
315*1458Sroot 		 */
316*1458Sroot 		language = INPI;
317*1458Sroot 		currentfilename = strsave(wordv[6]);
318*1458Sroot 		clob_last(currentfilename, '\0');
319*1458Sroot 		return(C_SYNC);
320*1458Sroot 	}
321*1458Sroot 	if(   (wordc == 3)
322*1458Sroot 	   && (strcmp(wordv[1], "In") == 0)
323*1458Sroot 	   && (lastchar(wordv[3]) == ':')
324*1458Sroot 	   && (instringset(wordv[2], Piroutines))
325*1458Sroot 	) {
326*1458Sroot 		language = INPI;
327*1458Sroot 		c_header = wordvsplice(0, wordc, wordv+1);
328*1458Sroot 		return(C_SYNC);
329*1458Sroot 	}
330*1458Sroot 	/*
331*1458Sroot 	 *	now, check for just the line number followed by the text
332*1458Sroot 	 */
333*1458Sroot 	if (alldigits(wordv[1])){
334*1458Sroot 		language = INPI;
335*1458Sroot 		c_linenumber = wordv[1];
336*1458Sroot 		return(C_IGNORE);
337*1458Sroot 	}
338*1458Sroot 	/*
339*1458Sroot 	 *	Attempt to match messages refering to a line number
340*1458Sroot 	 *
341*1458Sroot 	 *	Multiply defined label in case, lines %d and %d
342*1458Sroot 	 *	Goto %s from line %d is into a structured statement
343*1458Sroot 	 *	End matched %s on line %d
344*1458Sroot 	 *	Inserted keyword end matching %s on line %d
345*1458Sroot 	 */
346*1458Sroot 	multiple = structured = 0;
347*1458Sroot 	if (
348*1458Sroot 	       ( (wordc == 6) && (wordvcmp(wordv+1, 2, pi_Endmatched) == 0))
349*1458Sroot 	    || ( (wordc == 8) && (wordvcmp(wordv+1, 4, pi_Inserted) == 0))
350*1458Sroot 	    || ( multiple = ((wordc == 9) && (wordvcmp(wordv+1,6, pi_multiple) == 0) ) )
351*1458Sroot 	    || ( structured = ((wordc == 10) && (wordvcmp(wordv+6,5, pi_structured) == 0 ) ))
352*1458Sroot 	){
353*1458Sroot 		language = INPI;
354*1458Sroot 		nwordv = wordvsplice(2, wordc, wordv+1);
355*1458Sroot 		nwordv[0] = strsave(currentfilename);
356*1458Sroot 		nwordv[1] = structured ? wordv [5] : wordv[wordc];
357*1458Sroot 		wordc += 2;
358*1458Sroot 		wordv = nwordv - 1;
359*1458Sroot 		if (!multiple)
360*1458Sroot 			return(C_TRUE);
361*1458Sroot 		erroradd(wordc, nwordv, C_TRUE, C_UNKNOWN);
362*1458Sroot 		nwordv = wordvsplice(0, wordc, nwordv);
363*1458Sroot 		nwordv[1] = wordv[wordc - 2];
364*1458Sroot 		return(C_TRUE);
365*1458Sroot 	}
366*1458Sroot 	return(C_UNKNOWN);
367*1458Sroot }
368