1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
############################################################################
#
# Name: ipxref.icn
#
# Title: Produce cross reference for Icon program
#
# Author: Allan J. Anderson
#
# Date: June 10, 1988
#
############################################################################
#
# This program cross-references Icon programs. It lists the
# occurrences of each variable by line number. Variables are listed
# by procedure or separately as globals. The options specify the
# formatting of the output and whether or not to cross-reference
# quoted strings and non-alphanumerics. Variables that are followed
# by a left parenthesis are listed with an asterisk following the
# name. If a file is not specified, then standard input is cross-
# referenced.
#
# Options: The following options change the format defaults:
#
# -c n The column width per line number. The default is 4
# columns wide.
#
# -l n The starting column (i.e. left margin) of the line
# numbers. The default is column 40.
#
# -w n The column width of the whole output line. The default
# is 80 columns wide.
#
# Normally only alphanumerics are cross-referenced. These
# options expand what is considered:
#
# -q Include quoted strings.
#
# -x Include all non-alphanumerics.
#
# Note: This program assumes the subject file is a valid Icon pro-
# gram. For example, quotes are expected to be matched.
#
############################################################################
#
# Bugs:
#
# In some situations, the output is not properly formatted.
#
############################################################################
#
# Links: options, post
#
############################################################################
link options, post
global resword, linenum, letters, alphas, var, buffer, qflag, infile, xflag
global inmaxcol, inlmarg, inchunk, localvar, lin
record procrec(pname,begline,lastline)
procedure main(args)
local word, w2, p, prec, i, L, ln, switches, nfile
Init__("ipxref")
resword := ["break","by","case","default","do","dynamic","else","end",
"every","fail","global","if","initial","link", "local","next","not",
"of","procedure", "record","repeat","return","static","suspend","then",
"to","until","while"]
linenum := 0
var := table() # var[variable[proc]] is list of line numbers
prec := [] # list of procedure records
localvar := [] # list of local variables of current routine
buffer := [] # a put-back buffer for getword
proc := "global"
letters := &letters ++ '_'
alphas := letters ++ &digits
switches := options(args,"qxw+l+c+")
if \switches["q"] then qflag := 1
if \switches["x"] then xflag := 1
inmaxcol := \switches["w"]
inlmarg := \switches["l"]
inchunk := \switches["c"]
infile := open(args[1],"r") # could use some checking
while word := getword() do
if word == "link" then {
buffer := []
lin := ""
next
}
else if word == "procedure" then {
put(prec,procrec("",linenum,0))
proc := getword() | break
p := pull(prec)
p.pname := proc
put(prec,p)
}
else if word == ("global" | "link" | "record") then {
word := getword() | break
addword(word,"global",linenum)
while (w2 := getword()) == "," do {
if word == !resword then break
word := getword() | break
addword(word,"global",linenum)
}
put(buffer,w2)
}
else if word == ("local" | "dynamic" | "static") then {
word := getword() | break
put(localvar,word)
addword(word,proc,linenum)
while (w2 := getword()) == "," do {
if word == !resword then break
word := getword() | break
put(localvar,word)
addword(word,proc,linenum)
}
put(buffer,w2)
}
else if word == "end" then {
proc := "global"
localvar := []
p := pull(prec)
p.lastline := linenum
put(prec,p)
}
else if word == !resword then
next
else {
ln := linenum
if (w2 := getword()) == "(" then
word ||:= " *" # special mark for procedures
else
put(buffer,w2) # put back w2
addword(word,proc,ln)
}
every write(!format(var))
write("\n\nprocedures:\tlines:\n")
L := []
every p := !prec do
put(L,left(p.pname,16," ") || p.begline || "-" || p.lastline)
every write(!sort(L))
Term__()
end
procedure addword(word,proc,lineno)
if any(letters,word) | \xflag then {
/var[word] := table()
if /var[word]["global"] | (word == !\localvar) then {
/(var[word])[proc] := [word,proc]
put((var[word])[proc],lineno)
}
else {
/var[word]["global"] := [word,"global"]
put((var[word])["global"],lineno)
}
}
end
procedure getword()
local j, c
static i, nonwhite
initial nonwhite := ~' \t\n'
repeat {
if *buffer > 0 then return get(buffer)
if /lin | i = *lin + 1 then
if lin := read(infile) then {
i := 1
linenum +:= 1
}
else fail
if i := upto(nonwhite,lin,i) then { # skip white space
j := i
if lin[i] == ("'" | "\"") then { # don't xref quoted words
if /qflag then {
c := lin[i]
i +:= 1
repeat
if i := upto(c ++ '\\',lin,i) + 1 then
if lin[i - 1] == c then break
else i +:= 1
else {
i := 1
linenum +:= 1
lin := read(infile) | fail
}
}
else i +:= 1
}
else if lin[i] == "#" then { # don't xref comments; get next line
i := *lin + 1
}
else if i := many(alphas,lin,i) then
return lin[j:i]
else {
i +:= 1
return lin[i - 1]
}
}
else
i := *lin + 1
} # repeat
end
procedure format(T)
local V, block, n, L, lin, maxcol, lmargin, chunk, col
initial {
maxcol := \inmaxcol | 80
lmargin := \inlmarg | 40
chunk := \inchunk | 4
}
L := []
col := lmargin
every V := !T do
every block := !V do {
lin := left(block[1],16," ") || left(block[2],lmargin - 16," ")
every lin ||:= center(block[3 to *block],chunk," ") do {
col +:= chunk
if col >= maxcol - chunk then {
lin ||:= "\n\t\t\t\t\t"
col := lmargin
}
}
if col = lmargin then lin := lin[1:-6] # came out exactly even
put(L,lin)
col := lmargin
}
L := sort(L)
push(L,"variable\tprocedure\t\tline numbers\n")
return L
end
|