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.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/archruby/ruby/parser.rb', line 10

def initialize content
  super()
  @content = content
  @dependencies = []
  @classes = []
  @full_class_path = []
  @classes_and_dependencies = {}
  @module_names = []
  @complete_class_name = []
  @var_propagation = Archruby::Ruby::VarPropagation.new
  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

Instance Method Details

#add_dependency(const_name) ⇒ Object



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

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

#build_class_dependency(const_name, line_number) ⇒ Object



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

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



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

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

#get_complete_class_name(exp) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/archruby/ruby/parser.rb', line 49

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



23
24
25
# File 'lib/archruby/ruby/parser.rb', line 23

def parse
  process ruby_parser.parse @content
end

#process_and(exp) ⇒ Object



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

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

#process_arglist(exp) ⇒ Object



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

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

#process_args(exp) ⇒ Object



146
147
148
149
150
151
# File 'lib/archruby/ruby/parser.rb', line 146

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



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

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

#process_attrasgn(exp) ⇒ Object



188
189
190
191
192
# File 'lib/archruby/ruby/parser.rb', line 188

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

#process_block(exp) ⇒ Object



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

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

#process_block_pass(exp) ⇒ Object



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

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

#process_break(exp) ⇒ Object



364
365
366
# File 'lib/archruby/ruby/parser.rb', line 364

def process_break exp
  _ = exp
end

#process_call(exp) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/archruby/ruby/parser.rb', line 95

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
  args.map! {|sub_tree| process sub_tree}
end

#process_case(exp) ⇒ Object



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

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

#process_cdecl(exp) ⇒ Object



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

def process_cdecl exp
  _, constant_name = exp
end

#process_class(exp) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/archruby/ruby/parser.rb', line 32

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



166
167
168
169
170
# File 'lib/archruby/ruby/parser.rb', line 166

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

#process_colon3(exp) ⇒ Object



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

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



61
62
63
64
65
66
67
68
# File 'lib/archruby/ruby/parser.rb', line 61

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



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

def process_cvar exp
  _, variable_name = exp
end

#process_cvdecl(exp) ⇒ Object



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

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

#process_defn(exp) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/archruby/ruby/parser.rb', line 107

def process_defn exp
  @variables = []
  _, method_name, method_arguments, *args = exp
  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 = []
end

#process_defs(exp) ⇒ Object



182
183
184
185
186
# File 'lib/archruby/ruby/parser.rb', line 182

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

#process_dot2(exp) ⇒ Object



345
346
347
# File 'lib/archruby/ruby/parser.rb', line 345

def process_dot2 exp
  _, first, second = exp
end

#process_dot3(exp) ⇒ Object



399
400
401
# File 'lib/archruby/ruby/parser.rb', line 399

def process_dot3 exp
  _ = exp
end

#process_dregx(exp) ⇒ Object



368
369
370
# File 'lib/archruby/ruby/parser.rb', line 368

def process_dregx exp
  _, regex = exp
end

#process_dstr(exp) ⇒ Object



198
199
200
201
# File 'lib/archruby/ruby/parser.rb', line 198

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

#process_dxstr(exp) ⇒ Object



394
395
396
397
# File 'lib/archruby/ruby/parser.rb', line 394

def process_dxstr exp
  # shelling out
  _ = exp
end

#process_ensure(exp) ⇒ Object



377
378
379
380
381
# File 'lib/archruby/ruby/parser.rb', line 377

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



203
204
205
206
# File 'lib/archruby/ruby/parser.rb', line 203

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

#process_false(exp) ⇒ Object



254
255
256
# File 'lib/archruby/ruby/parser.rb', line 254

def process_false exp
  _ = exp
end

#process_gvar(exp) ⇒ Object



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

def process_gvar exp
  _, ruby_global_var_name = exp
end

#process_hash(exp) ⇒ Object



172
173
174
175
# File 'lib/archruby/ruby/parser.rb', line 172

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

#process_iasgn(exp) ⇒ Object



177
178
179
180
# File 'lib/archruby/ruby/parser.rb', line 177

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

#process_if(exp) ⇒ Object



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

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

#process_iter(exp) ⇒ Object



141
142
143
144
# File 'lib/archruby/ruby/parser.rb', line 141

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

#process_ivar(exp) ⇒ Object



194
195
196
# File 'lib/archruby/ruby/parser.rb', line 194

def process_ivar exp
  _, instance_variable_name = exp
end

#process_lasgn(exp) ⇒ Object



126
127
128
129
130
# File 'lib/archruby/ruby/parser.rb', line 126

def process_lasgn exp
  _, variable_name, *args = exp
  @variables.push(variable_name) if @variables
  args.map! {|sub_tree| process sub_tree}
end

#process_lit(exp) ⇒ Object



132
133
134
# File 'lib/archruby/ruby/parser.rb', line 132

def process_lit exp
  _, value = exp
end

#process_lvar(exp) ⇒ Object



136
137
138
139
# File 'lib/archruby/ruby/parser.rb', line 136

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

#process_masgn(exp) ⇒ Object



212
213
214
215
# File 'lib/archruby/ruby/parser.rb', line 212

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

#process_match3(exp) ⇒ Object



359
360
361
362
# File 'lib/archruby/ruby/parser.rb', line 359

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

#process_module(exp) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/archruby/ruby/parser.rb', line 297

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



293
294
295
# File 'lib/archruby/ruby/parser.rb', line 293

def process_next exp
  _ = exp
end

#process_nil(exp) ⇒ Object



153
154
155
# File 'lib/archruby/ruby/parser.rb', line 153

def process_nil exp
  _ = exp
end

#process_op_asgn1(exp) ⇒ Object



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

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



383
384
385
386
387
# File 'lib/archruby/ruby/parser.rb', line 383

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

#process_op_asgn_or(exp) ⇒ Object



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

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

#process_or(exp) ⇒ Object



283
284
285
286
# File 'lib/archruby/ruby/parser.rb', line 283

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

#process_resbody(exp) ⇒ Object



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

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

#process_rescue(exp) ⇒ Object



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

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

#process_return(exp) ⇒ Object



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

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

#process_sclass(exp) ⇒ Object



288
289
290
291
# File 'lib/archruby/ruby/parser.rb', line 288

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

#process_self(exp) ⇒ Object



208
209
210
# File 'lib/archruby/ruby/parser.rb', line 208

def process_self exp
  _ = exp
end

#process_splat(exp) ⇒ Object



389
390
391
392
# File 'lib/archruby/ruby/parser.rb', line 389

def process_splat exp
  _, left_assign = exp
  process left_assign
end

#process_str(exp) ⇒ Object



157
158
159
# File 'lib/archruby/ruby/parser.rb', line 157

def process_str exp
  _, string = exp
end

#process_super(exp) ⇒ Object



372
373
374
375
# File 'lib/archruby/ruby/parser.rb', line 372

def process_super exp
  _, args = exp
  process args
end

#process_to_ary(exp) ⇒ Object



312
313
314
315
# File 'lib/archruby/ruby/parser.rb', line 312

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

#process_true(exp) ⇒ Object



250
251
252
# File 'lib/archruby/ruby/parser.rb', line 250

def process_true exp
  _ = exp
end

#process_until(exp) ⇒ Object



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

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

#process_when(exp) ⇒ Object



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

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

#process_while(exp) ⇒ Object



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

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

#process_yield(exp) ⇒ Object



338
339
340
341
342
343
# File 'lib/archruby/ruby/parser.rb', line 338

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

#process_zsuper(exp) ⇒ Object



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

def process_zsuper exp
  _ = exp
end

#ruby_parserObject



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

def ruby_parser
  RubyParser.new
end