@@ -97,7 +97,6 @@ let captureAssociatedTypeName = "Capture"
97
97
let patternBuilderTypeName = " RegexBuilder "
98
98
let patternProtocolRequirementName = " regex "
99
99
let PatternTypeBaseName = " Regex "
100
- let emptyProtocolName = " EmptyCaptureProtocol "
101
100
let baseMatchTypeName = " Substring "
102
101
103
102
@main
@@ -128,12 +127,17 @@ struct VariadicsGenerator: ParsableCommand {
128
127
129
128
""" )
130
129
131
- for arity in 2 ... maxArity+ 1 {
132
- emitTupleStruct ( arity: arity)
130
+ print ( " Generating 'buildBlock(_:)' overloads... " , to: & standardError)
131
+ for arity in 1 ..< maxArity {
132
+ print ( " Capture arity: \( arity) " , to: & standardError)
133
+ emitUnaryBuildBlock ( arity: arity)
133
134
}
134
135
135
136
print ( " Generating concatenation overloads... " , to: & standardError)
136
137
for (leftArity, rightArity) in Permutations ( totalArity: maxArity) {
138
+ guard rightArity != 0 else {
139
+ continue
140
+ }
137
141
print (
138
142
" Left arity: \( leftArity) Right arity: \( rightArity) " ,
139
143
to: & standardError)
@@ -168,73 +172,22 @@ struct VariadicsGenerator: ParsableCommand {
168
172
if arity == 0 {
169
173
return genericParameters ( )
170
174
}
171
- return " Tuple \( arity ) < \( genericParameters ( ) ) > "
175
+ return " ( \( genericParameters ( ) ) ) "
172
176
}
173
177
174
- func emitTupleStruct( arity: Int ) {
175
- output ( """
176
- @frozen @dynamicMemberLookup
177
- public struct Tuple \( arity) <
178
- """ )
179
- outputForEach ( 0 ..< arity, separator: " , " ) {
180
- " _ \( $0) "
181
- }
182
- output ( " > { " )
183
- // `public typealias Tuple = (_0, ...)`
184
- output ( " \n public typealias Tuple = ( " )
185
- outputForEach ( 0 ..< arity, separator: " , " ) { " _ \( $0) " }
186
- output ( " ) " )
187
- // `public var tuple: Tuple`
188
- output ( " \n public var tuple: Tuple \n " )
189
- // `subscript(dynamicMember:)`
178
+ func emitUnaryBuildBlock( arity: Int ) {
179
+ assert ( arity > 0 )
180
+ let captureTypes = ( 0 ..< arity) . map { " C \( $0) " } . joined ( separator: " , " )
190
181
output ( """
191
- public subscript<T>(dynamicMember keyPath: WritableKeyPath<Tuple, T>) -> T {
192
- get { tuple[keyPath: keyPath] }
193
- _modify { yield &tuple[keyPath: keyPath] }
182
+ extension RegexBuilder {
183
+ public static func buildBlock<R: RegexProtocol, W, \( captureTypes) >(_ regex: R) -> R
184
+ where R.Match == (W, \( captureTypes) )
185
+ {
186
+ regex
194
187
}
195
- """ )
196
- output ( " \n } \n " )
197
- output ( " extension Tuple \( arity) : \( emptyProtocolName) where " )
198
- outputForEach ( 1 ..< arity, separator: " , " ) {
199
- " _ \( $0) : \( emptyProtocolName) "
200
- }
201
- output ( " {} \n " )
202
- output ( " extension Tuple \( arity) : MatchProtocol { \n " )
203
- output ( " public typealias Capture = " )
204
- if arity == 2 {
205
- output ( " _1 " )
206
- } else {
207
- output ( " Tuple \( arity- 1 ) < " )
208
- outputForEach ( 1 ..< arity, separator: " , " ) {
209
- " _ \( $0) "
210
188
}
211
- output ( " > " )
212
- }
213
- output ( " \n public init(_ tuple: Tuple) { self.tuple = tuple } " )
214
- // `public init(_0: _0, ...) { ... }`
215
- output ( " \n public init( " )
216
- outputForEach ( 0 ..< arity, separator: " , " ) {
217
- " _ _ \( $0) : _ \( $0) "
218
- }
219
- output ( " ) { \n " )
220
- output ( " self.init(( " )
221
- outputForEach ( 0 ..< arity, separator: " , " ) { " _ \( $0) " }
222
- output ( " )) \n " )
223
- output ( " } " )
224
- output ( " \n } \n " )
225
- // Equatable
226
- output ( " extension Tuple \( arity) : Equatable where " )
227
- outputForEach ( 0 ..< arity, separator: " , " ) {
228
- " _ \( $0) : Equatable "
229
- }
230
- output ( " { \n " )
231
- output ( " public static func == (lhs: Self, rhs: Self) -> Bool { \n " )
232
- output ( " " )
233
- outputForEach ( 0 ..< arity, separator: " && " ) {
234
- " lhs.tuple. \( $0) == rhs.tuple. \( $0) "
235
- }
236
- output ( " \n } \n " )
237
- output ( " } \n " )
189
+
190
+ """ )
238
191
}
239
192
240
193
func emitConcatenation( leftArity: Int , rightArity: Int ) {
@@ -254,11 +207,11 @@ struct VariadicsGenerator: ParsableCommand {
254
207
// Emit concatenation type declaration.
255
208
256
209
// public struct Concatenation2<W0, W1, C0, C1, R0: RegexProtocol, R1: RegexProtocol>: RegexProtocol
257
- // where R0.Match == Tuple2< W0, C0> , R1.Match == Tuple2< W1, C1>
210
+ // where R0.Match == ( W0, C0) , R1.Match == ( W1, C1)
258
211
// {
259
- // public typealias Match = Tuple3< Substring, C0, C1>
212
+ // public typealias Match = ( Substring, C0, C1)
260
213
//
261
- // public let regex: Regex<Tuple3< Substring, C0, C1> >
214
+ // public let regex: Regex<( Substring, C0, C1) >
262
215
//
263
216
// public init(_ r0: R0, _ r1: R1) {
264
217
// self.regex = .init(node: r0.regex.root.appending(r1.regex.root))
@@ -288,32 +241,32 @@ struct VariadicsGenerator: ParsableCommand {
288
241
if leftArity == 0 {
289
242
output ( " W0 " )
290
243
} else {
291
- output ( " Tuple \( leftArity + 1 ) < W0" )
244
+ output ( " ( W0" )
292
245
outputForEach ( 0 ..< leftArity) {
293
246
" , C \( $0) "
294
247
}
295
- output ( " > " )
248
+ output ( " ) " )
296
249
}
297
250
output ( " , R1.Match == " )
298
251
if rightArity == 0 {
299
252
output ( " W1 " )
300
253
} else {
301
- output ( " Tuple \( rightArity + 1 ) < W1" )
254
+ output ( " ( W1" )
302
255
outputForEach ( leftArity..< leftArity+ rightArity) {
303
256
" , C \( $0) "
304
257
}
305
- output ( " > " )
258
+ output ( " ) " )
306
259
}
307
260
output ( " { \n " )
308
261
output ( " public typealias \( matchAssociatedTypeName) = " )
309
262
if leftArity+ rightArity == 0 {
310
263
output ( baseMatchTypeName)
311
264
} else {
312
- output ( " Tuple \( leftArity + rightArity + 1 ) < \( baseMatchTypeName) , " )
265
+ output ( " ( \( baseMatchTypeName) , " )
313
266
outputForEach ( 0 ..< leftArity+ rightArity, separator: " , " ) {
314
267
" C \( $0) "
315
268
}
316
- output ( " > " )
269
+ output ( " ) " )
317
270
}
318
271
output ( " \n " )
319
272
output ( " public let \( patternProtocolRequirementName) : \( PatternTypeBaseName) < \( matchAssociatedTypeName) > \n " )
@@ -351,6 +304,7 @@ struct VariadicsGenerator: ParsableCommand {
351
304
// T + () = T
352
305
output ( """
353
306
extension RegexBuilder {
307
+ @_disfavoredOverload
354
308
public static func buildBlock<W0
355
309
""" )
356
310
outputForEach ( 0 ..< leftArity) {
@@ -364,24 +318,24 @@ struct VariadicsGenerator: ParsableCommand {
364
318
if leftArity == 0 {
365
319
output ( baseMatchTypeName)
366
320
} else {
367
- output ( " Tuple \( leftArity + 1 ) < \( baseMatchTypeName) " )
321
+ output ( " ( \( baseMatchTypeName) " )
368
322
outputForEach ( 0 ..< leftArity) {
369
323
" , C \( $0) "
370
324
}
371
- output ( " > " )
325
+ output ( " ) " )
372
326
}
373
327
output ( " > where R0. \( matchAssociatedTypeName) == " )
374
328
if leftArity == 0 {
375
329
output ( " W0 " )
376
330
} else {
377
- output ( " Tuple \( leftArity + 1 ) < W0" )
331
+ output ( " ( W0" )
378
332
outputForEach ( 0 ..< leftArity) {
379
333
" , C \( $0) "
380
334
}
381
- output ( " > " )
335
+ output ( " ) " )
382
336
}
383
337
output ( """
384
- , R1. \( matchAssociatedTypeName ) : \( emptyProtocolName ) {
338
+ {
385
339
.init(node: combined.regex.root.appending(next.regex.root))
386
340
}
387
341
}
@@ -435,9 +389,9 @@ struct VariadicsGenerator: ParsableCommand {
435
389
return result
436
390
}
437
391
let captures = ( 0 ..< arity) . map { " C \( $0) " } . joined ( separator: " , " )
438
- let capturesTupled = arity == 1 ? captures : " Tuple \( arity ) < \( captures) > "
392
+ let capturesTupled = arity == 1 ? captures : " ( \( captures) ) "
439
393
let componentConstraint : String = arity == 0 ? " " :
440
- " where Component.Match == Tuple \( arity + 1 ) < W, \( captures) > "
394
+ " where Component.Match == ( W, \( captures) ) "
441
395
let quantifiedCaptures : String = {
442
396
switch kind {
443
397
case . zeroOrOne:
@@ -446,7 +400,7 @@ struct VariadicsGenerator: ParsableCommand {
446
400
return " [ \( capturesTupled) ] "
447
401
}
448
402
} ( )
449
- let matchType = arity == 0 ? baseMatchTypeName : " Tuple2< \( baseMatchTypeName) , \( quantifiedCaptures) > "
403
+ let matchType = arity == 0 ? baseMatchTypeName : " ( \( baseMatchTypeName) , \( quantifiedCaptures) ) "
450
404
output ( """
451
405
public struct \( kind. typeName) _ \( arity) < \( genericParameters ( withConstraints: true ) ) >: \( regexProtocolName) \( componentConstraint) {
452
406
public typealias \( matchAssociatedTypeName) = \( matchType)
0 commit comments