Skip to content

Commit

Permalink
#114 Added is keyword to enum cases with tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
angelolloqui committed Mar 29, 2020
1 parent b8b26a9 commit be4a00f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
6 changes: 3 additions & 3 deletions Assets/Tests/KotlinTokenizer/enums.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ sealed class AnimationLengthAdvanced {
when (this) {
short -> return 2
long -> return 5.0
custom -> return duration
is custom -> return duration
}
}

Expand All @@ -65,10 +65,10 @@ sealed class AnimationLengthAdvanced {
}
when (enumValue) {
resetPasswordSendEmail -> return (category: "ResetPassword", name: "sendEmail", label: null)
paymentSelectorOpen -> return (category: "PaymentSelector", name: "open", label: "${tenant.name} - ${option.duration}min")
is paymentSelectorOpen -> return (category: "PaymentSelector", name: "open", label: "${tenant.name} - ${option.duration}min")
}
when (exception) {
qrCode -> {
is qrCode -> {
val message = serverMessage
if (message != null) {
trackError(name = name, message = message)
Expand Down
33 changes: 19 additions & 14 deletions Sources/SwiftKotlinFramework/KotlinTokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -540,28 +540,33 @@ public class KotlinTokenizer: SwiftTokenizer {
}

open override func tokenize(_ statement: SwitchStatement.Case.Item, node: ASTNode) -> [Token] {
guard let enumCasePattern = statement.pattern as? EnumCasePattern else {
let expression = (statement.pattern as? ExpressionPattern)?.expression
let prefix = expression != nil && !(expression is LiteralExpression) ? [statement.newToken(.keyword, "in", node)] : []
return [
prefix,
super.tokenize(statement, node: node)
].joined(token: statement.newToken(.space, " ", node))
let prefix: [Token]
if let expression = (statement.pattern as? ExpressionPattern)?.expression {
prefix = !(expression is LiteralExpression) ? [statement.newToken(.keyword, "in", node)] : []
} else {
prefix = []
}
let patternWithoutTuple = EnumCasePattern(typeIdentifier: enumCasePattern.typeIdentifier, name: enumCasePattern.name, tuplePattern: nil)
return [
tokenize(patternWithoutTuple, node: node),
statement.whereExpression.map { _ in [statement.newToken(.keyword, "where", node)] } ?? [],
statement.whereExpression.map { tokenize($0) } ?? []
].joined(token: statement.newToken(.space, " ", node))
prefix,
super.tokenize(statement, node: node)
].joined(token: statement.newToken(.space, " ", node))
}

open override func tokenize(_ pattern: EnumCasePattern, node: ASTNode) -> [Token] {
var tokens = super.tokenize(pattern, node: node)
let patternWithoutTuple: EnumCasePattern
let prefix: [Token]
if pattern.tuplePattern != nil {
patternWithoutTuple = EnumCasePattern(typeIdentifier: pattern.typeIdentifier, name: pattern.name, tuplePattern: nil)
prefix = [pattern.newToken(.keyword, "is", node), pattern.newToken(.space, " ", node)]
} else {
patternWithoutTuple = pattern
prefix = []
}
var tokens = super.tokenize(patternWithoutTuple, node: node)
if tokens.first?.value == "." {
tokens.remove(at: 0)
}
return tokens
return prefix + tokens
}

open override func tokenize(_ statement: ForInStatement) -> [Token] {
Expand Down

0 comments on commit be4a00f

Please sign in to comment.