Skip to content

Commit

Permalink
Merge pull request #109 from angelolloqui/fix/various
Browse files Browse the repository at this point in the history
Fix/various
  • Loading branch information
angelolloqui authored Mar 21, 2020
2 parents 54093fc + 3a8ccaf commit def9047
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: objective-c
osx_image: xcode11.2
osx_image: xcode11.3
install:
- swift package update
- swift package generate-xcodeproj --enable-code-coverage
Expand Down
2 changes: 1 addition & 1 deletion Assets/Tests/KotlinTokenizer/enums.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ enum CompassPoint {
case west
}

private enum Planet {
private enum Planet: Equatable {
case mercury, venus, earth
case mars, jupiter, saturn, uranus, neptune
}
Expand Down
1 change: 1 addition & 0 deletions Assets/Tests/KotlinTokenizer/foundation_types.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ var map: Promise<Map<Int, String>>? = null
var map: Map<Int, Promise<Map<String, String>>>
var map = mapOf(1 to "a", 2 to "b")
var map = mapOf<String , String>()
method(value = listOf("value1", "value"))
1 change: 1 addition & 0 deletions Assets/Tests/KotlinTokenizer/foundation_types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ var map: Promise<[Int: String]>?
var map: [Int: Promise<[String: String]>]
var map = [1: "a", 2: "b"]
var map = [String: String]()
method(value: ["value1", "value"])
8 changes: 8 additions & 0 deletions Assets/Tests/KotlinTokenizer/structs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ data class Person(

fun eat() {}
}

data class User(
var id: Int? = 0,
var name: String? = null,
var content: Content): Codable {

data class Content(val text: String) {}
}
11 changes: 10 additions & 1 deletion Assets/Tests/KotlinTokenizer/structs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@ struct Data {
var text: String
}

struct Person {
struct Person: Equatable {
let name: String
let surname: String
var age: Int

func eat() {}
}

struct User: Codable {
struct Content {
let text: String
}
var id: Int? = 0
var name: String?
var content: Content
}
2 changes: 1 addition & 1 deletion Sources/SwiftKotlinApp/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>0.2.2</string>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftKotlinCommandLine/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let kotlinTokenizer = KotlinTokenizer(
CommentsAdditionTransformPlugin()
]
)
let version = "0.2.1"
let version = "0.2.3"
let arguments = [
"output",
"help",
Expand All @@ -26,7 +26,7 @@ let arguments = [

func showHelp() {
print("swiftkotlin, version \(version)")
print("copyright (c) 2019 Angel G. Olloqui")
print("copyright (c) 2020 Angel G. Olloqui")
print("")
print("usage: swiftkotlin [<file>] [--output path]")
print("")
Expand Down
38 changes: 23 additions & 15 deletions Sources/SwiftKotlinFramework/KotlinTokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public class KotlinTokenizer: SwiftTokenizer {
accessLevelModifier: declaration.accessLevelModifier,
name: declaration.name,
genericParameterClause: declaration.genericParameterClause,
typeInheritanceClause: declaration.typeInheritanceClause,
typeInheritanceClause: nil,
genericWhereClause: declaration.genericWhereClause,
members: otherMembers)
newStruct.setSourceRange(declaration.sourceRange)
Expand All @@ -136,13 +136,6 @@ public class KotlinTokenizer: SwiftTokenizer {
.replacing({ $0.value == "struct"},
with: [declaration.newToken(.keyword, "data class")])

if !staticMembers.isEmpty, let bodyStart = tokens.firstIndex(where: { $0.value == "{"}) {
let companionTokens = indent(tokenizeCompanion(staticMembers, node: declaration))
.prefix(with: declaration.newToken(.linebreak, "\n"))
.suffix(with: declaration.newToken(.linebreak, "\n"))
tokens.insert(contentsOf: companionTokens, at: bodyStart + 1)
}

if !declarationMembers.isEmpty, let bodyStart = tokens.firstIndex(where: { $0.value == "{"}) {
let linebreak = declaration.newToken(.linebreak, "\n")
let declarationTokens: [Token]
Expand All @@ -166,6 +159,21 @@ public class KotlinTokenizer: SwiftTokenizer {
at: bodyStart - 1)
}

if let typeInheritanceList = declaration.typeInheritanceClause?.typeInheritanceList.nonEquatable,
typeInheritanceList.isEmpty == false,
let bodyStart = tokens.firstIndex(where: { $0.value == "{"}) {
let clause = TypeInheritanceClause(classRequirement: false, typeInheritanceList: typeInheritanceList)
let inheritanceTokens = tokenize(clause, node: declaration)
tokens.insert(contentsOf: inheritanceTokens, at: bodyStart - 1)
}

if !staticMembers.isEmpty, let bodyStart = tokens.firstIndex(where: { $0.value == "{"}) {
let companionTokens = indent(tokenizeCompanion(staticMembers, node: declaration))
.prefix(with: declaration.newToken(.linebreak, "\n"))
.suffix(with: declaration.newToken(.linebreak, "\n"))
tokens.insert(contentsOf: companionTokens, at: bodyStart + 1)
}

return tokens
}

Expand Down Expand Up @@ -431,8 +439,9 @@ public class KotlinTokenizer: SwiftTokenizer {

// Simple enums (no tuple values)
if !simpleCases.contains(where: { $0.tuple != nil }) {
if declaration.typeInheritanceClause != nil {
return tokenizeSimpleValueEnum(declaration:declaration, simpleCases: simpleCases)
let typeInheritanceList = declaration.typeInheritanceClause?.typeInheritanceList.nonEquatable
if typeInheritanceList?.isEmpty == false {
return tokenizeSimpleValueEnum(declaration: declaration, simpleCases: simpleCases)
} else {
return tokenizeNoValueEnum(declaration: declaration, simpleCases: simpleCases)
}
Expand Down Expand Up @@ -588,12 +597,11 @@ public class KotlinTokenizer: SwiftTokenizer {
case let .staticString(_, rawText):
return [expression.newToken(.string, conversionUnicodeString(rawText, node: expression))]
case .array(let exprs):
let isGenericTypeInfo = expression.lexicalParent is FunctionCallExpression
return
expression.newToken(.identifier, "listOf") +
expression.newToken(.startOfScope, isGenericTypeInfo ? "<" : "(") +
let isGenericTypeInfo = (expression.lexicalParent as? FunctionCallExpression)?.postfixExpression.textDescription.starts(with: "[") == true
return expression.newToken(.identifier, "listOf") +
expression.newToken(.startOfScope, isGenericTypeInfo ? "<" : "(") +
exprs.map { tokenize($0) }.joined(token: expression.newToken(.delimiter, ", ")) +
expression.newToken(.endOfScope, isGenericTypeInfo ? ">" : ")")
expression.newToken(.endOfScope, isGenericTypeInfo ? ">" : ")")
case .dictionary(let entries):
let isGenericTypeInfo = expression.lexicalParent is FunctionCallExpression
var entryTokens = entries.map { tokenize($0, node: expression) }.joined(token: expression.newToken(.delimiter, ", "))
Expand Down
7 changes: 7 additions & 0 deletions Sources/SwiftKotlinFramework/utils/AST+Operations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,10 @@ extension GuardStatement {
(bodyStatement is ReturnStatement || bodyStatement is ThrowStatement)
}
}

extension Collection where Iterator.Element == TypeIdentifier {
var nonEquatable: [TypeIdentifier] {
return filter { $0.names.contains { $0.name.textDescription != "Equatable" } }
}
}

2 changes: 2 additions & 0 deletions SwiftKotlinApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@
INFOPLIST_FILE = "$(SRCROOT)/Sources/SwiftKotlinApp/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.12;
MARKETING_VERSION = 0.2.3;
MTL_ENABLE_DEBUG_INFO = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.angelolloqui.SwiftKotlinApp;
PRODUCT_NAME = SwiftKotlin;
Expand Down Expand Up @@ -351,6 +352,7 @@
INFOPLIST_FILE = "$(SRCROOT)/Sources/SwiftKotlinApp/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.12;
MARKETING_VERSION = 0.2.3;
MTL_ENABLE_DEBUG_INFO = NO;
PRODUCT_BUNDLE_IDENTIFIER = com.angelolloqui.SwiftKotlinApp;
PRODUCT_NAME = SwiftKotlin;
Expand Down

0 comments on commit def9047

Please sign in to comment.