It contains the name of the source file, which will need editing, and will also need some work doing to the list of basic keywords, as there are many and I have only included those used in this program. The listing is the result of running the program on itself. It is fine for relatively small code files, it gets indigestion if applied to very large files, either through lack of memory or by losing its connection to the browser. The dimension of varname$ can be reduced to suit your program if memory is a problem.
Code: [Local Link Removed for Guests]
file /parser.bas exists
1 'program to list all user-defined names in a basic program, with line numbers listed where they are used.
2 'use to identify unused names, misspelled names, to find references etc.
3 'the keyword list is not complete, use the first run to find keywords that need to be added to the list
4 'feel free to improve the code, there is plenty of scope to do so
5 dim varname$(400,2) 'rows of variable names, string of line numbers
6 dim keywordlist$(33) = "-","space$","dim","if","then","wlog","do","until", "loop","sub","select","case","end","while","else","file","read$", "len","mid$","to","str$","exists","lcase$","wend","for","next","exit","or","ramfree","and","endif"
7 keywordcount = 33
8 line$ = ""
9 result = 9
10 varcount = 0
11 fname$ = "/parser.bas"
12 foundvar= 0
13 if file.exists(fname$) then wlog "file "+fname$+" exists"
14 i = 1
15 line$ = file.read$(fname$,i) 'line by line
16 line$ = lcase$(line$)
17 do while line$ <> "_eof_"
18 wlog str$(i)+" "+line$
19 if len(line$)>1 then process_line
20 i = i + 1
21 line$ = file.read$(fname$,i) 'line by line
22 line$ = lcase$(line$)
23 loop
24 i = i - 1
25 wlog "lines read "+str$(i)
26 listvars
27 end
28
29 sub process_line
30 extractedvar$ = ""
31 pos = 1
32 foundvar = 0
33 while pos <= len(line$)
34 thischar$ = mid$(line$,pos,1)
35 select case thischar$
36 case "'": pos = len(line$) 'skip
37 case "a" to "z", "a" to "z"
38 extractedvar$=extractedvar$ + thischar$
39 foundvar = 1
40 case "0" to "9","$","_"
41 if foundvar = 1 then extractedvar$=extractedvar$ + thischar$
42 case """":
43 pos = pos + 1
44 do while mid$(line$,pos,1)<> """"
45 pos = pos + 1
46 loop 'scan to closing quote
47 case else: '=() etc. etc.
48 if foundvar = 1 then capture_name
49 extractedvar$ = ""
50 foundvar = 0
51 end select
52 if foundvar = 1 and pos = len(line$) then capture_name
53 pos = pos+1
54 wend
55 end sub
56
57 sub capture_name
58 keyword_test
59 if result = 1 then savevar
60 end sub
61
62 sub keyword_test
63 result = 1
64 for k = 1 to keywordcount
65 if extractedvar$ = keywordlist$(k) then result = 0
66 next
67 end sub
68
69 sub savevar 'adds the var to the list
70 j=1
71 do until (varname$(j,1) = extractedvar$) or (varname$(j,1)="")
72 j = j + 1
73 loop
74 if (varname$(j,1) = extractedvar$) then
75 varname$(j,2) = varname$(j,2)+" "+str$(i)
76 else
77 varname$(j,1) = extractedvar$
78 varname$(j,2) = varname$(j,2)+" "+str$(i)
79 varcount = varcount+1
80 'wlog "varcount = "+str$(varcount)
81 j=j+1
82 endif
83 end sub
84
85 sub listvars
86 wlog "========================================="
87 wlog " program names in file: "+ fname$
88 wlog "========================================="
89 wlog "no of variable and subprogram names: "+str$(varcount)
90 for i = 1 to varcount
91 tablength = 20-len(varname$(i,1))
92 wlog varname$(i,1)+space$(tablength)+varname$(i,2)
93 next
94 end sub
95
96
lines read 96
=========================================
PROGRAM NAMES IN FILE: /parser.bas
=========================================
no of variable and subprogram names: 20
varname$ 5 71 71 74 75 75 77 78 78 91 92 92
keywordlist$ 6 65
keywordcount 7 64
line$ 8 15 16 16 17 18 19 21 22 22 33 34 36 44 52
result 9 59 63 65
varcount 10 79 79 89 90
fname$ 11 13 13 15 21 87
foundvar 12 32 39 41 48 50 52
i 14 15 18 20 20 21 24 24 25 75 78 90 91 92 92
process_line 19 29
listvars 26 85
extractedvar$ 30 38 38 41 41 49 65 71 74 77
pos 31 33 34 36 43 43 44 45 45 52 53 53
thischar$ 34 35 38 41
capture_name 48 52 57
keyword_test 58 62
savevar 59 69
k 64 65
j 70 71 71 72 72 74 75 75 77 78 78 81 81
tablength 91 92