|
| 1 | +// |
| 2 | +// TupleAssignment.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 tuple destructuring pattern for variable declarations. |
| 33 | +public struct TupleAssignment: CodeBlock { |
| 34 | + private let elements: [String] |
| 35 | + private let value: CodeBlock |
| 36 | + private var isAsync: Bool = false |
| 37 | + private var isThrowing: Bool = false |
| 38 | + |
| 39 | + /// Creates a tuple destructuring declaration. |
| 40 | + /// - Parameters: |
| 41 | + /// - elements: The names of the variables to destructure into. |
| 42 | + /// - value: The expression to destructure. |
| 43 | + public init(_ elements: [String], equals value: CodeBlock) { |
| 44 | + self.elements = elements |
| 45 | + self.value = value |
| 46 | + } |
| 47 | + |
| 48 | + /// Marks this destructuring as async. |
| 49 | + /// - Returns: A copy of the destructuring marked as async. |
| 50 | + public func async() -> Self { |
| 51 | + var copy = self |
| 52 | + copy.isAsync = true |
| 53 | + return copy |
| 54 | + } |
| 55 | + |
| 56 | + /// Marks this destructuring as throwing. |
| 57 | + /// - Returns: A copy of the destructuring marked as throwing. |
| 58 | + public func throwing() -> Self { |
| 59 | + var copy = self |
| 60 | + copy.isThrowing = true |
| 61 | + return copy |
| 62 | + } |
| 63 | + |
| 64 | + public var syntax: SyntaxProtocol { |
| 65 | + // Build the tuple pattern |
| 66 | + let patternElements = TuplePatternElementListSyntax( |
| 67 | + elements.enumerated().map { index, element in |
| 68 | + TuplePatternElementSyntax( |
| 69 | + label: nil, |
| 70 | + colon: nil, |
| 71 | + pattern: PatternSyntax(IdentifierPatternSyntax(identifier: .identifier(element))), |
| 72 | + trailingComma: index < elements.count - 1 ? .commaToken(trailingTrivia: .space) : nil |
| 73 | + ) |
| 74 | + } |
| 75 | + ) |
| 76 | + |
| 77 | + let tuplePattern = PatternSyntax( |
| 78 | + TuplePatternSyntax( |
| 79 | + leftParen: .leftParenToken(), |
| 80 | + elements: patternElements, |
| 81 | + rightParen: .rightParenToken() |
| 82 | + ) |
| 83 | + ) |
| 84 | + |
| 85 | + // Build the value expression |
| 86 | + let valueExpr: ExprSyntax |
| 87 | + if isThrowing { |
| 88 | + valueExpr = ExprSyntax( |
| 89 | + TryExprSyntax( |
| 90 | + tryKeyword: .keyword(.try, trailingTrivia: .space), |
| 91 | + expression: isAsync |
| 92 | + ? ExprSyntax( |
| 93 | + AwaitExprSyntax( |
| 94 | + awaitKeyword: .keyword(.await, trailingTrivia: .space), |
| 95 | + expression: value.syntax.as(ExprSyntax.self) |
| 96 | + ?? ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier(""))) |
| 97 | + )) |
| 98 | + : value.syntax.as(ExprSyntax.self) |
| 99 | + ?? ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier(""))) |
| 100 | + ) |
| 101 | + ) |
| 102 | + } else if isAsync { |
| 103 | + valueExpr = ExprSyntax( |
| 104 | + AwaitExprSyntax( |
| 105 | + awaitKeyword: .keyword(.await, trailingTrivia: .space), |
| 106 | + expression: value.syntax.as(ExprSyntax.self) |
| 107 | + ?? ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier(""))) |
| 108 | + ) |
| 109 | + ) |
| 110 | + } else { |
| 111 | + valueExpr = |
| 112 | + value.syntax.as(ExprSyntax.self) |
| 113 | + ?? ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier(""))) |
| 114 | + } |
| 115 | + |
| 116 | + // Build the variable declaration |
| 117 | + return VariableDeclSyntax( |
| 118 | + bindingSpecifier: .keyword(.let, trailingTrivia: .space), |
| 119 | + bindings: PatternBindingListSyntax([ |
| 120 | + PatternBindingSyntax( |
| 121 | + pattern: tuplePattern, |
| 122 | + initializer: InitializerClauseSyntax( |
| 123 | + equal: .equalToken(leadingTrivia: .space, trailingTrivia: .space), |
| 124 | + value: valueExpr |
| 125 | + ) |
| 126 | + ) |
| 127 | + ]) |
| 128 | + ) |
| 129 | + } |
| 130 | +} |
0 commit comments