Skip to content

Commit

Permalink
PR changes
Browse files Browse the repository at this point in the history
  • Loading branch information
angelolloqui committed Feb 7, 2019
1 parent 8f617aa commit 580b604
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 33 deletions.
2 changes: 2 additions & 0 deletions Assets/Tests/KotlinTokenizer/overrideargs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
class Test {

override fun dostuff(x: Int) {}

fun otherMethod(x: Int = 5) {}
}
5 changes: 4 additions & 1 deletion Assets/Tests/KotlinTokenizer/overrideargs.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
class Test {
override func dostuff(x:Int = 5) {
override func dostuff(x: Int = 5) {
}

func otherMethod(x: Int = 5) {
}
}

64 changes: 32 additions & 32 deletions Sources/SwiftKotlinFramework/KotlinTokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public class KotlinTokenizer: SwiftTokenizer {

var signatureTokens = tokenize(declaration.signature, node: declaration)
let bodyTokens = declaration.body.map(tokenize) ?? []
if modifierTokens.contains(where:{$0.value == "override" }) {

if declaration.isOverride {
// overridden methods can't have default args in kotlin:
signatureTokens = removeDefaultArgsFromParameters(tokens:signatureTokens)
}
Expand Down Expand Up @@ -1077,39 +1077,39 @@ public class KotlinTokenizer: SwiftTokenizer {
interpolatedString += remainingText
return [node.newToken(.string, interpolatedString)]
}

// function used to remove default arguments from override functions, since kotlin doesn't have them
private func removeDefaultArgsFromParameters(tokens:[Token]) -> [Token] {
var newTokens = [Token]()
var removing = false
var bracket = false
for t in tokens {
if removing && t.kind == .startOfScope && t.value == "(" {
bracket = true
}
if bracket && t.kind == .endOfScope && t.value == ")" {
bracket = false
removing = false
continue
}
if t.kind == .symbol && (t.value.contains("=")) {
removing = true
}
if t.kind == .delimiter && t.value.contains(",") {
removing = false
}
if !bracket && removing && t.kind == .endOfScope && t.value == ")" {
removing = false
}
if !removing {
newTokens.append(t)
}
}
return newTokens
}
}

public typealias InvertedConditionList = [InvertedCondition]
public struct InvertedCondition: ASTTokenizable {
public let condition: Condition
}

// function used to remove default arguments from override functions, since kotlin doesn't have them
private func removeDefaultArgsFromParameters(tokens:[Token]) -> [Token] {
var newTokens = [Token]()
var removing = false
var bracket = false
for t in tokens {
if removing && t.kind == .startOfScope && t.value == "(" {
bracket = true
}
if bracket && t.kind == .endOfScope && t.value == ")" {
bracket = false
removing = false
continue
}
if t.kind == .symbol && (t.value.contains("=")) {
removing = true
}
if t.kind == .delimiter && t.value.contains(",") {
removing = false
}
if !bracket && removing && t.kind == .endOfScope && t.value == ")" {
removing = false
}
if !removing {
newTokens.append(t)
}
}
return newTokens
}
14 changes: 14 additions & 0 deletions Sources/SwiftKotlinFramework/utils/AST+Operations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ extension FunctionDeclaration {
var isStatic: Bool {
return modifiers.isStatic
}
var isOverride: Bool {
return modifiers.isOverride
}
}

extension StructDeclaration.Member {
Expand Down Expand Up @@ -118,6 +121,10 @@ extension Collection where Iterator.Element == DeclarationModifier {
var isLazy: Bool {
return self.contains(where: { $0.isLazy })
}

var isOverride: Bool {
return self.contains(where: { $0.isOverride })
}
}

extension DeclarationModifier {
Expand All @@ -134,6 +141,13 @@ extension DeclarationModifier {
default: return false
}
}

var isOverride: Bool {
switch self {
case .override: return true
default: return false
}
}
}

extension SuperclassExpression {
Expand Down

0 comments on commit 580b604

Please sign in to comment.