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