Listing: 1 (* =========================================== *) 2 (* (c) 1990-1992 Clarion Software Corporation *) 3 (* =========================================== *) 4 5 MODULE CR; 6 7 (* Prints a cross-reference table of Modula-2 program. All user-defined 8 words are listed in alphabetical order together with the count of 9 the number of references and a list of all reference line numbers. 10 11 This program will cross reference the words in comments as well 12 as those in the source code. 13 *) 14 15 IMPORT Table, IO, Str, Lib, FIO; 16 17 (* Modula-2 Keywords and Predefined Identifiers *) 18 19 CONST 20 21 KeyWords = 22 'BY DO IF IN OF OR TO AND DIV END FOR MOD NOT SET VAR CASE ELSE '+ ***** ^ not supported yet 23 'EXIT FROM GOTO LOOP THEN TYPE WITH ARRAY BEGIN CONST ELSIF LABEL '+ ***** ^ not supported yet 24 'UNTIL WHILE EXPORT IMPORT MODULE RECORD REPEAT RETURN FORWARD '+ ***** ^ not supported yet 25 'POINTER PROCEDURE DEFINITION IMPLEMENTATION QUALIFIED CLASS'+ ***** ^ not supported yet 26 'SHORTINT INTEGER LONGINT BOOLEAN CHAR BYTE SHORTCARD CARDINAL '+ ***** ^ not supported yet 27 'WORD LONGCARD LONGWORD REAL LONGREAL ADDRESS SHORTADDR BITSET '+ ***** ^ not supported yet 28 'TRUE FALSE NIL PROC HALT NULLPROC CAP ABS CHR FLOAT HIGH ODD '+ ***** ^ not supported yet 29 'TRUNC VAL DEC EXCL INC INCL SIZE VSIZE ADR ORD NEW DISPOSE MAX MIN '; ***** ^ not supported yet 30 31 32 TYPE 33 SymTabPtr = POINTER TO SymTab; ***** ^ undeclared identifier 34 CLASS SymTab(Table.Element); (* Symbol table entry *) ***** ^ not supported yet 35 36 Count : INTEGER; (* Reference count *) 37 LRef : Table.TABLE; (* Cross-Ref List *) 38 Name : ARRAY [0..30] OF CHAR; (* This word *) 39 40 VIRTUAL PROCEDURE Compare( p : Table.ElementPtr ) : INTEGER; 41 42 END SymTab ; 43 44 CLASS IMPLEMENTATION SymTab; 45 46 VIRTUAL PROCEDURE Compare( p : Table.ElementPtr ) : INTEGER; 47 BEGIN 48 RETURN Str.Compare(Name,SymTabPtr(p)^.Name); 49 END Compare; 50 51 BEGIN 52 END SymTab ; 53 54 TYPE 55 56 LinTabPtr = POINTER TO LinTab; 57 58 CLASS LinTab(Table.Element); (* Line number table entry *) 59 60 RefLn : INTEGER; (* Line number *) 61 62 VIRTUAL PROCEDURE Compare( p : Table.ElementPtr ) : INTEGER; 63 64 END LinTab ; 65 66 CLASS IMPLEMENTATION LinTab; (* Line number table entry *) 67 68 VIRTUAL PROCEDURE Compare( p : Table.ElementPtr ) : INTEGER; 69 BEGIN 70 IF (RefLn < LinTabPtr(p)^.RefLn) THEN 71 RETURN -1; 72 ELSIF (RefLn > LinTabPtr(p)^.RefLn) THEN 73 RETURN 1; 74 ELSE 75 RETURN 0; 76 END; 77 END Compare; 78 79 BEGIN 80 END LinTab ; 81 82 83 VAR 84 ni : CARDINAL; (* Column count for printing routines *) 85 86 PROCEDURE PrintLineNo( p : Table.ElementPtr ); 87 (* Prints the line-number information for 'p', which is of 88 CLASS 'LinTab'. 89 *) 90 91 VAR 92 t, l : CARDINAL; 93 BEGIN 94 (* These first few lines count the number of digits in 'p.val' and 95 then check if there's enough room on the line for it. 96 *) 97 t := LinTabPtr(p)^.RefLn; 98 l := 1; 99 REPEAT 100 INC(l); 101 t := t DIV 10; 102 UNTIL t=0; (* l := number of digits in line number *) 103 IF (ni < l) THEN (* Not enough room *) 104 ni := 40; 105 IO.WrLn; 106 IO.WrCharRep(' ',37); 107 END; 108 IO.WrInt(LinTabPtr(p)^.RefLn,1); 109 IO.WrChar(' '); 110 DEC(ni,l); 111 END PrintLineNo; 112 113 PROCEDURE PrintSymbol( r : Table.ElementPtr ); 114 (* Prints the symbol, definition line and cross-reference lines 115 of the symbol 'r' (of type 'SymtabPtr') *) 116 BEGIN 117 IO.WrStrAdj(SymTabPtr(r)^.Name,-30); 118 IO.WrInt(SymTabPtr(r)^.Count,-4); 119 IO.WrStr(' : '); 120 ni := 40; 121 SymTabPtr(r)^.LRef.Apply(PrintLineNo); 122 (* List line numbers for symbol *) 123 IO.WrLn; 124 END PrintSymbol; 125 126 VAR 127 128 SymbolTable, (* Actual table *) 129 Predef : Table.TABLE; (* Key word table *) 130 LineNr : LinTab; (* Work Area *) 131 i,Item : CARDINAL; 132 Symbol : SymTab; (* Word Area *) 133 FileName : ARRAY [0..100] OF CHAR; 134 File : FIO.File; 135 Buffer : ARRAY [1..(1024 + FIO.BufferOverhead)] OF CHAR; 136 Line : ARRAY [0..1023] OF CHAR; 137 138 139 PROCEDURE FillPre; 140 (* Fill 'PreDef' with keyword from 'KeyWords' *) 141 VAR 142 i, j : CARDINAL; 143 x : SymTab; 144 BEGIN 145 Predef.Init; (* Initialize TABLE *) 146 i := 0; 147 WHILE (KeyWords[i] # 0C) DO 148 j := 0; 149 WHILE (KeyWords[i] # ' ') DO 150 x.Name[j] := KeyWords[i]; 151 INC(j); 152 INC(i); 153 END; 154 x.Name[j] := 0C; 155 IF (j # 0) THEN 156 Predef.Insert(x); 157 END; 158 INC(i); 159 END (* WHILE *); 160 END FillPre; 161 162 BEGIN 163 (* check parameters *) 164 IF Lib.ParamCount() <> 1 THEN 165 IO.WrStr('Try again : cr input-file'); 166 IO.WrLn; 167 HALT; 168 END; 169 170 SymbolTable.Init; (* Initialize symbol table *) 171 Lib.ParamStr(FileName,1); (* Get filename from command line *) 172 File := FIO.OpenRead(FileName); (* Open - only need to read it *) 173 FIO.AssignBuffer(File,Buffer); (* Associate a buffer with file 174 to speed up access *) 175 LineNr.RefLn := 1; (* Initialise line counter *) 176 LOOP 177 FIO.RdStr(File,Line); (* Read line from file *) 178 IF FIO.EOF THEN (* Check for end-of-file *) 179 EXIT; 180 END; 181 Item := 0; 182 LOOP 183 Str.Item(Symbol.Name,Line, 184 Str.CHARSET{ 0C..CHAR(255) } - 185 Str.CHARSET{ 'A'..'Z','a'..'z','_','0'..'9','.' },Item); 186 (* Extracts a single word from 'Line'. Each word 187 is delimited by any characters except a digit, a letter, 188 an underbar or a '.'. 189 *) 190 IF (Symbol.Name[0] = 0C) THEN (* Check that there was a word *) 191 EXIT; 192 END; 193 INC(Item); 194 IF (Symbol.Name[0] > '9') THEN (* Don't enter numbers in table *) 195 IF (SymbolTable.Find(Symbol)) THEN 196 (* Is word already in tabel? *) 197 INC(Symbol.Count); (* If, yes, increment count *) 198 ELSE 199 Symbol.Count := 1; 200 Symbol.LRef.Init; (* Initailize line number table *) 201 END; 202 Symbol.LRef.Insert(LineNr); (* Insert cross-reference *) 203 SymbolTable.Insert(Symbol); (* Insert/Update symbol entry *) 204 END; 205 END (* LOOP through line *); 206 INC(LineNr.RefLn); (* Next line *) 207 END (* LOOP - File *); 208 FillPre; (* Build table of predefined words *) 209 SymbolTable.Excl(Predef); (* Exclude predefined words from table *) 210 SymbolTable.Apply(PrintSymbol); (* Display table *) 211 Predef.Dispose; (* Reclaim storage for predefined words *) 212 SymbolTable.Dispose; (* Reclaim storage other names *) 213 END CR. 10 errors