1 # This file is part of NIT (http://www.nitlanguage.org).
3 # Copyright 2014 Frédéric Vachon <fredvac@gmail.com>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Uses a visitor to extract data from the javap output AST
18 # It sends the data to `code_generator` module
21 import javap_test_parser
28 var java_class
= new JavaClass
29 var declaration_type
: nullable String = null
30 var declaration_element
: nullable String = null
31 var full_class_name
= new Array[String]
34 var variable_type
= new JavaType
36 var is_generic_param
= false
37 var gen_params_index
= 0
39 var is_primitive_array
= false
42 var method_return_type
= new JavaType
43 var method_params
= new Array[JavaType]
46 redef fun visit
(n
) do n
.accept_visitor
(self)
50 fun accept_visitor
(v
: JavaVisitor) do visit_children
(v
)
53 redef class Nidentifier
54 redef fun accept_visitor
(v
)
56 if v
.declaration_type
== "class_header" then
58 if v
.declaration_element
== "id" then
59 v
.full_class_name
.add
(self.text
)
62 else if v
.declaration_type
== "variable" then
64 if v
.declaration_element
== "id" then
65 v
.variable_id
+= self.text
66 else if v
.declaration_element
== "type" then
67 if v
.is_generic_param
then
68 v
.variable_type
.generic_params
[v
.gen_params_index
].identifier
.add
(self.text
)
70 v
.variable_type
.identifier
.add
(self.text
)
74 else if v
.declaration_type
== "method" then
76 if v
.declaration_element
== "id" then
77 v
.method_id
= self.text
78 else if v
.declaration_element
== "return_type" then
79 if self.text
== "void" then
80 v
.method_return_type
.is_void
= true
81 else if v
.is_generic_param
then
82 v
.method_return_type
.generic_params
[v
.gen_params_index
].identifier
.add
(self.text
)
84 v
.method_return_type
.identifier
.add
(self.text
)
86 else if v
.declaration_element
== "parameter_list" then
87 if v
.is_generic_param
then
88 v
.method_params
[v
.param_index
].generic_params
[v
.gen_params_index
].identifier
.add
(self.text
)
90 v
.method_params
[v
.param_index
].identifier
.add
(self.text
)
100 # Primitive array node
101 redef class N_39d_91d_93d_39d
102 redef fun accept_visitor
(v
)
104 if v
.declaration_type
== "variable" then
105 if v
.declaration_element
== "type" then
106 if v
.is_generic_param
then
107 v
.variable_type
.generic_params
[v
.gen_params_index
].array_dimension
+= 1
109 v
.variable_type
.array_dimension
+= 1
113 else if v
.declaration_type
== "method" then
115 if v
.declaration_element
== "return_type" then
116 if v
.is_generic_param
then
117 v
.method_return_type
.generic_params
[v
.gen_params_index
].array_dimension
+= 1
119 v
.method_return_type
.array_dimension
+= 1
121 else if v
.declaration_element
== "parameter_list" then
122 if v
.is_generic_param
then
123 v
.method_params
[v
.param_index
].generic_params
[v
.gen_params_index
].array_dimension
+= 1
125 v
.method_params
[v
.param_index
].array_dimension
+= 1
135 redef class N_39dchar_39d
136 redef fun accept_visitor
(v
)
138 if v
.declaration_type
== "variable" then
139 if v
.declaration_element
== "type" then
140 v
.variable_type
.identifier
.add
(self.text
)
142 else if v
.declaration_type
== "method" then
143 if v
.declaration_element
== "return_type" then
144 v
.method_return_type
.identifier
.add
(self.text
)
145 else if v
.declaration_element
== "parameter_list" then
146 v
.method_params
[v
.param_index
].identifier
.add
(self.text
)
152 redef class N_39dboolean_39d
153 redef fun accept_visitor
(v
)
155 if v
.declaration_type
== "variable" then
156 if v
.declaration_element
== "type" then
157 v
.variable_type
.identifier
.add
(self.text
)
159 else if v
.declaration_type
== "method" then
160 if v
.declaration_element
== "return_type" then
161 v
.method_return_type
.identifier
.add
(self.text
)
162 else if v
.declaration_element
== "parameter_list" then
163 v
.method_params
[v
.param_index
].identifier
.add
(self.text
)
169 redef class N_39dfloat_39d
170 redef fun accept_visitor
(v
)
172 if v
.declaration_type
== "variable" then
173 if v
.declaration_element
== "type" then
174 v
.variable_type
.identifier
.add
(self.text
)
176 else if v
.declaration_type
== "method" then
177 if v
.declaration_element
== "return_type" then
178 v
.method_return_type
.identifier
.add
(self.text
)
179 else if v
.declaration_element
== "parameter_list" then
180 v
.method_params
[v
.param_index
].identifier
.add
(self.text
)
186 redef class N_39ddouble_39d
187 redef fun accept_visitor
(v
)
189 if v
.declaration_type
== "variable" then
190 if v
.declaration_element
== "type" then
191 v
.variable_type
.identifier
.add
(self.text
)
193 else if v
.declaration_type
== "method" then
194 if v
.declaration_element
== "return_type" then
195 v
.method_return_type
.identifier
.add
(self.text
)
196 else if v
.declaration_element
== "parameter_list" then
197 v
.method_params
[v
.param_index
].identifier
.add
(self.text
)
203 redef class N_39dbyte_39d
204 redef fun accept_visitor
(v
)
206 if v
.declaration_type
== "variable" then
207 if v
.declaration_element
== "type" then
208 v
.variable_type
.identifier
.add
(self.text
)
210 else if v
.declaration_type
== "method" then
211 if v
.declaration_element
== "return_type" then
212 v
.method_return_type
.identifier
.add
(self.text
)
213 else if v
.declaration_element
== "parameter_list" then
214 v
.method_params
[v
.param_index
].identifier
.add
(self.text
)
220 redef class N_39dshort_39d
221 redef fun accept_visitor
(v
)
223 if v
.declaration_type
== "variable" then
224 if v
.declaration_element
== "type" then
225 v
.variable_type
.identifier
.add
(self.text
)
227 else if v
.declaration_type
== "method" then
228 if v
.declaration_element
== "return_type" then
229 v
.method_return_type
.identifier
.add
(self.text
)
230 else if v
.declaration_element
== "parameter_list" then
231 v
.method_params
[v
.param_index
].identifier
.add
(self.text
)
237 redef class N_39dint_39d
238 redef fun accept_visitor
(v
)
240 if v
.declaration_type
== "variable" then
241 if v
.declaration_element
== "type" then
242 v
.variable_type
.identifier
.add
(self.text
)
244 else if v
.declaration_type
== "method" then
245 if v
.declaration_element
== "return_type" then
246 v
.method_return_type
.identifier
.add
(self.text
)
247 else if v
.declaration_element
== "parameter_list" then
248 v
.method_params
[v
.param_index
].identifier
.add
(self.text
)
254 redef class N_39dlong_39d
255 redef fun accept_visitor
(v
)
257 if v
.declaration_type
== "variable" then
258 if v
.declaration_element
== "type" then
259 v
.variable_type
.identifier
.add
(self.text
)
261 else if v
.declaration_type
== "method" then
262 if v
.declaration_element
== "return_type" then
263 v
.method_return_type
.identifier
.add
(self.text
)
264 else if v
.declaration_element
== "parameter_list" then
265 v
.method_params
[v
.param_index
].identifier
.add
(self.text
)
272 # C L A S S H E A D E R #
274 redef class Nclass_header
275 redef fun accept_visitor
(v
)
277 v
.declaration_type
= "class_header"
278 v
.declaration_element
= "id"
281 # Exit class declaration
282 v
.declaration_type
= null
283 v
.declaration_element
= null
285 v
.java_class
.name
= v
.full_class_name
289 # Extends declaration in the class header
290 redef class Nextends_declaration
291 redef fun accept_visitor
(v
)
293 v
.declaration_element
= "extends"
295 v
.declaration_element
= null
299 # Implements declaration in the class header
300 redef class Nimplements_declaration
301 redef fun accept_visitor
(v
)
303 v
.declaration_element
= "implements"
305 v
.declaration_element
= null
310 # F I E L D D E C L A R A T I O N S #
313 # Method declaration in the field declarations
314 redef class Nmethod_declaration
315 redef fun accept_visitor
(v
)
317 v
.declaration_type
= "method"
319 v
.declaration_type
= null
321 v
.java_class
.add_method
(v
.method_id
, v
.method_return_type
, v
.method_params
)
323 v
.method_params
.clear
325 v
.method_return_type
= new JavaType
329 # Constructor declaration in the field declarations
330 redef class Nconstructor_declaration
331 redef fun accept_visitor
(v
)
333 v
.declaration_type
= "constructor"
335 v
.declaration_type
= null
339 # Variable declaration in the field declarations
340 redef class Nvariable_declaration
341 redef fun accept_visitor
(v
)
343 v
.declaration_type
= "variable"
345 v
.declaration_type
= null
347 v
.java_class
.attributes
[v
.variable_id
] = v
.variable_type
350 v
.variable_type
= new JavaType
354 # Static declaration in the field declarations
355 redef class Nstatic_declaration
356 redef fun accept_visitor
(v
)
358 v
.declaration_type
= "static"
360 v
.declaration_type
= null
364 # Identifier of the field
365 redef class Nvariable_id
366 redef fun accept_visitor
(v
)
368 v
.declaration_element
= "id"
370 v
.declaration_element
= null
374 # Identifier of the method
375 redef class Nmethod_id
376 redef fun accept_visitor
(v
)
378 v
.declaration_element
= "id"
380 v
.declaration_element
= null
385 redef fun accept_visitor
(v
)
387 if v
.declaration_type
== "variable" and v
.declaration_element
!= "id" then
388 v
.declaration_element
= "type"
391 if v
.declaration_type
== "method" and v
.declaration_element
== null then
392 v
.declaration_element
= "return_type"
397 if v
.declaration_element
== "variable" then
398 v
.declaration_element
= null
403 redef class Ngeneric_param
404 redef fun accept_visitor
(v
)
406 # Ignore the weird generic return type declaration
407 if v
.declaration_type
== "method" then
408 if v
.declaration_element
== null then
409 v
.declaration_element
= "ignore"
411 v
.is_generic_param
= true
412 v
.gen_params_index
= 0
414 if v
.declaration_element
== "return_type" then
415 v
.method_return_type
.generic_params
= new Array[JavaType]
416 else if v
.declaration_element
== "parameter_list" then
417 v
.method_params
[v
.param_index
].generic_params
= new Array[JavaType]
420 else if v
.declaration_type
== "variable" then
421 if v
.declaration_element
== "type" then
422 v
.is_generic_param
= true
423 v
.gen_params_index
= 0
424 v
.variable_type
.generic_params
= new Array[JavaType]
430 v
.declaration_element
= null
431 v
.is_generic_param
= false
435 redef class Nparameter_list
436 redef fun accept_visitor
(v
)
438 v
.declaration_element
= "parameter_list"
441 v
.declaration_element
= null
446 redef class Nparameter
447 redef fun accept_visitor
(v
)
449 if v
.declaration_type
== "method" then
450 if v
.declaration_element
== "parameter_list" then
451 if v
.is_generic_param
then
452 v
.method_params
[v
.param_index
].generic_params
.add
(new JavaType)
456 v
.gen_params_index
+= 1
458 v
.method_params
.add
(new JavaType)
464 else if v
.declaration_element
== "return_type" and v
.is_generic_param
then
466 v
.method_return_type
.generic_params
.add
(new JavaType)
470 v
.gen_params_index
+= 1
472 else if v
.declaration_type
== "variable" then
473 if v
.declaration_element
== "type" and v
.is_generic_param
then
474 v
.variable_type
.generic_params
.add
(new JavaType)
478 v
.gen_params_index
+= 1
486 var p
= new TestParser_javap
489 var visitor
= new JavaVisitor
490 visitor
.enter_visit
(tree
)
492 var generator
= new CodeGenerator("bundle.nit", visitor
.java_class
)