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
|
# File 'lib/swift_republic.rb', line 66
def make_models_public(source_file, destination_file, reserved_keywords = [])
lines = []
bracket_stack = Array.new struct_stack = Array.new property_stack = Array.new
protocol_stack = Array.new
can_update_property_scope = false can_update_function_scope = true
file_name = File.basename(source_file)
p "Parsing #{file_name} to output --> #{destination_file}"
dirname = File.dirname(destination_file)
unless File.directory?(dirname)
FileUtils.mkdir_p(dirname)
end
t_file = File.new(destination_file, "w")
File.open(source_file) { |file|
lines = file.readlines
}
lines.each_with_index { |line, source_line_number|
indent_offset = line =~ /\S/
if reserved_keywords.any? { |keyword| line.include? keyword }
t_file.close
p "Could NOT parse #{file_name}. Dependencies/restricted keywords detected in line number #{source_line_number}: #{line}"
File.delete(destination_file)
break
elsif (line.include? "private") || (line.include? "fileprivate") || (line.include? "public")
if (line.include? "\{")
if not (line.include? "\}")
bracket_stack.push "other"
end
end
t_file.puts "#{line}"
elsif line.strip.start_with?("\/\/")
t_file.puts "#{line}"
elsif (line.include? "struct") && (line.include? "\{")
line["struct"] = "public struct"
t_file.puts "#{line}"
bracket_stack.push "#{line}"
struct_stack.push "#{line}"
elsif (line.include? "enum") && (line.include? "\{")
line["enum"] = "public enum"
t_file.puts "#{line}"
bracket_stack.push line
elsif (line.include? "protocol ") && (line.include? "\{")
line["protocol"] = "public protocol"
t_file.puts "#{line}"
bracket_stack.push line
protocol_stack.push line
elsif line.include? " func "
if can_update_function_scope line["func"] = "public func"
end
if line.include? "\{"
bracket_stack.push line
end
t_file.puts "#{line}"
elsif can_update_property_scope && (line.strip.start_with?("let"))
property_details = Property.new(struct_stack.last, line)
property_stack.push property_details
line.insert indent_offset, "public "
t_file.puts "#{line}"
elsif can_update_property_scope && (line.strip.start_with?("var"))
property_details = Property.new(struct_stack.last, line)
if line.strip.end_with?("\{")
bracket_stack.push line
else
property_stack.push property_details
end
line.insert indent_offset, "public "
t_file.puts "#{line}"
elsif can_update_property_scope && (line.strip.start_with?("static"))
line.insert indent_offset, "public "
t_file.puts "#{line}"
if (line.include? "\{")
if not (line.include? "\}")
bracket_stack.push "other"
end
end
elsif line.strip.start_with?("init(")
line["init("] = "public init("
if (line.include? "\{")
if not (line.include? "\}")
bracket_stack.push "other"
end
end
t_file.puts "#{line}"
elsif (line.include? "\{") if not (line.include? "\}")
bracket_stack.push "other"
end
t_file.puts "#{line}"
elsif (line.strip.length == 1) && (line.include? "\}")
top_object = bracket_stack.pop
if can_update_property_scope && (top_object.include? "struct")
latest_struct = struct_stack.pop
properties_array = Array.new
while (property_stack.size > 0) && (property_stack.last.associated_struct == latest_struct)
current_property = property_stack.pop
properties_array.push current_property
end
if properties_array.size > 0
struct_initializer = create_initializer(properties_array.reverse!)
t_file.puts struct_initializer
end
t_file.puts "#{line}"
elsif top_object.include? "protocol"
protocol_stack.pop
t_file.puts "#{line}"
else
t_file.puts "#{line}"
end
elsif (line.include? "\}")
bracket_stack.pop
t_file.puts "#{line} <<--Please fix formatting there, and run again."
else
t_file.puts "#{line}"
end
can_update_property_scope = (struct_stack.size > 0) && ((bracket_stack.last.include? "struct") || (bracket_stack.last.include? "enum")) can_update_function_scope = (protocol_stack.size == 0) }
t_file.close
end
|