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