-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode.swift
More file actions
225 lines (188 loc) · 5.8 KB
/
code.swift
File metadata and controls
225 lines (188 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import Foundation
// MARK: - Macro Tutorial Examples
// This file demonstrates the concepts from the "Creating Macros with SyntaxKit" tutorial
// MARK: - Example 1: Extension Macro Usage
// This shows how the @MyMacro would be used (as defined in the tutorial)
protocol MyProtocol {
var description: String { get }
}
// Example of what the macro would generate:
@MyMacro
enum Color: String {
case red = "red"
case green = "green"
case blue = "blue"
}
// The macro would generate:
// extension Color: MyProtocol {
// typealias MyType = String
// static let myProperty = ["red", "green", "blue"]
// var description: String {
// return myProperty.joined(separator: ", ")
// }
// }
//
// struct ColorWrapper {
// let value: Color
// init(value: Color) {
// self.value = value
// }
// var description: String {
// return value.description
// }
// }
// MARK: - Example 2: Freestanding Expression Macro Usage
// This shows how the #stringify macro would be used
// let result = #stringify(42 + 8)
// result would be (50, "42 + 8")
// MARK: - Example 3: Complex Macro Generation
// This shows what a complex macro might generate
struct User {
var status: UserStatus
var name: String
}
enum UserStatus: String {
case active = "active"
case inactive = "inactive"
case pending = "pending"
}
// A complex macro might generate an extension like this:
extension User {
enum Status: String {
case active = "active"
case inactive = "inactive"
case pending = "pending"
}
var isValid: Bool {
if status == .active {
return true
} else {
return false
}
}
func updateStatus(newStatus: Status) {
status = newStatus
print("Status updated to \(newStatus)")
}
static func createDefault() -> User {
return User(status: .pending, name: "Default")
}
}
// MARK: - Example 4: SyntaxKit Code Generation
// This shows how SyntaxKit would be used to generate the above code
import SyntaxKit
// Example of generating the User extension using SyntaxKit
let userExtension = Extension("User") {
// Add a nested enum
Enum("Status") {
EnumCase("active").equals("active")
EnumCase("inactive").equals("inactive")
EnumCase("pending").equals("pending")
}.inherits("String")
// Add a computed property with complex logic
ComputedProperty("isValid") {
If(VariableExp("status == .active"), then: {
Return { Literal.boolean(true) }
}, else: {
Return { Literal.boolean(false) }
})
}
// Add a method with parameters
Function("updateStatus", parameters: [Parameter("newStatus", type: "Status")]) {
Assignment("status", VariableExp("newStatus"))
Call("print") {
ParameterExp(unlabeled: "\"Status updated to \\(newStatus)\"")
}
}
// Add a static method
Function("createDefault", parameters: []) {
Return {
Init("User") {
Parameter(name: "status", value: ".pending")
Parameter(name: "name", value: "\"Default\"")
}
}
}.static()
}.inherits("Identifiable", "Codable")
// MARK: - Example 5: Error Handling in Macros
// This shows how macros should handle errors
enum MacroError: Error, CustomStringConvertible {
case onlyWorksWithEnums
case invalidCaseName(String)
case missingRawValue
var description: String {
switch self {
case .onlyWorksWithEnums:
return "This macro can only be applied to enums"
case .invalidCaseName(let name):
return "Invalid case name: \(name)"
case .missingRawValue:
return "Enum cases must have raw values"
}
}
}
// MARK: - Example 6: Testing Macros
// This shows how macros should be tested
/*
// Example test for the MyMacro
func testExtensionMacro() throws {
assertMacroExpansion(
"""
@MyMacro
enum Color: String {
case red = "red"
case blue = "blue"
}
""",
expandedSource: """
enum Color: String {
case red = "red"
case blue = "blue"
}
extension Color: MyProtocol {
typealias MyType = String
static let myProperty = ["red", "blue"]
var description: String {
return myProperty.joined(separator: ", ")
}
}
struct ColorWrapper {
let value: Color
init(value: Color) {
self.value = value
}
var description: String {
return value.description
}
}
""",
macros: ["MyMacro": MyMacro.self]
)
}
*/
// MARK: - Example 7: Integration with SwiftSyntax
// This shows how SyntaxKit can be mixed with raw SwiftSyntax
/*
// You can mix SyntaxKit with raw SwiftSyntax
let syntaxKitStruct = Struct("Generated") {
Variable(.let, name: "value", type: "String")
}
// Convert to SwiftSyntax and modify
var structDecl = syntaxKitStruct.syntax.as(StructDeclSyntax.self)!
structDecl = structDecl.with(\.modifiers, DeclModifierListSyntax {
DeclModifierSyntax(name: .keyword(.public))
})
// Convert back to SyntaxKit if needed
let modifiedStruct = Struct(structDecl)
*/
// MARK: - Usage Examples
// Example usage of the generated code
let color = Color.red
print(color.description) // Would print: "red, green, blue"
let user = User(status: .pending, name: "John")
print(user.isValid) // Would print: false
user.updateStatus(newStatus: .active)
print(user.isValid) // Would print: true
let defaultUser = User.createDefault()
print(defaultUser.name) // Would print: "Default"
print(defaultUser.status) // Would print: .pending