| 1 |
# xref.awk - cross reference an awk program |
| 2 |
|
| 3 |
# 12/2010: Modified for gawk test suite to use a variable |
| 4 |
# for the sort command and to use `sort -k1' instead of `sort +1' |
| 5 |
|
| 6 |
# 10/2020: Bug fix applied to code. |
| 7 |
|
| 8 |
BEGIN { |
| 9 |
if (sortcmd == "") sortcmd = "sort" # "sort -k1" |
| 10 |
|
| 11 |
# create array of keywords to be ignored by lexer |
| 12 |
asplit("BEGIN:END:atan2:break:close:continue:cos:delete:" \ |
| 13 |
"do:else:exit:exp:for:getline:gsub:if:in:index:int:" \ |
| 14 |
"length:log:match:next:print:printf:rand:return:sin:" \ |
| 15 |
"split:sprintf:sqrt:srand:sub:substr:system:while", |
| 16 |
keywords,":") |
| 17 |
|
| 18 |
# build the symbol-state table |
| 19 |
split("00:00:00:00:00:00:00:00:00:00:" \ |
| 20 |
"20:10:10:12:12:11:07:00:00:00:" \ |
| 21 |
"08:08:08:08:08:33:08:00:00:00:" \ |
| 22 |
"08:44:08:36:08:08:08:00:00:00:" \ |
| 23 |
"08:44:45:42:42:41:08",machine,":") |
| 24 |
|
| 25 |
# parse the input and store an intermediate representation |
| 26 |
# of the cross-reference information |
| 27 |
|
| 28 |
# set up the machine |
| 29 |
state = 1 |
| 30 |
|
| 31 |
# run the machine |
| 32 |
for (;;) { |
| 33 |
|
| 34 |
# get next symbol |
| 35 |
symb = lex() |
| 36 |
nextstate = substr(machine[state symb],1,1) |
| 37 |
act = substr(machine[state symb],2,1) |
| 38 |
|
| 39 |
# perform required action |
| 40 |
if ( act == "0" ) |
| 41 |
; # do nothing |
| 42 |
else if ( act == "1" ) { |
| 43 |
if ( ! inarray(tok,names) ) |
| 44 |
names[++nnames] = tok |
| 45 |
lines[tok,++xnames[tok]] = NR } |
| 46 |
else if ( act == "2" ) { |
| 47 |
if ( tok in local ) { |
| 48 |
tok = tok "(" funcname ")" |
| 49 |
if ( ! inarray(tok,names) ) |
| 50 |
names[++nnames] = tok |
| 51 |
lines[tok,++xnames[tok]] = NR } |
| 52 |
else { |
| 53 |
tok = tok "()" |
| 54 |
if ( ! inarray(tok,names) ) |
| 55 |
names[++nnames] = tok |
| 56 |
lines[tok,++xnames[tok]] = NR } } |
| 57 |
else if ( act == "3" ) { |
| 58 |
funcname = tok |
| 59 |
flines[tok] = NR } |
| 60 |
else if ( act == "4" ) |
| 61 |
braces++ |
| 62 |
else if ( act == "5" ) { |
| 63 |
braces-- |
| 64 |
if ( braces == 0 ) { |
| 65 |
for ( temp in local ) |
| 66 |
delete local[temp] |
| 67 |
funcname = "" |
| 68 |
nextstate = 1 } } |
| 69 |
else if ( act == "6" ) { |
| 70 |
local[tok] = 1 } |
| 71 |
else if ( act == "7" ) |
| 72 |
break |
| 73 |
else if ( act == "8" ) { |
| 74 |
print "error: xref.awk: line " NR ": aborting" \ |
| 75 |
> "/dev/con" |
| 76 |
exit 1 } |
| 77 |
|
| 78 |
# finished with current token |
| 79 |
state = nextstate } |
| 80 |
|
| 81 |
# finished parsing, now ready to print output |
| 82 |
for ( i = 1; i <= nnames; i++ ) { |
| 83 |
printf "%d ", xnames[names[i]] | sortcmd |
| 84 |
if ( index(names[i],"(") == 0 ) |
| 85 |
printf "%s(%d)", names[i], flines[names[i]] | sortcmd |
| 86 |
else |
| 87 |
printf "%s", names[i] | sortcmd |
| 88 |
for ( j = 1; j <= xnames[names[i]]; j++ ) |
| 89 |
if ( lines[names[i],j] != lines[names[i],j-1] ) |
| 90 |
printf " %d", lines[names[i],j] | sortcmd |
| 91 |
printf "\n" | sortcmd } |
| 92 |
|
| 93 |
close(sortcmd) |
| 94 |
} # END OF PROGRAM |
| 95 |
|
| 96 |
function asplit(str,arr,fs, n) { n = split(str,temp_asplit,fs) |
| 97 |
for ( i = 1; i <= n; i++ ) arr[temp_asplit[i]]++ } |
| 98 |
|
| 99 |
function inarray(val,arr, j, tmp) { |
| 100 |
for ( j in arr ) |
| 101 |
tmp[arr[j]]++ |
| 102 |
return (val in tmp) } |
| 103 |
|
| 104 |
function lex() { |
| 105 |
|
| 106 |
for (;;) { |
| 107 |
|
| 108 |
if ( tok == "(eof)" ) return 7 |
| 109 |
|
| 110 |
while ( length(line) == 0 ) |
| 111 |
if ( getline line == 0 ) { |
| 112 |
tok = "(eof)"; return 7 } |
| 113 |
|
| 114 |
sub(/^[ \t]+/,"",line) # remove white space, |
| 115 |
sub(/^"([^"]|\\")*"/,"",line) # quoted strings, |
| 116 |
sub(/^\/([^\/]|\\\/)+\//,"",line) # regular expressions, |
| 117 |
sub(/^#.*/,"",line) # and comments |
| 118 |
|
| 119 |
if ( line ~ /^function/ ) { |
| 120 |
tok = "function"; line = substr(line,9); return 1 } |
| 121 |
else if ( line ~ /^{/ ) { |
| 122 |
tok = "{"; line = substr(line,2); return 2 } |
| 123 |
else if ( line ~ /^}/ ) { |
| 124 |
tok = "}"; line = substr(line,2); return 3 } |
| 125 |
# change regexes to use posix character classes |
| 126 |
else if ( match(line,/^[[:alpha:]_][[:alnum:]_]*\[/) ) { |
| 127 |
tok = substr(line,1,RLENGTH-1) |
| 128 |
line = substr(line,RLENGTH+1) |
| 129 |
return 5 } |
| 130 |
else if ( match(line,/^[[:alpha:]_][[:alnum:]_]*\(/) ) { |
| 131 |
tok = substr(line,1,RLENGTH-1) |
| 132 |
line = substr(line,RLENGTH+1) |
| 133 |
if ( ! ( tok in keywords ) ) return 6 } |
| 134 |
else if ( match(line,/^[[:alpha:]_][[:alnum:]_]*/) ) { |
| 135 |
tok = substr(line,1,RLENGTH) |
| 136 |
line = substr(line,RLENGTH+1) |
| 137 |
if ( ! ( tok in keywords ) ) return 4 } |
| 138 |
else { |
| 139 |
match(line,/^[^[:alpha:]_{}]/) |
| 140 |
tok = substr(line,1,RLENGTH) |
| 141 |
line = substr(line,RLENGTH+1) } } } |