121637Sdist /* 221637Sdist * Copyright (c) 1980 Regents of the University of California. 3*34664Sbostic * All rights reserved. 4*34664Sbostic * 5*34664Sbostic * Redistribution and use in source and binary forms are permitted 6*34664Sbostic * provided that this notice is preserved and that due credit is given 7*34664Sbostic * to the University of California at Berkeley. The name of the University 8*34664Sbostic * may not be used to endorse or promote products derived from this 9*34664Sbostic * software without specific prior written permission. This software 10*34664Sbostic * is provided ``as is'' without express or implied warranty. 1121637Sdist */ 1221637Sdist 1321637Sdist #ifndef lint 14*34664Sbostic static char sccsid[] = "@(#)pi.c 5.2 (Berkeley) 06/06/88"; 15*34664Sbostic #endif /* not lint */ 1621637Sdist 171458Sroot #include <stdio.h> 181458Sroot #include <ctype.h> 191458Sroot #include "error.h" 201458Sroot 211458Sroot extern char *currentfilename; 221458Sroot static char *c_linenumber; 231458Sroot static char *unk_hdr[] = {"In", "program", "???"}; 241458Sroot static char **c_header = &unk_hdr[0]; 251458Sroot 261458Sroot /* 271458Sroot * Attempt to handle error messages produced by pi (and by pc) 281458Sroot * 291458Sroot * problem #1: There is no file name available when a file does not 301458Sroot * use a #include; this will have to be given to error 311458Sroot * in the command line. 321458Sroot * problem #2: pi doesn't always tell you what line number 331458Sroot * a error refers to; for example during the tree 341458Sroot * walk phase of code generation and error detection, 351458Sroot * an error can refer to "variable foo in procedure bletch" 361458Sroot * without giving a line number 371458Sroot * problem #3: line numbers, when available, are attached to 381458Sroot * the source line, along with the source line itself 391458Sroot * These line numbers must be extracted, and 401458Sroot * the source line thrown away. 411458Sroot * problem #4: Some error messages produce more than one line number 421458Sroot * on the same message. 431458Sroot * There are only two (I think): 441458Sroot * %s undefined on line%s 451458Sroot * %s improperly used on line%s 461458Sroot * here, the %s makes line plural or singular. 471458Sroot * 481458Sroot * Here are the error strings used in pi version 1.2 that can refer 491458Sroot * to a file name or line number: 501458Sroot * 511458Sroot * Multiply defined label in case, lines %d and %d 521458Sroot * Goto %s from line %d is into a structured statement 531458Sroot * End matched %s on line %d 541458Sroot * Inserted keyword end matching %s on line %d 551458Sroot * 561458Sroot * Here are the general pi patterns recognized: 571458Sroot * define piptr == -.*^-.* 581458Sroot * define msg = .* 591458Sroot * define digit = [0-9] 601458Sroot * definename = .* 611458Sroot * define date_format letter*3 letter*3 (digit | (digit digit)) 621458Sroot * (digit | (digit digit)):digit*2 digit*4 631458Sroot * 641458Sroot * {e,E} (piptr) (msg) Encounter an error during textual scan 651458Sroot * E {digit}* - (msg) Have an error message that refers to a new line 661458Sroot * E - msg Have an error message that refers to current 671458Sroot * function, program or procedure 681458Sroot * (date_format) (name): When switch compilation files 691458Sroot * ... (msg) When refer to the previous line 701458Sroot * 'In' ('procedure'|'function'|'program') (name): 711458Sroot * pi is now complaining about 2nd pass errors. 721458Sroot * 731458Sroot * Here is the output from a compilation 741458Sroot * 751458Sroot * 761458Sroot * 2 var i:integer; 771458Sroot * e --------------^--- Inserted ';' 781458Sroot * E 2 - All variables must be declared in one var part 791458Sroot * E 5 - Include filename must end in .i 801458Sroot * Mon Apr 21 15:56 1980 test.h: 811458Sroot * 2 begin 821458Sroot * e ------^--- Inserted ';' 831458Sroot * Mon Apr 21 16:06 1980 test.p: 841458Sroot * E 2 - Function type must be specified 851458Sroot * 6 procedure foo(var x:real); 861458Sroot * e ------^--- Inserted ';' 871458Sroot * In function bletch: 881458Sroot * E - No assignment to the function variable 891458Sroot * w - variable x is never used 901458Sroot * E 6 - foo is already defined in this block 911458Sroot * In procedure foo: 921458Sroot * w - variable x is neither used nor set 931458Sroot * 9 z : = 23; 941458Sroot * E --------------^--- Undefined variable 951458Sroot * 10 y = [1]; 961458Sroot * e ----------------^--- Inserted ':' 971458Sroot * 13 z := 345.; 981458Sroot * e -----------------------^--- Digits required after decimal point 991458Sroot * E 10 - Constant set involved in non set context 1001458Sroot * E 11 - Type clash: real is incompatible with integer 1011458Sroot * ... Type of expression clashed with type of variable in assignment 1021458Sroot * E 12 - Parameter type not identical to type of var parameter x of foo 1031458Sroot * In program mung: 1041458Sroot * w - variable y is never used 1051458Sroot * w - type foo is never used 1061458Sroot * w - function bletch is never used 1071458Sroot * E - z undefined on lines 9 13 1081458Sroot */ 1091458Sroot char *Months[] = { 1101458Sroot "Jan", "Feb", "Mar", "Apr", "May", "Jun", 1111458Sroot "Jul", "Aug", "Sep", "Oct","Nov", "Dec", 1121458Sroot 0 1131458Sroot }; 1141458Sroot char *Days[] = { 1151458Sroot "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", 0 1161458Sroot }; 1171458Sroot char *Piroutines[] = { 1181458Sroot "program", "function", "procedure", 0 1191458Sroot }; 1201458Sroot 1211458Sroot 1221458Sroot static boolean structured, multiple; 1231458Sroot 1241458Sroot char *pi_Endmatched[] = {"End", "matched"}; 1251458Sroot char *pi_Inserted[] = {"Inserted", "keyword", "end", "matching"}; 1261458Sroot 1271458Sroot char *pi_multiple[] = {"Mutiply", "defined", "label", "in", "case,", "line"}; 1281458Sroot char *pi_structured[] = {"is", "into", "a", "structured", "statement"}; 1291458Sroot 1301458Sroot char *pi_und1[] = {"undefined", "on", "line"}; 1311458Sroot char *pi_und2[] = {"undefined", "on", "lines"}; 1321458Sroot char *pi_imp1[] = {"improperly", "used", "on", "line"}; 1331458Sroot char *pi_imp2[] = {"improperly", "used", "on", "lines"}; 1341458Sroot 1351458Sroot boolean alldigits(string) 1365593Srrh reg char *string; 1371458Sroot { 1381458Sroot for (; *string && isdigit(*string); string++) 1391458Sroot continue; 1401458Sroot return(*string == '\0'); 1411458Sroot } 1421458Sroot boolean instringset(member, set) 1435593Srrh char *member; 1445593Srrh reg char **set; 1451458Sroot { 1461458Sroot for(; *set; set++){ 1471458Sroot if (strcmp(*set, member) == 0) 1481458Sroot return(TRUE); 1491458Sroot } 1501458Sroot return(FALSE); 1511458Sroot } 1521458Sroot 1531458Sroot boolean isdateformat(wordc, wordv) 1541458Sroot int wordc; 1551458Sroot char **wordv; 1561458Sroot { 1571458Sroot return( 1581458Sroot (wordc == 5) 1591458Sroot && (instringset(wordv[0], Days)) 1601458Sroot && (instringset(wordv[1], Months)) 1611458Sroot && (alldigits(wordv[2])) 1621458Sroot && (alldigits(wordv[4])) ); 1631458Sroot } 1641458Sroot 1651458Sroot boolean piptr(string) 1665593Srrh reg char *string; 1671458Sroot { 1681458Sroot if (*string != '-') 1691458Sroot return(FALSE); 1701458Sroot while (*string && *string == '-') 1711458Sroot string++; 1721458Sroot if (*string != '^') 1731458Sroot return(FALSE); 1741458Sroot string++; 1751458Sroot while (*string && *string == '-') 1761458Sroot string++; 1771458Sroot return(*string == '\0'); 1781458Sroot } 1791458Sroot 1801458Sroot extern int wordc; 1811458Sroot extern char **wordv; 1821458Sroot 1831458Sroot Errorclass pi() 1841458Sroot { 1851458Sroot char **nwordv; 1861458Sroot 18713611Ssam if (wordc < 2) 18813611Ssam return (C_UNKNOWN); 1891458Sroot if ( ( strlen(wordv[1]) == 1) 1901458Sroot && ( (wordv[1][0] == 'e') || (wordv[1][0] == 'E') ) 1911458Sroot && ( piptr(wordv[2]) ) 1921458Sroot ) { 1931458Sroot boolean longpiptr = 0; 1941458Sroot /* 1951458Sroot * We have recognized a first pass error of the form: 1961458Sroot * letter ------^---- message 1971458Sroot * 1981458Sroot * turn into an error message of the form: 1991458Sroot * 2001458Sroot * file line 'pascal errortype' letter \n |---- message 2011458Sroot * or of the form: 2021458Sroot * file line letter |---- message 2031458Sroot * when there are strlen("(*[pi]") or more 2041458Sroot * preceding '-' on the error pointer. 2051458Sroot * 2061458Sroot * Where the | is intended to be a down arrow, so that 2071458Sroot * the pi error messages can be inserted above the 2081458Sroot * line in error, instead of below. (All of the other 2091458Sroot * langauges put thier messages before the source line, 2101458Sroot * instead of after it as does pi.) 2111458Sroot * 2121458Sroot * where the pointer to the error has been truncated 2131458Sroot * by 6 characters to account for the fact that 2141458Sroot * the pointer points into a tab preceded input line. 2151458Sroot */ 2161458Sroot language = INPI; 2175593Srrh (void)substitute(wordv[2], '^', '|'); 2181458Sroot longpiptr = position(wordv[2],'|') > (6+8); 2191458Sroot nwordv = wordvsplice(longpiptr ? 2 : 4, wordc, wordv+1); 2201458Sroot nwordv[0] = strsave(currentfilename); 2211458Sroot nwordv[1] = strsave(c_linenumber); 2221458Sroot if (!longpiptr){ 2231458Sroot nwordv[2] = "pascal errortype"; 2241458Sroot nwordv[3] = wordv[1]; 2251458Sroot nwordv[4] = strsave("%%%\n"); 2261458Sroot if (strlen(nwordv[5]) > (8-2)) /* this is the pointer */ 2271458Sroot nwordv[5] += (8-2); /* bump over 6 characters */ 2281458Sroot } 2291458Sroot wordv = nwordv - 1; /* convert to 1 based */ 2301458Sroot wordc += longpiptr ? 2 : 4; 2311458Sroot return(C_TRUE); 2321458Sroot } 2331458Sroot if ( (wordc >= 4) 2341458Sroot && (strlen(wordv[1]) == 1) 2351458Sroot && ( (*wordv[1] == 'E') || (*wordv[1] == 'w') || (*wordv[1] == 'e') ) 2361458Sroot && (alldigits(wordv[2])) 2371458Sroot && (strlen(wordv[3]) == 1) 2381458Sroot && (wordv[3][0] == '-') 2391458Sroot ){ 2401458Sroot /* 2411458Sroot * Message of the form: letter linenumber - message 2421458Sroot * Turn into form: filename linenumber letter - message 2431458Sroot */ 2441458Sroot language = INPI; 2451458Sroot nwordv = wordvsplice(1, wordc, wordv + 1); 2461458Sroot nwordv[0] = strsave(currentfilename); 2471458Sroot nwordv[1] = wordv[2]; 2481458Sroot nwordv[2] = wordv[1]; 2491458Sroot c_linenumber = wordv[2]; 2501458Sroot wordc += 1; 2511458Sroot wordv = nwordv - 1; 2521458Sroot return(C_TRUE); 2531458Sroot } 2541458Sroot if ( (wordc >= 3) 2551458Sroot && (strlen(wordv[1]) == 1) 2561458Sroot && ( (*(wordv[1]) == 'E') || (*(wordv[1]) == 'w') || (*(wordv[1]) == 'e') ) 2571458Sroot && (strlen(wordv[2]) == 1) 2581458Sroot && (wordv[2][0] == '-') 2591458Sroot ) { 2601458Sroot /* 2611458Sroot * Message of the form: letter - message 2621458Sroot * This happens only when we are traversing the tree 2631458Sroot * during the second pass of pi, and discover semantic 2641458Sroot * errors. 2651458Sroot * 2661458Sroot * We have already (presumably) saved the header message 2671458Sroot * and can now construct a nulled error message for the 2681458Sroot * current file. 2691458Sroot * 2701458Sroot * Turns into a message of the form: 2711458Sroot * filename (header) letter - message 2721458Sroot * 2731458Sroot * First, see if it is a message referring to more than 2741458Sroot * one line number. Only of the form: 2751458Sroot * %s undefined on line%s 2761458Sroot * %s improperly used on line%s 2771458Sroot */ 2781458Sroot boolean undefined = 0; 2791458Sroot int wordindex; 2801458Sroot 2811458Sroot language = INPI; 2821458Sroot if ( (undefined = (wordvcmp(wordv+2, 3, pi_und1) == 0) ) 2831458Sroot || (undefined = (wordvcmp(wordv+2, 3, pi_und2) == 0) ) 2841458Sroot || (wordvcmp(wordv+2, 4, pi_imp1) == 0) 2851458Sroot || (wordvcmp(wordv+2, 4, pi_imp2) == 0) 2861458Sroot ){ 2871458Sroot for (wordindex = undefined ? 5 : 6; wordindex <= wordc; 2881458Sroot wordindex++){ 2891458Sroot nwordv = wordvsplice(2, undefined ? 2 : 3, wordv+1); 2901458Sroot nwordv[0] = strsave(currentfilename); 2911458Sroot nwordv[1] = wordv[wordindex]; 2921458Sroot if (wordindex != wordc) 2931458Sroot erroradd(undefined ? 4 : 5, nwordv, 2941458Sroot C_TRUE, C_UNKNOWN); 2951458Sroot } 2961458Sroot wordc = undefined ? 4 : 5; 2971458Sroot wordv = nwordv - 1; 2981458Sroot return(C_TRUE); 2991458Sroot } 3001458Sroot 3011458Sroot nwordv = wordvsplice(1+3, wordc, wordv+1); 3021458Sroot nwordv[0] = strsave(currentfilename); 3031458Sroot nwordv[1] = strsave(c_header[0]); 3041458Sroot nwordv[2] = strsave(c_header[1]); 3051458Sroot nwordv[3] = strsave(c_header[2]); 3061458Sroot wordv = nwordv - 1; 3071458Sroot wordc += 1 + 3; 3081458Sroot return(C_THISFILE); 3091458Sroot } 3101458Sroot if (strcmp(wordv[1], "...") == 0){ 3111458Sroot /* 3121458Sroot * have a continuation error message 3131458Sroot * of the form: ... message 3141458Sroot * Turn into form : filename linenumber message 3151458Sroot */ 3161458Sroot language = INPI; 3171458Sroot nwordv = wordvsplice(1, wordc, wordv+1); 3181458Sroot nwordv[0] = strsave(currentfilename); 3191458Sroot nwordv[1] = strsave(c_linenumber); 3201458Sroot wordv = nwordv - 1; 3211458Sroot wordc += 1; 3221458Sroot return(C_TRUE); 3231458Sroot } 3241458Sroot if( (wordc == 6) 3251458Sroot && (lastchar(wordv[6]) == ':') 3261458Sroot && (isdateformat(5, wordv + 1)) 3271458Sroot ){ 3281458Sroot /* 3291458Sroot * Have message that tells us we have changed files 3301458Sroot */ 3311458Sroot language = INPI; 3321458Sroot currentfilename = strsave(wordv[6]); 3331458Sroot clob_last(currentfilename, '\0'); 3341458Sroot return(C_SYNC); 3351458Sroot } 3361458Sroot if( (wordc == 3) 3371458Sroot && (strcmp(wordv[1], "In") == 0) 3381458Sroot && (lastchar(wordv[3]) == ':') 3391458Sroot && (instringset(wordv[2], Piroutines)) 3401458Sroot ) { 3411458Sroot language = INPI; 3421458Sroot c_header = wordvsplice(0, wordc, wordv+1); 3431458Sroot return(C_SYNC); 3441458Sroot } 3451458Sroot /* 3461458Sroot * now, check for just the line number followed by the text 3471458Sroot */ 3481458Sroot if (alldigits(wordv[1])){ 3491458Sroot language = INPI; 3501458Sroot c_linenumber = wordv[1]; 3511458Sroot return(C_IGNORE); 3521458Sroot } 3531458Sroot /* 3541458Sroot * Attempt to match messages refering to a line number 3551458Sroot * 3561458Sroot * Multiply defined label in case, lines %d and %d 3571458Sroot * Goto %s from line %d is into a structured statement 3581458Sroot * End matched %s on line %d 3591458Sroot * Inserted keyword end matching %s on line %d 3601458Sroot */ 3611458Sroot multiple = structured = 0; 3621458Sroot if ( 3631458Sroot ( (wordc == 6) && (wordvcmp(wordv+1, 2, pi_Endmatched) == 0)) 3641458Sroot || ( (wordc == 8) && (wordvcmp(wordv+1, 4, pi_Inserted) == 0)) 3651458Sroot || ( multiple = ((wordc == 9) && (wordvcmp(wordv+1,6, pi_multiple) == 0) ) ) 3661458Sroot || ( structured = ((wordc == 10) && (wordvcmp(wordv+6,5, pi_structured) == 0 ) )) 3671458Sroot ){ 3681458Sroot language = INPI; 3691458Sroot nwordv = wordvsplice(2, wordc, wordv+1); 3701458Sroot nwordv[0] = strsave(currentfilename); 3711458Sroot nwordv[1] = structured ? wordv [5] : wordv[wordc]; 3721458Sroot wordc += 2; 3731458Sroot wordv = nwordv - 1; 3741458Sroot if (!multiple) 3751458Sroot return(C_TRUE); 3761458Sroot erroradd(wordc, nwordv, C_TRUE, C_UNKNOWN); 3771458Sroot nwordv = wordvsplice(0, wordc, nwordv); 3781458Sroot nwordv[1] = wordv[wordc - 2]; 3791458Sroot return(C_TRUE); 3801458Sroot } 3811458Sroot return(C_UNKNOWN); 3821458Sroot } 383