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