Class: FileAnalyzer

Inherits:
Object
  • Object
show all
Includes:
RubyToken
Defined in:
lib/kwala/lib/code_analyzer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ FileAnalyzer

Returns a new instance of FileAnalyzer.



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
# File 'lib/kwala/lib/code_analyzer.rb', line 59

def initialize(file)
  @file = file
  @requires = []
  @classes = []
  @modules = []
  @constants = []
  @methods = []
  @g_vars = []
  @i_vars = []
  @c_vars = []
  @symbols = []
  @blocks = 0
  @yields = 0
  @raises = 0
  @rescues = 0
  @cases = 0
  @aliases = []
  @undefs = []
  @ensures = 0
  @evals = 0
  @big_begins = 0
  @big_ends = 0
  @exits = 0
  @defines = 0
  @retries = 0
  @redoes = 0
  @nexts = 0
  @breaks = 0
  @ensures = 0
  @assignments = 0
  @is_a_kind_ofs = 0
  @method_missing = 0
  @static_methods = []
  @tl_ref = Hash.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missingObject (readonly)

Returns the value of attribute method_missing.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def method_missing
  @method_missing
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def aliases
  @aliases
end

#assignmentsObject (readonly)

Returns the value of attribute assignments.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def assignments
  @assignments
end

#big_beginsObject (readonly)

Returns the value of attribute big_begins.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def big_begins
  @big_begins
end

#big_endsObject (readonly)

Returns the value of attribute big_ends.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def big_ends
  @big_ends
end

#blocksObject (readonly)

Returns the value of attribute blocks.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def blocks
  @blocks
end

#breaksObject (readonly)

Returns the value of attribute breaks.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def breaks
  @breaks
end

#c_varsObject (readonly)

Returns the value of attribute c_vars.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def c_vars
  @c_vars
end

#casesObject (readonly)

Returns the value of attribute cases.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def cases
  @cases
end

#classesObject (readonly)

Returns the value of attribute classes.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def classes
  @classes
end

#constantsObject (readonly)

Returns the value of attribute constants.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def constants
  @constants
end

#definesObject (readonly)

Returns the value of attribute defines.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def defines
  @defines
end

#ensuresObject (readonly)

Returns the value of attribute ensures.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def ensures
  @ensures
end

#evalsObject (readonly)

Returns the value of attribute evals.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def evals
  @evals
end

#exitsObject (readonly)

Returns the value of attribute exits.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def exits
  @exits
end

#fileObject (readonly)

Returns the value of attribute file.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def file
  @file
end

#g_varsObject (readonly)

Returns the value of attribute g_vars.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def g_vars
  @g_vars
end

#i_varsObject (readonly)

Returns the value of attribute i_vars.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def i_vars
  @i_vars
end

#is_a_kind_ofsObject (readonly)

Returns the value of attribute is_a_kind_ofs.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def is_a_kind_ofs
  @is_a_kind_ofs
end

#method_missingObject (readonly)

Returns the value of attribute method_missing.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def method_missing
  @method_missing
end

#methodsObject (readonly)

Returns the value of attribute methods.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def methods
  @methods
end

#modulesObject (readonly)

Returns the value of attribute modules.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def modules
  @modules
end

#nextsObject (readonly)

Returns the value of attribute nexts.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def nexts
  @nexts
end

#raisesObject (readonly)

Returns the value of attribute raises.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def raises
  @raises
end

#redoesObject (readonly)

Returns the value of attribute redoes.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def redoes
  @redoes
end

#requiresObject (readonly)

Returns the value of attribute requires.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def requires
  @requires
end

#rescuesObject (readonly)

Returns the value of attribute rescues.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def rescues
  @rescues
end

#retriesObject (readonly)

Returns the value of attribute retries.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def retries
  @retries
end

#static_methodsObject (readonly)

Returns the value of attribute static_methods.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def static_methods
  @static_methods
end

#symbolsObject (readonly)

Returns the value of attribute symbols.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def symbols
  @symbols
end

#undefsObject (readonly)

Returns the value of attribute undefs.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def undefs
  @undefs
end

#yieldsObject (readonly)

Returns the value of attribute yields.



52
53
54
# File 'lib/kwala/lib/code_analyzer.rb', line 52

def yields
  @yields
end

Instance Method Details

#analyzeObject



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
# File 'lib/kwala/lib/code_analyzer.rb', line 95

def analyze
  #puts "Lexing File: #{ @file }"

  lexer = RubyLex.new
  lexer.exception_on_syntax_error = false
  lexer.set_input(File.new(@file,"r"))

  tokens = Array.new
  while (tok = lexer.token)
    tok.indent = lexer.indent
    tokens<< tok
    @tl_ref[tok.object_id] = lexer.line_no
  end

  i = 0
  while i < tokens.size
    ni = check_token(tokens[i], tokens[i..-1])
    i += ni
  end


#    types, rem = BaseType.create_types_from_tokens(tokens)


#    puts "TYPES ----------------"
#    pp types
#    exit 1

  self
rescue RubyLex::SyntaxError => synerr
  $stderr.puts synerr
ensure
  self
end

#check_token(tok, stack) ⇒ Object



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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/kwala/lib/code_analyzer.rb', line 178

def check_token(tok, stack)
  i = 1
  case tok
  when TkCLASS
    name, i = find_identifier_name(stack)
    @classes<< name
  when TkMODULE
    name, i = find_identifier_name(stack)
    @modules<< name
  when TkCONSTANT
    #@contants<< tok.name
    i = find_static_method_call(tok, stack)
  when TkDEF
    name, i = find_identifier_name(stack)
    @methods<< name
  when TkIDENTIFIER, TkFID
    #@
    case tok.name
    when "require"
      @requires<< nil
    when "raise"
      @raises += 1
    when "eval", "instance_eval", "class_eval", "module_eval"
      @evals += 1
    when "is_a?", "kind_of?"
      @is_a_kind_ofs += 1
    when "method_missing"
      @method_missing += 1
    when /^exit!?$/
      @exits += 1
    end

  when TkSYMBEG
    name, i = find_identifier_name(stack)
    @symbols<< name
  when TkCVAR
    @c_vars<< tok.name
  when TkIVAR
    @i_vars<< tok.name
  when TkGVAR
    @g_vars << tok.name
  when TkDO,TkfLBRACE
    # do
    @blocks += 1
  when TkYIELD
    @yields += 1
  when TkEND
  when TkRESCUE
    @rescues += 1
  when TklBEGIN
    @big_begins += 1
  when TklEND
    @big_ends += 1
  when TkDEFINED
    @defines += 1
  when TkRETRY
    @retries += 1
  when TkREDO
    @redoes += 1
  when TkNEXT
    @nexts += 1
  when TkBREAK
    @breaks += 1
  when TkENSURE
    @ensures += 1
  when TkUNDEF
    @undefs << nil
  when TkCASE
    @cases += 1
  when TkALIAS
    name, i = find_identifier_name(stack)
    @aliases << name
  when TkASSIGN, TkOPASGN
    @assignments += 1
  else

  end
  i
end

#find_identifier_name(stream, check_types = [TkIDENTIFIER, TkFID, TkCONSTANT]) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/kwala/lib/code_analyzer.rb', line 258

def find_identifier_name(stream, check_types = [TkIDENTIFIER, TkFID, TkCONSTANT])
  i = 1
  name = ""
  found = false

  while !found && stream[i]
    tok = stream[i]
    if check_types.include?(tok.class)
      found = true
      name = tok.name
    else
      i += 1
    end
  end

  [name, i]
end

#find_static_method_call(tok, stream) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/kwala/lib/code_analyzer.rb', line 276

def find_static_method_call(tok, stream)
  sname = tok.name.dup
  searching = true
  i = 1
  while searching
    val = stream[i]
    case val
    when TkDOT
      sname<< "."
    when TkCOLON2
      sname<< "::"
    when TkCONSTANT, TkIDENTIFIER, TkFID
      sname << val.name
    when TkNL, TkSPACE
      searching = false
    when TkLPAREN
      sname<< "()"
      searching = false
    when TkfLBRACK
      sname<< "[]"
      searching = false
    when TkfLBRACE
      searching = false
    when TkRPAREN, TkRBRACK, TkRBRACE, TkDOT2, TkDOT3, TkBITOR, TkBITAND,
      TkCOMMA, TkSEMICOLON, TkPLUS, TkMINUS, TkMULT, TkDIV
      # close of a method call
      searching = false
    when TkASSIGN, TkMOD
      # could be class = ? most likely a constant
      searching = false
    when TkDSTRING, TkSTRING
      # ignore the constant as it is wrapped in a string
      searching = false
    else
      puts "NEW CASE #{ val.class } for #{self.file}"
      puts "Line: #{ @tl_ref[val.object_id] }"
      searching = false
    end
    i += 1
  end
  if tok.name != sname
    # Make sure we don't get things like Array[] or Array()
    if /:/ =~ sname && /^[A-Z]/ =~ sname.split(".").last.split(":").last
      @constants << sname
    else
      @static_methods<< sname
    end
  else
    @constants << sname
    #puts "JUST A CONST :#{ sname } ----"
    #puts "Line: #{ @tl_ref[tok.object_id] }"
  end
  i
end

#group_methods(tokens) ⇒ Object



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/kwala/lib/code_analyzer.rb', line 331

def group_methods(tokens)
#    meth_groups = []
#
#    cur_group_toks = []
#    indent = 0
#    indef = false
#
#    tokens.each do |tok|
#      cur_group_toks<< tok
#
#      if tok.class == TkDEF
#        indent = tok.indent
#        indef = true
#        cur_group_toks = [ tok ]
#      elsif tok.class == TkEND
#        if indef && (tok.indent == (indent - 1))
#          indef = false
#          indent = tok.indent
#
#          meth_groups<< cur_group_toks
#          cur_group_toks = []
#        end
#      end
#    end
#
#    puts "Meth group #{ meth_groups.size }"
#    pp meth_groups
#    exit
#
  groups, idx = group_tokens(tokens, [TkDEF])
  puts "Meth groups cnt #{ groups.size }"
  pp groups
  exit
end

#summarizeObject



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
# File 'lib/kwala/lib/code_analyzer.rb', line 130

def summarize
  puts "-- Summary for #{ @file } --",""
  puts "Requires".ljust(15) + ": " + @requires.size.to_s
  puts "Modules".ljust(15) + ": " + @modules.size.to_s
  puts "Classes".ljust(15) + ": " + @classes.size.to_s
  puts "Constants".ljust(15) + ": " + @constants.size.to_s
  puts "Methods".ljust(15) + ": " + @methods.size.to_s
  puts "Static Methods".ljust(15) + ": " + @static_methods.size.to_s
  puts "G Vars".ljust(15) + ": " + @g_vars.size.to_s
  puts "C Vars".ljust(15) + ": " + @c_vars.size.to_s
  puts "I Vars".ljust(15) + ": " + @i_vars.size.to_s
  puts "Symbols".ljust(15) + ": " + @symbols.size.to_s
  puts "Blocks".ljust(15) + ": " + @blocks.to_s
  puts "Yields".ljust(15) + ": " + @yields.to_s
  puts "Raises".ljust(15) + ": " + @raises.to_s
  puts "Rescues".ljust(15) + ": " + @rescues.to_s
  puts "Retries".ljust(15) + ": " + @retries.to_s
  puts "Redoes".ljust(15) + ": " + @redoes.to_s
  puts "Ensures".ljust(15) + ": " + @ensures.to_s
  puts "Undefs".ljust(15) + ": " + @undefs.size.to_s
  puts "Evals".ljust(15) + ": " + @evals.to_s
  puts "Aliases".ljust(15) + ": " + @aliases.size.to_s
  puts "BEGINs".ljust(15) + ": " + @big_begins.to_s
  puts "ENDs".ljust(15) + ": " + @big_ends.to_s
  puts "Exits".ljust(15) + ": " + @exits.to_s
  puts "Defined?".ljust(15) + ": " + @defines.to_s
  puts "Is_a? ".ljust(15) + ": " + @is_a_kind_ofs.to_s
  puts "M missing".ljust(15) + ": " + @method_missing.to_s
  puts "Nexts".ljust(15) + ": " + @nexts.to_s
  puts "Breaks".ljust(15) + ": " + @breaks.to_s
  puts "Cases".ljust(15) + ": " + @cases.to_s
  puts "Assignments".ljust(15) + ": " + @assignments.to_s
  puts "","",""

  if @constants.size > 0
    puts "","",""
    puts "Constants ->", @constants.sort.uniq
  end

  if @static_methods.size > 0
    puts "","",""
    puts "Static methods ->", @static_methods.sort.uniq
  end


  puts "","----------------------------"
end