|
| 1 | +# ``SyntaxKit`` |
| 2 | + |
| 3 | +SyntaxKit provides a declarative way to generate Swift code structures using SwiftSyntax. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +SyntaxKit allows developers to build Swift code using result builders which enable the creation of Swift code structures in a declarative way. Here's an example: |
| 8 | + |
| 9 | +```swift |
| 10 | +import SyntaxKit |
| 11 | + |
| 12 | +let code = Struct("BlackjackCard") { |
| 13 | + Enum("Suit") { |
| 14 | + EnumCase("spades").equals("♠") |
| 15 | + EnumCase("hearts").equals("♡") |
| 16 | + EnumCase("diamonds").equals("♢") |
| 17 | + EnumCase("clubs").equals("♣") |
| 18 | + } |
| 19 | + .inherits("Character") |
| 20 | + .comment("nested Suit enumeration") |
| 21 | +} |
| 22 | + |
| 23 | +let generatedCode = code.generateCode() |
| 24 | +``` |
| 25 | + |
| 26 | +This will generate the following Swift code: |
| 27 | + |
| 28 | +```swift |
| 29 | +struct BlackjackCard { |
| 30 | + // nested Suit enumeration |
| 31 | + enum Suit: Character { |
| 32 | + case spades = "♠" |
| 33 | + case hearts = "♡" |
| 34 | + case diamonds = "♢" |
| 35 | + case clubs = "♣" |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +## Full Example |
| 41 | + |
| 42 | +Here is a more comprehensive example that demonstrates many of SyntaxKit's features to generate a `BlackjackCard` struct. |
| 43 | + |
| 44 | +### DSL Code |
| 45 | + |
| 46 | +```swift |
| 47 | +import SyntaxKit |
| 48 | + |
| 49 | +let structExample = Struct("BlackjackCard") { |
| 50 | + Enum("Suit") { |
| 51 | + EnumCase("spades").equals("♠") |
| 52 | + EnumCase("hearts").equals("♡") |
| 53 | + EnumCase("diamonds").equals("♢") |
| 54 | + EnumCase("clubs").equals("♣") |
| 55 | + } |
| 56 | + .inherits("Character") |
| 57 | + .comment("nested Suit enumeration") |
| 58 | + |
| 59 | + Enum("Rank") { |
| 60 | + EnumCase("two").equals(2) |
| 61 | + EnumCase("three") |
| 62 | + EnumCase("four") |
| 63 | + EnumCase("five") |
| 64 | + EnumCase("six") |
| 65 | + EnumCase("seven") |
| 66 | + EnumCase("eight") |
| 67 | + EnumCase("nine") |
| 68 | + EnumCase("ten") |
| 69 | + EnumCase("jack") |
| 70 | + EnumCase("queen") |
| 71 | + EnumCase("king") |
| 72 | + EnumCase("ace") |
| 73 | + |
| 74 | + Struct("Values") { |
| 75 | + Variable(.let, name: "first", type: "Int") |
| 76 | + Variable(.let, name: "second", type: "Int?") |
| 77 | + } |
| 78 | + |
| 79 | + ComputedProperty("values") { |
| 80 | + Switch("self") { |
| 81 | + SwitchCase(".ace") { |
| 82 | + Return { |
| 83 | + Init("Values") { |
| 84 | + Parameter(name: "first", value: "1") |
| 85 | + Parameter(name: "second", value: "11") |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + SwitchCase(".jack", ".queen", ".king") { |
| 90 | + Return { |
| 91 | + Init("Values") { |
| 92 | + Parameter(name: "first", value: "10") |
| 93 | + Parameter(name: "second", value: "nil") |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + Default { |
| 98 | + Return { |
| 99 | + Init("Values") { |
| 100 | + Parameter(name: "first", value: "self.rawValue") |
| 101 | + Parameter(name: "second", value: "nil") |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + .inherits("Int") |
| 109 | + .comment("nested Rank enumeration") |
| 110 | + |
| 111 | + Variable(.let, name: "rank", type: "Rank") |
| 112 | + Variable(.let, name: "suit", type: "Suit") |
| 113 | + .comment("BlackjackCard properties and methods") |
| 114 | + |
| 115 | + ComputedProperty("description") { |
| 116 | + VariableDecl(.var, name: "output", equals: "\"suit is \\(suit.rawValue),\"") |
| 117 | + PlusAssign("output", "\" value is \\(rank.values.first)\"") |
| 118 | + If(Let("second", "rank.values.second"), then: { |
| 119 | + PlusAssign("output", "\" or \\(second)\"") |
| 120 | + }) |
| 121 | + Return { |
| 122 | + VariableExp("output") |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +### Generated Code |
| 129 | + |
| 130 | +```swift |
| 131 | +import Foundation |
| 132 | + |
| 133 | +struct BlackjackCard { |
| 134 | + // nested Suit enumeration |
| 135 | + enum Suit: Character { |
| 136 | + case spades = "♠" |
| 137 | + case hearts = "♡" |
| 138 | + case diamonds = "♢" |
| 139 | + case clubs = "♣" |
| 140 | + } |
| 141 | + |
| 142 | + // nested Rank enumeration |
| 143 | + enum Rank: Int { |
| 144 | + case two = 2 |
| 145 | + case three |
| 146 | + case four |
| 147 | + case five |
| 148 | + case six |
| 149 | + case seven |
| 150 | + case eight |
| 151 | + case nine |
| 152 | + case ten |
| 153 | + case jack |
| 154 | + case queen |
| 155 | + case king |
| 156 | + case ace |
| 157 | + |
| 158 | + struct Values { |
| 159 | + let first: Int, second: Int? |
| 160 | + } |
| 161 | + |
| 162 | + var values: Values { |
| 163 | + switch self { |
| 164 | + case .ace: |
| 165 | + return Values(first: 1, second: 11) |
| 166 | + case .jack, .queen, .king: |
| 167 | + return Values(first: 10, second: nil) |
| 168 | + default: |
| 169 | + return Values(first: self.rawValue, second: nil) |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + // BlackjackCard properties and methods |
| 175 | + let rank: Rank |
| 176 | + let suit: Suit |
| 177 | + var description: String { |
| 178 | + var output = "suit is \\(suit.rawValue)," |
| 179 | + output += " value is \\(rank.values.first)" |
| 180 | + if let second = rank.values.second { |
| 181 | + output += " or \\(second)" |
| 182 | + } |
| 183 | + return output |
| 184 | + } |
| 185 | +} |
| 186 | +``` |
| 187 | + |
| 188 | +## Topics |
| 189 | + |
| 190 | +### Declarations |
| 191 | + |
| 192 | +- ``Struct`` |
| 193 | +- ``Enum`` |
| 194 | +- ``EnumCase`` |
| 195 | +- ``Function`` |
| 196 | +- ``Init`` |
| 197 | +- ``ComputedProperty`` |
| 198 | +- ``VariableDecl`` |
| 199 | +- ``Let`` |
| 200 | +- ``Variable`` |
| 201 | + |
| 202 | +### Expressions & Statements |
| 203 | +- ``Assignment`` |
| 204 | +- ``PlusAssign`` |
| 205 | +- ``Return`` |
| 206 | +- ``VariableExp`` |
| 207 | + |
| 208 | +### Control Flow |
| 209 | +- ``If`` |
| 210 | +- ``Switch`` |
| 211 | +- ``SwitchCase`` |
| 212 | +- ``Default`` |
| 213 | + |
| 214 | +### Building Blocks |
| 215 | +- ``CodeBlock`` |
| 216 | +- ``Parameter`` |
| 217 | +- ``Literal`` |
| 218 | + |
0 commit comments