Module: RubyMM

Defined in:
lib/rubymm/query.rb,
lib/rubymm/parser.rb,
lib/rubymm/metamodel.rb

Defined Under Namespace

Classes: ArrayLiteral, BeginRescue, Block, BooleanLiteral, Call, ClassDecl, Constant, Def, ElementAssignement, GlobalVarAccess, GlobalVarAssignment, HashLiteral, HashPair, IfStatement, InstanceVarAccess, InstanceVarAssignement, IntLiteral, Literal, LocalVarAccess, LocalVarAssignment, ModuleDecl, NilLiteral, Return, Statement, StringLiteral, Symbol, Value, VarAccess, VarAssignement

Class Method Summary collapse

Class Method Details

.args_to_model(args_node) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/rubymm/parser.rb', line 223

def self.args_to_model(args_node)
  args=[]
  if args_node.is_a? ArrayNode
    #puts "ARGS #{args_node} #{args_node.size} #{args_node.class}"
    for i in 0..(args_node.size-1) 
      #puts "DEALING WITH #{i} #{args_node.get i} #{(args_node.get i).class}"
      args << node_to_model(args_node.get i)
    end
    args
  elsif args_node.is_a? ListNode 
    for i in 0..(args_node.size-1) 
      #puts "DEALING WITH #{i} #{args_node.get i} #{(args_node.get i).class}"
      args << node_to_model(args_node.get i)
    end
    args    
  else 
    raise "I don't know how to deal with #{args_node.class}"
  end
end

.body_node_to_contents(body_node, container_node) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rubymm/parser.rb', line 29

def self.body_node_to_contents(body_node,container_node)
  body = node_to_model(body_node)
  if body
    if body.is_a? RubyMM::Block 
      body.contents.each do |el|
        container_node.contents = container_node.contents << el 
      end
    else
      container_node.contents = container_node.contents << body
    end
  end
end

.bool(value) ⇒ Object



65
66
67
# File 'lib/rubymm/metamodel.rb', line 65

def self.bool(value)
  BooleanLiteral.build(value)
end

.constant(first_part, *other_parts) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rubymm/metamodel.rb', line 110

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

.int(value) ⇒ Object



69
70
71
# File 'lib/rubymm/metamodel.rb', line 69

def self.int(value)
  IntLiteral.build(value)
end

.is_call(node, name = nil, args = nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/rubymm/query.rb', line 3

def self.is_call(node,name=nil,args=nil)
  return false unless node.is_a? RubyMM::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



15
16
17
18
19
# File 'lib/rubymm/query.rb', line 15

def self.is_def(node,name=nil)
  return false unless node.is_a? RubyMM::Def
  return false if name and node.name!=name
  true
end

.localvarac(name) ⇒ Object



174
175
176
177
178
# File 'lib/rubymm/metamodel.rb', line 174

def self.localvarac(name)
  lva = LocalVarAccess.new
  lva.name = name
  lva
end

.node_to_model(node) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
94
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
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
160
161
162
163
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
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
# File 'lib/rubymm/parser.rb', line 42

def self.node_to_model(node)
  return nil if node==nil
  #puts "#{node} #{node.node_type.name}"
  case node.node_type.name
  when 'NEWLINENODE'
    node_to_model node.next_node
  when 'CALLNODE'
    model = RubyMM::Call.new
    model.name = node.name
    model.receiver = node_to_model node.receiver
    model.args = args_to_model node.args
    model.implicit_receiver = false
    model
  when 'VCALLNODE'
    model = RubyMM::Call.new
    model.name = node.name
    #model.receiver = node_to_model node.receiver
    #model.args = args_to_model node.args
    model.implicit_receiver = false
    model
  when 'FCALLNODE'
    model = RubyMM::Call.new
    model.name = node.name
    #model.receiver = node_to_model node.receiver
    model.args = args_to_model node.args
    model.implicit_receiver = true
    model   
  when 'DEFNNODE'
    model = RubyMM::Def.new
    model.name = node.name
    model.body = node_to_model node.body
    model.onself = false
    model
  when 'DEFSNODE'
    model = RubyMM::Def.new
    model.name = node.name
    model.body = node_to_model node.body
    model.onself = true
    model
  when 'BLOCKNODE'
    model = RubyMM::Block.new   
    for i in 0..(node.size-1)
      content_at_i = node_to_model(node.get i)
      #puts "Adding to contents #{content_at_i}" 
      model.contents = (model.contents << content_at_i)
      #puts "Contents #{model.contents.class}"
    end
    model
  when 'FIXNUMNODE'
    model = RubyMM::IntLiteral.new
    model.value = node.value
    model
  when 'STRNODE'
    model = RubyMM::StringLiteral.new
    model.value = node.value
    model.dynamic = false
    model
  when 'DSTRNODE'
    model = RubyMM::StringLiteral.new
    #model.value = node.value
    model.dynamic = true
    for i in 0..(node.size-1)
      model.pieces = model.pieces << node_to_model(node.get i)
    end
    model
  when 'EVSTRNODE'
    node_to_model(node.body)
  when 'CLASSNODE'
    model = RubyMM::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 'MODULENODE'
    model = RubyMM::ModuleDecl.new
    model.defname = node_to_model(node.getCPath)
    body_node_to_contents(node.body_node,model)
    model
  when 'NILNODE'
    if node.is_a? Java::OrgJrubyparserAst::NilImplicitNode
      return nil
    else
      RubyMM::NilLiteral.new
    end
  when 'COLON2NODE'
    model = RubyMM::Constant.new
    model.name = node.name
    model.container = node_to_model(node.left_node)
    model
  when 'SYMBOLNODE'
    model = RubyMM::Symbol.new
    model.name = node.name
    model
  when 'CONSTNODE'
    model = RubyMM::Constant.new
    model.name = node.name
    model
  when 'LOCALASGNNODE'
    model = RubyMM::LocalVarAssignment.new
    model.name_assigned = node.name
    model.value = node_to_model(node.value)
    model
  when 'LOCALVARNODE'
    model = RubyMM::LocalVarAccess.new
    model.name = node.name
    model
  when 'FALSENODE'
    model = RubyMM::BooleanLiteral.new
    model.value = false
    model
  when 'TRUENODE'
    model = RubyMM::BooleanLiteral.new
    model.value = true
    model
  when 'IFNODE'
    model = RubyMM::IfStatement.new
    model
  when 'GLOBALVARNODE'
    model = RubyMM::GlobalVarAccess.new
    model.name = node.name[1..-1]
    model
  when 'GLOBALASGNNODE'
    model = RubyMM::GlobalVarAssignment.new
    model.name_assigned = node.name[1..-1]
    model.value = node_to_model(node.value)
    model
  when 'HASHNODE'
    model = RubyMM::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 = RubyMM::HashPair.build key: k, value: v
      model.pairs = model.pairs << pair
    end
    model
  when 'ARRAYNODE'
    model = RubyMM::ArrayLiteral.new
    for i in 0..(node.count-1)
      v_node = node[i]
      v = node_to_model(v_node)
      model.values = model.values << v
    end
    model
  when 'ZARRAYNODE'
    RubyMM::ArrayLiteral.new
  when 'BEGINNODE'
    model = RubyMM::BeginRescue.new
    rescue_node = node.body
    raise 'AssertionFailed' unless rescue_node.node_type.name=='RESCUENODE'
    model.body = node_to_model(rescue_node.body)
    rescue_body_node = rescue_node.rescue
    raise 'AssertionFailed' unless rescue_body_node.node_type.name=='RESCUEBODYNODE'
    model.rescue_body = node_to_model(rescue_body_node.body)
    model
  when 'ATTRASSIGNNODE'
    model = RubyMM::ElementAssignement.new
    model.array = node_to_model(node.receiver)
    model.element = node_to_model(node.args[0])
    model.value = node_to_model(node.args[1])
    model
  when 'INSTASGNNODE'
    model = RubyMM::InstanceVarAssignement.new
    model.name_assigned = node.name[1..-1]
    model.value = node_to_model(node.value)
    model
  when 'INSTVARNODE'
    RubyMM::InstanceVarAccess.build node.name[1..-1]
  when 'RETURNNODE'
    model = RubyMM::Return.new
    model.value = node_to_model(node.value)
    model
  when 'CONSTDECLNODE'
    raise 'Const decl node: not implemented'
  else   
    raise "I don't know how to deal with #{node.node_type.name}"
  end
end

.parse(code) ⇒ Object



16
17
18
19
20
# File 'lib/rubymm/parser.rb', line 16

def self.parse(code)
  tree = JRubyParser.parse(code)
  #puts "Code: #{code} Root: #{tree}"
  tree_to_model(tree)
end

.parse_file(path) ⇒ Object



11
12
13
14
# File 'lib/rubymm/parser.rb', line 11

def self.parse_file(path)
  content = IO.read(path)
  self.parse(content)
end

.string(value) ⇒ Object



79
80
81
# File 'lib/rubymm/metamodel.rb', line 79

def self.string(value)
  StringLiteral.build(value)
end

.tree_to_model(tree) ⇒ Object



22
23
24
25
26
27
# File 'lib/rubymm/parser.rb', line 22

def self.tree_to_model(tree)
  unless tree.node_type.name=='ROOTNODE' 
    raise 'Root expected'
  end
  node_to_model tree.body
end