Class: Csharp2Swift
- Inherits:
-
Object
- Object
- Csharp2Swift
- Defined in:
- lib/csharp_2_swift.rb
Instance Method Summary collapse
- #constructors_to_inits(content) ⇒ Object
- #convert_bool_type(content) ⇒ Object
- #convert_const_field(content) ⇒ Object
- #convert_debug_assert(content) ⇒ Object
- #convert_double_type(content) ⇒ Object
- #convert_field(content) ⇒ Object
- #convert_float_type(content) ⇒ Object
- #convert_if(content) ⇒ Object
- #convert_int_type(content) ⇒ Object
- #convert_list_array_type(content) ⇒ Object
- #convert_list_list_type(content) ⇒ Object
- #convert_list_type(content) ⇒ Object
- #convert_locals(content) ⇒ Object
- #convert_method_decl_to_func_decl(content) ⇒ Object
- #convert_next_line_else(content) ⇒ Object
- #convert_property(content) ⇒ Object
- #convert_simple_range_for_loop(content) ⇒ Object
- #convert_string_type(content) ⇒ Object
- #convert_this_to_self(content) ⇒ Object
- #error(msg) ⇒ Object
- #execute(args) ⇒ Object
-
#initialize ⇒ Csharp2Swift
constructor
A new instance of Csharp2Swift.
- #insert_import(content) ⇒ Object
- #join_open_brace_to_last_line(content) ⇒ Object
- #parse(args) ⇒ Object
- #read_file(filename) ⇒ Object
- #remove_endregion(content) ⇒ Object
- #remove_eol_semicolons(content) ⇒ Object
- #remove_get_set(content) ⇒ Object
- #remove_namespace(content) ⇒ Object
- #remove_namespace_using(content) ⇒ Object
- #remove_new(content) ⇒ Object
- #remove_region(content) ⇒ Object
- #swap_args(arg_string) ⇒ Object
- #write_file(filename, content) ⇒ Object
Constructor Details
#initialize ⇒ Csharp2Swift
Returns a new instance of Csharp2Swift.
10 11 12 13 |
# File 'lib/csharp_2_swift.rb', line 10 def initialize @renamed_vars = {} @renamed_methods = {} end |
Instance Method Details
#constructors_to_inits(content) ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/csharp_2_swift.rb', line 269 def constructors_to_inits(content) re = /(?:(?:public|internal|private) +|)class +(\w+)/ m = re.match(content) while m != nil do content.gsub!(Regexp.new('(?:(?:public|internal) +|)' + m.captures[0] + " *\\("), 'init(') m = re.match(content, m.end(0)) end content.gsub!(/init\((.*)\)/) { |m| 'init(' + swap_args($1) + ')' } end |
#convert_bool_type(content) ⇒ Object
167 168 169 |
# File 'lib/csharp_2_swift.rb', line 167 def convert_bool_type(content) content.gsub!(/\bbool\b/, 'Bool') end |
#convert_const_field(content) ⇒ Object
241 242 243 244 245 246 247 248 |
# File 'lib/csharp_2_swift.rb', line 241 def convert_const_field(content) content.gsub!(/(^ *)(?:public|private|internal) +const +(.+?) +(.+?)( *= *.*?|)$/) { |m| v = $3 nv = v.lower_camelcase @renamed_vars[v] = nv $1 + 'let ' + nv + ': ' + $2 + $4 } end |
#convert_debug_assert(content) ⇒ Object
191 192 193 |
# File 'lib/csharp_2_swift.rb', line 191 def convert_debug_assert(content) content.gsub!(/Debug\.Assert\(/, 'assert(') end |
#convert_double_type(content) ⇒ Object
175 176 177 |
# File 'lib/csharp_2_swift.rb', line 175 def convert_double_type(content) content.gsub!(/\bdouble\b/, 'Double') end |
#convert_field(content) ⇒ Object
250 251 252 253 254 |
# File 'lib/csharp_2_swift.rb', line 250 def convert_field(content) content.gsub!(/(^ *)(?:public|private|internal) +(\w+) +(\w+)( *= *.*?|)$/) { |m| $1 + 'private var ' + $3 + ': ' + $2 + $4 } end |
#convert_float_type(content) ⇒ Object
171 172 173 |
# File 'lib/csharp_2_swift.rb', line 171 def convert_float_type(content) content.gsub!(/\bfloat\b/, 'Float') end |
#convert_if(content) ⇒ Object
300 301 302 303 304 305 306 |
# File 'lib/csharp_2_swift.rb', line 300 def convert_if(content) content.gsub!(/if *\((.*)\) +\{/, 'if \\1 {') content.gsub!(/if *\((.*?)\)\n( +)(.*?)\n/m) { |m| s = $2.length > 4 ? $2[0...-4] : s 'if ' + $1 + " {\n" + $2 + $3 + "\n" + s + "}\n" } end |
#convert_int_type(content) ⇒ Object
159 160 161 |
# File 'lib/csharp_2_swift.rb', line 159 def convert_int_type(content) content.gsub!(/\bint\b/, 'Int') end |
#convert_list_array_type(content) ⇒ Object
187 188 189 |
# File 'lib/csharp_2_swift.rb', line 187 def convert_list_array_type(content) content.gsub!(/(?:List|IList)<(\w+)>\[\]/, '[[\\1]]') end |
#convert_list_list_type(content) ⇒ Object
183 184 185 |
# File 'lib/csharp_2_swift.rb', line 183 def convert_list_list_type(content) content.gsub!(/(?:List|IList)<(?:List|IList)<(\w+)>>/, '[[\\1]]') end |
#convert_list_type(content) ⇒ Object
179 180 181 |
# File 'lib/csharp_2_swift.rb', line 179 def convert_list_type(content) content.gsub!(/(?:List|IList)<(\w+)>/, '[\\1]') end |
#convert_locals(content) ⇒ Object
296 297 298 |
# File 'lib/csharp_2_swift.rb', line 296 def convert_locals(content) content.gsub!(/^( *)(?!return|import)([A-Za-z0-9_\[\]<>]+) +(\w+)(?:( *= *.+)|)$/, '\\1let \\3\\4') end |
#convert_method_decl_to_func_decl(content) ⇒ Object
282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/csharp_2_swift.rb', line 282 def convert_method_decl_to_func_decl(content) # TODO: Override should be captured and re-inserted content.gsub!(/(?:(?:public|internal|private) +)(?:override +|)(.+) +(.*)\((.*)\) *\{/) { |m| f = $2 nf = f.lower_camelcase @renamed_methods[f] = nf if $1 == "void" 'func ' + nf + '(' + swap_args($3) + ') {' else 'func ' + nf + '(' + swap_args($3) + ') -> ' + $1 + ' {' end } end |
#convert_next_line_else(content) ⇒ Object
308 309 310 |
# File 'lib/csharp_2_swift.rb', line 308 def convert_next_line_else(content) content.gsub!(/\}\n +else \{/m, '} else {') end |
#convert_property(content) ⇒ Object
256 257 258 259 260 261 262 263 |
# File 'lib/csharp_2_swift.rb', line 256 def convert_property(content) content.gsub!(/(^ *)(?:public|private|internal) +(?!class)([A-Za-z0-9_\[\]<>]+) +(\w+)(?: *\{)/) { |m| v = $3 nv = v.lower_camelcase @renamed_vars[v] = nv $1 + 'var ' + nv + ': ' + $2 + ' {' } end |
#convert_simple_range_for_loop(content) ⇒ Object
312 313 314 315 |
# File 'lib/csharp_2_swift.rb', line 312 def convert_simple_range_for_loop(content) content.gsub!(/for \(.+ +(\w+) = (.+); \1 < (.*); \1\+\+\)/, 'for \\1 in \\2..<\\3') content.gsub!(/for \(.+ +(\w+) = (.+); \1 >= (.*); \1\-\-\)/, 'for \\1 in (\\3...\\2).reverse()') end |
#convert_string_type(content) ⇒ Object
163 164 165 |
# File 'lib/csharp_2_swift.rb', line 163 def convert_string_type(content) content.gsub!(/\bstring\b/, 'Int') end |
#convert_this_to_self(content) ⇒ Object
143 144 145 |
# File 'lib/csharp_2_swift.rb', line 143 def convert_this_to_self(content) content.gsub!(/this\./, 'self.') end |
#error(msg) ⇒ Object
336 337 338 |
# File 'lib/csharp_2_swift.rb', line 336 def error(msg) STDERR.puts "error: #{msg}".red end |
#execute(args) ⇒ Object
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 |
# File 'lib/csharp_2_swift.rb', line 60 def execute(args) = self.parse(args) if !File.exist?(.input_filename) error "File #{.input_filename} does not exist" exit end if .output_filename.length == 0 error "An output file must be specified." end .output_filename = File.(.output_filename) content = read_file(.input_filename) # Things that clean up the code and make other regex's easier remove_eol_semicolons(content) join_open_brace_to_last_line(content) remove_region(content) remove_endregion(content) remove_namespace_using(content) convert_this_to_self(content) convert_int_type(content) convert_string_type(content) convert_bool_type(content) convert_float_type(content) convert_double_type(content) convert_list_list_type(content) convert_list_array_type(content) convert_list_type(content) convert_debug_assert(content) remove_new(content) insert_import(content) # Slightly more complicated stuff remove_namespace(content) convert_property(content) remove_get_set(content) convert_const_field(content) convert_field(content) constructors_to_inits(content) convert_method_decl_to_func_decl(content) convert_locals(content) convert_if(content) convert_next_line_else(content) # Optional stuff convert_simple_range_for_loop(content) if .convert_simple_for_loops # Global search/replace @renamed_vars.each { |v, nv| content.gsub!(Regexp.new("\\." + v + "\\b"), '.' + nv) } @renamed_methods.each { |m, nm| content.gsub!(Regexp.new('\\b' + m + '\\('), nm + '(') } write_file(.output_filename, content) puts "\"#{.input_filename}\" -> \"#{.output_filename}\"" @renamed_vars.each {|k,v| puts k + ' -> ' + v} @renamed_methods.each {|k,v| puts k + '() -> ' + v + '()'} end |
#insert_import(content) ⇒ Object
199 200 201 |
# File 'lib/csharp_2_swift.rb', line 199 def insert_import(content) content.insert(0, "import Foundation\n") end |
#join_open_brace_to_last_line(content) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/csharp_2_swift.rb', line 129 def join_open_brace_to_last_line(content) re = / *\{$/m m = re.match(content) s = ' {' while m != nil do offset = m.offset(0) start = offset[0] content.slice!(offset[0]..offset[1]) content.insert(start - 1, s) m = re.match(content, start - 1 + s.length) end end |
#parse(args) ⇒ Object
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 |
# File 'lib/csharp_2_swift.rb', line 15 def parse(args) = OpenStruct.new .output_filename = '' .input_filename = nil .convert_simple_for_loops = false opt_parser = OptionParser.new do |opts| opts. = %Q(Roughly Convert C# to Swift. Version #{$VERSION} Copyright (c) John Lyon-Smith, 2015. Usage: #{File.basename(__FILE__)} [options] FILE ) opts.separator %Q(Description: This tool does a rough conversion of C# source code to Swift. The goal of the tool is to do most of the easy stuff that simply requires a lot of typing effort, and allow you to concentrate on the more difficult aspects of the conversion, such as library and framework usage. ) opts.separator %Q(Options: ) opts.on("-o", "--output FILE", String, "The output file. Default is the same as the input file.") do |file| .output_filename = File.(file) end opts.on("--convert_simple_for_loops", "Convert simple range based for loops, e.g. for(int i = 0; i < 10; i++) { }", "to range based loops of the form for i in 0..<10 { }") do |convert| .convert_simple_for_loops = convert end opts.on_tail("-?", "--help", "Show this message") do puts opts exit end end opt_parser.parse!(args) .input_filename = args.pop if .input_filename == nil error 'Need to specify a file to process' exit end .input_filename = File.(.input_filename) end |
#read_file(filename) ⇒ Object
326 327 328 329 330 |
# File 'lib/csharp_2_swift.rb', line 326 def read_file(filename) content = nil File.open(filename, 'rb') { |f| content = f.read() } content end |
#remove_endregion(content) ⇒ Object
151 152 153 |
# File 'lib/csharp_2_swift.rb', line 151 def remove_endregion(content) content.gsub!(/ *#endregion.*\n/, '') end |
#remove_eol_semicolons(content) ⇒ Object
125 126 127 |
# File 'lib/csharp_2_swift.rb', line 125 def remove_eol_semicolons(content) content.gsub!(/; *$/m, '') end |
#remove_get_set(content) ⇒ Object
265 266 267 |
# File 'lib/csharp_2_swift.rb', line 265 def remove_get_set(content) content.gsub!(/{ *get; *set; *}$/, '') end |
#remove_namespace(content) ⇒ Object
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 |
# File 'lib/csharp_2_swift.rb', line 203 def remove_namespace(content) re = / *namespace +.+ *\{$/ m = re.match(content) if m == nil return end i = m.end(0) n = 1 bol = (content[i] == "\n") while i < content.length do c = content[i] if bol and c == " " and i + 3 < content.length content.slice!(i..(i + 3)) c = content[i] end case c when "{" n += 1 when "}" n -= 1 if n == 0 content.slice!(i) # Take out the end curly content.slice!(m.begin(0)..m.end(0)) # Take out the original namespace break end when "\n" bol = true else bol = false end i += 1 end end |
#remove_namespace_using(content) ⇒ Object
155 156 157 |
# File 'lib/csharp_2_swift.rb', line 155 def remove_namespace_using(content) content.gsub!(/ *using (?!\().*\n/, '') end |
#remove_new(content) ⇒ Object
195 196 197 |
# File 'lib/csharp_2_swift.rb', line 195 def remove_new(content) content.gsub!(/new /, '') end |
#remove_region(content) ⇒ Object
147 148 149 |
# File 'lib/csharp_2_swift.rb', line 147 def remove_region(content) content.gsub!(/ *#region.*\n/, '') end |
#swap_args(arg_string) ⇒ Object
317 318 319 320 321 322 323 324 |
# File 'lib/csharp_2_swift.rb', line 317 def swap_args(arg_string) args = arg_string.split(/, */) args.collect! { |arg| a = arg.split(' ') a[1] + ': ' + a[0] } args.join(', ') end |
#write_file(filename, content) ⇒ Object
332 333 334 |
# File 'lib/csharp_2_swift.rb', line 332 def write_file(filename, content) File.open(filename, 'w') { |f| f.write(content) } end |