-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
b4c24db
commit 8b94a81
Showing
10 changed files
with
126 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
Sources/SwiftKotlinFramework/plugins/FoundationMethodsTransformPlugin.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// | ||
// FoundationMethodsTransformPlugin.swift | ||
// SwiftKotlinPackageDescription | ||
// | ||
// Created by Angel Luis Garcia on 10/12/2017. | ||
// | ||
|
||
import Foundation | ||
import Transform | ||
import AST | ||
|
||
public class FoundationMethodsTransformPlugin: TokenTransformPlugin { | ||
public var name: String { | ||
return "Foundation transformations" | ||
} | ||
|
||
public var description: String { | ||
return "Transforms methods on lists, maps,... like `first`, `count`... to their Kotlin variant" | ||
} | ||
|
||
public init() {} | ||
|
||
public func transform(tokens: [Token], topDeclaration: TopLevelDeclaration) throws -> [Token] { | ||
var newTokens = [Token]() | ||
|
||
for token in tokens { | ||
if token.kind == .identifier, | ||
let memberExpression = token.origin as? ExplicitMemberExpression, | ||
case ExplicitMemberExpression.Kind.namedType(let expression, let identifier) = memberExpression.kind, | ||
let inferredType = inferTypeFor(expression: expression, topDeclaration: topDeclaration), | ||
let replace = memberStringMappings[inferredType]?[identifier] { | ||
newTokens.append(memberExpression.newToken(.identifier, replace)) | ||
} else if token.kind == .identifier, token.value == "fatalError", let origin = token.origin, let node = token.node { | ||
newTokens.append(origin.newToken(.keyword, "throw", node)) | ||
newTokens.append(origin.newToken(.space, " ", node)) | ||
newTokens.append(origin.newToken(.identifier, "Exception", node)) | ||
} else { | ||
newTokens.append(token) | ||
} | ||
} | ||
|
||
return newTokens | ||
} | ||
|
||
func inferTypeFor(expression: PostfixExpression, topDeclaration: TopLevelDeclaration) -> String? { | ||
// TODO: Right now there is no way to infer types. Will be fixed in future versions of AST | ||
return "List" | ||
} | ||
|
||
let memberStringMappings = [ | ||
"List": [ | ||
"first": "firstOrNull()", | ||
"last": "lastOrNull()", | ||
"count": "size", | ||
"isEmpty": "isEmpty()" | ||
], | ||
"String": [ | ||
"count": "length", | ||
"uppercased": "toUpperCase", | ||
"lowercased": "toLowerCase" | ||
] | ||
] | ||
|
||
// TODO: How to map regex expressions that affect multiple tokens? | ||
let memberRegexMappings = [ | ||
"List": [ | ||
"index(of: \\(.+))": "indexOf($1)", | ||
"append(\\(.+))": "add($1)", | ||
"remove(at: \\(.+))": "removeAt($1)", | ||
"sorted(by: \\(.+))": "sortedWith(comparator = Comparator($1))", | ||
"joined(separator: \\(.+))": "joinToString(separator = $1)" | ||
] | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
Tests/SwiftKotlinFrameworkTests/Tests/plugins/FoundationMethodsTransformPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
val list = listOf(1, 2, 3) | ||
list.firstOrNull() | ||
list.lastOrNull() | ||
list.size | ||
list.isEmpty() | ||
throw Exception("An error ${error} occurred") |
19 changes: 19 additions & 0 deletions
19
Tests/SwiftKotlinFrameworkTests/Tests/plugins/FoundationMethodsTransformPlugin.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
let list = [1, 2, 3] | ||
|
||
list.first | ||
list.last | ||
list.count | ||
list.isEmpty | ||
//list.index(of: a) --> list.indexOf(a) | ||
//list.append(element) --> list.add(element) | ||
//list.remove(at: index) --> list.removeAt(index) | ||
//list.sorted(by: lambda) --> sortedWith(comparator = Comparator(lambda)) | ||
//list.joined(separator: joiner) --> elements. joinToString(separator = joiner) | ||
|
||
//let string = "A string" | ||
//string.count --> string.length | ||
//string.uppercased() --> string.toUpperCase() | ||
//string.lowercased() --> string.toLowerCase() | ||
|
||
fatalError("An error \(error) occurred") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters