Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added code to remove default arguments from overridden function definition #89

Merged
merged 3 commits into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Assets/Tests/KotlinTokenizer/overrideargs.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

class Test {

override fun dostuff(x: Int) {}

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

func otherMethod(x: Int = 5) {
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great! this is the kind of tests useful. I am going to add a second method with default arguments not overriden to check they are not removed in that case

}

45 changes: 40 additions & 5 deletions Sources/SwiftKotlinFramework/KotlinTokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class KotlinTokenizer: SwiftTokenizer {
.replacing({ $0.value == "let"},
with: [constant.newToken(.keyword, "val")])
}

open override func tokenize(_ declaration: FunctionDeclaration) -> [Token] {
let attrsTokens = tokenize(declaration.attributes, node: declaration)
let modifierTokens = declaration.modifiers.map { tokenize($0, node: declaration) }
Expand All @@ -35,15 +35,21 @@ public class KotlinTokenizer: SwiftTokenizer {
genericParameterClauseTokens
].joined(token: declaration.newToken(.space, " "))

let signatureTokens = tokenize(declaration.signature, node: declaration)
var signatureTokens = tokenize(declaration.signature, node: declaration)
let bodyTokens = declaration.body.map(tokenize) ?? []

return [

if declaration.isOverride {
// overridden methods can't have default args in kotlin:
signatureTokens = removeDefaultArgsFromParameters(tokens:signatureTokens)
}
let tokens = [
headTokens,
[declaration.newToken(.identifier, declaration.name)] + signatureTokens,
bodyTokens
].joined(token: declaration.newToken(.space, " "))
.prefix(with: declaration.newToken(.linebreak, "\n"))

return tokens
}

open override func tokenize(_ parameter: FunctionSignature.Parameter, node: ASTNode) -> [Token] {
Expand Down Expand Up @@ -1071,10 +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
}

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