Skip to content

Commit

Permalink
Merge pull request #44 from raptorxcz/feature/swift_syntax
Browse files Browse the repository at this point in the history
Integrate dummy generator
  • Loading branch information
libec authored Sep 11, 2023
2 parents a91f0a9 + 4944747 commit 7fd6219
Show file tree
Hide file tree
Showing 17 changed files with 659 additions and 794 deletions.
19 changes: 19 additions & 0 deletions Rubicon/Sources/Rubicon/Generator/ArgumentGenerator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
protocol ArgumentGenerator {
func makeCode(from declaration: ArgumentDeclaration) -> String
}

final class ArgumentGeneratorImpl: ArgumentGenerator {
private let typeGenerator: TypeGenerator

init(typeGenerator: TypeGenerator) {
self.typeGenerator = typeGenerator
}

func makeCode(from declaration: ArgumentDeclaration) -> String {
if let label = declaration.label {
return "\(label) \(declaration.name): \(typeGenerator.makeArgumentCode(from: declaration.type))"
} else {
return "\(declaration.name): \(typeGenerator.makeArgumentCode(from: declaration.type))"
}
}
}
175 changes: 0 additions & 175 deletions Rubicon/Sources/Rubicon/Generator/CreateDummyInteractor.swift

This file was deleted.

69 changes: 69 additions & 0 deletions Rubicon/Sources/Rubicon/Generator/DummyGenerator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
final class DummyGenerator {
private let protocolGenerator: ProtocolGenerator
private let variableGenerator: VariableGenerator
private let functionGenerator: FunctionGenerator
private let accessLevel: AccessLevel
private let accessLevelGenerator: AccessLevelGenerator

init(
protocolGenerator: ProtocolGenerator,
variableGenerator: VariableGenerator,
functionGenerator: FunctionGenerator,
accessLevel: AccessLevel,
accessLevelGenerator: AccessLevelGenerator
) {
self.protocolGenerator = protocolGenerator
self.variableGenerator = variableGenerator
self.functionGenerator = functionGenerator
self.accessLevel = accessLevel
self.accessLevelGenerator = accessLevelGenerator
}

func generate(from protocolType: ProtocolDeclaration) -> String {
let content = generateBody(from: protocolType)
return protocolGenerator.makeProtocol(
from: protocolType,
stub: "Dummy",
content: content.joined(separator: "\n")
)
}

private func generateBody(from protocolType: ProtocolDeclaration) -> [String] {
var content = [String]()

if !protocolType.variables.isEmpty {
content += protocolType.variables.map{
variableGenerator.makeStubCode(
from: $0,
getContent: "\t\t\tfatalError()",
setContent: "\t\t\tfatalError()"
)
}
content.append("")
}

let initRows = generateInit(for: protocolType)

if !initRows.isEmpty {
content.append(initRows)
}

content += protocolType.functions.map {
functionGenerator.makeCode(from: $0, content: "\t\tfatalError()") + "\n"
}

return content
}

private func generateInit(for type: ProtocolDeclaration) -> String {
guard accessLevel == .public else {
return ""
}

return """
\t\(accessLevelGenerator.makeContentAccessLevel())init() {
\t}
"""
}
}
47 changes: 47 additions & 0 deletions Rubicon/Sources/Rubicon/Generator/FunctionGenerator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
protocol FunctionGenerator {
func makeCode(from declaration: FunctionDeclaration, content: String) -> String
}

final class FunctionGeneratorImpl: FunctionGenerator {
private let accessLevelGenerator: AccessLevelGenerator
private let typeGenerator: TypeGenerator
private let argumentGenerator: ArgumentGenerator

init(
accessLevelGenerator: AccessLevelGenerator,
typeGenerator: TypeGenerator,
argumentGenerator: ArgumentGenerator
) {
self.accessLevelGenerator = accessLevelGenerator
self.typeGenerator = typeGenerator
self.argumentGenerator = argumentGenerator
}

func makeCode(from declaration: FunctionDeclaration, content: String) -> String {
let returnString = makeReturn(from: declaration)
let arguments = declaration.arguments.map(argumentGenerator.makeCode(from:)).joined(separator: ", ")
return """
\t\(accessLevelGenerator.makeContentAccessLevel())func \(declaration.name)(\(arguments)) \(returnString){
\(content)
\t}
"""
}

private func makeReturn(from declaration: FunctionDeclaration) -> String {
var result = ""

if declaration.isAsync {
result += "async "
}

if declaration.isThrowing {
result += "throws "
}

if let returnType = declaration.returnType {
result += "-> \(typeGenerator.makeVariableCode(from: returnType)) "
}

return result
}
}
5 changes: 5 additions & 0 deletions Rubicon/Sources/Rubicon/Generator/TypeGenerator.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
protocol TypeGenerator {
func makeVariableCode(from declaration: TypeDeclaration) -> String
func makeArgumentCode(from declaration: TypeDeclaration) -> String
}

final class TypeGeneratorImpl: TypeGenerator {
func makeVariableCode(from declaration: TypeDeclaration) -> String {
return declaration.name
}

func makeArgumentCode(from declaration: TypeDeclaration) -> String {
return (declaration.prefix.map(\.rawValue) + [declaration.name]).joined(separator: " ")
}
}
20 changes: 9 additions & 11 deletions Rubicon/Sources/Rubicon/Generator/VariableGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,21 @@ final class VariableGeneratorImpl: VariableGenerator {
if declaration.isConstant {
return """
\t\(accessLevelGenerator.makeContentAccessLevel())var \(declaration.identifier): \(typeGenerator.makeVariableCode(from: declaration.type)) {
\tget {
\t\tgetContent
\t\tget {
\(getContent)
\t\t}
\t}
}
"""
} else {
return """
\t\(accessLevelGenerator.makeContentAccessLevel())var \(declaration.identifier): \(typeGenerator.makeVariableCode(from: declaration.type)) {
\tget {
\t\tgetContent
\t}
\tset {
\t\tsetContent
\t\tget {
\(getContent)
\t\t}
\t\tset {
\(setContent)
\t\t}
\t}
}
"""
}
}
Expand Down
Loading

0 comments on commit 7fd6219

Please sign in to comment.