Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python 2.7のスクリプト。正規表現で強引に Objective-C を Swift に書き換えます。当然そのままでは使えないのでコピペしたり適宜修正が必要です。使い方: objc_to_swift.py ヘッダファイルへのパス 出力先
# encoding: utf-8
__author__ = 'hidashun'
import re
def replace_with_pattern(pattern, replace, text):
pattern = re.compile(pattern, flags=re.MULTILINE | re.VERBOSE)
return pattern.sub(replace, text)
def convert(text):
FIND_AND_REPLACE = '''
YES,true
NO,false
NSUInteger,Int *
NSInteger,Int *
BOOL,Bool *
(id),(AnyObject *)
void,Void
#pragma mark,// MARK:
@end,}
__PRETTY_FUNCTION__,__FUNCTION__
%s,%@
'''
for line in FIND_AND_REPLACE.strip().split('\n'):
find, replace = line.split(',')
text = text.replace(find, replace)
text = replace_with_pattern(r''';(\s//.*)$''', r'\1', text)
text = replace_with_pattern(r''';$''', r'', text)
text = replace_with_pattern(r'''
(\d+
\.
\d)
f
''', r'\1', text)
text = replace_with_pattern(r'''
\[
NSString
\s
stringWithFormat:
([^\[\]]+)
\]
''', r'NSString(format:\1)', text)
text = replace_with_pattern(r'''
([\[\(])
\(
\w+
\s\*
\)
''', r'\1', text)
text = replace_with_pattern(r'''
\[
\[
(\w+)
\s
alloc
\]
\s
init
\]
''', r'\1()', text)
text = replace_with_pattern(r'''
\[
\[
(\w+)
\s
alloc
\]
\s
initWith
([^\]]+)
\]
''', r'\1(\2)', text)
text = replace_with_pattern(r'''
\[
([^\]\[]+)
\s
([^ :]+)
\]
''', r'\1.\2()', text)
text = replace_with_pattern(r'''
\[
([^ \]\[]+)
\s
([^ :]+)
\]
''', r'\1.\2()', text)
text = replace_with_pattern(r'''
@"
([^"]*)
"
''', r'"\1"', text)
text = replace_with_pattern(r'''
@
\[
([^\[\]]*)
\]
''', r'[\1]', text)
text = replace_with_pattern(r'''
@
\{
([^{}]*)
\}
''', r'[\1]', text)
text = replace_with_pattern(r'''
@selector\(([\w:"]+)\)
''', r'"\1"', text)
text = replace_with_pattern(r'''
@implementation
\s
(\w+)
''', r'class \1 {', text)
text = replace_with_pattern(r'''
\[
([^ \[\]]+)
\s([^ :]+)
:
([^\[\]]+)
\]
''', r'\1.\2(\3)', text)
text = replace_with_pattern(r'''
\[
([^ \[\]]+)
\s([^ :]+)
:
(.+)
\]$
''', r'\1.\2(\3)', text)
text = replace_with_pattern(r'''
(if|for) \s \( (.+) \)
''', r'\1 \2', text)
text = replace_with_pattern(r'''
^
(-|\+)
(.*)$
\s*
\{
''', r'\1\2 {', text)
text = replace_with_pattern(r'''
\+
\s*
\(
(\w+) (?:\s\*)?
\)
([^:{}]*)
:
([^{}]*)
\s+
\{
\n
''', r'class func \2(\3) -> \1 {\n', text)
text = replace_with_pattern(r'''
\+
\s*
\(
(\w+) (?:\s\*)?
\)
([^:{]*)
\s+
\{
''', r'class func \2() -> \1 {', text)
text = replace_with_pattern(r'''
-
\s*
\(
(\w+) (?:\s\*)?
\)
([^:{}]*)
:
([^{}]*)
\s+
\{
\n
''', r'func \2(\3) -> \1 {\n', text)
text = replace_with_pattern(r'''
-
\s*
\(
(\w+) (?:\s\*)?
\)
([^:{]*)
\s+
\{
''', r'func \2() -> \1 {', text)
text = replace_with_pattern(r'''
^\#import.*\n
''', r'', text)
text = replace_with_pattern(r'''
\(
(\w+)
(?:\s\*)?
\)
(\w+)
''', r'\2: \1', text)
text = replace_with_pattern(r'''
\(
\(
(\w+)
\s
\*
\)
([^)]+)
\)
''', r'\2', text)
text = replace_with_pattern(r'''
([^ ]+)
\s*
\*
\s*
([^ ]+)
\s
=
''', r'let \2: \1 =', text)
text = replace_with_pattern(r'''
\s->\sVoid\s\{
''', r' {', text)
text = replace_with_pattern(r'''
func
\s
([^{]+)
\s
->
\s
IBAction
\s
\{
''', r'@IBAction func \1 {', text)
text = replace_with_pattern(r'''(?<!let\ )
(["\w]+)
\s
([\w:]+:)
''', r'\1, \2', text)
text = replace_with_pattern(r'''(?<!let\ )
let,\s(\w+):
''', r'let \1:', text) # Dirty
text = replace_with_pattern(r'''
\(
([^(]+)
\(\^\)
\(
([^)]+)
\)
\)
(\w+)
''', r'\3: (\2) -> \1', text)
text = replace_with_pattern(r'''
([\w]+):
(\1:)
''', r'\1:', text)
text = replace_with_pattern(r'''
([\w]+):
([\w]+:)
''', r'\1 \2', text)
text = replace_with_pattern(r'''
@interface
\s+
(.+)
:
(.+)$
''', r'class \1:\2 {', text)
text = replace_with_pattern(r'''
@interface
\s+
(\w+)
\s+
\(
(\w*)
\)
\s+
<([^>]*)>
''', r'class \1: \2\3 {', text)
text = text.replace('@end', '}')
text = replace_with_pattern(r'''
@property
\s
(\([^)]+\)\s)?
IBOutlet
''', r'@IBOutlet var', text)
text = replace_with_pattern(r'''
@property
\s
(\([^)]+\)\s)?
''', r'var ', text)
text = replace_with_pattern(r'''
(\w+)
\s*
\*
\s*
(\w+)
''', r'\2: \1', text)
text = replace_with_pattern(r'''
\.
isEqualToString
\(
([^\)]+)
\)
''', r' == \1', text)
text = replace_with_pattern(r'''
\.
(?:objectAtIndex|objectForKey)
\(
([^\)]+)
\)
''', r'[\1]', text)
text = replace_with_pattern(r'''
\^
(
\(
[^\)]+
\)
)
\s*
\{
''', r'{ \1 in', text)
text = replace_with_pattern(r'''\s\*''', r'', text)
return text
if __name__ == "__main__":
import sys
import os
objc_source_path = sys.argv[1]
swift_source_path = sys.argv[2]
file_dir = os.path.dirname(objc_source_path)
base = os.path.basename(objc_source_path)
filename = os.path.splitext(base)[0]
file_paths = [
os.path.join(file_dir, filename + '.h'),
os.path.join(file_dir, filename + '.m'),
]
lines = []
for file_path in file_paths:
with open(file_path, 'r') as file:
lines += file.readlines()
# print text
text = "\n".join(map(lambda line: line.rstrip(), lines)).decode('utf-8')
text = convert(text)
with open(swift_source_path, 'w') as file:
file.write(text.encode('utf-8'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.