a73d52820adaa31f45cbd572e4f3a5d14c9f1d86
[nit.git] / contrib / objcwrapper / src / objc_visitor.nit
1 module objc_visitor
2
3 import objc_model
4 import objc_parser
5
6 class Interpretor
7 super Visitor
8
9 var is_variable = false
10 var is_method = false
11 var is_parameter_name = false
12 var class_objc: nullable ObjcClass = null
13 var method_objc: ObjcMethod is noinit
14 var attribute_objc: ObjcAttribute is noinit
15 var param: Param is noinit
16 var model = new ObjcModel
17
18 redef fun visit(n) do n.accept_objc(self)
19 end
20
21 redef class Node
22 fun accept_objc(v: Interpretor) do visit_children(v)
23 end
24
25 redef class Nlines
26 redef fun accept_objc(v) do
27 end
28 end
29
30 redef class Nlines_interface
31 redef fun accept_objc(v) do
32 var interface_block = n_interface_block
33 var inheritance_block = n_inheritance
34 v.class_objc = null
35 if interface_block != null then
36 for class_objc in v.model.classes do
37 if class_objc.name == n_class.text then
38 v.class_objc = class_objc
39 end
40 end
41 if v.class_objc == null then
42 v.class_objc = new ObjcClass(n_class.text)
43 v.model.classes.add(v.class_objc)
44 end
45 if inheritance_block != null then v.enter_visit(inheritance_block)
46 v.enter_visit(interface_block)
47 end
48 end
49 end
50
51 redef class Ninheritance_add
52 redef fun accept_objc(v) do
53 var additional = n_additional
54 v.enter_visit(n_classe)
55 if additional != null then v.enter_visit(additional)
56 end
57 end
58
59 redef class Nclasse_class
60 redef fun accept_objc(v) do
61 v.class_objc.super_names.add(n_class.text)
62 end
63 end
64
65 redef class Nadditional_add
66 redef fun accept_objc(v) do
67 v.enter_visit(n_classe)
68 end
69 end
70
71 redef class Ninterface_block_instance
72 redef fun accept_objc(v) do
73 v.enter_visit(n_instance_declaration)
74 end
75 end
76
77 redef class Ninstance_declaration_signature
78 redef fun accept_objc(v) do
79 v.enter_visit(n_signature_block)
80 end
81 end
82
83 redef class Ninstance_declaration_property
84 redef fun accept_objc(v) do
85 v.enter_visit(n_property_declaration)
86 end
87 end
88
89 redef class Nproperty_declaration_property
90 redef fun accept_objc(v) do
91 v.enter_visit(n_property)
92 end
93 end
94
95 redef class Nsignature_block_signature
96 redef fun accept_objc(v) do
97 if n_signature.children.to_s.has("signature_named") or n_signature.children.to_s.has("signature_single") then
98 v.method_objc = new ObjcMethod
99
100 v.enter_visit(n_scope)
101 v.is_method = true
102 var signature_return_type = n_signature_return_type
103 if signature_return_type != null then v.enter_visit(signature_return_type)
104 v.is_method = false
105 v.enter_visit(n_signature)
106
107 v.class_objc.methods.add(v.method_objc)
108 end
109 end
110 end
111
112 redef class Nscope_instance
113 redef fun accept_objc(v) do
114 v.method_objc.scope = '-'
115 end
116 end
117
118 redef class Nscope_class
119 redef fun accept_objc(v) do
120 v.method_objc.scope = '+'
121 end
122 end
123
124 redef class Nsignature_return_type_return
125 redef fun accept_objc(v) do
126 v.enter_visit(n_type)
127 end
128 end
129
130 redef class Nsignature_named
131 redef fun accept_objc(v) do
132 v.param = new Param
133
134 v.enter_visit(n_left)
135 v.is_parameter_name = true
136 v.enter_visit(n_right)
137 v.is_parameter_name = false
138 v.enter_visit(n_signature_type)
139
140 v.method_objc.params.add(v.param)
141 end
142 end
143
144 redef class Nsignature_single
145 redef fun accept_objc(v) do
146 v.param = new Param
147 v.param.is_single = true
148 v.enter_visit(n_term)
149 v.method_objc.params.add(v.param)
150 end
151 end
152
153 redef class Nsignature_type_anonymous
154 redef fun accept_objc(v) do
155 v.enter_visit(n_type)
156 v.method_objc.is_commented = true
157 end
158 end
159
160 redef class Nsignature_type_table
161 redef fun accept_objc(v) do
162 v.enter_visit(n_type)
163 v.param.is_table = true
164 end
165 end
166
167 redef class Nsignature_type_pointer
168 redef fun accept_objc(v) do
169 v.enter_visit(n_type)
170 v.param.is_pointer = true
171 end
172 end
173
174 redef class Nsignature_type_unsigned
175 redef fun accept_objc(v) do
176 v.method_objc.is_commented = true
177 v.param.is_pointer = true
178 end
179 end
180
181 redef class Nsignature_type_protocol
182 redef fun accept_objc(v) do
183 v.enter_visit(n_type)
184 v.method_objc.is_commented = true
185 end
186 end
187
188 redef class Nsignature_type_normal
189 redef fun accept_objc(v) do
190 v.enter_visit(n_type)
191 end
192 end
193
194 redef class Nterm_private
195 redef fun accept_objc(v) do
196 if v.is_parameter_name then
197 v.param.variable_name = n_private.text
198 else if v.is_variable then
199 v.attribute_objc.name = n_private.text
200 else
201 v.param.name = n_private.text
202 end
203 end
204 end
205
206 redef class Nterm_class
207 redef fun accept_objc(v) do
208 if v.is_parameter_name then
209 v.param.variable_name = n_class.text
210 else if v.is_variable then
211 v.attribute_objc.name = n_class.text
212 else
213 v.param.name = n_class.text
214 end
215 end
216 end
217
218 redef class Nterm_var
219 redef fun accept_objc(v) do
220 if v.is_parameter_name then
221 v.param.variable_name = n_id.text
222 else if v.is_variable then
223 v.attribute_objc.name = n_id.text
224 else
225 v.param.name = n_id.text
226 end
227 end
228 end
229
230 redef class Nproperty_property
231 redef fun accept_objc(v) do
232 var protocol = n_protocols
233 if protocol == null then
234 v.is_variable = true
235 v.attribute_objc = new ObjcAttribute
236 v.enter_visit(n_left)
237 v.enter_visit(n_type)
238 v.class_objc.attributes.add(v.attribute_objc)
239 v.is_variable = false
240 end
241 end
242 end
243
244 redef class Ntype_type
245 redef fun accept_objc(v) do
246 v.enter_visit(n_data_type)
247 end
248 end
249
250 redef class Ndata_type_more
251 redef fun accept_objc(v) do
252 v.enter_visit(n_more_type)
253 end
254 end
255
256 redef class Ndata_type_otype
257 redef fun accept_objc(v) do
258 if v.is_variable then
259 v.attribute_objc.return_type = n_class.text
260 else if v.is_method then
261 v.method_objc.return_type = n_class.text
262 else
263 v.param.return_type = n_class.text
264 end
265 end
266 end
267
268 redef class Nmore_type_stype
269 redef fun accept_objc(v) do
270 v.enter_visit(n_specific_type)
271 end
272 end
273
274 redef class Nmore_type_ptype
275 redef fun accept_objc(v) do
276 v.enter_visit(n_primitive_type)
277 end
278 end
279
280 redef class Nspecific_type_i
281 redef fun accept_objc(v) do
282 if v.is_variable then
283 v.attribute_objc.return_type = "id"
284 else if v.is_method then
285 v.method_objc.return_type = "id"
286 else
287 v.param.return_type = "id"
288 end
289 end
290 end
291
292 redef class Nspecific_type_b
293 redef fun accept_objc(v) do
294 if v.is_variable then
295 v.attribute_objc.return_type = "Bool"
296 else if v.is_method then
297 v.method_objc.return_type = "Bool"
298 else
299 v.param.return_type = "Bool"
300 end
301 end
302 end
303
304 redef class Nspecific_type_val
305 redef fun accept_objc(v) do
306 if v.is_variable then
307 v.attribute_objc.return_type = "va_list"
308 else if v.is_method then
309 v.method_objc.return_type = "va_list"
310 else
311 v.param.return_type = "va_list"
312 end
313 end
314 end
315
316 redef class Nspecific_type_v
317 redef fun accept_objc(v) do
318 if v.is_variable then
319 v.attribute_objc.return_type = "void"
320 else if v.is_method then
321 v.method_objc.return_type = "void"
322 else
323 v.param.return_type = "void"
324 end
325 end
326 end
327
328 redef class Nprimitive_type_ui8
329 redef fun accept_objc(v) do
330 if v.is_variable then
331 v.attribute_objc.return_type = "uint8_t"
332 else if v.is_method then
333 v.method_objc.return_type = "uint8_t"
334 else
335 v.param.return_type = "uint8_t"
336 end
337 end
338 end
339
340 redef class Nprimitive_type_ui16
341 redef fun accept_objc(v) do
342 if v.is_variable then
343 v.attribute_objc.return_type = "uint16_t"
344 else if v.is_method then
345 v.method_objc.return_type = "uint16_t"
346 else
347 v.param.return_type = "uint16_t"
348 end
349 end
350 end
351
352 redef class Nprimitive_type_ui32
353 redef fun accept_objc(v) do
354 if v.is_variable then
355 v.attribute_objc.return_type = "uint32_t"
356 else if v.is_method then
357 v.method_objc.return_type = "uint32_t"
358 else
359 v.param.return_type = "uint32_t"
360 end
361 end
362 end
363
364 redef class Nprimitive_type_ui64
365 redef fun accept_objc(v) do
366 if v.is_variable then
367 v.attribute_objc.return_type = "uint64_t"
368 else if v.is_method then
369 v.method_objc.return_type = "uint64_t"
370 else
371 v.param.return_type = "uint64_t"
372 end
373 end
374 end
375
376 redef class Nprimitive_type_i8
377 redef fun accept_objc(v) do
378 if v.is_variable then
379 v.attribute_objc.return_type = "int8_t"
380 else if v.is_method then
381 v.method_objc.return_type = "int8_t"
382 else
383 v.param.return_type = "int8_t"
384 end
385 end
386 end
387
388 redef class Nprimitive_type_i16
389 redef fun accept_objc(v) do
390 if v.is_variable then
391 v.attribute_objc.return_type = "int16_t"
392 else if v.is_method then
393 v.method_objc.return_type = "int16_t"
394 else
395 v.param.return_type = "int16_t"
396 end
397 end
398 end
399
400 redef class Nprimitive_type_i32
401 redef fun accept_objc(v) do
402 if v.is_variable then
403 v.attribute_objc.return_type = "int32_t"
404 else if v.is_method then
405 v.method_objc.return_type = "int32_t"
406 else
407 v.param.return_type = "int32_t"
408 end
409 end
410 end
411
412 redef class Nprimitive_type_i64
413 redef fun accept_objc(v) do
414 if v.is_variable then
415 v.attribute_objc.return_type = "int64_t"
416 else if v.is_method then
417 v.method_objc.return_type = "int64_t"
418 else
419 v.param.return_type = "int64_t"
420 end
421 end
422 end
423
424 redef class Nprimitive_type_uc
425 redef fun accept_objc(v) do
426 if v.is_variable then
427 v.attribute_objc.return_type = "unichar"
428 else if v.is_method then
429 v.method_objc.return_type = "unichar"
430 else
431 v.param.return_type = "unichar"
432 end
433 end
434 end
435
436 redef class Nprimitive_type_c
437 redef fun accept_objc(v) do
438 if v.is_variable then
439 v.attribute_objc.return_type = "char"
440 else if v.is_method then
441 v.method_objc.return_type = "char"
442 else
443 v.param.return_type = "char"
444 end
445 end
446 end
447
448 redef class Nprimitive_type_s
449 redef fun accept_objc(v) do
450 if v.is_variable then
451 v.attribute_objc.return_type = "short"
452 else if v.is_method then
453 v.method_objc.return_type = "short"
454 else
455 v.param.return_type = "short"
456 end
457 end
458 end
459
460 redef class Nprimitive_type_si
461 redef fun accept_objc(v) do
462 if v.is_variable then
463 v.attribute_objc.return_type = "short int"
464 else if v.is_method then
465 v.method_objc.return_type = "short int"
466 else
467 v.param.return_type = "short int"
468 end
469 end
470 end
471
472 redef class Nprimitive_type_i
473 redef fun accept_objc(v) do
474 if v.is_variable then
475 v.attribute_objc.return_type = "int"
476 else if v.is_method then
477 v.method_objc.return_type = "int"
478 else
479 v.param.return_type = "int"
480 end
481 end
482 end
483
484 redef class Nprimitive_type_l
485 redef fun accept_objc(v) do
486 if v.is_variable then
487 v.attribute_objc.return_type = "long"
488 else if v.is_method then
489 v.method_objc.return_type = "long"
490 else
491 v.param.return_type = "long"
492 end
493 end
494 end
495
496 redef class Nprimitive_type_li
497 redef fun accept_objc(v) do
498 if v.is_variable then
499 v.attribute_objc.return_type = "long int"
500 else if v.is_method then
501 v.method_objc.return_type = "long int"
502 else
503 v.param.return_type = "long int"
504 end
505 end
506 end
507
508 redef class Nprimitive_type_ll
509 redef fun accept_objc(v) do
510 if v.is_variable then
511 v.attribute_objc.return_type = "long long"
512 else if v.is_method then
513 v.method_objc.return_type = "long long"
514 else
515 v.param.return_type = "long long"
516 end
517 end
518 end
519
520 redef class Nprimitive_type_lli
521 redef fun accept_objc(v) do
522 if v.is_variable then
523 v.attribute_objc.return_type = "long long int"
524 else if v.is_method then
525 v.method_objc.return_type = "long long int"
526 else
527 v.param.return_type = "long long int"
528 end
529 end
530 end
531
532 redef class Nprimitive_type_f
533 redef fun accept_objc(v) do
534 if v.is_variable then
535 v.attribute_objc.return_type = "float"
536 else if v.is_method then
537 v.method_objc.return_type = "float"
538 else
539 v.param.return_type = "float"
540 end
541 end
542 end
543
544 redef class Nprimitive_type_d
545 redef fun accept_objc(v) do
546 if v.is_variable then
547 v.attribute_objc.return_type = "double"
548 else if v.is_method then
549 v.method_objc.return_type = "double"
550 else
551 v.param.return_type = "double"
552 end
553 end
554 end
555
556 redef class Nprimitive_type_ld
557 redef fun accept_objc(v) do
558 if v.is_variable then
559 v.attribute_objc.return_type = "long double"
560 else if v.is_method then
561 v.method_objc.return_type = "long double"
562 else
563 v.param.return_type = "long double"
564 end
565 end
566 end