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