|
| 1 | +// |
| 2 | +// FunctionRequirement.swift |
| 3 | +// SyntaxKit |
| 4 | +// |
| 5 | +// Created by Leo Dion. |
| 6 | +// Copyright © 2025 BrightDigit. |
| 7 | +// |
| 8 | +// Permission is hereby granted, free of charge, to any person |
| 9 | +// obtaining a copy of this software and associated documentation |
| 10 | +// files (the “Software”), to deal in the Software without |
| 11 | +// restriction, including without limitation the rights to use, |
| 12 | +// copy, modify, merge, publish, distribute, sublicense, and/or |
| 13 | +// sell copies of the Software, and to permit persons to whom the |
| 14 | +// Software is furnished to do so, subject to the following |
| 15 | +// conditions: |
| 16 | +// |
| 17 | +// The above copyright notice and this permission notice shall be |
| 18 | +// included in all copies or substantial portions of the Software. |
| 19 | +// |
| 20 | +// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, |
| 21 | +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 22 | +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 23 | +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 24 | +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 25 | +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 26 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 27 | +// OTHER DEALINGS IN THE SOFTWARE. |
| 28 | +// |
| 29 | + |
| 30 | +import SwiftSyntax |
| 31 | + |
| 32 | +/// A function requirement within a protocol declaration (no body). |
| 33 | +public struct FunctionRequirement: CodeBlock { |
| 34 | + private let name: String |
| 35 | + private let parameters: [Parameter] |
| 36 | + private let returnType: String? |
| 37 | + private var isStatic: Bool = false |
| 38 | + private var isMutating: Bool = false |
| 39 | + |
| 40 | + /// Creates a parameterless function requirement. |
| 41 | + /// - Parameters: |
| 42 | + /// - name: The function name. |
| 43 | + /// - returnType: Optional return type. |
| 44 | + public init(_ name: String, returns returnType: String? = nil) { |
| 45 | + self.name = name |
| 46 | + self.parameters = [] |
| 47 | + self.returnType = returnType |
| 48 | + } |
| 49 | + |
| 50 | + /// Creates a function requirement with parameters. |
| 51 | + /// - Parameters: |
| 52 | + /// - name: The function name. |
| 53 | + /// - returnType: Optional return type. |
| 54 | + /// - params: A ParameterBuilderResult providing the parameters. |
| 55 | + public init( |
| 56 | + _ name: String, returns returnType: String? = nil, |
| 57 | + @ParameterBuilderResult _ params: () -> [Parameter] |
| 58 | + ) { |
| 59 | + self.name = name |
| 60 | + self.parameters = params() |
| 61 | + self.returnType = returnType |
| 62 | + } |
| 63 | + |
| 64 | + /// Marks the function requirement as `static`. |
| 65 | + public func `static`() -> Self { |
| 66 | + var copy = self |
| 67 | + copy.isStatic = true |
| 68 | + return copy |
| 69 | + } |
| 70 | + |
| 71 | + /// Marks the function requirement as `mutating`. |
| 72 | + public func mutating() -> Self { |
| 73 | + var copy = self |
| 74 | + copy.isMutating = true |
| 75 | + return copy |
| 76 | + } |
| 77 | + |
| 78 | + public var syntax: SyntaxProtocol { |
| 79 | + let funcKeyword = TokenSyntax.keyword(.func, trailingTrivia: .space) |
| 80 | + let identifier = TokenSyntax.identifier(name) |
| 81 | + |
| 82 | + // Parameters |
| 83 | + let paramList: FunctionParameterListSyntax |
| 84 | + if parameters.isEmpty { |
| 85 | + paramList = FunctionParameterListSyntax([]) |
| 86 | + } else { |
| 87 | + paramList = FunctionParameterListSyntax( |
| 88 | + parameters.enumerated().compactMap { index, param in |
| 89 | + guard !param.name.isEmpty, !param.type.isEmpty else { return nil } |
| 90 | + var paramSyntax = FunctionParameterSyntax( |
| 91 | + firstName: param.isUnnamed |
| 92 | + ? .wildcardToken(trailingTrivia: .space) : .identifier(param.name), |
| 93 | + secondName: param.isUnnamed ? .identifier(param.name) : nil, |
| 94 | + colon: .colonToken(leadingTrivia: .space, trailingTrivia: .space), |
| 95 | + type: IdentifierTypeSyntax(name: .identifier(param.type)), |
| 96 | + defaultValue: param.defaultValue.map { |
| 97 | + InitializerClauseSyntax( |
| 98 | + equal: .equalToken(leadingTrivia: .space, trailingTrivia: .space), |
| 99 | + value: ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier($0))) |
| 100 | + ) |
| 101 | + } |
| 102 | + ) |
| 103 | + if index < parameters.count - 1 { |
| 104 | + paramSyntax = paramSyntax.with(\.trailingComma, .commaToken(trailingTrivia: .space)) |
| 105 | + } |
| 106 | + return paramSyntax |
| 107 | + }) |
| 108 | + } |
| 109 | + |
| 110 | + // Return clause |
| 111 | + var returnClause: ReturnClauseSyntax? |
| 112 | + if let returnType = returnType { |
| 113 | + returnClause = ReturnClauseSyntax( |
| 114 | + arrow: .arrowToken(leadingTrivia: .space, trailingTrivia: .space), |
| 115 | + type: IdentifierTypeSyntax(name: .identifier(returnType)) |
| 116 | + ) |
| 117 | + } |
| 118 | + |
| 119 | + // Modifiers |
| 120 | + var modifiers: DeclModifierListSyntax = [] |
| 121 | + if isStatic { |
| 122 | + modifiers = DeclModifierListSyntax([ |
| 123 | + DeclModifierSyntax(name: .keyword(.static, trailingTrivia: .space)) |
| 124 | + ]) |
| 125 | + } |
| 126 | + if isMutating { |
| 127 | + modifiers = DeclModifierListSyntax( |
| 128 | + modifiers + [DeclModifierSyntax(name: .keyword(.mutating, trailingTrivia: .space))] |
| 129 | + ) |
| 130 | + } |
| 131 | + |
| 132 | + return FunctionDeclSyntax( |
| 133 | + attributes: AttributeListSyntax([]), |
| 134 | + modifiers: modifiers, |
| 135 | + funcKeyword: funcKeyword, |
| 136 | + name: identifier, |
| 137 | + signature: FunctionSignatureSyntax( |
| 138 | + parameterClause: FunctionParameterClauseSyntax( |
| 139 | + leftParen: .leftParenToken(), parameters: paramList, rightParen: .rightParenToken() |
| 140 | + ), |
| 141 | + effectSpecifiers: nil, |
| 142 | + returnClause: returnClause |
| 143 | + ), |
| 144 | + body: nil |
| 145 | + ) |
| 146 | + } |
| 147 | +} |
0 commit comments