-
Notifications
You must be signed in to change notification settings - Fork 0
/
processors.rb
173 lines (145 loc) · 4.01 KB
/
processors.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
# interface that parses for tokens to process.
SingleLineAnnotationSymbol = /\/\/@/
class ImplBlockProcessor
def initialize(str)
@str = str
end
def self.parse(str)
blocks = Array.new
str.scan(/@implementation\s+.*?@end/m).each do |block|
blocks << self.new(block)
end
return blocks
end
def generate
return @str + "\n"
end
end
class MethodPrototypesProcessor
def initialize(str)
@tokens = str.scan(/^[+-]\s*\(.*?\)\s*.*?(?=\{)/m)
end
def generate
generated = ""
@tokens.each do |token|
generated << token.gsub(/\/\/.*$/, '').strip << ";\n"
end
return generated
end
end
class ClassStartProcessor
Pattern = /@implementation\s+([\w]+)(.*)?/
AnnotationPattern = /(?:\s*\/\/@(.*))/
CategoryPattern = /\s*\(.*?\)/
attr_reader :isClassDefinition
def initialize(str)
super()
parsed = str.scan(Pattern).first
@className = parsed[0]
if parsed[1] != nil
@isClassDefinition = parsed[1].scan(CategoryPattern).flatten.empty?
@postClassName = parsed[1].scan(AnnotationPattern).flatten[0]
@postClassName ||= parsed[1].scan(CategoryPattern)[0]
end
@postClassName ||= ' :NSObject'
end
def generate
return '@interface ' << @className << @postClassName << "\n"
end
end
class ClassEndProcessor
def generate
return "@end"
end
end
class HeaderDeclarationProcessor
attr_reader :found
def initialize(str)
parsed = str.scan(/\/\*@*(.*)\*\//m).first
@found = ! parsed.nil?
@imports = ImportsProcessor.parse(parsed.to_s)
@ivars = IvarsProcessor.parse(parsed.to_s)
end
def generate(token_symbol)
generated = ""
instance_variable_get("@" << token_symbol.to_s).each {|token| generated << token.generate}
return generated
end
end
class ImportsProcessor
def self.parse(str)
tokens = Array.new
str.scan(/(?:#import|@class).*/).each do |proto|
tokens << self.new(proto)
end
return tokens
end
def initialize(str)
@str = str
end
def generate
return @str + "\n"
end
end
class IvarsProcessor
def self.parse(str)
tokens = []
parsed = str.split(/^\s*state:\s*\n/);
return tokens if parsed.length < 2
parsed[1].each do |line|
tokens << self.new(line.strip) if ! line.strip.empty?
end
return tokens
end
def initialize(ivarDeclaration)
@ivarDeclaration = ivarDeclaration
end
def generate
return @ivarDeclaration << ";\n"
end
end
class PropertiesProcessor
def self.parse(str)
scanned = str.scan( /@(synthesize|dynamic)\s*(.*?)(?:\s*#{SingleLineAnnotationSymbol})\s*(\(.*?\))\s*(.*)/ )
# TODO create hash of scanned tokens by type (synthesize|dynamic)
tokens = scanned.collect{ |e| e[1..-1] }
tokens_excluded = scanned.select{ |e| e[0].eql? 'dynamic' }.collect{ |e| e[1..-1] }
return self.new(tokens, tokens_excluded)
end
def initialize(tokens, tokens_excluded)
@tokens = tokens
@tokens_excluded = tokens_excluded
end
def get_ivar_tokens
return @tokens - @tokens_excluded
end
def generate(generationType)
generated = ""
case generationType
when :ivarBlock
tokens = self.get_ivar_tokens
return generated if tokens.empty?
generated << "// ivars for properties:\n"
tokens.each do |synthesizeLine|
generated << synthesizeLine[2] << " " << synthesizeLine[0] << "\n"
end
when :propertyDeclarationBlock
@tokens.each do |synthesizeLine|
generated << '@property' << synthesizeLine[1] << " " <<
synthesizeLine[2] << " " << synthesizeLine[0] << "\n"
end
end
return generated
end
end
class MethodPropertiesProcessor < PropertiesProcessor
def self.parse(str)
parsed = str.scan(/-\s*\((.*?)\)\s*(\w+)\s*\{?\s#{SingleLineAnnotationSymbol}\s*property(\(\w+\))/)
reorderedLines = []
parsed.each do |line|
type, name, attrs = line[0..2]
reorderedLines << [name + ";", attrs, type]
end
return self.new(reorderedLines, nil)
end
end