Class: SwiftTools::Csharp2Swift

Inherits:
Object
  • Object
show all
Defined in:
lib/csharp_2_swift.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCsharp2Swift

Returns a new instance of Csharp2Swift.



9
10
11
12
# File 'lib/csharp_2_swift.rb', line 9

def initialize
  @renamed_methods = {}
  @renamed_vars = {}
end

Instance Attribute Details

#renamed_methodsObject (readonly)

Returns the value of attribute renamed_methods.



7
8
9
# File 'lib/csharp_2_swift.rb', line 7

def renamed_methods
  @renamed_methods
end

#renamed_varsObject (readonly)

Returns the value of attribute renamed_vars.



6
7
8
# File 'lib/csharp_2_swift.rb', line 6

def renamed_vars
  @renamed_vars
end

Instance Method Details

#constructors_to_inits(content) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/csharp_2_swift.rb', line 204

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



102
103
104
# File 'lib/csharp_2_swift.rb', line 102

def convert_bool_type(content)
  content.gsub!(/\bbool\b/, 'Bool')
end

#convert_const_field(content) ⇒ Object



176
177
178
179
180
181
182
183
# File 'lib/csharp_2_swift.rb', line 176

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



126
127
128
# File 'lib/csharp_2_swift.rb', line 126

def convert_debug_assert(content)
  content.gsub!(/Debug\.Assert\(/, 'assert(')
end

#convert_double_type(content) ⇒ Object



110
111
112
# File 'lib/csharp_2_swift.rb', line 110

def convert_double_type(content)
  content.gsub!(/\bdouble\b/, 'Double')
end

#convert_field(content) ⇒ Object



185
186
187
188
189
# File 'lib/csharp_2_swift.rb', line 185

def convert_field(content)
  content.gsub!(/(^ *)(?:public|private|internal) +(\w+) +(\w+)( *= *.*?|)$/) { |m|
    $1 + 'private var ' + $3 + ': ' + $2 + $4
  }
end

#convert_float_type(content) ⇒ Object



106
107
108
# File 'lib/csharp_2_swift.rb', line 106

def convert_float_type(content)
  content.gsub!(/\bfloat\b/, 'Float')
end

#convert_if(content) ⇒ Object



235
236
237
238
239
240
241
# File 'lib/csharp_2_swift.rb', line 235

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



94
95
96
# File 'lib/csharp_2_swift.rb', line 94

def convert_int_type(content)
  content.gsub!(/\bint\b/, 'Int')
end

#convert_list_array_type(content) ⇒ Object



122
123
124
# File 'lib/csharp_2_swift.rb', line 122

def convert_list_array_type(content)
  content.gsub!(/(?:List|IList)<(\w+)>\[\]/, '[[\\1]]')
end

#convert_list_list_type(content) ⇒ Object



118
119
120
# File 'lib/csharp_2_swift.rb', line 118

def convert_list_list_type(content)
  content.gsub!(/(?:List|IList)<(?:List|IList)<(\w+)>>/, '[[\\1]]')
end

#convert_list_type(content) ⇒ Object



114
115
116
# File 'lib/csharp_2_swift.rb', line 114

def convert_list_type(content)
  content.gsub!(/(?:List|IList)<(\w+)>/, '[\\1]')
end

#convert_locals(content) ⇒ Object



231
232
233
# File 'lib/csharp_2_swift.rb', line 231

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



217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/csharp_2_swift.rb', line 217

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



243
244
245
# File 'lib/csharp_2_swift.rb', line 243

def convert_next_line_else(content)
  content.gsub!(/\}\n +else \{/m, '} else {')
end

#convert_property(content) ⇒ Object



191
192
193
194
195
196
197
198
# File 'lib/csharp_2_swift.rb', line 191

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



247
248
249
250
# File 'lib/csharp_2_swift.rb', line 247

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



98
99
100
# File 'lib/csharp_2_swift.rb', line 98

def convert_string_type(content)
  content.gsub!(/\bstring\b/, 'Int')
end

#convert_this_to_self(content) ⇒ Object



78
79
80
# File 'lib/csharp_2_swift.rb', line 78

def convert_this_to_self(content)
  content.gsub!(/this\./, 'self.')
end

#error(msg) ⇒ Object



271
272
273
# File 'lib/csharp_2_swift.rb', line 271

def error(msg)
  STDERR.puts "error: #{msg}".red
end

#execute(cs_file, swift_file, options) ⇒ Object



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
# File 'lib/csharp_2_swift.rb', line 14

def execute(cs_file, swift_file, options)
  content = cs_file.read()

  # 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)
  convert_simple_range_for_loop(content)

  # 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 + '(')
  }

  swift_file.write(content)
end

#insert_import(content) ⇒ Object



134
135
136
# File 'lib/csharp_2_swift.rb', line 134

def insert_import(content)
  content.insert(0, "import Foundation\n")
end

#join_open_brace_to_last_line(content) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/csharp_2_swift.rb', line 64

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

#read_file(filename) ⇒ Object



261
262
263
264
265
# File 'lib/csharp_2_swift.rb', line 261

def read_file(filename)
  content = nil
  File.open(filename, 'rb') { |f| content = f.read() }
  content
end

#remove_endregion(content) ⇒ Object



86
87
88
# File 'lib/csharp_2_swift.rb', line 86

def remove_endregion(content)
  content.gsub!(/ *#endregion.*\n/, '')
end

#remove_eol_semicolons(content) ⇒ Object



60
61
62
# File 'lib/csharp_2_swift.rb', line 60

def remove_eol_semicolons(content)
  content.gsub!(/; *$/m, '')
end

#remove_get_set(content) ⇒ Object



200
201
202
# File 'lib/csharp_2_swift.rb', line 200

def remove_get_set(content)
  content.gsub!(/{ *get; *set; *}$/, '')
end

#remove_namespace(content) ⇒ Object



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
# File 'lib/csharp_2_swift.rb', line 138

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



90
91
92
# File 'lib/csharp_2_swift.rb', line 90

def remove_namespace_using(content)
  content.gsub!(/ *using (?!\().*\n/, '')
end

#remove_new(content) ⇒ Object



130
131
132
# File 'lib/csharp_2_swift.rb', line 130

def remove_new(content)
  content.gsub!(/new /, '')
end

#remove_region(content) ⇒ Object



82
83
84
# File 'lib/csharp_2_swift.rb', line 82

def remove_region(content)
  content.gsub!(/ *#region.*\n/, '')
end

#swap_args(arg_string) ⇒ Object



252
253
254
255
256
257
258
259
# File 'lib/csharp_2_swift.rb', line 252

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



267
268
269
# File 'lib/csharp_2_swift.rb', line 267

def write_file(filename, content)
  File.open(filename, 'w') { |f| f.write(content) }
end