-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_maikit.rb
executable file
·1350 lines (1039 loc) · 37.5 KB
/
generate_maikit.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
222
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env ruby -w
require 'erb'
require 'fileutils'
require 'set'
xcode_path = '/Applications/Xcode-beta.app'
ios_sdk_path = xcode_path + '/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.0.sdk'
ios_uikit_header_path = ios_sdk_path + '/System/Library/Frameworks/UIKit.framework/Headers'
ios_foundation_header_path = ios_sdk_path + '/System/Library/Frameworks/Foundation.framework/Headers'
mac_sdk_path = xcode_path + '/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk'
mac_appkit_header_path = mac_sdk_path + '/System/Library/Frameworks/AppKit.framework/Headers'
mac_foundation_header_path = mac_sdk_path + '/System/Library/Frameworks/Foundation.framework/Headers'
output_path = 'MAIKit'
class String
def to_setter
selector = self.clone
selector[0] = selector[0,1].upcase
return 'set' + selector + ':'
end
end
class AppleType
attr_reader :modifiers
attr_reader :name
attr_reader :pointer
attr_reader :is_enum
@@modifier_tokens = [ 'nullable', 'null_resettable', '__kindof', 'const', 'oneway', 'volatile', 'IBOutlet' ]
def initialize(str)
str = str.strip
@is_enum = false
@pointer = str.end_with?('*')
if @pointer
str = str[0...-1].strip
end
str = str.gsub(/\s+\*/, '*')
@modifiers = []
tokens = str.gsub(/\s+/m, ' ').strip.split(' ')
while !tokens.empty?
token = tokens.shift
if @@modifier_tokens.include?(token)
@modifiers.push(token)
else
if tokens.empty?
@name = token
else
@name = token + ' ' + tokens.join(' ')
end
break
end
end
end
def is_block?
return self.name.include?("^")
end
def method_str
str = ''
name = self.name
append_asterisk = true
if self.pointer && !self.is_enum && self.name.start_with?('MAI')
name = 'id<' + name + 'Protocol>'
append_asterisk = false
end
if self.modifiers.length > 0
components = self.modifiers.dup
components.push(name)
str = components.join(' ')
else
str = name
end
if self.pointer && append_asterisk
str += '*'
end
return str
end
def to_s
str = ''
if self.modifiers.length > 0
components = self.modifiers.dup
components.push(self.name)
str = components.join(' ')
else
str = self.name
end
if self.pointer
str += '*'
end
return str
end
def cast_string
str = ''
name = self.name
if (name == 'IBAction' || name == 'IBOutlet')
name = 'void'
end
if self.modifiers.length > 0
components = self.modifiers.dup.select{ | modifier |
!modifier.include?('null') && !modifier.include?('oneway') && !modifier.include?('IBOutlet')
}
components.push(name)
str = components.join(' ')
else
str = name
end
if self.pointer
str += '*'
end
return str
end
def is_int?
return self.name == 'NSUInteger' || self.name == 'NSInteger' || self.name == 'unsigned int' || self.name == 'int'
end
def is_enum?()
return self.name.start_with?('MAI') && !self.pointer
end
def convert_to(prefix)
@name = prefix + self.name[2..-1]
end
def convert_to_mai_type(mai_classes, mai_protocols, mai_enums)
class_name = self.name
if class_name.start_with?("NS") or class_name.start_with?("UI")
replaced_name = "MAI" + class_name[2..-1]
if mai_enums.include?(replaced_name)
self.convert_to('MAI')
@is_enum = true
else
mai_classes.keys.each do | mai_class_name |
if replaced_name.split(' ').include? mai_class_name
self.convert_to('MAI')
break
end
end
end
end
mai_protocols.each do | mai_protocol_name, mai_protocol |
ns_protocol_name = "NS" + mai_protocol_name[3..-1]
ui_protocol_name = "UI" + mai_protocol_name[3..-1]
name = self.name
name.gsub!(/\b#{ns_protocol_name}\b/, mai_protocol_name)
name.gsub!(/\b#{ui_protocol_name}\b/, mai_protocol_name)
@name = name
end
mai_classes.each do | mai_class_name, mai_class |
ns_class_name = "NS" + mai_class_name[3..-1]
ui_class_name = "UI" + mai_class_name[3..-1]
name = self.name
name.gsub!(/\b#{ns_class_name}\b/, mai_class_name)
name.gsub!(/\b#{ui_class_name}\b/, mai_class_name)
@name = name
end
@name = @name.gsub(/\bNSRect\b/, 'CGRect')
@name = @name.gsub(/\bNSPoint\b/, 'CGPoint')
@name = @name.gsub(/\bNSRect\b/, 'CGRect')
end
def <=>(other)
if self.modifiers.length == 0 && other.modifiers.length == 0
if self.is_int? && other.is_enum?
return 0
elsif self.is_enum? && other.is_int?
return 0
end
end
result = (self.modifiers.sort <=> other.modifiers.sort)
if result != 0
return result
end
result = (self.name <=> other.name)
if result != 0
return result
end
result = (self.pointer <=> other.pointer)
if result != 0
if self.pointer
return 1
else
return -1
end
end
return 0
end
def ==(other)
return (self <=> other) == 0
end
end
class AppleInterface
attr_reader :name
attr_reader :methods
attr_reader :properties
attr_reader :protocols
attr_reader :ios_unavailable_methods
attr_reader :mac_unavailable_methods
attr_accessor :ios_name
attr_accessor :mac_name
attr_accessor :ios_methods
attr_accessor :mac_methods
attr_accessor :ios_properties
attr_accessor :mac_properties
def initialize(name)
@name = name
@methods = {}
@properties = {}
@protocols = []
@ios_unavailable_methods = {}
@mac_unavailable_methods = {}
@ios_methods = nil
@mac_methods = nil
@ios_properties = nil
@mac_properties = nil
end
def update_types(mai_classes, mai_protocols, mai_enums)
self.methods.each do | method_name, method|
method.update_types(mai_classes, mai_protocols, mai_enums)
end
self.properties.each do | property_name, property |
property.update_types(mai_classes, mai_protocols, mai_enums)
end
replaced_protocols = []
self.protocols.each do | protocol |
mai_protocol_name = 'MAI' + protocol[2...protocol.length]
if mai_protocols.include?(mai_protocol_name)
replaced_protocols.push(mai_protocol_name)
else
replaced_protocols.push(protocol)
end
end
@protocols = replaced_protocols
end
def add_method(method)
self.methods[method.name] = method
end
def add_ios_unavailable_method(method)
self.ios_unavailable_methods[method.name] = method
end
def add_mac_unavailable_method(method)
self.mac_unavailable_methods[method.name] = method
end
def get_method(name)
return self.methods[name]
end
def add_property(property)
self.properties[property.name] = property
end
def get_property(name)
return self.properties[name]
end
def add_protocol(protocol)
self.protocols.push(protocol)
end
def contains_protocol(protocol)
if self.protocols.include?(protocol)
return true
end
return false
end
def immediate_protocol_names(foundation_protocols)
return self.protocols.select{ | protocol |
(protocol.start_with?('MAI') or foundation_protocols.include?(protocol)) and
protocol != 'NSObject'
}
end
def protocols_str(foundation_protocols)
return self.immediate_protocol_names(foundation_protocols).join(',')
end
def all_methods(classes)
return self.methods
end
def all_properties(classes)
return self.properties
end
end
class AppleClass < AppleInterface
attr_accessor :superclass
def initialize(name, superclass = 'NSObject')
@superclass = superclass
super(name)
end
def all_methods(classes)
methods = {}
methods = methods.merge(self.methods)
superclass_name = self.superclass
while superclass_name != nil and classes[superclass_name] != nil and superclass_name != 'NSObject'
superclass = classes[superclass_name]
methods = methods.merge(superclass.methods)
superclass_name = superclass.superclass
end
return methods
end
def all_properties(classes)
properties = {}
properties = properties.merge(self.properties)
superclass_name = self.superclass
while superclass_name != nil and classes[superclass_name] != nil and superclass_name != 'NSObject'
superclass = classes[superclass_name]
properties = properties.merge(superclass.properties)
superclass_name = superclass.superclass
end
return properties
end
def root_class(classes)
superclass_name = self.superclass
while superclass_name != nil and superclass_name != 'NSObject'
if classes[superclass_name] == nil
return superclass_name
else
superclass = classes[superclass_name]
superclass_name = superclass.superclass
end
end
return 'NSObject'
end
end
class AppleMethod
include Comparable
attr_reader :prefix
attr_reader :name
attr_reader :return_type
attr_reader :argument_types
attr_reader :argument_names
attr_accessor :designated_initializer
attr_accessor :ns_unavailable
attr_accessor :ns_required
def initialize(
prefix,
name,
return_type,
argument_types,
argument_names,
designated_initializer,
ns_unavailable,
ns_required
)
@prefix = prefix
@name = name
@return_type = return_type
@argument_types = argument_types
@argument_names = argument_names
@designated_initializer = designated_initializer
@ns_unavailable = ns_unavailable
@ns_required = ns_required
end
def self.parse(line, ns_required)
match = /([\-|\+])\s*\(([\w\s\*\(\)\^]+)\)\s*(\w+)/.match(line)
if match != nil
prefix = match[1]
return_type = match[2].gsub(/\s+\*/, '*')
first_arg = match[3]
name = nil
match = line.scan(/(\w+)\s*:\s*\(([\w\s\*\,\(\^\)\<\>\[\]]+)\)\s*(\b(?!(NS|UI)_)\w+)\s*/)
method_components = []
argument_types = []
argument_names = []
for submatch in match
method_components.push(submatch[0])
argument_types.push(AppleType.new(submatch[1]))
argument_names.push(submatch[2])
end
if method_components.length > 0
name = method_components.join(":") + ":"
else
name = first_arg
end
designated_initializer = false
if /NS_DESIGNATED_INITIALIZER/.match(line) != nil
designated_initializer = true
end
ns_unavailable = false
if /NS_UNAVAILABLE/.match(line) != nil
ns_unavailable = true
end
return AppleMethod.new(
prefix,
name,
AppleType.new(return_type),
argument_types,
argument_names,
designated_initializer,
ns_unavailable,
ns_required
)
end
return nil
end
def is_class_method
return self.prefix == '+'
end
def is_initializer(cls)
return self.prefix == '-' && self.name.start_with?('init') &&
((self.return_type.pointer && self.return_type.name == cls) ||
(self.return_type.name == 'instancetype'))
end
def is_convenience_constructor(cls)
return self.is_class_method &&
((self.return_type.pointer && self.return_type.name == cls) ||
(self.return_type.name == 'instancetype'))
end
def to_s
str_value = ''
components = name.split(':')
if (components.length > 1 || name.end_with?(':'))
parts = []
for i in 0...components.length
component = components[i]
argument_type = self.argument_types[i].method_str
argument_name = self.argument_names[i]
parts.push(component + ':(' + argument_type + ')' + argument_name)
end
str_value = parts.join(' ')
else
str_value = self.name
end
str_value = self.prefix + '(' + self.return_type.method_str + ')' + str_value
return str_value
end
def method_call
method_call = ''
components = self.name.split(':')
if self.name.end_with?(':') && components.length != self.argument_names.length
raise "#{components.length} arguments required, but #{self.argument_names.length} for #{self.name}"
end
for i in 0...components.length
component = components[i]
if name.end_with?(':')
arg = self.argument_names[i]
if self.argument_types[i].is_block?
method_call += " #{component}:(id)#{arg}"
else
method_call += " #{component}:#{arg}"
end
else
method_call += " #{component}"
end
end
return method_call
end
def update_types(mai_classes, mai_protocols, mai_enums)
@return_type.convert_to_mai_type(mai_classes, mai_protocols, mai_enums)
@argument_types.each do | argument_type |
argument_type.convert_to_mai_type(mai_classes, mai_protocols, mai_enums)
end
end
def contains_protocol(protocol)
if self.return_type.name.include?('<' + protocol + '>')
return true
end
self.argument_types.each do | argument_type |
if argument_type.name.include?('<' + protocol + '>')
return true
end
end
return false
end
def <=>(other)
result = (self.prefix <=> other.prefix)
if result != 0
return result
end
result = (self.name <=> other.name)
if result != 0
return result
end
result = (self.return_type.name <=> other.return_type.name)
if result != 0
return result
end
result = self.argument_types.length <=> other.argument_types.length
if result != 0
return result
end
self.argument_types.zip(other.argument_types).each do | type1, type2 |
result = (type1 <=> type2)
if result != 0
return result
end
end
return 0
end
end
class AppleProperty
attr_reader :type
attr_reader :name
attr_reader :nullability
attr_accessor :readonly
attr_accessor :ns_required
READONLY = 'readonly'
NULLABLE = 'nullable'
NULL_RESETTABLE = 'null_resettable'
def initialize(
type,
name,
nullability,
readonly,
setter,
getter,
ns_required
)
@type = type
@name = name
@nullability = nullability
@readonly = readonly
@setter = setter
@getter = getter
@ns_required = ns_required
end
def self.parse(line, ns_required)
nullability = nil
readonly = false
line.gsub!(/\/\*.+\*\//, '')
match = /@property\s*(\(([^)]*)\)*)*\s*([\w\s\<\>\*\,\^]+)(\*\s*|\s+)(\b(?!(NS|UI)_)\w+)/.match(line)
if match != nil
attributes = ''
if match[2] != nil
attributes = match[2].delete(' ')
end
type = match[3].gsub(/\s+\*/, '*').strip
pointer = match[4].strip == '*'
name = match[5]
setter = nil
getter = nil
if pointer
type = type + '*'
end
for attribute in attributes.split(',')
if [ NULLABLE, NULL_RESETTABLE ].include?(attribute)
nullability = attribute
elsif [ READONLY ].include?(attribute)
readonly = true
else
submatch = /setter\s*=\s*([^\s]+)/.match(attribute)
if submatch != nil
setter = submatch[1]
end
submatch = /getter\s*=\s*([^\s]+)/.match(attribute)
if submatch != nil
getter = submatch[1]
end
end
end
return AppleProperty.new(
AppleType.new(type),
name,
nullability,
readonly,
setter,
getter,
ns_required
)
end
return nil
end
def to_s
str_value = '@property('
values = []
if self.readonly
values.push(READONLY)
end
if self.nullability != nil
values.push(self.nullability)
end
if self.setter != nil
values.push('setter=' + self.setter)
end
if self.getter != nil
values.push('getter=' + self.getter)
end
if not values.empty?
str_value = str_value + values.join(', ')
end
str_value = str_value + ') ' + self.type.method_str + ' ' + self.name
return str_value
end
def update_types(mai_classes, mai_protocols, mai_enums)
@type.convert_to_mai_type(mai_classes, mai_protocols, mai_enums)
end
def getter
return @getter || self.name
end
def setter
if self.readonly
return nil
end
return @setter || self.name.to_setter
end
def contains_protocol(protocol)
if self.type.name.include?('<' + protocol + '>')
return true
end
return false
end
def <=>(other)
result = (self.type <=> other.type)
if result != 0
return result
end
result = (self.name <=> other.name)
if result != 0
return result
end
result = (self.nullability <=> other.nullability)
if result != 0
return result
end
result = (self.readonly <=> other.readonly)
if result != 0
if self.readonly
return 1
else
return -1
end
end
result = (self.getter <=> other.getter)
if result != 0
return result
end
result = (self.setter <=> other.setter)
if result != 0
return result
end
if result != 0
return result
end
return 0
end
end
class AppleEnum
attr_reader :macro
attr_reader :type
attr_reader :name
attr_reader :members
@@mai_substitutions = {
'MAITextStorageEditedOptions' => 'MAITextStorageEditActions'
}
def initialize(macro, type, name)
@macro = macro
@type = type
@name = 'MAI' + name[2...name.length]
@members = []
if @@mai_substitutions[@name] != nil
@name = @@mai_substitutions[@name]
end
end
def add_member(name, value)
if value != nil
value = value.gsub('NS', 'MAI').gsub('UI', 'MAI')
end
if value == 'MAITableViewRowActionStyleDefault'
return
end
self.members.push({'name' => 'MAI' + name[2...name.length], 'value' => value })
end
def remove_member(name)
@members = self.members.select { | member | member['name'] != name }
end
def has_member?(name)
return self.members.select { | member | member['name'] == name }.length > 0
end
def member_names
return self.members.map { | member | member['name'] }
end
def <=>(other)
result = (self.type <=> other.type)
if result != 0
return result
end
result = (self.name <=> other.name)
if result != 0
return result
end
return self.members.map { | member | member['name'] } <=> other.members.map { | member | member['name'] }
end
def to_s
str_value = 'typedef ' + self.macro + '(' + self.type.to_s + ', ' + self.name + ") {\n"
str_value += self.members.map { | member |
member['value'] ? "#{member['name']} = #{member['value']}" : member['name']
}.join(",\n")
str_value += "\n};\n\n"
return str_value
end
end
def remove_comments!(str, comment_start='/*', comment_end='*/')
while start_idx = str.index(comment_start)
end_idx = str.index(comment_end, start_idx + comment_start.length) + comment_end.length - 1
str[start_idx .. end_idx] = ""
end
return str
end
def parse_headers(header_path)
classes = {}
protocols = {}
enums = {}
Dir.foreach(header_path) do | filename |
current_interface = nil
current_enum = nil
ns_required = false
deprecated = false
filename = header_path + '/' + filename
if File.directory?(filename)
next
end
contents = File.read(filename)
remove_comments!(contents)
partial_line = ''
contents.split("\n").each do | line |
if line.strip.start_with?('//')
next
end
index = line.index('//')
if index != nil
line = line[0...index]
end
if /@required/.match(line) != nil
ns_required = true
end
if /@optional/.match(line) != nil
ns_required = false
end
if /NS_DEPRECATED/.match(line) != nil
deprecated = true
end
match = /@interface\s+(\w+)\s*:\s*(\w+)\s*(\<(.+)\>)*/.match(line)
if match != nil
class_name = match[1]
superclass = match[2]
protocols_str = match[4]
current_interface = AppleClass.new(class_name, superclass)
if protocols_str != nil
protocols_str.split(',').each do | protocol_str |
protocol_str = protocol_str.strip
if !deprecated
current_interface.add_protocol(protocol_str)
end
deprecated = false
end
end
classes[class_name] = current_interface
end
match = /@interface\s+(\w+)\s*\((\w+)\)/.match(line)
if match != nil
class_name = match[1]
category_name = match[2]
if !category_name.include?('Protected')
current_interface = classes[class_name]
end
end
match = /@protocol\s*(\w+)\s*(\<(.+)\>)*/.match(line)
if match != nil
protocol_name = match[1]
current_interface = protocols[protocol_name]
protocols_str = match[3]
current_interface = AppleInterface.new(protocol_name)
ns_required = false
if protocols_str != nil
protocols_str.split(',').each do | protocol_str |
protocol_str = protocol_str.strip
if !deprecated
current_interface.add_protocol(protocol_str)
end
deprecated = false
end
end
protocols[protocol_name] = current_interface
end
match = /(NS_ENUM|NS_OPTIONS)\s*\(\s*(\w+)\s*,\s*(\w+)\s*\)/.match(line)
if match != nil
if line.start_with?('#define')
next
end
macro = match[1]
type = match[2]
name = match[3]
current_enum = AppleEnum.new(macro, AppleType.new(type), name)
enums[current_enum.name] = current_enum;
next
end
if current_enum != nil and line.strip[0] == '{'
next
end
if current_enum != nil and line.strip[0] == '}'
current_enum = nil
end