Class: Archruby::Ruby::Parser

Inherits:
SexpInterpreter
  • Object
show all
Defined in:
lib/archruby/ruby/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ Parser

Returns a new instance of Parser.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/archruby/ruby/parser.rb', line 11

def initialize(content)
  super()
  #a = Archruby::Presenters::Graph.new
  @content = content
  @dependencies = []
  @classes = []
  @full_class_path = []
  @classes_and_dependencies = {}
  @module_names = []
  @complete_class_name = []
  @var_propagation = Archruby::Ruby::VarPropagation.new
  @type_propagation_parser = Archruby::Ruby::TypeInference::Ruby::ParserForTypeinference.new
  @type_inference = []
  @method_calls = []
  parse
end

Instance Attribute Details

#classesObject (readonly)

Returns the value of attribute classes.



8
9
10
# File 'lib/archruby/ruby/parser.rb', line 8

def classes
  @classes
end

#classes_and_dependenciesObject (readonly)

Returns the value of attribute classes_and_dependencies.



8
9
10
# File 'lib/archruby/ruby/parser.rb', line 8

def classes_and_dependencies
  @classes_and_dependencies
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



8
9
10
# File 'lib/archruby/ruby/parser.rb', line 8

def dependencies
  @dependencies
end

#method_callsObject (readonly)

Returns the value of attribute method_calls.



8
9
10
# File 'lib/archruby/ruby/parser.rb', line 8

def method_calls
  @method_calls
end

#type_inferenceObject (readonly)

Returns the value of attribute type_inference.



8
9
10
# File 'lib/archruby/ruby/parser.rb', line 8

def type_inference
  @type_inference
end

#type_propagation_parserObject (readonly)

Returns the value of attribute type_propagation_parser.



8
9
10
# File 'lib/archruby/ruby/parser.rb', line 8

def type_propagation_parser
  @type_propagation_parser
end

Instance Method Details

#add_dependency(const_name) ⇒ Object



94
95
96
# File 'lib/archruby/ruby/parser.rb', line 94

def add_dependency(const_name)
  @dependencies << const_name.to_s if !@dependencies.include?(const_name.to_s)
end

#build_call_history(receiver, method_name, params_name) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/archruby/ruby/parser.rb', line 117

def build_call_history(receiver, method_name, params_name)
  @method_calls << {
    :class => @classes.last,
    :method => @current_method_name,
    :method_arguments => @current_arguments,
    :class_call => receiver,
    :method_call => method_name,
    :method_call_params => params_name
  }
end

#build_class_dependency(const_name, line_number) ⇒ Object



83
84
85
86
87
88
# File 'lib/archruby/ruby/parser.rb', line 83

def build_class_dependency(const_name, line_number)
  return if @classes.empty?
  class_name = @classes.last
  @classes_and_dependencies[class_name] = [] if @classes_and_dependencies[class_name].nil?
  @classes_and_dependencies[class_name] << Archruby::Architecture::Dependency.new(const_name, line_number)
end

#build_full_name(const_name) ⇒ Object



76
77
78
79
80
81
# File 'lib/archruby/ruby/parser.rb', line 76

def build_full_name(const_name)
  @full_class_path.unshift(const_name)
  full_class_path = @full_class_path.join('::')
  @full_class_path = []
  full_class_path
end

#build_type_inference(receiver, method_name, params, line_num) ⇒ Object



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
# File 'lib/archruby/ruby/parser.rb', line 128

def build_type_inference(receiver, method_name, params, line_num)
  if !@local_types.nil? && receiver && receiver[0] == :lvar
    receiver = @local_types[receiver[1]]
    params_name = []
    deps = []
    params.each do |param|
      if param[0] == :lvar
        params_name << param[1]
        type_inference = TypeInferenceDep.new(
          :class_source => @classes.last,
          :class_dep => @local_types[param[1]],
          :line_source_num => line_num
        )
        deps << type_inference
      elsif param[0] == :call
        process param
        type_inference = TypeInferenceDep.new(
          :class_source => @classes.last,
          :class_dep => @dependencies.last,
          :line_source_num => line_num
        )
        deps << type_inference
      end
    end
    build_call_history(receiver, method_name, params_name)
    @type_inference << {
      :class_name => receiver,
      :method_name => method_name,
      :dep => deps
    } if deps.size >= 1
  end
end

#extract_arguments(arguments) ⇒ Object



161
162
163
164
165
166
# File 'lib/archruby/ruby/parser.rb', line 161

def extract_arguments(arguments)
  _, *arg_vars = arguments
  if arg_vars[0].class == Symbol
    @current_arguments = arg_vars
  end
end

#get_complete_class_name(exp) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/archruby/ruby/parser.rb', line 55

def get_complete_class_name(exp)
  if exp[0] == :const
    _, const_name = exp
    @complete_class_name.unshift(const_name)
    return
  else
    _, first_part, last_constant_part = exp
    @complete_class_name.unshift(last_constant_part)
    get_complete_class_name first_part
  end
end

#parseObject



28
29
30
31
# File 'lib/archruby/ruby/parser.rb', line 28

def parse
  process ruby_parser.parse(@content)
  @type_propagation_parser.parse(@content)
end

#process_and(exp) ⇒ Object



295
296
297
298
# File 'lib/archruby/ruby/parser.rb', line 295

def process_and(exp)
  _, *args = exp
  args.map! {|sub_tree| process(sub_tree)}
end

#process_arglist(exp) ⇒ Object



342
343
344
345
# File 'lib/archruby/ruby/parser.rb', line 342

def process_arglist(exp)
  _, *args = exp
  args.map! {|sub_tree| process(sub_tree)}
end

#process_args(exp) ⇒ Object



215
216
217
218
219
220
# File 'lib/archruby/ruby/parser.rb', line 215

def process_args(exp)
  _, *args = exp
  args.map! do |sub_tree|
    process sub_tree if sub_tree.class != Symbol
  end
end

#process_array(exp) ⇒ Object



286
287
288
289
# File 'lib/archruby/ruby/parser.rb', line 286

def process_array(exp)
  _, *args = exp
  args.map! {|sub_tree| process(sub_tree)}
end

#process_attrasgn(exp) ⇒ Object



257
258
259
260
261
# File 'lib/archruby/ruby/parser.rb', line 257

def process_attrasgn(exp)
  _, object, method_call, *args = exp
  process(object)
  args.map! {|sub_tree| process(sub_tree)}
end

#process_block(exp) ⇒ Object



33
34
35
36
# File 'lib/archruby/ruby/parser.rb', line 33

def process_block(exp)
  _, *args = exp
  args.map! {|sub_tree| process(sub_tree)}
end

#process_block_pass(exp) ⇒ Object



347
348
349
350
# File 'lib/archruby/ruby/parser.rb', line 347

def process_block_pass(exp)
  _, *args = exp
  args.map! {|sub_tree| process(sub_tree)}
end

#process_break(exp) ⇒ Object



433
434
435
# File 'lib/archruby/ruby/parser.rb', line 433

def process_break(exp)
  _ = exp
end

#process_call(exp) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/archruby/ruby/parser.rb', line 105

def process_call(exp)
  _, receiver, method_name, *args = exp
  process(receiver)
  if receiver && (receiver[0] == :const || receiver[0] == :colon2)
    if @variables
      @var_propagation.push(@variables.last, exp.line, @dependencies.last)
    end
  end
  build_type_inference(receiver, method_name, args, exp.line)
  args.map! {|sub_tree| process sub_tree}
end

#process_case(exp) ⇒ Object



327
328
329
330
# File 'lib/archruby/ruby/parser.rb', line 327

def process_case(exp)
  _, *args = exp
  args.map! {|sub_tree| process(sub_tree)}
end

#process_cdecl(exp) ⇒ Object



291
292
293
# File 'lib/archruby/ruby/parser.rb', line 291

def process_cdecl(exp)
  _, constant_name = exp
end

#process_class(exp) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/archruby/ruby/parser.rb', line 38

def process_class(exp)
  _, class_name, *args = exp
  if class_name.class == Symbol
    if !@module_names.empty?
      @classes << "#{@module_names.join("::")}::#{class_name}"
    else
      @classes << class_name.to_s
    end
  else
    # cai aqui quando a definicao é algo do tipo: class Teste::De end
    get_complete_class_name(class_name)
    @classes << @complete_class_name.join("::")
    @complete_class_name = []
  end
  args.map! {|sub_tree| process(sub_tree)}
end

#process_colon2(exp) ⇒ Object



235
236
237
238
239
# File 'lib/archruby/ruby/parser.rb', line 235

def process_colon2(exp)
  _, first_part, last_part = exp
  @full_class_path.unshift(last_part)
  process(first_part)
end

#process_colon3(exp) ⇒ Object



98
99
100
101
102
103
# File 'lib/archruby/ruby/parser.rb', line 98

def process_colon3(exp)
  _, constant_name = exp
  const_name = build_full_name("::#{constant_name}")
  add_dependency(const_name)
  build_class_dependency(const_name, exp.line)
end

#process_const(exp) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/archruby/ruby/parser.rb', line 67

def process_const(exp)
  _, const_name = exp
  if !@full_class_path.empty?
    const_name = build_full_name(const_name)
  end
  add_dependency(const_name)
  build_class_dependency(const_name, exp.line)
end

#process_cvar(exp) ⇒ Object



397
398
399
# File 'lib/archruby/ruby/parser.rb', line 397

def process_cvar(exp)
  _, variable_name = exp
end

#process_cvdecl(exp) ⇒ Object



392
393
394
395
# File 'lib/archruby/ruby/parser.rb', line 392

def process_cvdecl(exp)
  _, variable_name, *args = exp
  args.map! {|sub_tree| process(sub_tree)}
end

#process_defn(exp) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/archruby/ruby/parser.rb', line 168

def process_defn(exp)
  @variables = []
  @local_types = {}
  _, method_name, method_arguments, *args = exp
  @current_method_name = method_name
  extract_arguments(method_arguments)
  process(method_arguments)
  args.map! {|sub_tree| process sub_tree}
  @var_propagation.vars.each do |var|
    var = var[var.keys.first]
    if var[:type]
      var[:lines].shift
      var[:lines].each do |line_number|
        build_class_dependency(var[:type], line_number)
      end
    end
  end
  @var_propagation = Archruby::Ruby::VarPropagation.new
  @variables = []
  @current_method_name = nil
  @current_arguments = nil
end

#process_defs(exp) ⇒ Object



251
252
253
254
255
# File 'lib/archruby/ruby/parser.rb', line 251

def process_defs(exp)
  _, receiver, method_name, arguments, *args = exp
  process(arguments)
  args.map! {|sub_tree| process(sub_tree)}
end

#process_dot2(exp) ⇒ Object



414
415
416
# File 'lib/archruby/ruby/parser.rb', line 414

def process_dot2(exp)
  _, first, second = exp
end

#process_dot3(exp) ⇒ Object



468
469
470
# File 'lib/archruby/ruby/parser.rb', line 468

def process_dot3(exp)
  _ = exp
end

#process_dregx(exp) ⇒ Object



437
438
439
# File 'lib/archruby/ruby/parser.rb', line 437

def process_dregx(exp)
  _, regex = exp
end

#process_dstr(exp) ⇒ Object



267
268
269
270
# File 'lib/archruby/ruby/parser.rb', line 267

def process_dstr(exp)
  _, string, *args = exp
  args.map! {|sub_tree| process(sub_tree)}
end

#process_dxstr(exp) ⇒ Object



463
464
465
466
# File 'lib/archruby/ruby/parser.rb', line 463

def process_dxstr(exp)
  # shelling out
  _ = exp
end

#process_ensure(exp) ⇒ Object



446
447
448
449
450
# File 'lib/archruby/ruby/parser.rb', line 446

def process_ensure(exp)
  _, rescue_clause, *ensure_clause = exp
  process(rescue_clause)
  ensure_clause.map! {|sub_tree| process(sub_tree)}
end

#process_evstr(exp) ⇒ Object



272
273
274
275
# File 'lib/archruby/ruby/parser.rb', line 272

def process_evstr(exp)
  _, *args = exp
  args.map! {|sub_tree| process(sub_tree)}
end

#process_false(exp) ⇒ Object



323
324
325
# File 'lib/archruby/ruby/parser.rb', line 323

def process_false(exp)
  _ = exp
end

#process_for(exp) ⇒ Object



90
91
92
# File 'lib/archruby/ruby/parser.rb', line 90

def process_for(exp)
  
end

#process_gvar(exp) ⇒ Object



310
311
312
# File 'lib/archruby/ruby/parser.rb', line 310

def process_gvar(exp)
  _, ruby_global_var_name = exp
end

#process_hash(exp) ⇒ Object



241
242
243
244
# File 'lib/archruby/ruby/parser.rb', line 241

def process_hash(exp)
  _, *args = exp
  args.map! {|sub_tree| process(sub_tree)}
end

#process_iasgn(exp) ⇒ Object



246
247
248
249
# File 'lib/archruby/ruby/parser.rb', line 246

def process_iasgn exp
  _, variable_name, *args = exp
  args.map! {|sub_tree| process(sub_tree)}
end

#process_if(exp) ⇒ Object



314
315
316
317
# File 'lib/archruby/ruby/parser.rb', line 314

def process_if(exp)
  _, *args = exp
  args.map! {|sub_tree| process(sub_tree)}
end

#process_iter(exp) ⇒ Object



210
211
212
213
# File 'lib/archruby/ruby/parser.rb', line 210

def process_iter(exp)
  _, *args = exp
  args.map! {|sub_tree| next if sub_tree.class == Fixnum; process(sub_tree)}
end

#process_ivar(exp) ⇒ Object



263
264
265
# File 'lib/archruby/ruby/parser.rb', line 263

def process_ivar(exp)
  _, instance_variable_name = exp
end

#process_lasgn(exp) ⇒ Object



191
192
193
194
195
196
197
198
199
# File 'lib/archruby/ruby/parser.rb', line 191

def process_lasgn(exp)
  _, variable_name, *args = exp
  const_access = args[0][0] == :call unless args[0].nil?
  @variables.push(variable_name) if @variables
  args.map!{ |sub_tree| process(sub_tree) }
  if @local_types.class == Hash && const_access
    @local_types[variable_name] = @dependencies.last
  end
end

#process_lit(exp) ⇒ Object



201
202
203
# File 'lib/archruby/ruby/parser.rb', line 201

def process_lit(exp)
  _, value = exp
end

#process_lvar(exp) ⇒ Object



205
206
207
208
# File 'lib/archruby/ruby/parser.rb', line 205

def process_lvar(exp)
  _, variable_name = exp
  @var_propagation.push(variable_name, exp.line)
end

#process_masgn(exp) ⇒ Object



281
282
283
284
# File 'lib/archruby/ruby/parser.rb', line 281

def process_masgn(exp)
  _, *args = exp
  args.map! {|sub_tree| process(sub_tree)}
end

#process_match3(exp) ⇒ Object



428
429
430
431
# File 'lib/archruby/ruby/parser.rb', line 428

def process_match3(exp)
  _, regular_expression, *args = exp
  args.map! {|sub_tree| process(sub_tree)}
end

#process_module(exp) ⇒ Object



366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/archruby/ruby/parser.rb', line 366

def process_module(exp)
  _, module_name, *args = exp
  if module_name.class == Symbol
    @module_names.push(module_name.to_s)
    @classes << @module_names.join('::')
  else
    get_complete_class_name(module_name)
    @classes << @complete_class_name.join('::')
    @module_names.push @complete_class_name.join('::')
    @complete_class_name = []
  end
  args.map! {|sub_tree| process(sub_tree)}
  @module_names.pop
end

#process_next(exp) ⇒ Object



362
363
364
# File 'lib/archruby/ruby/parser.rb', line 362

def process_next(exp)
  _ = exp
end

#process_nil(exp) ⇒ Object



222
223
224
# File 'lib/archruby/ruby/parser.rb', line 222

def process_nil(exp)
  _ = exp
end

#process_op_asgn1(exp) ⇒ Object



337
338
339
340
# File 'lib/archruby/ruby/parser.rb', line 337

def process_op_asgn1(exp)
  _, variabe_rec, position_to_access, operator, *args = exp
  args.map! {|sub_tree| process(sub_tree)}
end

#process_op_asgn2(exp) ⇒ Object



452
453
454
455
456
# File 'lib/archruby/ruby/parser.rb', line 452

def process_op_asgn2(exp)
  _, left_assign, variable, method, args = exp
  process(left_assign)
  process(args)
end

#process_op_asgn_or(exp) ⇒ Object



422
423
424
425
426
# File 'lib/archruby/ruby/parser.rb', line 422

def process_op_asgn_or(exp)
  _, first, second = exp
  process(first)
  process(second)
end

#process_or(exp) ⇒ Object



352
353
354
355
# File 'lib/archruby/ruby/parser.rb', line 352

def process_or(exp)
  _, *args = exp
  args.map! {|sub_tree| process(sub_tree)}
end

#process_resbody(exp) ⇒ Object



305
306
307
308
# File 'lib/archruby/ruby/parser.rb', line 305

def process_resbody(exp)
  _, *args = exp
  args.map! {|sub_tree| process(sub_tree)}
end

#process_rescue(exp) ⇒ Object



300
301
302
303
# File 'lib/archruby/ruby/parser.rb', line 300

def process_rescue(exp)
  _, *args = exp
  args.map! {|sub_tree| process(sub_tree)}
end

#process_return(exp) ⇒ Object



230
231
232
233
# File 'lib/archruby/ruby/parser.rb', line 230

def process_return(exp)
  _, *args = exp
  args.map! {|sub_tree| process(sub_tree) }
end

#process_sclass(exp) ⇒ Object



357
358
359
360
# File 'lib/archruby/ruby/parser.rb', line 357

def process_sclass(exp)
  _, *args = exp
  args.map! {|sub_tree| process sub_tree}
end

#process_self(exp) ⇒ Object



277
278
279
# File 'lib/archruby/ruby/parser.rb', line 277

def process_self(exp)
  _ = exp
end

#process_splat(exp) ⇒ Object



458
459
460
461
# File 'lib/archruby/ruby/parser.rb', line 458

def process_splat(exp)
  _, left_assign = exp
  process(left_assign)
end

#process_str(exp) ⇒ Object



226
227
228
# File 'lib/archruby/ruby/parser.rb', line 226

def process_str(exp)
  _, string = exp
end

#process_super(exp) ⇒ Object



441
442
443
444
# File 'lib/archruby/ruby/parser.rb', line 441

def process_super(exp)
  _, args = exp
  process(args)
end

#process_to_ary(exp) ⇒ Object



381
382
383
384
# File 'lib/archruby/ruby/parser.rb', line 381

def process_to_ary(exp)
  _, *args = exp
  args.map! {|sub_tree| process(sub_tree)}
end

#process_true(exp) ⇒ Object



319
320
321
# File 'lib/archruby/ruby/parser.rb', line 319

def process_true(exp)
  _ = exp
end

#process_until(exp) ⇒ Object



401
402
403
404
405
# File 'lib/archruby/ruby/parser.rb', line 401

def process_until(exp)
  _, condition, *args = exp
  true_clause = args.pop
  args.map! {|sub_tree| process(sub_tree)}
end

#process_when(exp) ⇒ Object



332
333
334
335
# File 'lib/archruby/ruby/parser.rb', line 332

def process_when(exp)
  _, *args = exp
  args.map! {|sub_tree| process(sub_tree)}
end

#process_while(exp) ⇒ Object



386
387
388
389
390
# File 'lib/archruby/ruby/parser.rb', line 386

def process_while(exp)
  _, condition, *args = exp
  true_clause = args.pop
  args.map! {|sub_tree| process(sub_tree)}
end

#process_yield(exp) ⇒ Object



407
408
409
410
411
412
# File 'lib/archruby/ruby/parser.rb', line 407

def process_yield(exp)
  _, *body = exp
  if !body.empty?
    body.map! {|sub_tree| process(sub_tree)}
  end
end

#process_zsuper(exp) ⇒ Object



418
419
420
# File 'lib/archruby/ruby/parser.rb', line 418

def process_zsuper(exp)
  _ = exp
end

#ruby_parserObject



472
473
474
# File 'lib/archruby/ruby/parser.rb', line 472

def ruby_parser
  RubyParser.new
end