Module: CodeModels::Ruby
- Defined in:
- lib/codemodels/ruby/code.rb,
lib/codemodels/ruby/query.rb,
lib/codemodels/ruby/parser.rb,
lib/codemodels/ruby/language.rb,
lib/codemodels/ruby/metamodel.rb,
lib/codemodels/ruby/model_building.rb,
lib/codemodels/ruby/info_extraction.rb
Defined Under Namespace
Modules: InfoExtraction, RawNodeAccessModule, RubyInfoExtractionFunctionalities
Classes: AbstractCodeBlock, AliasStatement, AndOperator, Argument, ArrayLiteral, Assignment, BackReference, BeginEndBlock, BinaryOperator, Block, BlockReference, BlockVarAccess, BlockVarAssignment, BooleanLiteral, BreakStatement, Call, CallToSuper, CaseStatement, ClassDecl, ClassVarAccess, ClassVarAssignment, CmdLineStringLiteral, CodeBlock, Constant, ConstantDecl, Def, DynamicRegExpLiteral, DynamicStringLiteral, DynamicSymbol, ElementAssignment, ElementOperationAssignment, ExplicitReceiverCall, FloatLiteral, ForStatement, FormalArgument, GlobalScopeReference, GlobalVarAccess, GlobalVarAssignment, HashLiteral, HashPair, IfStatement, ImplicitReceiverCall, InstanceDef, InstanceVarAccess, InstanceVarAssignment, IntLiteral, IsDefined, Literal, LiteralReference, LocalVarAccess, LocalVarAssignment, ModuleDecl, MultipleAssignment, NextStatement, NilLiteral, NthGroupReference, OperatorAssignment, OrAssignment, OrOperator, Parser, ParsingError, Range, RegExpLiteral, RegexMatcher, RegexTryer, RescueClause, RescueStatement, RetryStatement, Return, RubyLanguage, RubyNode, Self, SelfDef, SingletonClassDecl, Splat, SplittedArgument, Statement, StaticRegExpLiteral, StaticStringLiteral, StringLiteral, SuperCall, Symbol, UnaryOperation, UndefStatement, UnknownNodeType, UntilStatement, Value, VarAccess, VarAssignment, WhenClause, WhileStatement, YieldStatement
Class Attribute Summary collapse
Class Method Summary
collapse
-
.args_to_model(args_node) ⇒ Object
-
.assert_node_type(node, type) ⇒ Object
-
.body_node_to_contents(body_node, container_node) ⇒ Object
-
.bool(value) ⇒ Object
-
.clean_args(args) ⇒ Object
-
.constant(first_part, *other_parts) ⇒ Object
-
.containment_pos(node) ⇒ Object
-
.corresponding_node(model_element, node_tree) ⇒ Object
node tree contains the original TO BE FIXED.
-
.generate_model_per_file(src, dest, model_ext = 'rb.lm', max_nesting = 500, error_handler = nil) ⇒ Object
-
.generate_models_in_dir(src, dest, model_ext = 'rb.lm', max_nesting = 500, error_handler = nil) ⇒ Object
-
.get_var_name_depending_on_parser_version(node, prefix_size = 1) ⇒ Object
-
.handle_models_in_dir(src, error_handler = nil, model_handler) ⇒ Object
-
.handle_serialized_models_in_dir(src, error_handler = nil, model_handler) ⇒ Object
-
.int(value) ⇒ Object
-
.is_call(node, name = nil, args = nil) ⇒ Object
-
.is_def(node, name = nil) ⇒ Object
-
.localvarac(name) ⇒ Object
-
.my_args_flattener(args_node) ⇒ Object
-
.node_to_model(node, parent_model = nil) ⇒ Object
-
.node_tree_from_code(code) ⇒ Object
-
.parse(code) ⇒ Object
-
.parse_code(code) ⇒ Object
-
.parse_file(path) ⇒ Object
-
.populate_from_list(array, list_node) ⇒ Object
-
.process_body(node, model) ⇒ Object
-
.process_formal_args(node, model) ⇒ Object
-
.splat(v) ⇒ Object
-
.string(value) ⇒ Object
-
.to_code(node) ⇒ Object
-
.tree_to_model(tree) ⇒ Object
-
.unknown_node_type_found(node) ⇒ Object
Class Attribute Details
.skip_unknown_node ⇒ Object
Returns the value of attribute skip_unknown_node.
25
26
27
|
# File 'lib/codemodels/ruby/parser.rb', line 25
def skip_unknown_node
@skip_unknown_node
end
|
Class Method Details
.args_to_model(args_node) ⇒ Object
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
|
# File 'lib/codemodels/ruby/parser.rb', line 771
def self.args_to_model(args_node)
args=[]
if args_node.is_a? ArrayNode
for i in 0..(args_node.size-1)
args << node_to_model(args_node.get i)
end
args
elsif args_node.is_a? ListNode
for i in 0..(args_node.size-1)
args << node_to_model(args_node.get i)
end
args
elsif args_node.is_a? ArgsNode
populate_from_list(args,args_node.pre) if args_node.pre
populate_from_list(args,args_node.optional) if args_node.optional
populate_from_list(args,args_node.rest) if args_node.rest
populate_from_list(args,args_node.post) if args_node.post
args
elsif args_node.is_a? Node
args << node_to_model(args_node)
args
else
raise UnknownNodeType.new(args_node,'in args')
end
end
|
.assert_node_type(node, type) ⇒ Object
85
86
87
|
# File 'lib/codemodels/ruby/parser.rb', line 85
def self.assert_node_type(node,type)
raise ParsingError.new(node,"AssertionFailed: #{type} expected but #{node.node_type.name} found") unless node.node_type.name==type
end
|
.body_node_to_contents(body_node, container_node) ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/codemodels/ruby/parser.rb', line 111
def self.body_node_to_contents(body_node,container_node)
body = node_to_model(body_node)
if body
if body.is_a? Ruby::Block
body.contents.each do |el|
container_node.addContents(el)
end
else
container_node.addContents( body )
end
end
end
|
.bool(value) ⇒ Object
169
170
171
|
# File 'lib/codemodels/ruby/metamodel.rb', line 169
def self.bool(value)
BooleanLiteral.build(value)
end
|
.clean_args(args) ⇒ Object
760
761
762
763
764
765
766
767
768
769
|
# File 'lib/codemodels/ruby/parser.rb', line 760
def self.clean_args(args)
for i in 0..(args.count-1)
if args[i].is_a? Ruby::MultipleAssignment
old = args[i]
args[i] = Ruby::SplittedArgument.new
old.assignments.each {|a| args[i].names = args[i].names << a.name_assigned}
end
end
args
end
|
.constant(first_part, *other_parts) ⇒ Object
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
# File 'lib/codemodels/ruby/metamodel.rb', line 246
def self.constant(first_part,*other_parts)
cont = Constant.build(first_part)
return cont if other_parts.count == 0
new_first_part, *new_other_parts = other_parts
internal_constant = constant(new_first_part, *new_other_parts)
if internal_constant.container
internal_constant.top_container.container = cont
else
internal_constant.container = cont
end
internal_constant
end
|
.containment_pos(node) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/codemodels/ruby/parser.rb', line 55
def self.containment_pos(node)
container = node.eContainer
children = node.eContainer.send(node.eContainingFeature)
if children.respond_to?(:each)
children.each_with_index do |c,i|
return i if c==node
end
raise "Not found"
else
raise "Not found" unless children==node
0
end
end
|
.corresponding_node(model_element, node_tree) ⇒ Object
node tree contains the original TO BE FIXED
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/codemodels/ruby/parser.rb', line 71
def self.corresponding_node(model_element,node_tree)
return node_tree unless model_element.eContainer
corresponding_parent_node = corresponding_node(model_element.eContainer,node_tree)
containment_pos = containment_pos(model_element)
containing_feat = model_element.eContainingFeature
children = corresponding_parent_node.send(containing_feat)
if children.respond_to?(:each)
children[containment_pos]
else
children
end
end
|
.generate_model_per_file(src, dest, model_ext = 'rb.lm', max_nesting = 500, error_handler = nil) ⇒ Object
27
28
29
30
31
32
|
# File 'lib/codemodels/ruby/model_building.rb', line 27
def self.generate_model_per_file(src,dest,model_ext='rb.lm',max_nesting=500,error_handler=nil)
CodeModels::ModelBuilding.generate_model_per_file(src,dest,max_nesting,error_handler) do |src|
root = Ruby.parse_file(src)
CodeModels::Serialization.rgenobject_to_model(root)
end
end
|
.generate_models_in_dir(src, dest, model_ext = 'rb.lm', max_nesting = 500, error_handler = nil) ⇒ Object
20
21
22
23
24
25
|
# File 'lib/codemodels/ruby/model_building.rb', line 20
def self.generate_models_in_dir(src,dest,model_ext='rb.lm',max_nesting=500,error_handler=nil)
CodeModels::ModelBuilding.generate_models_in_dir(src,dest,'rb',model_ext,max_nesting,error_handler) do |src|
root = Ruby.parse_file(src)
CodeModels::Serialization.rgenobject_to_model(root)
end
end
|
.get_var_name_depending_on_parser_version(node, prefix_size = 1) ⇒ Object
124
125
126
127
128
129
130
|
# File 'lib/codemodels/ruby/parser.rb', line 124
def self.get_var_name_depending_on_parser_version(node,prefix_size=1)
if node.respond_to? :lexical_name return node.name
else
return node.name[prefix_size..-1]
end
end
|
.handle_models_in_dir(src, error_handler = nil, model_handler) ⇒ Object
7
8
9
10
11
|
# File 'lib/codemodels/ruby/model_building.rb', line 7
def self.handle_models_in_dir(src,error_handler=nil,model_handler)
CodeModels::ModelBuilding.handle_models_in_dir(src,'rb',error_handler,model_handler) do |src|
root = Ruby.parse_file(src)
end
end
|
.handle_serialized_models_in_dir(src, error_handler = nil, model_handler) ⇒ Object
13
14
15
16
17
18
|
# File 'lib/codemodels/ruby/model_building.rb', line 13
def self.handle_serialized_models_in_dir(src,error_handler=nil,model_handler)
CodeModels::ModelBuilding.handle_models_in_dir(src,'rb',error_handler,model_handler) do |src|
root = Ruby.parse_file(src)
CodeModels::Serialization.rgenobject_to_model(root)
end
end
|
.int(value) ⇒ Object
173
174
175
|
# File 'lib/codemodels/ruby/metamodel.rb', line 173
def self.int(value)
IntLiteral.build(value)
end
|
.is_call(node, name = nil, args = nil) ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
|
# File 'lib/codemodels/ruby/query.rb', line 5
def self.is_call(node,name=nil,args=nil)
return false unless node.is_a? Ruby::Call
return false if name and node.name!=name
if args
return false if args.count != node.args.count
for i in 0..(args.count-1)
return false unless args[i].eql?(node.args[i])
end
end
true
end
|
.is_def(node, name = nil) ⇒ Object
17
18
19
20
21
|
# File 'lib/codemodels/ruby/query.rb', line 17
def self.is_def(node,name=nil)
return false unless node.is_a? Ruby::Def
return false if name and node.name!=name
true
end
|
.localvarac(name) ⇒ Object
334
335
336
337
338
|
# File 'lib/codemodels/ruby/metamodel.rb', line 334
def self.localvarac(name)
lva = LocalVarAccess.new
lva.name = name
lva
end
|
.my_args_flattener(args_node) ⇒ Object
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
|
# File 'lib/codemodels/ruby/parser.rb', line 132
def self.my_args_flattener(args_node)
if args_node.node_type.name=='ARGSPUSHNODE'
res = my_args_flattener(args_node.firstNode)
res << node_to_model(args_node.secondNode)
res
elsif args_node.node_type.name=='ARGSCATNODE'
res = my_args_flattener(args_node.firstNode)
if args_node.secondNode.is_a?(ArrayNode)
res.concat(my_args_flattener(args_node.secondNode))
else
res << Ruby.splat(node_to_model(args_node.secondNode))
end
res.class.class_eval do
include RawNodeAccessModule
end
res.original_node = args_node.secondNode
res
elsif args_node.is_a? ListNode
res = []
for i in 0..(args_node.size-1)
res << node_to_model(args_node.get i)
end
res
elsif args_node.node_type.name=='SPLATNODE'
res = []
res << node_to_model(args_node)
res
else
raise "Unknown: #{args_node.node_type.name} at #{args_node.position}, parent #{args_node.parent}"
end
end
|
.node_to_model(node, parent_model = nil) ⇒ Object
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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
330
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
|
# File 'lib/codemodels/ruby/parser.rb', line 223
def self.node_to_model(node,parent_model=nil)
return nil if node==nil
case node.node_type.name
when 'NEWLINENODE'
model = node_to_model node.next_node
when 'FLOATNODE'
model = Ruby::FloatLiteral.new
model.value = node.value
model
when 'FIXNUMNODE'
model = Ruby::IntLiteral.new
model.value = node.value
model
when 'STRNODE'
model = Ruby::StaticStringLiteral.new
model.value = node.value
model
when 'DSTRNODE'
model = Ruby::DynamicStringLiteral.new
for i in 0..(node.size-1)
model.addPieces( node_to_model(node.get i) )
end
model
when 'DXSTRNODE'
model = Ruby::CmdLineStringLiteral.new
for i in 0...node.size
model.addPieces( node_to_model(node.get i) )
end
model
when 'DSYMBOLNODE'
model = Ruby::DynamicSymbol.new
for i in 0...node.size
model.addPieces( node_to_model(node.get i) )
end
model
when 'DREGEXPNODE'
model = Ruby::DynamicRegExpLiteral.new
for i in 0...node.size
model.addPieces( node_to_model(node.get i) )
end
model
when 'NILNODE'
begin
implicit_nil = Java::OrgJrubyparserAst::NilImplicitNode
rescue
implicit_nil = nil end
if implicit_nil && (node.is_a? Java::OrgJrubyparserAst::NilImplicitNode)
return nil
else
model = Ruby::NilLiteral.new
end
when 'FALSENODE'
model = Ruby::BooleanLiteral.new
model.value = false
model
when 'TRUENODE'
model = Ruby::BooleanLiteral.new
model.value = true
model
when 'REGEXPNODE'
model = Ruby::StaticRegExpLiteral.new
model.value = node.value
model
when 'LOCALVARNODE'
model = Ruby::LocalVarAccess.new
model.name = node.name
model
when 'DVARNODE'
model = Ruby::BlockVarAccess.new
model.name = node.name
model
when 'GLOBALVARNODE'
model = Ruby::GlobalVarAccess.new
model.name = get_var_name_depending_on_parser_version(node)
model
when 'CLASSVARNODE'
model = Ruby::ClassVarAccess.new
model.name = get_var_name_depending_on_parser_version(node,2)
model
when 'INSTVARNODE'
model = Ruby::InstanceVarAccess.build get_var_name_depending_on_parser_version(node)
when 'LOCALASGNNODE'
model = Ruby::LocalVarAssignment.new
model.name_assigned = node.name
model.value = node_to_model(node.value)
model
when 'DASGNNODE'
model = Ruby::BlockVarAssignment.new
model.name_assigned = node.name
model.value = node_to_model(node.value)
model
when 'GLOBALASGNNODE'
model = Ruby::GlobalVarAssignment.new
model.name_assigned = get_var_name_depending_on_parser_version(node)
model.value = node_to_model(node.value)
model
when 'CLASSVARASGNNODE'
model = Ruby::ClassVarAssignment.new
model.name_assigned = get_var_name_depending_on_parser_version(node,2)
model.value = node_to_model(node.value)
model
when 'INSTASGNNODE'
model = Ruby::InstanceVarAssignment.new
model.name_assigned = get_var_name_depending_on_parser_version(node)
model.value = node_to_model(node.value)
model
when 'OPELEMENTASGNNODE'
model = Ruby::ElementOperationAssignment.new
model.container = node_to_model(node.receiver)
model.element = node_to_model(node.args[0])
model.value = node_to_model(node.value)
model.operator = node.operator_name
model
when 'ATTRASSIGNNODE'
model = Ruby::ElementAssignment.new
model.container = node_to_model(node.receiver)
if node.args model.element = node_to_model(node.args[0])
model.value = node_to_model(node.args[1])
end
model
when 'OPASGNORNODE'
model = Ruby::OrAssignment.new
model.assigned = node_to_model(node.first)
model.value = node_to_model(node.second).value
model
when 'MULTIPLEASGNNODE'
model = Ruby::MultipleAssignment.new
if node.pre
for i in 0..(node.pre.count-1)
model.addAssignments( node_to_model(node.pre.get(i)) )
end
end
values_model = node_to_model(node.value)
if values_model.respond_to? :values
values_model.values.each {|x| model.addValues(x) }
else
model.addValues( values_model )
end
model
when 'MULTIPLEASGN19NODE'
model = Ruby::MultipleAssignment.new
if node.pre
for i in 0..(node.pre.count-1)
model.addAssignments( node_to_model(node.pre.get(i)) )
end
end
values_model = node_to_model(node.value)
if values_model.respond_to? :values
values_model.values.each {|x| model.addValues(x)}
else
model.addValues( values_model )
end
model
when 'OPASGNNODE'
model = Ruby::OperatorAssignment.new
model.value = node_to_model(node.valueNode)
model.container = node_to_model(node.receiverNode)
model.element_name = node.variable_name
model.operator_name = node.operator_name
model
when 'CONSTDECLNODE'
model = Ruby::ConstantDecl.new
model.name = node.name
model.value = node_to_model(node.value)
model
when 'ALIASNODE'
model = Ruby::AliasStatement.new
model.old_name = node_to_model(node.oldName)
model.new_name = node_to_model(node.newName)
model
when 'CASENODE'
model = Ruby::CaseStatement.new
for ci in 0..(node.cases.count-1)
c = node.cases[ci]
model.addWhen_clauses( node_to_model(c) )
end
model.else_body = node_to_model(node.else)
model
when 'WHENNODE'
model = Ruby::WhenClause.new
model.body = node_to_model(node.body)
model.condition = node_to_model(node.expression)
model
when 'UNDEFNODE'
model = Ruby::UndefStatement.new
model.name = node_to_model(node.name)
model
when 'WHILENODE'
model = Ruby::WhileStatement.new
model.body = node_to_model(node.body)
model.condition = node_to_model(node.condition)
model
when 'FORNODE'
model = Ruby::ForStatement.new
model.body = node_to_model(node.body)
model.collection = node_to_model(node.iter)
model.iterator = node_to_model(node.var)
model
when 'BREAKNODE'
model = Ruby::BreakStatement.new
when 'UNTILNODE'
model = Ruby::UntilStatement.new
model.body = node_to_model(node.body)
model.condition = node_to_model(node.condition)
model
when 'RESCUENODE'
model = Ruby::RescueStatement.new
model.body = node_to_model(node.body)
model.value = node_to_model(node.rescueNode.body)
model
when 'NTHREFNODE'
model = Ruby::NthGroupReference.build(node.matchNumber)
when 'YIELDNODE'
model = Ruby::YieldStatement.new
when 'NEXTNODE'
model = Ruby::NextStatement.new
when 'COLON3NODE'
model = Ruby::GlobalScopeReference.new
model.name = node.name
model
when 'DOTNODE'
model = Ruby::Range.new
model.lower = node_to_model(node.beginNode)
model.upper = node_to_model(node.endNode)
model
when 'MATCH2NODE'
model = Ruby::RegexTryer.new
model.checked_value = node_to_model(node.value)
model.regex = node_to_model(node.receiver)
model
when 'MATCH3NODE'
model = Ruby::RegexMatcher.new
model.checked_value = node_to_model(node.value)
model.regex = node_to_model(node.receiver)
model
when 'LITERALNODE'
model = Ruby::LiteralReference.new
model.value = node.name
model
when 'SELFNODE'
model = Ruby::Self.new
model
when 'ZSUPERNODE'
model = Ruby::CallToSuper.new
if node.iter
model.block_arg = node_to_model(node.iter)
end
model
when 'SUPERNODE'
model = Ruby::SuperCall.new
if node.args.node_type.name == 'BLOCKPASSNODE'
model.block_arg = node_to_model(node.args)
args_to_process = node.args.args
else
args_to_process = node.args
end
if node.iter==nil and args_to_process.is_a?IterNode
model.block_arg = node_to_model(args_to_process)
else
model.block_arg = node_to_model(node.iter) if node.iter
model.args = my_args_flattener(args_to_process) if args_to_process
end
model
when 'CALLNODE'
model = Ruby::ExplicitReceiverCall.new
model.name = node.name
model.receiver = node_to_model node.receiver
if node.args.node_type.name == 'BLOCKPASSNODE'
model.block_arg = node_to_model(node.args)
args_to_process = node.args.args
else
args_to_process = node.args
end
if node.iter==nil and args_to_process.is_a?IterNode
model.block_arg = node_to_model(args_to_process)
else
model.block_arg = node_to_model(node.iter) if node.iter
model.args = my_args_flattener(args_to_process) if args_to_process
end
model
when 'VCALLNODE'
model = Ruby::ExplicitReceiverCall.new
model.name = node.name
model
when 'FCALLNODE'
model = Ruby::ImplicitReceiverCall.new
model.name = node.name
if node.args.node_type.name == 'BLOCKPASSNODE'
model.block_arg = node_to_model(node.args)
args_to_process = node.args.args
else
args_to_process = node.args
end
if node.iter==nil and args_to_process.is_a?IterNode
model.block_arg = node_to_model(args_to_process)
else
model.block_arg = node_to_model(node.iter) if node.iter
model.args = my_args_flattener(args_to_process) if args_to_process
end
model
when 'DEFNNODE'
model = Ruby::InstanceDef.new
model.name = node.name
process_formal_args(node,model)
process_body(node,model)
model
when 'DEFSNODE'
model = Ruby::SelfDef.new
model.name = node.name
process_formal_args(node,model)
process_body(node,model)
model
when 'BLOCKNODE'
model = Ruby::Block.new
for i in 0..(node.size-1)
content_at_i = node_to_model(node.get i)
model.addContents( content_at_i )
end
model
when 'BACKREFNODE'
model = Ruby::BackReference.new
when 'EVSTRNODE'
model = node_to_model(node.body)
when 'CLASSNODE'
model = Ruby::ClassDecl.new
model.defname = node_to_model(node.getCPath)
model.super_class = node_to_model(node.super)
body_node_to_contents(node.body_node,model)
model
when 'SCLASSNODE'
model = Ruby::SingletonClassDecl.new
model.object = node_to_model(node.receiver)
body_node_to_contents(node.body_node,model)
model
when 'MODULENODE'
model = Ruby::ModuleDecl.new
model.defname = node_to_model(node.getCPath)
body_node_to_contents(node.body_node,model)
model
when 'COLON2NODE'
model = Ruby::Constant.new
model.name = node.name
model.container = node_to_model(node.left_node)
model
when 'SYMBOLNODE'
model = Ruby::Symbol.new
model.name = node.name
model
when 'CONSTNODE'
model = Ruby::Constant.new
model.name = node.name
model
when 'IFNODE'
model = Ruby::IfStatement.new
model.condition = node_to_model(node.condition)
model.then_body = node_to_model(node.then_body)
model.else_body = node_to_model(node.else_body)
model
when 'HASHNODE'
model = Ruby::HashLiteral.new
count = node.get_list_node.count / 2
for i in 0..(count-1)
k_node = node.get_list_node[i*2]
k = node_to_model(k_node)
v_node = node.get_list_node[i*2 +1]
v = node_to_model(v_node)
pair = Ruby::HashPair.build key: k, value: v
model.addPairs(pair)
end
model
when 'DEFINEDNODE'
model = Ruby::IsDefined.new
model.value = node_to_model(node.expression)
model
when 'ARRAYNODE'
model = Ruby::ArrayLiteral.new
for i in 0..(node.count-1)
v_node = node[i]
v = node_to_model(v_node)
model.addValues(v)
end
model
when 'SPLATNODE'
model = Ruby::Splat.new
model.splatted = node_to_model(node.value)
model
when 'UNARYCALLNODE'
model = Ruby::UnaryOperation.new
model.value = node_to_model(node.receiver)
model.operator_name = node.lexical_name
model
when 'ZARRAYNODE'
model = Ruby::ArrayLiteral.new
when 'RETRYNODE'
model = Ruby::RetryStatement.new
when 'BEGINNODE'
model = Ruby::BeginEndBlock.new
process_body(node,model)
model
when 'RETURNNODE'
model = Ruby::Return.new
model.value = node_to_model(node.value)
model
when 'ANDNODE'
model = Ruby::AndOperator.new
model.left = node_to_model(node.first)
model.right = node_to_model(node.second)
model
when 'ORNODE'
model = Ruby::OrOperator.new
model.left = node_to_model(node.first)
model.right = node_to_model(node.second)
model
when 'ITERNODE'
model = Ruby::CodeBlock.new
model.args = clean_args(args_to_model(node.var))
model.body = node_to_model(node.body)
model
when 'ARGUMENTNODE'
model = Ruby::Argument.new
model.name = node.name
model
when 'RESTARG'
model = Ruby::Argument.new
model.name = node.name
model
when 'BLOCKPASSNODE'
model = Ruby::BlockReference.new
model.value = node_to_model(node.body)
model
when 'ARGSCATNODE'
model = Ruby::ArrayLiteral.new
model.values = my_args_flattener(node)
model
when 'ARGSPUSHNODE'
model = Ruby::ArrayLiteral.new
model.values = my_args_flattener(node)
model
when 'OPTARGNODE'
model = Ruby::FormalArgument.new
model.name = node.name
model.default_value = node_to_model(node.value)
model
else
unknown_node_type_found(node)
end
model.class.class_eval do
include RawNodeAccessModule
end
model.original_node = node
model
end
|
.node_tree_from_code(code) ⇒ Object
100
101
102
|
# File 'lib/codemodels/ruby/parser.rb', line 100
def self.node_tree_from_code(code)
JRubyParser.parse(code, {:version=>JRubyParser::Compat::RUBY2_0})
end
|
.parse(code) ⇒ Object
96
97
98
|
# File 'lib/codemodels/ruby/parser.rb', line 96
def self.parse(code)
parse_code(code)
end
|
.parse_code(code) ⇒ Object
90
91
92
93
|
# File 'lib/codemodels/ruby/parser.rb', line 90
def self.parse_code(code)
tree = JRubyParser.parse(code, {:version=>JRubyParser::Compat::RUBY2_0})
tree_to_model(tree)
end
|
.parse_file(path) ⇒ Object
28
29
30
31
|
# File 'lib/codemodels/ruby/parser.rb', line 28
def self.parse_file(path)
content = IO.read(path)
self.parse(content)
end
|
.populate_from_list(array, list_node) ⇒ Object
750
751
752
753
754
755
756
757
758
|
# File 'lib/codemodels/ruby/parser.rb', line 750
def self.populate_from_list(array,list_node)
if list_node.respond_to? :size
for i in 0..(list_node.size-1)
array << node_to_model(list_node.get i)
end
else
array << node_to_model(list_node)
end
end
|
.process_body(node, model) ⇒ Object
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
# File 'lib/codemodels/ruby/parser.rb', line 164
def self.process_body(node,model)
if node.body==nil
model.body = nil
elsif node.body.node_type.name=='RESCUENODE'
rescue_node = node.body
model.body = node_to_model(rescue_node.body)
rescue_body_node = rescue_node.rescue
raise 'AssertionFailed' unless rescue_body_node.node_type.name=='RESCUEBODYNODE'
rescue_clause_model = Ruby::RescueClause.new
rescue_clause_model.class.class_eval do
include RawNodeAccessModule
end
rescue_clause_model.original_node = node
rescue_clause_model.body = node_to_model(rescue_body_node.body)
model.addRescue_clauses( rescue_clause_model )
elsif node.body.node_type.name=='ENSURENODE'
ensure_node = node.body
model.ensure_body = node_to_model(ensure_node.ensure)
model.body = node_to_model(ensure_node.body)
else
model.body = node_to_model(node.body,model)
end
end
|
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
|
# File 'lib/codemodels/ruby/parser.rb', line 190
def self.process_formal_args(node,model)
args = []
args_node = node.args
populate_from_list(args,args_node.pre) if args_node.pre
populate_from_list(args,args_node.optional) if args_node.optional
populate_from_list(args,args_node.rest) if args_node.rest
populate_from_list(args,args_node.post) if args_node.post
args.each do |a|
if a.is_a?(Argument)
fa = FormalArgument.new
fa.name = a.name
fa.class.class_eval do
include RawNodeAccessModule
end
fa.original_node = node
model.addFormal_args(fa)
elsif a.is_a?(FormalArgument)
if a.default_value.is_a? VarAssignment
a.default_value = a.default_value.value
end
model.addFormal_args(a)
else
raise "Unexpected #{a.class}"
end
end
end
|
.splat(v) ⇒ Object
432
433
434
435
436
|
# File 'lib/codemodels/ruby/metamodel.rb', line 432
def self.splat(v)
s = Splat.new
s.splatted = v
s
end
|
.string(value) ⇒ Object
198
199
200
|
# File 'lib/codemodels/ruby/metamodel.rb', line 198
def self.string(value)
StaticStringLiteral.build(value)
end
|
.to_code(node) ⇒ Object
4
5
6
7
8
9
10
11
|
# File 'lib/codemodels/ruby/code.rb', line 4
def self.to_code(node)
on = node.original_node
sw = java.io.StringWriter.new
rwv = org.jrubyparser.rewriter.ReWriteVisitor.new(sw,'')
cbw = org.jrubyparser.rewriter.ClassBodyWriter.new(rwv,on)
cbw.write
sw.to_string
end
|
.tree_to_model(tree) ⇒ Object
104
105
106
107
108
109
|
# File 'lib/codemodels/ruby/parser.rb', line 104
def self.tree_to_model(tree)
unless tree.node_type.name=='ROOTNODE'
raise 'Root expected'
end
node_to_model tree.body
end
|
.unknown_node_type_found(node) ⇒ Object
742
743
744
745
746
747
748
|
# File 'lib/codemodels/ruby/parser.rb', line 742
def self.unknown_node_type_found(node)
if Ruby.skip_unknown_node
puts "skipping #{node.node_type.name} at #{node.position}..."
else
raise UnknownNodeType.new(node)
end
end
|